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 "Reshaper.h" 3#include "../D3D/MappedResource.h" 4#include "../D3D/Binder.h" 5#include "../D3D/shaders.h" 6#include "reshapedMultiply.h" 7 8namespace 9{ 10using namespace DirectCompute ; 11struct Constants 12 { 13// Size and strides of the source tensor 14TensorShape arg0 ; 15uint32_t zzPadding ; 16// Count of elements per panel 17uint32_t panelSize ; 18// Layer strides of the output matrix 19 std::array < uint32_t ,2 > layerStrides ; 20 }; 21} 22 23HRESULT DirectCompute ::Reshaper ::createConstants () 24{ 25constexpr uint32_t cb = sizeof (Constants ); 26CD3D11_BUFFER_DESC desc {cb ,D3D11_BIND_CONSTANT_BUFFER ,D3D11_USAGE_DYNAMIC ,D3D11_CPU_ACCESS_WRITE }; 27return device ()-> CreateBuffer (& desc ,nullptr ,& constantBuffer ); 28} 29 30HRESULT DirectCompute ::Reshaper ::makePanels (Tensor & tensor ,eDataType dataType ) 31{ 32if ( !constantBuffer ) 33CHECK (createConstants () ); 34 35constexpr uint32_t TILE_SIZE = ReshapedMultiply ::TILE_SIZE ; 36 37// Reshaping into column major horizontal panels, height = TILE_SIZE, width = width of the source matrix 38 39 std::array < uint32_t ,4 > ne = tensor .ne ; 40const uint32_t groupsX = (ne [1 ]+ TILE_SIZE - 1 ) /TILE_SIZE ; 41ne [1 ]= groupsX * TILE_SIZE ;; 42// Each panel has [ size.x, TILE_SIZE ] elements 43const uint32_t panelSize = ne [0 ]* TILE_SIZE ; 44 45Tensor result ; 46result .create (dataType ,ne ); 47 48 { 49MappedResource mapped ; 50CHECK (mapped .map (constantBuffer , false ) ); 51Constants & cb = * (Constants * )mapped .data (); 52 53store (cb .arg0 .ne ,tensor .sizeVec () ); 54store (cb .arg0 .nb ,tensor .stridesVec () ); 55cb .panelSize = panelSize ; 56cb .layerStrides [0 ]= result .nb [2 ]; 57cb .layerStrides [1 ]= result .nb [3 ]; 58 } 59 60csSetCB (constantBuffer ); 61 { 62Binder bind ; 63bind .bind (tensor ,result ); 64bindShader ( eComputeShader::matReshapePanels ); 65context ()-> Dispatch (groupsX ,tensor .ne [2 ],tensor .ne [3 ] ); 66 } 67 68tensor .nb [0 ]= 0 ; 69tensor .nb [1 ]= panelSize ; 70tensor .nb [2 ]= result .nb [2 ]; 71tensor .nb [3 ]= result .nb [3 ]; 72tensor .setGpuViews (result ); 73return S_OK ; 74} 75 76DirectCompute ::Reshaper ::~Reshaper () 77{ 78if (constantBuffer ) 79csSetCB (nullptr ); 80}