/* * The MIT License (MIT) * * Copyright (c) 2015-2022 Advanced Micro Devices, 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 #include #include #include #include #include #include #include #include namespace migraphx { inline namespace MIGRAPHX_INLINE_NS { namespace onnx { template std::vector to_int64_vector(const std::vector& input_vector) { std::vector output_vector(input_vector.begin(), input_vector.end()); return output_vector; } struct parse_deconvolution : op_parser { std::vector operators() const { return {{"ConvTranspose"}}; } instruction_ref parse(const op_desc& /*opd*/, const onnx_parser& parser, onnx_parser::node_info info, std::vector args) const { operation op = make_op("deconvolution"); value values = op.to_value(); // op::deconvolution op; auto l0 = args[0]; std::vector padding; bool asym_padding = false; auto in_lens = l0->get_shape().lens(); assert(in_lens.size() > 2); auto kdims = in_lens.size() - 2; // ensure pads availabe only when auto_pad is "NOT_SET" check_padding_mode(info, "CONV_TRANSPOSE"); if(contains(info.attributes, "pads")) { copy(info.attributes["pads"].ints(), std::back_inserter(padding)); asym_padding = is_asym_padding(padding); if(not asym_padding) { size_t pad_ndims = padding.size() / 2; check_attr_sizes(kdims, pad_ndims, "PARSE_CONV_TRANSPOSE: inconsistent paddings"); values["padding"].clear(); std::transform(padding.begin(), padding.begin() + pad_ndims, std::back_inserter(values["padding"]), [](auto pad_val) { return pad_val; }); } } if(contains(info.attributes, "strides")) { values["stride"].clear(); copy(info.attributes["strides"].ints(), std::back_inserter(values["stride"])); check_attr_sizes( kdims, values["stride"].size(), "PARSE_CONV_TRANSPOSE: inconsistent strides"); } if(contains(info.attributes, "dilations")) { values["dilation"].clear(); copy(info.attributes["dilations"].ints(), std::back_inserter(values["dilation"])); check_attr_sizes( kdims, values["dilation"].size(), "PARSE_CONV_TRANSPOSE: inconsistent dilations"); } // TODO: auto padding needs to be implemented for this parser and operator if(contains(info.attributes, "auto_pad")) { auto s = info.attributes["auto_pad"].s(); if(contains(info.attributes, "pads") and to_upper(s) != "NOTSET") { MIGRAPHX_THROW("PARSE_CONV_TRANSPOSE: auto_pad and padding cannot be specified " "simultaneously"); } if(s.find("SAME") != std::string::npos) { bool is_same_upper = (s.find("SAME_UPPER") != std::string::npos); values["padding_mode"] = is_same_upper ? to_value(op::padding_mode_t::same_upper) : to_value(op::padding_mode_t::same_lower); } } if(contains(info.attributes, "group")) { values["group"] = parser.parse_value(info.attributes.at("group")).at(); } recalc_conv_attributes(values, kdims); op.from_value(values); auto l1 = info.add_instruction(op, l0, args[1]); std::vector dims = to_int64_vector(l1->get_shape().lens()); std::vector curr_shape(dims.begin() + 2, dims.end()); if(asym_padding) { std::vector axes(kdims); std::iota(axes.begin(), axes.end(), 2); // ignore first 2 dims auto pad_kdim_start = padding.begin() + kdims; std::vector starts(padding.begin(), pad_kdim_start); std::vector ends{}; std::transform(curr_shape.begin(), curr_shape.end(), pad_kdim_start, std::back_inserter(ends), [](auto curr_dim, auto pad_dim) { return curr_dim - pad_dim; }); l1 = info.add_instruction( make_op("slice", {{"axes", axes}, {"starts", starts}, {"ends", ends}}), l1); } if(contains(info.attributes, "output_padding")) { size_t non_kdims = dims.size() * 2 - kdims; std::vector output_padding(non_kdims, 0); copy(info.attributes["output_padding"].ints(), std::back_inserter(output_padding)); check_attr_sizes(kdims, output_padding.size() - non_kdims, "PARSE_CONV_TRANSPOSE: inconsistent output padding"); l1 = info.add_instruction(make_op("pad", {{"pads", output_padding}}), l1); } if(contains(info.attributes, "output_shape")) { std::vector output_shape; copy(info.attributes["output_shape"].ints(), std::back_inserter(output_shape)); check_attr_sizes( kdims, output_shape.size(), "PARSE_CONV_TRANSPOSE: inconsistent output shape"); dims = to_int64_vector(l1->get_shape().lens()); copy(dims.begin() + 2, dims.end(), curr_shape.begin()); if(curr_shape != output_shape) { std::vector target_padding(dims.size() * 2 - kdims, 0); std::transform(output_shape.begin(), output_shape.end(), curr_shape.begin(), std::back_inserter(target_padding), [](auto out_dim, auto curr_dim) { return out_dim - curr_dim; }); l1 = info.add_instruction(make_op("pad", {{"pads", target_padding}}), l1); } } return info.add_bias(args, l1, 1); } }; } // namespace onnx } // namespace MIGRAPHX_INLINE_NS } // namespace migraphx