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
4.2 KiB154 linesraw
1// GGML_TASK_COMPUTE step for matrix*matrix product, where nb01 < nb00
2Buffer<float> arg0: register( t0 );
3Buffer<float> arg1: register( t1 );
4RWBuffer<float> resultTensor: register( u0 );
5RWBuffer<float> tempBuffer: register( u1 );
6
7cbuffer Constants: register( b0 )
8{
9	uint4 aSize: packoffset( c0 );
10	uint4 aStride: packoffset( c1 );
11	uint4 bSize: packoffset( c2 );
12	uint4 bStride: packoffset( c3 );
13	uint4 resSize: packoffset( c4 );
14	bool resultFp16 : packoffset( c5.x );
15	uint ne: packoffset( c5.y );
16}
17
18#include "miscUtils.hlsli"
19
20// tempBuffer[ rdi .. ] = 0.0
21inline void writeTempZeros( uint rdi, const uint len, const uint thread )
22{
23	const uint rdiEnd = rdi + len;
24	for( rdi += thread; rdi < rdiEnd; rdi += 32 )
25		tempBuffer[ rdi ] = 0.0;
26}
27
28// tempBuffer[ rdi .. ] += mul * arg0[ rsi .. ]
29inline void vectorMad( uint rsi, uint rdi, const uint len, const float mul, const uint thread )
30{
31	const uint rsiEnd = rsi + len;
32	rsi += thread;
33	rdi += thread;
34	for( ; rsi < rsiEnd; rsi += 32, rdi += 32 )
35	{
36		float f = tempBuffer[ rdi ];
37		f = mad( mul, arg0[ rsi ], f );
38		[branch]
39		if( resultFp16 )
40			f = adjustFp16( f );
41		tempBuffer[ rdi ] = f;
42	}
43}
44
45// resultTensor[ rdi .. ] = tempBuffer[ rsi .. ]
46inline void copyRow( uint rsi, uint rdi, const uint len, const uint thread )
47{
48	const uint rsiEnd = rsi + len;
49	rsi += thread;
50	rdi += thread;
51	for( ; rsi < rsiEnd; rsi += 32, rdi += 32 )
52	{
53		float f = tempBuffer[ rsi ];
54		resultTensor[ rdi ] = f;
55	}
56}
57
58// resultTensor[ rdi .. ] += tempBuffer[ rsi .. ]
59inline void addRow( uint rsi, uint rdi, const uint len, const uint thread )
60{
61	const uint rsiEnd = rsi + len;
62	rsi += thread;
63	rdi += thread;
64	for( ; rsi < rsiEnd; rsi += 32, rdi += 32 )
65	{
66		float f = resultTensor[ rdi ];
67		f += tempBuffer[ rsi ];
68		resultTensor[ rdi ] = f;
69	}
70}
71
72[numthreads( 32, 1, 1 )]
73void main( const uint3 group: SV_GroupID, const uint thread : SV_GroupIndex )
74{
75	const uint i1 = group[ 0 ];
76	const uint i2 = group[ 1 ];
77	const uint i3 = group[ 2 ];
78
79	const uint ne00 = aSize[ 0 ];
80	const uint ne01 = aSize[ 1 ];
81	const uint ne02 = aSize[ 2 ];
82	const uint ne03 = aSize[ 3 ];
83
84	const uint ne10 = bSize[ 0 ];
85	const uint ne11 = bSize[ 1 ];
86	const uint ne12 = bSize[ 2 ];
87	const uint ne13 = bSize[ 3 ];
88
89	const uint ne0 = resSize[ 0 ];
90	const uint ne1 = resSize[ 1 ];
91	const uint ne2 = resSize[ 2 ];
92	const uint ne3 = resSize[ 3 ];
93
94	const uint nb00 = aStride[ 0 ];
95	const uint nb01 = aStride[ 1 ];
96	const uint nb02 = aStride[ 2 ];
97	const uint nb03 = aStride[ 3 ];
98
99	const uint nb10 = bStride[ 0 ];
100	const uint nb11 = bStride[ 1 ];
101	const uint nb12 = bStride[ 2 ];
102	const uint nb13 = bStride[ 3 ];
103
104	// dst_row = wdata + wo + i3*ne2*ne1*ne0 + i2*ne1*ne0 + i1*ne0;
105	const uint tempRowThread0 = i3 * ne2 * ne1 * ne0 + i2 * ne1 * ne0 + i1 * ne0;
106
107	// Faking 4 CPU threads trying to achieve bitwise compatibility with the CPU version
108	const uint nth = 4;
109
110	// GGML_TASK_COMPUTE
111	{
112		// src0_col = src0->data + ( i00 * nb00 + i02 * nb02 + i03 * nb03 );
113		const uint aBase = i2 * nb02 + i3 * nb03;
114		// src1_val = *      (float *) ((char *) src1->data + (i10*nb10 + i11*nb11 + i12*nb12 + i13*nb13));
115		const uint bBase = i1 * nb11 + i2 * nb12 + i3 * nb13;
116
117		// total columns in src1
118		const uint nc = ne10;
119		// columns per thread
120		const uint dc = ( nc + nth - 1 ) / nth;
121
122		uint tempRow = tempRowThread0;
123		for( uint ith = 0; ith < nth; ith++, tempRow += ne )
124		{
125			writeTempZeros( tempRow, ne01, thread );
126
127			// column range for this thread
128			const uint ic0 = dc * ith;
129			const uint ic1 = min( ic0 + dc, nc );
130
131			for( uint ic = ic0; ic < ic1; ic++ )
132			{
133				const uint idxA = aBase + ic * aStride[ 0 ];
134				const uint idxB = bBase + ic * bStride[ 0 ];
135				const float bValue = arg1[ idxB ];
136				vectorMad( idxA, tempRow, ne01, bValue, thread );
137			}
138		}
139	}
140
141	// GGML_TASK_FINALIZE
142	{
143		const uint rdi = tempRowThread0;
144		// const uint rdi = i1 * resSize[ 0 ] + i2 * resSize[ 0 ] * resSize[ 1 ] + i3 * resSize[ 0 ] * resSize[ 1 ] * resSize[ 2 ];
145		// const uint rdi = ( ( i3 * resSize[ 2 ] + i2 ) * resSize[ 1 ] + i1 ) * resSize[ 0 ];
146
147		uint tempRow = tempRowThread0;
148		copyRow( tempRow, rdi, ne01, thread );
149
150		tempRow += ne;
151		for( uint ith = 1; ith < nth; ith++, tempRow += ne )
152			addRow( tempRow, rdi, ne01, thread );
153	}
154}