Skip to content

tensor.cast not working #6368

Closed
Closed

Description

@silvasean

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

Activity

  1. added
    bug 🐞
    help wanted
    on Jun 30, 2021
  2. MaheshRavishankar commented on Jun 30, 2021

    @MaheshRavishankar
    Collaborator

    (would be useful to also attach a dump of -print-ir-after-all for such errors to see where the issue really is). Will do so in a bit and check.

  3. MaheshRavishankar commented on Jun 30, 2021

    @MaheshRavishankar
    Collaborator

    Thanks! Looks like we are missing a conversion from tensor.cast to hal.tensor.cast. I am not sure why we even have hal.tensor.cast in the inputs in one place and tensor.cast in the other...

  4. benvanik commented on Jun 30, 2021

    @benvanik
    Collaborator

    hal.tensor.cast is what we want on external ABI boundaries - it lets us explicitly specify the type moved across the program boundary (an iree_hal_buffer_view_t) and how to get its shape information. Internally within a module tensor.cast should be fine (though it doesn't carry shapes and is not a great op - it'd be better to have a variant that was like hal.tensor.cast as otherwise we'd need to insert things like the shapex.tie_shape stuff again to attach that information).

  5. MaheshRavishankar commented on Jun 30, 2021

    @MaheshRavishankar
    Collaborator

    Ok, actually I feel like this op should not even make it into flow. Its actually losing shape information and seems like an intermediate IR state.

  6. benvanik commented on Jun 30, 2021

    @benvanik
    Collaborator

    If this is a public function then we can do something like what we do with the SavedModelToIreeABI pass. That is what does this type casting on the tensorflow side, saying that tensorflow I/O comes into the module in as bufferviews or nested lists and other things that are frontend-specific and difficult/impossible to infer later on in the lowering. There's a generic version of this in WrapEntryPoints that is used for generic inputs so func(tensor) -> tensor becomes func(buffer_view) -> buffer_view (iree-translate adds that with the -iree-native-bindings-support option).

    If this is internal to the model then maybe a lowering to a flow.tensor.reshape could handle this.

  7. silvasean commented on Jun 30, 2021

    @silvasean
    ContributorAuthor

    There's two things going on here:

    1. The tensor.cast op itself. As mentioned, it is really an identity, and e.g after doing per-dimension shape inference becomes a no-op, EXCEPT
    2. At ABI boundaries, where we need to formally add/erase static type information to satisfy the function return type/function argument types

    In npcomp, we run shape inference which eliminates almost all of these sorts of ops (this is actually still at the Torch level, this op comes in from TorchToLinalg, but the same idea applies), and then a pass which propagates the information to the ABI boundaries to eliminate the need for these formal ops. Doing that relies on the assumption that tensor<4x4xf32> and tensor<?x?xf32> are passed the same way from user API's, which is true for IREE (but is not true e.g. for some of the upstream weird calling conventions where memrefs get split into a C ABI as a raw pointer + a number of 'index' values equal to the number of ?'s -- changing the number of ?'s is an ABI-breaking change in that case)

  8. benvanik commented on Jun 30, 2021

    @benvanik
    Collaborator

    Yeah - the ABI boundary is where we could handle these. We could, for example, extend WrapEntryPoints to look for these ops and replace them with the appropriate forms.

    What we previously had was func(tensor) lowering deep into the HAL dialect before it decided to turn the tensors into hal.buffer_view, but that was a mess as then if targeting a different runtime API like you mention it isn't guaranteed to become that (tflite passes just unshaped buffers and sends shape information in a side-channel, for example). It can also hide information from the linalg/flow levels that want to better carry shape dimensions around as SSA values rooted up to input/down to output.

    The new approach is to specify what types you expect at runtime on the ABI boundary (like an __attribute__((fastcall)) or __attribute__((stdcall)) in C) and insert the placeholder conversion for them - in the case of iree_hal_buffer_view_t being passed adding a hal.tensor.cast to move between that type/shape-erased !hal.buffer_view into the tensor<...>. This avoids the ambiguity lower in the stack as the HAL knows precisely when it needs to materialize the buffer view types vs. when it's dealing with an internal tensor that it can treat as effectively just an offset into a ringbuffer with no runtime materialization at all. The bonus is that this also enables all the other kind of interop that may be desired - for example, passing in dicts for kwargs that are fetched and unpacked into primitives, custom types, and buffer_views without anything lower in the stack needing to somehow infer that - especially tricky for custom types/other dialect interaction.

    So, now that we know there are two things - which are you hitting? Both I'm guessing?

  9. silvasean commented on Jun 30, 2021

    @silvasean
    ContributorAuthor

    I just looked, and I think it is mainly the ABI boundary one. I tried a 2 layer MLP but the tensor.cast ops end up disappearing through canonicalization currently. Probably more complex examples could result in tensor.cast ops that survive in the middle of the program, but that's not the immediate issue.

  10. stellaraccident commented on Jul 1, 2021

    @stellaraccident
    Collaborator

    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.

  11. stellaraccident commented on Jul 1, 2021

    @stellaraccident
    Collaborator

    I tried a 2 layer MLP but the tensor.cast ops end up disappearing through canonicalization currently.

    It is useful to distinguish these "loopback" cases that just return an operand and functions that do real work. I've had to find/fix a number of the loopback cases that don't show up in real programs.

  12. benvanik commented on Jul 1, 2021

    @benvanik
    Collaborator

    I think you are right about doing tensor_cast to HAL.tensor.cast early on for the public functions - we could make this part of WrapEntryPoints.

  13. 1 remaining item

  14. silvasean commented on Jul 1, 2021

    @silvasean
    ContributorAuthor

    It is useful to distinguish these "loopback" cases that just return an operand and functions that do real work. I've had to find/fix a number of the loopback cases that don't show up in real programs.

    Sorry, my statement was not clear. Canonicalization removes all the tensor.cast ops in the program except the "ABI" tensor.cast at the end before the return. It's not specific to this loopback test case (you can see in my reduced test case it is called %5 :) ).

    I should have said

    I tried a 2 layer MLP but the [internal] tensor.cast ops end up disappearing through canonicalization currently.

  15. MaheshRavishankar commented on Jul 1, 2021

    @MaheshRavishankar
    Collaborator

    @silvasean moving this back to you for now, till exact way forward is decided.

  16. stellaraccident commented on Jul 1, 2021

    @stellaraccident
    Collaborator

    I assigned to me but have hours left before going offline for an extended period. Will see if a solution emerges.

  17. added a commit that references this issue on Jul 1, 2021
    158b52d
  18. stellaraccident commented on Jul 1, 2021

    @stellaraccident
    Collaborator

    Since landing the above requires an LLVM bump to avoid a mid-air collision, someone else will need to pick it up next week as I'll be out. Maybe Yi can finish/land it? I pushed it to a branch in the main repo so that others can push commits to it and merge.

  19. added 2 commits that reference this issue on Jul 2, 2021
    845fe18
    30016e8
  20. added 3 commits that reference this issue on Jul 7, 2021
    c28d0d2
    3039bb8
    d04a098
  21. added a commit that references this issue on Jul 9, 2021
    cd247ca
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug 🐞help wanted

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions