@@ -170,14 +170,18 @@ def test_save_checkpoint_fallback(self, checkpoint_io_components, tmp_path, mock
170170 checkpoint = {"model" : torch .nn .Linear (2 , 2 )}
171171 ckpt_version_path = str (tmp_path / "diff_path" )
172172
173+ storage_options = {"content_metadata" : {"version" : 1 }}
174+
173175 expected_return = mocker .MagicMock ()
174176 alt_checkpoint_io .save_checkpoint .return_value = expected_return
175177
176178 # When
177- result = checkpoint_io .save_checkpoint (checkpoint , ckpt_version_path )
179+ result = checkpoint_io .save_checkpoint (checkpoint , ckpt_version_path , storage_options = storage_options )
178180
179181 # Then
180- alt_checkpoint_io .save_checkpoint .assert_called_once_with (checkpoint , ckpt_version_path )
182+ alt_checkpoint_io .save_checkpoint .assert_called_once_with (
183+ checkpoint , ckpt_version_path , storage_options = storage_options
184+ )
181185 assert result is expected_return
182186
183187 def test_save_ml_flashpoint_checkpoint_writes_common_state_dict (self , checkpoint_io_components , mocker ):
@@ -222,6 +226,120 @@ def test_save_ml_flashpoint_checkpoint_writes_common_state_dict(self, checkpoint
222226 loaded_common_state_dict = torch .load (common_state_file_path )
223227 assert loaded_common_state_dict == common_state_dict
224228
229+ def test_save_ml_flashpoint_checkpoint_writes_metadata (self , checkpoint_io_components , mocker ):
230+ """Tests that content_metadata is injected into the checkpoint before saving."""
231+ # Given
232+ mocker .patch ("ml_flashpoint.adapter.megatron.save_utils.torch.distributed.get_node_local_rank" , return_value = 0 )
233+ checkpoint_io = checkpoint_io_components ["checkpoint_io" ]
234+ base_path = checkpoint_io_components ["base_path" ]
235+ ckpt_version_path = base_path + "/checkpoint1"
236+
237+ checkpoint = {"some_state" : 123 }
238+ storage_options = {"content_metadata" : {"is_mlf" : True }}
239+
240+ mock_save_preprocess = mocker .patch (
241+ "ml_flashpoint.adapter.megatron.save_utils.mcore_state_dict_utils.save_preprocess" , return_value = ({}, {})
242+ )
243+ mocker .patch ("ml_flashpoint.adapter.megatron.save_utils.torch.save" )
244+ mocker .patch .object (checkpoint_io , "_save_context" )
245+
246+ # When
247+ checkpoint_io .save_checkpoint (checkpoint , ckpt_version_path , storage_options )
248+
249+ # Then
250+ mock_save_preprocess .assert_called_once ()
251+ modified_checkpoint = mock_save_preprocess .call_args [0 ][0 ]
252+ assert "content_metadata" in modified_checkpoint
253+ assert modified_checkpoint ["content_metadata" ] == {"is_mlf" : True }
254+
255+ def test_save_ml_flashpoint_checkpoint_does_not_overwrite_existing_metadata (self , checkpoint_io_components , mocker ):
256+ """Tests that existing content_metadata in the checkpoint is not overwritten
257+ if storage_options doesn't provide it."""
258+ # Given
259+ mocker .patch ("ml_flashpoint.adapter.megatron.save_utils.torch.distributed.get_node_local_rank" , return_value = 0 )
260+ checkpoint_io = checkpoint_io_components ["checkpoint_io" ]
261+ base_path = checkpoint_io_components ["base_path" ]
262+ ckpt_version_path = base_path + "/checkpoint_no_overwrite"
263+
264+ # Prepare a checkpoint that already contains metadata
265+ original_metadata = {"existing_key" : "original_value" }
266+ checkpoint = {"model_state" : [1 , 2 , 3 ], "content_metadata" : original_metadata }
267+
268+ mocker .patch (
269+ "ml_flashpoint.adapter.megatron.save_utils.mcore_state_dict_utils.save_preprocess" , return_value = ({}, {})
270+ )
271+
272+ mocker .patch ("ml_flashpoint.adapter.megatron.save_utils.torch.save" )
273+ mocker .patch .object (checkpoint_io , "_save_context" )
274+
275+ # Scenario 1: storage_options is None
276+ # When
277+ checkpoint_io .save_checkpoint (checkpoint , ckpt_version_path , storage_options = None )
278+ # Then: Verify metadata was not modified or removed
279+ assert checkpoint ["content_metadata" ] == original_metadata
280+
281+ # Scenario 2: storage_options is an empty dictionary {}
282+ # When
283+ checkpoint_io .save_checkpoint (checkpoint , ckpt_version_path , storage_options = {})
284+ # Then: Verify metadata still remains unchanged
285+ assert checkpoint ["content_metadata" ] == original_metadata
286+
287+ def test_load_content_metadata_fallback (self , checkpoint_io_components , tmp_path ):
288+ """Tests load_content_metadata falls back to alternative IO for non-MLF paths."""
289+ # Given
290+ checkpoint_io = checkpoint_io_components ["checkpoint_io" ]
291+ alt_checkpoint_io = checkpoint_io_components ["alt_checkpoint_io" ]
292+ ckpt_version_path = str (tmp_path / "diff_path" )
293+
294+ expected_metadata = {"meta" : "fallback" }
295+ alt_checkpoint_io .load_content_metadata .return_value = expected_metadata
296+
297+ # When
298+ result = checkpoint_io .load_content_metadata (ckpt_version_path )
299+
300+ # Then
301+ alt_checkpoint_io .load_content_metadata .assert_called_once_with (ckpt_version_path , None )
302+ assert result == expected_metadata
303+
304+ def test_load_content_metadata_from_preloaded (self , checkpoint_io_components ):
305+ """Tests load_content_metadata prioritizes preloaded_state_dict."""
306+ # Given
307+ checkpoint_io = checkpoint_io_components ["checkpoint_io" ]
308+ ckpt_version_path = checkpoint_io .flashpoint_base_dir .data + "/checkpoint1"
309+
310+ expected_metadata = {"from_memory" : True }
311+ preloaded = {"content_metadata" : expected_metadata }
312+
313+ # When
314+ result = checkpoint_io .load_content_metadata (ckpt_version_path , preloaded_state_dict = preloaded )
315+
316+ # Then
317+ assert result == expected_metadata
318+
319+ def test_load_content_metadata_from_disk (self , checkpoint_io_components , mocker ):
320+ """Tests load_content_metadata loads from common.pt."""
321+ # Given
322+ checkpoint_io = checkpoint_io_components ["checkpoint_io" ]
323+ ckpt_version_path = checkpoint_io .flashpoint_base_dir .data + "/checkpoint1"
324+
325+ mocker .patch ("ml_flashpoint.adapter.nemo.checkpoint_io.os.path.exists" , return_value = True )
326+
327+ expected_metadata = {"from_disk" : True }
328+ # Mock torch.load to return a dictionary containing our expected content_metadata
329+ mock_torch_load = mocker .patch (
330+ "ml_flashpoint.adapter.nemo.checkpoint_io.torch.load" , return_value = {"content_metadata" : expected_metadata }
331+ )
332+
333+ # When
334+ result = checkpoint_io .load_content_metadata (ckpt_version_path )
335+
336+ # Then
337+ # It should load the common state dict from disk safely (CPU, weights_only=False) and extract the metadata
338+ mock_torch_load .assert_called_once ()
339+ assert mock_torch_load .call_args [1 ]["map_location" ] == "cpu"
340+ assert mock_torch_load .call_args [1 ]["weights_only" ] is False
341+ assert result == expected_metadata
342+
225343 def test_save_ml_flashpoint_checkpoint_async_success (self , checkpoint_io_components , mocker ):
226344 """Tests a successful asynchronous MLF save."""
227345 # Given
0 commit comments