1
1
#include < cstdio>
2
2
#include " nn-config-builder.hpp"
3
+ #include " nn-quants.hpp"
3
4
#include " nn-vulkan.hpp"
4
5
5
6
#define N_BATCHES 4
@@ -309,11 +310,11 @@ void testShift_F32_F32() {
309
310
}
310
311
311
312
void testCast_F32_F32 () {
312
- #define CAST_DIM 64
313
+ #define CAST_F32_DIM 64
313
314
execute (
314
315
[](NnNetConfigBuilder *netBuilder, NnNodeConfigBuilder *nodeBuilder, NnSegmentConfigBuilder *segmentBuilder) {
315
- NnUint xPipeIndex = netBuilder->addPipe (" X" , size2D (F_32, N_BATCHES, CAST_DIM ));
316
- NnUint yPipeIndex = netBuilder->addPipe (" Y" , size2D (F_32, N_BATCHES, CAST_DIM ));
316
+ NnUint xPipeIndex = netBuilder->addPipe (" X" , size2D (F_32, N_BATCHES, CAST_F32_DIM ));
317
+ NnUint yPipeIndex = netBuilder->addPipe (" Y" , size2D (F_32, N_BATCHES, CAST_F32_DIM ));
317
318
segmentBuilder->addOp (
318
319
OP_CAST, " cast" , 0 ,
319
320
pointerBatchConfig (SRC_PIPE, xPipeIndex),
@@ -327,25 +328,56 @@ void testCast_F32_F32() {
327
328
float *xPipe = (float *)execution->pipes [0 ];
328
329
float *yPipe = (float *)execution->pipes [1 ];
329
330
330
- for (NnUint b = 0 ; b < N_BATCHES; b++) {
331
- for (NnUint i = 0 ; i < CAST_DIM; i++)
332
- xPipe[b * CAST_DIM + i] = (float )b;
333
- }
331
+ for (NnUint i = 0 ; i < N_BATCHES * CAST_F32_DIM; i++)
332
+ xPipe[i] = (float )(i + 1 );
334
333
335
334
// act
336
335
executor->forward ();
337
336
338
337
// assert
339
- for (NnUint b = 0 ; b < N_BATCHES; b++) {
340
- for (NnUint i = 0 ; i < CAST_DIM; i++) {
341
- NnUint j = b * CAST_DIM + i;
342
- assertFloat (j, yPipe[j], (float )b, 0 .00001f );
343
- }
344
- }
338
+ for (NnUint i = 0 ; i < N_BATCHES * CAST_F32_DIM; i++)
339
+ assertFloat (i, yPipe[i], (float )(i + 1 ), 0 .00001f );
345
340
printOk (" testCast_F32_F32" );
346
341
});
347
342
}
348
343
344
+ void testCast_F32_Q80 () {
345
+ #define CAST_Q80_DIM 256
346
+ execute (
347
+ [](NnNetConfigBuilder *netBuilder, NnNodeConfigBuilder *nodeBuilder, NnSegmentConfigBuilder *segmentBuilder) {
348
+ NnUint xPipeIndex = netBuilder->addPipe (" X" , size2D (F_32, N_BATCHES, CAST_Q80_DIM));
349
+ NnUint yPipeIndex = netBuilder->addPipe (" Y" , size2D (F_Q80, N_BATCHES, CAST_Q80_DIM));
350
+ segmentBuilder->addOp (
351
+ OP_CAST, " cast" , 0 ,
352
+ pointerBatchConfig (SRC_PIPE, xPipeIndex),
353
+ pointerBatchConfig (SRC_PIPE, yPipeIndex),
354
+ size0 (),
355
+ NnCastOpCodeConfig{});
356
+ },
357
+ [](NnExecutor *executor, NnNetExecution *execution, NnVulkanDevice *device) {
358
+ // arrange
359
+ execution->setBatchSize (N_BATCHES);
360
+ float *xPipe = (float *)execution->pipes [0 ];
361
+ NnBlockQ80 *yPipe = (NnBlockQ80 *)execution->pipes [1 ];
362
+
363
+ for (NnUint i = 0 ; i < N_BATCHES * CAST_Q80_DIM; i++)
364
+ xPipe[i] = (float )(i + 1 );
365
+
366
+ // act
367
+ executor->forward ();
368
+
369
+ float yF32[CAST_Q80_DIM * N_BATCHES];
370
+ dequantizeQ80toF32 (yPipe, yF32, CAST_Q80_DIM * N_BATCHES, 1 , 0 );
371
+
372
+ for (NnUint i = 0 ; i < N_BATCHES * CAST_Q80_DIM; i++) {
373
+ const float expectedV = (float )(i + 1 );
374
+ const float change = (yF32[i] - expectedV) / expectedV;
375
+ assertFloat (i, change, 0.0 , 0 .009f );
376
+ }
377
+ printOk (" testCast_F32_Q80" );
378
+ });
379
+ }
380
+
349
381
void testRope_F32_F32 () {
350
382
#define ROPE_DIM 2048
351
383
#define ROPE_KV_DIM 512
@@ -490,13 +522,16 @@ void multiheadAtt_F32_F32() {
490
522
}
491
523
492
524
int main () {
525
+ initQuants ();
526
+
493
527
testRmsNorm_F32_F32_F32 ();
494
528
testSilu_F32_F32 ();
495
529
testMul_F32_F32 ();
496
530
testMergeAdd_F32_F32 ();
497
531
testEmbedding_F32_F32 ();
498
532
testShift_F32_F32 ();
499
533
testCast_F32_F32 ();
534
+ testCast_F32_Q80 ();
500
535
testRope_F32_F32 ();
501
536
matmul_F32_F32_F32 ();
502
537
multiheadAtt_F32_F32 ();
0 commit comments