ngraph_operator.cc 19.1 KB
Newer Older
B
baojun-nervana 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include <glog/logging.h>

#include <algorithm>
#include <map>

#include "paddle/fluid/framework/feed_fetch_type.h"
B
baojun-nervana 已提交
21 22 23
#include "paddle/fluid/framework/framework.pb.h"
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/ngraph_bridge.h"
B
baojun-nervana 已提交
24
#include "paddle/fluid/framework/ngraph_operator.h"
B
baojun-nervana 已提交
25
#include "paddle/fluid/framework/tensor.h"
B
baojun-nervana 已提交
26 27 28
#include "paddle/fluid/framework/var_desc.h"
#include "paddle/fluid/framework/var_type.h"

B
baojun-nervana 已提交
29 30
#include "ngraph/ngraph.hpp"

B
baojun-nervana 已提交
31 32 33
namespace paddle {
namespace framework {

B
baojun-nervana 已提交
34 35 36 37 38 39 40 41 42 43
static ngraph::Shape Ddim2Shape(const DDim& dims) {
  ngraph::Shape sp;
  for (int i = 0; i < dims.size(); ++i) {
    int k = dims[i];
    k = k == 0 ? 1 : k;
    sp.push_back(k);
  }
  return sp;
}

B
baojun-nervana 已提交
44 45 46 47 48 49 50 51
static std::map<proto::VarType::Type, ngraph::element::Type> pd2ng_type_map = {
    {proto::VarType::FP32, ngraph::element::f32},
    {proto::VarType::FP64, ngraph::element::f64},
    {proto::VarType::INT32, ngraph::element::i32},
    {proto::VarType::INT64, ngraph::element::i64},
    {proto::VarType::BOOL, ngraph::element::boolean},
};

B
baojun-nervana 已提交
52 53 54 55 56 57 58
typedef enum {                /* nGraph support state on ops          */
               FULL_TRAIN,    /* Support full ops for train           */
               PARTIAL_TRAIN, /* Support partial ops for train        */
               FULL_TEST,     /* Support full list of ops for test    */
               PARTIAL_TEST   /* Support partial list of ops for test */
} op_state;

B
baojun-nervana 已提交
59
// perform graph build through bridge and execute computation
B
baojun-nervana 已提交
60
class NgraphEngine {
B
baojun-nervana 已提交
61
 public:
B
baojun-nervana 已提交
62 63 64 65 66 67 68 69
  explicit NgraphEngine(const Scope& scope, const platform::Place& place,
                        const std::vector<std::shared_ptr<OperatorBase>>& ops,
                        const std::unordered_map<
                            std::string, ngraph::element::Type>& var_type_map,
                        const std::unordered_set<std::string>& persist,
                        const std::unordered_set<std::string>& fetches,
                        const std::unordered_set<std::string>& post_op_inputs,
                        op_state ng_op_state)
B
baojun-nervana 已提交
70 71 72 73 74 75 76
      : scope_(scope),
        place_(place),
        fused_ops_(ops),
        var_type_map_(var_type_map),
        persistables_(persist),
        fetches_(fetches),
        post_op_inputs_(post_op_inputs),
B
baojun-nervana 已提交
77 78 79 80 81 82 83 84 85 86 87
        ng_op_state_(ng_op_state) {
    var_in_node_map_ = std::make_shared<
        std::unordered_map<std::string, std::shared_ptr<ngraph::Node>>>();

    var_node_map_ = std::make_shared<
        std::unordered_map<std::string, std::shared_ptr<ngraph::Node>>>();

    BuildNgIO();

    GetNgFunction();
  }
B
baojun-nervana 已提交
88 89 90 91 92

  void Run(const Scope& scope, const platform::Place& place) const;

