executor.cpp 5.2 KB
Newer Older
朔-望's avatar
朔-望 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

/* 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 "executor.h"
#include "lod_tensor.h"
#include "operators/conv_op.h"
#include "variable.h"

namespace paddle_mobile {
26
    namespace framework {
朔-望's avatar
朔-望 已提交
27

28 29 30 31 32 33 34
        template <typename Dtype>
        Executor<Dtype>::Executor(const Program<Dtype> p) : program_(p) {
            if (use_optimize_) {
                to_predict_program_ = program_.optimizeProgram;
            } else {
                to_predict_program_ = program_.originProgram;
            }
朔-望's avatar
朔-望 已提交
35

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
            const std::vector<std::shared_ptr<BlockDesc>> blocks =
                to_predict_program_->Blocks();
            //  std::cout << " **block size " << blocks.size() << std::endl;
            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();
                //    std::cout << " ops " << ops.size() << std::endl;
                for (int j = 0; j < ops.size(); ++j) {
                    std::shared_ptr<OpDesc> op = ops[j];
                    //        std::cout << " input 0 " << op->Input("Input")[0]
                    //        << std::endl;
                    if (op->Type() == "conv2d" &&
                        op->Input("Input")[0] == "pixel") {
                        //        std::cout << " conv2d attr size: " <<
                        //        op->GetAttrMap().size()
                        //                  << std::endl;
                        //        std::cout << " input size: " <<
                        //        op->GetInputs().size() <<
                        //        std::endl;
朔-望's avatar
朔-望 已提交
55

56 57 58
                        //        std::cout << " output size: " <<
                        //        op->GetOutputs().size() <<
                        //        std::endl;
朔-望's avatar
朔-望 已提交
59

60 61 62 63 64 65 66
                        Attribute strides_attr = op->GetAttrMap().at("strides");
                        std::vector<int> stride =
                            strides_attr.Get<std::vector<int>>();
                        for (int k = 0; k < stride.size(); ++k) {
                            //          std::cout << " stride " << stride[k] <<
                            //          std::endl;
                        }
朔-望's avatar
朔-望 已提交
67

68 69 70 71 72 73 74 75 76
                        std::shared_ptr<operators::ConvOp<Dtype, float>> conv =
                            std::make_shared<operators::ConvOp<Dtype, float>>(
                                op->Type(), op->GetInputs(), op->GetOutputs(),
                                op->GetAttrMap(), program_.scope);
                        ops_of_block_[*block_desc.get()].push_back(conv);
                    }
                }
            }
        }
朔-望's avatar
朔-望 已提交
77

78 79 80 81 82 83 84
        template <typename Dtype>
        std::shared_ptr<Tensor> Executor<Dtype>::predict(Tensor &t) {
            // feed
            auto scope = program_.scope;
            Variable *g_feed_value = scope->Var("pixel");
            auto tensor = g_feed_value->GetMutable<Tensor>();
            tensor->ShareDataWith(t);
朔-望's avatar
朔-望 已提交
85

86 87 88 89 90 91
            Variable *con_output = scope->Var("conv2d_0.tmp_0");
            Tensor *output_tensor = con_output->GetMutable<Tensor>();
            output_tensor->mutable_data<float>({1, 16, 32, 32});
            //  std::cout << typeid(output_tensor).name() << std::endl;
            //  std::cout << "output_tensor dims: " << output_tensor->dims() <<
            //  std::endl;
朔-望's avatar
朔-望 已提交
92

93 94
            std::shared_ptr<Tensor> out_tensor = std::make_shared<LoDTensor>();
            out_tensor.reset(output_tensor);
朔-望's avatar
朔-望 已提交
95

96 97 98
            predict(t, 0);
            return out_tensor;
        }
朔-望's avatar
朔-望 已提交
99

100 101 102 103 104 105 106 107 108 109 110
        template <typename Dtype>
        void Executor<Dtype>::predict(const Tensor &t, 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];
                //    std::cout << "开始run" << std::endl;
                op->Run();
            }
        }
朔-望's avatar
朔-望 已提交
111

112
        template class Executor<CPU>;
朔-望's avatar
朔-望 已提交
113

114
    } // namespace framework
L
liuruilong 已提交
115
} // namespace paddle_mobile