I am following this tutorial: https://huggingface.co/transformers/torchscript.html
to create a trace of my custom BERT model, however when running the exact same dummy_input
I receive an error:
TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect.
We cant record the data flow of Python values, so this value will be treated as a constant in the future.
Having loaded in my model and tokenizer, the code to create the trace is the following:
text = "[CLS] Who was Jim Henson ? [SEP] Jim Henson was a puppeteer [SEP]"
tokenized_text = tokenizer.tokenize(text)
# Masking one of the input tokens
masked_index = 8
tokenized_text[masked_index] = '[MASK]'
indexed_tokens = tokenizer.convert_tokens_to_ids(tokenized_text)
segments_ids = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1]
tokens_tensor = torch.tensor([indexed_tokens])
segments_tensors = torch.tensor([segments_ids])
dummy_input = [tokens_tensor, segments_tensors]
traced_model = torch.jit.trace(model, dummy_input)
The dummy_input
is a list of tensors so I'm not sure where the Boolean
type is coming into play here. Does anyone understand why this error is occurring and whether the Boolean conversion is happening?
Many Thanks