 private:
  static std::unordered_map<std::string, std::shared_ptr<ngraph::Function>>
B
baojun-nervana 已提交
93
      func_cache_;
B
baojun-nervana 已提交
94 95 96 97 98 99 100 101
  const Scope& scope_;
  const platform::Place& place_;
  std::vector<std::shared_ptr<OperatorBase>> fused_ops_;
  std::unordered_map<std::string, ngraph::element::Type> var_type_map_;
  std::unordered_set<std::string> persistables_;
  std::unordered_set<std::string> fetches_;
  std::unordered_set<std::string> post_op_inputs_;
  op_state ng_op_state_;
B
baojun-nervana 已提交
102

B
baojun-nervana 已提交
103
  // ngraph backend eg. CPU
B
baojun-nervana 已提交
104
  static std::shared_ptr<ngraph::runtime::Backend> backend_;
B
baojun-nervana 已提交
105
  // ngraph function to call and execute
B
baojun-nervana 已提交
106 107 108 109 110
  std::shared_ptr<ngraph::Function> ngraph_function_;
  // var_name of inputs
  std::vector<std::string> var_in_;
  // var_name of outputs from  fetch in order
  std::vector<std::string> var_out_;
B
baojun-nervana 已提交
111
  // map input vars to nodes
B
baojun-nervana 已提交
112 113 114 115 116 117 118
  std::shared_ptr<
      std::unordered_map<std::string, std::shared_ptr<ngraph::Node>>>
      var_in_node_map_;
  // map each var name with a ngraph node
  std::shared_ptr<
      std::unordered_map<std::string, std::shared_ptr<ngraph::Node>>>
      var_node_map_;
B
baojun-nervana 已提交
119
  // cache key to check if function is cached
B
baojun-nervana 已提交
120
  std::shared_ptr<std::string> GetCacheKey();
B
baojun-nervana 已提交
121
  // get ngraph input and define ngraph input parameters
B
baojun-nervana 已提交
122
  void GetNgInputShape(std::shared_ptr<OperatorBase> op);
B
baojun-nervana 已提交
123
  // Call ngraph bridge to map ops
B
baojun-nervana 已提交
124
  void BuildNgNodes();
B
baojun-nervana 已提交
125
  // get the ngraph input and output var list
B
baojun-nervana 已提交
126
  void BuildNgIO();
B
baojun-nervana 已提交
127
  // build ngraph function call
B
baojun-nervana 已提交
128
  void BuildNgFunction();
B
baojun-nervana 已提交
129
  // Check cache for ngraph function or otherwise build the function
B
baojun-nervana 已提交
130
  void GetNgFunction();
B
baojun-nervana 已提交
131 132 133
};

std::vector<std::vector<std::vector<std::unique_ptr<OperatorBase>>::iterator>>
B
baojun-nervana 已提交
134
NgraphOperator::NgraphOpIntervals(
B
baojun-nervana 已提交
135 136 137 138 139 140 141 142
    std::vector<std::unique_ptr<paddle::framework::OperatorBase>>* ops) {
  std::vector<std::vector<std::vector<std::unique_ptr<OperatorBase>>::iterator>>
      intervals;
  if (ops->empty()) {
    return intervals;
  }
  size_t size = ops->size();
  size_t left = 0;
B
baojun-nervana 已提交
143
  while (left < size && ops->at(left)->Type() != kFeedOpType) {
B
baojun-nervana 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    ++left;
  }
  if (left == size) {
    return intervals;
  }
  while (left < size && ops->at(left)->Type() == kFeedOpType) {
    ++left;
  }

  size_t right = left;
  while (right < size && ops->at(right)->Type() != kFetchOpType) {
    ++right;
  }
  if (right == size) {
    return intervals;
  }
  if (left >= right) return intervals;

  // (left, right - 1) represents indices between feed and fetch
  size_t pivot = left;
  while (pivot < right) {
    auto op_type = ops->at(pivot)->Type();
    if (paddle::framework::NgraphBridge::NG_NODE_MAP.find(op_type) ==
        paddle::framework::NgraphBridge::NG_NODE_MAP.end()) {
      ++pivot;
    } else {
      size_t start = pivot, end = start;
      while (pivot < right &&
             (paddle::framework::NgraphBridge::NG_NODE_MAP.find(
B
baojun-nervana 已提交
173
                  ops->at(pivot)->Type()) !=
B
baojun-nervana 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186
              paddle::framework::NgraphBridge::NG_NODE_MAP.end())) {
        ++pivot;
        ++end;
      }
      std::vector<std::vector<std::unique_ptr<OperatorBase>>::iterator>
          interval = {ops->begin() + start, ops->begin() + end};
      intervals.push_back(interval);
    }
  }  // end while

  return intervals;
}

B
baojun-nervana 已提交
187
NgraphOperator::NgraphOperator(
B
baojun-nervana 已提交
188 189 190
    const ProgramDesc& prog, size_t block_id,
    std::vector<std::unique_ptr<OperatorBase>>::iterator start,
    std::vector<std::unique_ptr<OperatorBase>>::iterator end,
B
baojun-nervana 已提交
191 192
    const std::string& type, const VariableNameMap& inputs,
    const VariableNameMap& outputs, const AttributeMap& attrs)
B
baojun-nervana 已提交
193 194 195
    : OperatorBase(type, inputs, outputs, attrs),
      pdesc_(prog),
      block_(block_id) {
B
baojun-nervana 已提交
196 197
  for (std::vector<std::unique_ptr<OperatorBase>>::iterator it = start;
       it != end; ++it) {
B
baojun-nervana 已提交
198
    fused_ops_.push_back(std::move(*it));
B
baojun-nervana 已提交
199 200 201 202 203 204
  }

  for (std::vector<std::unique_ptr<OperatorBase>>::iterator it = end;
       (*it)->Type() != kFetchOpType; ++it) {
    for (auto& var_name_item : (*it)->Inputs()) {
      for (auto& var_name : var_name_item.second) {
B
baojun-nervana 已提交
205
        post_op_inputs_.insert(var_name);
B
baojun-nervana 已提交
206 207 208 209 210
      }
    }
  }

  if ((*(start - 1))->Type() == kFeedOpType && (*end)->Type() == kFetchOpType) {
B
baojun-nervana 已提交
211
    is_full_ = true;
B
baojun-nervana 已提交
212 213
  }

B
baojun-nervana 已提交
214
  Process();
B
baojun-nervana 已提交
215 216
}

B
baojun-nervana 已提交
217
void NgraphOperator::Process() {
B
baojun-nervana 已提交
218
  auto& bdesc = pdesc_.Block(block_);
B
baojun-nervana 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
  for (auto& var : bdesc.AllVars()) {
    if (!(var->GetType() == proto::VarType::SELECTED_ROWS ||
          var->GetType() == proto::VarType::LOD_TENSOR ||
          var->GetType() == proto::VarType::LOD_TENSOR_ARRAY)) {
      continue;
    }

    auto var_name = var->Name();
    if (var->Name() == framework::kEmptyVarName) {
      continue;
    }

    if (var_name != "fetch" && var_name != "feed") {
      auto pd_type = var->GetDataType();
      if (pd2ng_type_map.find(pd_type) == pd2ng_type_map.end()) {
        PADDLE_THROW("Data type of var %s not found in pd2ng_type_map",
                     var_name);
      }
B
baojun-nervana 已提交
237
      var_type_map_[var_name] = pd2ng_type_map[pd_type];
B
baojun-nervana 已提交
238 239 240
    }

    if (var->Persistable()) {
B
baojun-nervana 已提交
241
      persistables_.insert(var->Name());
B
baojun-nervana 已提交
242 243 244 245 246 247
    }
  }

  for (auto* op : bdesc.AllOps()) {
    if (op->Type() == kFetchOpType) {
      std::string fetch_target_name = op->Input("X")[0];
B
baojun-nervana 已提交
248
      fetches_.insert(fetch_target_name);
B
baojun-nervana 已提交
249 250 251 252
    }
  }
}

B
baojun-nervana 已提交
253 254
void NgraphOperator::RunImpl(const Scope& scope,
                             const platform::Place& place) const {
B
baojun-nervana 已提交
255 256
  op_state ng_op_state = PARTIAL_TEST;
  auto& bdesc = pdesc_.Block(block_);
B
baojun-nervana 已提交
257 258
  for (auto* op : bdesc.AllOps()) {
    if (op->Type().find("_grad") != std::string::npos) {
B
baojun-nervana 已提交
259
      ng_op_state = PARTIAL_TRAIN;
B
baojun-nervana 已提交
260 261 262 263
      break;
    }
  }

B
baojun-nervana 已提交
264
  if (is_full_) {
B
baojun-nervana 已提交
265
    ng_op_state = ng_op_state == PARTIAL_TEST ? FULL_TEST : FULL_TRAIN;
B
baojun-nervana 已提交
266 267
  }

B
baojun-nervana 已提交
268 269 270 271
  NgraphEngine ngraph_engine(scope, place, fused_ops_, var_type_map_,
                             persistables_, fetches_, post_op_inputs_,
                             ng_op_state);
  ngraph_engine.Run(scope, place);
B
baojun-nervana 已提交
272 273
}

B
baojun-nervana 已提交
274
std::unordered_map<std::string, std::shared_ptr<ngraph::Function>>
B
baojun-nervana 已提交
275
    NgraphEngine::func_cache_ = {};
B
baojun-nervana 已提交
276

B
baojun-nervana 已提交
277
std::shared_ptr<ngraph::runtime::Backend> NgraphEngine::backend_ =
B
baojun-nervana 已提交
278 279
    ngraph::runtime::Backend::create("CPU");

B
baojun-nervana 已提交
280
void NgraphEngine::GetNgInputShape(std::shared_ptr<OperatorBase> op) {
X
Xin Pan 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294
  RuntimeContext ctx;
  for (auto& var_name_item : op->Inputs()) {
    std::vector<Variable*> input_vars = ctx.inputs[var_name_item.first];
    for (auto& var_name : var_name_item.second) {
      input_vars.push_back(scope_.FindVar(var_name));
    }
  }
  for (auto& var_name_item : op->Outputs()) {
    std::vector<Variable*> output_vars = ctx.outputs[var_name_item.first];
    for (auto& var_name : var_name_item.second) {
      output_vars.push_back(scope_.FindVar(var_name));
    }
  }
  op->RuntimeInferShape(scope_, place_, ctx);
B
baojun-nervana 已提交
295
  for (auto& var_name_item : op->Inputs()) {
B
baojun-nervana 已提交
296 297
    for (auto& var_name : var_name_item.second) {
      auto* var = scope_.FindVar(var_name);
B
baojun-nervana 已提交
298
      if (var && var->IsType<LoDTensor>()) {
B
baojun-nervana 已提交
299 300 301 302 303 304 305 306 307 308 309
        auto* tensor_pd = GetLoDTensorOrSelectedRowsValueFromVar(*var);
        auto sp = Ddim2Shape(tensor_pd->dims());
        if (std::find(var_in_.begin(), var_in_.end(), var_name) !=
            var_in_.end()) {
          if (var_node_map_->find(var_name) == var_node_map_->end()) {
            auto ng_type = var_type_map_.at(var_name);
            auto prm =
                std::make_shared<ngraph::op::Parameter>(ng_type, sp, true);
            (*var_node_map_)[var_name] = prm;
            (*var_in_node_map_)[var_name] = prm;
          }
B
baojun-nervana 已提交
310 311 312 313 314 315
        }
      }
    }
  }
}

B
baojun-nervana 已提交
316
void NgraphEngine::BuildNgNodes() {
B
baojun-nervana 已提交
317 318 319
  for (auto& var_name : var_out_) {
    if (var_node_map_->find(var_name) == var_node_map_->end()) {
      auto* var = scope_.FindVar(var_name);
B
baojun-nervana 已提交
320
      if (var && var->IsType<LoDTensor>()) {
B
baojun-nervana 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333
        auto* tensor_pd = GetLoDTensorOrSelectedRowsValueFromVar(*var);
        auto& ddim = tensor_pd->dims();
        auto ng_shape = Ddim2Shape(ddim);
        auto ng_type = var_type_map_.at(var_name);
        auto prm =
            std::make_shared<ngraph::op::Parameter>(ng_type, ng_shape, true);
        (*var_node_map_)[var_name] = prm;
      }
    }
  }

  paddle::framework::NgraphBridge ngb(var_node_map_);
  for (auto& op : fused_ops_) {
B
baojun-nervana 已提交
334
    ngb.BuildNgNode(op);
B
baojun-nervana 已提交
335 336 337
  }
}

B
baojun-nervana 已提交
338
void NgraphEngine::BuildNgIO() {
B
baojun-nervana 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
  std::unordered_set<std::string> inputs;
  std::unordered_set<std::string> outputs;

  for (auto& op : fused_ops_) {
    for (auto& var_name_item : op->Inputs()) {
      for (auto& var_name : var_name_item.second) {
        inputs.insert(var_name);
        const bool is_output = outputs.find(var_name) != outputs.end();
        if (!is_output &&
            std::find(var_in_.begin(), var_in_.end(), var_name) ==
                var_in_.end()) {
          // fill var_in here to keep lhs and rhs order
          var_in_.push_back(var_name);
        }
      }
    }

    if (op->Type() != "fill_constant") {
      GetNgInputShape(op);
    }

    for (auto& var_name_item : op->Outputs()) {
      PADDLE_ENFORCE_LE(var_name_item.second.size(), 1,
                        "op %s has more than 1 output - Not handling yet",
                        op->Type());
      for (auto& var_name : var_name_item.second) {
        outputs.insert(var_name);
      }
    }
  }

  // var_out.clear();
  for (auto& op : fused_ops_) {
    for (auto& var_name_item : op->Outputs()) {
      PADDLE_ENFORCE_LE(var_name_item.second.size(), 1,
                        "op %s has more than 1 output - Not handling yet",
                        op->Type());
      for (auto& var_name : var_name_item.second) {
        switch (ng_op_state_) {
          case PARTIAL_TEST:
            if (post_op_inputs_.find(var_name) != post_op_inputs_.end() ||
                fetches_.find(var_name) != fetches_.end()) {
              var_out_.push_back(var_name);
            }
            break;
          case FULL_TEST:
            if (fetches_.find(var_name) != fetches_.end()) {
              var_out_.push_back(var_name);
            }
            break;
          case PARTIAL_TRAIN:
            if (fetches_.find(var_name) != fetches_.end() ||
                post_op_inputs_.find(var_name) != post_op_inputs_.end() ||
                persistables_.find(var_name) != persistables_.end()) {
              var_out_.push_back(var_name);
            }
            break;
          case FULL_TRAIN:
            if (fetches_.find(var_name) != fetches_.end() ||
                persistables_.find(var_name) != persistables_.end()) {
              var_out_.push_back(var_name);
            }
            break;
          default:
            var_out_.push_back(var_name);
        }
      }
    }
  }
}

B
baojun-nervana 已提交
410
void NgraphEngine::BuildNgFunction() {
B
baojun-nervana 已提交
411
  BuildNgNodes();
B
baojun-nervana 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
  ngraph_function_ = nullptr;
  ngraph::NodeVector func_outputs;
  ngraph::op::ParameterVector func_inputs;

  for (auto& vo : var_out_) {
    func_outputs.push_back(var_node_map_->at(vo));
  }

  for (auto& vi : var_in_) {
    std::shared_ptr<ngraph::op::Parameter> prm =
        std::dynamic_pointer_cast<ngraph::op::Parameter>(
            var_in_node_map_->at(vi));
    func_inputs.push_back(prm);
  }

  ngraph_function_ =
      std::make_shared<ngraph::Function>(func_outputs, func_inputs);
}

B
baojun-nervana 已提交
431
std::shared_ptr<std::string> NgraphEngine::GetCacheKey() {
B
baojun-nervana 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
  auto cache_key = std::make_shared<std::string>("");
  *cache_key += std::to_string(fused_ops_.size());
  for (auto& op : fused_ops_) {
    *cache_key += op->Type();
  }
  for (auto& var_name : var_in_) {
    auto shape = var_node_map_->at(var_name)->get_shape();
    *cache_key += var_name;
    *cache_key += var_type_map_.at(var_name).c_type_string();
    for (size_t i = 0; i < shape.size(); ++i) {
      *cache_key += std::to_string(shape.at(i));
    }
  }

  for (auto& var_name : var_out_) {
    auto* var = scope_.FindVar(var_name);
B
baojun-nervana 已提交
448
    if (var && var->IsType<LoDTensor>()) {
B
baojun-nervana 已提交
449 450 451 452 453 454 455 456 457 458
      auto* tensor_pd = GetLoDTensorOrSelectedRowsValueFromVar(*var);
      auto& ddim = tensor_pd->dims();
      for (int i = 0; i < ddim.size(); ++i) {
        *cache_key += std::to_string(ddim[i]);
      }
    }
  }
  return cache_key;
}

B
baojun-nervana 已提交
459
void NgraphEngine::GetNgFunction() {
B
baojun-nervana 已提交
460 461 462 463 464 465 466 467 468 469 470 471 472 473
  bool cache_on = true;
  if (cache_on) {
    std::string cache_key_val = *GetCacheKey();
    if (func_cache_.find(cache_key_val) != func_cache_.end()) {
      ngraph_function_ = func_cache_.at(cache_key_val);
    } else {
      BuildNgFunction();
      func_cache_[cache_key_val] = ngraph_function_;
    }
  } else {
    BuildNgFunction();
  }
}

B
baojun-nervana 已提交
474
void NgraphEngine::Run(const Scope& scope, const platform::Place& place) const {
B
baojun-nervana 已提交
475 476 477 478 479 480 481 482
  std::vector<std::shared_ptr<ngraph::runtime::Tensor>> t_in;
  std::vector<std::shared_ptr<ngraph::runtime::Tensor>> t_out;

  for (size_t i = 0; i < var_in_.size(); ++i) {
    auto vi = var_in_.at(i);
    auto sp = var_node_map_->at(vi)->get_shape();
    std::shared_ptr<ngraph::runtime::Tensor> ti;
    auto* var = scope.FindVar(vi);
B
baojun-nervana 已提交
483
    if (var && var->IsType<LoDTensor>()) {
B
baojun-nervana 已提交
484 485 486
      auto* tensor_pd = GetLoDTensorOrSelectedRowsValueFromVar(*var);
      PADDLE_ENFORCE(sp == Ddim2Shape(tensor_pd->dims()),
                     "Ensure ngraph tensor layout align with paddle tensor");
Y
Yu Yang 已提交
487
      if (tensor_pd->type() == proto::VarType::FP32) {
B
baojun-nervana 已提交
488 489 490
        const float* arr = tensor_pd->data<float>();
        ti = backend_->create_tensor(ngraph::element::f32, sp,
                                     const_cast<float*>(arr));
Y
Yu Yang 已提交
491
      } else if (tensor_pd->type() == proto::VarType::INT32) {
B
baojun-nervana 已提交
492 493 494
        const int* arr = tensor_pd->data<int>();
        ti = backend_->create_tensor(ngraph::element::i32, sp,
                                     const_cast<int*>(arr));
Y
Yu Yang 已提交
495
      } else if (tensor_pd->type() == proto::VarType::INT64) {
B
baojun-nervana 已提交
496 497 498
        const int64_t* arr = tensor_pd->data<int64_t>();
        ti = backend_->create_tensor(ngraph::element::i64, sp,
                                     const_cast<int64_t*>(arr));
Y
Yu Yang 已提交
499
      } else if (tensor_pd->type() == proto::VarType::FP64) {
B
baojun-nervana 已提交
500 501 502
        const double* arr = tensor_pd->data<double>();
        ti = backend_->create_tensor(ngraph::element::f64, sp,
                                     const_cast<double*>(arr));
Y
Yu Yang 已提交
503
      } else if (tensor_pd->type() == proto::VarType::BOOL) {
B
baojun-nervana 已提交
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
        const bool* arr = tensor_pd->data<bool>();
        ti = backend_->create_tensor(ngraph::element::boolean, sp,
                                     const_cast<bool*>(arr));
      } else {
        PADDLE_THROW("Data type not handling for var %s", vi);
      }
    } else {
      PADDLE_THROW("Cannot find var or tensor with var name %s", vi);
    }
    bool is_test = (ng_op_state_ == PARTIAL_TEST || ng_op_state_ == FULL_TEST)
                       ? true
                       : false;
    bool is_persistable =
        (persistables_.find(vi) != persistables_.end()) ? true : false;
    if (is_test && is_persistable) {
      ti->set_stale(false);
    }
    t_in.push_back(ti);
  }

  for (size_t i = 0; i < var_out_.size(); ++i) {
    auto var_name = var_out_[i];
    auto* var = scope.FindVar(var_name);
    std::shared_ptr<ngraph::runtime::Tensor> to;
B
baojun-nervana 已提交
528
    if (var && var->IsType<LoDTensor>()) {
B
baojun-nervana 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
      auto* tensor_pd = GetMutableLoDTensorOrSelectedRowsValueFromVar(var);
      auto dd = tensor_pd->dims();
      ngraph::Shape sp = Ddim2Shape(dd);
      auto ng_type = var_type_map_.at(var_name);
      if (ng_type == ngraph::element::f32) {
        auto pd_arr = tensor_pd->mutable_data<float>(place);
        to = backend_->create_tensor(ngraph::element::f32, sp, pd_arr);
      } else if (ng_type == ngraph::element::i64) {
        auto pd_arr = tensor_pd->mutable_data<int64_t>(place);
        to = backend_->create_tensor(ngraph::element::i64, sp, pd_arr);
      } else if (ng_type == ngraph::element::f64) {
        auto pd_arr = tensor_pd->mutable_data<double>(place);
        to = backend_->create_tensor(ngraph::element::f64, sp, pd_arr);
      } else if (ng_type == ngraph::element::boolean) {
        auto pd_arr = tensor_pd->mutable_data<bool>(place);
        to = backend_->create_tensor(ngraph::element::boolean, sp, pd_arr);
      } else {
        PADDLE_THROW("Data type not handled in for var %s", var_name);
      }
      t_out.push_back(to);
    } else {
      PADDLE_THROW("Cannot find var or tensor with var name %s", var_name);
    }
  }

  backend_->call(ngraph_function_, t_out, t_in);
B
baojun-nervana 已提交
555
}  // NgraphEngine::RunImpl
B
baojun-nervana 已提交
556 557
}  // namespace framework
}  // namespace paddle