From 6b2eead298e5197b1639147b83b9b97faebc0b22 Mon Sep 17 00:00:00 2001 From: cmx-Y Date: Mon, 10 Feb 2025 16:30:02 +0800 Subject: [PATCH] [examples] Add Gemmini depthwise_conv examples --- examples/GemminiDialect/dw-tile-conv.mlir | 101 ++++++++++++++++++ .../dw_conv_2d_nchw_chw_i8.mlir | 43 ++++++++ examples/GemminiDialect/makefile | 68 ++++++++++++ 3 files changed, 212 insertions(+) create mode 100644 examples/GemminiDialect/dw-tile-conv.mlir create mode 100644 examples/GemminiDialect/dw_conv_2d_nchw_chw_i8.mlir diff --git a/examples/GemminiDialect/dw-tile-conv.mlir b/examples/GemminiDialect/dw-tile-conv.mlir new file mode 100644 index 0000000000..d3541193eb --- /dev/null +++ b/examples/GemminiDialect/dw-tile-conv.mlir @@ -0,0 +1,101 @@ +// RUN: buddy-opt %s \ +// RUN: --lower-gemmini | \ +// RUN: FileCheck %s + +// n x h x w x c +// batchSize = 1 inputDim = 5 inChannels = 2 +memref.global "private" @input : memref<1x5x5x2xi8> = dense<[[[[1, 2], [0, 0], [-1, -1], [0, 0], [1, 1]], + [[1, 2], [0, 0], [-1, -1], [0, 0], [1, 1]], + [[1, 2], [0, 0], [-1, -1], [0, 0], [1, 1]], + [[1, 2], [0, 0], [-1, -1], [0, 0], [1, 1]], + [[1, 2], [0, 0], [-1, -1], [0, 0], [1, 1]]]]> + +// chw x f +// outChannels = 2 kernelDim = 3 inChannels = 2 +memref.global "private" @weight : memref<9x1xi8> = dense<[[1], [1], [1], + [1], [1], [1], + [1], [1], [1]]> + +// outChannels = 1 +memref.global "private" @bias : memref<1xi32> = dense<[1]> + +func.func @main() -> i64 { + %0 = arith.constant 0 : i64 + %3 = arith.constant 3 : i64 + %input = memref.get_global @input : memref<1x5x5x2xi8> + %weight = memref.get_global @weight : memref<9x1xi8> + %bias = memref.get_global @bias : memref<1xi32> + %output0 = memref.alloc() : memref<9x1xi8> + %output1 = memref.alloc() : memref<9x1xi8> + %output = memref.alloc() : memref<9x2xi8> + + %subview0 = memref.alloc() : memref<1x5x5x1xi8> + %subview1 = memref.alloc() : memref<1x5x5x1xi8> + + linalg.generic { + indexing_maps = [ + affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3*2)>, + affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)> + ], + iterator_types = ["parallel", "parallel", "parallel", "parallel"] + } + ins (%input : memref<1x5x5x2xi8>) + outs (%subview0 : memref<1x5x5x1xi8>) { + ^bb0(%in : i8, %out : i8): + linalg.yield %in : i8 + } + + linalg.generic { + indexing_maps = [ + affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3*2 + 1)>, + affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)> + ], + iterator_types = ["parallel", "parallel", "parallel", "parallel"] + } + ins (%input : memref<1x5x5x2xi8>) + outs (%subview1 : memref<1x5x5x1xi8>) { + ^bb0(%in : i8, %out : i8): + linalg.yield %in : i8 + } + + //gemmini.print %subview0 : memref<1x5x5x1xi8> + //gemmini.print %subview1 : memref<1x5x5x1xi8> + + gemmini.tile_conv %subview0 %weight %bias %output0 %3 %3 %3 {stride = 1}: + memref<1x5x5x1xi8> memref<9x1xi8> memref<1xi32> memref<9x1xi8> i64 i64 i64 + // gemmini.print %output0 : memref<9x1xi8> + + gemmini.tile_conv %subview1 %weight %bias %output1 %3 %3 %3 {stride = 1}: + memref<1x5x5x1xi8> memref<9x1xi8> memref<1xi32> memref<9x1xi8> i64 i64 i64 + // gemmini.print %output1 : memref<9x1xi8> + + linalg.generic { + indexing_maps = [ + affine_map<(d0, d1) -> (d0, d1)>, + affine_map<(d0, d1) -> (d0, d1 * 2)> + ], + iterator_types = ["parallel", "parallel"] + } + ins(%output0 : memref<9x1xi8>) + outs(%output : memref<9x2xi8>) { + ^bb0(%in: i8, %out: i8): + linalg.yield %in : i8 + } + + linalg.generic { + indexing_maps = [ + affine_map<(d0, d1) -> (d0, d1)>, + affine_map<(d0, d1) -> (d0, d1 * 2 + 1)> + ], + iterator_types = ["parallel", "parallel"] + } + ins(%output1 : memref<9x1xi8>) + outs(%output : memref<9x2xi8>) { + ^bb0(%in: i8, %out: i8): + linalg.yield %in : i8 + } + + gemmini.print %output : memref<9x2xi8> + + return %0 : i64 +} diff --git a/examples/GemminiDialect/dw_conv_2d_nchw_chw_i8.mlir b/examples/GemminiDialect/dw_conv_2d_nchw_chw_i8.mlir new file mode 100644 index 0000000000..b072728488 --- /dev/null +++ b/examples/GemminiDialect/dw_conv_2d_nchw_chw_i8.mlir @@ -0,0 +1,43 @@ +// RUN: buddy-opt %s \ +// RUN: --convert-linalg-to-gemmini | \ +// RUN: FileCheck %s + +memref.global "private" @input : memref<2x2x5x5xi8> = dense<[[[[1, 0, -1, 0, 1], + [1, 0, -1, 0, 1], + [1, 0, -1, 0, 1], + [1, 0, -1, 0, 1], + [-1, 0, 1, 0, -1]], + [[-1, 0, 1, 0, -1], + [-1, 0, 1, 0, -1], + [-1, 0, 1, 0, -1], + [-1, 0, 1, 0, -1], + [-1, 0, 1, 0, -1]]], + [[[1, 0, 2, 0, 1], + [1, 0, 2, 0, 1], + [1, 0, 2, 0, 1], + [1, 0, 2, 0, 1], + [-1, 0, 2, 0, -1]], + [[-1, 0, 2, 0, -1], + [-1, 0, 2, 0, -1], + [-1, 0, 2, 0, -1], + [-1, 0, 2, 0, -1], + [-1, 0, 2, 0, -1]]]]> + +memref.global "private" @weight : memref<2x3x3xi8> = dense<[[[1, 2, 3], + [3, 2, 1], + [1, 2, 3]], + [[3, 2, 1], + [1, 2, 3], + [3, 2, 1]]]> + +func.func @main() -> i8 { + %0 = arith.constant 0 : i8 + %mem0 = memref.get_global @input : memref<2x2x5x5xi8> + %mem1 = memref.get_global @weight : memref<2x3x3xi8> + %mem2 = memref.alloc() : memref<2x2x3x3xi8> + linalg.depthwise_conv_2d_nchw_chw + ins (%mem0, %mem1 : memref<2x2x5x5xi8>, memref<2x3x3xi8>) + outs(%mem2 : memref<2x2x3x3xi8>) + gemmini.print %mem2 : memref<2x2x3x3xi8> + return %0 : i8 +} diff --git a/examples/GemminiDialect/makefile b/examples/GemminiDialect/makefile index 6be7693262..bf9edba699 100644 --- a/examples/GemminiDialect/makefile +++ b/examples/GemminiDialect/makefile @@ -396,6 +396,37 @@ tile-conv-run: @riscv64-unknown-linux-gnu-gcc log.o -O2 -static -o a.out @spike --extension=gemmini pk a.out +dw-tile-conv-lower: + @${BUDDY_OPT} ./dw-tile-conv.mlir \ + -convert-linalg-to-loops \ + -lower-gemmini \ + -o log.mlir + +dw-tile-conv-translate: + @${BUDDY_OPT} ./dw-tile-conv.mlir \ + -convert-linalg-to-loops \ + -lower-gemmini | \ + ${BUDDY_TRANSLATE} -buddy-to-llvmir \ + -o log.ll + +dw-tile-conv-asm: + @${BUDDY_OPT} ./dw-tile-conv.mlir \ + -convert-linalg-to-loops \ + -lower-gemmini | \ + ${BUDDY_TRANSLATE} -buddy-to-llvmir | \ + ${BUDDY_LLC} -filetype=asm -mtriple=riscv64 \ + -mattr=+buddyext,+D -float-abi=hard \ + -o log.s + +dw-tile-conv-run: + @${BUDDY_OPT} ./dw-tile-conv.mlir -convert-linalg-to-loops -lower-gemmini | \ + ${BUDDY_TRANSLATE} --buddy-to-llvmir | \ + ${BUDDY_LLC} -filetype=obj -mtriple=riscv64 \ + -mattr=+buddyext,+D -float-abi=hard \ + -o log.o + @riscv64-unknown-linux-gnu-gcc log.o -O2 -static -o a.out + @spike --extension=gemmini pk a.out + tile-conv-igelu-lower: @${BUDDY_OPT} ./tile-conv-igelu.mlir \ -lower-gemmini \ @@ -610,6 +641,43 @@ gemmini-linalg-conv2d-nchw-fchw-i8-run: @riscv64-unknown-linux-gnu-gcc log.o -O2 -static -o a.out @spike --extension=gemmini pk a.out +gemmini-linalg-dw-conv2d-nchw-chw-i8-lower: + @${BUDDY_OPT} ./dw_conv_2d_nchw_chw_i8.mlir \ + -convert-linalg-to-gemmini \ + -convert-linalg-to-loops \ + -lower-gemmini \ + -o log.mlir + +gemmini-linalg-dw-conv2d-nchw-chw-i8-translate: + @${BUDDY_OPT} ./dw_conv_2d_nchw_chw_i8.mlir \ + -convert-linalg-to-gemmini \ + -convert-linalg-to-loops \ + -lower-gemmini | \ + ${BUDDY_TRANSLATE} -buddy-to-llvmir \ + -o log.ll + +gemmini-linalg-dw-conv2d-nchw-chw-i8-asm: + @${BUDDY_OPT} ./dw_conv_2d_nchw_chw_i8.mlir \ + -convert-linalg-to-gemmini \ + -convert-linalg-to-loops \ + -lower-gemmini | \ + ${BUDDY_TRANSLATE} -buddy-to-llvmir | \ + ${BUDDY_LLC} -filetype=asm -mtriple=riscv64 \ + -mattr=+buddyext,+D -float-abi=hard \ + -o log.s + +gemmini-linalg-dw-conv2d-nchw-chw-i8-run: + @${BUDDY_OPT} ./dw_conv_2d_nchw_chw_i8.mlir \ + -convert-linalg-to-gemmini \ + -convert-linalg-to-loops \ + -lower-gemmini | \ + ${BUDDY_TRANSLATE} -buddy-to-llvmir | \ + ${BUDDY_LLC} -filetype=obj -mtriple=riscv64 \ + -mattr=+buddyext,+D -float-abi=hard \ + -o log.o + @riscv64-unknown-linux-gnu-gcc log.o -O2 -static -o a.out + @spike --extension=gemmini pk a.out + gemmini-linalg-conv2d-nchw-fchw-f32-lower: @${BUDDY_OPT} ./conv_2d_nchw_fchw_f32.mlir \ -convert-linalg-to-gemmini="acc_t=f32" \