compile_pointwise.cpp 2.82 KB
Newer Older
1
2
#include <migraphx/gpu/compile_pointwise.hpp>
#include <migraphx/gpu/compile_hip_code_object.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
3
#include <migraphx/gpu/compile_hip.hpp>
4
#include <migraphx/gpu/context.hpp>
5
#include <migraphx/cpp_generator.hpp>
6
7
8
#include <migraphx/ranges.hpp>
#include <migraphx/reduce_dims.hpp>
#include <migraphx/stringutils.hpp>
9
10
11
12
#include <migraphx/dead_code_elimination.hpp>
#include <migraphx/eliminate_common_subexpression.hpp>
#include <migraphx/module.hpp>
#include <migraphx/pass_manager.hpp>
13
14
15
16
17
18
19
20
21
22

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>

23
namespace migraphx {
24

25
26
${preamble}

27
28
29
30
31
32
33
34
extern "C" {
__global__ void kernel(${params}) 
{
    pointwise(${lambda}, ${args});
}
    
}

35
36
} // namespace migraphx

37
38
39
40
int main() {}

)__migraphx__";

41
42
43
44
operation compile_pointwise(context&,
                            const std::vector<shape>& inputs,
                            const std::string& lambda,
                            const std::string& preamble)
45
46
47
48
49
50
{
    hip_compile_options options;
    options.global         = compute_global(inputs.front().elements());
    options.local          = 1024;
    options.inputs         = inputs;
    options.output         = inputs.back();
51
    options.virtual_inputs = reduce_dims(inputs);
52
    options.params         = "-Wno-float-equal";
53
54
55
    auto src               = interpolate_string(pointwise_kernel,
                                  {{"params", enum_params(inputs.size(), "void * private_p")},
                                   {"args", enum_params(inputs.size(), "private_p")},
56
57
                                   {"lambda", lambda},
                                   {"preamble", preamble}});
58
59
60
    return compile_hip_code_object(src, options);
}

61
62
63
64
operation compile_pointwise(context& ctx, const std::vector<shape>& inputs, module m)
{
    run_passes(m, {eliminate_common_subexpression{}, dead_code_elimination{}});
    cpp_generator g;
65
    g.fmap([](const std::string& fname) { return "migraphx::" + fname; });
66
67
68
69
70
71
72
73
74
75
    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})");
    auto name =
        g.create_function(g.generate_module(m).set_attributes({"__device__"}).set_generic_types(m));
    return compile_pointwise((ctx), inputs, "MIGRAPHX_LIFT(" + name + ")", g.str());
76
77
}

78
79
80
} // namespace gpu
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx