question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IndexError because pytorch does not support int32 for indexing like onnx

See original GitHub issue

Been trying a bunch of things to solve this now but the error message isn’t very helpful. It just sounds like it’s just the input dtype that’s handled incorrectly.

  File "/home/richard/miniconda3/envs/3.9.13/lib/python3.9/site-packages/torch/fx/graph_module.py", line 630, in wrapped_call
    raise e.with_traceback(None)
IndexError: tensors used as indices must be long, byte or bool tensors

Here is the onnx file I’m trying to convert https://drive.google.com/file/d/1FX_D6dcYEoVssr-y29F5RLbhmhf4RmyZ/view?usp=sharing

It is supposed to take a tensor of type long. Here’s an example in json: https://drive.google.com/file/d/1yTSxwOY10g0cULEVt3KQZWSzziDpD8bC/view?usp=sharing

This is how I try to run it:

    model = torch.load(torch_path).eval().requires_grad_(False)

    tokens = torch.tensor(
        json.loads(Path(f"tests/{model_name}/tokens.json").read_text())
    ).long()
    text_encodings = torch.from_numpy(np.array(model(tokens)))

The onnx file works with onnxruntime and gives the correct result.

    tokens = np.array(
        json.loads(Path(f"tests/{model_name}/tokens.json").read_text())
    ).astype(np.int64)

    providers = ["CPUExecutionProvider"]
    m = rt.InferenceSession(onnx_path, providers=providers)
    text_encoding = m.run(output_names, dict(inputs=tokens))

Do you have any tips for what could be wrong?

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
senysenyseny16commented, Nov 25, 2022

Hi @samedii

Sorry for the wait, we will get back to this issue as soon as possible.

1reaction
samediicommented, Sep 29, 2022

@senysenyseny16 Thank you! I had to give up eventually on fixing the ONNX file manually. At the moment I’m running the text encoder via ONNX because it’s not used as often as the image encoder (text-guided image generation) but it would be very nice if it wasn’t necessary.

The package is open source if you want to know what it would be used for: https://github.com/samedii/pytorch-zero-lit

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · ENOT-AutoDL/onnx2torch - GitHub
Convert ONNX models to PyTorch. Contribute to ENOT-AutoDL/onnx2torch ... IndexError because pytorch does not support int32 for indexing like onnx.
Read more >
Indexing or masking in ONNX error while using ...
Hi, I am trying to do simple task which is indexing or masking and export it to onnx while using “ONNX_ATEN_FALLBACK” operator at...
Read more >
Pytorch: IndexError: index out of range in self. How to solve?
Last time I got this same IndexError: index out of range in self using BERT was because my input text was too long...
Read more >
PyTorch Release v1.3.0 | Exxact Blog
Here is the newest PyTorch release v1.3.0 featuring new mobile support, named tensors, quantization, type promotion, and many more new features.
Read more >
mxnet.ndarray.ndarray — Apache MXNet documentation
It needs to be caught here since the call doesn't even reach backend. size = 1 for ... This functions supports advanced indexing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found