Skip to content

[VMVX] lower linalg.batch_matmul to DT microkernels #15314

Closed

Description

@hanhanW

We are able to run linalg.batch_matmul on llvm-cpu data-tiling + ukernel path, but not for vmvx. Most of features are done in #14431

To enable it on vmvx, we need:

  1. Enhance DecomposeBatchMmt4DOps pass to handle batch_size != 1 cases. If the linalg.batch_mmt4d has non-one batch size, we tile the dimension with size=1 and lower it to linalg.mmt4d using the existing patterns.
  2. Add the pass to vmvx pipeline

Activity

  1. added
    codegen
    codegen/vmvx
    on Oct 26, 2023
  2. hanhanW commented on Oct 30, 2023

    @hanhanW
    ContributorAuthor

    An e2e example is below:

    func.func @foo(%arg0: tensor<4x10x20xf32>, %arg1: tensor<4x20x30xf32>) -> tensor<4x10x30xf32>{
      %cst = arith.constant 0.000000e+00 : f32
      %0 = tensor.empty() : tensor<4x10x30xf32>
      %1 = linalg.fill ins(%cst : f32) outs(%0 : tensor<4x10x30xf32>) -> tensor<4x10x30xf32>
      %2 = linalg.batch_matmul ins(%arg0, %arg1 : tensor<4x10x20xf32>, tensor<4x20x30xf32>) outs(%1 : tensor<4x10x30xf32>) -> tensor<4x10x30xf32>
      return %2 : tensor<4x10x30xf32>
    }

    To compile the program: iree-compile --output-format=vm-bytecode --iree-hal-target-backends=vmvx --iree-vmvx-enable-microkernels --iree-opt-data-tiling ~/repro.mlir -o /tmp/a.vmfb

    ===

    What we want is scf::tileConsumerAndFuseProducerGreedilyUsingSCFForOp because we'd like to fuse fill op as well. E.g.,

    func.func @batch_mmt4d_with_fill(%arg0: tensor<12x10x32x8x1xf32>, %arg1: tensor<12x80x32x4x1xf32>) -> tensor<12x10x80x8x4xf32> {
      %cst = arith.constant 0.000000e+00 : f32
      %empty = tensor.empty() : tensor<12x10x80x8x4xf32>
      %0 = linalg.fill ins(%cst : f32) outs(%empty : tensor<12x10x80x8x4xf32>) -> tensor<12x10x80x8x4xf32>
      %1 = linalg.batch_mmt4d ins(%arg0, %arg1 : tensor<12x10x32x8x1xf32>, tensor<12x80x32x4x1xf32>) outs(%0 : tensor<12x10x80x8x4xf32>) -> tensor<12x10x80x8x4xf32>
      return %1 : tensor<12x10x80x8x4xf32>
    }

    should be lowered to

      func.func @batch_mmt4d_with_fill(%arg0: tensor<12x10x32x8x1xf32>, %arg1: tensor<12x80x32x4x1xf32>) -> tensor<12x10x80x8x4xf32> {
        %c12 = arith.constant 12 : index
        %c0 = arith.constant 0 : index
        %c1 = arith.constant 1 : index
        %cst = arith.constant 0.000000e+00 : f32
        %0 = tensor.empty() : tensor<12x10x80x8x4xf32>
        %1 = scf.for %arg2 = %c0 to %c12 step %c1 iter_args(%arg3 = %0) -> (tensor<12x10x80x8x4xf32>) {
          %extracted_slice = tensor.extract_slice %arg0[%arg2, 0, 0, 0, 0] [1, 10, 32, 8, 1] [1, 1, 1, 1, 1] : tensor<12x10x32x8x1xf32> to tensor<1x10x32x8x1xf32>
          %extracted_slice_0 = tensor.extract_slice %arg1[%arg2, 0, 0, 0, 0] [1, 80, 32, 4, 1] [1, 1, 1, 1, 1] : tensor<12x80x32x4x1xf32> to tensor<1x80x32x4x1xf32>
          %extracted_slice_1 = tensor.extract_slice %arg3[%arg2, 0, 0, 0, 0] [1, 10, 80, 8, 4] [1, 1, 1, 1, 1] : tensor<12x10x80x8x4xf32> to tensor<1x10x80x8x4xf32>
          %2 = linalg.fill ins(%cst : f32) outs(%extracted_slice_1 : tensor<1x10x80x8x4xf32>) -> tensor<1x10x80x8x4xf32>
          %3 = linalg.batch_mmt4d ins(%extracted_slice, %extracted_slice_0 : tensor<1x10x32x8x1xf32>, tensor<1x80x32x4x1xf32>) outs(%2 : tensor<1x10x80x8x4xf32>) -> tensor<1x10x80x8x4xf32>
          %inserted_slice = tensor.insert_slice %3 into %arg3[%arg2, 0, 0, 0, 0] [1, 10, 80, 8, 4] [1, 1, 1, 1, 1] : tensor<1x10x80x8x4xf32> into tensor<12x10x80x8x4xf32>
          scf.yield %inserted_slice : tensor<12x10x80x8x4xf32>
        }
        return %1 : tensor<12x10x80x8x4xf32>
      }

    then the rest of patterns will rewrite linalg.batch_mmt4d op with linalg.mmt4d op, e.g.,

      func.func @batch_mmt4d_with_fill(%arg0: tensor<12x10x32x8x1xf32>, %arg1: tensor<12x80x32x4x1xf32>) -> tensor<12x10x80x8x4xf32> {
        %c12 = arith.constant 12 : index
        %c0 = arith.constant 0 : index
        %c1 = arith.constant 1 : index
        %cst = arith.constant 0.000000e+00 : f32
        %0 = tensor.empty() : tensor<12x10x80x8x4xf32>
        %1 = scf.for %arg2 = %c0 to %c12 step %c1 iter_args(%arg3 = %0) -> (tensor<12x10x80x8x4xf32>) {
          %extracted_slice = tensor.extract_slice %arg3[%arg2, 0, 0, 0, 0] [1, 10, 80, 8, 4] [1, 1, 1, 1, 1] : tensor<12x10x80x8x4xf32> to tensor<10x80x8x4xf32>
          %2 = linalg.fill ins(%cst : f32) outs(%extracted_slice : tensor<10x80x8x4xf32>) -> tensor<10x80x8x4xf32>
          %extracted_slice_0 = tensor.extract_slice %arg0[%arg2, 0, 0, 0, 0] [1, 10, 32, 8, 1] [1, 1, 1, 1, 1] : tensor<12x10x32x8x1xf32> to tensor<10x32x8x1xf32>
          %extracted_slice_1 = tensor.extract_slice %arg1[%arg2, 0, 0, 0, 0] [1, 80, 32, 4, 1] [1, 1, 1, 1, 1] : tensor<12x80x32x4x1xf32> to tensor<80x32x4x1xf32>
          %3 = linalg.mmt4d ins(%extracted_slice_0, %extracted_slice_1 : tensor<10x32x8x1xf32>, tensor<80x32x4x1xf32>) outs(%2 : tensor<10x80x8x4xf32>) -> tensor<10x80x8x4xf32>
          %inserted_slice = tensor.insert_slice %3 into %arg3[%arg2, 0, 0, 0, 0] [1, 10, 80, 8, 4] [1, 1, 1, 1, 1] : tensor<10x80x8x4xf32> into tensor<12x10x80x8x4xf32>
          scf.yield %inserted_slice : tensor<12x10x80x8x4xf32>
        }
        return %1 : tensor<12x10x80x8x4xf32>
      }
  3. bjacob commented on Oct 30, 2023

    @bjacob
    Collaborator

    @Shukla-Gaurav to create an e2e-to-end correctness test for batch_matmul that can easily be run on any backend including VMVX, create it as a .mlir file in tests/e2e/linalg.

    The conv2d.mlir test there is a good example to start from.

    In CMakeLists.txt add a iree_check_single_backend_test_suite call like this to test VMVX + data-tiling + microkernels:

    iree_check_single_backend_test_suite(
      NAME
        check_vmvx_local-task
      SRCS
        "mybatchmatmultest.mlir"
      TARGET_BACKEND
        "vmvx"
      DRIVER
        "local-task"
      COMPILER_FLAGS
        "--iree-opt-data-tiling"
        "--iree-vmvx-enable-microkernels"
    )
    

    Eventually to make this PR pass CI, you will have to lift that to BUILD.Bazel. It's not hard, just a bit convoluted due to the usage of enforce_glob. Then run python build_tools/bazel_to_cmake/bazel_to_cmake.py to generate CMakeLists.txt, it will overwrite your hand-edited CMakeLists.txt, save it before and iterate until bazel_to_cmake's output is identical.

  4. Shukla-Gaurav commented on Nov 1, 2023

    @Shukla-Gaurav

    Thanks for all the reference @bjacob @hanhanW

  5. hanhanW commented on Nov 2, 2023

    @hanhanW
    ContributorAuthor

    I'd like to check in if there are any updates? I'm happy to help if there are any issues.

  6. Shukla-Gaurav commented on Nov 3, 2023

    @Shukla-Gaurav

    @hanhanW I made progress on updating the DecomposeBatchMmt4DOps pass in order to add support for non unit batch dims.
    I am not blocked on anything, exploring the codebase related to Tiling and Fusion. Thanks for asking.

  7. self-assigned this
    on Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

codegencodegen/vmvxgood first issue 🌱

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions