onnxruntime icon indicating copy to clipboard operation
onnxruntime copied to clipboard

[Web] Failed to compile shader on WebGL

Open hunghuynh1011 opened this issue 3 years ago • 1 comments

Describe the issue

My model converted from tensorflow to onnx. It run on wasm successfully, but fail on WebGL.

Error log:

ortCreate.ts:42 Error: Failed to compile shader: ERROR: 0:37: 'X' : undeclared identifier
ERROR: 0:37: 'texture' : no matching overloaded function found
ERROR: 0:37: '=' : dimension mismatch
ERROR: 0:37: '=' : cannot convert from 'const mediump float' to 'highp 4-component vector of float'

Shader source:
#version 300 es
    precision highp float;
    precision highp int;
    precision highp sampler2D;
    in vec2 TexCoords;
    out vec4 outputColor;
    const vec2 halfCR = vec2(0.5, 0.5);

    // Custom vector types to handle higher dimenalities.
    struct ivec5
    {
      int x;
      int y;
      int z;
      int w;
      int u;
    };

    struct ivec6
    {
      int x;
      int y;
      int z;
      int w;
      int u;
      int v;
    };

    int imod(int x, int y) {
      return x - y * (x / y);
    }

    
    uniform sampler2D A;
    
    void main() {
                    vec4 v = texture(X, TexCoords);
                    outputColor = v;
                }
    at t.WebGLContext.compileShader (ort-web.min.js:6:341417)
    at t.ProgramManager.compile (ort-web.min.js:6:321731)
    at ort-web.min.js:6:321119
    at t.Profiler.event (ort-web.min.js:6:365352)
    at t.ProgramManager.build (ort-web.min.js:6:321053)
    at t.WebGLInferenceHandler.executeProgram (ort-web.min.js:6:209696)
    at t.WebGLInferenceHandler.run (ort-web.min.js:6:209830)
    at t.resize [as impl] (ort-web.min.js:6:281656)
    at ort-web.min.js:6:350100
    at t.Profiler.event (ort-web.min.js:6:365352)

Could you please help me figure out this issue?

Thank you

To reproduce

code to load model and choose backend

export async function createModelCpu(model: ArrayBuffer): Promise<InferenceSession> {
  init();
  return await InferenceSession.create(model, {executionProviders: ['wasm']});
}
export async function createModelGpu(model: ArrayBuffer): Promise<InferenceSession> {
  init();
  return await InferenceSession.create(model, {executionProviders: ['webgl']});
}

Urgency

No response

ONNX Runtime Installation

Released Package

ONNX Runtime Version or Commit ID

1.12.1

Execution Provider

WebGL

hunghuynh1011 avatar Sep 12 '22 09:09 hunghuynh1011

This looks like the WebGL shader is generated incorrectly. Could you share more details ( eg. sharing the model ) so that I can try to reproduce the issue?

fs-eire avatar Sep 19 '22 23:09 fs-eire

I get the the same error but with pytorch. I suspect the F.interpolate function...

class ScaleDown(nn.Module):
    def __init__(self, a=1):
        super().__init__()
        self.a = a
    def forward(self, x):
        return F.interpolate(x, (x.size(-2)//self.a, x.size(-1)//self.a), mode='area')

class ScaleUp(nn.Module):
    def __init__(self, a=1):
        super().__init__()
        self.a = a
    def forward(self, x):
        return F.interpolate(x, (x.size(-2)*self.a, x.size(-1)*self.a), mode='nearest')

Here is my onnx file https://drive.google.com/file/d/1mBZjxBCBNATeqvaE9BWpOC32YOysMwd-/view?usp=sharing

KoKuToru avatar Feb 10 '23 17:02 KoKuToru