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

[DL] Add AlexNet benchmark. #87

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
benchmarks/DeepLearning/Models/MiniLM-L6/MiniLM-L6-12.mlir filter=lfs diff=lfs merge=lfs -text
benchmarks/DeepLearning/Models/MiniLM-L6/MiniLM-L6-200.mlir filter=lfs diff=lfs merge=lfs -text
benchmarks/DeepLearning/Models/VGG-13/VGG-13.mlir filter=lfs diff=lfs merge=lfs -text
benchmarks/DeepLearning/Models/AlexNet/alexnet_tosa.mlir filter=lfs diff=lfs merge=lfs -text
124 changes: 124 additions & 0 deletions benchmarks/DeepLearning/Models/AlexNet/AlexNetDefaultBenchmark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
//===- AlexNetDefaultBenchmark.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 file implements the benchmark for e2e alexnet.
// The alexnet_tosa.mlir is generated from torch-mlir project.
//
//===----------------------------------------------------------------------===//

#include <benchmark/benchmark.h>
#include <buddy/Core/Container.h>
#include <buddy/DIP/ImageContainer.h>
#include <fstream>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <string>

namespace {

// Declare the alexnet C interface.
extern "C" {
void _mlir_ciface_alexnet(MemRef<float, 2> *output, Img<float, 4> *input);
}

const cv::Mat imagePreprocessing() {
cv::Mat inputImage =
cv::imread("../../benchmarks/DeepLearning/Models/AlexNet/Images/"
"traffic-light.png");
assert(!inputImage.empty() && "Could not read the image.");
cv::Mat resizedImage;
int imageWidth = 224;
int imageHeight = 224;
cv::resize(inputImage, resizedImage, cv::Size(imageWidth, imageHeight),
cv::INTER_LINEAR);
return resizedImage;
}

cv::Mat image = imagePreprocessing();

intptr_t sizesInput[4] = {1, 3, image.rows, image.cols};
intptr_t sizesOutput[2] = {1, 1000};

Img<float, 4> input(image, sizesInput, true);
MemRef<float, 2> output(sizesOutput);

// Define benchmark function.
void BM_alexnet(benchmark::State &state) {
for (auto _ : state) {
for (int i = 0; i < state.range(0); ++i) {
_mlir_ciface_alexnet(&output, &input);
}
}
}

// Softmax function.
void softmax(float *input, size_t size) {
assert(0 <= size <= sizeof(input) / sizeof(float));
int i;
float m, sum, constant;
m = -INFINITY;
for (i = 0; i < size; ++i) {
if (m < input[i]) {
m = input[i];
}
}

sum = 0.0;
for (i = 0; i < size; ++i) {
sum += exp(input[i] - m);
}

constant = m + log(sum);
for (i = 0; i < size; ++i) {
input[i] = exp(input[i] - constant);
}
}

std::string getLabel(int idx) {
std::ifstream in("../../benchmarks/DeepLearning/Models/AlexNet/Labels.txt");
assert(in.is_open() && "Could not read the label file.");
std::string label;
for (int i = 0; i < idx; ++i)
std::getline(in, label);
std::getline(in, label);
in.close();
return label;
}

} // namespace

// Register benchmarking function with different arguments.
BENCHMARK(BM_alexnet)->Arg(1)->Unit(benchmark::kMillisecond);

// Print result function.
void printResult() {
// Run the model and activation function.
_mlir_ciface_alexnet(&output, &input);
auto out = output.getData();
softmax(out, 1000);
// Find the classification and print the result.
float maxVal = 0;
float maxIdx = 0;
for (int i = 0; i < 1000; ++i) {
if (out[i] > maxVal) {
maxVal = out[i];
maxIdx = i;
}
}
std::cout << "Classification Index: " << maxIdx << std::endl;
std::cout << "Classification: " << getLabel(maxIdx) << std::endl;
std::cout << "Probability: " << maxVal << std::endl;
}
45 changes: 45 additions & 0 deletions benchmarks/DeepLearning/Models/AlexNet/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
set(alexnet_TOSA_PIPELINE "builtin.module(func.func(tosa-to-linalg-named),func.func(tosa-to-linalg),func.func(tosa-to-tensor),func.func(tosa-to-arith))")

# Compile MLIR file to object file.
add_custom_command(OUTPUT alexnet-default.o
COMMAND
${LLVM_MLIR_BINARY_DIR}/mlir-opt ${CMAKE_CURRENT_SOURCE_DIR}/alexnet_tosa.mlir
--pass-pipeline="${alexnet_TOSA_PIPELINE}" |
${LLVM_MLIR_BINARY_DIR}/mlir-opt
--test-linalg-transform-patterns="test-generalize-pad-tensor"
--linalg-bufferize
--convert-linalg-to-loops
--func-bufferize
--arith-bufferize
--tensor-bufferize
--finalizing-bufferize
--convert-vector-to-scf
--convert-scf-to-cf
--expand-strided-metadata
--lower-affine
--convert-vector-to-llvm
--memref-expand
--arith-expand
--convert-arith-to-llvm
--finalize-memref-to-llvm
--convert-math-to-llvm
--llvm-request-c-wrappers
--convert-func-to-llvm
--reconcile-unrealized-casts |
${LLVM_MLIR_BINARY_DIR}/mlir-translate --mlir-to-llvmir |
${LLVM_MLIR_BINARY_DIR}/llc -mtriple=${BUDDY_OPT_TRIPLE} -mattr=${BUDDY_OPT_ATTR}
--filetype=obj -o ${CMAKE_CURRENT_BINARY_DIR}/alexnet-default.o
)

add_library(alexnetDefault STATIC alexnet-default.o)
set_target_properties(alexnetDefault PROPERTIES LINKER_LANGUAGE CXX)

add_executable(alexnet-benchmark Main.cpp AlexNetDefaultBenchmark.cpp)
# Link libraries
target_link_directories(alexnet-benchmark PRIVATE ${LLVM_MLIR_LIBRARY_DIR})
target_link_libraries(alexnet-benchmark
alexnetDefault
GoogleBenchmark
mlir_c_runner_utils
${OpenCV_LIBS}
)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading