Repository navigation
FIX get_config: keep NumPy scalar arguments as Python numbers - #1871
Merged
bruAristimunha merged 4 commits intoDec 1, 2026
Merged
bruAristimunha merged 4 commits into
bruAristimunha merged 4 commits into
Conversation
build_model_config and the Hugging Face Hub config left out arguments passed as NumPy scalars (np.int64, np.float32), so from_config and from_pretrained rebuilt the default. Convert NumPy scalars, also inside lists, tuples and dicts, to Python numbers before the JSON checks, and in _update_init_kwargs so reset_head(np.int64(n)) keeps the config serializable.
…ig-numpy-scalars # Conflicts: # docs/whats_new.rst
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #1871 +/- ##
=======================================
Coverage 94.39% 94.40%
=======================================
Files 192 192
Lines 26549 26567 +18
=======================================
+ Hits 25061 25080 +19
+ Misses 1488 1487 -1 🚀 New features to boost your workflow:
|
…rrays Review follow-up (P2/P3): exact-type checks left subclasses of list/tuple/dict (OrderedDict, custom lists) holding raw NumPy scalars (JSON then fails or silently drops them), never converted NumPy dict keys, and dropped 0-d arrays. Widen to isinstance, normalize keys, and unwrap 0-d arrays with .item() like the dataset save path (braindecode#1863).
Collaborator
|
Maintainer note: widened |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
get_config()leaves out a constructor argument passed as a NumPy scalar, such asn_outputs=y.max() + 1(annp.int64) orsfreq=np.float32(200.0).np.int64andnp.float32are notintorfloat, so the Hub mixin config andbuild_model_configboth drop them: the config holds the default (Hub) or nothing, andfrom_configbuilds the default. On master:reset_head(np.int64(3))stores the NumPy value, andjson.dumps(model.get_config())raisesTypeError. Copilot flagged the same loss on #1260, which fixed it inside Guetschel2026 only.Changes
models/util.py:_numpy_scalars_to_pythonturns NumPy scalars into Python numbers with.item(), also inside plain lists, tuples and dicts.build_model_configapplies it before its JSON check (the path withouthuggingface_hub).models/base.py: the mixin's_is_jsonableand_encode_argapply it, so the Hub config keeps these values;_update_init_kwargsapplies it forreset_head.test_base.pyand a changelog entry.Testing
Windows 11, Python 3.12, torch 2.15.0 CPU,
pytest -n 2 -q -rfEs. Same commands on master and on this branch, first at 92cb203, then again after merging master at f2b9783. Counts are passed / failed / skipped, master then branch.test_config.py,test_huggingface.py,test_base.py(f2b9783): 846 / 5 / 22 and 849 / 5 / 22. The 3 extra are the new tests; the 5 failures, on both, are the symlink tests oftest_huggingface.py(WinError 1314, no symlink privilege).test_integration.py(92cb203): 1153 / 1 / 209 and 1154 / 0 / 209. The master failure is a crashed xdist worker; that test passes alone on both.test_model_contract.py(92cb203): 1359 / 0 / 110 on both.This branch after the merge with f2b9783,
-n 3:test_integration.py1154 / 0 / 209 andtest_model_contract.py1359 / 0 / 110, as before the merge.The other 43 files of
test/unit_tests/models/(92cb203): 5025 / 6 / 159 (47 xfailed) on both. The 6 failures are a WindowsUnicodeDecodeErrorintest_model_rules.pyandtest_private_blocks.py; withPYTHONUTF8=1, 17 / 0 / 0 on both.The model test files the merge changed,
test_models.py,test_foundation_models.py,test_codebrain_compile.py(f2b9783): 1457 / 0 / 28 on both.Hub and serialization files outside
models/(test_hub_integration.py,test_hub_formats.py,test_serialization.py,test_util.py, 92cb203): 111 / 0 / 119 on both.Skip reasons, identical on both: no network, no CUDA, MPS or other accelerator, no CPU bfloat16 LSTM kernel,
torch.compileon Windows, export not possible on this platform or too slow for CI, missing optional packages (zarr,peft,pywt,ptwt), models that are not classifiers or have no embedding or activation parameter, fixed-length or tokenizer models, and the contract exemptions the tests print (meta tensors, noch_mask, graph fitted later, fixed or grouped montages, 5-D inputs).Mutations, each one undoing one changed line or helper branch: the 7 mutants all fail a new test (the
huborinit-kwargscase, or thereset_headtest).repr(get_config())of the 104 models ofmodels_mandatory_parameters, built with plain Python arguments, on both config paths: identical to master.pre-commiton the changed files (command in the checklist below): passed.New or changed behavior is covered by relevant regression tests (or N/A with reason)
Style checks recorded, e.g.
pre-commit run --files <changed files>docs/whats_new.rstupdated (at most 3 lines), or theno changeloglabel, which a maintainer adds, for changes to code that is not released yet (the changelog CI check requires one of the two)Notes for reviewers
from_configand the loading code are not modified.np.float32is saved at its float32 value (np.float32(0.1)gives0.10000000149011612), the value the model received..tolist()round-trips BaRISTA and CSBrain in a local probe, but rebuilds the model with a list where the user passed an array. I would rather do that in a separate PR.BaseConcatDataset.save.