ngraph_helper.h 4.8 KB
Newer Older
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) 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. */

#ifdef PADDLE_WITH_NGRAPH
#pragma once

#include <functional>
#include <string>
#include <vector>
#include "ngraph/ngraph.hpp"

namespace paddle {
namespace platform {

26
ngraph::Shape FlattenTo2d(ngraph::Shape sh, int num) {
27 28 29 30 31 32 33 34 35
  auto x1 = std::accumulate(std::begin(sh), std::begin(sh) + num, 1,
                            std::multiplies<size_t>());
  auto x2 = std::accumulate(std::begin(sh) + num, std::end(sh), 1,
                            std::multiplies<size_t>());
  size_t x1_l = static_cast<size_t>(x1);
  size_t x2_l = static_cast<size_t>(x2);
  return ngraph::Shape{x1_l, x2_l};
}

36 37
std::shared_ptr<ngraph::Node> NgReshaper(std::shared_ptr<ngraph::Node> input,
                                         ngraph::Shape shape) {
38 39 40 41 42 43
  std::vector<size_t> input_order(input->get_shape().size());
  std::iota(std::begin(input_order), std::end(input_order), 0);
  return std::make_shared<ngraph::op::Reshape>(
      input, ngraph::AxisVector(input_order), shape);
}

44
std::shared_ptr<ngraph::Node> GetNode(
45
    const std::shared_ptr<paddle::framework::OperatorBase>& op,
46
    const std::string name, const paddle::framework::VariableNameMap& var_map,
47 48 49
    std::shared_ptr<
        std::unordered_map<std::string, std::shared_ptr<ngraph::Node>>>
        ngb_node_map) {
50
  auto& var_names = var_map.at(name);
51
  PADDLE_ENFORCE_EQ(var_names.size(), 1,
52 53
                    "op %s name %s expects one associated var", op->Type(),
                    name);
54 55 56 57 58 59 60
  if (ngb_node_map->find(var_names[0]) != ngb_node_map->end()) {
    return (*ngb_node_map)[var_names[0]];
  } else {
    return nullptr;
  }
}

61
std::shared_ptr<ngraph::Node> GetInputNode(
62
    const std::shared_ptr<paddle::framework::OperatorBase>& op,
63
    const std::string name,
64 65 66
    std::shared_ptr<
        std::unordered_map<std::string, std::shared_ptr<ngraph::Node>>>
        ngb_node_map) {
67
  return GetNode(op, name, op->Inputs(), ngb_node_map);
68 69
}

70
std::shared_ptr<ngraph::Node> GetOutputNode(
71
    const std::shared_ptr<paddle::framework::OperatorBase>& op,
72
    const std::string name,
73 74 75
    std::shared_ptr<
        std::unordered_map<std::string, std::shared_ptr<ngraph::Node>>>
        ngb_node_map) {
76
  return GetNode(op, name, op->Outputs(), ngb_node_map);
77 78
}

79
void SetOutputNode(
80
    const std::shared_ptr<paddle::framework::OperatorBase>& op,
81
    const std::string name, std::shared_ptr<ngraph::Node> node,
82 83 84
    std::shared_ptr<
        std::unordered_map<std::string, std::shared_ptr<ngraph::Node>>>
        ngb_node_map) {
85
  auto& var_names = op->Outputs().at(name);
86
  if (var_names.size() == 1) {
87 88 89 90 91 92 93 94 95 96
    /*  */
    auto dummy_out = GetOutputNode(op, name, ngb_node_map);
    if (dummy_out && dummy_out->get_shape() != node->get_shape()) {
      node = NgReshaper(node, dummy_out->get_shape());
    }
    if (dummy_out &&
        dummy_out->get_element_type() != node->get_element_type()) {
      node = std::make_shared<ngraph::op::Convert>(
          node, dummy_out->get_element_type());
    }
97 98 99 100
    (*ngb_node_map)[var_names[0]] = node;
  } else if (var_names.size() == 0) {
    (*ngb_node_map)[""] = node;
  } else {
101
    PADDLE_THROW("name %s has more than 1 var_names.", name);
102 103 104
  }
}

105
bool HasOutput(const std::shared_ptr<paddle::framework::OperatorBase>& op,
106
               const std::string name) {
107
  auto& outputs = op->Outputs();
108 109
  if (outputs.find(name) == outputs.end()) return false;
  return outputs.at(name).size() > 0;
110 111
}

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
inline void GetMidDims(const ngraph::Shape& x_shape,
                       const ngraph::Shape& y_shape, int axis, int* pre, int* n,
                       int* post) {
  *pre = 1;
  *n = 1;
  *post = 1;
  for (int i = 0; i < axis; ++i) {
    (*pre) *= x_shape[i];
  }

  for (size_t i = 0; i < y_shape.size(); ++i) {
    PADDLE_ENFORCE_EQ(x_shape[i + axis], y_shape[i],
                      "Broadcast dimension mismatch.");
    (*n) *= y_shape[i];
  }

  for (size_t i = axis + y_shape.size(); i < x_shape.size(); ++i) {
    (*post) *= x_shape[i];
  }
}

inline void TrimTrailingSingularDims(ngraph::Shape* shape) {
  // Remove trailing dimensions of size 1 for y
  auto actual_shape_size = shape->size();
  for (; actual_shape_size != 0; --actual_shape_size) {
    if ((*shape)[actual_shape_size - 1] != 1) {
      break;
    } else {
      shape->pop_back();
    }
  }
}
144 145 146 147
}  // namespace platform
}  // namespace paddle

#endif