Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Gemmini]add Gemmini-MiniLM-L6. #96

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

```
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions benchmarks/Gemmini/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
add_subdirectory(ResNet-101)
add_subdirectory(MiniLM-L6)
46 changes: 46 additions & 0 deletions benchmarks/Gemmini/MiniLM-L6/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
70 changes: 70 additions & 0 deletions benchmarks/Gemmini/MiniLM-L6/Main.cpp
Original file line number Diff line number Diff line change
@@ -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 <buddy/Core/Container.h>
#include <buddy/LLM/TextContainer.h>
#include <cmath>
#include <iostream>
#include <string>

namespace {

// Declare the model C interface.
extern "C" {
void _mlir_ciface_forward(MemRef<float, 2> *output,
buddy::Text<long long, 2> *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<long long, 2> input(str);
intptr_t sizesOutput[2] = {1, 2};
MemRef<float, 2> output(sizesOutput);
input.tokenizeLlama(
"../../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;
}