Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
add op C++ benchmark test framework(matmul, elementwise_add op benchm…
Browse files Browse the repository at this point in the history
…ark test)
  • Loading branch information
wenming2014 committed Nov 9, 2020
1 parent f05f22b commit d14ffd6
Show file tree
Hide file tree
Showing 29 changed files with 659 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cinn/backends/codegen_cuda_dev.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "cinn/backends/codegen_cuda_dev.h"
#include "cinn/ir/ir_verify.h"

#include <fstream>
#include <set>
#include <unordered_set>

#include "cinn/ir/ir_verify.h"
#include "cinn/optim/remove_nested_block.h"

namespace cinn {
Expand Down
2 changes: 1 addition & 1 deletion cinn/backends/llvm/codegen_llvm.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "cinn/backends/llvm/codegen_llvm.h"
#include "cinn/ir/ir_verify.h"

#include <glog/logging.h>
#include <glog/stl_logging.h>
Expand All @@ -22,6 +21,7 @@
#include "cinn/common/type.h"
#include "cinn/ir/ir_operators.h"
#include "cinn/ir/ir_printer.h"
#include "cinn/ir/ir_verify.h"
#include "cinn/runtime/cinn_runtime.h"
#include "cinn/runtime/intrinsic.h"
#include "cinn/utils/string.h"
Expand Down
1 change: 0 additions & 1 deletion cinn/backends/llvm/llvm_optimizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ void LLVMModuleOptimizer::operator()(llvm::Module *m) {
std::for_each(m->begin(), m->end(), [&fpm](auto &fn) { fpm->run(fn); });
fpm->doFinalization();

// TODO(Superjomn) Enable this. This is quite slow when turned on.
mpm->run(*m);
}

Expand Down
1 change: 1 addition & 0 deletions cinn/backends/llvm/simple_jit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <llvm/Transforms/Scalar/GVN.h>
#include <llvm/Transforms/Scalar/Reassociate.h>
#include <llvm/Transforms/Scalar/SimplifyCFG.h>

#include <string>
#include <utility>

Expand Down
4 changes: 2 additions & 2 deletions cinn/common/test_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ struct BufferBuilder {

private:
template <typename T>
void RandomFloat(void* arr, int len) {
void RandomFloat(void* arr, uint64_t len) {
auto* data = static_cast<T*>(arr);
for (int i = 0; i < len; i++) {
for (uint64_t i = 0; i < len; i++) {
data[i] = static_cast<T>(rand()) / RAND_MAX; // NOLINT
}
}
Expand Down
2 changes: 1 addition & 1 deletion cinn/common/type.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once
#include <glog/logging.h>

#include <memory>
#include <string>

#include <memory>
#include "cinn/common/macros.h"
#include "cinn/runtime/cinn_runtime.h"

Expand Down
1 change: 1 addition & 0 deletions cinn/common/type_test.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "cinn/common/type.h"

#include <gtest/gtest.h>

namespace cinn::common {
Expand Down
3 changes: 2 additions & 1 deletion cinn/frontend/interpreter_test.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "cinn/frontend/interpreter.h"

#include <gtest/gtest.h>

#include "cinn/frontend/interpreter.h"
#include "cinn/runtime/use_extern_funcs.h"

DEFINE_string(model_dir, "", "");
Expand Down
1 change: 1 addition & 0 deletions cinn/ir/intrinsic_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <llvm/ADT/ArrayRef.h>
#include <llvm/ADT/SmallVector.h>
#include <llvm/Support/Casting.h>

#include "cinn/common/type.h"
#include "cinn/ir/ir.h"

Expand Down
1 change: 1 addition & 0 deletions cinn/ir/intrinsic_ops_test.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "cinn/ir/intrinsic_ops.h"

#include <gtest/gtest.h>

namespace cinn::ir {
Expand Down
2 changes: 1 addition & 1 deletion cinn/ir/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
#pragma once

#include <algorithm>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <variant>
#include <vector>

#include <map>
#include "cinn/common/shared.h"
#include "cinn/common/type.h"
#include "cinn/ir/function_base.h"
Expand Down
1 change: 1 addition & 0 deletions cinn/ir/ir_verify.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "cinn/ir/ir_verify.h"

#include "cinn/ir/ir_mutator.h"
#include "cinn/ir/ir_printer.h"

Expand Down
2 changes: 2 additions & 0 deletions cinn/ir/ir_verify_test.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "cinn/ir/ir_verify.h"

#include <gtest/gtest.h>

#include "cinn/ir/ir_operators.h"

namespace cinn::ir {
Expand Down
4 changes: 2 additions & 2 deletions cinn/runtime/cinn_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ typedef struct cinn_buffer_t {
memcpy(this->dims, dims, dimensions * sizeof(cinn_dimension_t));
}

CINN_ALWAYS_INLINE int num_elements() const {
int res = 1;
CINN_ALWAYS_INLINE uint64_t num_elements() const {
uint64_t res = 1;
for (int i = 0; i < dimensions; i++) {
res *= dims[i];
}
Expand Down
5 changes: 2 additions & 3 deletions cinn/runtime/cinn_x86_device_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ int cinn_x86_malloc(void* context, cinn_buffer_t* buf) {
if (buf->memory) {
free(buf->memory);
}
int bytes = buf->type.bytes() * buf->num_elements();
if (buf->align == 0) {
buf->memory = (unsigned char*)malloc(bytes);
buf->memory = (unsigned char*)malloc(memory_size);
} else {
buf->memory = (unsigned char*)aligned_alloc(buf->align, bytes);
buf->memory = (unsigned char*)aligned_alloc(buf->align, memory_size);
}
buf->memory_size = memory_size;
CINN_LOG("buf.memory size is %ld\n", buf->memory_size);
Expand Down
10 changes: 5 additions & 5 deletions cinn/utils/timer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ namespace cinn {
namespace utils {

float Timer::Stop() {
end_ = std::chrono::system_clock::now();
auto ts = std::chrono::duration_cast<std::chrono::milliseconds>(end_ - start_);
float ms = 1000.f * static_cast<float>(ts.count()) * std::chrono::milliseconds::period::num /
std::chrono::milliseconds::period::den;
end_ = std::chrono::high_resolution_clock::now();
auto ts = std::chrono::duration_cast<std::chrono::nanoseconds>(end_ - start_);
float ms = 1000. * static_cast<double>(ts.count()) * std::chrono::nanoseconds::period::num /
std::chrono::nanoseconds::period::den;
return ms;
}

void Timer::Start() { start_ = std::chrono::system_clock::now(); }
void Timer::Start() { start_ = std::chrono::high_resolution_clock::now(); }

} // namespace utils
} // namespace cinn
2 changes: 1 addition & 1 deletion cinn/utils/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Timer {
float Stop();

private:
std::chrono::time_point<std::chrono::system_clock> start_, end_;
std::chrono::time_point<std::chrono::high_resolution_clock, std::chrono::nanoseconds> start_, end_;
};

} // namespace utils
Expand Down
1 change: 1 addition & 0 deletions cinnrt/dialect/diagnostic_utils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <llvm/Support/SourceMgr.h>
#include <mlir/IR/Diagnostics.h>

#include <memory>

namespace cinnrt::dialect {
Expand Down
2 changes: 2 additions & 0 deletions cinnrt/dialect/mlir_loader.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once

#include <mlir/IR/Module.h>

#include <memory>
#include <string_view>

#include "cinn/frontend/syntax.h"

namespace cinnrt::dialect {
Expand Down
1 change: 1 addition & 0 deletions cinnrt/host_context/core_runtime.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <llvm/ADT/ArrayRef.h>

#include <memory>
#include <string>

Expand Down
2 changes: 1 addition & 1 deletion cmake/core.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function(cc_test TARGET_NAME)
set_property(TEST ${TARGET_NAME} PROPERTY RUN_SERIAL 1)
endif()
# No unit test should exceed 10 minutes.
set_tests_properties(${TARGET_NAME} PROPERTIES TIMEOUT 600)
set_tests_properties(${TARGET_NAME} PROPERTIES TIMEOUT 6000)
endif()
endfunction()

Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include_directories(${CMAKE_SOURCE_DIR}/cinn/runtime)
add_subdirectory(benchmark)

cc_test(test01_elementwise_add_main SRCS test01_elementwise_add_main.cc DEPS core
ARGS ${global_test_args}
Expand Down
8 changes: 8 additions & 0 deletions tests/benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
include_directories(${CMAKE_SOURCE_DIR}/cinn/runtime)
set(srcs test_utils.cc test_matmul.cc test_elementwise.cc)

cc_test(test_matmul SRCS test_matmul.cc test_utils.cc DEPS core ARGS ${global_test_args})
target_compile_options(test_matmul PRIVATE "-O3")

cc_test(test_elementwise SRCS test_elementwise.cc test_utils.cc DEPS core ARGS ${global_test_args})
target_compile_options(test_elementwise PRIVATE "-O3")
32 changes: 32 additions & 0 deletions tests/benchmark/test_elementwise.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "tests/benchmark/test_elementwise.h"

#include <gtest/gtest.h>

#include "cinn/cinn.h"
#include "cinn/hlir/framework/node.h"

namespace cinn {
namespace tests {

void ElementwiseAddTester::Compare() {
std::vector<float *> all_datas = GetAllDatas();
int out_dims = GetOutDims();
CHECK_EQ(all_datas.size(), 3U) << "elementwise_add should have 3 args.\n";
for (int i = 0; i < out_dims; ++i) {
EXPECT_EQ((all_datas[0][i] + all_datas[1][i]), all_datas[2][i]);
}
}

TEST(test_elementwise_add, default) {
int M = 100;
int N = 32;
std::vector<std::vector<int>> input_shapes{{M, N}, {M, N}};
std::string op_name = "elementwise_add";
hlir::framework::NodeAttr attrs;
ElementwiseAddTester add_tester(op_name, input_shapes);
std::vector<Type> type{Float(32)};
add_tester.TestOp("elementwise_add_default", attrs, type);
}

} // namespace tests
} // namespace cinn
24 changes: 24 additions & 0 deletions tests/benchmark/test_elementwise.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <string>
#include <vector>

#include "tests/benchmark/test_utils.h"

namespace cinn {
namespace tests {

class ElementwiseAddTester : public OpBenchmarkTester {
public:
ElementwiseAddTester(const std::string &op_name,
const std::vector<std::vector<int>> &input_shapes,
const common::Target &target = common::DefaultHostTarget(),
int repeat = 10,
float diff = 1e-5)
: OpBenchmarkTester(op_name, input_shapes, target, repeat, diff) {}

void Compare() override;
};

} // namespace tests
} // namespace cinn
Loading

0 comments on commit d14ffd6

Please sign in to comment.