pointwise.cpp 7.96 KB
Newer Older
1
2
3
4
5
6
7
8
#include <migraphx/gpu/compiler.hpp>
#include <migraphx/gpu/context.hpp>
#include <migraphx/gpu/compile_hip_code_object.hpp>
#include <migraphx/gpu/compile_hip.hpp>

#include <migraphx/cpp_generator.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/reduce_dims.hpp>
9
#include <migraphx/permutation.hpp>
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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 {

static const char* const pointwise_kernel = R"__migraphx__(
#include <migraphx/kernels/index.hpp>
#include <migraphx/kernels/pointwise.hpp>
#include <args.hpp>

namespace migraphx {

${preamble}

extern "C" {
kahmed10's avatar
kahmed10 committed
30
__global__ void ${kernel}(${params}) 
31
{
32
33
    auto idx = make_index();
    pointwise(idx, auto_preload<${preloads}>(idx), vectorize<${vec_size}, ${axis}>())(${lambda}, ${args});
34
35
36
37
38
39
40
41
}
    
}

} // namespace migraphx

)__migraphx__";

kahmed10's avatar
kahmed10 committed
42
43
44
45
46
47
48
49
50
51
52
53
static std::vector<std::string> get_op_names(const module& m)
{
    std::vector<std::string> result;
    for(auto& ins : m)
    {
        if(starts_with(ins.name(), "@"))
            continue;
        result.push_back(ins.name());
    }
    return result;
}

54
55
56
57
struct pointwise_compiler : compiler<pointwise_compiler>
{
    std::vector<std::string> names() const { return {"pointwise"}; }

58
    static std::size_t oversubscribe_if(bool b)
59
    {
60
        if(b)
61
            return 256;
62
63
64
65
66
67
68
69
        else
            return 1;
    }
    static std::size_t find_fast_axis(const std::vector<shape>& inputs)
    {
        auto permutation = find_permutation(inputs);
        auto it          = std::max_element(permutation.begin(), permutation.end());
        return it - permutation.begin();
70
    }
71
    static std::vector<bool> preload(std::size_t axis, const std::vector<shape>& inputs)
72
    {
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
        const std::size_t max_lds_bytes = 4096;
        std::vector<bool> result;
        std::transform(inputs.begin(),
                       inputs.end(),
                       std::back_inserter(result),
                       [&](const shape& input) { return input.strides()[axis] == 0; });
        auto bytes = std::inner_product(inputs.begin(),
                                        inputs.end(),
                                        result.begin(),
                                        std::size_t{0},
                                        std::plus<>{},
                                        [](const shape& s, bool b) -> std::size_t {
                                            if(b)
                                                return s.bytes();
                                            return 0;
                                        });
        if(bytes < max_lds_bytes)
            return result;
        // TODO: Try to partially preload items
        std::fill(result.begin(), result.end(), false);
        return result;
    }
    static std::string preload_str(const std::vector<bool>& bs)
    {
        std::vector<std::string> bool_strs;
        std::transform(bs.begin(), std::prev(bs.end()), std::back_inserter(bool_strs), [](bool b) {
            if(b)
                return "true";
            return "false";
        });
        return "false, " + join_strings(bool_strs, ", ");
    }
    static std::vector<std::size_t> vector_sizes(const std::vector<shape>& inputs)
    {
        // If all inputs is half then only use half2
108
        if(std::all_of(inputs.begin(), inputs.end(), [](const auto& s) {
109
               return s.type() == shape::half_type;
110
           }))
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
            return {2};
        return {4, 2};
    }
    static auto vectorize_elements(std::size_t axis, const std::vector<shape>& inputs)
    {
        auto sizes = vector_sizes(inputs);
        std::vector<std::size_t> max_vec_size;
        std::transform(inputs.begin(),
                       inputs.end(),
                       std::back_inserter(max_vec_size),
                       [&](const auto& input) -> std::size_t {
                           auto stride = input.strides()[axis];
                           auto len    = input.lens()[axis];
                           if(stride != 0 and stride != 1)
                               return 1;
                           auto it = std::find_if(
                               sizes.begin(), sizes.end(), [&](auto i) { return (len % i) == 0; });
                           if(it != sizes.end())
                               return *it;
                           return 1;
                       });
        return *std::min_element(max_vec_size.begin(), max_vec_size.end());
133
134
135
136
137
138
139
140
    }
    operation compile_op(context& ctx, const std::vector<shape>& inputs, const value& v) const
    {
        hip_compile_options options;
        options.inputs         = inputs;
        options.output         = inputs.back();
        options.virtual_inputs = reduce_dims(inputs);
        options.params         = "-Wno-float-equal";
141
142
143
144
145
        auto axis              = find_fast_axis(options.virtual_inputs);
        auto vec_size          = vectorize_elements(axis, options.virtual_inputs);
        auto preloads          = preload(axis, options.virtual_inputs);
        auto is_preloading =
            std::accumulate(preloads.begin(), preloads.end(), false, std::logical_or<>{});
kahmed10's avatar
kahmed10 committed
146
        options.kernel_name = v.get("kernel", "kernel");
147
148
149
150
151
        options.set_launch_params(v,
                                  compute_global_for(ctx,
                                                     options.output.elements() / vec_size,
                                                     oversubscribe_if(not is_preloading)));
        auto src = interpolate_string(pointwise_kernel,
kahmed10's avatar
kahmed10 committed
152
153
                                      {{"kernel", options.kernel_name},
                                       {"params", enum_params(inputs.size(), "void * private_p")},
154
155
                                       {"args", enum_params(inputs.size(), "private_p")},
                                       {"lambda", v.at("lambda").to<std::string>()},
156
157
158
                                       {"vec_size", std::to_string(vec_size)},
                                       {"axis", std::to_string(axis)},
                                       {"preloads", preload_str(preloads)},
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
                                       {"preamble", v.get("preamble", std::string{})}});
        return compile_hip_code_object(src, options);
    }

