target.cpp 6.78 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
#include <migraphx/adjust_allocation.hpp>
Paul's avatar
Paul committed
25
#include <migraphx/auto_contiguous.hpp>
26
#include <migraphx/check_context.hpp>
Paul's avatar
Paul committed
27
#include <migraphx/dead_code_elimination.hpp>
28
#include <migraphx/eliminate_allocation.hpp>
Paul's avatar
Paul committed
29
#include <migraphx/eliminate_common_subexpression.hpp>
Paul's avatar
Paul committed
30
#include <migraphx/eliminate_concat.hpp>
31
#include <migraphx/eliminate_contiguous.hpp>
32
#include <migraphx/eliminate_data_type.hpp>
33
#include <migraphx/eliminate_identity.hpp>
34
#include <migraphx/eliminate_pad.hpp>
35
#include <migraphx/fuse_pointwise.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
36
#include <migraphx/inline_module.hpp>
kahmed10's avatar
kahmed10 committed
37
#include <migraphx/insert_pad.hpp>
38
#include <migraphx/layout_nhwc.hpp>
39
#include <migraphx/memory_coloring.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
40
#include <migraphx/normalize_ops.hpp>
41
#include <migraphx/preallocate_param.hpp>
42
43
#include <migraphx/propagate_constant.hpp>
#include <migraphx/register_target.hpp>
44
#include <migraphx/replace_allocate.hpp>
45
#include <migraphx/rewrite_gelu.hpp>
46
#include <migraphx/rewrite_pooling.hpp>
turneram's avatar
turneram committed
47
#include <migraphx/rewrite_quantization.hpp>
48
#include <migraphx/rewrite_rnn.hpp>
Paul's avatar
Paul committed
49
#include <migraphx/schedule.hpp>
50
#include <migraphx/simplify_algebra.hpp>
turneram's avatar
turneram committed
51
#include <migraphx/simplify_qdq.hpp>
52
#include <migraphx/simplify_reshapes.hpp>
53
#include <migraphx/gpu/allocation_model.hpp>
54
#include <migraphx/gpu/compile_miopen.hpp>
55
#include <migraphx/gpu/compile_ops.hpp>
56
57
#include <migraphx/gpu/concat_gpu_opt.hpp>
#include <migraphx/gpu/context.hpp>
58
#include <migraphx/gpu/device_name.hpp>
Paul Fultz II's avatar
Paul Fultz II committed
59
#include <migraphx/gpu/fuse_mlir.hpp>
60
#include <migraphx/gpu/fuse_ops.hpp>
61
#include <migraphx/gpu/prefuse_ops.hpp>
62
63
64
65
66
67
#include <migraphx/gpu/lowering.hpp>
#include <migraphx/gpu/pack_int8_args.hpp>
#include <migraphx/gpu/schedule_model.hpp>
#include <migraphx/gpu/sync_device.hpp>
#include <migraphx/gpu/target.hpp>
#include <migraphx/gpu/write_literals.hpp>
Paul's avatar
Paul committed
68

Paul's avatar
Paul committed
69
namespace migraphx {
Paul's avatar
Paul committed
70
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
71
namespace gpu {
mei-ye's avatar
mei-ye committed
72

73
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_DISABLE_SCHEDULE_PASS)
74
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_DISABLE_POINTWISE_FUSION)
75
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_ENABLE_NHWC)
76
77
78
79
80
81
82
83
84
85
86
87
88

struct id_pass
{
    std::string name() const { return "id"; }
    void apple(const module&) const {}
};

pass enable_pass(bool enabled, pass p)
{
    if(enabled)
        return p;
    return id_pass{};
}
Paul's avatar
Paul committed
89

