target.cpp 6.47 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/memory_coloring.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
39
#include <migraphx/normalize_ops.hpp>
40
#include <migraphx/preallocate_param.hpp>
41
42
#include <migraphx/propagate_constant.hpp>
#include <migraphx/register_target.hpp>
43
#include <migraphx/replace_allocate.hpp>
44
#include <migraphx/rewrite_gelu.hpp>
45
#include <migraphx/rewrite_pooling.hpp>
turneram's avatar
turneram committed
46
#include <migraphx/rewrite_quantization.hpp>
47
#include <migraphx/rewrite_rnn.hpp>
Paul's avatar
Paul committed
48
#include <migraphx/schedule.hpp>
49
#include <migraphx/simplify_algebra.hpp>
turneram's avatar
turneram committed
50
#include <migraphx/simplify_qdq.hpp>
51
#include <migraphx/simplify_reshapes.hpp>
52
#include <migraphx/gpu/allocation_model.hpp>
53
#include <migraphx/gpu/compile_ops.hpp>
54
55
#include <migraphx/gpu/concat_gpu_opt.hpp>
#include <migraphx/gpu/context.hpp>
56
#include <migraphx/gpu/device_name.hpp>
Paul Fultz II's avatar
Paul Fultz II committed
57
#include <migraphx/gpu/fuse_mlir.hpp>
58
#include <migraphx/gpu/fuse_ops.hpp>
59
#include <migraphx/gpu/prefuse_ops.hpp>
60
61
62
63
64
65
#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
66

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

71
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_DISABLE_SCHEDULE_PASS)
72
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_DISABLE_POINTWISE_FUSION)
73
74
75
76
77
78
79
80
81
82
83
84
85

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
86

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

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

166
migraphx::context target::get_context() const { return context(gpu::get_device_id()); }
167

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

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

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

174
175
MIGRAPHX_REGISTER_TARGET(target);

Paul's avatar
Paul committed
176
} // namespace gpu
Paul's avatar
Paul committed
177
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
178
} // namespace migraphx