Skip to content

Commit 284b207

Browse files
author
Chris Elion
authored
Merge pull request #3631 from Unity-Technologies/release-0.15.0-fix-stats
make sure top-level timer is closed before writing
2 parents 40d6547 + f4359bb commit 284b207

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

ml-agents/mlagents/trainers/learn.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from mlagents_envs.side_channel.side_channel import SideChannel
3131
from mlagents_envs.side_channel.engine_configuration_channel import EngineConfig
3232
from mlagents_envs.exception import UnityEnvironmentException
33-
from mlagents_envs.timers import hierarchical_timer
33+
from mlagents_envs.timers import hierarchical_timer, get_timer_tree
3434
from mlagents.logging_util import create_logger
3535

3636

@@ -329,6 +329,18 @@ def run_training(run_seed: int, options: RunOptions) -> None:
329329
tc.start_learning(env_manager)
330330
finally:
331331
env_manager.close()
332+
write_timing_tree(summaries_dir, options.run_id)
333+
334+
335+
def write_timing_tree(summaries_dir: str, run_id: str) -> None:
336+
timing_path = f"{summaries_dir}/{run_id}_timers.json"
337+
try:
338+
with open(timing_path, "w") as f:
339+
json.dump(get_timer_tree(), f, indent=4)
340+
except FileNotFoundError:
341+
logging.warning(
342+
f"Unable to save to {timing_path}. Make sure the directory exists"
343+
)
332344

333345

334346
def create_sampler_manager(sampler_config, run_seed=None):

ml-agents/mlagents/trainers/trainer_controller.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import os
66
import sys
7-
import json
87
import logging
98
from typing import Dict, Optional, Set
109
from collections import defaultdict
@@ -18,7 +17,7 @@
1817
UnityCommunicationException,
1918
)
2019
from mlagents.trainers.sampler_class import SamplerManager
21-
from mlagents_envs.timers import hierarchical_timer, get_timer_tree, timed
20+
from mlagents_envs.timers import hierarchical_timer, timed
2221
from mlagents.trainers.trainer import Trainer
2322
from mlagents.trainers.meta_curriculum import MetaCurriculum
2423
from mlagents.trainers.trainer_util import TrainerFactory
@@ -106,16 +105,6 @@ def _save_model_when_interrupted(self):
106105
)
107106
self._save_model()
108107

109-
def _write_timing_tree(self) -> None:
110-
timing_path = f"{self.summaries_dir}/{self.run_id}_timers.json"
111-
try:
112-
with open(timing_path, "w") as f:
113-
json.dump(get_timer_tree(), f, indent=4)
114-
except FileNotFoundError:
115-
self.logger.warning(
116-
f"Unable to save to {timing_path}. Make sure the directory exists"
117-
)
118-
119108
def _export_graph(self):
120109
"""
121110
Exports latest saved models to .nn format for Unity embedding.
@@ -231,7 +220,6 @@ def start_learning(self, env_manager: EnvManager) -> None:
231220
pass
232221
if self.train_model:
233222
self._export_graph()
234-
self._write_timing_tree()
235223

236224
def end_trainer_episodes(
237225
self, env: EnvManager, lessons_incremented: Dict[str, bool]

0 commit comments

Comments
 (0)