target.cpp 6.39 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
45
#include <migraphx/rewrite_batchnorm.hpp>
#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
56
#include <migraphx/gpu/concat_gpu_opt.hpp>
#include <migraphx/gpu/context.hpp>
#include <migraphx/gpu/fuse_ops.hpp>
57
#include <migraphx/gpu/prefuse_ops.hpp>
58
#include <migraphx/gpu/lowering.hpp>
59
#include <migraphx/gpu/mlir_conv.hpp>
60
61
62
63
64
#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
65

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

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

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
85

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

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

Paul's avatar
Paul committed
164
migraphx::context target::get_context() const { return context{}; }
165

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

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

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

172
173
MIGRAPHX_REGISTER_TARGET(target);

Paul's avatar
Paul committed
174
} // namespace gpu
Paul's avatar
Paul committed
175
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
176
} // namespace migraphx