Skip to content

Instantly share code, notes, and snippets.

@HSILA
Last active July 2, 2022 06:15
Show Gist options
  • Save HSILA/8388b1925539c3a82271cbe437409623 to your computer and use it in GitHub Desktop.
Save HSILA/8388b1925539c3a82271cbe437409623 to your computer and use it in GitHub Desktop.
ONNX CumSum operator shouldn't get boolean but model checker won't check it
import torch
from torch import nn
import onnx
import onnxruntime
a = torch.tensor([12, 0, 5, 154], dtype=torch.float32)
# a.to(torch.bool) = tensor([True, False, True, True])
# ~a.to(torch.bool) = tensor([False, True, False, False])
class NeuralNetwork(nn.Module):
def __init__(self):
super(NeuralNetwork, self).__init__()
def forward(self, x):
not_mask = x.to(torch.bool)
x = not_mask.cumsum(0, dtype=torch.float32)
return x
model = NeuralNetwork()
model.eval()
x = torch.tensor([12, 0, 5, 154], dtype=torch.float32, requires_grad=True)
torch_out = model(x)
torch.onnx.export(model, x,
"test.onnx",
export_params=True,
opset_version=11,
do_constant_folding=True,
input_names = ['input'],
output_names = ['output'])
onnx_model = onnx.load("test.onnx")
onnx.checker.check_model(onnx_model) # No errors in this line, which should be
ort_session = onnxruntime.InferenceSession("test.onnx") # Error line
# InvalidGraph: [ONNXRuntimeError] : 10 : INVALID_GRAPH : Load model from test.onnx failed:This is an invalid model.
# Type Error: Type 'tensor(bool)' of input parameter (1) of operator (CumSum) in node (CumSum_2) is invalid.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment