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.0 KiB82 linesraw
1// Ported from ggml_compute_forward_norm_f32
2// Dispatch [ ( ne01 + 31 ) / 32, 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 double eps = 1e-5; // TODO: make this a parameter
14
15#include "groupReduce.hlsli"
16
17double computeVectorSum( uint i, const uint length )
18{
19	double res = 0.0;
20	const uint iEnd = i + length;
21	for( ; i < iEnd; i++ )
22		res += arg0[ i ];
23	return res;
24}
25
26double offsetAndComputeSumSquares( uint rsi, uint rdi, const double mean, const uint length )
27{
28	precise double sum2 = 0.0;
29	const uint rsiEnd = rsi + length;
30	for( ; rsi < rsiEnd; rsi++, rdi++ )
31	{
32		double v = arg0[ rsi ];
33		v -= mean;
34		result[ rdi ] = (float)v;
35		double prod = v * v;
36		sum2 += prod;
37	}
38	return sum2;
39}
40
41void scaleVector( uint rdi, const float scale, const uint length )
42{
43	const uint rdiEnd = rdi + length;
44	for( ; rdi < rdiEnd; rdi++ )
45	{
46		float f = result[ rdi ];
47		f *= scale;
48		result[ rdi ] = f;
49	}
50}
51
52#include "fp64Utils.hlsli"
53
54[ numthreads( 32, 1, 1 ) ]
55void main( uint3 dtid: SV_DispatchThreadID )
56{
57	const uint i03 = dtid.z;
58	const uint i02 = dtid.y;
59	const uint i01 = dtid.x;
60	if( i01 >= src0_elements[ 1 ] )
61		return;
62
63	const uint nb01 = src0_strides[ 1 ];
64	const uint nb02 = src0_strides[ 2 ];
65	const uint nb03 = src0_strides[ 3 ];
66
67	const uint p = i01 * nb01 + i02 * nb02 + i03 * nb03;
68	const uint ne00 = src0_elements[ 0 ];
69
70	double mean = computeVectorSum( p, ne00 );
71	mean = div64( mean, (double)(int)ne00 );
72
73	const uint nb1 = result_strides[ 1 ];
74	const uint nb2 = result_strides[ 2 ];
75	const uint nb3 = result_strides[ 3 ];
76	const uint y = i01 * nb1 + i02 * nb2 + i03 * nb3;
77
78	const double sum2 = offsetAndComputeSumSquares( p, y, mean, ne00 );
79	const float scale = (float)div64( 1.0, sqrt64( sum2 / (float)(int)ne00 + eps ) );
80
81	scaleVector( y, scale, ne00 );
82}