-
Notifications
You must be signed in to change notification settings - Fork 1
/
row_col_quant.cpp
1663 lines (1394 loc) · 64 KB
/
row_col_quant.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
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
#include <oneapi/dpl/execution>
#include <oneapi/dpl/algorithm>
#include <sycl/sycl.hpp>
#include <dpct/dpct.hpp>
#include <dpct/dpl_utils.hpp>
#include <dpct/dpl_extras/dpcpp_extensions.h>
#include <dpct/lib_common_utils.hpp>
#include "oneapi/dnnl/dnnl.hpp"
#define ERR_NOT_IMPLEMENTED 100
#define HLF_MAX 65504
#define TH 512
#define NUM 4
#define NUM_BLOCK 512
#define THREADS_ESTIMATE 512
#define NUM_ESTIMATE 8
#define BLOCK_ESTIMATE 512
#define NUM_PER_THREAD 4
#define STATS_THREADS 64
#define STATS_ITEMS 4
#define STATS_ROWS 16
using namespace dnnl;
typedef sycl::ext::oneapi::bfloat16 bf16;
typedef sycl::local_accessor<uint8_t ,1> sycl_la;
typedef sycl::accessor<int, 1> sycl_dacc;
typedef sycl::accessor<float, 1> sycl_dacc_float;
typedef sycl::accessor<unsigned char, 1> sycl_dacc_uc;
typedef sycl::accessor<char, 1> sycl_dacc_char;
template <typename T> __dpct_inline__ int sgn(T val)
{
return (T(0) < val) - (val < T(0));
}
int fill_up_to_nearest_multiple(int value, int multiple)
{
return value + (value % multiple == 0 ? 0 : (multiple - (value % multiple)));
}
//Cub Helpers provided for reference -> already in dpcpp_extensions.h
/// Load linear segment items into block format across threads
/// Helper for Block Load
namespace dpct_{
namespace group{
enum load_algorithm {
BLOCK_LOAD_DIRECT,
BLOCK_LOAD_STRIPED,
// To-do: BLOCK_LOAD_WARP_TRANSPOSE
};
// loads a linear segment of workgroup items into a blocked arrangement.
template <size_t ITEMS_PER_WORK_ITEM, typename InputT,
typename InputIteratorT, typename Item>
__dpct_inline__ void load_blocked(const Item &item, InputIteratorT block_itr,
InputT (&items)[ITEMS_PER_WORK_ITEM]) {
// This implementation does not take in account range loading across
// workgroup items To-do: Decide whether range loading is required for group
// loading
size_t linear_tid = item.get_local_linear_id();
int ltid = int(linear_tid);
uint32_t workgroup_offset = linear_tid * ITEMS_PER_WORK_ITEM;
//static const CONSTANT char FMT[] = "n: %u\n";
//sycl::ext::oneapi::experimental::printf(FMT,ltid);
#pragma unroll
for (size_t idx = 0; idx < ITEMS_PER_WORK_ITEM; idx++) {
items[idx] = block_itr[workgroup_offset + idx];
}
}
// loads a linear segment of workgroup items into a striped arrangement.
template <size_t ITEMS_PER_WORK_ITEM, typename InputT,
typename InputIteratorT, typename Item>
__dpct_inline__ void load_striped(const Item &item, InputIteratorT block_itr,
InputT (&items)[ITEMS_PER_WORK_ITEM]) {
// This implementation does not take in account range loading across
// workgroup items To-do: Decide whether range loading is required for group
// loading
size_t linear_tid = item.get_local_linear_id();
size_t group_work_items = item.get_local_range().size();
//static const CONSTANT char FMT[] = "n: %u\n";
//sycl::ext::oneapi::experimental::printf(FMT,linear_tid);
//sycl::ext::oneapi::experimental::printf("y: %u\n",group_work_items);
//sycl::ext::oneapi::experimental::printf("items_per_wi: %u\n",ITEMS_PER_WORK_ITEM);
#pragma unroll
for (size_t idx = 0; idx < ITEMS_PER_WORK_ITEM; idx++) {
items[idx] = block_itr[linear_tid + (idx * group_work_items)];
}
}
// loads a linear segment of workgroup items into a subgroup striped
// arrangement. Created as free function until exchange mechanism is
// implemented.
// To-do: inline this function with BLOCK_LOAD_WARP_TRANSPOSE mechanism
template <size_t ITEMS_PER_WORK_ITEM, typename InputT, typename InputIteratorT,
typename Item>
__dpct_inline__ void
uninitialized_load_subgroup_striped(const Item &item, InputIteratorT block_itr,
InputT (&items)[ITEMS_PER_WORK_ITEM]) {
// This implementation does not take in account range loading across
// workgroup items To-do: Decide whether range loading is required for group
// loading
// This implementation uses unintialized memory for loading linear segments
// into warp striped arrangement.
uint32_t subgroup_offset = item.get_sub_group().get_local_linear_id();
uint32_t subgroup_size = item.get_sub_group().get_local_linear_range();
uint32_t subgroup_idx = item.get_sub_group().get_group_linear_id();
uint32_t initial_offset =
(subgroup_idx * ITEMS_PER_WORK_ITEM * subgroup_size) + subgroup_offset;
#pragma unroll
for (size_t idx = 0; idx < ITEMS_PER_WORK_ITEM; idx++) {
new (&items[idx]) InputT(block_itr[initial_offset + (idx * subgroup_size)]);
}
}
template <size_t ITEMS_PER_WORK_ITEM, load_algorithm ALGORITHM, typename InputT,
typename InputIteratorT, typename Item>
class workgroup_load {
public:
static size_t get_local_memory_size(size_t group_work_items) { return 0; }
workgroup_load(uint8_t *local_memory) : _local_memory(local_memory) {}
__dpct_inline__ void load(const Item &item, InputIteratorT block_itr,
InputT (&items)[ITEMS_PER_WORK_ITEM]) {
if constexpr (ALGORITHM == dpct_::group::load_algorithm::BLOCK_LOAD_DIRECT) {
//sycl::ext::oneapi::experimental::printf(" in direct ");
load_blocked<ITEMS_PER_WORK_ITEM, InputT>(item, block_itr, items);
} if constexpr (ALGORITHM == BLOCK_LOAD_STRIPED) {
//sycl::ext::oneapi::experimental::printf(" in striped ");
load_striped<ITEMS_PER_WORK_ITEM, InputT>(item, block_itr, items);
}
}
private:
uint8_t *_local_memory;
};
enum store_algorithm {
BLOCK_STORE_DIRECT,
BLOCK_STORE_STRIPED,
// To-do: BLOCK_STORE_WARP_TRANSPOSE
// To-do: BLOCK_STORE_VECTORIZE
};
/// Stores a blocked arrangement of work items linear segment of items.
template <size_t ITEMS_PER_WORK_ITEM, typename InputT,
typename OutputIteratorT, typename Item>
__dpct_inline__ void store_blocked(const Item &item, OutputIteratorT block_itr,
InputT (&items)[ITEMS_PER_WORK_ITEM]) {
// This implementation does not take in account range storage across
// workgroup items To-do: Decide whether range storage is required for group
// storage
size_t linear_tid = item.get_local_linear_id();
OutputIteratorT workitem_itr = block_itr + (linear_tid * ITEMS_PER_WORK_ITEM);
#pragma unroll
for (uint32_t idx = 0; idx < ITEMS_PER_WORK_ITEM; idx++) {
workitem_itr[idx] = items[idx];
}
}
/// Stores a striped arrangement of work items linear segment of items.
template <size_t ITEMS_PER_WORK_ITEM, typename InputT,
typename OutputIteratorT, typename Item>
__dpct_inline__ void store_striped(const Item &item, OutputIteratorT block_itr,
InputT (&items)[ITEMS_PER_WORK_ITEM]) {
// This implementation does not take in account range storage across
// workgroup items To-do: Decide whether range storage is required for group
// storage
size_t linear_tid = item.get_local_linear_id();
OutputIteratorT workitem_itr = block_itr + linear_tid;
size_t GROUP_WORK_ITEMS = item.get_global_range().size();
#pragma unroll
for (uint32_t idx = 0; idx < ITEMS_PER_WORK_ITEM; idx++) {
workitem_itr[(idx * GROUP_WORK_ITEMS)] = items[idx];
}
}
/// Stores a warp-striped arrangement of work items linear segment of items.
// Created as free function until exchange mechanism is
// implemented.
// To-do: inline this function with BLOCK_STORE_WARP_TRANSPOSE mechanism
template <size_t ITEMS_PER_WORK_ITEM, typename InputT, typename OutputIteratorT,
typename Item>
__dpct_inline__ void
store_subgroup_striped(const Item &item, OutputIteratorT block_itr,
InputT (&items)[ITEMS_PER_WORK_ITEM]) {
// This implementation does not take in account range loading across
// workgroup items To-do: Decide whether range loading is required for group
// loading
// This implementation uses unintialized memory for loading linear segments
// into warp striped arrangement.
uint32_t subgroup_offset = item.get_sub_group().get_local_linear_id();
uint32_t subgroup_size = item.get_sub_group().get_local_linear_range();
uint32_t subgroup_idx = item.get_sub_group().get_group_linear_id();
uint32_t initial_offset =
(subgroup_idx * ITEMS_PER_WORK_ITEM * subgroup_size) + subgroup_offset;
OutputIteratorT workitem_itr = block_itr + initial_offset;
#pragma unroll
for (uint32_t idx = 0; idx < ITEMS_PER_WORK_ITEM; idx++) {
workitem_itr[(idx * subgroup_size)] = items[idx];
}
}
// template parameters :
// ITEMS_PER_WORK_ITEM: size_t variable controlling the number of items per
// thread/work_item
// ALGORITHM: store_algorithm variable controlling the type of store operation.
// InputT: type for input sequence.
// OutputIteratorT: output iterator type
// Item : typename parameter resembling sycl::nd_item<3> .
template <size_t ITEMS_PER_WORK_ITEM, store_algorithm ALGORITHM, typename InputT,
typename OutputIteratorT, typename Item>
class workgroup_store {
public:
static size_t get_local_memory_size(size_t group_work_items) { return 0; }
workgroup_store(uint8_t *local_memory) : _local_memory(local_memory) {}
__dpct_inline__ void store(const Item &item, OutputIteratorT block_itr,
InputT (&items)[ITEMS_PER_WORK_ITEM]) {
if constexpr (ALGORITHM == BLOCK_STORE_DIRECT) {
store_blocked<ITEMS_PER_WORK_ITEM>(item, block_itr, (&items)[ITEMS_PER_WORK_ITEM]);
} else if constexpr (ALGORITHM == BLOCK_STORE_STRIPED) {
store_striped<ITEMS_PER_WORK_ITEM>(item, block_itr, (&items)[ITEMS_PER_WORK_ITEM]);
}
}
private:
uint8_t *_local_memory;
};
}
}
template <typename T, int VALUES_PER_THREAD> class exchange {
public:
static size_t get_local_memory_size(size_t group_threads) {
size_t padding_values =
(INSERT_PADDING)
? ((group_threads * VALUES_PER_THREAD) >> LOG_LOCAL_MEMORY_BANKS)
: 0;
return (group_threads * VALUES_PER_THREAD + padding_values) * sizeof(T);
}
exchange(uint8_t *local_memory) : _local_memory(local_memory) {}
public:
// TODO: Investigate if padding is required for performance,
// and if specializations are required for specific target hardware.
static size_t adjust_by_padding(size_t offset) {
if constexpr (INSERT_PADDING) {
offset = dpct::group::detail::shr_add(offset, LOG_LOCAL_MEMORY_BANKS, offset);
}
return offset;
}
struct get_blocked_offset {
template <typename Item> size_t operator()(Item item, size_t i) {
size_t offset = item.get_local_id(0) * VALUES_PER_THREAD + i;
return adjust_by_padding(offset);
}
};
struct get_striped_offset {
template <typename Item> size_t operator()(Item item, size_t i) {
size_t offset = i * item.get_local_range(2) * item.get_local_range(1) *
item.get_local_range(0) +
item.get_local_id(0);
return adjust_by_padding(offset);
}
};
template <typename Iterator> struct get_scatter_offset {
Iterator begin;
get_scatter_offset(const int (&ranks)[VALUES_PER_THREAD]) { begin = ranks; }
template <typename Item> size_t operator()(Item item, size_t i) const {
// iterator i is expected to be within bounds [0,VALUES_PER_THREAD)
return adjust_by_padding(begin[i]);
}
};
template <typename Item, typename offsetFunctorTypeFW,
typename offsetFunctorTypeRV>
__dpct_inline__ void helper_exchange(Item item, T (&keys)[VALUES_PER_THREAD],
offsetFunctorTypeFW &offset_functor_fw,
offsetFunctorTypeRV &offset_functor_rv) {
T *buffer = reinterpret_cast<T *>(_local_memory);
#pragma unroll
for (size_t i = 0; i < VALUES_PER_THREAD; i++) {
size_t offset = offset_functor_fw(item, i);
buffer[offset] = keys[i];
}
item.barrier(sycl::access::fence_space::local_space);
#pragma unroll
for (size_t i = 0; i < VALUES_PER_THREAD; i++) {
size_t offset = offset_functor_rv(item, i);
keys[i] = buffer[offset];
}
}
public:
/// Rearrange elements from blocked order to striped order
template <typename Item>
__dpct_inline__ void blocked_to_striped(Item item,
T (&keys)[VALUES_PER_THREAD]) {
get_striped_offset getStripedOffset;
get_blocked_offset getBlockedOffset;
helper_exchange(item, keys, getStripedOffset, getBlockedOffset);
}
/// Rearrange elements from striped order to blocked order
template <typename Item>
__dpct_inline__ void striped_to_blocked(Item item,
T (&keys)[VALUES_PER_THREAD]) {
get_blocked_offset getBlockedOffset;
get_striped_offset getStripedOffset;
helper_exchange(item, keys, getBlockedOffset, getStripedOffset);
}
/// Rearrange elements from rank order to blocked order
template <typename Item>
__dpct_inline__ void scatter_to_blocked(Item item,
T (&keys)[VALUES_PER_THREAD],
int (&ranks)[VALUES_PER_THREAD]) {
get_scatter_offset<int *> getScatterOffset(ranks);
get_blocked_offset getBlockedOffset;
helper_exchange(item, keys, getScatterOffset, getBlockedOffset);
}
/// Rearrange elements from scatter order to striped order
template <typename Item>
__dpct_inline__ void scatter_to_striped(Item item,
T (&keys)[VALUES_PER_THREAD],
int (&ranks)[VALUES_PER_THREAD]) {
get_scatter_offset<int *> getScatterOffset(ranks);
get_striped_offset getStripedOffset;
helper_exchange(item, keys, getScatterOffset, getStripedOffset);
}
private:
static constexpr int LOG_LOCAL_MEMORY_BANKS = 4;
static constexpr bool INSERT_PADDING =
(VALUES_PER_THREAD > 4) &&
(dpct::group::detail::power_of_two<VALUES_PER_THREAD>::VALUE);
uint8_t *_local_memory;
};
template <typename T, int VALUES_PER_THREAD, bool DESCENDING = false>
class radix_sort {
public:
static size_t get_local_memory_size(size_t group_threads) {
size_t ranks_size =
dpct::group::detail::radix_rank<RADIX_BITS>::get_local_memory_size(group_threads);
size_t exchange_size =
dpct::group::exchange<T, VALUES_PER_THREAD>::get_local_memory_size(group_threads);
return sycl::max(ranks_size, exchange_size);
}
radix_sort(uint8_t *local_memory) : _local_memory(local_memory) {}
template <typename Item>
__dpct_inline__ void
helper_sort(const Item &item, T (&keys)[VALUES_PER_THREAD], int begin_bit = 0,
int end_bit = 8 * sizeof(T), bool is_striped = false) {
uint32_t(&unsigned_keys)[VALUES_PER_THREAD] =
reinterpret_cast<uint32_t(&)[VALUES_PER_THREAD]>(keys);
#pragma unroll
for (int i = 0; i < VALUES_PER_THREAD; ++i) {
unsigned_keys[i] = dpct::group::detail::traits<T>::twiddle_in(unsigned_keys[i]);
}
for (int i = begin_bit; i < end_bit; i += RADIX_BITS) {
int pass_bits = sycl::min(RADIX_BITS, end_bit - begin_bit);
int ranks[VALUES_PER_THREAD];
dpct::group::detail::radix_rank<RADIX_BITS, DESCENDING>(_local_memory)
.template rank_keys(item, unsigned_keys, ranks, i, pass_bits);
item.barrier(sycl::access::fence_space::local_space);
bool last_iter = i + RADIX_BITS > end_bit;
if (last_iter && is_striped) {
dpct::group::exchange<T, VALUES_PER_THREAD>(_local_memory)
.scatter_to_striped(item, keys, ranks);
} else {
dpct::group::exchange<T, VALUES_PER_THREAD>(_local_memory)
.scatter_to_blocked(item, keys, ranks);
}
item.barrier(sycl::access::fence_space::local_space);
}
#pragma unroll
for (int i = 0; i < VALUES_PER_THREAD; ++i) {
unsigned_keys[i] = dpct::group::detail::traits<T>::twiddle_out(unsigned_keys[i]);
}
}
template <typename Item>
__dpct_inline__ void
sort_blocked(const Item &item, T (&keys)[VALUES_PER_THREAD],
int begin_bit = 0, int end_bit = 8 * sizeof(T)) {
helper_sort(item, keys, begin_bit, end_bit, false);
}
template <typename Item>
__dpct_inline__ void
sort_blocked_to_striped(const Item &item, T (&keys)[VALUES_PER_THREAD],
int begin_bit = 0, int end_bit = 8 * sizeof(T)) {
helper_sort(item, keys, begin_bit, end_bit, true);
}
private:
static constexpr int RADIX_BITS = 4;
uint8_t *_local_memory;
};
//=====================================================
#define FLT_MAX std::numeric_limits<float>::max()
#define FLT_MIN std::numeric_limits<float>::min()
//================================helpers===========================
// source: https://stackoverflow.com/questions/17399119/how-do-i-use-atomicmax-on-floating-point-values-in-cuda
float atomicMax(float* address, float val) {
int* address_as_i = reinterpret_cast<int*>(address);
int old = *address_as_i, assumed;
do {
assumed = old;
old = dpct::atomic_compare_exchange_strong<sycl::access::address_space::generic_space>(reinterpret_cast<int*>(address), assumed, sycl::bit_cast<int>(sycl::fmax(val, sycl::bit_cast<float>(assumed))));
} while (assumed != old);
return sycl::bit_cast<float>(old);
}
float atomicMin(float* address, float val) {
int* address_as_i = reinterpret_cast<int*>(address);
int old = *address_as_i, assumed;
do {
assumed = old;
old = dpct::atomic_compare_exchange_strong<sycl::access::address_space::generic_space>(reinterpret_cast<int*>(address), assumed, sycl::bit_cast<int>(sycl::fmin(val, sycl::bit_cast<float>(assumed))));
} while (assumed != old);
return sycl::bit_cast<float>(old);
}
float dDequantizeFP4(unsigned char val, float absmax)
{
float sign = (val & 0b1000) == 8 ? -1.0f : 1.0f;
if((val & 0b0110) == 0)
{
// subnormal
if((val & 0b0001) == 0)
return 0.0f;
else
return sign*0.0625f*absmax;
}
else
{
// normal
float exponent = ((val & 0b0100) == 4 ? 2.0f : 8.0f) + ((val & 0b0010) == 2 ? 0.0f : 2.0f);
float fraction = (val & 0b0001) == 1 ? 1.5f : 1.0f;
return sign*exponent*fraction*absmax;
}
}
float d2DequantizeFP4(unsigned char val)
{
float sign = (val & 0b1000) == 8 ? -1.0f : 1.0f;
if((val & 0b0110) == 0)
{
// subnormal
if((val & 0b0001) == 0)
return 0.0f;
else
return sign*0.0625f;
}
else
{
// normal
float exponent = ((val & 0b0100) == 4 ? 2.0f : 8.0f) + ((val & 0b0010) == 2 ? 0.0f : 2.0f);
float fraction = (val & 0b0001) == 1 ? 1.5f : 1.0f;
return sign*exponent*fraction;
}
}
float dDequantizeFP4Tree(unsigned char val, float absmax)
{
float sign = (val & 0b1000) == 8 ? -1.0f : 1.0f;
if((val & 0b0100) == 4) // 0
if((val & 0b0010) == 2) //01
if((val & 0b0001) == 1) // 111
return 0.25000000f*absmax*sign; // 1111
else
return 0.16666667f*absmax*sign; // 1110
else
if((val & 0b0001) == 1) // 110
return 0.50000000f*absmax*sign; // 1101
else
return 0.33333333f*absmax*sign; // 1100
else
if((val & 0b0010) == 2) //10
if((val & 0b0001) == 1) // 101
return 1.00000000f*absmax*sign; // 1011
else
return 0.66666667f*absmax*sign; // 1010
else
if((val & 0b0001) == 1) // 100
return 5.208333333e-03f*absmax*sign; // 1001
else
return 0.00000000f*absmax*sign; // 1000
}
unsigned char dQuantizeFP4(float x)
{
// FP4 with bias of 3
// first bit is a sign
// subnormals
// 0b000 = 0
// 0b001 = 0.0625
// 0b110 = 2
// 0b111 = 3
// 0b100 = 4
// 0b101 = 6
// 0b010 = 8
// 0b011 = 12
// we do a binary search
// the pivots are divided by 12 (the FP4 absmax)
// since we assume input data is in [-1.0, 1.0]
// !be careful here, its easy to make a mistake
// that is difficult to notice if you add an extra
// zero somewhere!
int sign = x < 0 ? 0b1000 : 0b0000;
x = sycl::fabs(x);
if(x > 0.29166667f)
if( x > 0.583333f)
if( x > 0.8333333f)
return 0b0011+sign;
else
return 0b0010+sign;
else
if(x > 0.4166667f)
return 0b101+sign;
else
return 0b100+sign;
else
if(x > 0.0859375f)
if(x > 0.20833333f)
return 0b0111+sign;
else
return 0b0110+sign;
else
if(x > 0.00260417f)
return 0b0001+sign;
else
return 0b0000+sign;
}
sycl::half dhDequantizeNF4(unsigned char val)
{
// the values for this tree was generated by test_normal_map_tree
// in the file tests/test_functional.py
if((val & 0b1000) == 8)
if((val & 0b0100) == 4) // 1
if((val & 0b0010) == 2) // 11
if((val & 0b0001) == 1) // 111
return 1.0f;
else
return 0.7229568362236023f;
else
if((val & 0b0001) == 1) // 110
return 0.5626170039176941f;
else
return 0.44070982933044434f;
else
if((val & 0b0010) == 2) //10
if((val & 0b0001) == 1) // 101
return 0.33791524171829224f;
else
return 0.24611230194568634f;
else
if((val & 0b0001) == 1) // 100
return 0.16093020141124725f;
else
return 0.07958029955625534f;
else
if((val & 0b0100) == 4) // 0
if((val & 0b0010) == 2) //01
if((val & 0b0001) == 1) // 011
return 0.0f;
else
return -0.09105003625154495f;
else
if((val & 0b0001) == 1) // 010
return -0.18477343022823334f;
else
return -0.28444138169288635f;
else
if((val & 0b0010) == 2) //00
if((val & 0b0001) == 1) // 001
return -0.39491748809814453f;
else
return -0.5250730514526367f;
else
if((val & 0b0001) == 1) // 000
return -0.6961928009986877f;
else
return -1.0f;
}
float dDequantizeNF4(unsigned char val)
{
// the values for this tree was generated by test_normal_map_tree
// in the file tests/test_functional.py
if((val & 0b1000) == 8)
if((val & 0b0100) == 4) // 1
if((val & 0b0010) == 2) // 11
if((val & 0b0001) == 1) // 111
return 1.0f;
else
return 0.7229568362236023f;
else
if((val & 0b0001) == 1) // 110
return 0.5626170039176941f;
else
return 0.44070982933044434f;
else
if((val & 0b0010) == 2) //10
if((val & 0b0001) == 1) // 101
return 0.33791524171829224f;
else
return 0.24611230194568634f;
else
if((val & 0b0001) == 1) // 100
return 0.16093020141124725f;
else
return 0.07958029955625534f;
else
if((val & 0b0100) == 4) // 0
if((val & 0b0010) == 2) //01
if((val & 0b0001) == 1) // 011
return 0.0f;
else
return -0.09105003625154495f;
else
if((val & 0b0001) == 1) // 010
return -0.18477343022823334f;
else
return -0.28444138169288635f;
else
if((val & 0b0010) == 2) //00
if((val & 0b0001) == 1) // 001
return -0.39491748809814453f;
else
return -0.5250730514526367f;
else
if((val & 0b0001) == 1) // 000
return -0.6961928009986877f;
else
return -1.0f;
}
unsigned char dQuantizeNF4(float x)
{
// the values for this tree was generated by test_normal_map_tree
// in the file tests/test_functional.py
if(x > 0.03979014977812767f)
if(x > 0.3893125355243683f) // 1
if(x > 0.6427869200706482f) // 11
if(x > 0.8614784181118011f) // 111
return 0b1111;
else
return 0b1110;
else
if(x > 0.5016634166240692f) // 110
return 0b1101;
else
return 0b1100;
else
if(x > 0.2035212516784668f) // 10
if(x > 0.2920137718319893f) // 101
return 0b1011;
else
return 0b1010;
else
if(x > 0.1202552504837513f) // 100
return 0b1001;
else
return 0b1000;
else
if(x > -0.33967943489551544f) // 0
if(x > -0.13791173323988914f) // 01
if(x > -0.045525018125772476f) // 011
return 0b0111;
else
return 0b0110;
else
if(x > -0.23460740596055984f) // 010
return 0b0101;
else
return 0b0100;
else
if(x > -0.6106329262256622f) // 00
if(x > -0.4599952697753906f) // 001
return 0b0011;
else
return 0b0010;
else
if(x > -0.8480964004993439f) // 000
return 0b0001;
else
return 0b0000;
}
// sign function for lion
// taken from https://stackoverflow.com/a/4609795, but not sure if there's a proper way to do this in CUDA
template <int STOCHASTIC>
unsigned char dQuantize(float* smem_code, const float rand, float x)
{
int pivot = 127;
int upper_pivot = 255;
int lower_pivot = 0;
float lower = -1.0f;
float upper = 1.0f;
float val = smem_code[pivot];
// i>>=1 = {32, 16, 8, 4, 2, 1}
for(int i = 64; i > 0; i>>=1)
{
if(x > val)
{
lower_pivot = pivot;
lower = val;
pivot+=i;
}
else
{
upper_pivot = pivot;
upper = val;
pivot-=i;
}
val = smem_code[pivot];
}
if(upper_pivot == 255)
upper = smem_code[upper_pivot];
if(lower_pivot == 0)
lower = smem_code[lower_pivot];
if(!STOCHASTIC)
{
if(x > val)
{
float midpoint = (upper+val)*0.5f;
if(x > midpoint)
{
return upper_pivot;
}
else
return pivot;
}
else
{
float midpoint = (lower+val)*0.5f;
if(x < midpoint)
return lower_pivot;
else
return pivot;
}
}
else
{
if(x > val)
{
float dist_to_upper = sycl::fabs(upper-x);
float dist_full = upper-val;
if(rand >= dist_to_upper/dist_full) return upper_pivot;
else return pivot;
}
else
{
float dist_to_lower = sycl::fabs(lower-x);
float dist_full = val-lower;
if(rand >= dist_to_lower/dist_full) return lower_pivot;
else return pivot;
}
}
}
//========================helper-==============================
#define NUM8BIT 16
#define NUM_THREADS 512
#define NUM_PER_BLOCK 512
//=====================================k double row col quant============================
template <int THREADS, int ITEMS_PER_THREAD, int TILE_ROWS, int TILE_COLS, int SPARSE_DECOMP> void kDoubleRowColQuant(sycl::half *__restrict__ const A, float *__restrict__ const rowStats, float * __restrict__ const colStats, char *out_col_normed, char *out_row_normed, int *rowidx, int *colidx, sycl::half *val, int * __restrict__ nnz_block_ptr, float threshold, int rows, int cols, int tiledCols, const sycl::nd_item<3> &item_ct1, float *smem_row_stats, unsigned int *smem_nnz_row_idx, const sycl_la &tacc, const sycl::accessor<sycl::half, 1> &dacc_A, const sycl_dacc_char &dacc_out_col_normed, const sycl_dacc_char &dacc_out_row_normed, const sycl_dacc_float &dacc_rowStats, const sycl_dacc_float &dacc_colStats, const sycl_dacc &dacc_rowidx, const sycl_dacc &dacc_colidx, const sycl::accessor<sycl::half, 1> &dacc_val, const sycl_dacc &dacc_nnz_block_ptr)
{
// assumes TILE_SIZE == THREADS*ITEMS_PER_THREAD
// Each thread reads the same column but multiple rows
// Rows are loaded in shared memory and access is shared across the threadblock (broadcast)
// 0. Load row stats data into shared memory; load col stat (1 fixed per thread)
// 1. Load data row by row (should be at least with TILE_SIZE = 512)
// 2. quantize data with row/col stats
// 3. Store data (TILE_SIZE = 512 is a bit slow, but should still be close enough to good performance)
// each block loads TILE_COLs columns and TILE_ROW rows
// after reading a tile the row counter increase by TILE_ROWS
// the col counter reset after reading TILE_COL elements
const int base_row = ((item_ct1.get_group(2)*TILE_COLS)/tiledCols)*TILE_ROWS;
// col increases by TILE_SIZE for each block and wraps back to 0 after tiledCols is reached
const int base_col = (item_ct1.get_group(2)*TILE_COLS) % tiledCols;
const int base_idx = (base_row*cols) + base_col;
const int items_per_load = ITEMS_PER_THREAD*THREADS;
//colStats ,rowStats rowidx, colidx,val ,nnz
using group_load_half = dpct_::group::workgroup_load<ITEMS_PER_THREAD, dpct_::group::load_algorithm::BLOCK_LOAD_DIRECT, sycl::half, sycl::half *, sycl::nd_item<3>>;
using group_store_char = dpct_::group::workgroup_store<ITEMS_PER_THREAD, dpct_::group::store_algorithm::BLOCK_STORE_DIRECT, char, char *, sycl::nd_item<3>>;
auto *d_A = dacc_A.get_multi_ptr<sycl::access::decorated::yes>().get();
auto *d_out_col_normed = dacc_out_col_normed.get_multi_ptr<sycl::access::decorated::yes>().get();
auto *d_out_row_normed = dacc_out_row_normed.get_multi_ptr<sycl::access::decorated::yes>().get();
auto *tmp = tacc.get_multi_ptr<sycl::access::decorated::yes>().get();
sycl::half local_data[ITEMS_PER_THREAD];
float local_col_stats[ITEMS_PER_THREAD];
char local_quantized_data[ITEMS_PER_THREAD];
// 0. Load row stats data into shared memory; load col stat (1 fixed per thread)
#pragma unroll ITEMS_PER_THREAD
for(int j = 0; j < ITEMS_PER_THREAD; j++)
if(base_col+(item_ct1.get_local_id(2)*ITEMS_PER_THREAD) + j < cols)
/*
To-do: __fdividef call is used in a macro/template definition and may not be valid for all macro/template uses.
*/
local_col_stats[j] = 127.0f / dacc_colStats[base_col+(item_ct1.get_local_id(2)*ITEMS_PER_THREAD)+j];
for(int i = item_ct1.get_local_id(2); i < TILE_ROWS; i+=item_ct1.get_local_range(2))
{
if(base_row + i < rows)
smem_row_stats[i] = dacc_rowStats[base_row+i];
if(SPARSE_DECOMP)
smem_nnz_row_idx[i] = dacc_nnz_block_ptr[(TILE_ROWS*item_ct1.get_group(2)) + i];
}
item_ct1.barrier(sycl::access::fence_space::local_space);
// we load row after row from the base_position
// 1. Load data row by row (should be at least with TILE_SIZE = 512)
for(int row = 0; row < TILE_ROWS; row++)
{
if(base_row + row >= rows){ break; }
int i = base_idx + (row*cols);
int valid_items = cols - base_col > items_per_load ? items_per_load : cols - base_col;
// 1. load 8 values per thread
// 2. compute 2-max in registers (64 max per warp)
// 3. do warp reduction + broadcast back
// 4. Up-shift maxed value, write index into shared memory, replace with 2nd largest
// 5. Repeat (3) 8 times for top 8 values in 256
// 6. store with byte index
group_load_half(tmp).load(item_ct1, d_A, local_data);
float row_stat = 127.0f / smem_row_stats[row];
// 2. quantize data with row/col stats
#pragma unroll ITEMS_PER_THREAD
for(int j = 0; j < ITEMS_PER_THREAD; j++)
{
// we already pre-normalized the col/row stat:
// what this does is float/absmax*127 = int8
if(SPARSE_DECOMP)
{
if(sycl::fabs((float)local_data[j]) >= threshold)
{
local_quantized_data[j] = 0;
int old_idx = dpct::atomic_fetch_compare_inc<sycl::access::address_space::generic_space>(&smem_nnz_row_idx[row], UINT_MAX);
dacc_rowidx[old_idx] = base_row+row;
dacc_colidx[old_idx] = base_col+(item_ct1.get_local_id(2)*ITEMS_PER_THREAD)+j;
dacc_val[old_idx] = local_data[j];
}
else
{
local_quantized_data[j] = (char)(sycl::rint(sycl::vec<sycl::half, 1>(local_data[j]).convert<float, sycl::rounding_mode::automatic>()[0]*row_stat));
}
}
else
local_quantized_data[j] = (char)(sycl::rint(sycl::vec<sycl::half, 1>(local_data[j]).convert<float, sycl::rounding_mode::automatic>()[0]*row_stat));
}
// 1. load 8 values per thread
// 2. compute 2-max in registers (64 max per warp)
// 3. do warp reduction + broadcast back
// 4. Up-shift maxed value, write index into shared memory, replace with 2nd largest
// 5. Repeat (3) 8 times for top 8 values in 256
// 6. store with byte index
group_store_char(tmp).store(item_ct1, d_out_row_normed, local_quantized_data);
// 2. quantize data with row/col stats
#pragma unroll ITEMS_PER_THREAD
for(int j = 0; j < ITEMS_PER_THREAD; j++)
{
// we already pre-normalized the col/row stat:
// what this does is float/absmax*127 = int8
local_quantized_data[j] = (char)(sycl::rint(sycl::vec<sycl::half, 1>(local_data[j]).convert<float, sycl::rounding_mode::automatic>()[0]*local_col_stats[j]));
}
item_ct1.barrier(sycl::access::fence_space::local_space);
//StoreInt8(storeint8).Store(&(out_col_normed[i]), local_quantized_data, valid_items);
// 1. load 8 values per thread
// 2. compute 2-max in registers (64 max per warp)
// 3. do warp reduction + broadcast back
// 4. Up-shift maxed value, write index into shared memory, replace with 2nd largest
// 5. Repeat (3) 8 times for top 8 values in 256
// 6. store with byte index
group_store_char(tmp).store(item_ct1, d_out_col_normed, local_quantized_data);
}
}
//============================================k transform row format=====================================================
template <int THREADS, int ITEMS_PER_THREAD, int TILE_ROWS, int TILE_COLS, int TRANSPOSE, int FORMAT> SYCL_EXTERNAL void kTransformRowToFormat(char *__restrict__ const A, char *out, int rows, int cols, int tiledCols, int outRows, int outCols, const sycl::nd_item<3> &item_ct1, char *smem_data,
const sycl_dacc_char &dacc_A, const sycl_dacc_char &dacc_out)