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 KiB72 linesraw
1#include "stdafx.h"
2#include "TensorShape.h"
3#include "../source/ggml.h"
4using namespace DirectCompute;
5
6TensorShape::TensorShape()
7{
8	setZero();
9}
10
11TensorShape::TensorShape( const TensorShape& that )
12{
13	_mm_storeu_si128( ( __m128i* )ne.data(), that.sizeVec() );
14	_mm_storeu_si128( ( __m128i* )nb.data(), that.stridesVec() );
15}
16
17void TensorShape::operator=( const TensorShape& that )
18{
19	_mm_storeu_si128( ( __m128i* )ne.data(), that.sizeVec() );
20	_mm_storeu_si128( ( __m128i* )nb.data(), that.stridesVec() );
21}
22
23HRESULT TensorShape::create( const ggml_tensor& ggml )
24{
25	for( size_t i = 0; i < 4; i++ )
26		ne[ i ] = (uint32_t)ggml.ne[ i ];
27
28	const ggml_type dataType = ggml.type;
29	// Verify a few things
30	uint32_t cbElement = (uint32_t)ggml_type_size( dataType );
31	for( size_t i = 0; i < 4; i++ )
32	{
33		size_t stride = ggml.nb[ i ];
34		if( 0 != stride % cbElement )
35			return E_INVALIDARG;
36		size_t nn = stride / cbElement;
37		if( nn > UINT_MAX )
38			return DISP_E_OVERFLOW;
39		nb[ i ] = (uint32_t)nn;
40	}
41	return S_OK;
42}
43
44TensorShape::TensorShape( const ggml_tensor& ggml )
45{
46	HRESULT hr = create( ggml );
47	if( FAILED( hr ) )
48		throw hr;
49}
50
51void TensorShape::setDenseStrides()
52{
53	nb[ 0 ] = 1;
54	nb[ 1 ] = ne[ 0 ];
55	const uint32_t p01 = ne[ 0 ] * ne[ 1 ];
56	nb[ 2 ] = p01;
57	nb[ 3 ] = p01 * ne[ 2 ];
58}
59
60bool DirectCompute::canMulMat( const TensorShape& t0, const TensorShape& t1 )
61{
62	/*
63	return
64		( t0.ne[ 0 ] == t1.ne[ 0 ] ) &&
65		( t0.ne[ 2 ] == t1.ne[ 2 ] ) &&
66		( t0.ne[ 3 ] == t1.ne[ 3 ] ); */
67	__m128i a = t0.sizeVec();
68	__m128i b = t1.sizeVec();
69	__m128i xx = _mm_xor_si128( a, b );
70	xx = _mm_shuffle_epi32( xx, _MM_SHUFFLE( 3, 2, 0, 0 ) );
71	return (bool)_mm_testz_si128( xx, xx );
72}