pointwise.cpp 4.95 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's avatar
Paul committed
5
#include <migraphx/gpu/compile_gen.hpp>
6
7
8
9

#include <migraphx/cpp_generator.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/reduce_dims.hpp>
10
#include <migraphx/permutation.hpp>
11
12
13
14
15
16
17
18
19
20
#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's avatar
Paul committed
21
using namespace migraphx::gpu::gen; // NOLINT
Paul's avatar
Paul committed
22

23
24
25
26
27
28
29
30
31
32
33
34
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" {
__global__ void kernel(${params}) 
{
35
    auto idx = make_index();
Paul's avatar
Paul committed
36
    pointwise(idx, ${transformers})(${lambda}, ${args});
37
38
39
40
41
42
43
44
45
46
}
    
}

} // namespace migraphx

)__migraphx__";

struct pointwise_compiler : compiler<pointwise_compiler>
{
Paul's avatar
Paul committed
47
    std::vector<std::string> names() const { return {"pointwise", "contiguous"}; }
48

49
    static std::size_t oversubscribe_if(bool b)
50
    {
51
        if(b)
52
            return 256;
53
54
55
        else
            return 1;
    }
Paul's avatar
Paul committed
56
57
58
59
60
61
62
63
64
65
66
67
68
    static std::size_t compute_local(gen::vectorize v, const std::vector<shape>& inputs)
    {
        const std::size_t max_local = 1024;
        if (std::none_of(inputs.begin(), inputs.end(), [&](auto s) {
            return s.transposed();
        }))
            return max_local;
        if (std::any_of(inputs.begin(), inputs.end(), [&](auto s) {
            return s.broadcasted() or s.strides()[v.axis] != 1;
        }))
            return max_local;
        return inputs.front().lens()[v.axis] / v.size;
    }
69
70
71
72
73
74
75
    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";
76
        auto axis              = find_fast_axis(options.virtual_inputs);
Paul's avatar
Format  
Paul committed
77
        auto vec               = vectorize::elements(axis, options.virtual_inputs);
Paul's avatar
Paul committed
78
        auto preloads          = preload::broadcasts(axis, options.virtual_inputs);
Paul's avatar
Format  
Paul committed
79
80
81
82
        options.set_launch_params(
            v,
            compute_global_for(ctx,
                               options.output.elements() / vec.size,
Paul's avatar
Paul committed
83
                               oversubscribe_if(not preloads.is_preloading())), compute_local(vec, options.virtual_inputs));
84
        auto src = interpolate_string(pointwise_kernel,
85
86
87
                                      {{"params", enum_params(inputs.size(), "void * private_p")},
                                       {"args", enum_params(inputs.size(), "private_p")},
                                       {"lambda", v.at("lambda").to<std::string>()},
Paul's avatar
Paul committed
88
                                       {"transformers", make_transformer_args(preloads, vec)},
89
90
91
92
                                       {"preamble", v.get("preamble", std::string{})}});
        return compile_hip_code_object(src, options);
    }

Paul's avatar
Paul committed
93
    compiler_replace compile(context& ctx, instruction_ref ins, const operation& op) const
94
    {
Paul's avatar
Format  
Paul committed
95
        if(op.name() == "contiguous")
Paul's avatar
Paul committed
96
        {
Paul's avatar
Format  
Paul committed
97
98
            return replace(compile_op(
                ctx, to_shapes(ins->inputs()), {{"lambda", "[](auto x) { return x; }"}}));
Paul's avatar
Paul committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
        }
        else
        {
            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
Paul's avatar
Format  
Paul committed
116
117
118
            g.fresult([](const shape& s) {
                return "migraphx::convert<" + shape::cpp_type(s.type()) + ">";
            });
Paul's avatar
Paul committed
119
120
121
            auto name = g.create_function(
                g.generate_module(*pm).set_attributes({"__device__"}).set_generic_types(*pm));
            std::string lambda = "MIGRAPHX_LIFT(" + name + ")";
Paul's avatar
Format  
Paul committed
122
123
            return replace(compile_op(
                ctx, to_shapes(ins->inputs()), {{"lambda", lambda}, {"preamble", g.str()}}));
Paul's avatar
Paul committed
124
        }
125
126
127
128
129
    }
};
} // namespace gpu
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx