Skip to content

Commit

Permalink
Changing read/write checkpoint method names
Browse files Browse the repository at this point in the history
  • Loading branch information
nv-braf committed Oct 16, 2024
1 parent c391107 commit 3c9edf7
Show file tree
Hide file tree
Showing 16 changed files with 53 additions and 42 deletions.
14 changes: 7 additions & 7 deletions genai-perf/genai_perf/checkpoint/checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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"]
)
}
Expand Down Expand Up @@ -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__
4 changes: 2 additions & 2 deletions genai-perf/genai_perf/config/generate/genai_perf_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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":
"""
Expand Down
4 changes: 2 additions & 2 deletions genai-perf/genai_perf/config/generate/perf_analyzer_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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":
"""
Expand Down
8 changes: 4 additions & 4 deletions genai-perf/genai_perf/config/run/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,28 @@ 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}

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
Expand Down
18 changes: 10 additions & 8 deletions genai-perf/genai_perf/config/run/run_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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":
"""
Expand All @@ -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
Expand Down
10 changes: 6 additions & 4 deletions genai-perf/genai_perf/measurements/run_config_measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion genai-perf/genai_perf/record/gpu_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions genai-perf/genai_perf/record/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 5 additions & 2 deletions genai-perf/tests/test_checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
###########################################################################
Expand All @@ -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"])
Expand Down
6 changes: 4 additions & 2 deletions genai-perf/tests/test_genai_perf_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion genai-perf/tests/test_model_config_measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

Expand Down
2 changes: 1 addition & 1 deletion genai-perf/tests/test_perf_analyzer_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

Expand Down
4 changes: 3 additions & 1 deletion genai-perf/tests/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion genai-perf/tests/test_run_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

Expand Down
2 changes: 1 addition & 1 deletion genai-perf/tests/test_run_config_measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

Expand Down

0 comments on commit 3c9edf7

Please sign in to comment.