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
1.6 KiB68 linesraw
1#include "stdafx.h"
2#include "DecoderTensors.h"
3using namespace CpuCompute;
4
5#if TENSOR_GGML_COMPAT
6namespace
7{
8	class CompatContext
9	{
10		std::vector<ggml_tensor>& vec;
11		size_t index;
12
13	public:
14		CompatContext( std::vector<ggml_tensor>& dest, size_t layers ) :
15			vec( dest )
16		{
17			constexpr size_t tensorsPerLayer = 21;
18			const size_t count = tensorsPerLayer * layers + 4;
19			vec.resize( count );
20			index = 0;
21		}
22
23		void add( const Tensor& rsi, ggml_tensor*& res )
24		{
25			ggml_tensor& ten = vec[ index ];
26			index++;
27			ten = rsi.ggml();
28			res = &ten;
29		}
30
31		void add2( const TensorPair& rsi, ggml_tensor*& w, ggml_tensor*& b )
32		{
33			add( rsi.w, w );
34			add( rsi.b, b );
35		}
36
37		bool isComplete() const
38		{
39			return index == vec.size();
40		}
41	};
42}
43
44void DecoderTensors::makeCompatTensors()
45{
46	CompatContext ctx( ggml, layers.size() );
47
48	ctx.add( positionalEmbedding, d_pe );
49	ctx.add( tokenEmbedding, d_te );
50	ctx.add2( ln, d_ln_w, d_ln_b );
51
52	for( auto& i : layers )
53	{
54		ctx.add2( i.attnLn0, i.attn_ln_0_w, i.attn_ln_0_b );
55		ctx.add2( i.attnLn1, i.attn_ln_1_w, i.attn_ln_1_b );
56		ctx.add2( i.attnQuery, i.attn_q_w, i.attn_q_b );
57		ctx.add( i.attnKey, i.attn_k_w );
58		ctx.add2( i.attnValue, i.attn_v_w, i.attn_v_b );
59		ctx.add2( i.crossAttnLn0, i.cross_attn_ln_0_w, i.cross_attn_ln_0_b );
60		ctx.add2( i.crossAttnLn1, i.cross_attn_ln_1_w, i.cross_attn_ln_1_b );
61		ctx.add2( i.crossAttnQuery, i.cross_attn_q_w, i.cross_attn_q_b );
62		ctx.add2( i.mlpLn, i.mlp_ln_w, i.mlp_ln_b );
63		ctx.add2( i.mlp0, i.mlp_0_w, i.mlp_0_b );
64		ctx.add2( i.mlp1, i.mlp_1_w, i.mlp_1_b );
65	}
66	assert( ctx.isComplete() );
67}
68#endif