Skip to content

iree-compile failure on simple i8 matmul with VMVX microkernels #9903

Closed

Description

@bjacob

This is blocking enabling i8 matmul e2e tests with vmvx microkernels enabled (#9904)

Testcase a.mlir:

func.func @matmul_DYNxDYNxi8_times_DYNxDYNxi8_into_DYNxDYNxi32(%lhs: tensor<?x?xi8>, %rhs: tensor<?x?xi8>, %acc: tensor<?x?xi32>) -> tensor<?x?xi32> {
  %result = linalg.matmul ins(%lhs, %rhs: tensor<?x?xi8>, tensor<?x?xi8>) outs(%acc: tensor<?x?xi32>) -> tensor<?x?xi32>
  return %result: tensor<?x?xi32>
}

Repro:

$ tools/iree-compile --output-format=vm-bytecode --mlir-print-op-on-diagnostic=false --iree-hal-target-backends=vmvx --iree-vmvx-enable-microkernels /tmp/a.mlir -o /tmp/a.vmfb
/tmp/a.mlir:2:13: error: operand #1 does not dominate this use
  %result = linalg.matmul ins(%lhs, %rhs: tensor<?x?xi8>, tensor<?x?xi8>) outs(%acc: tensor<?x?xi32>) -> tensor<?x?xi32>
            ^
/tmp/a.mlir:1:1: note: called from
func.func @matmul_DYNxDYNxi8_times_DYNxDYNxi8_into_DYNxDYNxi32(%lhs: tensor<?x?xi8>, %rhs: tensor<?x?xi8>, %acc: tensor<?x?xi32>) -> tensor<?x?xi32> {
^
/tmp/a.mlir:2:13: note: operand defined here (op in the same block)
  %result = linalg.matmul ins(%lhs, %rhs: tensor<?x?xi8>, tensor<?x?xi8>) outs(%acc: tensor<?x?xi32>) -> tensor<?x?xi32>
            ^
/tmp/a.mlir:2:13: error: failed to run translation of source executable to target executable for backend #hal.executable.target<"vmvx", "vmvx-bytecode-fb">
  %result = linalg.matmul ins(%lhs, %rhs: tensor<?x?xi8>, tensor<?x?xi8>) outs(%acc: tensor<?x?xi32>) -> tensor<?x?xi32>
            ^
/tmp/a.mlir:1:1: note: called from
func.func @matmul_DYNxDYNxi8_times_DYNxDYNxi8_into_DYNxDYNxi32(%lhs: tensor<?x?xi8>, %rhs: tensor<?x?xi8>, %acc: tensor<?x?xi32>) -> tensor<?x?xi32> {
^
/tmp/a.mlir:2:13: error: failed to serialize executables
  %result = linalg.matmul ins(%lhs, %rhs: tensor<?x?xi8>, tensor<?x?xi8>) outs(%acc: tensor<?x?xi32>) -> tensor<?x?xi32>
            ^
/tmp/a.mlir:1:1: note: called from
func.func @matmul_DYNxDYNxi8_times_DYNxDYNxi8_into_DYNxDYNxi32(%lhs: tensor<?x?xi8>, %rhs: tensor<?x?xi8>, %acc: tensor<?x?xi32>) -> tensor<?x?xi32> {
^
compilation failed

Activity

  1. bjacob commented on Jul 26, 2022

    @bjacob
    CollaboratorAuthor
  2. benvanik commented on Jul 26, 2022

    @benvanik
    Collaborator

    Can you post the --mlir-print-ir-after-all? (I can look at this later but may be able to spot the issue here)

  3. bjacob commented on Jul 27, 2022

    @bjacob
    CollaboratorAuthor

    Can you post the --mlir-print-ir-after-all? (I can look at this later but may be able to spot the issue here)

    https://gist.github.com/bjacob/4b5e102f75b9f58661e63f68dab45182

  4. added
    bug 🐞
    codegen/vmvx
    on Jul 27, 2022
  5. benvanik commented on Jul 27, 2022

    @benvanik
    Collaborator

    Looks like a bug in memref subview handling: %34 = "arith.muli"(%arg0, %44) : (index, index) -> index (using %44 before it is defined) /cc @stellaraccident

    // -----// IR Dump After VMVXLowerLinalgMicrokernels Failed (iree-vmvx-lower-linalg-microkernels) //----- //
    "func.func"() ({
      %0 = "arith.constant"() {value = 1 : index} : () -> index
      %1 = "arith.constant"() {value = 1 : i32} : () -> i32
      %2 = "arith.constant"() {value = 0 : index} : () -> index
      %3 = "arith.constant"() {value = 0 : i32} : () -> i32
      %4 = "hal.interface.constant.load"() {index = 0 : index} : () -> i32
      %5 = "hal.interface.constant.load"() {index = 1 : index} : () -> i32
      %6 = "hal.interface.constant.load"() {index = 2 : index} : () -> i32
      %7 = "hal.interface.constant.load"() {index = 3 : index} : () -> i32
      %8 = "hal.interface.constant.load"() {index = 4 : index} : () -> i32
      %9 = "hal.interface.constant.load"() {index = 5 : index} : () -> i32
      %10 = "arith.index_cast"(%4) : (i32) -> index
      %11 = "arith.index_cast"(%5) : (i32) -> index
      %12 = "arith.index_cast"(%6) : (i32) -> index
      %13 = "arith.index_cast"(%7) : (i32) -> index
      %14 = "arith.index_cast"(%8) : (i32) -> index
      %15 = "arith.index_cast"(%9) : (i32) -> index
      %16 = "hal.interface.binding.subspan"(%2, %12, %13) {alignment = 64 : index, binding = 0 : index, descriptor_type = 7 : i32, operand_segment_sizes = dense<[1, 2]> : vector<2xi32>, set = 0 : index} : (index, index, index) -> memref<?x?xi8>
      %17 = "memref.collapse_shape"(%16) {reassociation = [[0, 1]]} : (memref<?x?xi8>) -> memref<?xi8>
      "memref.assume_alignment"(%16) {alignment = 64 : i32} : (memref<?x?xi8>) -> ()
      %18 = "hal.interface.binding.subspan"(%2, %14, %15) {alignment = 64 : index, binding = 1 : index, descriptor_type = 7 : i32, operand_segment_sizes = dense<[1, 2]> : vector<2xi32>, set = 0 : index} : (index, index, index) -> memref<?x?xi8>
      %19 = "memref.collapse_shape"(%18) {reassociation = [[0, 1]]} : (memref<?x?xi8>) -> memref<?xi8>
      "memref.assume_alignment"(%18) {alignment = 64 : i32} : (memref<?x?xi8>) -> ()
      %20 = "hal.interface.binding.subspan"(%2, %10, %11) {alignment = 64 : index, binding = 2 : index, descriptor_type = 7 : i32, operand_segment_sizes = dense<[1, 2]> : vector<2xi32>, set = 0 : index} : (index, index, index) -> memref<?x?xi32>
      "memref.assume_alignment"(%20) {alignment = 64 : i32} : (memref<?x?xi32>) -> ()
      %21 = "hal.interface.binding.subspan"(%2, %12, %15) {alignment = 64 : index, binding = 3 : index, descriptor_type = 7 : i32, operand_segment_sizes = dense<[1, 2]> : vector<2xi32>, set = 0 : index} : (index, index, index) -> memref<?x?xi32>
      %22 = "memref.collapse_shape"(%21) {reassociation = [[0, 1]]} : (memref<?x?xi32>) -> memref<?xi32>
      %23 = "memref.collapse_shape"(%21) {reassociation = [[0, 1]]} : (memref<?x?xi32>) -> memref<?xi32>
      "memref.assume_alignment"(%21) {alignment = 64 : i32} : (memref<?x?xi32>) -> ()
      %24 = "hal.interface.workgroup.id"() {dimension = 0 : index} : () -> index
      %25 = "hal.interface.workgroup.count"() {dimension = 0 : index} : () -> index
      %26 = "hal.interface.workgroup.id"() {dimension = 1 : index} : () -> index
      %27 = "hal.interface.workgroup.count"() {dimension = 1 : index} : () -> index
      %28 = "affine.apply"(%26) {map = affine_map<()[s0] -> (s0 * 64)>} : (index) -> index
      %29 = "affine.apply"(%27) {map = affine_map<()[s0] -> (s0 * 64)>} : (index) -> index
      "scf.for"(%28, %12, %29) ({
      ^bb0(%arg0: index):
        %30 = "affine.apply"(%24) {map = affine_map<()[s0] -> (s0 * 64)>} : (index) -> index
        %31 = "affine.apply"(%25) {map = affine_map<()[s0] -> (s0 * 64)>} : (index) -> index
        "scf.for"(%30, %15, %31) ({
        ^bb0(%arg1: index):
          %32 = "affine.min"(%arg0, %12) {map = affine_map<(d0)[s0] -> (-d0 + s0, 64)>} : (index, index) -> index
          %33 = "affine.min"(%arg1, %15) {map = affine_map<(d0)[s0] -> (-d0 + s0, 64)>} : (index, index) -> index
          %34 = "arith.muli"(%arg0, %44) : (index, index) -> index
          %35 = "arith.addi"(%34, %arg1) : (index, index) -> index
          %36 = "arith.muli"(%arg0, %41) : (index, index) -> index
          %37 = "arith.addi"(%36, %arg1) : (index, index) -> index
          %38 = "memref.subview"(%21, %arg0, %arg1, %32, %33) {operand_segment_sizes = dense<[1, 2, 2, 0]> : vector<4xi32>, static_offsets = [-9223372036854775808, -9223372036854775808], static_sizes = [-1, -1], static_strides = [1, 1]} : (memref<?x?xi32>, index, index, index, index) -> memref<?x?xi32, affine_map<(d0, d1)[s0, s1] -> (d0 * s1 + s0 + d1)>>
          %39 = "memref.subview"(%20, %arg0, %arg1, %32, %33) {operand_segment_sizes = dense<[1, 2, 2, 0]> : vector<4xi32>, static_offsets = [-9223372036854775808, -9223372036854775808], static_sizes = [-1, -1], static_strides = [1, 1]} : (memref<?x?xi32>, index, index, index, index) -> memref<?x?xi32, affine_map<(d0, d1)[s0, s1] -> (d0 * s1 + s0 + d1)>>
          %40 = "arith.muli"(%arg0, %42) : (index, index) -> index
          %41 = "memref.dim"(%21, %0) : (memref<?x?xi32>, index) -> index
          "vmvx.fill2d"(%3, %22, %37, %41, %32, %33) : (i32, memref<?xi32>, index, index, index, index) -> ()
          %42 = "memref.dim"(%16, %0) : (memref<?x?xi8>, index) -> index
          %43 = "memref.dim"(%18, %0) : (memref<?x?xi8>, index) -> index
          %44 = "memref.dim"(%21, %0) : (memref<?x?xi32>, index) -> index
          "vmvx.matmul"(%17, %40, %42, %19, %arg1, %43, %23, %35, %44, %32, %33, %14, %1, %1) {flags = 0 : i32, lhs_type = i8, out_type = i32, rhs_type = i8} : (memref<?xi8>, index, index, memref<?xi8>, index, index, memref<?xi32>, index, index, index, index, index, i32, i32) -> ()
          "linalg.generic"(%39, %38) ({
          ^bb0(%arg2: i32, %arg3: i32):
            %45 = "arith.addi"(%arg3, %arg2) : (i32, i32) -> i32
            "linalg.yield"(%45) : (i32) -> ()
          }) {indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>], iterator_types = ["parallel", "parallel"], operand_segment_sizes = dense<1> : vector<2xi32>} : (memref<?x?xi32, affine_map<(d0, d1)[s0, s1] -> (d0 * s1 + s0 + d1)>>, memref<?x?xi32, affine_map<(d0, d1)[s0, s1] -> (d0 * s1 + s0 + d1)>>) -> ()
          "scf.yield"() : () -> ()
        }) : (index, index, index) -> ()
        "scf.yield"() : () -> ()
      }) : (index, index, index) -> ()
      "func.return"() : () -> ()
    }) {function_type = () -> (), sym_name = "matmul_DYNxDYNxi8_times_DYNxDYNxi8_into_DYNxDYNxi32_dispatch_0"} : () -> ()
    
  6. benvanik commented on Jul 27, 2022

    @benvanik
    Collaborator

    May be the memref.dims getting inserted in the wrong order.

  7. benvanik commented on Jul 27, 2022

    @benvanik
    Collaborator

    ok, found a fix (though it may not be right); issue is that this code doesn't set a builder to the right place:
    https://github.com/google/iree/blob/main/compiler/src/iree/compiler/Codegen/VMVX/LowerLinalgMicrokernels.cpp#L227-L228

    I've got that patched and now can lower all the way to the end where we bail because there's no vmvx.matmul.i8i8i32! 🥳

    Here's the IR after translation:

    hal.executable.variant public @vmvx_bytecode_fb, target = <"vmvx", "vmvx-bytecode-fb"> {
      hal.executable.export public @matmul_DYNxDYNxi8_times_DYNxDYNxi8_into_DYNxDYNxi32_dispatch_0 ordinal(0) layout(#hal.executable.layout<push_constants = 6, sets = [<0, bindings = [<0, storage_buffer>, <1, storage_buffer>, <2, storage_buffer>, <3, storage_buffer>]>]>) attributes {translation_info = #iree_codegen.translation_info<VMVXDefault workload_per_wg = [64, 64]>} {
      ^bb0(%arg0: !hal.device, %arg1: index, %arg2: index):
        %c1 = arith.constant 1 : index
        %0 = affine.apply affine_map<()[s0] -> (s0 ceildiv 64)>()[%arg1]
        %1 = affine.apply affine_map<()[s0] -> (s0 ceildiv 64)>()[%arg2]
        hal.return %1, %0, %c1 : index, index, index
      }
      builtin.module {
        func.func @matmul_DYNxDYNxi8_times_DYNxDYNxi8_into_DYNxDYNxi32_dispatch_0(%arg0: memref<?xi8>, %arg1: memref<?xi32>, %arg2: !util.list<memref<?xi8>>, %arg3: index, %arg4: index, %arg5: index, %arg6: index, %arg7: index, %arg8: index, %arg9: index, %arg10: index, %arg11: index) {
          %c-1 = arith.constant -1 : index
          %c64 = arith.constant 64 : index
          %c5 = arith.constant 5 : index
          %c4 = arith.constant 4 : index
          %c3 = arith.constant 3 : index
          %c2 = arith.constant 2 : index
          %c1 = arith.constant 1 : index
          %c1_i32 = arith.constant 1 : i32
          %c0 = arith.constant 0 : index
          %c0_i32 = arith.constant 0 : i32
          %0 = memref.load %arg1[%c1] : memref<?xi32>
          %1 = memref.load %arg1[%c2] : memref<?xi32>
          %2 = memref.load %arg1[%c3] : memref<?xi32>
          %3 = memref.load %arg1[%c4] : memref<?xi32>
          %4 = memref.load %arg1[%c5] : memref<?xi32>
          %5 = arith.index_cast %0 : i32 to index
          %6 = arith.index_cast %1 : i32 to index
          %7 = arith.index_cast %2 : i32 to index
          %8 = arith.index_cast %3 : i32 to index
          %9 = arith.index_cast %4 : i32 to index
          %10 = util.list.get %arg2[%c0] : !util.list<memref<?xi8>>
          %11 = util.list.get %arg2[%c1] : !util.list<memref<?xi8>>
          %12 = util.list.get %arg2[%c2] : !util.list<memref<?xi8>>
          %13 = builtin.unrealized_conversion_cast %12 : memref<?xi8> to memref<?xi32>
          %14 = util.list.get %arg2[%c3] : !util.list<memref<?xi8>>
          %15 = builtin.unrealized_conversion_cast %14 : memref<?xi8> to memref<?xi32>
          %16 = arith.muli %arg4, %c64 : index
          %17 = arith.muli %arg10, %c64 : index
          %18 = arith.muli %arg3, %c64 : index
          %19 = arith.muli %arg9, %c64 : index
          scf.for %arg12 = %16 to %6 step %17 {
            %20 = arith.muli %arg12, %c-1 : index
            %21 = arith.addi %20, %6 : index
            %22 = arith.cmpi slt, %21, %c64 : index
            %23 = arith.select %22, %21, %c64 : index
            %24 = arith.muli %arg12, %9 : index
            %25 = arith.muli %arg12, %7 : index
            scf.for %arg13 = %18 to %9 step %19 {
              %26 = arith.muli %arg13, %c-1 : index
              %27 = arith.addi %26, %9 : index
              %28 = arith.cmpi slt, %27, %c64 : index
              %29 = arith.select %28, %27, %c64 : index
              %30 = arith.addi %24, %arg13 : index
              vmvx.fill2d scalar(%c0_i32 : i32) out(%15 offset %30 row_stride %9 : memref<?xi32>) sizes(%23, %29)
              vmvx.matmul lhs(%10 offset %25 row_stride %7 : memref<?xi8>) rhs(%11 offset %arg13 row_stride %9 : memref<?xi8>) out(%15 offset %30 row_stride %9 : memref<?xi32>) mnk(%23, %29, %8) scale(%c1_i32 : i32, %c1_i32 : i32) flags(0) : (i8, i8, i32)
              scf.for %arg14 = %c0 to %23 step %c1 {
                %31 = arith.addi %arg14, %arg12 : index
                %32 = arith.muli %31, %5 : index
                %33 = arith.muli %31, %9 : index
                scf.for %arg15 = %c0 to %29 step %c1 {
                  %34 = arith.addi %arg15, %arg13 : index
                  %35 = arith.addi %34, %32 : index
                  %36 = memref.load %13[%35] : memref<?xi32>
                  %37 = arith.addi %34, %33 : index
                  %38 = memref.load %15[%37] : memref<?xi32>
                  %39 = arith.addi %38, %36 : i32
                  memref.store %39, %15[%37] : memref<?xi32>
                }
              }
            }
          }
          return
        }
      }
    }
  8. added a commit that references this issue on Jul 27, 2022
    4092f40
  9. added a commit that references this issue on Jul 27, 2022
    83d3cc1
  10. moved this to Done in IREEon Jul 27, 2022
  11. bjacob commented on Aug 17, 2022

    @bjacob
    CollaboratorAuthor

    The originally reported failure mode is indeed fixed, but the original testcase (PR description above) still reproduces some other failure:

    $ tools/iree-compile --output-format=vm-bytecode --mlir-print-op-on-diagnostic=false --iree-hal-target-backends=vmvx --iree-vmvx-enable-microkernels /tmp/a.mlir -o /tmp/a.vmfb
    /tmp/a.mlir:2:13: error: failed to resolve VM function import for vmvx.matmul.i8i8i32
      %result = linalg.matmul ins(%lhs, %rhs: tensor<?x?xi8>, tensor<?x?xi8>) outs(%acc: tensor<?x?xi32>) -> tensor<?x?xi32>
                ^
    /tmp/a.mlir:1:1: note: called from
    func.func @matmul_DYNxDYNxi8_times_DYNxDYNxi8_into_DYNxDYNxi32(%lhs: tensor<?x?xi8>, %rhs: tensor<?x?xi8>, %acc: tensor<?x?xi32>) -> tensor<?x?xi32> {
    ^
    /tmp/a.mlir:2:13: error: failed to legalize operation 'vmvx.matmul' that was explicitly marked illegal
      %result = linalg.matmul ins(%lhs, %rhs: tensor<?x?xi8>, tensor<?x?xi8>) outs(%acc: tensor<?x?xi32>) -> tensor<?x?xi32>
                ^
    /tmp/a.mlir:1:1: note: called from
    func.func @matmul_DYNxDYNxi8_times_DYNxDYNxi8_into_DYNxDYNxi32(%lhs: tensor<?x?xi8>, %rhs: tensor<?x?xi8>, %acc: tensor<?x?xi32>) -> tensor<?x?xi32> {
    ^
    /tmp/a.mlir:2:13: error: conversion to vm.module failed
      %result = linalg.matmul ins(%lhs, %rhs: tensor<?x?xi8>, tensor<?x?xi8>) outs(%acc: tensor<?x?xi32>) -> tensor<?x?xi32>
                ^
    /tmp/a.mlir:1:1: note: called from
    func.func @matmul_DYNxDYNxi8_times_DYNxDYNxi8_into_DYNxDYNxi32(%lhs: tensor<?x?xi8>, %rhs: tensor<?x?xi8>, %acc: tensor<?x?xi32>) -> tensor<?x?xi32> {
    ^
    /tmp/a.mlir:2:13: error: failed to run translation of source executable to target executable for backend #hal.executable.target<"vmvx", "vmvx-bytecode-fb">
      %result = linalg.matmul ins(%lhs, %rhs: tensor<?x?xi8>, tensor<?x?xi8>) outs(%acc: tensor<?x?xi32>) -> tensor<?x?xi32>
                ^
    /tmp/a.mlir:1:1: note: called from
    func.func @matmul_DYNxDYNxi8_times_DYNxDYNxi8_into_DYNxDYNxi32(%lhs: tensor<?x?xi8>, %rhs: tensor<?x?xi8>, %acc: tensor<?x?xi32>) -> tensor<?x?xi32> {
    ^
    /tmp/a.mlir:2:13: error: failed to serialize executables
      %result = linalg.matmul ins(%lhs, %rhs: tensor<?x?xi8>, tensor<?x?xi8>) outs(%acc: tensor<?x?xi32>) -> tensor<?x?xi32>
                ^
    /tmp/a.mlir:1:1: note: called from
    func.func @matmul_DYNxDYNxi8_times_DYNxDYNxi8_into_DYNxDYNxi32(%lhs: tensor<?x?xi8>, %rhs: tensor<?x?xi8>, %acc: tensor<?x?xi32>) -> tensor<?x?xi32> {
    ^
    compilation failed
    
  12. reopened this on Aug 17, 2022
  13. bjacob commented on Aug 17, 2022

    @bjacob
    CollaboratorAuthor

    Looks like a much milder failure now, basically just hinting to implement the missing vmvx.matmul.i8 --- doing it!

  14. stellaraccident commented on Aug 17, 2022

    @stellaraccident
    Collaborator

    Thanks! Yes, I hadn't yet specialized anything outside of f32 (and int 32 for elementwise). Go for it.

  15. added a commit that references this issue on Aug 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug 🐞codegen/vmvx

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions