def hvd_rotation_profiler_config_parser(config_folder, monkeypatch): config_path = os.path.join(config_folder, "hvd_rotation_profiler_config_parser.json") monkeypatch.setenv("SMPROFILER_CONFIG_PATH", config_path) return ProfilerConfigParser()
def native_tf2_pyinstrument_profiler_config_parser(config_folder, monkeypatch): config_path = os.path.join( config_folder, "test_native_tf2_pyinstrument_profiler_config_parser.json") monkeypatch.setenv("SMPROFILER_CONFIG_PATH", config_path) return ProfilerConfigParser(FRAMEWORK.TENSORFLOW)
def case_insensitive_profiler_config_parser(config_folder, monkeypatch): config_path = os.path.join(config_folder, "case_insensitive_profiler_config_parser.json") monkeypatch.setenv("SMPROFILER_CONFIG_PATH", config_path) return ProfilerConfigParser()
def user_disabled_profiler_config_parser(config_folder, monkeypatch): config_path = os.path.join(config_folder, "user_disabled_profile_config_parser.json") monkeypatch.setenv("SMPROFILER_CONFIG_PATH", config_path) return ProfilerConfigParser()
def invalid_string_data_profiler_config_parser(config_folder, monkeypatch): config_path = os.path.join( config_folder, "invalid_string_data_profiler_config_parser.json") monkeypatch.setenv("SMPROFILER_CONFIG_PATH", config_path) return ProfilerConfigParser()
def missing_config_profiler_config_parser(config_folder, monkeypatch): config_path = os.path.join( config_folder, "missing_profile_config_parser.json") # doesn't exist monkeypatch.setenv("SMPROFILER_CONFIG_PATH", config_path) return ProfilerConfigParser()
def tf2_profiler_config_parser_by_time(config_folder, monkeypatch): config_path = os.path.join(config_folder, "test_tf2_profiler_config_parser_by_time.json") monkeypatch.setenv("SMPROFILER_CONFIG_PATH", config_path) return ProfilerConfigParser()
# smdistributed.dataparallel should be invoked via `mpirun`. # It supports EC2 machines with 8 GPUs per machine. smdataparallel = None if check_smdataparallel_env(): try: import smdistributed.dataparallel.torch.distributed as smdataparallel except ImportError: pass DEFAULT_INCLUDE_COLLECTIONS = [CollectionKeys.LOSSES] python_profiler = None # Enable python profiling if profiling is enabled. profiler_config_parser = ProfilerConfigParser() if profiler_config_parser.profiling_enabled: config = profiler_config_parser.config if config.python_profiling_config.is_enabled(): python_profiler = PythonProfiler.get_python_profiler(config, "pytorch") python_profiler.start_profiling(StepPhase.START) class Hook(CallbackHook): """ The _TraceEventData is similar to a structure that contains the event data to be written in the event file. It contains the following metadata: training_phase such as "X", "M", "I" etc. start time, duration and end time in microseconds pid is the process id that wants to record this event. and event arguments.
def __init__( self, collection_manager: CollectionManager, default_include_collections: List[str], init_step: int = 0, out_dir: Optional[str] = None, export_tensorboard: bool = False, tensorboard_dir: Optional[str] = None, dry_run: bool = False, reduction_config: Optional[ReductionConfig] = None, save_config: Optional[Union[SaveConfig, Dict[ModeKeys, SaveConfigMode]]] = None, include_regex: Optional[List[str]] = None, include_collections: Optional[List[str]] = None, save_all: bool = False, include_workers: str = "one", profiler_config_parser: Optional[ProfilerConfigParser] = None, ): """ A class used to represent the hook which gets attached to the training process. This takes the form appropriate for the framework such as tf.train.SessionRunHook for TF, Callback for keras... ... Attributes ---------- out_dir : str represents a path into which outputs will be written to. The hook raises error if the 'out_dir' already exists. The implementation does not support merging the tensors generated in current job with tensors from previous job. Hence, ensure that the 'out_dir' does not exist. dry_run : bool when dry run is set, behavior is only described in the log file. tensors are not actually saved. save_config: SaveConfig object Takes save config object which is applied as default for all included tensors. A collection can optionally have its own saveconfig object which overrides this for its tensors. reduction_config: ReductionConfig object if passed, this reduction config object is used as default for all tensors included. A collection has its own saveconfig object which overrides this for its tensors. if this is not passed, tensor is saved in full. include_regex: list of str takes as input the list of string representing regular expressions. Tensors whose names match these regular expressions will be saved. These tensors will be available as part of the `default` collection. include_collections: list of str representing collection names takes as input the collections which should be saved. if this is empty, it defaults to including all collections from code save_all: bool a shortcut for saving all tensors in the model. they are all saved in the collection `all` include_workers: str makes the hook save data from all workers profiler_config_parser: ProfilerConfigParser object if passed, use this profiler configuration. by default, set up a new profiler configuration here. """ self.out_dir = verify_and_get_out_dir(out_dir) self.tensorboard_dir = get_tensorboard_dir( export_tensorboard=export_tensorboard, tensorboard_dir=tensorboard_dir, out_dir=self.out_dir, ) self.dry_run = dry_run self.worker = None # when smdebug is used during an unsupported dist training process # we write data only from the process that has self.first_process set to True. self.first_process = None self.save_all_workers = True if include_workers == "all" else False self.chief_worker = DEFAULT_WORKER_NAME if include_collections is None: include_collections = default_include_collections else: include_collections = flatten(include_collections) self.include_collections = list( set(include_collections).union(set(default_include_collections))) self.save_all = save_all self.save_config = SaveConfig.parse(save_config) if reduction_config is None: reduction_config = ReductionConfig(save_raw_tensor=True) self.reduction_config = reduction_config self.include_regex = include_regex self.collection_manager = collection_manager self.init_step = init_step # The written_tensor_name_for_step dictionary stores # the names of each tensor saved for every step. # This is to detect name clashes. # If a name clash is detected, it is avoided by appending # an index to the tensor name. self.written_tensor_name_for_step = defaultdict(int) self.logger = logger if self.tensorboard_dir is None: self.logger.info( f"tensorboard_dir has not been set for the hook. SMDebug will not be exporting tensorboard summaries." ) if include_regex is not None: collection_manager.get( CollectionKeys.DEFAULT).include(include_regex) if CollectionKeys.DEFAULT not in self.include_collections: self.include_collections.append(CollectionKeys.DEFAULT) self.save_all = save_all if self.save_all: collection_manager.get(CollectionKeys.ALL).include(".*") if CollectionKeys.ALL not in self.include_collections: self.include_collections.append(CollectionKeys.ALL) if (CollectionKeys.DEFAULT not in self.include_collections and collection_manager.get(CollectionKeys.DEFAULT).include_regex): self.logger.warn("The `default` collection was not passed to " "include_collections. So it is not being saved") self._collections_to_save = set() self._collections_to_save_for_step = None self.prepared_collections = False self.tensor_to_collections = {} self.step = init_step self.last_saved_step = None self.mode = ModeKeys.GLOBAL self.mode_steps = {ModeKeys.GLOBAL: init_step} self.writer = None if profiler_config_parser is None: profiler_config_parser = ProfilerConfigParser() profiler_config_parser.load_config() self.profiler_config_parser = profiler_config_parser self.timeline_writer = TimelineFileWriter( profiler_config_parser=profiler_config_parser) self.hvd_reader = None self.is_smdataparallel_profiling = False if is_sagemaker_job() and SageMakerFileMetricsWriter is not None: self.metrics_writer = SageMakerFileMetricsWriter() else: self.metrics_writer = None # Maps ModeKeys to FileWriter objects self.tb_writers = {} # Cache scalars that are being saved through save_scalar() calls self.scalar_cache = [] self.logger.info("Saving to {}".format(self.out_dir)) atexit.register(self._cleanup) # Check if there is any last saved state. Initialize the hook based last saved state. self.training_run = 0 self._initialize_to_last_saved_state() self.custom_tensors_to_save = dict()