From 6aa6329deae79e3f3d480db42fb9d0ff2b60409d Mon Sep 17 00:00:00 2001 From: linuxlonelyeagle <2020382038@qq.com> Date: Thu, 21 Sep 2023 02:54:56 +0800 Subject: [PATCH 1/3] add Gemmini-MiniLM-L6. --- README.md | 3 +- benchmarks/Gemmini/CMakeLists.txt | 1 + benchmarks/Gemmini/MiniLM-L6/CMakeLists.txt | 46 ++++++++++++++ benchmarks/Gemmini/MiniLM-L6/Main.cpp | 70 +++++++++++++++++++++ 4 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 benchmarks/Gemmini/MiniLM-L6/CMakeLists.txt create mode 100644 benchmarks/Gemmini/MiniLM-L6/Main.cpp diff --git a/README.md b/README.md index 1dc9c115..1b63cebc 100644 --- a/README.md +++ b/README.md @@ -202,7 +202,7 @@ Before building the benchmark target, please see the following table and ensure | Cases | Hardware Configuration | | -------------- | ------------- | | Gemmini-ResNet-101 | defaultFpConfig ([link](./docs/GemminiConfig.md#using-default-float-point-configuration)) | - +| Gemmini-MiniLM-L6 | defaultFpConfig We assume you have already built all the components in the Gemmini README file. Now, let's build and run the cases. ``` @@ -216,6 +216,7 @@ $ cmake -G Ninja .. \ $ ninja $ cd bin $ spike --extension=gemmini pk Gemmini-ResNet-101 +$ spike --extension=gemmini pk Gemmini-MiniLM-L6 ``` ## Operation Optimization Benchmark diff --git a/benchmarks/Gemmini/CMakeLists.txt b/benchmarks/Gemmini/CMakeLists.txt index 44933fdb..c70d7259 100644 --- a/benchmarks/Gemmini/CMakeLists.txt +++ b/benchmarks/Gemmini/CMakeLists.txt @@ -1 +1,2 @@ add_subdirectory(ResNet-101) +add_subdirectory(MiniLM-L6) diff --git a/benchmarks/Gemmini/MiniLM-L6/CMakeLists.txt b/benchmarks/Gemmini/MiniLM-L6/CMakeLists.txt new file mode 100644 index 00000000..0ebef7d6 --- /dev/null +++ b/benchmarks/Gemmini/MiniLM-L6/CMakeLists.txt @@ -0,0 +1,46 @@ +set(BUDDY_MLIR_BINARY_DIR ${BUDDY_MLIR_BUILD_DIR}/bin) +set(CMAKE_CXX_COMPILER riscv64-unknown-linux-gnu-g++) +set(GEMMINI_CONFIG "dim=4 acc_rows=4096 acc_t=f32 elem_t=f32") +set(MINILM-L6-DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../DeepLearning/Models/MiniLM-L6) + +if (NOT DEFINED ENV{RISCV}) + message(FATAL_ERROR "Can't find RISCV environment variable(missing: RISCV_TOOLCHAIN)") +endif() + +add_custom_command( + OUTPUT + minilm-l6.o + COMMAND + ${BUDDY_MLIR_BINARY_DIR}/buddy-opt ${MINILM-L6-DIR}/MiniLM-L6-12.mlir + --linalg-bufferize + --llvm-request-c-wrappers + --convert-linalg-to-gemmini="acc_t=f32" + --convert-linalg-to-loops + --func-bufferize + --arith-bufferize + --tensor-bufferize + --finalizing-bufferize + --lower-affine + --convert-scf-to-cf + --expand-strided-metadata + --convert-vector-to-llvm + --memref-expand + --arith-expand + -lower-gemmini=${GEMMINI_CONFIG} + --convert-arith-to-llvm + --finalize-memref-to-llvm + --convert-math-to-llvm + -reconcile-unrealized-casts | + ${BUDDY_MLIR_BINARY_DIR}/buddy-translate -buddy-to-llvmir | + ${BUDDY_MLIR_BINARY_DIR}/buddy-llc -filetype=obj -mtriple=riscv64 + -mattr=+buddyext,+D -float-abi=hard -o minilm-l6.o +) + +add_library(MINILM-L6 STATIC minilm-l6.o) +set_target_properties(MINILM-L6 PROPERTIES LINKER_LANGUAGE CXX) +add_executable(Gemmini-MiniLM-L6 Main.cpp) +target_link_libraries(Gemmini-MiniLM-L6 + -static + MINILM-L6 + CRunnerUtils +) diff --git a/benchmarks/Gemmini/MiniLM-L6/Main.cpp b/benchmarks/Gemmini/MiniLM-L6/Main.cpp new file mode 100644 index 00000000..b10fafaf --- /dev/null +++ b/benchmarks/Gemmini/MiniLM-L6/Main.cpp @@ -0,0 +1,70 @@ +//===- Main.cpp -----------------------------------------------------------===// +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//===----------------------------------------------------------------------===// +// +// This is the main file of the Gemmini MiniLM-L6 benchmark. +// +//===----------------------------------------------------------------------===// + +#include +#include +#include +#include +#include + +namespace { + +// Declare the model C interface. +extern "C" { +void _mlir_ciface_forward(MemRef *output, + buddy::Text *input); +} + +// Softmax function. +void softmax(float *input, size_t size) { + assert(size > 0); + float m = input[0]; // Find the maximum value + for (size_t i = 1; i < size; ++i) { + if (input[i] > m) { + m = input[i]; + } + } + float sum = 0.0; + for (size_t i = 0; i < size; ++i) { + sum += expf(input[i] - m); + } + float constant = m + logf(sum); + for (size_t i = 0; i < size; ++i) { + input[i] = expf(input[i] - constant); + } +} +} // namespace + +int main() { + std::string str = "buddy compiler is a domain specific compiler!"; + buddy::Text input(str); + intptr_t sizesOutput[2] = {1, 2}; + MemRef output(sizesOutput); + input.tokenize("../../benchmarks/DeepLearning/Models/MiniLM-L6/Vocab.txt", + 200); + _mlir_ciface_forward(&output, &input); + auto out = output.getData(); + softmax(out, 2); + std::cout << std::string(53, '-') << std::endl; + std::cout << "Input: " << str << std::endl; + printf("The probability of positive label: %.2lf\n", out[1]); + printf("The probability of negative label: %.2lf\n", out[0]); + return 0; +} From 889e93c0bf33d9b02dbba070048b8669db463e44 Mon Sep 17 00:00:00 2001 From: linuxlonelyeagle <2020382038@qq.com> Date: Thu, 21 Sep 2023 02:58:07 +0800 Subject: [PATCH 2/3] add Gemmini-MiniLM-L6. --- benchmarks/Gemmini/MiniLM-L6/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/benchmarks/Gemmini/MiniLM-L6/CMakeLists.txt b/benchmarks/Gemmini/MiniLM-L6/CMakeLists.txt index 0ebef7d6..a70270a7 100644 --- a/benchmarks/Gemmini/MiniLM-L6/CMakeLists.txt +++ b/benchmarks/Gemmini/MiniLM-L6/CMakeLists.txt @@ -26,11 +26,11 @@ add_custom_command( --convert-vector-to-llvm --memref-expand --arith-expand - -lower-gemmini=${GEMMINI_CONFIG} + --lower-gemmini=${GEMMINI_CONFIG} --convert-arith-to-llvm --finalize-memref-to-llvm --convert-math-to-llvm - -reconcile-unrealized-casts | + --reconcile-unrealized-casts | ${BUDDY_MLIR_BINARY_DIR}/buddy-translate -buddy-to-llvmir | ${BUDDY_MLIR_BINARY_DIR}/buddy-llc -filetype=obj -mtriple=riscv64 -mattr=+buddyext,+D -float-abi=hard -o minilm-l6.o From 79921897eb4bc0f3006313d6abad4f10e6777c79 Mon Sep 17 00:00:00 2001 From: linuxlonelyeagle <2020382038@qq.com> Date: Sun, 5 Nov 2023 00:35:43 +0000 Subject: [PATCH 3/3] update main.cpp. --- benchmarks/Gemmini/MiniLM-L6/Main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/benchmarks/Gemmini/MiniLM-L6/Main.cpp b/benchmarks/Gemmini/MiniLM-L6/Main.cpp index b10fafaf..f6ac9b25 100644 --- a/benchmarks/Gemmini/MiniLM-L6/Main.cpp +++ b/benchmarks/Gemmini/MiniLM-L6/Main.cpp @@ -57,8 +57,8 @@ int main() { buddy::Text input(str); intptr_t sizesOutput[2] = {1, 2}; MemRef output(sizesOutput); - input.tokenize("../../benchmarks/DeepLearning/Models/MiniLM-L6/Vocab.txt", - 200); + input.tokenizeLlama( + "../../benchmarks/DeepLearning/Models/MiniLM-L6/Vocab.txt", 200); _mlir_ciface_forward(&output, &input); auto out = output.getData(); softmax(out, 2);