90
std::vector<pass> target::get_passes(migraphx::context& gctx, const compile_options& options) const
Paul's avatar
Paul committed
91
{
Paul's avatar
Paul committed
92
    auto& ctx = any_cast<context>(gctx);
93
94
95
96
97
98
    std::set<shape::type_t> unsupported_types(shape::types().begin(), shape::types().end());
    unsupported_types.erase(shape::type_t::float_type);
    unsupported_types.erase(shape::type_t::half_type);
    unsupported_types.erase(shape::type_t::bool_type);
    unsupported_types.erase(shape::type_t::int8_type);
    unsupported_types.erase(shape::type_t::uint8_type);
Shucai Xiao's avatar
Shucai Xiao committed
99
    unsupported_types.erase(shape::type_t::tuple_type);
Paul's avatar
Paul committed
100
101
102
    // clang-format off
    return
    {
Shucai Xiao's avatar
Shucai Xiao committed
103
        normalize_ops{},
104
        dead_code_elimination{},
turneram's avatar
turneram committed
105
        simplify_qdq{},
turneram's avatar
turneram committed
106
107
        rewrite_quantization{},
        dead_code_elimination{},
108
        eliminate_data_type{unsupported_types, shape::type_t::float_type},
109
        simplify_reshapes{},
110
        eliminate_identity{},
111
        eliminate_pad{},
112
        dead_code_elimination{},
kahmed10's avatar
kahmed10 committed
113
114
        insert_pad{},
        dead_code_elimination{},
Shucai Xiao's avatar
Shucai Xiao committed
115
        rewrite_rnn{},
Shucai Xiao's avatar
Shucai Xiao committed
116
        dead_code_elimination{},
Shucai Xiao's avatar
Shucai Xiao committed
117
        inline_module{},
118
        rewrite_pooling{},
Shucai Xiao's avatar
Shucai Xiao committed
119
        dead_code_elimination{},
120
121
        rewrite_gelu{},
        dead_code_elimination{},
Paul's avatar
Paul committed
122
123
        eliminate_common_subexpression{},
        dead_code_elimination{},
Paul's avatar
Paul committed
124
        simplify_algebra{},
125
        simplify_reshapes{},
126
127
128
        enable_pass(enabled(MIGRAPHX_ENABLE_NHWC{}), layout_nhwc{}),
        dead_code_elimination{},
        simplify_reshapes{},
129
        simplify_algebra{},
130
131
        prefuse_ops{},
        dead_code_elimination{},
Paul's avatar
Paul committed
132
        auto_contiguous{},
133
        simplify_reshapes{},
134
135
        propagate_constant{},
        dead_code_elimination{},
136
        enable_pass(not enabled(MIGRAPHX_DISABLE_POINTWISE_FUSION{}), fuse_pointwise{}),
137
        dead_code_elimination{},
Paul Fultz II's avatar
Paul Fultz II committed
138
139
        fuse_mlir{&ctx},
        dead_code_elimination{},
140
        lowering{&ctx, options.offload_copy},
141
        eliminate_contiguous{"gpu::contiguous"},
Paul's avatar
Paul committed
142
        dead_code_elimination{},
143
144
        eliminate_concat{concat_gpu_optimization{}},
        dead_code_elimination{},
145
146
        compile_miopen{&gctx},
        dead_code_elimination{},
147
148
        pack_int8_args{},
        dead_code_elimination{},
kahmed10's avatar
kahmed10 committed
149
        fuse_ops{&ctx, options.fast_math},
Paul's avatar
Paul committed
150
        dead_code_elimination{},
151
152
        replace_allocate{gpu_allocation_model{}, options.offload_copy},
        dead_code_elimination{},
153
154
        adjust_allocation{gpu_allocation_model{}},
        dead_code_elimination{},
155
156
        compile_ops{&ctx},
        dead_code_elimination{},
Paul's avatar
Paul committed
157
        write_literals{&ctx},
158
        schedule{gpu::schedule_model{ctx.get_current_device().nstreams()}, not enabled(MIGRAPHX_DISABLE_SCHEDULE_PASS{})},
Paul's avatar
Paul committed
159
        memory_coloring{"hip::allocate"},
160
        sync_device{},
161
        preallocate_param{"scratch", gpu_allocation_model{}},
Paul's avatar
Paul committed
162
        dead_code_elimination{},
Paul's avatar
Paul committed
163
        eliminate_allocation{"hip::allocate"},
164
        check_context<context>{},
165
        normalize_ops{},
166
167
        dead_code_elimination{},
        eliminate_identity{}
Paul's avatar
Paul committed
168
169
    };
    // clang-format on
Paul's avatar
Paul committed
170
}
Paul's avatar
Paul committed
171

172
std::string target::name() const { return "gpu"; }
Paul's avatar
Paul committed
173

174
migraphx::context target::get_context() const { return context(gpu::get_device_id()); }
175

Shucai Xiao's avatar
Shucai Xiao committed
176
argument target::copy_to(const argument& arg) const { return gpu::to_gpu(arg); }
177

Shucai Xiao's avatar
Shucai Xiao committed
178
argument target::copy_from(const argument& arg) const { return gpu::from_gpu(arg); }
179

Shucai Xiao's avatar
Shucai Xiao committed
180
argument target::allocate(const shape& s) const { return gpu::allocate_gpu(s); }
181

182
183
MIGRAPHX_REGISTER_TARGET(target);

Paul's avatar
Paul committed
184
} // namespace gpu
Paul's avatar
Paul committed
185
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
186
} // namespace migraphx