microsoft / onnxjs

ONNX.js: run ONNX models using JavaScript
Other
1.77k stars 129 forks source link

Error when using Gather #179

Open elliotwaite opened 4 years ago

elliotwaite commented 4 years ago

When I try to use a graph that uses the Gather operator, I get the error: Failed to compile shader: ERROR: 0:87: '' : array size must be greater than zero.

I am not sure if this is an issue with PyTorch or ONNX.js, so I opened an issue with PyTorch as well here: https://github.com/pytorch/pytorch/issues/32956

Does the graph generated by PyTorch look valid?

To Reproduce

I used the following PyTorch code to generate the ONNX model:

import torch
import torch.nn as nn

class Model(nn.Module):
  def forward(self, x):
    x = x[:, 0]
    return x

def main():
  model = Model()
  dummy_input = torch.zeros(1, 2)
  torch.onnx.export(model, dummy_input, 'model.onnx', verbose=True)

if __name__ == '__main__':
  main()

This creates the model.onnx file and prints out the following graph information:

graph(%0 : Float(1, 2)):
  %1 : Tensor = onnx::Constant[value={0}]()
  %2 : Float(1) = onnx::Gather[axis=1](%0, %1)
  return (%2)

I then used the following ONNX.js code to try to run data through the model:

<html>
  <body>
    <script src="https://cdn.jsdelivr.net/npm/onnxjs/dist/onnx.min.js"></script>
    <script>
      async function test() {
        const sess = new onnx.InferenceSession()
        await sess.loadModel('./model.onnx')
        const input = new onnx.Tensor(new Float32Array(2), 'float32', [1, 2])
        const outputMap = await sess.run([input])
        const outputTensor = outputMap.values().next().value
        console.log(`Output tensor: ${outputTensor.data}`)
      }
      test();
    </script>
  </body>
</html>

And I got the following error in the browser's console:

Uncaught (in promise) Error: Failed to compile shader: ERROR: 0:87: '' : array size must be greater than zero

    at t.compileShader (webgl-context.ts:174)
    at t.compile (program-manager.ts:97)
    at program-manager.ts:67
    at t.event (instrument.ts:294)
    at t.build (program-manager.ts:64)
    at t.run (inference-handler.ts:26)
    at e.run (gather.ts:10)
    at t.<anonymous> (execution-plan.ts:104)
    at backend.ts:102
    at Object.next (backend.ts:102)
selmalee commented 3 years ago

+1