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#pragma once 2#include "../D3D/enums.h" 3#include "../ML/TensorShape.h" 4// 1 = new tensors can be allocated with `nullptr` iMemoryAllocator, by allocating memory internally and counting these references 5// 0 = memory allocator is mandatory, create() methods will fail with E_POINTER if the allocator is `nullptr` 6#define TENSOR_INTERNAL_ALLOC 0 7 8// 1 = expose compatibility API for GGML interop 9#define TENSOR_GGML_COMPAT 0 10 11#if TENSOR_GGML_COMPAT 12#include "../source/ggml.h" 13#endif 14 15namespace CpuCompute 16{ 17using DirectCompute ::TensorShape ; 18using DirectCompute ::eDataType ; 19 20__interface iMemoryAllocator 21 { 22void * allocate (size_t cb ,size_t align ); 23 }; 24__interface iArenaAllocator :public iMemoryAllocator 25 { 26void resetArena (); 27 }; 28 29#if TENSOR_GGML_COMPAT 30class Tensor ; 31class GgmlTensorView 32 { 33ggml_tensor tensor ; 34public : 35GgmlTensorView (const Tensor & t ); 36operator ggml_tensor * ( ) { return & tensor; } 37}; 38#endif 39 40// A functional equivalent of ggml_tensor structure, designed for use from C++ 41class Tensor : public TensorShape 42{ 43void * m_data = nullptr ; 44 45eDataType m_type = ( eDataType ) 0xFF ; 46 47#if TENSOR_INTERNAL_ALLOC 48// True when the memory block was allocated internally by this class 49// In this case, this class does reference counting to support cheap copies. 50// False when it's owned by someone else, such as iMemoryAllocator object, or a GGML's tensor 51bool ownsMemory = false; 52void deallocate (); 53#endif 54 55// Private constructors for fromData() methods 56Tensor ( void * pointer, eDataType type, std::initializer_list < uint32_t > size ); 57Tensor ( void * pointer, eDataType type, uint32_t length ) noexcept; 58public : 59// Trivial constructors 60Tensor () = default; 61#if TENSOR_INTERNAL_ALLOC 62~ Tensor () 63{ 64deallocate (); 65} 66#else 67~ Tensor () = default; 68#endif 69Tensor ( Tensor && that ) noexcept; 70void operator = ( Tensor && that ) noexcept; 71Tensor( const Tensor & that ); 72void operator = ( const Tensor & that ); 73 74// Allocate a new tensor 75HRESULT create ( eDataType type, const std ::array < uint32_t , 4 >& sizeElements , iMemoryAllocator * alloc = nullptr ); 76// Allocate a new tensor 77HRESULT create ( eDataType type, std ::initializer_list < uint32_t > sizeElements, iMemoryAllocator * alloc = nullptr ); 78// Attach to pre-existing block of memory, interpreting the data as a dense tensor of the specified type and size 79HRESULT attach ( void * pointer, eDataType type, std ::initializer_list < uint32_t > sizeElements ); 80// Attach to pre-existing block of memory, interpret the data as a dense vector of the specified type and length 81static Tensor fromData ( void * pointer, eDataType type, uint32_t length ); 82 83eDataType type () const { return m_type; } 84void * data () const { return m_data; } 85 86uint16_t * fp16 () 87{ 88assert ( m_type == eDataType:: FP16 ); 89assert ( nullptr != m_data ); 90return ( uint16_t * )m_data; 91} 92const uint16_t * fp16 () const 93{ 94assert ( m_type == eDataType:: FP16 ); 95assert ( nullptr != m_data ); 96return ( uint16_t * )m_data; 97} 98float * fp32 () 99{ 100assert ( m_type == eDataType:: FP32 ); 101assert ( nullptr != m_data ); 102return ( float * )m_data; 103} 104const float * fp32 () const 105{ 106assert ( m_type == eDataType:: FP32 ); 107assert ( nullptr != m_data ); 108return ( float * )m_data; 109} 110 111Tensor reshape3d ( uint32_t ne0, uint32_t ne1, uint32_t ne2 ) const; 112 113void setType ( eDataType dt ) 114{ 115m_type = dt; 116} 117void setDataPointer ( void * pv ) 118{ 119m_data = pv; 120} 121 122#if TENSOR_GGML_COMPAT 123// Compatibility with GGML's tensors, for testing and lulz 124Tensor( const ggml_tensor * ggml ); 125ggml_tensor ggml () const ; 126 127operator GgmlTensorView () const 128{ 129return GgmlTensorView ( * this ); 130} 131#endif 132}; 133 134// A pair of tensors containing weights and biases; apparently, both tensors are of the same shape 135struct TensorPair 136{ 137Tensor w , b ; 138 }; 139}