parse_instancenorm.cpp 5.78 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.
 */
Paul Fultz II's avatar
Paul Fultz II committed
24
25
26
27
28
29
30
31
32
33
34
#include <migraphx/onnx/op_parser.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_instancenorm : op_parser<parse_instancenorm>
{
35
    std::set<shape::type_t> valid_types = {shape::float_type, shape::half_type, shape::double_type};
36

Paul Fultz II's avatar
Paul Fultz II committed
37
38
    std::vector<op_desc> operators() const { return {{"InstanceNormalization"}}; }

39
    instruction_ref parse(const op_desc& opd,
Paul Fultz II's avatar
Paul Fultz II committed
40
41
                          const onnx_parser& parser,
                          onnx_parser::node_info info,
42
                          std::vector<instruction_ref> oargs) const
Paul Fultz II's avatar
Paul Fultz II committed
43
44
45
46
    {
        // y = scale * ( x - mean ) / sqrt ( variance + epsilon ) + bias
        // mean = reduce_mean({D1, D2, ... Dk}, x)
        // variance = reduce_mean({D1, D2, ... Dk}, (x - mean)^2)
47
        bool convert_fp16 = false;
48
        float epsilon     = 1e-5f;
Paul Fultz II's avatar
Paul Fultz II committed
49
50
51
52
        if(contains(info.attributes, "epsilon"))
        {
            epsilon = parser.parse_value(info.attributes.at("epsilon")).at<float>();
        }
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
        auto dtype         = oargs[0]->get_shape().type();
        auto literal_dtype = dtype;
        std::vector<instruction_ref> args;
        if(dtype == shape::half_type and convert_fp16)
        {
            args.push_back(info.add_instruction(
                make_op("convert", {{"target_type", shape::float_type}}), oargs[0]));
            args.push_back(info.add_instruction(
                make_op("convert", {{"target_type", shape::float_type}}), oargs[1]));
            args.push_back(info.add_instruction(
                make_op("convert", {{"target_type", shape::float_type}}), oargs[2]));
            literal_dtype = shape::float_type;
        }
        else
        {
            args = oargs;
        }

Paul Fultz II's avatar
Paul Fultz II committed
71
72
73
74
        auto x     = args[0];
        auto scale = args[1];
        auto bias  = args[2];
        auto dims  = x->get_shape().lens();
75
76
77
78
        if(not contains(valid_types, dtype))
            MIGRAPHX_THROW(opd.op_name + ": invalid output type: " + std::to_string(dtype) +
                           ". Valid types are 1 (float), 10 (half), and 11 (double).");

Paul Fultz II's avatar
Paul Fultz II committed
79
80
81
82
83
84
85
86
        auto ndims = dims.size();
        assert(ndims >= 2);
        auto kdims = ndims - 2;

        std::vector<int64_t> axes(kdims);
        std::iota(axes.begin(), axes.end(), 2);
        auto mean = info.add_instruction(make_op("reduce_mean", {{"axes", axes}}), x);
        auto mean_bcast =
87
            info.add_instruction(make_op("multibroadcast", {{"out_lens", dims}}), mean);
88
89
90
91
92
93
94
95
96
97
98
99
100
        auto l1                    = info.add_instruction(make_op("sub"), x, mean_bcast);
        std::string reduce_op_name = (dtype == shape::half_type) ? "reduce_sum" : "reduce_mean";
        if(dtype == shape::half_type)
        {
            double n =
                std::accumulate(dims.begin() + 2, dims.end(), 1, [&](const auto& i, const auto& j) {
                    return i * j;
                });
            n              = 1.0 / std::sqrt(n);
            auto n_literal = info.add_literal(literal{dtype, {n}});
            mean_bcast     = info.add_common_op("mul", {mean_bcast, n_literal});
            x              = info.add_common_op("mul", {x, n_literal});
        }
Paul Fultz II's avatar
Paul Fultz II committed
101
        auto l0              = info.add_instruction(make_op("sqdiff"), x, mean_bcast);
102
        auto variance        = info.add_instruction(make_op(reduce_op_name, {{"axes", axes}}), l0);
103
        auto epsilon_literal = info.add_literal(literal{shape{literal_dtype}, {epsilon}});
104
105
        auto epsilon_bcast =
            info.add_instruction(make_op("multibroadcast", {{"out_lens", dims}}), epsilon_literal);
Paul Fultz II's avatar
Paul Fultz II committed
106
        auto variance_bcast =
107
            info.add_instruction(make_op("multibroadcast", {{"out_lens", dims}}), variance);
Paul Fultz II's avatar
Paul Fultz II committed
108
109
110
111
        auto l2 = info.add_instruction(make_op("add"), variance_bcast, epsilon_bcast);
        auto l3 = info.add_instruction(make_op("rsqrt"), l2);
        auto l4 = info.add_instruction(make_op("mul"), l1, l3);
        auto scale_bcast =
112
            info.add_instruction(make_op("broadcast", {{"axis", 1}, {"out_lens", dims}}), scale);
Paul Fultz II's avatar
Paul Fultz II committed
113
        auto bias_bcast =
114
            info.add_instruction(make_op("broadcast", {{"axis", 1}, {"out_lens", dims}}), bias);
115
116
117
118
119
120
121
122
        auto l5  = info.add_instruction(make_op("mul"), l4, scale_bcast);
        auto ret = info.add_instruction(make_op("add"), l5, bias_bcast);
        if(dtype == shape::half_type and convert_fp16)
        {
            return info.add_instruction(make_op("convert", {{"target_type", shape::half_type}}),
                                        ret);
        }
        return ret;
Paul Fultz II's avatar
Paul Fultz II committed
123
124
125
126
127
128
    }
};

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