Skip to content

Cannot run convert_to_hf.py #201

@timpal0l

Description

@timpal0l

I ran composer main.py yamls/modernbert/modernbert-base-pretrain.yaml on a small toy dataset. And I wish to load the new model using huggingface transformes. But the convert_to_hf.py script throws the following error;

python convert_to_hf.py --output-name my-modernbert --output-dir my-folder --input-checkpoint checkpoints/modernbert-base-pretrain/ep0-ba16-rank0.pt
/home/tim/anaconda3/envs/bert24/lib/python3.11/site-packages/composer/utils/checkpoint.py:117: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.
  return torch.load(_ensure_valid_checkpoint(checkpoint_filepath), map_location=map_location)
╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
│ /home/tim/ModernBERT/convert_to_hf.py:100 in main                                                │
│                                                                                                  │
│    97Convert a ModernBERT Composer checkpoint to HuggingFace pretrained format.             │
│    98 │   """                                                                                    │
│    99 │   target_path = f"{output_dir}/{output_name}"                                            │
│ ❱ 100 │   write_huggingface_pretrained_from_composer_checkpoint(input_checkpoint, target_path)   │
│   101 │                                                                                          │
│   102 │   # Process pytorch_model.bin                                                            │
│   103 │   state_dict_path = f"{target_path}/pytorch_model.bin"                                   │
│                                                                                                  │
│ /home/tim/anaconda3/envs/bert24/lib/python3.11/site-packages/composer/models/huggingface.py:907  │
│ in write_huggingface_pretrained_from_composer_checkpoint                                         │
│                                                                                                  │
│   904 │                                                                                          │
│   905 │   # load tokenizer                                                                       │
│   906 │   hf_state = composer_state_dict['state']['integrations']['huggingface']                 │
│ ❱ 907 │   hf_tokenizer = HuggingFaceModel.load_huggingface_tokenizer_from_saved_state(hf_state   │
│   908 │   assert hf_tokenizer is not None                                                        │
│   909 │   hf_tokenizer.save_pretrained(output_folder)                                            │
│   910                                                                                            │
│                                                                                                  │
│ /home/tim/anaconda3/envs/bert24/lib/python3.11/site-packages/composer/models/huggingface.py:290  │
│ in load_huggingface_tokenizer_from_saved_state                                                   │
│                                                                                                  │
│   287 │   │   │   │   │   with open(tokenizer_file_path, 'wb') as _f:                            │
│   288 │   │   │   │   │   │   _f.write(s.serialized_model_proto())                               │
│   289 │   │   │                                                                                  │
│ ❱ 290 │   │   │   hf_tokenizer = transformers.AutoTokenizer.from_pretrained(                     │
│   291 │   │   │   │   tokenizer_save_dir,                                                        │
│   292 │   │   │   │   trust_remote_code=trust_remote_code,                                       │
│   293 │   │   │   )                                                                              │
│                                                                                                  │
│ /home/tim/anaconda3/envs/bert24/lib/python3.11/site-packages/transformers/models/auto/tokenizati │
│ on_auto.py:897 in from_pretrained                                                                │
│                                                                                                  │
│   894 │   │   │   │   raise ValueError(                                                          │
│   895 │   │   │   │   │   f"Tokenizer class {tokenizer_class_candidate} does not exist or is n   │
│   896 │   │   │   │   )                                                                          │
│ ❱ 897 │   │   │   return tokenizer_class.from_pretrained(pretrained_model_name_or_path, *input   │
│   898 │   │                                                                                      │
│   899 │   │   # Otherwise we have to be creative.                                                │
│   900 │   │   # if model is an encoder decoder, the encoder tokenizer class is used by default   │
│                                                                                                  │
│ /home/tim/anaconda3/envs/bert24/lib/python3.11/site-packages/transformers/tokenization_utils_bas │
│ e.py:2271 in from_pretrained                                                                     │
│                                                                                                  │
│   2268 │   │   │   else:                                                                         │
│   2269 │   │   │   │   logger.info(f"loading file {file_path} from cache at {resolved_vocab_fil  │
│   2270 │   │                                                                                     │
│ ❱ 2271 │   │   return cls._from_pretrained(                                                      │
│   2272 │   │   │   resolved_vocab_files,                                                         │
│   2273 │   │   │   pretrained_model_name_or_path,                                                │
│   2274 │   │   │   init_configuration,                                                           │
│                                                                                                  │
│ /home/tim/anaconda3/envs/bert24/lib/python3.11/site-packages/transformers/tokenization_utils_bas │
│ e.py:2505 in _from_pretrained                                                                    │
│                                                                                                  │
│   2502 │   │                                                                                     │
│   2503 │   │   # Instantiate the tokenizer.                                                      │
│   2504 │   │   try:                                                                              │
│ ❱ 2505 │   │   │   tokenizer = cls(*init_inputs, **init_kwargs)                                  │
│   2506 │   │   except OSError:                                                                   │
│   2507 │   │   │   raise OSError(                                                                │
│   2508 │   │   │   │   "Unable to load vocabulary from file. "                                   │
│                                                                                                  │
│ /home/tim/anaconda3/envs/bert24/lib/python3.11/site-packages/transformers/tokenization_utils_fas │
│ t.py:115 in __init__                                                                             │
│                                                                                                  │
│   112 │   │   │   fast_tokenizer = copy.deepcopy(tokenizer_object)                               │
│   113 │   │   elif fast_tokenizer_file is not None and not from_slow:                            │
│   114 │   │   │   # We have a serialization from tokenizers which let us directly build the ba   │
│ ❱ 115 │   │   │   fast_tokenizer = TokenizerFast.from_file(fast_tokenizer_file)                  │
│   116 │   │   elif slow_tokenizer is not None:                                                   │
│   117 │   │   │   # We need to convert a slow tokenizer to build the backend                     │118 │   │   │   fast_tokenizer = convert_slow_tokenizer(slow_tokenizer)                        │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
Exception: data did not match any variant of untagged enum ModelWrapper at line 1 column 2128613
pip list
aiohappyeyeballs            2.4.0
aiohttp                     3.10.5
aiosignal                   1.3.1
antlr4-python3-runtime      4.9.3
anyio                       4.4.0
appdirs                     1.4.4
argcomplete                 3.5.0
arrow                       1.3.0
attrs                       24.2.0
azure-core                  1.30.2
azure-identity              1.17.1
azure-storage-blob          12.22.0
azure-storage-file-datalake 12.16.0
backoff                     2.2.1
bcrypt                      4.2.0
binaryornot                 0.4.4
boto3                       1.35.2
botocore                    1.35.2
Brotli                      1.1.0
cachetools                  5.5.0
certifi                     2024.7.4
cffi                        1.17.0
chardet                     5.2.0
charset-normalizer          3.3.2
circuitbreaker              1.4.0
click                       8.1.7
colorama                    0.4.6
contourpy                   1.2.1
cookiecutter                2.6.0
coolname                    2.2.0
cramjam                     2.8.3
cryptography                41.0.7
cycler                      0.12.1
dataclasses                 0.8
datasets                    2.21.0
dill                        0.3.8
docker-pycreds              0.4.0
einops                      0.8.0
evaluate                    0.4.1
exceptiongroup              1.2.2
execnet                     2.1.1
filelock                    3.15.4
flash-attn                  2.6.3
fonttools                   4.53.1
frozenlist                  1.4.1
fsspec                      2024.6.1
gitdb                       4.0.11
GitPython                   3.1.43
gmpy2                       2.1.5
google-api-core             2.19.1
google-auth                 2.33.0
google-cloud-core           2.4.1
google-cloud-storage        2.10.0
google-crc32c               1.1.2
google-resumable-media      2.7.2
googleapis-common-protos    1.63.2
gql                         3.5.0
graphql-core                3.2.3
grpcio                      1.62.2
h2                          4.1.0
hpack                       4.0.0
huggingface_hub             0.24.6
huggingface_hub             0.24.6
huggingface_hub             0.24.6
hyperframe                  6.0.1
idna                        3.7
importlib_metadata          8.4.0
iniconfig                   2.0.0
isodate                     0.6.1
Jinja2                      3.1.4
jmespath                    1.0.1
kiwisolver                  1.4.5
lightning-utilities         0.11.6
llvmlite                    0.43.0
markdown-it-py              3.0.0
MarkupSafe                  2.1.5
matplotlib                  3.9.2
mdurl                       0.1.2
mosaicml                    0.24.1
mosaicml-cli                0.6.41
mosaicml-streaming          0.8.0
mpmath                      1.3.0
msal                        1.30.0
msal-extensions             1.1.0
multidict                   6.0.5
multiprocess                0.70.16
munkres                     1.1.4
networkx                    3.3
ninja                       1.11.1.1
numba                       0.60.0
numpy                       1.26.4
oci                         2.132.0
omegaconf                   2.3.0
packaging                   24.1
pandas                      2.2.2
paramiko                    3.4.1
pathtools                   0.1.2
pillow                      10.3.0
pip                         24.2
platformdirs                4.2.2
pluggy                      1.5.0
portalocker                 2.10.1
prompt-toolkit              3.0.36
proto-plus                  1.23.0
protobuf                    4.25.3
psutil                      6.0.0
py-cpuinfo                  9.0.0
pyarrow                     17.0.0
pyarrow-hotfix              0.6
pyasn1                      0.6.0
pyasn1_modules              0.4.0
pycairo                     1.26.1
pycparser                   2.22
Pygments                    2.18.0
PyGObject                   3.48.2
PyJWT                       2.9.0
PyNaCl                      1.5.0
pyOpenSSL                   23.3.0
pyparsing                   3.1.2
PySide6                     6.7.2
PySocks                     1.7.1
pytest                      8.3.2
pytest-xdist                3.6.1
python-dateutil             2.9.0.post0
python-slugify              8.0.4
python-snappy               0.7.2
pytorch_ranger              0.1.1
pytz                        2024.1
pyu2f                       0.1.5
PyYAML                      6.0.2
questionary                 2.0.1
regex                       2024.7.24
requests                    2.32.3
responses                   0.18.0
rich                        13.7.1
rsa                         4.9
ruamel.yaml                 0.18.6
ruamel.yaml.clib            0.2.8
s3transfer                  0.10.2
safetensors                 0.4.4
sentry-sdk                  2.13.0
setproctitle                1.3.3
setuptools                  72.2.0
shellingham                 1.5.4
shiboken6                   6.7.2
six                         1.16.0
smmap                       5.0.0
sniffio                     1.3.1
sympy                       1.13.2
tabulate                    0.9.0
termcolor                   2.4.0
text-unidecode              1.3
tokenizers                  0.19.1
tomli                       2.0.1
torch                       2.4.0
torch-optimi                0.2.1
torch-optimizer             0.3.0
torchaudio                  2.4.0
torchmetrics                1.4.0.post0
torchvision                 0.19.0
tornado                     6.4.1
tqdm                        4.66.5
transformers                4.44.1
triton                      3.0.0
typer                       0.12.4
typer-slim                  0.12.4
types-python-dateutil       2.9.0.20240821
typing_extensions           4.12.2
tzdata                      2024.1
urllib3                     2.2.2
validators                  0.33.0
wandb                       0.16.6
wcwidth                     0.2.13
websockets                  11.0.3
wheel                       0.44.0
xxhash                      3.5.0
yarl                        1.9.4
zipp                        3.20.0
zstandard                   0.23.0
zstd                        1.5.5.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions