huggingface / transformers

🤗 Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.
https://huggingface.co/transformers
Apache License 2.0
132.87k stars 26.5k forks source link

Converting facebook-encodec to onnx fails with KeyError: 'encodec' #27602

Closed kalradivyanshu closed 8 months ago

kalradivyanshu commented 10 months ago

System Info

Who can help?

@sanchit-gandhi

Information

Tasks

Reproduction

  1. Download all files in https://huggingface.co/facebook/encodec_24khz/tree/main to a folder encodec24khz
  2. run python -m transformers.onnx --model=encodec24khz onnx/

Expected behavior

The encodec model should be exported to ONNX as described here: https://huggingface.co/docs/transformers/v4.17.0/en/serialization#exporting-a-model-to-onnx.

But it fails and gives the following error:

❯ python -m transformers.onnx --model=encodec24khz onnx/
Local PyTorch model found.
Framework not requested. Using torch to export to ONNX.
Traceback (most recent call last):
  File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/user/.local/lib/python3.9/site-packages/transformers/onnx/__main__.py", line 240, in <module>
    main()
  File "/home/user/.local/lib/python3.9/site-packages/transformers/onnx/__main__.py", line 232, in main
    export_with_transformers(args)
  File "/home/user/.local/lib/python3.9/site-packages/transformers/onnx/__main__.py", line 75, in export_with_transformers
    model = FeaturesManager.get_model_from_feature(
  File "/home/user/.local/lib/python3.9/site-packages/transformers/onnx/features.py", line 701, in get_model_from_feature
    model = model_class.from_pretrained(model, cache_dir=cache_dir)
  File "/home/user/.local/lib/python3.9/site-packages/transformers/models/auto/auto_factory.py", line 441, in from_pretrained
    config, kwargs = AutoConfig.from_pretrained(
  File "/home/user/.local/lib/python3.9/site-packages/transformers/models/auto/configuration_auto.py", line 937, in from_pretrained
    config_class = CONFIG_MAPPING[config_dict["model_type"]]
  File "/home/user/.local/lib/python3.9/site-packages/transformers/models/auto/configuration_auto.py", line 643, in __getitem__
    raise KeyError(key)
KeyError: 'encodec'
ArthurZucker commented 10 months ago

Hey! Thanks for this, I'll add this as a feature request as the error I am getting is the following:

Framework not requested. Using torch to export to ONNX.
/Users/arthurzucker/.pyenv/versions/py310/lib/python3.10/site-packages/torch/nn/utils/weight_norm.py:30: UserWarning: torch.nn.utils.weight_norm is deprecated in favor of torch.nn.utils.parametrizations.weight_norm.
  warnings.warn("torch.nn.utils.weight_norm is deprecated in favor of torch.nn.utils.parametrizations.weight_norm.")
Traceback (most recent call last):
  File "/Users/arthurzucker/.pyenv/versions/3.10.13/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/Users/arthurzucker/.pyenv/versions/3.10.13/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/Users/arthurzucker/Work/transformers/src/transformers/onnx/__main__.py", line 242, in <module>
    main()
  File "/Users/arthurzucker/Work/transformers/src/transformers/onnx/__main__.py", line 234, in main
    export_with_transformers(args)
  File "/Users/arthurzucker/Work/transformers/src/transformers/onnx/__main__.py", line 79, in export_with_transformers
    model_kind, model_onnx_config = FeaturesManager.check_supported_model_or_raise(model, feature=args.feature)
  File "/Users/arthurzucker/Work/transformers/src/transformers/onnx/features.py", line 728, in check_supported_model_or_raise
    model_features = FeaturesManager.get_supported_features_for_model_type(model_type, model_name=model_name)
  File "/Users/arthurzucker/Work/transformers/src/transformers/onnx/features.py", line 575, in get_supported_features_for_model_type
    raise KeyError(
KeyError: "encodec is not supported yet. Only ['albert', 'bart', 'beit', 'bert', 'big-bird', 'bigbird-pegasus', 'blenderbot', 'blenderbot-small', 'bloom', 'camembert', 'clip', 'codegen', 'convbert', 'convnext', 'data2vec-text', 'data2vec-vision', 'deberta', 'deberta-v2', 'deit', 'detr', 'distilbert', 'electra', 'flaubert', 'gpt2', 'gptj', 'gpt-neo', 'groupvit', 'ibert', 'imagegpt', 'layoutlm', 'layoutlmv3', 'levit', 'longt5', 'longformer', 'marian', 'mbart', 'mobilebert', 'mobilenet-v1', 'mobilenet-v2', 'mobilevit', 'mt5', 'm2m-100', 'owlvit', 'perceiver', 'poolformer', 'rembert', 'resnet', 'roberta', 'roformer', 'segformer', 'squeezebert', 'swin', 't5', 'vision-encoder-decoder', 'vit', 'whisper', 'xlm', 'xlm-roberta', 'yolos'] are supported. If you want to support encodec please propose a PR or open up an issue."

which is expected.

kalradivyanshu commented 10 months ago

I can work on this @ArthurZucker, can you guide me on how i can get started?

ArthurZucker commented 10 months ago

Hey! 🤗 I think @fxmarty will be of better help than me.

fxmarty commented 8 months ago

@kalradivyanshu closing as duplicate of https://github.com/huggingface/optimum/issues/1545

transformers.onnx is deprecated in favor of optimum.exporters.onnx, for reference: https://huggingface.co/docs/transformers/serialization

There is an open PR for the ONNX support for encodec: https://github.com/huggingface/optimum/pull/1620