argmax.hpp 3.65 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.
 */
24
25
26
27
#ifndef MIGRAPHX_GUARD_OPERATORS_ARGMAX_HPP
#define MIGRAPHX_GUARD_OPERATORS_ARGMAX_HPP

#include <migraphx/check_shapes.hpp>
28
29
#include <migraphx/argument.hpp>
#include <migraphx/par_for.hpp>
30
#include <migraphx/config.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
31
32
#include <migraphx/value.hpp>
#include <migraphx/op/normalize_attribute.hpp>
Charlie Lin's avatar
Charlie Lin committed
33
#include <migraphx/dyn_output.hpp>
34
35
36
37
38
39
40

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace op {

struct argmax
{
41
    int64_t axis = 0;
42
43
44
45

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
46
        return pack(f(self.axis, "axis"));
47
48
    }

Shucai Xiao's avatar
Shucai Xiao committed
49
50
51
52
53
54
55
    value attributes() const
    {
        value normalize;
        normalize["axis"] = value::array{normalize_attribute::include_min};
        return {{"normalize_axes", normalize}};
    }

56
57
    std::string name() const { return "argmax"; }

Shucai Xiao's avatar
Shucai Xiao committed
58
    shape normalize_compute_shape(std::vector<shape> inputs) const
59
    {
Charlie Lin's avatar
Charlie Lin committed
60
61
62
63
64
        check_shapes{inputs, *this, true}.has(1);
        const auto& s0 = inputs[0];
        if(s0.dynamic())
        {
            auto dyn_dims  = s0.dyn_dims();
65
            dyn_dims[axis] = {1, 1};
Charlie Lin's avatar
Charlie Lin committed
66
67
68
69
70
71
72
73
            return {shape::int64_type, dyn_dims};
        }
        else
        {
            auto lens  = s0.lens();
            lens[axis] = 1;
            return {shape::int64_type, lens};
        }
74
    }
75
76

    template <class T>
77
    int64_t calc_argmax(T& input, std::vector<std::size_t>& indices, size_t item_num) const
78
79
80
81
82
    {
        auto max_val      = input(indices.begin(), indices.end());
        int64_t max_index = 0;
        for(std::size_t i = 1; i < item_num; ++i)
        {
83
84
            indices[axis] = i;
            auto cur_val  = input(indices.begin(), indices.end());
Shucai Xiao's avatar
Shucai Xiao committed
85
            if(max_val < cur_val)
86
            {
Shucai Xiao's avatar
Shucai Xiao committed
87
                max_val   = cur_val;
88
89
90
91
92
93
                max_index = i;
            }
        }
        return max_index;
    }

Charlie Lin's avatar
Charlie Lin committed
94
    argument compute(const dyn_output& dyn_out, std::vector<argument> args) const
95
    {
Charlie Lin's avatar
Charlie Lin committed
96
        argument result{dyn_out.computed_shape};
97
        auto batch_item_num = args.front().get_shape().lens()[axis];
98
99
100

        result.visit([&](auto output) {
            args[0].visit([&](auto input) {
Charlie Lin's avatar
Charlie Lin committed
101
102
                par_for(dyn_out.computed_shape.elements(), [&](auto i) {
                    auto data_idx = dyn_out.computed_shape.multi(i);
103
                    output[i]     = this->calc_argmax(input, data_idx, batch_item_num);
104
105
106
107
108
109
                });
            });
        });

        return result;
    }
110
111
112
113
114
115
116
};

} // namespace op
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx

#endif