-
Notifications
You must be signed in to change notification settings - Fork 19.7k
Fix #20598 — Custom models/layers with sublayers in nested lists fail to save/load weights #22362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -893,6 +893,20 @@ def _save_container_state( | |
| inner_path=file_utils.join(inner_path, name).replace("\\", "/"), | ||
| visited_saveables=visited_saveables, | ||
| ) | ||
| elif isinstance(saveable, (list, dict, tuple, set)): | ||
| name = "_container" | ||
| if name in used_names: | ||
| used_names[name] += 1 | ||
| name = f"{name}_{used_names[name]}" | ||
| else: | ||
| used_names[name] = 0 | ||
| _save_container_state( | ||
| saveable, | ||
| weights_store, | ||
| assets_store, | ||
| inner_path=file_utils.join(inner_path, name).replace("\\", "/"), | ||
| visited_saveables=visited_saveables, | ||
| ) | ||
|
|
||
|
|
||
| def _load_container_state( | ||
|
|
@@ -929,6 +943,23 @@ def _load_container_state( | |
| failed_saveables=failed_saveables, | ||
| error_msgs=error_msgs, | ||
| ) | ||
| elif isinstance(saveable, (list, dict, tuple, set)): | ||
| name = "_container" | ||
| if name in used_names: | ||
| used_names[name] += 1 | ||
| name = f"{name}_{used_names[name]}" | ||
| else: | ||
| used_names[name] = 0 | ||
|
Comment on lines
+947
to
+952
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block duplicates the name generation logic found in name = "_container"
name = _get_unique_container_name(name, used_names) |
||
| _load_container_state( | ||
| saveable, | ||
| weights_store, | ||
| assets_store, | ||
| inner_path=file_utils.join(inner_path, name).replace("\\", "/"), | ||
| skip_mismatch=skip_mismatch, | ||
| visited_saveables=visited_saveables, | ||
| failed_saveables=failed_saveables, | ||
| error_msgs=error_msgs, | ||
| ) | ||
|
|
||
|
|
||
| class DiskIOStore: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic for generating a unique name for the container (
_container,_container_1, etc.) is duplicated here and in_load_container_state. Consider extracting this logic into a small helper function to improve maintainability and reduce redundancy. This aligns with the principle of keeping APIs modular and automating repetitive tasks (Repository Style Guide, lines 51, 124).