-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathmatmul.cpp
406 lines (349 loc) · 16.3 KB
/
matmul.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*******************************************************************************
* Copyright 2024-2025 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
#include "graph/backend/dnnl/kernels/matmul.hpp"
#include "graph/backend/dnnl/passes/compile_ops.hpp"
#include "graph/backend/dnnl/passes/constant_propagation.hpp"
#include "graph/backend/dnnl/passes/insert_ops.hpp"
#include "graph/backend/dnnl/passes/layout_propagation.hpp"
#include "graph/backend/dnnl/passes/lower.hpp"
#include "graph/backend/dnnl/passes/memory_planning.hpp"
#include "graph/backend/dnnl/passes/transform.hpp"
#include "graph/backend/dnnl/passes/utils.hpp"
#include "graph/backend/dnnl/op_executable.hpp"
namespace dnnl {
namespace impl {
namespace graph {
namespace dnnl_impl {
template <bool quantized>
status_t matmul_t<quantized>::compile_impl(const dnnl_partition_impl_t *part,
const engine_t *g_engine, const std::vector<logical_tensor_t> &inputs,
const std::vector<logical_tensor_t> &outputs) {
p_engine_ = make_dnnl_engine(*g_engine);
g_alloc_
= reinterpret_cast<graph::allocator_t *>(g_engine->get_allocator());
subgraph_ = std::make_shared<subgraph_t>(
part->get_ops(), p_engine_, part->get_fpmath_mode(), true, true);
BACKEND_DNNL_CHECK(set_given_inputs_outputs(subgraph_, inputs, outputs));
subgraph_visualizer_t vis(part->id(), [this](const value_t *val) {
return this->memory_planner_.get_memory_info(val);
});
pass_pipeline_t pipeline(vis);
BACKEND_DNNL_ADD_PASS(pipeline, lower_down);
// Decompose select to binary ops if necessary
BACKEND_DNNL_ADD_PASS(pipeline, decompose_select_to_binary_ops);
BACKEND_DNNL_ADD_PASS(pipeline, fuse_bias_add);
// check if bias exists
BACKEND_DNNL_ADD_PASS(pipeline, check_with_bias);
if (quantized) {
BACKEND_DNNL_ADD_PASS(pipeline, lift_up_typecast);
BACKEND_DNNL_ADD_PASS(pipeline, lift_up_quantize);
BACKEND_DNNL_ADD_PASS(pipeline, fuse_typecast_to_matmul_or_conv);
BACKEND_DNNL_ADD_PASS(pipeline, fuse_typecast_to_add);
BACKEND_DNNL_ADD_PASS(pipeline, fuse_post_typecast_to_predecessor);
BACKEND_DNNL_ADD_PASS(pipeline, fuse_typecast_to_mul_scales);
BACKEND_DNNL_ADD_PASS(
pipeline, insert_permute_for_dynamic_mul_scale_sub_zp);
BACKEND_DNNL_ADD_PASS(pipeline, convert_bias_to_f32);
}
BACKEND_DNNL_ADD_PASS(pipeline, fuse_mul_sigmoid_to_swish);
if (quantized) {
BACKEND_DNNL_ADD_PASS(pipeline, remove_quant_data_with_no_effect);
BACKEND_DNNL_ADD_PASS(pipeline, convert_to_runtime_src_scales);
BACKEND_DNNL_ADD_PASS(pipeline, fuse_src_scales);
BACKEND_DNNL_ADD_PASS(pipeline, convert_to_runtime_src_zero_points);
BACKEND_DNNL_ADD_PASS(pipeline, fuse_src_zero_points);
// tricky here.
BACKEND_DNNL_ADD_PASS(pipeline, insert_runtime_u8_to_s8_for_matmul);
}
BACKEND_DNNL_ADD_PASS(pipeline, binary_canonicalization);
BACKEND_DNNL_ADD_PASS(pipeline, binary_broadcast_swap);
BACKEND_DNNL_ADD_PASS(pipeline, fuse_post_ops);
if (quantized) {
BACKEND_DNNL_ADD_PASS(pipeline, convert_to_runtime_dst_scales);
BACKEND_DNNL_ADD_PASS(pipeline, fuse_dst_scales);
BACKEND_DNNL_ADD_PASS(pipeline, convert_to_runtime_dst_zero_points);
BACKEND_DNNL_ADD_PASS(pipeline, fuse_dst_zero_points);
BACKEND_DNNL_ADD_PASS(pipeline, convert_runtime_mul_scales);
BACKEND_DNNL_ADD_PASS(pipeline, convert_runtime_zero_points);
// fuse neighboring mul_scales and zdd_zps op to quantize/dequantize
BACKEND_DNNL_ADD_PASS(pipeline, fuse_dynamic_mul_scales_add_zps);
BACKEND_DNNL_ADD_PASS(pipeline, fuse_dynamic_sub_zps_mul_scales);
BACKEND_DNNL_ADD_PASS(pipeline, convert_dynamic_quantize_ops);
}
BACKEND_DNNL_ADD_PASS(pipeline, insert_u8_to_s8_for_matmul);
BACKEND_DNNL_ADD_PASS(pipeline, insert_permute_for_matmul);
BACKEND_DNNL_ADD_PASS(pipeline, insert_reshape_for_ndx2d_matmul);
BACKEND_DNNL_ADD_PASS(pipeline, insert_unsqueeze_and_squeeze_for_matmul);
pipeline.reset_visualize_arg(true, false);
// do constant propagation here so that we can
// prepare constant info for other optimizations.
if (enabled_constant_cache()) {
BACKEND_DNNL_ADD_PASS(pipeline, constant_propagation);
}
BACKEND_DNNL_ADD_PASS(pipeline, infer_shape);
BACKEND_DNNL_ADD_PASS(pipeline, fuse_dst_transpose_to_predecessor);
BACKEND_DNNL_ADD_PASS(pipeline, layout_propagation);
BACKEND_DNNL_ADD_PASS(pipeline, fuse_adjacent_reorders);
// do constant propagation again since layout propagation may
// insert/delete operators
if (enabled_constant_cache()) {
BACKEND_DNNL_ADD_PASS(pipeline, constant_propagation);
}
// bind the memory for each op
auto memory_plan = [&](std::shared_ptr<subgraph_t> &sg) {
return memory_planner_.run(sg);
};
pipeline.reset_visualize_arg(true, true);
BACKEND_DNNL_ADD_PASS(pipeline, memory_plan);
BACKEND_DNNL_ADD_PASS(pipeline, compile_ops);
// Run the added passes
BACKEND_DNNL_CHECK(pipeline.run(subgraph_));
// fill information for inputs logical tensors
for (size_t i = 0; i < inputs.size(); i++) {
auto &in = const_cast<logical_tensor_t &>(inputs[i]);
in = subgraph_->ins_[i];
}
// fill information for outputs logical tensors
for (size_t i = 0; i < outputs.size(); i++) {
auto &out = const_cast<logical_tensor_t &>(outputs[i]);
out = subgraph_->outs_[i];
}
resource_ctor_ = [this]() {
return this->memory_planner_.get_exec_args_set().clone();
};
const_md_hash_ = generate_constant_md_hash(part->id(),
memory_planner_.get_exec_args_set().get_persistent_mem_desc_list());
return status::success;
}
template <bool quantized>
void matmul_t<quantized>::prepare_args_set(const execution_args_set_t *res,
const std::vector<tensor_t> &inputs,
const std::vector<tensor_t> &outputs, const scratchpad_t &scratchpad) {
// update the data of partition in/outputs args
for (const auto &mem_idx : res->get_mems_use_external_inputs()) {
mem_idx.first.set_data_handle(inputs[mem_idx.second].get_data_handle());
}
for (const auto &mem_idx : res->get_mems_use_external_outputs()) {
mem_idx.first.set_data_handle(
outputs[mem_idx.second].get_data_handle());
}
grantor_t var_grantor = memory_planner_.internal_temporary_grantor(
scratchpad.get_buffer());
for (auto &mem_offkey : res->get_mems_use_internal_temporary()) {
mem_offkey.first.set_data_handle(var_grantor.get(mem_offkey.second));
}
}
template <bool quantized>
status_t matmul_t<quantized>::execute_impl(const stream_t *g_stream,
const std::vector<tensor_t> &inputs,
const std::vector<tensor_t> &outputs) {
dnnl::stream p_stream = make_dnnl_stream(p_engine_, *g_stream);
// each thread's own local resource
thread_local_cache_t<execution_args_set_t> res_cache;
execution_args_set_t *res = res_cache.get_or_add(
reinterpret_cast<size_t>(this), resource_ctor_);
temporary_scratchpad_t scratchpad(
memory_planner_.total_internal_temporary_size(), p_engine_,
*g_alloc_);
assertm(scratchpad.size()
>= memory_planner_.total_internal_temporary_size(),
"no enough scratchpad memory");
prepare_args_set(res, inputs, outputs, scratchpad);
constant_cache_t::cached_t c_buffer;
if (enabled_constant_cache()) {
const size_t encoded_key
= encode_constant_cache_key(inputs, const_md_hash_);
std::promise<constant_cache_t::cached_t> c_promise;
constant_cache_t::value_t cached_value
= dnnl_constant_cache_get_or_add(p_engine_, encoded_key,
memory_planner_.total_internal_persistent_size(),
c_promise.get_future());
bool is_from_cache = cached_value.valid();
if (is_from_cache) {
c_buffer = cached_value.get();
grantor_t c_grantor = memory_planner_.internal_persistent_grantor(
c_buffer->data<char>());
for (auto &mem_offkey : res->get_mems_use_internal_persistent()) {
mem_offkey.first.set_data_handle(
c_grantor.get(mem_offkey.second));
}
} else {
c_buffer = std::make_shared<dnnl_constant_buffer_t>(
memory_planner_.total_internal_persistent_size(), p_engine_,
g_alloc_);
grantor_t c_grantor = memory_planner_.internal_persistent_grantor(
c_buffer->data<char>());
for (auto &mem_offkey : res->get_mems_use_internal_persistent()) {
mem_offkey.first.set_data_handle(
c_grantor.get(mem_offkey.second));
}
for (size_t i = 0; i < subgraph_->execs_.size(); i++) {
if (!subgraph_->is_constant_[i]) continue;
subgraph_->execs_[i]->execute(
p_stream, res->get_exec_args()[i]);
}
c_promise.set_value(c_buffer);
}
}
for (size_t i = 0; i < subgraph_->execs_.size(); i++) {
if (subgraph_->is_constant_[i]) continue;
subgraph_->execs_[i]->execute(p_stream, res->get_exec_args()[i]);
}
return status::success;
}
#ifdef DNNL_WITH_SYCL
template <bool quantized>
status_t matmul_t<quantized>::sycl_execute_impl(const stream_t *g_stream,
const std::vector<tensor_t> &inputs,
const std::vector<tensor_t> &outputs,
const std::vector<::sycl::event> &sycl_deps,
::sycl::event *sycl_event) {
auto deps = sycl_deps;
::sycl::event returned_event;
dnnl::stream p_stream = make_dnnl_stream(p_engine_, *g_stream);
// each thread's own local resource
thread_local_cache_t<execution_args_set_t> res_cache;
execution_args_set_t *res = res_cache.get_or_add(
reinterpret_cast<size_t>(this), resource_ctor_);
temporary_scratchpad_t scratchpad(
memory_planner_.total_internal_temporary_size(), p_engine_,
*g_alloc_);
assertm(scratchpad.size()
>= memory_planner_.total_internal_temporary_size(),
"no enough scratchpad memory");
prepare_args_set(res, inputs, outputs, scratchpad);
constant_cache_t::cached_t c_buffer;
if (enabled_constant_cache()) {
const size_t encoded_key
= encode_constant_cache_key(inputs, const_md_hash_);
std::promise<constant_cache_t::cached_t> c_promise;
constant_cache_t::value_t cached_value
= dnnl_constant_cache_get_or_add(p_engine_, encoded_key,
memory_planner_.total_internal_persistent_size(),
c_promise.get_future());
bool is_from_cache = cached_value.valid();
if (is_from_cache) {
c_buffer = cached_value.get();
grantor_t c_grantor = memory_planner_.internal_persistent_grantor(
c_buffer->data<char>());
for (auto &mem_offkey : res->get_mems_use_internal_persistent()) {
mem_offkey.first.set_data_handle(
c_grantor.get(mem_offkey.second));
}
} else {
c_buffer = std::make_shared<dnnl_constant_buffer_t>(
memory_planner_.total_internal_persistent_size(), p_engine_,
g_alloc_);
grantor_t c_grantor = memory_planner_.internal_persistent_grantor(
c_buffer->data<char>());
for (auto &mem_offkey : res->get_mems_use_internal_persistent()) {
mem_offkey.first.set_data_handle(
c_grantor.get(mem_offkey.second));
}
for (size_t i = 0; i < subgraph_->execs_.size(); i++) {
if (!subgraph_->is_constant_[i]) continue;
returned_event = subgraph_->execs_[i]->execute_sycl(
p_stream, res->get_exec_args()[i], deps);
deps = {returned_event};
}
c_promise.set_value(c_buffer);
}
}
for (size_t i = 0; i < subgraph_->execs_.size(); i++) {
if (subgraph_->is_constant_[i]) continue;
returned_event = subgraph_->execs_[i]->execute_sycl(
p_stream, res->get_exec_args()[i], deps);
deps = {returned_event};
}
scratchpad.set_deps(returned_event);
if (sycl_event) *sycl_event = returned_event;
return status::success;
}
#endif
#if DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
template <bool quantized>
status_t matmul_t<quantized>::ocl_execute_impl(const stream_t *g_stream,
const std::vector<tensor_t> &inputs,
const std::vector<tensor_t> &outputs,
const std::vector<cl_event> &cl_deps, cl_event *ret_event) {
auto deps = cl_deps;
cl_event returned_event {};
dnnl::stream p_stream = make_dnnl_stream(p_engine_, *g_stream);
// each thread's own local resource
thread_local_cache_t<execution_args_set_t> res_cache;
execution_args_set_t *res = res_cache.get_or_add(
reinterpret_cast<size_t>(this), resource_ctor_);
temporary_scratchpad_t scratchpad(
memory_planner_.total_internal_temporary_size(), p_engine_,
*g_alloc_);
assertm(scratchpad.size()
>= memory_planner_.total_internal_temporary_size(),
"no enough scratchpad memory");
prepare_args_set(res, inputs, outputs, scratchpad);
constant_cache_t::cached_t c_buffer;
if (enabled_constant_cache()) {
const size_t encoded_key
= encode_constant_cache_key(inputs, const_md_hash_);
std::promise<constant_cache_t::cached_t> c_promise;
constant_cache_t::value_t cached_value
= dnnl_constant_cache_get_or_add(p_engine_, encoded_key,
memory_planner_.total_internal_persistent_size(),
c_promise.get_future());
bool is_from_cache = cached_value.valid();
if (is_from_cache) {
c_buffer = cached_value.get();
grantor_t c_grantor = memory_planner_.internal_persistent_grantor(
c_buffer->data<char>());
for (auto &mem_offkey : res->get_mems_use_internal_persistent()) {
mem_offkey.first.set_data_handle(
c_grantor.get(mem_offkey.second));
}
} else {
c_buffer = std::make_shared<dnnl_constant_buffer_t>(
memory_planner_.total_internal_persistent_size(), p_engine_,
g_alloc_);
grantor_t c_grantor = memory_planner_.internal_persistent_grantor(
c_buffer->data<char>());
for (auto &mem_offkey : res->get_mems_use_internal_persistent()) {
mem_offkey.first.set_data_handle(
c_grantor.get(mem_offkey.second));
}
for (size_t i = 0; i < subgraph_->execs_.size(); i++) {
if (!subgraph_->is_constant_[i]) continue;
returned_event = subgraph_->execs_[i]->execute_ocl(
p_stream, res->get_exec_args()[i], deps);
deps = {returned_event};
}
c_promise.set_value(c_buffer);
}
}
for (size_t i = 0; i < subgraph_->execs_.size(); i++) {
if (subgraph_->is_constant_[i]) continue;
returned_event = subgraph_->execs_[i]->execute_ocl(
p_stream, res->get_exec_args()[i], deps);
deps = {returned_event};
}
scratchpad.set_deps(returned_event);
if (ret_event) *ret_event = returned_event;
return status::success;
}
#endif
template struct matmul_t<false>;
template struct matmul_t<true>;
} // namespace dnnl_impl
} // namespace graph
} // namespace impl
} // namespace dnnl