summaryrefslogtreecommitdiffstats
path: root/Whisper/ML/testUtils.h
blob: 1e62e3854b7151462f6dae23fe4259b12d41d85f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#pragma once
#include "../D3D/downloadBuffer.h"
#include "../D3D/RenderDoc/renderDoc.h"
#include <unordered_set>
#include <functional>

// Funfact: this code written by ChatGPT
namespace std
{
	template<>
	struct hash<array<uint32_t, 8>>
	{
		size_t operator()( const array<uint32_t, 8>& arr ) const
		{
			size_t result = 0;
			for( uint32_t element : arr )
				result = ( result * 31 ) ^ element;
			return result;
		}
	};
}

namespace DirectCompute
{
	struct sTensorDiff
	{
		// maximum( absolute( a - b ) )
		float maxAbsDiff;
		// average( ( a - b )^2 )
		float avgDiffSquared;
		size_t length;

		void print() const;
		void print( const char* what ) const;
	};

	// Compute difference between 2 FP32 vectors
	sTensorDiff computeDiff( const float* a, const float* b, size_t length );

	// Compute difference between 2 FP16 vectors
	sTensorDiff computeDiff( const uint16_t* a, const uint16_t* b, size_t length );

	class Tensor;
	sTensorDiff computeDiff( const Tensor& a, const Tensor& b );

	HRESULT dbgWriteBinaryFile( LPCTSTR fileName, const void* rsi, size_t cb );

	// Print unique sizes of the two tensors
	class PrintUniqueTensorSizes
	{
		std::unordered_set<std::array<uint32_t, 8>> set;
		const char* const what;
		void printImpl( const std::array<uint32_t, 8>& a );

	public:
		PrintUniqueTensorSizes( const char* w ) : what( w ) { }

		void print( const Tensor& lhs, const Tensor& rhs );
		void print( const Tensor& lhs );
		void print( const int* lhs, const int* rhs );
	};
}