iree: tensor.cast not working

Describe the bug

tensor.cast op doesn’t seem to be supported

To Reproduce

Compile the following IR:

func @forward(%arg0: tensor<4x4xf32>) -> tensor<?x?xf32> {
  %5 = tensor.cast %arg0 : tensor<4x4xf32> to tensor<?x?xf32>
  return %5 : tensor<?x?xf32>
}

I see the error:

<stdin>:2:8: error: 'tensor.cast' op operand type '!hal.buffer_view' and result type 'tensor<?x?xf32>' are cast incompatible                                                                                      
  %5 = tensor.cast %arg0 : tensor<4x4xf32> to tensor<?x?xf32>
       ^
<stdin>:1:1: note: called from
func @forward(%arg0: tensor<4x4xf32>, %arg1: tensor<4x4xf32>) -> tensor<?x?xf32> {
^
<stdin>:0:0: error: conversion from source -> vm failed

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (8 by maintainers)

Commits related to this issue

Most upvoted comments

These boundary conditions drive me nuts and need some more rigor.

While not how hal.tensor.cast is defined now (i.e. it lacks builders for generically constructing this form), I’m not sure that the following is an incorrect use of the op (i.e. if we converted tensor.cast -> hal.tensor.cast). Like Ben said, it isn’t great because as non-shape-carrying, it has to use local shape resolution (i.e. memref.dim and looking up the chain), but for these kind of widening cases, I’m not sure it is wrong (although, likely missing canonicalizations to be good).

  func private @_forward(%arg0: tensor<4x4xf32> loc("<stdin>":1:15)) -> tensor<?x?xf32> {
    %0 = tensor.cast %arg0 : tensor<4x4xf32> to tensor<?x?xf32> loc(#loc3)
    return %0 : tensor<?x?xf32> loc(#loc4)
  }

To:

  func private @_forward(%arg0: tensor<4x4xf32> loc("<stdin>":1:15)) -> tensor<?x?xf32> {
    %0 = memref.dim %arg0, %c0 : tensor<4x4xf32>
    %1 = memref.dim %arg0, %c1 : tensor<4x4xf32>
    %2 = hal.tensor.cast %arg0 : tensor<4x4xf32> -> tensor<?x?xf32>{%0, %1}
    return %2 : tensor<?x?xf32>
  }

I’m not an expert on this, though.