diff --git a/genai-perf/genai_perf/checkpoint/checkpoint.py b/genai-perf/genai_perf/checkpoint/checkpoint.py index 790b85f2..162f338c 100644 --- a/genai-perf/genai_perf/checkpoint/checkpoint.py +++ b/genai-perf/genai_perf/checkpoint/checkpoint.py @@ -41,19 +41,19 @@ class Checkpoint: results: Results def __post_init__(self): - self._read_from_checkpoint() + self._create_class_from_checkpoint() ########################################################################### # Read/Write Methods ########################################################################### - def write_to_checkpoint(self) -> None: - state_dict = {"Results": self.results.write_to_checkpoint()} + def create_checkpoint_object(self) -> None: + state_dict = {"Results": self.results.create_checkpoint_object()} checkpoint_file_path = self._create_checkpoint_file_path() with open(checkpoint_file_path, "w") as checkpoint_file: json.dump(state_dict, checkpoint_file, default=checkpoint_encoder) - def _read_from_checkpoint(self) -> None: + def _create_class_from_checkpoint(self) -> None: checkpoint_file_path = self._create_checkpoint_file_path() if os.path.isfile(checkpoint_file_path): @@ -62,7 +62,7 @@ def _read_from_checkpoint(self) -> None: with open(checkpoint_file_path, "r") as checkpoint_file: checkpoint_json = json.load(checkpoint_file) self._state: CheckpointObject = { - "Results": Results.read_from_checkpoint( + "Results": Results.create_class_from_checkpoint( checkpoint_json["Results"] ) } @@ -92,7 +92,7 @@ def _create_checkpoint_file_path(self) -> str: def checkpoint_encoder(obj): if isinstance(obj, bytes): return obj.decode("utf-8") - elif hasattr(obj, "write_to_checkpoint"): - return obj.write_to_checkpoint() + elif hasattr(obj, "create_checkpoint_object"): + return obj.create_checkpoint_object() else: return obj.__dict__ diff --git a/genai-perf/genai_perf/config/generate/genai_perf_config.py b/genai-perf/genai_perf/config/generate/genai_perf_config.py index 36988596..5e200caa 100644 --- a/genai-perf/genai_perf/config/generate/genai_perf_config.py +++ b/genai-perf/genai_perf/config/generate/genai_perf_config.py @@ -62,7 +62,7 @@ def _set_options_based_on_objective( ########################################################################### # Checkpoint Methods ########################################################################### - def write_to_checkpoint(self) -> CheckpointObject: + def create_checkpoint_object(self) -> CheckpointObject: """ Converts the class data into a dictionary that can be written to the checkpoint file @@ -72,7 +72,7 @@ def write_to_checkpoint(self) -> CheckpointObject: return genai_perf_config_dict @classmethod - def read_from_checkpoint( + def create_class_from_checkpoint( cls, genai_perf_config_dict: CheckpointObject ) -> "GenAIPerfConfig": """ diff --git a/genai-perf/genai_perf/config/generate/perf_analyzer_config.py b/genai-perf/genai_perf/config/generate/perf_analyzer_config.py index 0e698651..3c2f0da8 100644 --- a/genai-perf/genai_perf/config/generate/perf_analyzer_config.py +++ b/genai-perf/genai_perf/config/generate/perf_analyzer_config.py @@ -163,7 +163,7 @@ def _remove_option_from_cli_string( ########################################################################### # Checkpoint Methods ########################################################################### - def write_to_checkpoint(self) -> CheckpointObject: + def create_checkpoint_object(self) -> CheckpointObject: """ Converts the class data into a dictionary that can be written to the checkpoint file @@ -173,7 +173,7 @@ def write_to_checkpoint(self) -> CheckpointObject: return pa_config_dict @classmethod - def read_from_checkpoint( + def create_class_from_checkpoint( cls, perf_analyzer_config_dict: CheckpointObject ) -> "PerfAnalyzerConfig": """ diff --git a/genai-perf/genai_perf/config/run/results.py b/genai-perf/genai_perf/config/run/results.py index 43641272..9e886dd3 100644 --- a/genai-perf/genai_perf/config/run/results.py +++ b/genai-perf/genai_perf/config/run/results.py @@ -35,13 +35,13 @@ class Results: ########################################################################### # Checkpoint Methods ########################################################################### - def write_to_checkpoint(self) -> CheckpointObject: + def create_checkpoint_object(self) -> CheckpointObject: """ Converts the class data into a dictionary that can be written to the checkpoint file """ run_config_dicts = [ - run_config.write_to_checkpoint() for run_config in self.run_configs + run_config.create_checkpoint_object() for run_config in self.run_configs ] results_dict = {"run_configs": run_config_dicts} @@ -49,14 +49,14 @@ def write_to_checkpoint(self) -> CheckpointObject: return results_dict @classmethod - def read_from_checkpoint(cls, results_dict: CheckpointObject) -> "Results": + def create_class_from_checkpoint(cls, results_dict: CheckpointObject) -> "Results": """ Takes the checkpoint's representation of the class and creates (and populates) a new instance of Results """ results = Results() for run_config_dict in results_dict["run_configs"]: - run_config = RunConfig.read_from_checkpoint(run_config_dict) + run_config = RunConfig.create_class_from_checkpoint(run_config_dict) results.add_run_config(run_config) return results diff --git a/genai-perf/genai_perf/config/run/run_config.py b/genai-perf/genai_perf/config/run/run_config.py index 82e0f1f2..57e86897 100644 --- a/genai-perf/genai_perf/config/run/run_config.py +++ b/genai-perf/genai_perf/config/run/run_config.py @@ -53,34 +53,36 @@ class RunConfig: ########################################################################### # Checkpoint Methods ########################################################################### - def write_to_checkpoint(self) -> CheckpointObject: + def create_checkpoint_object(self) -> CheckpointObject: """ Converts the class data into a dictionary that can be written to the checkpoint file """ run_config_dict = { "name": self.name, - "genai_perf_config": self.genai_perf_config.write_to_checkpoint(), - "perf_analyzer_config": self.perf_analyzer_config.write_to_checkpoint(), - "measurement": self.measurement.write_to_checkpoint(), + "genai_perf_config": self.genai_perf_config.create_checkpoint_object(), + "perf_analyzer_config": self.perf_analyzer_config.create_checkpoint_object(), + "measurement": self.measurement.create_checkpoint_object(), } return run_config_dict @classmethod - def read_from_checkpoint(cls, run_config_dict: CheckpointObject) -> "RunConfig": + def create_class_from_checkpoint( + cls, run_config_dict: CheckpointObject + ) -> "RunConfig": """ Takes the checkpoint's representation of the class and creates (and populates) a new instance of a RCM """ name = run_config_dict["name"] - genai_perf_config = GenAIPerfConfig.read_from_checkpoint( + genai_perf_config = GenAIPerfConfig.create_class_from_checkpoint( run_config_dict["genai_perf_config"] ) - perf_analyzer_config = PerfAnalyzerConfig.read_from_checkpoint( + perf_analyzer_config = PerfAnalyzerConfig.create_class_from_checkpoint( run_config_dict["perf_analyzer_config"] ) - measurement = RunConfigMeasurement.read_from_checkpoint( + measurement = RunConfigMeasurement.create_class_from_checkpoint( run_config_dict["measurement"] ) diff --git a/genai-perf/genai_perf/measurements/model_config_measurement.py b/genai-perf/genai_perf/measurements/model_config_measurement.py index 66ff20dc..aecedf0f 100644 --- a/genai-perf/genai_perf/measurements/model_config_measurement.py +++ b/genai-perf/genai_perf/measurements/model_config_measurement.py @@ -102,7 +102,7 @@ def set_metric_objectives(self, metric_objectives: MetricObjectives) -> None: ########################################################################### # Checkpoint Methods ########################################################################### - def write_to_checkpoint(self) -> CheckpointObject: + def create_checkpoint_object(self) -> CheckpointObject: """ Converts the class data into a dictionary that can be written to the checkpoint file @@ -116,7 +116,7 @@ def write_to_checkpoint(self) -> CheckpointObject: return mcm_dict @classmethod - def read_from_checkpoint( + def create_class_from_checkpoint( cls, mcm_dict: CheckpointObject ) -> "ModelConfigMeasurement": """ @@ -137,7 +137,7 @@ def _read_perf_metrics_from_checkpoint( for [tag, record_dict] in perf_metrics_dict.values(): record = Record.get(tag) - record = record.read_from_checkpoint(record_dict) # type: ignore + record = record.create_class_from_checkpoint(record_dict) # type: ignore perf_metrics[tag] = record # type: ignore return perf_metrics diff --git a/genai-perf/genai_perf/measurements/run_config_measurement.py b/genai-perf/genai_perf/measurements/run_config_measurement.py index 676cf5ec..242b386c 100644 --- a/genai-perf/genai_perf/measurements/run_config_measurement.py +++ b/genai-perf/genai_perf/measurements/run_config_measurement.py @@ -258,7 +258,7 @@ def add_perf_metrics( ########################################################################### # Checkpoint Methods ########################################################################### - def write_to_checkpoint(self) -> CheckpointObject: + def create_checkpoint_object(self) -> CheckpointObject: """ Converts the class data into a dictionary that can be written to the checkpoint file @@ -273,7 +273,9 @@ def write_to_checkpoint(self) -> CheckpointObject: return rcm_dict @classmethod - def read_from_checkpoint(cls, rcm_dict: CheckpointObject) -> "RunConfigMeasurement": + def create_class_from_checkpoint( + cls, rcm_dict: CheckpointObject + ) -> "RunConfigMeasurement": """ Takes the checkpoint's representation of the class and creates (and populates) a new instance of a RCM @@ -304,7 +306,7 @@ def _read_gpu_metrics_from_checkpoint( gpu_metric_dict: Any = {} for tag, record_dict in gpu_metrics_dict.values(): record = Record.get(tag) - record = record.read_from_checkpoint(record_dict) # type: ignore + record = record.create_class_from_checkpoint(record_dict) # type: ignore gpu_metric_dict[tag] = record gpu_metrics[gpu_uuid] = gpu_metric_dict @@ -318,7 +320,7 @@ def _read_model_config_measurements_from_checkpoint( model_config_measurements: ModelConfigMeasurements = {} for model_name, mcm_dict in mcm_dicts.items(): model_config_measurements[model_name] = ( - ModelConfigMeasurement.read_from_checkpoint(mcm_dict) + ModelConfigMeasurement.create_class_from_checkpoint(mcm_dict) ) return model_config_measurements diff --git a/genai-perf/genai_perf/record/gpu_record.py b/genai-perf/genai_perf/record/gpu_record.py index 1eb95398..92a2c5f0 100644 --- a/genai-perf/genai_perf/record/gpu_record.py +++ b/genai-perf/genai_perf/record/gpu_record.py @@ -48,7 +48,7 @@ def device_uuid(self) -> str: return self._device_uuid @classmethod - def read_from_checkpoint(cls, record_dict) -> "Record": + def create_class_from_checkpoint(cls, record_dict) -> "Record": record = cls(0) for key in ["_value", "_timestamp", "_device"]: if key in record_dict: diff --git a/genai-perf/genai_perf/record/record.py b/genai-perf/genai_perf/record/record.py index ca342dec..122ed4c1 100644 --- a/genai-perf/genai_perf/record/record.py +++ b/genai-perf/genai_perf/record/record.py @@ -159,11 +159,11 @@ def tag(self) -> str: the name tag of the record type. """ - def write_to_checkpoint(self): + def create_checkpoint_object(self): return (self.tag, self.__dict__) @classmethod - def read_from_checkpoint(cls, record_dict) -> "Record": + def create_class_from_checkpoint(cls, record_dict) -> "Record": record = cls(0, 0) for key in ["_value", "_timestamp"]: if key in record_dict: diff --git a/genai-perf/tests/test_checkpoint.py b/genai-perf/tests/test_checkpoint.py index b21cdc74..ecd6b9d8 100644 --- a/genai-perf/tests/test_checkpoint.py +++ b/genai-perf/tests/test_checkpoint.py @@ -61,6 +61,9 @@ def setUp(self): def tearDown(self): patch.stopall() + # TODO: TPA-501: Add mock file I/O and check for empty directory and improperly + # formatted checkpoint files + ########################################################################### # Checkpoint Tests ########################################################################### @@ -73,8 +76,8 @@ def test_checkpoint_methods(self): self.assertEqual({}, self._checkpoint._state) # Then write and read back the Results - self._checkpoint.write_to_checkpoint() - self._checkpoint._read_from_checkpoint() + self._checkpoint.create_checkpoint_object() + self._checkpoint._create_class_from_checkpoint() os.remove(self._checkpoint._create_checkpoint_file_path()) self.assertEqual(self._results, self._checkpoint._state["Results"]) diff --git a/genai-perf/tests/test_genai_perf_config.py b/genai-perf/tests/test_genai_perf_config.py index 328473bd..9ee624c8 100644 --- a/genai-perf/tests/test_genai_perf_config.py +++ b/genai-perf/tests/test_genai_perf_config.py @@ -85,8 +85,10 @@ def test_checkpoint_methods(self): self._default_genai_perf_config, default=checkpoint_encoder ) - genai_perf_config_from_checkpoint = GenAIPerfConfig.read_from_checkpoint( - json.loads(genai_perf_config_json) + genai_perf_config_from_checkpoint = ( + GenAIPerfConfig.create_class_from_checkpoint( + json.loads(genai_perf_config_json) + ) ) self.assertEqual( diff --git a/genai-perf/tests/test_model_config_measurement.py b/genai-perf/tests/test_model_config_measurement.py index 4361a686..5f22b45e 100644 --- a/genai-perf/tests/test_model_config_measurement.py +++ b/genai-perf/tests/test_model_config_measurement.py @@ -121,7 +121,7 @@ def test_checkpoint_methods(self): """ mcmA_json = json.dumps(self.mcmA, default=checkpoint_encoder) - mcmA_from_checkpoint = ModelConfigMeasurement.read_from_checkpoint( + mcmA_from_checkpoint = ModelConfigMeasurement.create_class_from_checkpoint( json.loads(mcmA_json) ) diff --git a/genai-perf/tests/test_perf_analyzer_config.py b/genai-perf/tests/test_perf_analyzer_config.py index de4b52fb..d1a4936b 100644 --- a/genai-perf/tests/test_perf_analyzer_config.py +++ b/genai-perf/tests/test_perf_analyzer_config.py @@ -167,7 +167,7 @@ def test_checkpoint_methods(self): self._default_perf_analyzer_config, default=checkpoint_encoder ) - pa_config_from_checkpoint = PerfAnalyzerConfig.read_from_checkpoint( + pa_config_from_checkpoint = PerfAnalyzerConfig.create_class_from_checkpoint( json.loads(pa_config_json) ) diff --git a/genai-perf/tests/test_results.py b/genai-perf/tests/test_results.py index a0dea789..8136303c 100644 --- a/genai-perf/tests/test_results.py +++ b/genai-perf/tests/test_results.py @@ -54,7 +54,9 @@ def test_checkpoint_methods(self): """ results_json = json.dumps(self._results, default=checkpoint_encoder) - results_from_checkpoint = Results.read_from_checkpoint(json.loads(results_json)) + results_from_checkpoint = Results.create_class_from_checkpoint( + json.loads(results_json) + ) self.assertEqual(results_from_checkpoint, self._results) diff --git a/genai-perf/tests/test_run_config.py b/genai-perf/tests/test_run_config.py index 9f51c04a..fd2b83f4 100644 --- a/genai-perf/tests/test_run_config.py +++ b/genai-perf/tests/test_run_config.py @@ -63,7 +63,7 @@ def test_checkpoint_methods(self): """ run_config_json = json.dumps(self._run_config, default=checkpoint_encoder) - run_config_from_checkpoint = RunConfig.read_from_checkpoint( + run_config_from_checkpoint = RunConfig.create_class_from_checkpoint( json.loads(run_config_json) ) diff --git a/genai-perf/tests/test_run_config_measurement.py b/genai-perf/tests/test_run_config_measurement.py index 940159f7..ca356617 100644 --- a/genai-perf/tests/test_run_config_measurement.py +++ b/genai-perf/tests/test_run_config_measurement.py @@ -223,7 +223,7 @@ def test_checkpoint_methods(self): rcmA = self._create_rcmA() rcmA_json = json.dumps(rcmA, default=checkpoint_encoder) - rcmA_from_checkpoint = RunConfigMeasurement.read_from_checkpoint( + rcmA_from_checkpoint = RunConfigMeasurement.create_class_from_checkpoint( json.loads(rcmA_json) )