"test/vscode:/vscode.git/clone" did not exist on "14a2464bd8d53a285efd0003ec3a92c8ab111b66"
pointwise.cpp 6.22 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
#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
28
#include <migraphx/gpu/compile_gen.hpp>
29
30
31
32

#include <migraphx/cpp_generator.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/reduce_dims.hpp>
33
#include <migraphx/permutation.hpp>
34
35
36
37
38
39
40
41
42
43
#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
44
45
using namespace migraphx::gpu::gen; // NOLINT

46
47
48
49
50
51
52
53
54
55
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
56
__global__ void ${kernel}(${params}) 
57
{
58
    auto idx = make_index();
Paul Fultz II's avatar
Paul Fultz II committed
59
    pointwise(idx, ${transformers})(${lambda}, ${args});
60
61
62
63
64
65
66
67
}
    
}

} // namespace migraphx

)__migraphx__";

kahmed10's avatar
kahmed10 committed
68
69
70
71
72
73
74
75
76
77
78
79
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;
}

80
81
struct pointwise_compiler : compiler<pointwise_compiler>
{
82
    std::vector<std::string> names() const { return {"pointwise", "contiguous"}; }
83

84
    static std::size_t oversubscribe_if(bool b)
85
    {
86
        if(b)
87
            return 256;
88
89
90
        else
            return 1;
    }
91
92
93
94
95
96
97
    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";
98
        auto axis              = find_fast_axis(options.virtual_inputs);
Paul Fultz II's avatar
Paul Fultz II committed
99
100
101
102
103
104
105
106
        auto vec               = vectorize::elements(axis, options.virtual_inputs);
        auto preloads          = preload::broadcasts(axis, options.virtual_inputs);
        options.kernel_name    = v.get("kernel", "kernel");
        options.set_launch_params(
            v,
            compute_global_for(ctx,
                               options.output.elements() / vec.size,
                               oversubscribe_if(not preloads.is_preloading())));
107
        auto src = interpolate_string(pointwise_kernel,
kahmed10's avatar
kahmed10 committed
108
109
                                      {{"kernel", options.kernel_name},
                                       {"params", enum_params(inputs.size(), "void * private_p")},
110
111
                                       {"args", enum_params(inputs.size(), "private_p")},
                                       {"lambda", v.at("lambda").to<std::string>()},
Paul Fultz II's avatar
Paul Fultz II committed
112
                                       {"transformers", make_transformer_args(preloads, vec)},
113
114
115
116
                                       {"preamble", v.get("preamble", std::string{})}});
        return compile_hip_code_object(src, options);
    }

117
    compiler_replace compile(context& ctx, instruction_ref ins, const operation& op) const
118
    {
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
        if(op.name() == "contiguous")
        {
            return replace(compile_op(
                ctx,
                to_shapes(ins->inputs()),
                {{"lambda", "[](auto x) { return x; }"}, {"kernel", "contiguous_kernel"}}));
        }
        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
            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 + ")";
            auto op_names      = get_op_names(*pm);
            op_names.push_back("kernel");
            auto op_name_string = join_strings(op_names, "_");
            return replace(compile_op(
                ctx,
                to_shapes(ins->inputs()),
                {{"lambda", lambda}, {"preamble", g.str()}, {"kernel", op_name_string}}));
        }
156
157
158
159
160
    }
};
} // namespace gpu
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx