提交 6703eb21 编写于 作者: L Liangliang He

Add conv2d 1x1 stride=1 benchmark

上级 7fbe7361
...@@ -59,6 +59,7 @@ cc_test( ...@@ -59,6 +59,7 @@ cc_test(
srcs = glob(["*_benchmark.cc"]), srcs = glob(["*_benchmark.cc"]),
deps = [ deps = [
":ops", ":ops",
":test",
"//mace/core:core", "//mace/core:core",
"//mace/core:test_benchmark_main", "//mace/core:test_benchmark_main",
], ],
......
...@@ -4,12 +4,12 @@ ...@@ -4,12 +4,12 @@
#include <algorithm> #include <algorithm>
#include "mace/core/operator.h"
#include "mace/core/testing/test_benchmark.h" #include "mace/core/testing/test_benchmark.h"
#include "mace/kernels/conv_2d.h" #include "mace/ops/conv_2d.h"
#include "mace/kernels/conv_pool_2d_util.h" #include "mace/ops/ops_test_util.h"
namespace mace { namespace mace {
namespace kernels {
template <DeviceType D, typename T> template <DeviceType D, typename T>
static void Conv2d(int iters, int batch, int channels, int height, int width, static void Conv2d(int iters, int batch, int channels, int height, int width,
...@@ -17,8 +17,32 @@ static void Conv2d(int iters, int batch, int channels, int height, int width, ...@@ -17,8 +17,32 @@ static void Conv2d(int iters, int batch, int channels, int height, int width,
Padding padding, int output_channels) { Padding padding, int output_channels) {
mace::testing::StopTiming(); mace::testing::StopTiming();
OpsTestNet net;
OpDefBuilder("Conv2d", "Conv2dTest")
.Input("Input")
.Input("Filter")
.Input("Bias")
.Output("Output")
.Finalize(net.operator_def());
// Add args
net.AddIntsArg("strides", {stride, stride});
net.AddIntArg("padding", padding);
net.AddIntsArg("dilations", {1, 1});
// Add input data
net.AddRandomInput<float>("Input", {batch, channels, height, width});
net.AddRandomInput<float>("Filter", {output_channels, channels, kernel_h, kernel_w});
net.AddRandomInput<float>("Bias", {output_channels});
// Worm-up
for (int i = 0; i < 5; ++i) {
net.RunOp(D);
}
mace::testing::StartTiming(); mace::testing::StartTiming();
while(iters--) { while(iters--) {
net.RunOp(D);
} }
} }
...@@ -36,7 +60,7 @@ static void Conv2d(int iters, int batch, int channels, int height, int width, ...@@ -36,7 +60,7 @@ static void Conv2d(int iters, int batch, int channels, int height, int width,
BM_CONV_2D_MACRO(N, C, H, W, KH, KW, S, P, OC, TYPE, CPU); \ BM_CONV_2D_MACRO(N, C, H, W, KH, KW, S, P, OC, TYPE, CPU); \
BM_CONV_2D_MACRO(N, C, H, W, KH, KW, S, P, OC, TYPE, NEON); BM_CONV_2D_MACRO(N, C, H, W, KH, KW, S, P, OC, TYPE, NEON);
BM_CONV_2D(1, 3, 256, 256, 1, 1, 1, VALID, 64, float);
BM_CONV_2D(1, 64, 32, 32, 1, 1, 1, VALID, 128, float); BM_CONV_2D(1, 64, 32, 32, 1, 1, 1, VALID, 128, float);
} // namespace kernels
} // namespace mace } // namespace mace
...@@ -3,8 +3,8 @@ ...@@ -3,8 +3,8 @@
// //
#include "mace/core/operator.h" #include "mace/core/operator.h"
#include "mace/ops/ops_test_util.h"
#include "mace/ops/conv_2d.h" #include "mace/ops/conv_2d.h"
#include "mace/ops/ops_test_util.h"
using namespace mace; using namespace mace;
......
...@@ -37,17 +37,33 @@ class OpDefBuilder { ...@@ -37,17 +37,33 @@ class OpDefBuilder {
class OpsTestNet { class OpsTestNet {
public: public:
OpsTestNet() {}
template <typename T> template <typename T>
void AddInputFromArray(const char* name, void AddInputFromArray(const char* name,
const std::vector<index_t>& shape, const std::vector<index_t>& shape,
const std::vector<T>& data) { const std::vector<T>& data) {
Tensor* input = ws_.CreateTensor(name, cpu_allocator(), DataTypeToEnum<T>::v()); Tensor* input = ws_.CreateTensor(name, cpu_allocator(), DataTypeToEnum<T>::v());
input->Resize(shape); input->Resize(shape);
float* input_data = input->mutable_data<float>(); T* input_data = input->mutable_data<T>();
// TODO check the dims MACE_CHECK(input->size() == data.size());
memcpy(input_data, data.data(), data.size() * sizeof(T)); memcpy(input_data, data.data(), data.size() * sizeof(T));
} }
template <typename T>
void AddRandomInput(const char* name, const std::vector<index_t>& shape) {
Tensor* input = ws_.CreateTensor(name, cpu_allocator(), DataTypeToEnum<T>::v());
input->Resize(shape);
float* input_data = input->mutable_data<T>();
std::random_device rd;
std::mt19937 gen(rd());
std::normal_distribution<T> nd(0, 1);
std::generate(input_data, input_data + input->size(),
[&gen, &nd]{ return nd(gen); });
}
void AddIntArg(const char* name, const int value) { void AddIntArg(const char* name, const int value) {
auto arg = op_def_.add_arg(); auto arg = op_def_.add_arg();
arg->set_name(name); arg->set_name(name);
...@@ -98,7 +114,7 @@ class OpsTestNet { ...@@ -98,7 +114,7 @@ class OpsTestNet {
if (!net_) { if (!net_) {
NetDef net_def; NetDef net_def;
net_def.add_op()->CopyFrom(op_def_); net_def.add_op()->CopyFrom(op_def_);
VLOG(0) << net_def.DebugString(); VLOG(3) << net_def.DebugString();
net_ = CreateNet(net_def, &ws_, device); net_ = CreateNet(net_def, &ws_, device);
} }
return net_->Run(); return net_->Run();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册