    compiler_replace compile(context& ctx, instruction_ref ins, const operation&) const
    {
        assert(not ins->module_inputs().empty());
        auto* pm = ins->module_inputs().front();
        run_passes(*pm, {eliminate_common_subexpression{}, dead_code_elimination{}});
        cpp_generator g;
        g.fmap([](const std::string& fname) { return "migraphx::" + fname; });
        g.add_point_op("where", "${function:where}(${0}, ${1}, ${2})");
        g.add_point_op("prelu", "${function:where}(${0} < 0, ${0} * ${1}, ${0})");
        g.add_point_op("sign",
                       "${function:where}(${0} > 0, 1, ${function:where}(${0} < 0, -1, 0))");
        g.add_point_op("equal", "migraphx::abs(${0} == ${1})");
        g.add_point_op("less", "migraphx::abs(${0} < ${1})");
        g.add_point_op("greater", "migraphx::abs(${0} > ${1})");
        g.add_point_op("not", "migraphx::abs(not ${0})");
        // Add explict conversions
        g.fresult(
            [](const shape& s) { return "migraphx::convert<" + shape::cpp_type(s.type()) + ">"; });
        auto name = g.create_function(
            g.generate_module(*pm).set_attributes({"__device__"}).set_generic_types(*pm));
        std::string lambda = "MIGRAPHX_LIFT(" + name + ")";
kahmed10's avatar
kahmed10 committed
184
185
186
        auto op_names      = get_op_names(*pm);
        op_names.push_back("kernel");
        auto op_name_string = join_strings(op_names, "_");
187
        return replace(
kahmed10's avatar
kahmed10 committed
188
189
190
            compile_op(ctx,
                       to_shapes(ins->inputs()),
                       {{"lambda", lambda}, {"preamble", g.str()}, {"kernel", op_name_string}}));
191
192
193
194
195
    }
};
} // namespace gpu
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx