"vscode:/vscode.git/clone" did not exist on "f65e94fc71093caf4243643022eac30a449c6f7c"
reduce.cpp 6.09 KB
Newer Older
1
2
3
4
#include <migraphx/gpu/compiler.hpp>
#include <migraphx/gpu/context.hpp>
#include <migraphx/gpu/compile_hip_code_object.hpp>
#include <migraphx/gpu/compile_hip.hpp>
Paul Fultz II's avatar
Paul Fultz II committed
5
#include <migraphx/gpu/compile_gen.hpp>
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include <migraphx/cpp_generator.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/reduce_dims.hpp>
#include <migraphx/stringutils.hpp>
#include <migraphx/dead_code_elimination.hpp>
#include <migraphx/eliminate_common_subexpression.hpp>
#include <migraphx/module.hpp>
#include <migraphx/pass_manager.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace gpu {

Paul Fultz II's avatar
Paul Fultz II committed
20
21
using namespace migraphx::gpu::gen; // NOLINT

22
23
24
static const char* const simple_reduce_kernel = R"__migraphx__(
#include <migraphx/kernels/index.hpp>
#include <migraphx/kernels/reduce.hpp>
Paul Fultz II's avatar
Paul Fultz II committed
25
#include <migraphx/kernels/vectorize.hpp>
26
27
28
29
30
31
32
#include <args.hpp>

namespace migraphx {

${preamble}

extern "C" {
Paul Fultz II's avatar
Paul Fultz II committed
33
__global__ void reduce_kernel(void* input_p, void* output_p) 
34
{
Paul Fultz II's avatar
Paul Fultz II committed
35
36
    
    transform_args(make_tensors(), ${transformers})(input_p, output_p)([](auto input, auto output) {
37

Paul Fultz II's avatar
Paul Fultz II committed
38
        simple_reduce<reduce::${algo}>(${reduction}, ${init}, input, output, ${read}, ${write});
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
    });
}
    
}

} // namespace migraphx

)__migraphx__";

static std::size_t get_reduce_elements(const std::vector<shape>& inputs)
{
    return inputs.front().elements() / inputs.back().elements();
}
static std::size_t get_reduce_elements(const std::vector<instruction_ref>& inputs)
{
    return get_reduce_elements(to_shapes(inputs));
}

Paul Fultz II's avatar
Paul Fultz II committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
static std::vector<std::size_t> get_reduce_lens(const std::vector<std::size_t>& input_lens,
                                                const std::vector<std::size_t>& output_lens)
{
    std::vector<std::size_t> reduce_lens;
    std::transform(output_lens.begin(),
                   output_lens.end(),
                   input_lens.begin(),
                   std::back_inserter(reduce_lens),
                   [](auto x, auto y) -> std::size_t {
                       if(x == y)
                           return 1;
                       else
                           return y;
                   });
    return reduce_lens;
}

static std::string get_reduce_algo(const std::vector<shape>& inputs)
{
    auto rlens      = get_reduce_lens(inputs.front().lens(), inputs.back().lens());
    const auto init = std::numeric_limits<std::size_t>::max();
    // The minimum stride
    auto min_stride = std::inner_product(
        rlens.begin(),
        rlens.end(),
        inputs.front().strides().begin(),
        init,
        [](auto x, auto y) { return std::min(x, y); },
        [](auto len, auto stride) { return len == 1 ? init : stride; });
    if(min_stride > 2)
        return "lane";
    return "block";
}

91
92
93
94
95
96
97
98
99
100
struct reduce_compiler : compiler<reduce_compiler>
{
    std::vector<std::string> names() const
    {
        return {"reduce", "reduce_sum", "reduce_mean", "reduce_max", "reduce_min", "reduce_prod"};
    }

    operation compile_op(context& ctx, const std::vector<shape>& inputs, const value& v) const
    {
        hip_compile_options options;
Paul Fultz II's avatar
Paul Fultz II committed
101
102
103
104
105
106
107
108
109
110
111
112
113
        options.inputs         = inputs;
        options.output         = inputs.back();
        options.virtual_inputs = reduce_dims(inputs);
        auto faxis             = find_fast_axis({options.virtual_inputs.front()});
        vectorize vec{};
        // Vectorize if the axis is a reduction axis
        if(options.virtual_inputs.back().lens()[faxis] == 1)
        {
            vec = vectorize::elements(faxis, options.virtual_inputs);
        }
        auto relements = get_reduce_elements(options.virtual_inputs) / vec.size;
        auto nelements = options.virtual_inputs.back().elements();
        auto algo      = v.get("algo", get_reduce_algo(options.virtual_inputs));
Paul Fultz II's avatar
Paul Fultz II committed
114
115
        if(algo == "block")
        {
Paul Fultz II's avatar
Paul Fultz II committed
116
            auto block_size = compute_block_size(relements, 256);
Paul Fultz II's avatar
Paul Fultz II committed
117
            options.set_launch_params(
Paul Fultz II's avatar
Paul Fultz II committed
118
                v, compute_global_for(ctx, nelements * block_size, 256), block_size);
Paul Fultz II's avatar
Paul Fultz II committed
119
120
121
        }
        else if(algo == "lane")
        {
Paul Fultz II's avatar
Paul Fultz II committed
122
            options.set_launch_params(v, compute_global_for(ctx, nelements, 256));
Paul Fultz II's avatar
Paul Fultz II committed
123
124
125
126
127
        }
        else
        {
            MIGRAPHX_THROW("Unknown reduce algo: " + algo);
        }
Paul Fultz II's avatar
Paul Fultz II committed
128
129
130
        options.kernel_name  = "reduce_kernel";
        std::string identity = "[](auto x) { return x; }";
        auto src             = interpolate_string(simple_reduce_kernel,
131
132
133
134
                                      {{"reduction", v.at("reduction").to<std::string>()},
                                       {"init", v.get("init", std::string{"0"})},
                                       {"read", v.get("read", identity)},
                                       {"write", v.get("write", identity)},
Paul Fultz II's avatar
Paul Fultz II committed
135
                                       {"algo", algo},
Paul Fultz II's avatar
Paul Fultz II committed
136
                                       {"transformers", make_transformer_args(vec)},
137
                                       {"preamble", v.get("preamble", std::string{})}});
Paul Fultz II's avatar
Paul Fultz II committed
138
        options.params += "-Wno-float-equal";
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
        return compile_hip_code_object(src, options);
    }

    compiler_replace compile(context& ctx, instruction_ref ins, const operation& op) const
    {
        value v              = value::object{};
        auto reduce_elements = get_reduce_elements(ins->inputs());
        if(op.name() == "reduce_sum")
        {
            v["reduction"] = "op::sum{}";
        }
        else if(op.name() == "reduce_mean")
        {
            v["reduction"] = "op::sum{}";
            v["write"]     = "op::mean{" + std::to_string(reduce_elements) + "}";
        }
        else if(op.name() == "reduce_max")
        {
            v["reduction"] = "op::max{}";
            v["init"]      = "lowest{}";
        }
        else if(op.name() == "reduce_min")
        {
            v["reduction"] = "op::min{}";
            v["init"]      = "highest{}";
        }
        else if(op.name() == "reduce_prod")
        {
            v["reduction"] = "op::product{}";
            v["init"]      = "1";
        }
        else
        {
            MIGRAPHX_THROW("Unsupported reduce");
        }
        return replace(compile_op(ctx, to_shapes(ins->inputs()), v));
    }
};
} // namespace gpu
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx