提交 9bfe2c66 编写于 作者: E eclipsess

change op test file (add,mul)

上级 45de1e84
# gen test
ADD_EXECUTABLE(paddle-mobile-test main.cpp test_helper.h elementwise_add_op_test.h test_include.h mul_op_test.h)
target_link_libraries(paddle-mobile-test paddle-mobile)
# gen test # gen test
ADD_EXECUTABLE(test-conv-op operators/test_cov_op.cpp test_helper.h test_include.h) ADD_EXECUTABLE(test-conv-op operators/test_cov_op.cpp test_helper.h test_include.h)
target_link_libraries(test-conv-op paddle-mobile) target_link_libraries(test-conv-op paddle-mobile)
# gen test
ADD_EXECUTABLE(test-mul-op operators/test_mul_op.cpp test_helper.h test_include.h)
target_link_libraries(test-mul-op paddle-mobile)
# gen test
ADD_EXECUTABLE(test-elementwiseadd-op operators/test_elementwise_add_op.cpp test_helper.h test_include.h)
target_link_libraries(test-elementwiseadd-op paddle-mobile)
# gen test log # gen test log
ADD_EXECUTABLE(test-log common/test_log.cpp) ADD_EXECUTABLE(test-log common/test_log.cpp)
target_link_libraries(test-log paddle-mobile) target_link_libraries(test-log paddle-mobile)
......
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
==============================================================================*/
#include "elementwise_add_op_test.h"
#include "framework/executor.h"
#include "io.h"
#include "mul_op_test.h"
#include "test_helper.h"
//
// template <typename T>
// void SetupTensor(paddle::framework::LoDTensor* input,
// paddle::framework::DDim dims, T lower, T upper) {
// static unsigned int seed = 100;
// std::mt19937 rng(seed++);
// std::uniform_real_distribution<double> uniform_dist(0, 1);
//
// T* input_ptr = input->mutable_data<T>(dims, paddle::platform::CPUPlace());
// for (int i = 0; i < input->numel(); ++i) {
// input_ptr[i] = static_cast<T>(uniform_dist(rng) * (upper - lower) +
// lower);
// }
//}
int main() {
std::string data_set = "cifar10";
//
// if (data_set == "cifar10") {
// SetupTensor<float>(&input, {FLAGS_batch_size, 3, 32, 32},
// static_cast<float>(0), static_cast<float>(1));
// } else if (data_set == "imagenet") {
// SetupTensor<float>(&input, {FLAGS_batch_size, 3, 224, 224},
// static_cast<float>(0), static_cast<float>(1));
// } else {
// LOG(FATAL) << "Only cifar10 or imagenet is supported.";
// }
paddle_mobile::Loader<paddle_mobile::CPU> loader;
auto program = loader.Load(std::string(
"../../../test/models/image_classification_resnet.inference.model"));
paddle_mobile::framework::Executor<paddle_mobile::CPU> executor(program);
paddle_mobile::framework::Tensor input;
SetupTensor<float>(&input, {1, 3, 32, 32}, static_cast<float>(0),
static_cast<float>(1));
float *input_ptr = input.data<float>();
for (int i = 0; i < input.numel(); ++i) {
// std::cout << input_ptr[i] << std::endl;
}
// std::cout << "input: " << input.memory_size() << std::endl;
// std::cout << "input: " << input.numel() << std::endl;
auto output = executor.predict(input);
// std::cout << "output: " << output->memory_size() << std::endl;
// std::cout << "output: " << output->numel() << std::endl;
// float* output_ptr = output->data<float>();
// for (int j = 0; j < output->numel(); ++j) {
// std::cout << " value of output: " << output_ptr[j] << std::endl;
//
paddle_mobile::test::testElementwiseAdd();
paddle_mobile::test::testMul();
return 0;
}
...@@ -18,119 +18,120 @@ SOFTWARE. ...@@ -18,119 +18,120 @@ SOFTWARE.
==============================================================================*/ ==============================================================================*/
#pragma once #pragma once
#include "operators/elementwise_add_op.h" #include "operators/elementwise_add_op.h"
#include "test_include.h" #include "../test_include.h"
namespace paddle_mobile { namespace paddle_mobile {
namespace framework { namespace framework {
template <typename Dtype> class TestElementwiseAddOp { template<typename Dtype>
public: class TestElementwiseAddOp {
TestElementwiseAddOp(const Program<Dtype> p) : program_(p) { public:
if (use_optimize_) { TestElementwiseAddOp(const Program<Dtype> p) : program_(p) {
to_predict_program_ = program_.optimizeProgram; if (use_optimize_) {
} else { to_predict_program_ = program_.optimizeProgram;
to_predict_program_ = program_.originProgram; } else {
} to_predict_program_ = program_.originProgram;
const std::vector<std::shared_ptr<BlockDesc>> blocks =
to_predict_program_->Blocks();
// DLOG << " **block size " << blocks.size();
for (int i = 0; i < blocks.size(); ++i) {
std::shared_ptr<BlockDesc> block_desc = blocks[i];
std::vector<std::shared_ptr<OpDesc>> ops = block_desc->Ops();
// DLOG << " ops " << ops.size();
for (int j = 0; j < ops.size(); ++j) {
std::shared_ptr<OpDesc> op = ops[j];
// if (op->Type() ==
// "elementwise_add") {
// if
// (op->GetAttrMap().at("axis").Get<int>()
// != -1) {
// DLOG << "attr: axis =
// "
// <<
// op->GetAttrMap().at("axis").Get<int>();
// }
// }
// DLOG << "op:" << op->Type();
if (op->Type() == "elementwise_add" &&
op->Input("X")[0] == "batch_norm_2.tmp_2") {
DLOG << " elementwise_add attr size: "
<< op->GetAttrMap().size();
DLOG << " inputs size: " << op->GetInputs().size();
DLOG << " outputs size: " << op->GetOutputs().size();
DLOG << " Input X is : " << op->Input("X")[0];
DLOG << " Input Y is : " << op->Input("Y")[0];
DLOG << " Output Out is : " << op->Output("Out")[0];
Attribute axis_attr = op->GetAttrMap().at("axis");
int axis = axis_attr.Get<int>();
DLOG << " Attr axis is : " << axis;
std::shared_ptr<operators::ElementwiseAddOp<Dtype, float>>
add = std::make_shared<
operators::ElementwiseAddOp<Dtype, float>>(
op->Type(), op->GetInputs(), op->GetOutputs(),
op->GetAttrMap(), program_.scope);
ops_of_block_[*block_desc.get()].push_back(add);
} }
const std::vector<std::shared_ptr<BlockDesc>> blocks =
to_predict_program_->Blocks();
// DLOG << " **block size " << blocks.size();
for (int i = 0; i < blocks.size(); ++i) {
std::shared_ptr<BlockDesc> block_desc = blocks[i];
std::vector<std::shared_ptr<OpDesc>> ops = block_desc->Ops();
// DLOG << " ops " << ops.size();
for (int j = 0; j < ops.size(); ++j) {
std::shared_ptr<OpDesc> op = ops[j];
// if (op->Type() ==
// "elementwise_add") {
// if
// (op->GetAttrMap().at("axis").Get<int>()
// != -1) {
// DLOG << "attr: axis =
// "
// <<
// op->GetAttrMap().at("axis").Get<int>();
// }
// }
// DLOG << "op:" << op->Type();
if (op->Type() == "elementwise_add" &&
op->Input("X")[0] == "batch_norm_2.tmp_2") {
DLOG << " elementwise_add attr size: "
<< op->GetAttrMap().size();
DLOG << " inputs size: " << op->GetInputs().size();
DLOG << " outputs size: " << op->GetOutputs().size();
DLOG << " Input X is : " << op->Input("X")[0];
DLOG << " Input Y is : " << op->Input("Y")[0];
DLOG << " Output Out is : " << op->Output("Out")[0];
Attribute axis_attr = op->GetAttrMap().at("axis");
int axis = axis_attr.Get<int>();
DLOG << " Attr axis is : " << axis;
std::shared_ptr<operators::ElementwiseAddOp<Dtype, float>>
add = std::make_shared<
operators::ElementwiseAddOp<Dtype, float>>(
op->Type(), op->GetInputs(), op->GetOutputs(),
op->GetAttrMap(), program_.scope);
ops_of_block_[*block_desc.get()].push_back(add);
}
}
}
}
std::shared_ptr<Tensor> predict_add(Tensor &t1, Tensor &t2) {
// feed
auto scope = program_.scope;
Variable *x_feed_value = scope->Var("batch_norm_2.tmp_2");
auto tensor_x = x_feed_value->GetMutable<Tensor>();
tensor_x->ShareDataWith(t1);
Variable *y_feed_value = scope->Var("batch_norm_0.tmp_3");
auto tensor_y = y_feed_value->GetMutable<Tensor>();
tensor_y->ShareDataWith(t2);
Variable *con_output = scope->Var("elementwise_add_0.tmp_0");
Tensor *output_tensor = con_output->GetMutable<Tensor>();
output_tensor->mutable_data<float>({1, 3, 224, 224});
// DLOG << typeid(output_tensor).name();
// DLOG << "output_tensor dims: " << output_tensor->dims();
std::shared_ptr<Tensor> out_tensor = std::make_shared<LoDTensor>();
out_tensor.reset(output_tensor);
predict_add(t1, t2, 0);
return out_tensor;
} }
}
} private:
const framework::Program<Dtype> program_;
std::shared_ptr<Tensor> predict_add(Tensor &t1, Tensor &t2) { std::shared_ptr<ProgramDesc> to_predict_program_;
// feed std::map<framework::BlockDesc,
auto scope = program_.scope; std::vector<std::shared_ptr<OperatorBase<Dtype>>>>
Variable *x_feed_value = scope->Var("batch_norm_2.tmp_2"); ops_of_block_;
auto tensor_x = x_feed_value->GetMutable<Tensor>(); bool use_optimize_ = false;
tensor_x->ShareDataWith(t1);
void predict_add(const Tensor &t1, const Tensor &t2, int block_id) {
Variable *y_feed_value = scope->Var("batch_norm_0.tmp_3"); std::shared_ptr<BlockDesc> to_predict_block =
auto tensor_y = y_feed_value->GetMutable<Tensor>(); to_predict_program_->Block(block_id);
tensor_y->ShareDataWith(t2); for (int j = 0; j < ops_of_block_[*to_predict_block.get()].size();
++j) {
Variable *con_output = scope->Var("elementwise_add_0.tmp_0"); auto op = ops_of_block_[*to_predict_block.get()][j];
Tensor *output_tensor = con_output->GetMutable<Tensor>(); DLOG << "op -> run()";
output_tensor->mutable_data<float>({1, 3, 224, 224}); op->Run();
// DLOG << typeid(output_tensor).name(); }
// DLOG << "output_tensor dims: " << output_tensor->dims(); }
};
std::shared_ptr<Tensor> out_tensor = std::make_shared<LoDTensor>();
out_tensor.reset(output_tensor); template
class TestElementwiseAddOp<CPU>;
predict_add(t1, t2, 0); } // namespace framework
return out_tensor; } // namespace paddle_mobile
} int main() {
private:
const framework::Program<Dtype> program_;
std::shared_ptr<ProgramDesc> to_predict_program_;
std::map<framework::BlockDesc,
std::vector<std::shared_ptr<OperatorBase<Dtype>>>>
ops_of_block_;
bool use_optimize_ = false;
void predict_add(const Tensor &t1, const Tensor &t2, int block_id) {
std::shared_ptr<BlockDesc> to_predict_block =
to_predict_program_->Block(block_id);
for (int j = 0; j < ops_of_block_[*to_predict_block.get()].size();
++j) {
auto op = ops_of_block_[*to_predict_block.get()][j];
DLOG << "op -> run()";
op->Run();
}
}
};
template class TestElementwiseAddOp<CPU>;
} // namespace framework
namespace test {
void testElementwiseAdd() {
DLOG << "----------**********----------"; DLOG << "----------**********----------";
DLOG << "begin to run ElementAddOp Test"; DLOG << "begin to run ElementAddOp Test";
paddle_mobile::Loader<paddle_mobile::CPU> loader; paddle_mobile::Loader<paddle_mobile::CPU> loader;
auto program = auto program =
loader.Load(std::string("../../test/models/" loader.Load(std::string("../../../test/models/"
"image_classification_resnet.inference.model")); "image_classification_resnet.inference.model"));
/// input x (1,3,224,224) /// input x (1,3,224,224)
...@@ -159,6 +160,6 @@ void testElementwiseAdd() { ...@@ -159,6 +160,6 @@ void testElementwiseAdd() {
DLOG << inputx_ptr[226] << " + " << inputy_ptr[2] << " = " DLOG << inputx_ptr[226] << " + " << inputy_ptr[2] << " = "
<< output_add_ptr[226]; << output_add_ptr[226];
return 0;
} }
} // namespace test
} // namespace paddle_mobile
...@@ -18,121 +18,123 @@ SOFTWARE. ...@@ -18,121 +18,123 @@ SOFTWARE.
==============================================================================*/ ==============================================================================*/
#pragma once #pragma once
#include "operators/mul_op.h" #include "operators/mul_op.h"
#include "test_include.h" #include "../test_include.h"
namespace paddle_mobile { namespace paddle_mobile {
namespace framework { namespace framework {
template <typename Dtype> class TestMulOp { template<typename Dtype>
public: class TestMulOp {
TestMulOp(const Program<Dtype> p) : program_(p) { public:
if (use_optimize_) { TestMulOp(const Program <Dtype> p) : program_(p) {
to_predict_program_ = program_.optimizeProgram; if (use_optimize_) {
} else { to_predict_program_ = program_.optimizeProgram;
to_predict_program_ = program_.originProgram; } else {
} to_predict_program_ = program_.originProgram;
}
const std::vector<std::shared_ptr<BlockDesc>> blocks = const std::vector<std::shared_ptr<BlockDesc>> blocks =
to_predict_program_->Blocks(); to_predict_program_->Blocks();
// DLOG << " **block size " << blocks.size(); // DLOG << " **block size " << blocks.size();
for (int i = 0; i < blocks.size(); ++i) { for (int i = 0; i < blocks.size(); ++i) {
std::shared_ptr<BlockDesc> block_desc = blocks[i]; std::shared_ptr<BlockDesc> block_desc = blocks[i];
std::vector<std::shared_ptr<OpDesc>> ops = block_desc->Ops(); std::vector<std::shared_ptr<OpDesc>> ops = block_desc->Ops();
// DLOG << " ops " << ops.size(); // DLOG << " ops " << ops.size();
for (int j = 0; j < ops.size(); ++j) { for (int j = 0; j < ops.size(); ++j) {
std::shared_ptr<OpDesc> op = ops[j]; std::shared_ptr<OpDesc> op = ops[j];
// if (op->Type() == "mul") { // if (op->Type() == "mul") {
// DLOG << "x_num_col_dims : // DLOG << "x_num_col_dims :
// " // "
// << op->GetAttrMap() // << op->GetAttrMap()
// .at("x_num_col_dims") // .at("x_num_col_dims")
// .Get<int>(); // .Get<int>();
// DLOG << "y_num_col_dims : // DLOG << "y_num_col_dims :
// " // "
// << op->GetAttrMap() // << op->GetAttrMap()
// .at("y_num_col_dims") // .at("y_num_col_dims")
// .Get<int>(); // .Get<int>();
// DLOG << " Input X is : " // DLOG << " Input X is : "
// << op->Input("X")[0]; // << op->Input("X")[0];
// } // }
// DLOG << "op:" << op->Type(); // DLOG << "op:" << op->Type();
if (op->Type() == "mul" && if (op->Type() == "mul" &&
op->Input("X")[0] == "pool2d_0.tmp_0") { op->Input("X")[0] == "pool2d_0.tmp_0") {
DLOG << " mul attr size: " << op->GetAttrMap().size(); DLOG << " mul attr size: " << op->GetAttrMap().size();
DLOG << " inputs size: " << op->GetInputs().size(); DLOG << " inputs size: " << op->GetInputs().size();
DLOG << " outputs size: " << op->GetOutputs().size(); DLOG << " outputs size: " << op->GetOutputs().size();
DLOG << " Input X is : " << op->Input("X")[0]; DLOG << " Input X is : " << op->Input("X")[0];
DLOG << " Input Y is : " << op->Input("Y")[0]; DLOG << " Input Y is : " << op->Input("Y")[0];
DLOG << " Output Out is : " << op->Output("Out")[0]; DLOG << " Output Out is : " << op->Output("Out")[0];
DLOG << "x_num_col_dims : " DLOG << "x_num_col_dims : "
<< op->GetAttrMap().at("x_num_col_dims").Get<int>(); << op->GetAttrMap().at("x_num_col_dims").Get<int>();
DLOG << "y_num_col_dims : " DLOG << "y_num_col_dims : "
<< op->GetAttrMap().at("y_num_col_dims").Get<int>(); << op->GetAttrMap().at("y_num_col_dims").Get<int>();
std::shared_ptr<operators::MulOp<Dtype, float>> add = std::shared_ptr<operators::MulOp<Dtype, float>> add =
std::make_shared<operators::MulOp<Dtype, float>>( std::make_shared<operators::MulOp<Dtype, float>>(
op->Type(), op->GetInputs(), op->GetOutputs(), op->Type(), op->GetInputs(), op->GetOutputs(),
op->GetAttrMap(), program_.scope); op->GetAttrMap(), program_.scope);
ops_of_block_[*block_desc.get()].push_back(add); ops_of_block_[*block_desc.get()].push_back(add);
}
}
} }
} }
}
}
std::shared_ptr<Tensor> predict_add(Tensor &t1, Tensor &t2) { std::shared_ptr<Tensor> predict_add(Tensor &t1, Tensor &t2) {
// feed // feed
auto scope = program_.scope; auto scope = program_.scope;
Variable *x_feed_value = scope->Var("pool2d_0.tmp_0"); Variable *x_feed_value = scope->Var("pool2d_0.tmp_0");
auto tensor_x = x_feed_value->GetMutable<Tensor>(); auto tensor_x = x_feed_value->GetMutable<Tensor>();
tensor_x->ShareDataWith(t1); tensor_x->ShareDataWith(t1);
Variable *y_feed_value = scope->Var("fc_0.w_0"); Variable *y_feed_value = scope->Var("fc_0.w_0");
auto tensor_y = y_feed_value->GetMutable<Tensor>(); auto tensor_y = y_feed_value->GetMutable<Tensor>();
tensor_y->ShareDataWith(t2); tensor_y->ShareDataWith(t2);
Variable *con_output = scope->Var("fc_0.tmp_0"); Variable *con_output = scope->Var("fc_0.tmp_0");
Tensor *output_tensor = con_output->GetMutable<Tensor>(); Tensor *output_tensor = con_output->GetMutable<Tensor>();
output_tensor->mutable_data<float>({3, 3}); output_tensor->mutable_data<float>({3, 3});
// DLOG << typeid(output_tensor).name(); // DLOG << typeid(output_tensor).name();
// DLOG << "output_tensor dims: " << output_tensor->dims(); // DLOG << "output_tensor dims: " << output_tensor->dims();
std::shared_ptr<Tensor> out_tensor = std::make_shared<LoDTensor>(); std::shared_ptr<Tensor> out_tensor = std::make_shared<LoDTensor>();
out_tensor.reset(output_tensor); out_tensor.reset(output_tensor);
predict_add(t1, t2, 0); predict_add(t1, t2, 0);
return out_tensor; return out_tensor;
} }
private: private:
const framework::Program<Dtype> program_; const framework::Program<Dtype> program_;
std::shared_ptr<ProgramDesc> to_predict_program_; std::shared_ptr<ProgramDesc> to_predict_program_;
std::map<framework::BlockDesc, std::map<framework::BlockDesc,
std::vector<std::shared_ptr<OperatorBase<Dtype>>>> std::vector<std::shared_ptr<OperatorBase < Dtype>>>>
ops_of_block_; ops_of_block_;
bool use_optimize_ = false; bool use_optimize_ = false;
void predict_add(const Tensor &t1, const Tensor &t2, int block_id) { void predict_add(const Tensor &t1, const Tensor &t2, int block_id) {
std::shared_ptr<BlockDesc> to_predict_block = std::shared_ptr<BlockDesc> to_predict_block =
to_predict_program_->Block(block_id); to_predict_program_->Block(block_id);
for (int j = 0; j < ops_of_block_[*to_predict_block.get()].size(); for (int j = 0; j < ops_of_block_[*to_predict_block.get()].size();
++j) { ++j) {
auto op = ops_of_block_[*to_predict_block.get()][j]; auto op = ops_of_block_[*to_predict_block.get()][j];
DLOG << "op -> run()"; DLOG << "op -> run()";
op->Run(); op->Run();
} }
} }
}; };
template class TestMulOp<CPU>; template
} // namespace framework class TestMulOp<CPU>;
} // namespace framework
} // namespaece paddle_mobile
namespace test { int main () {
void testMul() {
DLOG << "----------**********----------"; DLOG << "----------**********----------";
DLOG << "begin to run MulOp Test"; DLOG << "begin to run MulOp Test";
paddle_mobile::Loader<paddle_mobile::CPU> loader; paddle_mobile::Loader<paddle_mobile::CPU> loader;
auto program = auto program =
loader.Load(std::string("../../test/models/" loader.Load(std::string("../../../test/models/"
"image_classification_resnet.inference.model")); "image_classification_resnet.inference.model"));
/// input x (3,2,1,1) /// input x (3,2,1,1)
...@@ -185,6 +187,5 @@ void testMul() { ...@@ -185,6 +187,5 @@ void testMul() {
DLOG << inputx_ptr[0] << " x " << inputy_ptr[0] << " + " << inputx_ptr[1] DLOG << inputx_ptr[0] << " x " << inputy_ptr[0] << " + " << inputx_ptr[1]
<< " x " << inputy_ptr[0 + 3] << " = " << output_mul_ptr[0]; << " x " << inputy_ptr[0 + 3] << " = " << output_mul_ptr[0];
return 0;
} }
} // namespace test
} // namespace paddle_mobile
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册