/*Copyright (c) 2019 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. */ #pragma once #include #include #include #include #include #include #include "ngraph/ngraph.hpp" #include "paddle/fluid/operators/ngraph/ops/op_bridge.h" #include "paddle/fluid/platform/ngraph_helper.h" namespace paddle { namespace operators { namespace ngraphs { ngraph::Shape calc_output_shape(const ngraph::Shape& input_shape, const std::vector& v_shape) { auto out_shape = v_shape; for (size_t i = 0; i < v_shape.size(); ++i) { if (v_shape[i] == 0) { out_shape[i] = input_shape[i]; } } int size_input = ngraph::shape_size(input_shape); int size_out = 1; for (auto o : out_shape) { if (o > 0) size_out *= o; } for (auto& o : out_shape) { if (o == -1) o = size_input / size_out; } return ngraph::Shape(out_shape.begin(), out_shape.end()); } template static void BuildReshapeNode( const std::shared_ptr& op, std::shared_ptr< std::unordered_map>> ngb_node_map) { std::shared_ptr input = platform::GetInputNode(op, "X", ngb_node_map); auto input_shape = input->get_shape(); // TODO(mozga-intel) The vector of shape is not supported yet, that's // asDispensable() operator" std::shared_ptr shape = platform::GetInputNode(op, "Shape", ngb_node_map); auto op_attrs = framework::AttrReader(op->Attrs()); std::vector v_shape = op_attrs.Get>("shape"); auto out = input; if (shape != nullptr) { ngraph::Shape new_shape; for (auto& it : shape->get_shape()) { new_shape.push_back(it); } out = platform::NgReshaper(input, shape->get_shape()); } else { auto out_shape = calc_output_shape(input_shape, v_shape); out = platform::NgReshaper(input, out_shape); } if (is_v2) { ngraph::Shape input_xshape(input_shape.size() + 1); input_xshape[0] = 0; std::copy(input_shape.begin(), input_shape.end(), input_xshape.begin() + 1); auto xshape_node = std::make_shared( input->get_element_type(), input_xshape, std::vector{}); platform::SetOutputNode(op, "XShape", xshape_node, ngb_node_map); } platform::SetOutputNode(op, "Out", out, ngb_node_map); } template void BuildReshapeGradNode( const std::shared_ptr& op, std::shared_ptr< std::unordered_map>> ngb_node_map) { auto dout = paddle::platform::GetInputNode(op, "Out@GRAD", ngb_node_map); ngraph::Shape out_shape; if (is_v2) { auto& xshape = platform::GetInputNode(op, "XShape", ngb_node_map)->get_shape(); out_shape.resize(xshape.size() - 1); std::copy(xshape.begin() + 1, xshape.end(), out_shape.begin()); } else { auto input = paddle::platform::GetInputNode(op, "X", ngb_node_map); out_shape = input->get_shape(); } auto dx = platform::NgReshaper(dout, out_shape); paddle::platform::SetOutputNode(op, "X@GRAD", dx, ngb_node_map); } } // namespace ngraphs } // namespace operators } // namespace paddle REGISTER_NG_OP(reshape, BuildReshapeNode); REGISTER_NG_OP(reshape2, BuildReshapeNode); REGISTER_NG_OP(reshape_grad, BuildReshapeGradNode); REGISTER_NG_OP(reshape2_grad, BuildReshapeGradNode);