|
| 1 | +import sys |
| 2 | +import uuid |
| 3 | +import mlagents_envs |
| 4 | + |
| 5 | +from mlagents_envs.exception import UnityCommunicationException |
| 6 | +from mlagents_envs.side_channel import SideChannel, IncomingMessage, OutgoingMessage |
| 7 | +from mlagents_envs.communicator_objects.training_analytics_pb2 import ( |
| 8 | + TrainingEnvironmentInitialized, |
| 9 | +) |
| 10 | +from google.protobuf.any_pb2 import Any |
| 11 | + |
| 12 | + |
| 13 | +class DefaultTrainingAnalyticsSideChannel(SideChannel): |
| 14 | + """ |
| 15 | + Side channel that sends information about the training to the Unity environment so it can be logged. |
| 16 | + """ |
| 17 | + |
| 18 | + CHANNEL_ID = uuid.UUID("b664a4a9-d86f-5a5f-95cb-e8353a7e8356") |
| 19 | + |
| 20 | + def __init__(self) -> None: |
| 21 | + # >>> uuid.uuid5(uuid.NAMESPACE_URL, "com.unity.ml-agents/TrainingAnalyticsSideChannel") |
| 22 | + # UUID('b664a4a9-d86f-5a5f-95cb-e8353a7e8356') |
| 23 | + # We purposefully use the SAME side channel as the TrainingAnalyticsSideChannel |
| 24 | + |
| 25 | + super().__init__(DefaultTrainingAnalyticsSideChannel.CHANNEL_ID) |
| 26 | + |
| 27 | + def on_message_received(self, msg: IncomingMessage) -> None: |
| 28 | + raise UnityCommunicationException( |
| 29 | + "The DefaultTrainingAnalyticsSideChannel received a message from Unity, " |
| 30 | + + "this should not have happened." |
| 31 | + ) |
| 32 | + |
| 33 | + def environment_initialized(self) -> None: |
| 34 | + # Tuple of (major, minor, patch) |
| 35 | + vi = sys.version_info |
| 36 | + |
| 37 | + msg = TrainingEnvironmentInitialized( |
| 38 | + python_version=f"{vi[0]}.{vi[1]}.{vi[2]}", |
| 39 | + mlagents_version="Custom", |
| 40 | + mlagents_envs_version=mlagents_envs.__version__, |
| 41 | + torch_version="Unknown", |
| 42 | + torch_device_type="Unknown", |
| 43 | + ) |
| 44 | + any_message = Any() |
| 45 | + any_message.Pack(msg) |
| 46 | + |
| 47 | + env_init_msg = OutgoingMessage() |
| 48 | + env_init_msg.set_raw_bytes(any_message.SerializeToString()) # type: ignore |
| 49 | + super().queue_message_to_send(env_init_msg) |
0 commit comments