forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvulkan_compute_api_test.cpp
More file actions
3339 lines (2763 loc) · 104 KB
/
Copy pathvulkan_compute_api_test.cpp
File metadata and controls
3339 lines (2763 loc) · 104 KB
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <gtest/gtest.h>
#include <bitset>
#include <iomanip>
#include <utility>
#include <vector>
#include <executorch/runtime/core/exec_aten/exec_aten.h>
#include <executorch/backends/vulkan/runtime/api/api.h>
#include <executorch/backends/vulkan/runtime/graph/ops/OperatorRegistry.h>
#include <executorch/backends/vulkan/runtime/graph/ops/utils/StagingUtils.h>
#include <executorch/backends/vulkan/runtime/graph/ops/impl/utils/TensorUtils.h>
#include <executorch/backends/vulkan/runtime/graph/ops/impl/utils/QPackUtils.h>
#include <executorch/backends/vulkan/test/utils/test_utils.h>
#include <executorch/backends/vulkan/runtime/graph/ops/DispatchNode.h>
#include <executorch/backends/vulkan/runtime/graph/ops/impl/Int8x4Staging.h>
using namespace vkcompute;
using namespace vkcompute::api;
std::vector<float>
transpose_matrix(std::vector<float>& mat, const int H, const int W) {
std::vector<float> out(W * H);
for (int out_y = 0; out_y < H; ++out_y) {
for (int out_x = 0; out_x < W; ++out_x) {
out[out_x * H + out_y] = mat[out_y * W + out_x];
}
}
return out;
}
std::vector<float> compute_reference_matmul(
std::vector<float>& mat1,
std::vector<float>& mat2,
const int M,
const int K,
const int N) {
std::vector<float> out(M * N);
for (int out_y = 0; out_y < M; ++out_y) {
for (int out_x = 0; out_x < N; ++out_x) {
out[out_y * N + out_x] = 0;
for (int k = 0; k < K; ++k) {
out[out_y * N + out_x] += mat1[out_y * K + k] * mat2[k * N + out_x];
}
}
}
return out;
}
std::vector<std::vector<int64_t>> standard_sizes_to_test = {
// 1D
{7},
{13},
{24},
// 2D
{7, 11},
{13, 6},
// 3D
{2, 9, 7},
{9, 15, 19},
{7, 11, 24},
{13, 8, 11},
{12, 11, 19},
// 4D
{2, 2, 3, 5},
{9, 13, 11, 17},
{17, 14, 18, 20},
{7, 13, 12, 21},
{3, 8, 13, 17},
};
//
// Compute API Tests
//
class VulkanComputeAPITest : public ::testing::Test {
public:
void SetUp() override {
// Make sure we are starting with a clean slate
EXPECT_TRUE(get_vma_allocation_count() == 0);
}
void TearDown() override {
context()->flush();
// Make sure we are ending with a clean slate
EXPECT_TRUE(get_vma_allocation_count() == 0);
}
};
TEST_F(VulkanComputeAPITest, print_adapter) {
std::cout << *(context()->adapter_ptr()) << std::endl;
}
#if defined(VK_KHR_pipeline_executable_properties) && \
defined(ETVK_INSPECT_PIPELINES)
TEST_F(VulkanComputeAPITest, print_shader_executable_properties) {
context()->print_shader_executable_properties(
VK_KERNEL(binary_add_nobroadcast__test_half), {0});
}
#endif // VK_KHR_pipeline_executable_properties && ETVK_INSPECT_PIPELINES
std::vector<int64_t> get_reference_dim_order(
const size_t ndim,
const int32_t packed_dim,
const int32_t outer_packed_dim,
const bool block_transposed) {
// Special case for zero dim tensors
if (ndim == 0) {
return {0};
}
// Initialize dim_order as {0, 1, 2, ..., ndim-1}
std::vector<int64_t> dim_order(ndim);
for (size_t i = 0; i < ndim; ++i) {
dim_order[i] = static_cast<int64_t>(i);
}
int64_t ndim_signed = static_cast<int64_t>(ndim);
// Convert WHCN indices to NCHW indices
// packed_dim and outer_packed_dim are in WHCN order (0=W, 1=H, 2=C, 3=N)
// NCHW index = ndim - 1 - WHCN index
int64_t last_dim_nchw = ndim_signed - 1 - packed_dim;
int64_t second_last_dim_nchw = ndim_signed - 1 - outer_packed_dim;
if (block_transposed) {
std::swap(last_dim_nchw, second_last_dim_nchw);
}
// Move last_dim_nchw to the back (if valid)
bool last_dim_valid = last_dim_nchw >= 0 && last_dim_nchw < ndim_signed;
if (last_dim_valid) {
auto it = std::find(dim_order.begin(), dim_order.end(), last_dim_nchw);
if (it != dim_order.end()) {
dim_order.erase(it);
dim_order.push_back(last_dim_nchw);
}
}
// Move second_last_dim_nchw to:
// a) second last position if last_dim_nchw is valid
// b) last position if last_dim_nchw was not valid
bool second_last_dim_valid =
second_last_dim_nchw >= 0 && second_last_dim_nchw < ndim_signed;
if (second_last_dim_valid) {
auto it =
std::find(dim_order.begin(), dim_order.end(), second_last_dim_nchw);
if (it != dim_order.end()) {
dim_order.erase(it);
if (last_dim_valid && ndim >= 2) {
// Insert at second last position
dim_order.insert(dim_order.end() - 1, second_last_dim_nchw);
} else {
// Insert at last position
dim_order.push_back(second_last_dim_nchw);
}
}
}
return dim_order;
}
std::vector<int64_t> get_reference_padded_sizes(
const std::vector<int64_t>& sizes,
const int32_t packed_dim,
const int32_t packed_dim_block_size,
const int32_t outer_packed_dim = -1,
const int32_t outer_packed_dim_block_size = 1) {
int64_t ndim = sizes.size();
if (ndim == 0) {
ndim = 1;
}
// Tensor sizes will be unsqueezed up to the next multiple of 4
const int64_t ndim_up4 = utils::align_up_4(ndim);
std::vector<int64_t> padded_sizes(ndim_up4);
for (int64_t i = 0; i < ndim_up4; ++i) {
padded_sizes.at(i) = utils::val_at(i - ndim_up4, sizes);
}
// Pad the packed dim to the next multiple of the block size if > 1
if (packed_dim_block_size > 1) {
const int64_t dim_offset = packed_dim + 1;
const int64_t padded_dim_size = utils::val_at(-dim_offset, sizes);
padded_sizes.at(ndim_up4 - dim_offset) = utils::align_up(
padded_dim_size, static_cast<int64_t>(packed_dim_block_size));
}
// For block-packed layouts, also pad the outer packed dimension if > 1
if (outer_packed_dim >= 0 && outer_packed_dim != packed_dim &&
outer_packed_dim_block_size > 1) {
const int64_t outer_dim_offset = outer_packed_dim + 1;
const int64_t outer_padded_dim_size =
utils::val_at(-outer_dim_offset, sizes);
padded_sizes.at(ndim_up4 - outer_dim_offset) = utils::align_up(
outer_padded_dim_size,
static_cast<int64_t>(outer_packed_dim_block_size));
}
return padded_sizes;
}
std::vector<int64_t> get_reference_strides(
const std::vector<int64_t>& sizes,
const utils::GPUMemoryLayout layout,
const bool flip_unsqueezed = false) {
int64_t C = utils::val_at(-3, sizes);
int64_t H = utils::val_at(-2, sizes);
int64_t W = utils::val_at(-1, sizes);
int64_t numel = utils::multiply_integers(sizes);
switch (layout) {
case utils::kWidthPacked:
switch (sizes.size()) {
case 1:
if (flip_unsqueezed)
return {1, numel, numel, numel};
return {1};
case 2:
if (flip_unsqueezed)
return {1, W, numel, numel};
return {W, 1};
case 3:
if (flip_unsqueezed)
return {1, W, H * W, numel};
return {H * W, W, 1};
case 4:
if (flip_unsqueezed)
return {1, W, H * W, C * H * W};
return {C * H * W, H * W, W, 1};
default:
return {};
}
break;
case utils::kHeightPacked:
switch (sizes.size()) {
case 1:
if (flip_unsqueezed)
return {1, numel, numel, numel};
return {1};
case 2:
if (flip_unsqueezed)
return {H, 1, numel, numel};
return {1, H};
return {1, H};
case 3:
if (flip_unsqueezed)
return {H, 1, H * W, numel};
return {W * H, 1, H};
case 4:
if (flip_unsqueezed)
return {H, 1, W * H, C * W * H};
return {C * W * H, W * H, 1, H};
default:
return {};
}
case utils::kChannelsPacked:
switch (sizes.size()) {
case 1:
if (flip_unsqueezed)
return {1, numel, numel, numel};
return {1};
case 2:
if (flip_unsqueezed)
return {1, W, numel, numel};
return {W, 1};
case 3:
if (flip_unsqueezed)
return {C, W * C, 1, numel};
return {1, W * C, C};
case 4:
if (flip_unsqueezed)
return {C, W * C, 1, H * W * C};
return {H * W * C, 1, W * C, C};
default:
return {};
}
default:
VK_THROW("Unsupported memory layout: ", layout);
}
return {};
}
int64_t get_reference_physical_numel(
const vkapi::ScalarType dtype,
const std::vector<int64_t>& padded_sizes) {
size_t numel = utils::multiply_integers(padded_sizes);
// For kInt8x4, the data buffer is interpreted as an array of int32, where
// each int32 contains 4xint8 values. To account for this, the number of
// elements needs to be divided by 4.
if (dtype == vkapi::kInt8x4) {
// Should already be a multiple of 4 due to padding
if (numel % 4 != 0) {
VK_THROW("Expected numel to be multiple of 4 for kInt8x4");
}
numel /= 4;
}
// For 8-bit types, align to the next multiple of 4. For devices that do not
// support 8-bit storage buffers, the tensor data will be interpreted as an
// array of int32 instead.
if (vkapi::element_size(dtype) == 1) {
numel = utils::align_up_4(numel);
}
return numel;
}
utils::uvec3 get_reference_image_extents(
const vkapi::ScalarType dtype,
const int32_t packed_dim,
const int32_t outer_packed_dim,
const bool is_block_packed,
const std::vector<int64_t>& padded_sizes,
const std::vector<int64_t>& axis_map) {
utils::uvec3 extents({1, 1, 1});
const int64_t packed_dim_axis = axis_map.at(packed_dim);
const int64_t outer_packed_dim_axis = axis_map.at(outer_packed_dim);
// If the packed dim is not padded to the next multiple of 4, then that means
// this tensor is using buffer storage and does not require texture extents.
const int64_t packed_dim_idx = padded_sizes.size() - 1 - packed_dim;
if (padded_sizes.at(packed_dim_idx) % 4 != 0) {
return extents;
}
// For high dimensional tensors, buffer storage must be used. No need to
// compute image extents in this case.
if (padded_sizes.size() > 4) {
return extents;
}
// First three elements of axis_map indicate which (X,Y,Z) image axis the
// width, height, and channels dim of the tensor maps to.
for (int whcn_dim = 0; whcn_dim < 3; ++whcn_dim) {
const int64_t axis = axis_map.at(whcn_dim);
const int64_t dim = padded_sizes.size() - 1 - whcn_dim;
extents[axis] = utils::safe_downcast<uint32_t>(padded_sizes.at(dim));
}
// For "regular" tensor dtypes, 4 elements along the packed dim are packed
// into one texel (4-component vectorized type). However, for kInt8x4 dtype,
// an additional level of packing is employed where 4 int8 elements are
// packed into one int32, and then 4 int32 are packed into each ivec4 texel.
if (dtype == vkapi::kInt8x4) {
// For layouts with only one packed dimension, loading an ivec4 texel from
// the texture loads 16 int8 values (4 int32 that each contain 4 int8).
if (!is_block_packed) {
extents[packed_dim_axis] = utils::div_up(extents[packed_dim_axis], 16u);
}
// Layouts with two packed dimension (e.g., 4W4C, 4H4W) load a 4x4 block of
// data from two dimensions with each ivec4 texel load, as opposed to 16
// adjacent values from a single dimension.
else {
if (extents[outer_packed_dim_axis] % 4 != 0) {
VK_THROW("Expected outer_packed_dim_axis extent to be multiple of 4");
}
extents[outer_packed_dim_axis] /= 4;
if (extents[packed_dim_axis] % 4 != 0) {
VK_THROW("Expected packed_dim_axis extent to be multiple of 4");
}
extents[packed_dim_axis] /= 4;
}
} else {
extents[packed_dim_axis] /= 4;
}
// axis_map[3] indicates the WHCN index of the dimension used for batch
// concatenation. Thus a double lookup is required to determine the image axis
// used for batch concatenation.
const int64_t concatted_whcn_dim = axis_map.at(3);
const int64_t batch_axis = axis_map.at(concatted_whcn_dim);
// Multiply the extents of the batch axis by the batch size.
extents[batch_axis] *= padded_sizes.at(0);
return extents;
}
TEST_F(VulkanComputeAPITest, empty_init_shader_info_test) {
vkapi::ShaderInfo empty_shader_info;
EXPECT_FALSE(empty_shader_info);
EXPECT_TRUE(empty_shader_info.src_code.bin == nullptr);
EXPECT_TRUE(empty_shader_info.src_code.size == 0u);
}
bool compare_vectors(
const std::vector<int32_t>& v32,
const std::vector<int64_t>& v64) {
if (v32.size() != v64.size()) {
return false;
}
for (size_t i = 0; i < v32.size(); ++i) {
if (static_cast<int64_t>(v32[i]) != v64[i]) {
return false;
}
}
return true;
}
TEST_F(VulkanComputeAPITest, tensor_layout_metadata_test) {
// Test all combinations of tensor sizes, storage types, and memory layouts
// to ensure that layout metadata is computed correctly
// Define test configuration for each layout type
struct LayoutTestConfig {
utils::GPUMemoryLayout layout;
vkapi::ScalarType dtype;
int32_t packed_dim;
int32_t outer_packed_dim;
bool is_block_packed;
bool block_transposed;
};
std::vector<LayoutTestConfig> layout_configs = {
// Standard layouts with float dtype
// For non-block-packed: outer_packed_dim = (packed_dim == 0) ? 1 : 0
{utils::kWidthPacked,
vkapi::kFloat,
WHCN::kWidthDim,
WHCN::kHeightDim,
false,
false},
{utils::kHeightPacked,
vkapi::kFloat,
WHCN::kHeightDim,
WHCN::kWidthDim,
false,
false},
{utils::kChannelsPacked,
vkapi::kFloat,
WHCN::kChannelsDim,
WHCN::kWidthDim,
false,
false},
// Packed int8 vector layouts (single-dimension packed)
// Use kChar, which should be converted to kInt8x4
{utils::kPackedInt8_4W,
vkapi::kChar,
WHCN::kWidthDim,
WHCN::kHeightDim,
false,
false},
{utils::kPackedInt8_4C,
vkapi::kChar,
WHCN::kChannelsDim,
WHCN::kWidthDim,
false,
false},
// Packed int8 block layouts (two-dimension packed)
// Use kChar, which should be converted to kInt8x4
{utils::kPackedInt8_4W4C,
vkapi::kChar,
WHCN::kChannelsDim,
WHCN::kWidthDim,
true,
false},
{utils::kPackedInt8_4H4W,
vkapi::kChar,
WHCN::kWidthDim,
WHCN::kHeightDim,
true,
false},
{utils::kPackedInt8_4C1W,
vkapi::kChar,
WHCN::kChannelsDim,
WHCN::kWidthDim,
false,
true},
};
std::vector<utils::StorageType> storage_types = {
utils::kBuffer, utils::kTexture3D};
for (const auto& sizes : standard_sizes_to_test) {
if (sizes.size() < 2) {
continue; // Skip 1D tensors
}
for (const auto& storage_type : storage_types) {
for (const auto& config : layout_configs) {
// Skip block-packed layouts for tensors with less than 3 dimensions
if (config.is_block_packed && sizes.size() < 3) {
continue;
}
// Create tensor
vTensor tensor(
context(),
sizes,
config.dtype,
storage_type,
config.layout,
/*allocate_memory = */ false);
// Verify sizes
ASSERT_TRUE(tensor.sizes() == sizes)
<< "Sizes mismatch for layout=" << static_cast<int>(config.layout)
<< ", storage=" << static_cast<int>(storage_type);
// Verify dtype
// For packed int8 layouts, kChar should be converted to kInt8x4
vkapi::ScalarType expected_dtype = config.dtype;
if (config.dtype == vkapi::kChar) {
expected_dtype = vkapi::kInt8x4;
}
ASSERT_EQ(tensor.dtype(), expected_dtype)
<< "Dtype mismatch for layout=" << static_cast<int>(config.layout)
<< ", expected=" << static_cast<int>(expected_dtype)
<< ", got=" << static_cast<int>(tensor.dtype());
// Determine packed_dim_block_size based on layout and storage type
// - kInt8 non-block-packed + texture: 16 (16 values per texel)
// - kInt8 non-block-packed + buffer: 4 (alignment)
// - kInt8 block-packed: 4 (4 values per dim per texel)
// - Standard texture: 4 (4 values per texel)
// - Contiguous buffer: 1 (no padding)
const bool is_non_block_packed_int8 =
config.dtype == vkapi::kChar && !config.is_block_packed;
int32_t expected_packed_dim_block_size;
if (is_non_block_packed_int8 && storage_type != utils::kBuffer) {
expected_packed_dim_block_size = 16;
} else if (config.dtype == vkapi::kChar) {
expected_packed_dim_block_size = 4;
} else if (storage_type != utils::kBuffer) {
expected_packed_dim_block_size = 4;
} else {
expected_packed_dim_block_size = 1;
}
// For block-packed layouts, outer_packed_dim is also padded
const int32_t expected_outer_packed_dim_block_size =
config.is_block_packed ? 4 : 1;
// Expected block_numel is the product of the two block sizes
const int32_t expected_block_numel = expected_packed_dim_block_size *
expected_outer_packed_dim_block_size;
// Verify packed_dim_info
const auto& packed_dim_info = tensor.packed_dim_info();
ASSERT_EQ(packed_dim_info.packed_dim, config.packed_dim)
<< "packed_dim mismatch for layout="
<< static_cast<int>(config.layout);
ASSERT_EQ(
packed_dim_info.packed_dim_block_size,
expected_packed_dim_block_size)
<< "packed_dim_block_size mismatch for layout="
<< static_cast<int>(config.layout);
ASSERT_EQ(packed_dim_info.outer_packed_dim, config.outer_packed_dim)
<< "outer_packed_dim mismatch for layout="
<< static_cast<int>(config.layout);
ASSERT_EQ(
packed_dim_info.outer_packed_dim_block_size,
expected_outer_packed_dim_block_size)
<< "outer_packed_dim_block_size mismatch for layout="
<< static_cast<int>(config.layout);
ASSERT_EQ(packed_dim_info.block_numel, expected_block_numel)
<< "block_numel mismatch for layout="
<< static_cast<int>(config.layout);
ASSERT_EQ(packed_dim_info.block_transposed, config.block_transposed)
<< "block_transposed mismatch for layout="
<< static_cast<int>(config.layout);
// Verify dim_order
std::vector<int64_t> ref_dim_order = get_reference_dim_order(
sizes.size(),
config.packed_dim,
config.outer_packed_dim,
config.block_transposed);
ASSERT_TRUE(tensor.dim_order() == ref_dim_order)
<< "Dim order mismatch for layout="
<< static_cast<int>(config.layout);
// Verify padded_sizes
std::vector<int64_t> ref_padded_sizes = get_reference_padded_sizes(
sizes,
config.packed_dim,
expected_packed_dim_block_size,
config.outer_packed_dim,
expected_outer_packed_dim_block_size);
ASSERT_TRUE(tensor.padded_sizes() == ref_padded_sizes)
<< "Padded sizes mismatch for layout="
<< static_cast<int>(config.layout);
if (storage_type == utils::kBuffer) {
// For buffer tensors, verify strides (only for standard layouts)
// For int8 layouts, we rely on padded_sizes and dim_order
// verification
if (config.dtype == vkapi::kFloat) {
std::vector<int64_t> ref_strides =
get_reference_strides(sizes, config.layout);
ASSERT_TRUE(tensor.strides() == ref_strides)
<< "Strides mismatch for layout="
<< static_cast<int>(config.layout);
// Also test flip_and_unsqueeze operations
int64_t numel = utils::multiply_integers(sizes);
std::vector<int64_t> unsqueezed_strides =
flip_and_unsqueeze<int64_t>(
tensor.strides(), kTensorStrides, numel);
std::vector<int64_t> ref_unsqueezed_strides =
get_reference_strides(sizes, config.layout, true);
ASSERT_TRUE(unsqueezed_strides == ref_unsqueezed_strides);
}
// Verify physical_numel for buffer storage
int64_t ref_physical_numel =
get_reference_physical_numel(expected_dtype, ref_padded_sizes);
ASSERT_EQ(tensor.physical_numel(), ref_physical_numel)
<< "Physical numel mismatch for buffer storage with layout="
<< static_cast<int>(config.layout);
} else {
// For texture tensors, verify axis_map
std::vector<int64_t> expected_axis_map = {0, 1, 2, 2};
ASSERT_TRUE(tensor.axis_map() == expected_axis_map)
<< "Axis map mismatch for texture tensor with layout="
<< static_cast<int>(config.layout);
ASSERT_TRUE(tensor.has_standard_axis_map());
// Verify image_extents for texture storage
utils::uvec3 ref_image_extents = get_reference_image_extents(
expected_dtype,
config.packed_dim,
config.outer_packed_dim,
config.is_block_packed,
ref_padded_sizes,
expected_axis_map);
ASSERT_EQ(tensor.image_extents(), ref_image_extents)
<< "Image extents mismatch for texture storage with layout="
<< static_cast<int>(config.layout);
}
}
}
}
}
TEST_F(VulkanComputeAPITest, tensor_layout_metadata_test_against_golden) {
// Test with hardcoded golden values for specific test cases.
// This complements the reference implementation test by providing concrete
// examples with known-good values.
struct TestCase {
std::vector<int64_t> sizes;
vkapi::ScalarType dtype;
utils::GPUMemoryLayout layout;
// Expected values for both buffer and texture storage
std::vector<int64_t> expected_dim_order;
std::vector<int64_t> expected_padded_sizes_buffer;
std::vector<int64_t> expected_padded_sizes_texture;
std::vector<int64_t> expected_strides_buffer;
int64_t expected_physical_numel_buffer;
int64_t expected_physical_numel_texture;
utils::uvec3 expected_image_extents;
};
std::vector<TestCase> test_cases = {
// 1D tensor [7] with width packed, float dtype
{/* sizes */ {7},
/* dtype */ vkapi::kFloat,
/* layout */ utils::kWidthPacked,
/* expected_dim_order */ {0},
/* expected_padded_sizes_buffer */ {1, 1, 1, 7},
/* expected_padded_sizes_texture */ {1, 1, 1, 8},
/* expected_strides_buffer */ {1},
/* expected_physical_numel_buffer */ 7,
/* expected_physical_numel_texture */ 8,
/* expected_image_extents */ {2, 1, 1}},
// 1D tensor [13] with width packed, float dtype
{/* sizes */ {13},
/* dtype */ vkapi::kFloat,
/* layout */ utils::kWidthPacked,
/* expected_dim_order */ {0},
/* expected_padded_sizes_buffer */ {1, 1, 1, 13},
/* expected_padded_sizes_texture */ {1, 1, 1, 16},
/* expected_strides_buffer */ {1},
/* expected_physical_numel_buffer */ 13,
/* expected_physical_numel_texture */ 16,
/* expected_image_extents */ {4, 1, 1}},
// 1D tensor [7] with channels packed, float dtype
// C dimension (implicit, size 1) is padded to 4
{/* sizes */ {7},
/* dtype */ vkapi::kFloat,
/* layout */ utils::kChannelsPacked,
/* expected_dim_order */ {0},
/* expected_padded_sizes_buffer */ {1, 1, 1, 7},
/* expected_padded_sizes_texture */ {1, 4, 1, 7},
/* expected_strides_buffer */ {1},
/* expected_physical_numel_buffer */ 7,
/* expected_physical_numel_texture */ 28,
/* expected_image_extents */ {7, 1, 1}},
// 2D tensor [5, 7] with width packed, float dtype
{/* sizes */ {5, 7},
/* dtype */ vkapi::kFloat,
/* layout */ utils::kWidthPacked,
/* expected_dim_order */ {0, 1},
/* expected_padded_sizes_buffer */ {1, 1, 5, 7},
/* expected_padded_sizes_texture */ {1, 1, 5, 8},
/* expected_strides_buffer */ {7, 1},
/* expected_physical_numel_buffer */ 35,
/* expected_physical_numel_texture */ 40,
/* expected_image_extents */ {2, 5, 1}},
// 3D tensor [3, 5, 7] with channels packed, float dtype
{/* sizes */ {3, 5, 7},
/* dtype */ vkapi::kFloat,
/* layout */ utils::kChannelsPacked,
/* expected_dim_order */ {1, 2, 0},
/* expected_padded_sizes_buffer */ {1, 3, 5, 7},
/* expected_padded_sizes_texture */ {1, 4, 5, 7},
/* expected_strides_buffer */ {1, 7 * 3, 3},
/* expected_physical_numel_buffer */ 105,
/* expected_physical_numel_texture */ 140,
/* expected_image_extents */ {7, 5, 1}},
// 4D tensor [2, 3, 5, 7] with height packed, float dtype
{/* sizes */ {2, 3, 5, 7},
/* dtype */ vkapi::kFloat,
/* layout */ utils::kHeightPacked,
/* expected_dim_order */ {0, 1, 3, 2},
/* expected_padded_sizes_buffer */ {2, 3, 5, 7},
/* expected_padded_sizes_texture */ {2, 3, 8, 7},
/* expected_strides_buffer */ {3 * 5 * 7, 5 * 7, 1, 5},
/* expected_physical_numel_buffer */ 210,
/* expected_physical_numel_texture */ 336,
/* expected_image_extents */ {7, 2, 6}},
// 3D tensor [8, 12, 16] with packed int8 4W layout
{/* sizes */ {8, 12, 16},
/* dtype */ vkapi::kChar,
/* layout */ utils::kPackedInt8_4W,
/* expected_dim_order */ {0, 1, 2},
/* expected_padded_sizes_buffer */ {1, 8, 12, 16},
/* expected_padded_sizes_texture */ {1, 8, 12, 16},
/* expected_strides_buffer */ {},
/* expected_physical_numel_buffer */ 384,
/* expected_physical_numel_texture */ 384,
/* expected_image_extents */ {1, 12, 8}},
// 3D tensor [8, 12, 16] with packed int8 4W4C block layout
{/* sizes */ {8, 12, 16},
/* dtype */ vkapi::kChar,
/* layout */ utils::kPackedInt8_4W4C,
/* expected_dim_order */ {1, 2, 0},
/* expected_padded_sizes_buffer */ {1, 8, 12, 16},
/* expected_padded_sizes_texture */ {1, 8, 12, 16},
/* expected_strides_buffer */ {},
/* expected_physical_numel_buffer */ 384,
/* expected_physical_numel_texture */ 384,
/* expected_image_extents */ {4, 12, 2}},
// 3D tensor [9, 13, 17] with packed int8 4C layout (odd sizes)
// For texture, packed_dim (channels) is padded to multiple of 16
{/* sizes */ {9, 13, 17},
/* dtype */ vkapi::kChar,
/* layout */ utils::kPackedInt8_4C,
/* expected_dim_order */ {1, 2, 0},
/* expected_padded_sizes_buffer */ {1, 12, 13, 17},
/* expected_padded_sizes_texture */ {1, 16, 13, 17},
/* expected_strides_buffer */ {},
/* expected_physical_numel_buffer */ 663,
/* expected_physical_numel_texture */ 884,
/* expected_image_extents */ {17, 13, 1}},
// 3D tensor [9, 13, 17] with packed int8 4H4W block layout (odd sizes)
{/* sizes */ {9, 13, 17},
/* dtype */ vkapi::kChar,
/* layout */ utils::kPackedInt8_4H4W,
/* expected_dim_order */ {0, 1, 2},
/* expected_padded_sizes_buffer */ {1, 9, 16, 20},
/* expected_padded_sizes_texture */ {1, 9, 16, 20},
/* expected_strides_buffer */ {},
/* expected_physical_numel_buffer */ 720,
/* expected_physical_numel_texture */ 720,
/* expected_image_extents */ {5, 4, 9}},
// 3D tensor [9, 13, 17] with packed int8 4C1W block-transposed layout
// packed_dim = channels (2), outer_packed_dim = width (0)
// block_transposed = true, so dim_order swaps: [1, 0, 2] instead of
// [1, 2, 0] Channels padded to 12 (multiple of 4), width padded to 20
// (multiple of 4)
{/* sizes */ {9, 13, 17},
/* dtype */ vkapi::kChar,
/* layout */ utils::kPackedInt8_4C1W,
/* expected_dim_order */ {1, 0, 2},
/* expected_padded_sizes_buffer */ {1, 12, 13, 17},
/* expected_padded_sizes_texture */ {1, 16, 13, 17},
/* expected_strides_buffer */ {},
/* expected_physical_numel_buffer */ 663,
/* expected_physical_numel_texture */ 884,
/* expected_image_extents */ {17, 13, 1}},
// 4D tensor [2, 8, 12, 16] with packed int8 4C1W block-transposed layout
// Tests 4D case with block_transposed = true
{/* sizes */ {2, 8, 12, 16},
/* dtype */ vkapi::kChar,
/* layout */ utils::kPackedInt8_4C1W,
/* expected_dim_order */ {0, 2, 1, 3},
/* expected_padded_sizes_buffer */ {2, 8, 12, 16},
/* expected_padded_sizes_texture */ {2, 16, 12, 16},
/* expected_strides_buffer */ {},
/* expected_physical_numel_buffer */ 768,
/* expected_physical_numel_texture */ 1536,
/* expected_image_extents */ {16, 12, 2}},
};
for (size_t i = 0; i < test_cases.size(); ++i) {
const auto& tc = test_cases[i];
// Test with buffer storage
{
vTensor tensor_buffer(
context(),
tc.sizes,
tc.dtype,
utils::kBuffer,
tc.layout,
/*allocate_memory = */ false);
// Verify dtype (kChar -> kInt8x4)
vkapi::ScalarType expected_dtype = tc.dtype;
if (tc.dtype == vkapi::kChar) {
expected_dtype = vkapi::kInt8x4;
}
ASSERT_EQ(tensor_buffer.dtype(), expected_dtype)
<< "Test case " << i << ": Buffer dtype mismatch";
// Verify dim_order
ASSERT_TRUE(tensor_buffer.dim_order() == tc.expected_dim_order)
<< "Test case " << i << ": Buffer dim_order mismatch"
<< " (expected size: " << tc.expected_dim_order.size()
<< ", actual size: " << tensor_buffer.dim_order().size() << ")";
// Verify padded_sizes
ASSERT_TRUE(
tensor_buffer.padded_sizes() == tc.expected_padded_sizes_buffer)
<< "Test case " << i << ": Buffer padded_sizes mismatch";
// Verify strides (only for float dtype)
if (tc.dtype == vkapi::kFloat && !tc.expected_strides_buffer.empty()) {
ASSERT_TRUE(tensor_buffer.strides() == tc.expected_strides_buffer)
<< "Test case " << i << ": Buffer strides mismatch";
}
// Verify physical_numel
ASSERT_EQ(
tensor_buffer.physical_numel(), tc.expected_physical_numel_buffer)
<< "Test case " << i << ": Buffer physical_numel mismatch";
}
// Test with texture storage
{
vTensor tensor_texture(
context(),
tc.sizes,
tc.dtype,
utils::kTexture3D,
tc.layout,
/*allocate_memory = */ false);
// Verify dtype (kChar -> kInt8x4)
vkapi::ScalarType expected_dtype = tc.dtype;
if (tc.dtype == vkapi::kChar) {
expected_dtype = vkapi::kInt8x4;
}
ASSERT_EQ(tensor_texture.dtype(), expected_dtype)
<< "Test case " << i << ": Texture dtype mismatch";
// Verify dim_order (texture doesn't use dim_order, but it's still
// computed)
ASSERT_TRUE(tensor_texture.dim_order() == tc.expected_dim_order)
<< "Test case " << i << ": Texture dim_order mismatch";
// Verify padded_sizes
ASSERT_TRUE(
tensor_texture.padded_sizes() == tc.expected_padded_sizes_texture)
<< "Test case " << i << ": Texture padded_sizes mismatch";
// Verify axis_map
std::vector<int64_t> expected_axis_map = {0, 1, 2, 2};
ASSERT_TRUE(tensor_texture.axis_map() == expected_axis_map)
<< "Test case " << i << ": Texture axis_map mismatch";
// Verify physical_numel
ASSERT_EQ(
tensor_texture.physical_numel(), tc.expected_physical_numel_texture)
<< "Test case " << i << ": Texture physical_numel mismatch";
// Verify image_extents
ASSERT_EQ(tensor_texture.image_extents(), tc.expected_image_extents)
<< "Test case " << i << ": Texture image_extents mismatch"
<< " (expected: [" << tc.expected_image_extents[0] << ", "
<< tc.expected_image_extents[1] << ", "
<< tc.expected_image_extents[2] << "], got: ["
<< tensor_texture.image_extents()[0] << ", "
<< tensor_texture.image_extents()[1] << ", "
<< tensor_texture.image_extents()[2] << "])";
}
}
}
// Test that texture-backed tensors can serve all metadata UBO requests
// (sizes, strides, dim_order, numel, logical_limits) without exceeding the
// pre-allocated UBO budget. This is a regression test for an issue where
// calculate_max_ubo_nbytes() only allocated 2 fields for texture tensors
// (sizes + logical_limits), but operators like Linear/MatMul unconditionally
// request strides_ubo() and numel_ubo() on all tensors regardless of storage
// type, causing an assertion failure:
// "Uniform data allocation has exceeded Tensor uniform buffer size"
TEST_F(VulkanComputeAPITest, texture_tensor_ubo_metadata_budget_test) {
// Create a texture-backed tensor (the default for most Vulkan ops)
std::vector<int64_t> sizes = {4, 8, 8};
vTensor texture_tensor = vTensor(
context(),
sizes,
vkapi::kFloat,
utils::StorageType::TEXTURE_3D,
utils::GPUMemoryLayout::TENSOR_CHANNELS_PACKED);
// These two UBOs are within the original 2-field texture budget:
// Field 1: sizes (ivec4)
EXPECT_NO_THROW(texture_tensor.sizes_ubo());
// Field 2: logical_limits (uvec3)
EXPECT_NO_THROW(texture_tensor.logical_limits_ubo());
// These UBOs exceed the original 2-field texture budget but are
// unconditionally requested by ops like Linear, MatMul, etc.
// Without the fix, these will trigger:
// VK_CHECK_COND((uniforms_size_ + ubo_nbytes) <= max_ubo_nbytes_)
// Field 3: strides (ivec4) - FAILS without fix
EXPECT_NO_THROW(texture_tensor.strides_ubo());
// Field 4: numel (int32) - FAILS without fix
EXPECT_NO_THROW(texture_tensor.numel_ubo());
// Field 5: dim_order (ivec4) - FAILS without fix
EXPECT_NO_THROW(texture_tensor.dim_order_ubo());
// Also verify a buffer-backed tensor still works (should always have had
// enough budget for all 4+ fields)
vTensor buffer_tensor = vTensor(
context(),
sizes,
vkapi::kFloat,
utils::StorageType::BUFFER,
utils::GPUMemoryLayout::TENSOR_CHANNELS_PACKED);
EXPECT_NO_THROW(buffer_tensor.sizes_ubo());
EXPECT_NO_THROW(buffer_tensor.strides_ubo());
EXPECT_NO_THROW(buffer_tensor.dim_order_ubo());
EXPECT_NO_THROW(buffer_tensor.numel_ubo());
}
TEST_F(VulkanComputeAPITest, view_of_view_test) {
constexpr int N = 3;
constexpr int C = 5;
constexpr int H = 17;
constexpr int W = 19;
std::vector<int64_t> sizes = {N, C, H, W};
vTensor t1 = vTensor(
context(), sizes, vkapi::kFloat, utils::kTexture3D, utils::kWidthPacked);
vTensor t2 = vTensor(t1);
EXPECT_TRUE(t2.sizes() == sizes);
vTensor t3 = vTensor(t2);
EXPECT_TRUE(t2.sizes() == sizes);
t2.virtual_transpose(1, 2);
std::vector<int64_t> expected_t2_sizes = {N, H, C, W};
EXPECT_TRUE(t2.sizes() == expected_t2_sizes);
// Because t3 was created before t2's metadata was updated, we need to first
// update t3's metadata to match t2's metadata. Then the transpose will yield
// the correct metadata.
t3.virtual_clone(t2);
t3.virtual_transpose(2, 3);
std::vector<int64_t> expected_t3_sizes = {N, H, W, C};