Linalg.conv_2d_nchw_fchw' op inferred input/output operand #0 has shape's dimension #3 to be greater than or equal

This is my code.

memref.global "private" @input : memref<1x5x5x3xi8> = dense<[[[[1,1,1], [0,0,0], [-1,-1, -1], [0,0,0], [1,1,1]],
                                                              [[1,1,1], [0,0,0], [-1,-1,-1], [0,0,0], [1,1,1]],
                                                              [[1,1,1], [0,0,0], [-1,-1,-1], [0,0,0], [1,1,1]],
                                                              [[1,1,1], [0,0,0], [-1,-1,-1], [0,0,0], [1,1,1]],
                                                              [[1,1,1], [0,0,0], [-1,-1,-1], [0,0,0], [1,1,1]]]]>

// outChannels = 2 kernelDim = 3 inChannels = 3
memref.global "private" @weight : memref<2x3x3x3xi8> = dense<[[[[1,2,1],[1,2,1],[1,2,1]],
                                                                [[1,2,1],[1,2,1],[1,2,1]],
                                                                [[1,2,1],[1,2,1],[1,2,1]]],
                                                                [[[1,2,1],[1,2,1],[1,2,1]],
                                                                [[1,2,1],[1,2,1],[1,2,1]],
                                                                [[1,2,1],[1,2,1],[1,2,1]]]]>

func.func @main() {
  %mem0 = memref.get_global @input  : memref<1x5x5x3xi8> 
  %mem1 = memref.get_global @weight : memref<2x3x3x3xi8>
  %mem2 = memref.alloc() : memref<1x3x3x2xi8> 
  linalg.conv_2d_nchw_fchw 
    ins (%mem0, %mem1 : memref<1x5x5x3xi8>, memref<2x3x3x3xi8>)
  outs(%mem2 : memref<1x3x3x2xi8>)
  return 
}

When i run opt,it says

error: 'linalg.conv_2d_nchw_fchw' op inferred input/output operand #0 has shape's dimension #3 to be greater than or equal to 4, but found 3

At the beginning, my inChannels were 1, it sayed

error: 'linalg.conv_2d_nchw_fchw' op inferred input/output operand #0 has shape's dimension #3 to be greater than or equal to 2, but found 1

then I made the inChannels 2, it sayed

error: 'linalg.conv_2d_nchw_fchw' op inferred input/output operand #0 has shape's dimension #3 to be greater than or equal to 3, but found 2

then I made the inChannels 3, it sayed

error: 'linalg.conv_2d_nchw_fchw' op inferred input/output operand #0 has shape's dimension #3 to be greater than or equal to 4, but found 3

I don’t know what I’ve set up wrong, but if anyone can help me, I’d appreciate it.Thanks!

The number of channels in the weights (3) doesn’t match the number of channels in your input (5). Also, the size of the output needs to be adjusted. (I find it helpful to look at mlir/python/mlir/dialects/linalg/opdsl/ops/core_named_ops.py to reason about the dimensions.) Something like this seems to be accepted by mlir-opt:

memref.global "private" @input : memref<1x5x5x3xi8> = dense<[[[[1,1,1], [0,0,0], [-1,-1, -1], [0,0,0], [1,1,1]],
                                                              [[1,1,1], [0,0,0], [-1,-1,-1], [0,0,0], [1,1,1]],
                                                              [[1,1,1], [0,0,0], [-1,-1,-1], [0,0,0], [1,1,1]],
                                                              [[1,1,1], [0,0,0], [-1,-1,-1], [0,0,0], [1,1,1]],
                                                              [[1,1,1], [0,0,0], [-1,-1,-1], [0,0,0], [1,1,1]]]]>

// outChannels = 2 kernelDim = 3 inChannels = 5
memref.global "private" @weight : memref<2x5x3x3xi8> = dense<[[[[1,2,1],[1,2,1],[1,2,1]],
                                                               [[1,2,1],[1,2,1],[1,2,1]],
                                                               [[1,2,1],[1,2,1],[1,2,1]],
                                                               [[1,2,1],[1,2,1],[1,2,1]],
                                                               [[1,2,1],[1,2,1],[1,2,1]]
                                                              ],
                                                              [[[1,2,1],[1,2,1],[1,2,1]],
                                                               [[1,2,1],[1,2,1],[1,2,1]],
                                                               [[1,2,1],[1,2,1],[1,2,1]],
                                                               [[1,2,1],[1,2,1],[1,2,1]],
                                                               [[1,2,1],[1,2,1],[1,2,1]]
                                                              ]
                                                             ]>

func.func @main() {
  %mem0 = memref.get_global @input  : memref<1x5x5x3xi8> 
  %mem1 = memref.get_global @weight : memref<2x5x3x3xi8>
  %mem2 = memref.alloc() : memref<1x2x3x1xi8> 
  linalg.conv_2d_nchw_fchw 
    ins (%mem0, %mem1 : memref<1x5x5x3xi8>, memref<2x5x3x3xi8>)
  outs(%mem2 : memref<1x2x3x1xi8>)
  return 
}
1 Like

Wow! I found out that it seems like I got something wrong, I wanted linalg.conv2d’s lower to the other op, but their inchannel is in a different position, I know where it’s wrong. thanks!