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
8c4603c
master
1#include "stdafx.h" 2#include "tensorOpsTests.h" 3#include "MlContext.h" 4#include "TensorEx.h" 5#include "../D3D/shaders.h" 6#include "../D3D/Binder.h" 7#include "testUtils.h" 8#include "../Whisper/WhisperContext.h" 9 10void DirectCompute ::testMulMat (const ggml_tensor * src0 ,const ggml_tensor * src1 ,const ggml_tensor * dst ,const void * tempBuffer ) 11{ 12return ; 13CaptureRaii capture ; 14const size_t nb00 = src0 -> nb [0 ]; 15const size_t nb01 = src0 -> nb [1 ]; 16 17if (src0 -> type != GGML_TYPE_F16 ) 18return ;// TODO 19 20if (nb01 < nb00 ) 21return ;// TODO 22 23WhisperContext & ctx = WhisperContext ::current (); 24 25Tensor arg0 ,arg1 ; 26check (arg0 .create (* src0 , eBufferUse::Immutable , true ) ); 27check (arg1 .create (* src1 , eBufferUse::Immutable , true ) ); 28TensorEx res ; 29check (res .create (* dst , eBufferUse::ReadWriteDownload , false ) ); 30 31ctx .mulMat (arg0 ,arg1 ,res ); 32 33 std::vector < float > tv ; 34check (res .download (tv ) ); 35 36const size_t len = tv .size (); 37computeDiff (tv .data (), (const float * )dst -> data ,len )."testMulMat-product" ); 38 39#if 0 40dbgWriteBinaryFile (L"product-orig.bin" ,dst -> data ,len * 4 ); 41dbgWriteBinaryFile (L"product-gpu.bin" ,tv .data (),len * 4 ); 42__debugbreak (); 43#endif 44} 45 46#if 0 47void DirectCompute ::testMulMatReshape (const ggml_tensor * src1 ,const void * tempBuffer ) 48{ 49Tensor src ; 50check (src .create (* src1 , eBufferUse::Immutable , true ) ); 51 52const size_t ne10 = (uint32_t )src1 -> ne [0 ]; 53const size_t ne11 = (uint32_t )src1 -> ne [1 ]; 54const size_t ne12 = (uint32_t )src1 -> ne [2 ]; 55const size_t ne13 = (uint32_t )src1 -> ne [3 ]; 56if (1 != ne13 ) 57throw E_UNEXPECTED ; 58const size_t tempLength = ne10 * ne11 * ne12 * ne13 ; 59 60Context & ctx = Context ::current (); 61const ReadWriteViews & temp = ctx .temp .fp16 (tempLength ); 62 63 { 64Binder bind ; 65ctx .cb .bind (); 66 67bindShader ( eComputeShader::mulMatDotReshape ); 68 69ctx .cb .update (src ); 70bind .bind (src ,temp ); 71context ()-> Dispatch ( (UINT )ne11 , (UINT )ne12 ,1 ); 72 } 73 74 std::vector < uint16_t > reshaped ; 75check (downloadBuffer (temp ,reshaped ) ); 76computeDiff (reshaped .data (), (const uint16_t * )tempBuffer ,reshaped .size () )."testMulMatReshape" ); 77 78#if 0 79dbgWriteBinaryFile (L"fp32.bin" ,src1 -> data ,ggml_nbytes (src1 ) ); 80dbgWriteBinaryFile (L"fp16-cpu.bin" ,tempBuffer ,reshaped .size ()* 2 ); 81dbgWriteBinaryFile (L"fp16-gpu.bin" ,reshaped .data (),reshaped .size ()* 2 ); 82__debugbreak (); 83#endif 84} 85#endif 86 87void DirectCompute ::computeMulMat (const ggml_tensor * src0 ,const ggml_tensor * src1 ,ggml_tensor * dst ) 88{ 89CaptureRaii capture ; 90const size_t nb00 = src0 -> nb [0 ]; 91const size_t nb01 = src0 -> nb [1 ]; 92 93if (src0 -> type != GGML_TYPE_F16 ) 94throw E_INVALIDARG ; 95if (nb01 < nb00 ) 96throw E_INVALIDARG ; 97 98WhisperContext & ctx = WhisperContext ::current (); 99 100Tensor arg0 ,arg1 ; 101check (arg0 .create (* src0 , eBufferUse::Immutable , true ) ); 102check (arg1 .create (* src1 , eBufferUse::Immutable , true ) ); 103TensorEx res ; 104check (res .create (* dst , eBufferUse::ReadWriteDownload , false ) ); 105 106ctx .mulMat (arg0 ,arg1 ,res ); 107 108check (res .download (dst -> data ) ); 109} 110 111void DirectCompute ::testFlashAttention (const ggml_tensor * q ,const ggml_tensor * k ,const ggml_tensor * v ,bool masked ,const ggml_tensor * dst ) 112{ 113CaptureRaii capture ; 114 115Tensor Q ,K ,V ; 116TensorEx res ; 117check (Q .create (* q , eBufferUse::Immutable , true ) ); 118check (K .create (* k , eBufferUse::Immutable , true ) ); 119check (V .create (* v , eBufferUse::Immutable , true ) ); 120check (res .create (* dst , eBufferUse::ReadWriteDownload , false ) ); 121 122WhisperContext & ctx = WhisperContext ::current (); 123ctx .flashAttention (Q ,K ,V ,res ,masked ); 124 125 std::vector < float > tv ; 126check (res .download (tv ) ); 127 128const size_t len = tv .size (); 129computeDiff (tv .data (), (const float * )dst -> data ,len )."testFlashAttention" ); 130} 131 132void DirectCompute ::computeFlashAttention (const ggml_tensor * q ,const ggml_tensor * k ,const ggml_tensor * v ,bool masked ,ggml_tensor * dst ) 133{ 134CaptureRaii capture ; 135 136Tensor Q ,K ,V ; 137TensorEx res ; 138check (Q .create (* q , eBufferUse::Immutable , true ) ); 139check (K .create (* k , eBufferUse::Immutable , true ) ); 140check (V .create (* v , eBufferUse::Immutable , true ) ); 141check (res .create (* dst , eBufferUse::ReadWriteDownload , false ) ); 142 143WhisperContext & ctx = WhisperContext ::current (); 144ctx .flashAttention (Q ,K ,V ,res ,masked ); 145 146check (res .download (dst -> data ) ); 147} 148 149void DirectCompute ::testConvolution (const ggml_tensor * src0 ,const ggml_tensor * src1 ,const ggml_tensor * dst ) 150{ 151CaptureRaii capture ; 152 153Tensor arg0 ,arg1 ; 154check (arg0 .create (* src0 , eBufferUse::Immutable , true ) ); 155check (arg1 .create (* src1 , eBufferUse::Immutable , true ) ); 156TensorEx res ; 157check (res .create (* dst , eBufferUse::ReadWriteDownload , false ) ); 158 159WhisperContext & ctx = WhisperContext ::current (); 160ctx .convolution (arg0 ,arg1 ,res ); 161 162 std::vector < float > tv ; 163check (res .download (tv ) ); 164 165const size_t len = tv .size (); 166computeDiff (tv .data (), (const float * )dst -> data ,len )."testConvolution" ); 167} 168 169void DirectCompute ::computeConvolution (const ggml_tensor * src0 ,const ggml_tensor * src1 ,ggml_tensor * dst ) 170{ 171CaptureRaii capture ; 172 173Tensor arg0 ,arg1 ; 174check (arg0 .create (* src0 , eBufferUse::Immutable , true ) ); 175check (arg1 .create (* src1 , eBufferUse::Immutable , true ) ); 176TensorEx res ; 177check (res .create (* dst , eBufferUse::ReadWriteDownload , false ) ); 178 179WhisperContext & ctx = WhisperContext ::current (); 180ctx .convolution (arg0 ,arg1 ,res ); 181 182res .download (dst -> data ); 183}