forked from pytorch/pytorch
/
transpose.cpp
483 lines (425 loc) · 16.9 KB
/
transpose.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#include <torch/csrc/jit/codegen/cuda/arith.h>
#include <torch/csrc/jit/codegen/cuda/executor.h>
#include <torch/csrc/jit/codegen/cuda/fusion.h>
#include <torch/csrc/jit/codegen/cuda/ir_all_nodes.h>
#include <torch/csrc/jit/codegen/cuda/ir_utils.h>
#include <torch/csrc/jit/codegen/cuda/lower2device.h>
#include <torch/csrc/jit/codegen/cuda/ops/all_ops.h>
#include <torch/csrc/jit/codegen/cuda/scheduler/all_schedulers.h>
#include <benchmark/benchmark.h>
#include <cuda_runtime.h>
#include <benchmarks/cpp/nvfuser/utils.h>
#define TRANSPOSE_CONFIG {true, false, false, false}
using namespace torch::jit::fuser::cuda;
struct TransposeConfig {
bool input1_transpose_axes = false;
bool input2_transpose_axes = false;
bool intermediate_transpose_axes = false;
bool output_transpose_axes = false;
};
std::vector<at::Tensor> generateInputs(
DataType dtype,
int num_dims,
std::pair<int, int> axes,
int perm_size,
int innerdim_size,
bool input1_transpose_axes,
bool input2_transpose_axes,
bool non_vectorize_offset = false,
int iter_size = 32) {
at::manual_seed(0);
auto options =
at::TensorOptions().dtype(data_type_to_aten(dtype)).device(at::kCUDA, 0);
std::vector<int64_t> transpose_shape(num_dims, iter_size);
transpose_shape[axes.second] = innerdim_size;
transpose_shape[axes.first] = perm_size;
std::vector<int64_t> non_transpose_shape(num_dims, iter_size);
non_transpose_shape[axes.first] = innerdim_size;
non_transpose_shape[axes.second] = perm_size;
// TensorType: Concrete, Contig, Symbolic
// Vectorization | Unroll - Add 1 to sizes
// Shift axis by 1 to disable vectorize loads
if (non_vectorize_offset) {
for (auto idx : c10::irange(transpose_shape.size())) {
transpose_shape[idx] += 1;
}
for (auto idx : c10::irange(non_transpose_shape.size())) {
non_transpose_shape[idx] += 1;
}
}
auto optionalTransposeSize =
[&transpose_shape, &non_transpose_shape](bool transpose_tensor) {
return (transpose_tensor) ? transpose_shape : non_transpose_shape;
};
at::Tensor aten_input1 =
at::randn(optionalTransposeSize(input1_transpose_axes), options);
at::Tensor aten_input2 =
at::randn(optionalTransposeSize(input2_transpose_axes), options);
return {aten_input1, aten_input2};
}
//------------------------------------------------------------------------------
static void setupTranspose(
Fusion* fusion,
DataType dtype,
int num_dims,
std::pair<int, int> axes,
TransposeConfig tc) {
FusionGuard fg(fusion);
typedef std::pair<int, int> transpose_axes;
auto getTransposeMap =
[](const transpose_axes& axes) -> std::unordered_map<int, int> {
return {{axes.first, axes.second}, {axes.second, axes.first}};
};
auto optionalTranspose = [&getTransposeMap, axes](
TensorView* tv, bool is_transpose) {
return (is_transpose) ? transpose(tv, getTransposeMap(axes)) : tv;
};
auto input1 = makeContigTensor(num_dims);
auto input2 = makeContigTensor(num_dims);
fusion->addInput(input1);
fusion->addInput(input2);
auto ot_input1 = optionalTranspose(input1, tc.input1_transpose_axes);
auto ot_input2 = optionalTranspose(input2, tc.input2_transpose_axes);
auto intermediate = add(ot_input1, ot_input2);
auto ot_intermediate =
optionalTranspose(intermediate, tc.intermediate_transpose_axes);
auto output = relu(ot_intermediate);
auto ot_output = optionalTranspose(output, tc.output_transpose_axes);
fusion->addOutput(ot_output);
}
static void NvFuserScheduler_Transpose(
benchmark::State& benchmark_state,
FusionExecutorCache* fusion_executor_cache,
DataType dtype,
int num_dims,
std::pair<int, int> axes,
TransposeConfig tc) {
auto aten_inputs = generateInputs(
dtype,
num_dims,
axes,
benchmark_state.range(0),
benchmark_state.range(1),
tc.input1_transpose_axes,
tc.input2_transpose_axes);
auto at_input1 = aten_inputs[0];
auto at_input2 = aten_inputs[1];
std::vector<c10::IValue> fuser_inputs = {at_input1, at_input2};
runBenchmarkIterations(benchmark_state, fusion_executor_cache, fuser_inputs);
benchmark_state.SetBytesProcessed(
int64_t(benchmark_state.iterations()) *
((at_input1.numel() * 3) * int64_t(dataTypeSize(dtype))));
}
//------------------------------------------------------------------------------
#define NVFUSER_TRANSPOSE_SQUARE_RUN( \
TITLE, DTYPE, NUM_DIMS, AXIS1, AXIS2, CONFIG) \
NVFUSER_BENCHMARK_DEFINE( \
TITLE, \
setupTranspose, \
NvFuserScheduler_Transpose, \
DTYPE, \
NUM_DIMS, \
{AXIS1, AXIS2}, \
CONFIG); \
\
NVFUSER_BENCHMARK_RUN(TITLE) \
->RangeMultiplier(8) \
->Args({9, 2408}) \
->Args({16, 512}) \
->Args({18, 96}) \
->Args({24, 96}) \
->Args({24, 256}) \
->Args({24, 512}) \
->Args({32, 27}) \
->Args({32, 96}) \
->Args({32, 288}) \
->Args({32, 864}) \
->Args({40, 120}) \
->Args({48, 128}) \
->Args({48, 256}) \
->Args({49, 512}) \
->Args({49, 1024}) \
->Args({49, 2048}) \
->Args({49, 4608}) \
->Args({64, 64}) \
->Args({64, 96}) \
->Args({64, 128}) \
->Args({64, 147}) \
->Args({64, 192}) \
->Args({64, 256}) \
->Args({64, 288}) \
->Args({64, 512}) \
->Args({80, 64}) \
->Args({81, 1728}) \
->Args({83, 1728}) \
->Args({96, 864}) \
->Args({100, 1280}) \
->Args({100, 4032}) \
->Args({120, 40}) \
->Args({128, 128}) \
->Args({128, 512}) \
->Args({128, 1152}) \
->Args({192, 128}) \
->Args({192, 256}) \
->Args({192, 720}) \
->Args({192, 768}) \
->Args({192, 1120}) \
->Args({192, 1728}) \
->Args({196, 256}) \
->Args({196, 512}) \
->Args({196, 1024}) \
->Args({196, 2304}) \
->Args({256, 256}) \
->Args({256, 1024}) \
->Args({256, 2304}) \
->Args({284, 512}) \
->Args({320, 1280}) \
->Args({320, 1728}) \
->Args({324, 2592}) \
->Args({361, 768}) \
->Args({361, 1120}) \
->Args({384, 2}) \
->Args({384, 32}) \
->Args({384, 128}) \
->Args({384, 256}) \
->Args({384, 512}) \
->Args({384, 1280}) \
->Args({384, 2592}) \
->Args({384, 4032}) \
->Args({448, 1280}) \
->Args({480, 16}) \
->Args({480, 256}) \
->Args({512, 2}) \
->Args({512, 16}) \
->Args({512, 128}) \
->Args({512, 256}) \
->Args({512, 1024}) \
->Args({512, 2048}) \
->Args({512, 3072}) \
->Args({512, 4608}) \
->Args({784, 40}) \
->Args({784, 120}) \
->Args({784, 128}) \
->Args({784, 1152}) \
->Args({1001, 2408}) \
->Args({1024, 16}) \
->Args({1024, 256}) \
->Args({1024, 512}) \
->Args({1024, 1024}) \
->Args({1024, 3072}) \
->Args({1369, 192}) \
->Args({1369, 256}) \
->Args({1369, 288}) \
->Args({2048, 512}) \
->Args({2048, 1024}) \
->Args({2250, 27}) \
->Args({3072, 512}) \
->Args({3072, 1024}) \
->Args({3136, 64}) \
->Args({5329, 720}) \
->Args({5625, 64}) \
->Args({12544, 147}) \
->Args({22201, 288}) \
->Unit(benchmark::kMicrosecond)
NVFUSER_TRANSPOSE_SQUARE_RUN(
NF_Transpose_Random_fp32_Inner_2D_01_Axis,
DataType::Float,
2 /* num_dims */,
0 /* axis1 */,
1 /* axis2 */,
TransposeConfig(TRANSPOSE_CONFIG));
NVFUSER_TRANSPOSE_SQUARE_RUN(
NF_Transpose_Random_fp32_Inner_3D_02_Axis,
DataType::Float,
3 /* num_dims */,
0 /* axis1 */,
2 /* axis2 */,
TransposeConfig(TRANSPOSE_CONFIG));
NVFUSER_TRANSPOSE_SQUARE_RUN(
NF_Transpose_Random_fp32_Inner_3D_12_Axis,
DataType::Float,
3 /* num_dims */,
1 /* axis1 */,
2 /* axis2 */,
TransposeConfig(TRANSPOSE_CONFIG));
NVFUSER_TRANSPOSE_SQUARE_RUN(
NF_Transpose_Random_fp32_Outer_3D_01_Axis,
DataType::Float,
3 /* num_dims */,
0 /* axis1 */,
1 /* axis2 */,
TransposeConfig(TRANSPOSE_CONFIG));
//------------------------------------------------------------------------------
NVFUSER_TRANSPOSE_SQUARE_RUN(
NF_Transpose_Random_fp16_Inner_2D_01_Axis,
DataType::Half,
2 /* num_dims */,
0 /* axis1 */,
1 /* axis2 */,
TransposeConfig(TRANSPOSE_CONFIG));
NVFUSER_TRANSPOSE_SQUARE_RUN(
NF_Transpose_Random_fp16_Inner_3D_02_Axis,
DataType::Half,
3 /* num_dims */,
0 /* axis1 */,
2 /* axis2 */,
TransposeConfig(TRANSPOSE_CONFIG));
NVFUSER_TRANSPOSE_SQUARE_RUN(
NF_Transpose_Random_fp16_Inner_3D_12_Axis,
DataType::Half,
3 /* num_dims */,
1 /* axis1 */,
2 /* axis2 */,
TransposeConfig(TRANSPOSE_CONFIG));
NVFUSER_TRANSPOSE_SQUARE_RUN(
NF_Transpose_Random_fp16_Outer_3D_01_Axis,
DataType::Half,
3 /* num_dims */,
0 /* axis1 */,
1 /* axis2 */,
TransposeConfig(TRANSPOSE_CONFIG));
//------------------------------------------------------------------------------
#define NVFUSER_TRANSPOSE_RUN(TITLE, DTYPE, NUM_DIMS, AXIS1, AXIS2, CONFIG) \
NVFUSER_BENCHMARK_DEFINE( \
TITLE, \
setupTranspose, \
NvFuserScheduler_Transpose, \
DTYPE, \
NUM_DIMS, \
{AXIS1, AXIS2}, \
CONFIG); \
\
NVFUSER_BENCHMARK_RUN(TITLE) \
->RangeMultiplier(8) \
->Ranges({{2, 256 * 256}, {160, 320}}) \
->Unit(benchmark::kMicrosecond) \
NVFUSER_TRANSPOSE_RUN(
NF_Transpose_fp32_Inner_2D_01_Axis,
DataType::Float,
2 /* num_dims */,
0 /* axis1 */,
1 /* axis2 */,
TransposeConfig(TRANSPOSE_CONFIG));
NVFUSER_TRANSPOSE_RUN(
NF_Transpose_fp32_Inner_3D_02_Axis,
DataType::Float,
3 /* num_dims */,
0 /* axis1 */,
2 /* axis2 */,
TransposeConfig(TRANSPOSE_CONFIG));
NVFUSER_TRANSPOSE_RUN(
NF_Transpose_fp32_Inner_3D_12_Axis,
DataType::Float,
3 /* num_dims */,
1 /* axis1 */,
2 /* axis2 */,
TransposeConfig(TRANSPOSE_CONFIG));
NVFUSER_TRANSPOSE_RUN(
NF_Transpose_fp32_Outer_3D_01_Axis,
DataType::Float,
3 /* num_dims */,
0 /* axis1 */,
1 /* axis2 */,
TransposeConfig(TRANSPOSE_CONFIG));
//------------------------------------------------------------------------------
NVFUSER_TRANSPOSE_RUN(
NF_Transpose_fp16_Inner_2D_01_Axis,
DataType::Half,
2 /* num_dims */,
0 /* axis1 */,
1 /* axis2 */,
TransposeConfig(TRANSPOSE_CONFIG));
NVFUSER_TRANSPOSE_RUN(
NF_Transpose_fp16_Inner_3D_02_Axis,
DataType::Half,
3 /* num_dims */,
0 /* axis1 */,
2 /* axis2 */,
TransposeConfig(TRANSPOSE_CONFIG));
NVFUSER_TRANSPOSE_RUN(
NF_Transpose_fp16_Inner_3D_12_Axis,
DataType::Half,
3 /* num_dims */,
1 /* axis1 */,
2 /* axis2 */,
TransposeConfig(TRANSPOSE_CONFIG));
NVFUSER_TRANSPOSE_RUN(
NF_Transpose_fp16_Outer_3D_01_Axis,
DataType::Half,
3 /* num_dims */,
0 /* axis1 */,
1 /* axis2 */,
TransposeConfig(TRANSPOSE_CONFIG));
//------------------------------------------------------------------------------
static void Baseline_Transpose(
benchmark::State& benchmark_state,
DataType dtype,
int num_dims,
std::pair<int, int> axes,
TransposeConfig tc) {
auto aten_inputs = generateInputs(
dtype,
num_dims,
axes,
benchmark_state.range(0),
benchmark_state.range(1),
tc.input1_transpose_axes,
tc.input2_transpose_axes);
auto at_input1 = aten_inputs[0];
auto at_input2 = aten_inputs[1];
auto optionalTransposeAten = [&axes](at::Tensor at, bool is_transpose) {
return (is_transpose) ? at::transpose(at, axes.first, axes.second) : at;
};
for (auto _ : benchmark_state) {
clearL2Cache();
CudaKernelTimer timer;
auto at_ot_input1 =
optionalTransposeAten(at_input1, tc.input1_transpose_axes);
auto at_ot_input2 =
optionalTransposeAten(at_input2, tc.input2_transpose_axes);
auto at_intermediate = add(at_ot_input1, at_ot_input2);
auto at_ot_intermediate =
optionalTransposeAten(at_intermediate, tc.intermediate_transpose_axes);
auto at_output = relu(at_ot_intermediate);
auto at_ot_output =
optionalTransposeAten(at_output, tc.output_transpose_axes);
benchmark_state.SetIterationTime(timer.elapsed() / 1000.0);
}
// Sync everything up before we're finished, don't want to run ahead on the
// cpu while benchmarking.
cudaDeviceSynchronize();
benchmark_state.SetBytesProcessed(
int64_t(benchmark_state.iterations()) *
(at_input1.numel() * 3 * int64_t(dataTypeSize(dtype))));
}
//------------------------------------------------------------------------------
static void Baseline_Transpose_fp32_Inner_2D_01_Axis(
benchmark::State& benchmark_state) {
Baseline_Transpose(
benchmark_state,
DataType::Float,
2 /* num_dims */,
{0, 1} /* axes */,
TRANSPOSE_CONFIG);
}
static void Baseline_Transpose_fp16_Inner_2D_01_Axis(
benchmark::State& benchmark_state) {
Baseline_Transpose(
benchmark_state,
DataType::Half,
2 /* num_dims */,
{0, 1} /* axes */,
TRANSPOSE_CONFIG);
}
//------------------------------------------------------------------------------
BENCHMARK(Baseline_Transpose_fp32_Inner_2D_01_Axis)
// ->RangeMultiplier(2)
->Ranges({{2, 1024 * 1024}, {160, 320}})
->Unit(benchmark::kMicrosecond)
->UseManualTime();
BENCHMARK(Baseline_Transpose_fp16_Inner_2D_01_Axis)
// ->RangeMultiplier(2)
->Ranges({{2, 1024 * 1024}, {160, 320}})
->Unit(benchmark::kMicrosecond)
->UseManualTime();
//------------------------------------------------------------------------------