yum-archive/TaSTT-Whisper

High-performance GPGPU inference of OpenAI's Whisper automatic speech recognition (ASR) model

git clone https://git.yummers.dev/yum-archive/TaSTT-Whisper

KonstantinSource codes8c4603c

master
2.1 KiB86 linesraw
1// Ported from ggml_compute_forward_norm_f32
2// Dispatch [ ne01, ne02, ne03 ] thread groups of this shader
3Buffer<float> arg0: register( t0 );
4RWBuffer<float> result: register( u0 );
5
6cbuffer Constants: register( b0 )
7{
8	uint4 src0_elements: packoffset( c0 );
9	uint4 src0_strides: packoffset( c1 );
10	uint4 result_strides: packoffset( c3 );
11}
12
13static const float eps = 1e-5f; // TODO: make this a parameter
14
15#include "groupReduce.hlsli"
16
17float computeVectorSum( uint i, const uint length, const uint thread )
18{
19	float res = 0.0;
20
21	const uint iEnd = i + length;
22	i += thread;
23	for( ; i < iEnd; i += 32 )
24		res += arg0[ i ];
25
26	horizontalSumBroadcast( thread, res );
27	return res;
28}
29
30float offsetAndComputeSumSquares( uint rsi, uint rdi, const float mean, const uint length, const uint thread )
31{
32	float sum2 = 0.0;
33
34	const uint rsiEnd = rsi + length;
35	rsi += thread;
36	rdi += thread;
37	for( ; rsi < rsiEnd; rsi += 32, rdi += 32 )
38	{
39		float v = arg0[ rsi ] - mean;
40		result[ rdi ] = v;
41		sum2 = mad( v, v, sum2 );
42	}
43
44	horizontalSumBroadcast( thread, sum2 );
45	return sum2;
46}
47
48void scaleVector( uint rdi, const float scale, const uint length, const uint thread )
49{
50	const uint rdiEnd = rdi + length;
51	for( rdi += thread; rdi < rdiEnd; rdi += 32 )
52	{
53		float f = result[ rdi ];
54		f *= scale;
55		result[ rdi ] = f;
56	}
57}
58
59[ numthreads( 32, 1, 1 ) ]
60void main( uint3 group: SV_GroupID, uint thread : SV_GroupIndex )
61{
62	const uint i03 = group.z;
63	const uint i02 = group.y;
64	const uint i01 = group.x;
65
66	const uint nb01 = src0_strides[ 1 ];
67	const uint nb02 = src0_strides[ 2 ];
68	const uint nb03 = src0_strides[ 3 ];
69
70	const uint p = i01 * nb01 + i02 * nb02 + i03 * nb03;
71
72	const uint ne00 = src0_elements[ 0 ];
73
74	float mean = computeVectorSum( p, ne00, thread );
75	mean /= (float)(int)ne00;
76
77	const uint nb1 = result_strides[ 1 ];
78	const uint nb2 = result_strides[ 2 ];
79	const uint nb3 = result_strides[ 3 ];
80	const uint y = i01 * nb1 + i02 * nb2 + i03 * nb3;
81
82	float sum2 = offsetAndComputeSumSquares( p, y, mean, ne00, thread );
83	const float scale = 1.0 / sqrt( sum2 / (float)(int)ne00 + eps );
84
85	scaleVector( y, scale, ne00, thread );
86}