-
Notifications
You must be signed in to change notification settings - Fork 12
/
int8kernel_m2.S
648 lines (527 loc) · 15.6 KB
/
int8kernel_m2.S
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
#if __aarch64__
#ifndef __APPLE__
#include "public.h"
#define dst x0
#define pa x1
#define pb x2
#define k x3
#define n x4
#define ldc x5
#ifdef GEMM_REQUANT
#define scales x6
#define bias x7
#endif
#define src_ori x8
#define kd8_ori x9
#define dst_ptr_0 x10
#define dst_ptr_1 x11
#define tmp_ptr_b x12
#define is_kd8_even x13
#define kd4_ori x14
#define kd2_ori x15
#define kd1_ori x16
#define kd x17
#define nd4_ori x18
#define nd2_ori x19
#define nd1_ori x20
#define nd x21
#define temp x22
#define temp1 x23
#define temp2 x24
//void int8kernel_m2(int32_t* dst, const uint8_t* pa, const uint8_t* pb, size_t k, size_t n, ldc);
//void int8kernel_m2_requant(int32_t* dst, const uint8_t* pa, const uint8_t* pb, size_t k, size_t n, ldc, scales, bias);
.macro INIT
eor v8.16b, v8.16b, v8.16b
eor v9.16b, v9.16b, v9.16b
eor v10.16b, v10.16b, v10.16b
eor v11.16b, v11.16b, v11.16b
eor v12.16b, v12.16b, v12.16b
eor v13.16b, v13.16b, v13.16b
eor v14.16b, v14.16b, v14.16b
eor v15.16b, v15.16b, v15.16b
eor v16.16b, v16.16b, v16.16b
eor v17.16b, v17.16b, v17.16b
eor v18.16b, v18.16b, v18.16b
eor v19.16b, v19.16b, v19.16b
eor v20.16b, v20.16b, v20.16b
eor v21.16b, v21.16b, v21.16b
eor v22.16b, v22.16b, v22.16b
eor v23.16b, v23.16b, v23.16b
.endm
.macro DECOMPOSE_K
// divide k into 8*kd8_ori + 4*kd4_ori + 2*kd2_ori + 1*kd1_ori
lsr kd8_ori, k, 3 // kd8_ori = k / 8, means loopk
lsr temp, kd8_ori, 1
lsl is_kd8_even, temp, 1
sub is_kd8_even, kd8_ori, is_kd8_even
lsl temp, kd8_ori, 3
sub k, k, temp
lsr kd4_ori, k, 2
lsl temp, kd4_ori, 2
sub k, k, temp
lsr kd2_ori, k, 1
lsl temp, kd2_ori, 1
sub k, k, temp
mov kd1_ori, k
.endm
.macro DECOMPOSE_N
// divide n into 4*nd4_ori + 2*nd2_ori + 1*nd1_ori
lsr nd4_ori, n, 2
lsl nd, nd4_ori, 2
sub n, n, nd
lsr nd2_ori, n, 1
lsl nd, nd2_ori, 1
sub n, n, nd
mov nd1_ori, n
.endm
.text
.align 5
#ifndef FUNCTION_NAME
#define FUNCTION_NAME int8kernel_m2
#endif
ASM_DECLARE FUNCTION_NAME
SAVE_REGS
#ifndef GEMM_REQUANT // if requant, typeof C = int8_t
lsl ldc, ldc, #2 // else sizeof(int32_t) = 4
#endif
mov dst_ptr_0, dst
add dst_ptr_1, dst_ptr_0, ldc
DECOMPOSE_K
DECOMPOSE_N
cmp nd4_ori, #0
beq nd2_start // means n < 4
loopnd4:
INIT
mov src_ori, pa // PanelA
cmp kd8_ori, #0
beq loopkd4_nd4
mov kd, kd8_ori
cmp is_kd8_even, #0
beq loopkd8_nd4_even // loop number is even
// start loopm2_kd8_nd4
subs kd, kd, #1
ld1 {v4.8b, v5.8b, v6.8b, v7.8b}, [pb], #32 // load four lines of B
ld1 {v2.8b, v3.8b}, [pa], #16 // load two lines of PanelA
smull v0.8h, v4.8b, v2.8b
smull v1.8h, v4.8b, v3.8b
saddlp v8.4s, v0.8h
saddlp v12.4s, v1.8h
smull v0.8h, v5.8b, v2.8b
smull v1.8h, v5.8b, v3.8b
saddlp v9.4s, v0.8h
saddlp v13.4s, v1.8h
smull v0.8h, v6.8b, v2.8b
smull v1.8h, v6.8b, v3.8b
saddlp v10.4s, v0.8h
saddlp v14.4s, v1.8h
smull v0.8h, v7.8b, v2.8b
smull v1.8h, v7.8b, v3.8b
saddlp v11.4s, v0.8h
saddlp v15.4s, v1.8h
cmp kd, #0
beq loopkd8_nd4_end
loopkd8_nd4_even:
add tmp_ptr_b, pb, #32
ld1 {v4.8b, v5.8b}, [pb], #16
ld1 {v2.8b, v3.8b}, [pa], #16
smull v0.8h, v4.8b, v2.8b
smull v1.8h, v5.8b, v2.8b
ld1 {v6.8b, v7.8b}, [tmp_ptr_b], #16
ld1 {v24.8b, v25.8b}, [pa], #16
smlal v0.8h, v6.8b, v24.8b
smlal v1.8h, v7.8b, v24.8b
sadalp v8.4s, v0.8h
sadalp v9.4s, v1.8h
smull v0.8h, v4.8b, v3.8b
smull v1.8h, v5.8b, v3.8b
smlal v0.8h, v6.8b, v25.8b
smlal v1.8h, v7.8b, v25.8b
sadalp v12.4s, v0.8h
sadalp v13.4s, v1.8h
// start v10v11, v14v15, v18v19, v22v23, error here!
ld1 {v4.8b, v5.8b}, [pb], #16
smull v0.8h, v4.8b, v2.8b
smull v1.8h, v5.8b, v2.8b
ld1 {v6.8b, v7.8b}, [tmp_ptr_b], #16
smlal v0.8h, v6.8b, v24.8b
smlal v1.8h, v7.8b, v24.8b
sadalp v10.4s, v0.8h
sadalp v11.4s, v1.8h
smull v0.8h, v4.8b, v3.8b
smull v1.8h, v5.8b, v3.8b
smlal v0.8h, v6.8b, v25.8b
smlal v1.8h, v7.8b, v25.8b
sadalp v14.4s, v0.8h
sadalp v15.4s, v1.8h
add pb, pb, #32
subs kd, kd, #2
bne loopkd8_nd4_even
loopkd8_nd4_end:
addp v8.4s, v8.4s, v9.4s
addp v10.4s, v10.4s, v11.4s
addp v12.4s, v12.4s, v13.4s
addp v14.4s, v14.4s, v15.4s
addp v8.4s, v8.4s, v10.4s
addp v9.4s, v12.4s, v14.4s
// start process kd4 kd2 kd1 cases
loopkd4_nd4:
cmp kd4_ori, 0
beq loopkd2_nd4
// start subkernel_m2n4k4
ld1 {v4.8b, v5.8b}, [pb], #16 // load B4x4
sxtl v4.8h, v4.8b
sxtl v5.8h, v5.8b
mov v6.d[0], v4.d[1]
mov v7.d[0], v5.d[1]
ld1 {v2.8b}, [pa], #8 // load A2x4
sxtl v2.8h, v2.8b
mov v3.d[0], v2.d[1]
smull v12.4s, v2.4h, v4.4h
smull v13.4s, v2.4h, v6.4h
smull v14.4s, v2.4h, v5.4h
smull v15.4s, v2.4h, v7.4h
addp v12.4s, v12.4s, v13.4s
addp v14.4s, v14.4s, v15.4s
addp v12.4s, v12.4s, v14.4s
add v8.4s, v8.4s, v12.4s
smull v16.4s, v3.4h, v4.4h
smull v17.4s, v3.4h, v6.4h
smull v18.4s, v3.4h, v5.4h
smull v19.4s, v3.4h, v7.4h
addp v16.4s, v16.4s, v17.4s
addp v18.4s, v18.4s, v19.4s
addp v16.4s, v16.4s, v18.4s
add v9.4s, v9.4s, v16.4s
loopkd2_nd4:
cmp kd2_ori, 0
beq loopkd1_nd4
// start subkernel_m2n4k2
ld1 {v4.8b}, [pa] // load A2x2
add pa, pa, #4
ld1 {v0.8b}, [pb], #8 // load B2x4
// 00 11 22 33
rev32 v1.4h, v0.4h // 11 00 33 22
rev64 v2.2s, v0.2s // 22 33 00 11
rev64 v3.4h, v0.4h // 33 22 11 00
smull v12.8h, v4.8b, v0.8b
smull v13.8h, v4.8b, v1.8b
smull v14.8h, v4.8b, v2.8b
smull v15.8h, v4.8b, v3.8b
saddlp v12.4s, v12.8h
saddlp v13.4s, v13.8h
saddlp v14.4s, v14.8h
saddlp v15.4s, v15.8h
mov v16.s[0], v12.s[0]
mov v16.s[1], v13.s[0]
mov v16.s[2], v14.s[0]
mov v16.s[3], v15.s[0]
mov v17.s[0], v13.s[1]
mov v17.s[1], v12.s[1]
mov v17.s[2], v15.s[1]
mov v17.s[3], v14.s[1]
add v8.4s, v8.4s, v16.4s
add v9.4s, v9.4s, v17.4s
loopkd1_nd4:
cmp kd1_ori, 0
beq loopnd4_end
// start subkernel_m2n4k1
ld1 {v4.8b}, [pb] // load B1x4
add pb, pb, #4
ld1 {v2.8b}, [pa] // load A2x1
add pa, pa, #2
sxtl v4.8h, v4.8b
sxtl v2.8h, v2.8b
smlal v8.4s, v4.4h, v2.h[0]
smlal v9.4s, v4.4h, v2.h[1]
loopnd4_end:
#ifdef GEMM_REQUANT
ld1 {v12.2s}, [scales]
// int32 => fp32
scvtf v8.4s, v8.4s
scvtf v9.4s, v9.4s
// fp32 *= scale_tm
fmul v8.4s, v8.4s, v12.s[0]
fmul v9.4s, v9.4s, v12.s[1]
cmp bias, #0
beq loopnd4_end_requant // skip add bias
// fp32 += bias_tm
ld1 {v14.2s}, [bias]
dup v15.4s, v14.s[0]
fadd v8.4s, v8.4s, v15.4s
dup v15.4s, v14.s[1]
fadd v9.4s, v9.4s, v15.4s
loopnd4_end_requant:
// fp32 -> int32
fcvtas v8.4s, v8.4s
fcvtas v9.4s, v9.4s
// int32 -> int16
sqxtn v6.4h, v8.4s
sqxtn2 v6.8h, v9.4s
// int16 -> int8
sqxtn v8.8b, v6.8h
// save
st1 {v8.s}[0], [dst_ptr_0]
add dst_ptr_0, dst_ptr_0, #4
st1 {v8.s}[1], [dst_ptr_1]
add dst_ptr_1, dst_ptr_1, #4
#else
st1 {v8.4s}, [dst_ptr_0], #16
st1 {v9.4s}, [dst_ptr_1], #16
#endif
subs nd4_ori, nd4_ori, #1
mov pa, src_ori
bne loopnd4
nd2_start:
cmp nd2_ori, #0 // means n < 2
beq nd1_start
INIT
mov src_ori, pa // PanelA
cmp kd8_ori, #0
beq loopkd4_nd2 // k <= 7
mov kd, kd8_ori
cmp is_kd8_even, #0
beq loopkd8_nd2_even // loop number is even
// start loopmd2_kd8_nd2
subs kd, kd, #1
ld1 {v4.8b, v5.8b}, [pb], #16 // load two lines of B
ld1 {v2.8b, v3.8b}, [pa], #16 // load two lines of PanelA
smull v0.8h, v4.8b, v2.8b
smull v1.8h, v4.8b, v3.8b
saddlp v8.4s, v0.8h
saddlp v12.4s, v1.8h
smull v0.8h, v5.8b, v2.8b
smull v1.8h, v5.8b, v3.8b
saddlp v9.4s, v0.8h
saddlp v13.4s, v1.8h
cmp kd, #0
beq loopkd8_nd2_end
loopkd8_nd2_even:
// TODO
ld1 {v4.8b, v5.8b}, [pb], #16
ld1 {v2.8b, v3.8b}, [pa], #16
smull v0.8h, v4.8b, v2.8b
ld1 {v6.8b, v7.8b}, [pb], #16
smull v1.8h, v5.8b, v2.8b
ld1 {v24.8b, v25.8b}, [pa], #16
smlal v0.8h, v6.8b, v24.8b
smlal v1.8h, v7.8b, v24.8b
sadalp v8.4s, v0.8h
sadalp v9.4s, v1.8h
smull v0.8h, v4.8b, v3.8b
smull v1.8h, v5.8b, v3.8b
smlal v0.8h, v6.8b, v25.8b
smlal v1.8h, v7.8b, v25.8b
sadalp v12.4s, v0.8h
sadalp v13.4s, v1.8h
subs kd, kd, #2
bne loopkd8_nd2_even
loopkd8_nd2_end:
addp v8.4s, v8.4s, v9.4s
addp v12.4s, v12.4s, v13.4s
addp v8.4s, v8.4s, v8.4s
addp v12.4s, v12.4s, v12.4s
// start process kd4 kd2 kd1 cases
loopkd4_nd2:
cmp kd4_ori, 0
beq loopkd2_nd2
// start subkernel_m2n2k4
ld1 {v4.8b}, [pb], #8 // load B4x2
sxtl v4.8h, v4.8b
mov v6.d[0], v4.d[1]
ld1 {v2.8b}, [pa], #8 // load first A2x4
sxtl v2.8h, v2.8b
mov v3.d[0], v2.d[1]
smull v9.4s, v2.4h, v4.4h
smull v10.4s, v2.4h, v6.4h
addp v9.4s, v9.4s, v10.4s
addp v9.4s, v9.4s, v9.4s
add v8.4s, v8.4s, v9.4s
smull v13.4s, v3.4h, v4.4h
smull v14.4s, v3.4h, v6.4h
addp v13.4s, v13.4s, v14.4s
addp v13.4s, v13.4s, v13.4s
add v12.4s, v12.4s, v13.4s
loopkd2_nd2:
cmp kd2_ori, 0
beq loopkd1_nd2
// start subkernel_m2n2k2
ld1 {v4.8b}, [pa] // load A2x2
add pa, pa, #4
ld1 {v0.8b}, [pb] // load B2x2
add pb, pb, #4
// 00 11
rev32 v1.4h, v0.4h // 11 00
smull v21.8h, v4.8b, v0.8b
smull v22.8h, v4.8b, v1.8b
saddlp v21.4s, v21.8h
saddlp v22.4s, v22.8h
mov v9.s[0], v21.s[0]
mov v9.s[1], v22.s[0]
add v8.4s, v8.4s, v9.4s
mov v13.s[0], v22.s[1]
mov v13.s[1], v21.s[1]
add v12.4s, v12.4s, v13.4s
loopkd1_nd2:
cmp kd1_ori, 0
beq loopnd2_end
// start subkernel_m2n2k1
ld1 {v4.8b}, [pb] // load B1x2
add pb, pb, #2
ld1 {v2.8b}, [pa] // load A4x1
add pa, pa, #2
sxtl v4.8h, v4.8b
sxtl v2.8h, v2.8b
smlal v8.4s, v4.4h, v2.h[0]
smlal v12.4s, v4.4h, v2.h[1]
loopnd2_end:
#ifdef GEMM_REQUANT
mov v8.d[1], v12.d[0]
// v12: 0 1
ld1 {v12.2s}, [scales]
zip1 v12.4s, v12.4s, v12.4s
// v12: 0 0 1 1
// int32 => fp32
scvtf v8.4s, v8.4s
// fp32 *= scale_tm
fmul v8.4s, v8.4s, v12.4s
cmp bias, #0
beq loopnd2_end_requant // skip add bias
// fp32 += bias_tm
ld1 {v12.2s}, [bias]
zip1 v12.4s, v12.4s, v12.4s
fadd v8.4s, v8.4s, v12.4s
loopnd2_end_requant:
// fp32 -> int32
fcvtas v8.4s, v8.4s
// int32 -> int16
sqxtn v8.4h, v8.4s
// int16 -> int8
sqxtn v8.8b, v8.8h
// save
st1 {v8.h}[0], [dst_ptr_0]
add dst_ptr_0, dst_ptr_0, #2
st1 {v8.h}[1], [dst_ptr_1]
add dst_ptr_1, dst_ptr_1, #2
#else
st1 {v8.2s}, [dst_ptr_0], #8
st1 {v12.2s}, [dst_ptr_1], #8
#endif
mov pa, src_ori
nd1_start:
cmp nd1_ori, 0 // means left n == 0
beq finish
INIT
cmp kd8_ori, #0
beq loopkd4_nd1 // k <= 7
mov kd, kd8_ori
cmp is_kd8_even, 0
beq loopkd8_nd1_even // loop number is even
// start loopkd8_nd1
subs kd, kd, #1
ld1 {v4.8b}, [pb], #8 // load four lines of B
ld1 {v2.8b, v3.8b}, [pa], #16 // load two lines of PanelA
smull v0.8h, v4.8b, v2.8b
smull v1.8h, v4.8b, v3.8b
saddlp v8.4s, v0.8h
saddlp v12.4s, v1.8h
cmp kd, #0
beq loopkd8_nd1_end
loopkd8_nd1_even:
ld1 {v4.8b, v5.8b}, [pb], #16
ld1 {v24.8b, v25.8b, v26.8b, v27.8b}, [pa], #32
smull v0.8h, v24.8b, v4.8b
smlal v0.8h, v26.8b, v5.8b
sadalp v8.4s, v0.8h
smull v1.8h, v25.8b, v4.8b
smlal v1.8h, v27.8b, v5.8b
sadalp v12.4s, v1.8h
subs kd, kd, #2
bne loopkd8_nd1_even
loopkd8_nd1_end:
addp v8.4s, v8.4s, v8.4s
addp v8.4s, v8.4s, v8.4s
addp v12.4s, v12.4s, v12.4s
addp v12.4s, v12.4s, v12.4s
// start process kd4 kd2 kd1 cases
loopkd4_nd1:
cmp kd4_ori, 0
beq loopkd2_nd1
// start subkernel_m2n1k2
ld1 {v4.8b}, [pb] // load B4x1
add pb, pb, #4
sxtl v4.8h, v4.8b // extend B4x1 to v4
ld1 {v2.8b}, [pa], #8 // load A2x4
sxtl v2.8h, v2.8b
mov v5.d[0], v2.d[1]
smull v9.4s, v2.4h, v4.4h
addp v9.4s, v9.4s, v9.4s
addp v9.4s, v9.4s, v9.4s
add v8.4s, v8.4s, v9.4s
smull v13.4s, v5.4h, v4.4h
addp v13.4s, v13.4s, v13.4s
addp v13.4s, v13.4s, v13.4s
add v12.4s, v12.4s, v13.4s
loopkd2_nd1:
cmp kd2_ori, 0
beq loopkd1_nd1
// start subkernel_m2n1k2
ld1 {v4.8b}, [pa] // load A2x2
add pa, pa, #4
ld1 {v0.8b}, [pb] // load B2x1
add pb, pb, #2
mov v0.h[1], v0.h[0]
smull v0.8h, v0.8b, v4.8b
saddlp v0.4s, v0.8h
mov v9.s[0], v0.s[0]
add v8.4s, v8.4s, v9.4s
mov v13.s[0], v0.s[1]
add v12.4s, v12.4s, v13.4s
loopkd1_nd1:
cmp kd1_ori, 0
beq loopnd1_end
// start subkernel_m2n1k1
ld1 {v0.8b}, [pb] // load B1x1
add pb, pb, #1
ld1 {v1.8b}, [pa] // load A2x1
add pa, pa, #2
sxtl v1.8h, v1.8b
sxtl v0.8h, v0.8b
smull v0.4s, v1.4h, v0.h[0]
mov v1.s[0], v0.s[1]
add v8.4s, v8.4s, v0.4s
add v12.4s, v12.4s, v1.4s
loopnd1_end:
#ifdef GEMM_REQUANT
mov v8.s[1], v12.s[0]
// v12: s0 s1
ld1 {v12.2s}, [scales]
// int32 => fp32
scvtf v8.2s, v8.2s
// fp32 *= scale_tm
fmul v8.2s, v8.2s, v12.2s
cmp bias, #0
beq loopnd1_end_requant // skip add bias
// fp32 += bias_tm
ld1 {v12.2s}, [bias]
fadd v8.2s, v8.2s, v12.2s
loopnd1_end_requant:
// fp32 -> int32
fcvtas v8.2s, v8.2s
// int32 -> int16
sqxtn v8.4h, v8.4s
// int16 -> int8
sqxtn v8.8b, v8.8h
// save
st1 {v8.b}[0], [dst_ptr_0]
st1 {v8.b}[1], [dst_ptr_1]
#else
// no need to add the last output pointer
st1 {v8.s}[0], [dst_ptr_0]
st1 {v12.s}[0], [dst_ptr_1]
#endif
finish:
mov x0, #0 // set return value
RESTORE_REGS
ret
#endif
#endif