Onnx-tensorrt: No importer registered for op: GatherElements

Created on 23 Jul 2020  路  6Comments  路  Source: onnx/onnx-tensorrt

When importing a model from ONNX, I get:

In node 173 (parseGraph): UNSUPPORTED_NODE: No importer registered for op: GatherElements

When will GatherElements be supported?
Or does anyone know a workaround?

Most helpful comment

@bonlime Thanks. I re-implemented torch.gather using array indexing as follows:

import torch
import itertools
import operator

def gather(input, dim, index):
    indices = [torch.arange(size, device=index.device) for size in index.shape]
    indices = list(torch.meshgrid(*indices))
    indices[dim] = index
    sizes = list(reversed(list(itertools.accumulate(reversed(index.shape), operator.mul))))
    index = sum((index * size for index, size in zip(indices, sizes[1:] + [1])))
    output = input.flatten()[index]
    return output

input = torch.randn(3, 3, 3)
index = torch.randint_like(input, 3, dtype=torch.long)
output1 = torch.gather(input, 1, index)
output2 = gather(input, 1, index)
assert((output1 == output2).all())

This works, and I successfully imported a model from ONNX.

All 6 comments

Encountered the same problem today.
[ERROR] INVALID_ARGUMENT: getPluginCreator could not find plugin GatherElements version 1
It looks like for now the only option is to rewrite the code to avoid using it (I'm porting pytorch -> onnx first and only then onnx -> trt)

FYI, I've managed to fix this problem by using usual array indexing instead of torch.gather. don't know if it could help in your case

@bonlime Thanks. I re-implemented torch.gather using array indexing as follows:

import torch
import itertools
import operator

def gather(input, dim, index):
    indices = [torch.arange(size, device=index.device) for size in index.shape]
    indices = list(torch.meshgrid(*indices))
    indices[dim] = index
    sizes = list(reversed(list(itertools.accumulate(reversed(index.shape), operator.mul))))
    index = sum((index * size for index, size in zip(indices, sizes[1:] + [1])))
    output = input.flatten()[index]
    return output

input = torch.randn(3, 3, 3)
index = torch.randint_like(input, 3, dtype=torch.long)
output1 = torch.gather(input, 1, index)
output2 = gather(input, 1, index)
assert((output1 == output2).all())

This works, and I successfully imported a model from ONNX.

@bonlime Thanks. I re-implemented torch.gather using array indexing as follows:

import torch
import itertools
import operator

def gather(input, dim, index):
    indices = [torch.arange(size, device=index.device) for size in index.shape]
    indices = list(torch.meshgrid(*indices))
    indices[dim] = index
    sizes = list(reversed(list(itertools.accumulate(reversed(index.shape), operator.mul))))
    index = sum((index * size for index, size in zip(indices, sizes[1:] + [1])))
    output = input.flatten()[index]
    return output

input = torch.randn(3, 3, 3)
index = torch.randint_like(input, 3, dtype=torch.long)
output1 = torch.gather(input, 1, index)
output2 = gather(input, 1, index)
assert((output1 == output2).all())

This works, and I successfully imported a model from ONNX.

Thank you for sharing this code, but when I implement your code it will be incorrect when input.size(dim)!=index.size(dim)
so I change it to:

import torch
import itertools
import operator

def gather(input, dim, index):
    indices = [torch.arange(size, device=index.device) for size in index.shape]
    indices = list(torch.meshgrid(*indices))
    indices[dim] = index
    sizes = list(reversed(list(itertools.accumulate(reversed(input.shape), operator.mul))))
    index = sum((index * size for index, size in zip(indices, sizes[1:] + [1])))
    output = input.flatten()[index]
    return output

input = torch.randn(3, 3, 3)
index = torch.randint_like(input, 3, dtype=torch.long)
output1 = torch.gather(input, 1, index)
output2 = gather(input, 1, index)
assert((output1 == output2).all())

just change this line sizes = list(reversed(list(itertools.accumulate(reversed(input.shape), operator.mul))))
This will be work when dimension size is different
Thanks again!

@chenfeiyi That really helped me. Thank you!

@chenfeiyi Thanks! You saved my life...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andrew-yang0722 picture andrew-yang0722  路  3Comments

aif2017 picture aif2017  路  3Comments

austinmw picture austinmw  路  6Comments

shroudie picture shroudie  路  3Comments

felixplum picture felixplum  路  7Comments