Skip to content
Snippets Groups Projects
Unverified Commit c6430183 authored by joejiong's avatar joejiong Committed by GitHub
Browse files

Merge pull request #5 from Joejiong/add-op-benchmark

Add conv2d mlir files and with naive implementation nchw& nhwc 
parents 503a0df5 6007b60c
No related branches found
No related tags found
No related merge requests found
Showing with 298 additions and 0 deletions
add_subdirectory(Models)
add_subdirectory(Ops)
add_subdirectory(DepthwiseConv2DNhwcHwcOp)
add_subdirectory(Conv2DNhwcHwcfOp)
add_subdirectory(Conv2DNchwFchwOp)
set(BUDDY_OPT_ATTR avx512f)
set(LLVM_MLIR_BINARY_DIR ${BUDDY_OPT_BUILD_DIR}/../llvm/build/bin)
add_custom_command(OUTPUT conv-2d-nchw-fchw.o
COMMAND ${LLVM_MLIR_BINARY_DIR}/mlir-opt ${BUDDY_SOURCE_DIR}/benchmarks/DeepLearning/Ops/Conv2DNchwFchwOp/Conv2DNchwFchw.mlir
-convert-linalg-to-loops
-convert-scf-to-std
-convert-linalg-to-llvm
-lower-affine
--convert-memref-to-llvm
-convert-std-to-llvm='emit-c-wrappers=1'
-reconcile-unrealized-casts|
${LLVM_MLIR_BINARY_DIR}/mlir-translate --mlir-to-llvmir |
${LLVM_MLIR_BINARY_DIR}/llc -mtriple=x86_64-unknown-linux-gnu -mattr=${BUDDY_OPT_ATTR}
--filetype=obj -o ${BUDDY_BINARY_DIR}/../benchmarks/DeepLearning/Ops/Conv2DNchwFchwOp/conv-2d-nchw-fchw.o
)
add_library(Conv2DNchwFchw conv-2d-nchw-fchw.o)
set_target_properties(Conv2DNchwFchw PROPERTIES LINKER_LANGUAGE CXX)
add_executable(conv-2d-nchw-fchw-benchmark Main.cpp MLIROptBenchmark.cpp)
target_link_libraries(conv-2d-nchw-fchw-benchmark
GoogleBenchmark
Conv2DNchwFchw
Container
)
// generate from iree processed alexnet or lenet mlir file
func @conv_2d_nchw_fchw(%input: memref<?x?x?x?xf32>, %filter: memref<?x?x?x?xf32>, %output: memref<?x?x?x?xf32>) {
linalg.conv_2d_nchw_fchw
{dilations = dense<1> : tensor<2xi64>, strides = dense<1> : tensor<2xi64>}
ins(%input, %filter : memref<?x?x?x?xf32>, memref<?x?x?x?xf32>)
outs(%output : memref<?x?x?x?xf32>)
return
}
//===- MLIROptBenchmark.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 conv2d(nchw-fchw) operation.
//
//===----------------------------------------------------------------------===//
#include "Utils/Container.h"
#include <benchmark/benchmark.h>
namespace
{
// Declare the mobilenet C interface.
extern "C"
{
void _mlir_ciface_conv_2d_nchw_fchw(MemRef<float, 4> *input,
MemRef<float, 4> *filter,
MemRef<float, 4> *output);
}
intptr_t sizesInput[4] = {1, 2, 3, 3};
intptr_t sizesFilter[4] = {2, 2, 2, 2};
intptr_t sizesOutput[4] = {1, 2, 2, 2};
// Create input, filter, and output.
MemRef<float, 4> inputMemRef(sizesInput, 2.0);
MemRef<float, 4> filterMemRef(sizesFilter, 3.0);
MemRef<float, 4> outputMemRef(sizesOutput, 0.0);
// Define benchmark function.
void BM_Conv2DNchwFchw(benchmark::State &state)
{
for (auto _ : state)
{
for (int i = 0; i < state.range(0); ++i)
{
_mlir_ciface_conv_2d_nchw_fchw(&inputMemRef, &filterMemRef,
&outputMemRef);
}
}
}
} // namespace
// Register benchmarking function with different arguments.
BENCHMARK(BM_Conv2DNchwFchw)->Arg(1);
BENCHMARK(BM_Conv2DNchwFchw)->Arg(4);
// Print result function.
void printResult()
{
// Clear the output memref.
MemRef<float, 4> outputMemRef(sizesOutput, 0.0);
// Run the mlir function.
_mlir_ciface_conv_2d_nchw_fchw(&inputMemRef, &filterMemRef,
&outputMemRef);
// Print the output.
std::cout << "Output: [ ";
for (int i = 0; i < 8; ++i)
std::cout << outputMemRef[i] << " ";
std::cout << "]" << std::endl;
}
//===- 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 conv2d(nchw_filter_fchw) benchmark.
//
//===----------------------------------------------------------------------===//
#include <benchmark/benchmark.h>
void printResult();
int main(int argc, char **argv)
{
// Run benchmark.
::benchmark::Initialize(&argc, argv);
::benchmark::RunSpecifiedBenchmarks();
// Print result.
printResult();
return 0;
}
\ No newline at end of file
set(BUDDY_OPT_ATTR avx512f)
set(LLVM_MLIR_BINARY_DIR ${BUDDY_OPT_BUILD_DIR}/../llvm/build/bin)
add_custom_command(OUTPUT conv-2d-nhwc-hwcf.o
COMMAND ${LLVM_MLIR_BINARY_DIR}/mlir-opt ${BUDDY_SOURCE_DIR}/benchmarks/DeepLearning/Ops/Conv2DNhwcHwcfOp/Conv2DNhwcHwcf.mlir
-convert-linalg-to-loops
-convert-scf-to-std
-convert-linalg-to-llvm
-lower-affine
--convert-memref-to-llvm
-convert-std-to-llvm='emit-c-wrappers=1'
-reconcile-unrealized-casts|
${LLVM_MLIR_BINARY_DIR}/mlir-translate --mlir-to-llvmir |
${LLVM_MLIR_BINARY_DIR}/llc
-mtriple=x86_64-unknown-linux-gnu
-mattr=${BUDDY_OPT_ATTR}
--filetype=obj
-o ${BUDDY_BINARY_DIR}/../benchmarks/DeepLearning/Ops/Conv2DNhwcHwcfOp/conv-2d-nhwc-hwcf.o
)
add_library(Conv2DNhwcHwcf conv-2d-nhwc-hwcf.o)
set_target_properties(Conv2DNhwcHwcf PROPERTIES LINKER_LANGUAGE CXX)
add_executable(conv-2d-nhwc-hwcf-benchmark Main.cpp MLIROptBenchmark.cpp)
target_link_libraries(conv-2d-nhwc-hwcf-benchmark
GoogleBenchmark
Conv2DNhwcHwcf
Container
)
// generate from iree processed mobilenet mlir file
func @conv_2d_nhwc_hwcf(%input: memref<?x?x?x?xf32>, %filter: memref<?x?x?x?xf32>, %output: memref<?x?x?x?xf32>) {
linalg.conv_2d_nhwc_hwcf
{dilations = dense<1> : tensor<2xi64>, strides = dense<1> : tensor<2xi64>}
ins(%input, %filter : memref<?x?x?x?xf32>, memref<?x?x?x?xf32>)
outs(%output : memref<?x?x?x?xf32>)
return
}
//===- MLIROptBenchmark.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 conv2d(nhwc-hwcf) operation.
//
//===----------------------------------------------------------------------===//
#include "Utils/Container.h"
#include <benchmark/benchmark.h>
namespace
{
// Declare the mobilenet C interface.
extern "C"
{
void _mlir_ciface_conv_2d_nhwc_hwcf(MemRef<float, 4> *input,
MemRef<float, 4> *filter,
MemRef<float, 4> *output);
}
intptr_t sizesInput[4] = {1, 3, 3, 2};
intptr_t sizesFilter[4] = {2, 2, 2, 2};
intptr_t sizesOutput[4] = {1, 2, 2, 2};
// Create input, filter, and output.
MemRef<float, 4> inputMemRef(sizesInput, 2.0);
MemRef<float, 4> filterMemRef(sizesFilter, 3.0);
MemRef<float, 4> outputMemRef(sizesOutput, 0.0);
// Define benchmark function.
void BM_Conv2DNhwcHwcf(benchmark::State &state)
{
for (auto _ : state)
{
for (int i = 0; i < state.range(0); ++i)
{
_mlir_ciface_conv_2d_nhwc_hwcf(&inputMemRef, &filterMemRef,
&outputMemRef);
}
}
}
} // namespace
// Register benchmarking function with different arguments.
BENCHMARK(BM_Conv2DNhwcHwcf)->Arg(1);
BENCHMARK(BM_Conv2DNhwcHwcf)->Arg(4);
// Print result function.
void printResult()
{
// Clear the output memref.
MemRef<float, 4> outputMemRef(sizesOutput, 0.0);
// Run the mlir function.
_mlir_ciface_conv_2d_nhwc_hwcf(&inputMemRef, &filterMemRef,
&outputMemRef);
// Print the output.
std::cout << "Output: [ ";
for (int i = 0; i < 8; ++i)
std::cout << outputMemRef[i] << " ";
std::cout << "]" << std::endl;
}
//===- 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 conv2d(nhwc_filter_hwcf) benchmark.
//
//===----------------------------------------------------------------------===//
#include <benchmark/benchmark.h>
void printResult();
int main(int argc, char **argv)
{
// Run benchmark.
::benchmark::Initialize(&argc, argv);
::benchmark::RunSpecifiedBenchmarks();
// Print result.
printResult();
return 0;
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment