[Solved] size_from_dim: Assertion `dim >= 0 && (size_t)dim < sizes_.size()` failed.

Problems encountered

/home/optimizer-master/third_party/onnx_common/tensor.h:117: size_from_dim: Assertion dim >= 0 && (size_t)dim < sizes_.size() failed.


Problem description

When import onnx, I encountered the above problems



If there is an op operation that cannot be exported during onnx export, this error will be reported
How to Solve:
find the operation that cannot be exported and add a judgment. If you export onnx, the op will not be used. An example is as follows

def forward(self, x):
    if torch.onnx.is_in_onnx_export():# if onnx
        return x * self.act(x)/6
    else: # normally
        return x * self.act(x + 3)/6

Read More: