parse_if.cpp 6.42 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
/*
 * 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.
 */
Shucai Xiao's avatar
Shucai Xiao committed
24
#include <migraphx/instruction_ref.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <migraphx/onnx/op_parser.hpp>
#include <migraphx/onnx/onnx_parser.hpp>
#include <migraphx/onnx/checks.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/make_op.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace onnx {

struct parse_if : op_parser<parse_if>
{
    std::vector<op_desc> operators() const { return {{"If"}}; }

    std::vector<instruction_ref> parse(const op_desc& /*opd*/,
                                       onnx_parser& parser,
                                       const onnx_parser::node_info& info,
                                       std::vector<instruction_ref> args) const
    {
Shucai Xiao's avatar
Shucai Xiao committed
45
46
        const auto& then_graph = info.attributes.at("then_branch").g();
        const auto& else_graph = info.attributes.at("else_branch").g();
Shucai Xiao's avatar
Shucai Xiao committed
47

Shucai Xiao's avatar
Shucai Xiao committed
48
        if(args.front()->get_shape().elements() != 1)
Shucai Xiao's avatar
Shucai Xiao committed
49
        {
50
51
            MIGRAPHX_THROW("PARSE_IF: " + info.name +
                           " condition input can have only one element!");
Shucai Xiao's avatar
Shucai Xiao committed
52
53
        }

Shucai Xiao's avatar
Shucai Xiao committed
54
55
        std::string then_name = info.name + "_if";
        module_ref then_mdl   = parser.prog.create_module(then_name);
Shucai Xiao's avatar
Shucai Xiao committed
56

Shucai Xiao's avatar
Shucai Xiao committed
57
58
        std::string else_name = info.name + "_else";
        module_ref else_mdl   = parser.prog.create_module(else_name);
Shucai Xiao's avatar
Shucai Xiao committed
59

Shucai Xiao's avatar
Shucai Xiao committed
60
61
        // parse the then sub_graph
        parser.parse_graph(then_mdl, then_graph);
Shucai Xiao's avatar
Shucai Xiao committed
62

Shucai Xiao's avatar
Shucai Xiao committed
63
64
        // parse_the else sub_graph
        parser.parse_graph(else_mdl, else_graph);
Shucai Xiao's avatar
Shucai Xiao committed
65

Shucai Xiao's avatar
Shucai Xiao committed
66
67
        auto then_out_shapes = then_mdl->get_output_shapes();
        auto else_out_shapes = else_mdl->get_output_shapes();
68
69
70
71

        assert(then_out_shapes.size() == else_out_shapes.size());

        // Must have the same type for both if/else blocks by onnx spec
72
        // Add exception for empty constant scalars
73
        if(then_out_shapes.at(0).type() != else_out_shapes.at(0).type())
Shucai Xiao's avatar
Shucai Xiao committed
74
        {
75
            MIGRAPHX_THROW("PARSE_IF: " + info.name +
76
                           " then and else sub_grahps must have same output type! " +
77
78
                           then_out_shapes.at(0).type_string() + " vs " +
                           else_out_shapes.at(0).type_string());
79
80
        }

81
        if(not then_out_shapes.at(0).dynamic() && not else_out_shapes.at(0).dynamic())
82
        {
83
            // allocate buffer
84
85
            if(then_out_shapes.at(0).scalar() && not else_out_shapes.at(0).scalar())
            {
86
87
88
                auto ins = std::prev(then_mdl->end());
                auto l   = migraphx::literal{else_out_shapes.at(0), else_out_shapes.at(0).lens()};
                auto new_lit = then_mdl->insert_literal(ins, l);
89

90
                then_mdl->replace_return({new_lit});
91
92
93
            }
            else if(not then_out_shapes.at(0).scalar() && else_out_shapes.at(0).scalar())
            {
94
95
96
97
                auto ins = std::prev(else_mdl->end());
                auto l =
                    migraphx::literal{else_out_shapes.at(0).type(), then_out_shapes.at(0).lens()};
                auto new_lit = else_mdl->insert_literal(ins, l);
98

99
                else_mdl->replace_return({new_lit});
100
            }
101
            else
102
            {
103
104
105
106
107
108
109
                // First dimension must agree
                if(then_out_shapes.at(0).lens().at(0) != else_out_shapes.at(0).lens().at(0))
                {
                    MIGRAPHX_THROW("PARSE_IF: " + then_out_shapes.at(0).type_string() + " & " +
                                   else_out_shapes.at(0).type_string() +
                                   " are incompatible output shapes for then/cases");
                }
110
            }
111
112
113
            auto then_out_strides = then_out_shapes.at(0).strides();
            auto else_out_strides = else_out_shapes.at(0).strides();

114
            // Generate compatible output types based on largest dimension with rank 1 tensor
115
116
            if(then_out_strides.size() > else_out_strides.size())
            {
117
                auto reshape_ins = else_mdl->insert_instruction(
118
                    std::prev(else_mdl->end()),
119
120
                    migraphx::make_op("reshape",
                                      {{"dims", {else_out_shapes.at(0).lens().at(0), 1}}}),
121
                    std::prev(else_mdl->end())->inputs().front());
122
                else_mdl->replace_return({reshape_ins});
123
124
            }
            else if(then_out_strides.size() < else_out_strides.size())
125
            {
126
                auto reshape_ins = then_mdl->insert_instruction(
127
                    std::prev(then_mdl->end()),
128
129
                    migraphx::make_op("reshape",
                                      {{"dims", {then_out_shapes.at(0).lens().at(0), 1}}}),
130
                    std::prev(then_mdl->end())->inputs().front());
131
                then_mdl->replace_return({reshape_ins});
132
            }
Shucai Xiao's avatar
Shucai Xiao committed
133
        }
Shucai Xiao's avatar
Shucai Xiao committed
134

Shucai Xiao's avatar
Shucai Xiao committed
135
136
137
        auto if_ret = info.add_instruction(make_op("if"), args, {then_mdl, else_mdl});
        auto out_s  = if_ret->get_shape();
        assert(out_s.type() == shape::tuple_type);
Shucai Xiao's avatar
Shucai Xiao committed
138

Shucai Xiao's avatar
Shucai Xiao committed
139
140
141
142
143
144
        const auto& vec_shapes = out_s.sub_shapes();
        std::vector<instruction_ref> out_inss;
        for(std::size_t i = 0; i < vec_shapes.size(); ++i)
        {
            auto ret = info.add_instruction(make_op("get_tuple_elem", {{"index", i}}), if_ret);
            out_inss.push_back(ret);
Shucai Xiao's avatar
Shucai Xiao committed
145
        }
Shucai Xiao's avatar
Shucai Xiao committed
146
147

        return out_inss;
Shucai Xiao's avatar
Shucai Xiao committed
148
149
150
151
152
153
    }
};

} // namespace onnx
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx