From e36ecf5cf8369a072c260f1c385727420dbf2270 Mon Sep 17 00:00:00 2001 From: yzhang93 Date: Wed, 16 Oct 2024 10:49:43 -0700 Subject: [PATCH] Increase the K tile size in L1 for matmul ops --- .../iree-amd-aie/Transforms/AMDAIEUtils.cpp | 15 +++++++++++++++ .../iree-amd-aie/Transforms/AMDAIEUtils.h | 4 ++++ .../iree-amd-aie/Transforms/KernelDispatch.cpp | 6 +++++- .../Transforms/test/lowering_strategy.mlir | 6 +++--- .../test/lowering_strategy_objectfifo.mlir | 16 ++++++++-------- 5 files changed, 35 insertions(+), 12 deletions(-) diff --git a/compiler/plugins/target/AMD-AIE/iree-amd-aie/Transforms/AMDAIEUtils.cpp b/compiler/plugins/target/AMD-AIE/iree-amd-aie/Transforms/AMDAIEUtils.cpp index a26d77e21..9fd2d5f40 100644 --- a/compiler/plugins/target/AMD-AIE/iree-amd-aie/Transforms/AMDAIEUtils.cpp +++ b/compiler/plugins/target/AMD-AIE/iree-amd-aie/Transforms/AMDAIEUtils.cpp @@ -351,6 +351,21 @@ bool isMatmulProducerOfElementwise(linalg::LinalgOp linalgOp) { return false; } +/// Utility to identify if `linalgOp` is a matmul operation with an elementwise +/// op downstream in its computation tree. +bool isElementwiseConsumerOfMatmul(linalg::LinalgOp linalgOp) { + if (!isa(linalgOp) && !isMatmul(linalgOp)) { + return false; + } + for (Operation *userOp : linalgOp->getUsers()) { + auto linalgUser = dyn_cast(userOp); + if (linalgUser && isElementwise(linalgUser)) { + return true; + } + } + return false; +} + std::string utohexstr(uint32_t value, size_t width, bool header, bool lowercase) { std::string res = ""; diff --git a/compiler/plugins/target/AMD-AIE/iree-amd-aie/Transforms/AMDAIEUtils.h b/compiler/plugins/target/AMD-AIE/iree-amd-aie/Transforms/AMDAIEUtils.h index d657fff4d..72bb2ce4a 100644 --- a/compiler/plugins/target/AMD-AIE/iree-amd-aie/Transforms/AMDAIEUtils.h +++ b/compiler/plugins/target/AMD-AIE/iree-amd-aie/Transforms/AMDAIEUtils.h @@ -94,6 +94,10 @@ bool isMatmulInDefChain(Value operand); /// matmul-like op upstream in its computation tree. bool isMatmulProducerOfElementwise(linalg::LinalgOp linalgOp); +/// Utility to identify if `linalgOp` is a matmul operation with an elementwise +/// op downstream in its computation tree. +bool isElementwiseConsumerOfMatmul(linalg::LinalgOp linalgOp); + /// Utility to convert a `uint32_t` value into a hex string. std::string utohexstr(uint32_t value, size_t width, bool header = true, bool lowercase = false); diff --git a/compiler/plugins/target/AMD-AIE/iree-amd-aie/Transforms/KernelDispatch.cpp b/compiler/plugins/target/AMD-AIE/iree-amd-aie/Transforms/KernelDispatch.cpp index 398298120..dc87bad8a 100644 --- a/compiler/plugins/target/AMD-AIE/iree-amd-aie/Transforms/KernelDispatch.cpp +++ b/compiler/plugins/target/AMD-AIE/iree-amd-aie/Transforms/KernelDispatch.cpp @@ -224,7 +224,11 @@ FailureOr ParameterSetting::create(linalg::LinalgOp linalgOp, // the second level of inner pack size (vector instruction size). uint32_t m0Pack = (M0 / 2) % m1Pack == 0 ? (M0 / 2) : M0; uint32_t n0Pack = (N0 / 2) % n1Pack == 0 ? (N0 / 2) : N0; - uint32_t k0Pack = findLargestFactor(K, maxL1Size); + + // For matmul ops, make the most use of L1 memory by scaling the K tiling + // dimension. + uint32_t kPackScale = isElementwiseConsumerOfMatmul(linalgOp) ? 1 : 2; + uint32_t k0Pack = findLargestFactor(K, kPackScale * maxL1Size); return ParameterSetting{M0, N0, K0, M1, N1, K1, m0Pack, n0Pack, k0Pack, m1Pack, n1Pack, k1Pack}; diff --git a/compiler/plugins/target/AMD-AIE/iree-amd-aie/Transforms/test/lowering_strategy.mlir b/compiler/plugins/target/AMD-AIE/iree-amd-aie/Transforms/test/lowering_strategy.mlir index dd4181a01..8b5c00dc8 100644 --- a/compiler/plugins/target/AMD-AIE/iree-amd-aie/Transforms/test/lowering_strategy.mlir +++ b/compiler/plugins/target/AMD-AIE/iree-amd-aie/Transforms/test/lowering_strategy.mlir @@ -190,7 +190,7 @@ builtin.module { // ----- // CHECK-PACK-PEEL{LITERAL}: #config = #iree_codegen.lowering_config -// CHECK-PACK-PEEL{LITERAL}: #packingConfig = #amdaie.packing_config +// CHECK-PACK-PEEL{LITERAL}: #packingConfig = #amdaie.packing_config #pipeline_layout = #hal.pipeline.layout, , @@ -217,7 +217,7 @@ builtin.module { // ----- // CHECK-PACK-PEEL{LITERAL}: #config = #iree_codegen.lowering_config -// CHECK-PACK-PEEL{LITERAL}: #packingConfig = #amdaie.packing_config +// CHECK-PACK-PEEL{LITERAL}: #packingConfig = #amdaie.packing_config #pipeline_layout = #hal.pipeline.layout, , @@ -245,7 +245,7 @@ module { // CHECK-PAD-PACK{LITERAL}: #config = #iree_codegen.lowering_config // CHECK-PAD-PACK{LITERAL}: #packingConfig = #amdaie.packing_config // CHECK-PACK-PEEL{LITERAL}: #config = #iree_codegen.lowering_config -// CHECK-PACK-PEEL{LITERAL}: #packingConfig = #amdaie.packing_config +// CHECK-PACK-PEEL{LITERAL}: #packingConfig = #amdaie.packing_config #pipeline_layout = #hal.pipeline.layout, , diff --git a/compiler/plugins/target/AMD-AIE/iree-amd-aie/Transforms/test/lowering_strategy_objectfifo.mlir b/compiler/plugins/target/AMD-AIE/iree-amd-aie/Transforms/test/lowering_strategy_objectfifo.mlir index c31f21899..8b14d9a00 100644 --- a/compiler/plugins/target/AMD-AIE/iree-amd-aie/Transforms/test/lowering_strategy_objectfifo.mlir +++ b/compiler/plugins/target/AMD-AIE/iree-amd-aie/Transforms/test/lowering_strategy_objectfifo.mlir @@ -1,7 +1,7 @@ // RUN: iree-opt --split-input-file --pass-pipeline='builtin.module(iree-amdaie-lowering-strategy{use-pass-pipeline=pack-peel use-lower-to-aie-pipeline=objectFifo})' %s | FileCheck %s -// CHECK-{LITERAL}: #config = #iree_codegen.lowering_config -// CHECK-{LITERAL}: #packingConfig = #amdaie.packing_config +// CHECK{LITERAL}: #config = #iree_codegen.lowering_config +// CHECK{LITERAL}: #packingConfig = #amdaie.packing_config #pipeline_layout = #hal.pipeline.layout, , @@ -27,8 +27,8 @@ module { // ----- -// CHECK-{LITERAL}: #config = #iree_codegen.lowering_config -// CHECK-{LITERAL}: #packingConfig = #amdaie.packing_config +// CHECK{LITERAL}: #config = #iree_codegen.lowering_config +// CHECK{LITERAL}: #packingConfig = #amdaie.packing_config #pipeline_layout = #hal.pipeline.layout, , @@ -54,8 +54,8 @@ module { // ----- -// CHECK-{LITERAL}: #config = #iree_codegen.lowering_config -// CHECK-{LITERAL}: #packingConfig = #amdaie.packing_config +// CHECK{LITERAL}: #config = #iree_codegen.lowering_config +// CHECK{LITERAL}: #packingConfig = #amdaie.packing_config #pipeline_layout = #hal.pipeline.layout, , @@ -81,8 +81,8 @@ module { // ----- -// CHECK-{LITERAL}: #config = #iree_codegen.lowering_config -// CHECK-{LITERAL}: #amdaie.packing_config +// CHECK{LITERAL}: #config = #iree_codegen.lowering_config +// CHECK{LITERAL}: #amdaie.packing_config #pipeline_layout = #hal.pipeline.layout, ,