Cannot export this model to TorchScript

#2
by YanbinZhu - opened

transformers-cli env

  • transformers version: 4.32.1
  • Platform: macOS-12.5.1-arm64-arm-64bit
  • Python version: 3.9.16
  • Huggingface_hub version: 0.16.4
  • Safetensors version: 0.3.3
  • Accelerate version: not installed
  • Accelerate config: not found
  • PyTorch version (GPU?): 2.0.1 (False)
  • Tensorflow version (GPU?): not installed (NA)
  • Flax version (CPU?/GPU?/TPU?): not installed (NA)
  • Jax version: not installed
  • JaxLib version: not installed
  • Using GPU in script?:
  • Using distributed or parallel set-up in script?:

Code:

import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification, BertConfig

model_name = "MoritzLaurer/DeBERTa-v3-large-mnli-fever-anli-ling-wanli"
tokenizer = AutoTokenizer.from_pretrained(model_name)

Initializing the model with the torchscript flag

Flag set to True even though it is not necessary as this model does not have an LM Head.

config = BertConfig(
vocab_size_or_config_json_file=32000,
hidden_size=768,
num_hidden_layers=12,
num_attention_heads=12,
intermediate_size=3072,
torchscript=True,
)

Instantiating the model

model = AutoModelForSequenceClassification.from_config(config)

model.eval()
premise = "Test case."
hypothesis = "issue"

inputs = tokenizer(premise, hypothesis, truncation=True, return_tensors="pt")

print(inputs)

model = AutoModelForSequenceClassification.from_pretrained(model_name, torchscript=True)
traced_model = torch.jit.trace(model, [inputs['input_ids'], inputs['attention_mask'], inputs['token_type_ids']])

torch.jit.save(traced_model, "DeBERTa_4.pt")

RuntimeError:
Could not export Python function call 'XSoftmax'. Remove calls to Python functions before export. Did you forget to add @script or @script_method annotation? If this is a nn.ModuleList, add it to constants:

This comment has been hidden

Sign up or log in to comment