feat(scan_layers): dynamically set scan_layers from checkpoint metadata#4344
Conversation
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
shralex
left a comment
There was a problem hiding this comment.
Thanks Jacky!
Can you add some comments to wherever scan_layers is described (types.py, base.yml) describing this behavior when resuming from a checkpoint.
Also, can we change docs in appropriate places to explain this new behavior. I remember I added a bunch of warnings related to scan_layers previously to the docs, these might need to be updated since you no longer need to specify scan_layers when resuming from a checkpoint.
dc53711 to
220893d
Compare
2ddeddf to
0985ebb
Compare
shralex
left a comment
There was a problem hiding this comment.
Thanks for the changes. I made a few suggestions to update comments. The main missing thing is updates to conversion scripts: to_huggingface should auto-detect the setting as well from the provided checkpoint (and error if its setting conflicts with the checkpoint:
Consider calling the new helper function in to_huggingface.py's main function right after initialising the config:
Initialize maxtext config
config = pyconfig.initialize_pydantic(argv)
Auto-resolve scan_layers from checkpoint metadata if not explicitly provided
config = verify_and_sync_scan_layers(config)
to_maxtext should write the scan_layers metadata into the checkpoint it creates, but in the current implementation, it does not actually do so. I believe we should provide a config when calling save_weights_to_checkpoint,
xibinliu
left a comment
There was a problem hiding this comment.
I think we should also cover the following metadata handling when doing checkpoints conversion:
-
to_maxtext.py
Generate metadata data with scanned_layers and lora parameters and attach the metadata to the MaxText checkpoints. -
to_hugging_face.py:
Besides the lora parameters, we should also read scan_layers from the MaxText metadata and use it for conversion.
| is_explicit = "scan_layers" in model_fields_set if model_fields_set is not None else True | ||
|
|
||
| if is_explicit: | ||
| if saved_scan_layers != config.scan_layers: |
There was a problem hiding this comment.
No need to check if the check in the above comment is implemented.
There was a problem hiding this comment.
Correct, with the early return in place, that duplicate check was removed. Thanks!
There was a problem hiding this comment.
Remove this line:
if saved_scan_layers != config.scan_layers:
0985ebb to
5297b98
Compare
a169899 to
bc9d5aa
Compare
Description
This PR is a follow-up to PR #4304 and PR #4340. It introduces dynamic synchronization of
scan_layersconfiguration from checkpoint metadata when a checkpoint path is provided andscan_layersis not explicitly specified by the user on the command-line, in environment variables, or as programmatic keyword arguments.Key Changes:
src/maxtext/configs/pyconfig.py):env_keys).__pydantic_fields_set__to only the union of explicit CLI, environment, and keyword argument keys. This avoids the default Pydantic behavior where unpacking a dictionary of default configurations marks every single field as explicitly set.src/maxtext/utils/model_creation_utils.py):verify_and_sync_scan_layers(config).from_pretrained, resolved the active config viaconfig = verify_and_sync_scan_layers(config)without verbose comments, keeping the code highly readable.scan_layersis explicitly specified by the user and mismatches checkpoint metadata, aValueErrorconfiguration mismatch error is raised.Tests
TestVerifyAndSyncScanLayersinsidetests/unit/model_creation_utils_test.py:test_sync_to_false_when_implicit: Asserts thatscan_layersdynamically updates toFalsefrom checkpoint metadata when not explicitly specified by the user.test_sync_to_true_when_implicit: Asserts thatscan_layersstays/updates toTruewhen checkpoint metadata isTrue.test_explicit_match_raises_no_error: Asserts that providing matching explicit values does not trigger validation errors.test_explicit_mismatch_raises_value_error: Asserts that an explicit conflict raises the expectedValueErrorconfiguration mismatch error.model_creation_utils_test.pypass successfully:Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.