提交 e6459329 编写于 作者: E eclipsess

code style

上级 9bfe2c66
...@@ -17,114 +17,112 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE ...@@ -17,114 +17,112 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
==============================================================================*/ ==============================================================================*/
#pragma once #pragma once
#include "operators/elementwise_add_op.h"
#include "../test_include.h" #include "../test_include.h"
#include "operators/elementwise_add_op.h"
namespace paddle_mobile { namespace paddle_mobile {
namespace framework { namespace framework {
template<typename Dtype> template <typename Dtype> class TestElementwiseAddOp {
class TestElementwiseAddOp { public:
public: TestElementwiseAddOp(const Program<Dtype> p) : program_(p) {
TestElementwiseAddOp(const Program<Dtype> p) : program_(p) { if (use_optimize_) {
if (use_optimize_) { to_predict_program_ = program_.optimizeProgram;
to_predict_program_ = program_.optimizeProgram; } else {
} else { to_predict_program_ = program_.originProgram;
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() ==
// if (op->Type() == // "elementwise_add") {
// "elementwise_add") { // if
// if // (op->GetAttrMap().at("axis").Get<int>()
// (op->GetAttrMap().at("axis").Get<int>() // != -1) {
// != -1) { // DLOG << "attr: axis =
// DLOG << "attr: axis = // "
// " // <<
// << // op->GetAttrMap().at("axis").Get<int>();
// op->GetAttrMap().at("axis").Get<int>(); // }
// } // }
// } // DLOG << "op:" << op->Type();
// DLOG << "op:" << op->Type(); if (op->Type() == "elementwise_add" &&
if (op->Type() == "elementwise_add" && op->Input("X")[0] == "batch_norm_2.tmp_2") {
op->Input("X")[0] == "batch_norm_2.tmp_2") { DLOG << " elementwise_add attr size: "
DLOG << " elementwise_add attr size: " << op->GetAttrMap().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]; Attribute axis_attr = op->GetAttrMap().at("axis");
Attribute axis_attr = op->GetAttrMap().at("axis"); int axis = axis_attr.Get<int>();
int axis = axis_attr.Get<int>(); DLOG << " Attr axis is : " << axis;
DLOG << " Attr axis is : " << axis;
std::shared_ptr<operators::ElementwiseAddOp<Dtype, float>>
std::shared_ptr<operators::ElementwiseAddOp<Dtype, float>> add = std::make_shared<
add = std::make_shared< operators::ElementwiseAddOp<Dtype, float>>(
operators::ElementwiseAddOp<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) { }
// feed
auto scope = program_.scope; std::shared_ptr<Tensor> predict_add(Tensor &t1, Tensor &t2) {
Variable *x_feed_value = scope->Var("batch_norm_2.tmp_2"); // feed
auto tensor_x = x_feed_value->GetMutable<Tensor>(); auto scope = program_.scope;
tensor_x->ShareDataWith(t1); Variable *x_feed_value = scope->Var("batch_norm_2.tmp_2");
auto tensor_x = x_feed_value->GetMutable<Tensor>();
Variable *y_feed_value = scope->Var("batch_norm_0.tmp_3"); tensor_x->ShareDataWith(t1);
auto tensor_y = y_feed_value->GetMutable<Tensor>();
tensor_y->ShareDataWith(t2); Variable *y_feed_value = scope->Var("batch_norm_0.tmp_3");
auto tensor_y = y_feed_value->GetMutable<Tensor>();
Variable *con_output = scope->Var("elementwise_add_0.tmp_0"); tensor_y->ShareDataWith(t2);
Tensor *output_tensor = con_output->GetMutable<Tensor>();
output_tensor->mutable_data<float>({1, 3, 224, 224}); Variable *con_output = scope->Var("elementwise_add_0.tmp_0");
// DLOG << typeid(output_tensor).name(); Tensor *output_tensor = con_output->GetMutable<Tensor>();
// DLOG << "output_tensor dims: " << output_tensor->dims(); output_tensor->mutable_data<float>({1, 3, 224, 224});
// DLOG << typeid(output_tensor).name();
std::shared_ptr<Tensor> out_tensor = std::make_shared<LoDTensor>(); // DLOG << "output_tensor dims: " << output_tensor->dims();
out_tensor.reset(output_tensor);
std::shared_ptr<Tensor> out_tensor = std::make_shared<LoDTensor>();
predict_add(t1, t2, 0); out_tensor.reset(output_tensor);
return out_tensor;
} predict_add(t1, t2, 0);
return out_tensor;
private: }
const framework::Program<Dtype> program_;
std::shared_ptr<ProgramDesc> to_predict_program_; private:
std::map<framework::BlockDesc, const framework::Program<Dtype> program_;
std::vector<std::shared_ptr<OperatorBase<Dtype>>>> std::shared_ptr<ProgramDesc> to_predict_program_;
ops_of_block_; std::map<framework::BlockDesc,
bool use_optimize_ = false; std::vector<std::shared_ptr<OperatorBase<Dtype>>>>
ops_of_block_;
void predict_add(const Tensor &t1, const Tensor &t2, int block_id) { bool use_optimize_ = false;
std::shared_ptr<BlockDesc> to_predict_block =
to_predict_program_->Block(block_id); void predict_add(const Tensor &t1, const Tensor &t2, int block_id) {
for (int j = 0; j < ops_of_block_[*to_predict_block.get()].size(); std::shared_ptr<BlockDesc> to_predict_block =
++j) { to_predict_program_->Block(block_id);
auto op = ops_of_block_[*to_predict_block.get()][j]; for (int j = 0; j < ops_of_block_[*to_predict_block.get()].size();
DLOG << "op -> run()"; ++j) {
op->Run(); auto op = ops_of_block_[*to_predict_block.get()][j];
} DLOG << "op -> run()";
} op->Run();
}; }
}
template };
class TestElementwiseAddOp<CPU>;
} // namespace framework template class TestElementwiseAddOp<CPU>;
} // namespace framework
} // namespace paddle_mobile } // namespace paddle_mobile
int main() { int main() {
DLOG << "----------**********----------"; DLOG << "----------**********----------";
...@@ -162,4 +160,3 @@ int main() { ...@@ -162,4 +160,3 @@ int main() {
<< output_add_ptr[226]; << output_add_ptr[226];
return 0; return 0;
} }
...@@ -17,119 +17,117 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE ...@@ -17,119 +17,117 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
==============================================================================*/ ==============================================================================*/
#pragma once #pragma once
#include "operators/mul_op.h"
#include "../test_include.h" #include "../test_include.h"
#include "operators/mul_op.h"
namespace paddle_mobile { namespace paddle_mobile {
namespace framework { namespace framework {
template<typename Dtype> template <typename Dtype> class TestMulOp {
class TestMulOp { public:
public: TestMulOp(const Program<Dtype> p) : program_(p) {
TestMulOp(const Program <Dtype> p) : program_(p) { if (use_optimize_) {
if (use_optimize_) { to_predict_program_ = program_.optimizeProgram;
to_predict_program_ = program_.optimizeProgram; } else {
} else { to_predict_program_ = program_.originProgram;
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 template class TestMulOp<CPU>;
class TestMulOp<CPU>; } // namespace framework
} // namespace framework } // namespace paddle_mobile
} // namespaece paddle_mobile
int main () { int main() {
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;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册