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
4.1 KiB122 linesraw
1#pragma warning disable CS0649 // Field is never assigned to
2using ComLight;
3using System.ComponentModel;
4using System.Runtime.InteropServices;
5
6namespace Whisper.Internal
7{
8	/// <summary>Size of the buffers owned by the <see cref="iTranscribeResult" /> object</summary>
9	public readonly struct sTranscribeLength
10	{
11		/// <summary>Count of segments</summary>
12		public readonly int countSegments;
13		/// <summary>Total count of tokens, for all segments combined</summary>
14		public readonly int countTokens;
15	}
16
17	/// <summary>Output data from the model</summary>
18	[ComInterface( "2871a73f-5ce3-48f8-8779-6582ee11935e", eMarshalDirection.ToManaged ), CustomConventions( typeof( NativeLogger ) )]
19	public interface iTranscribeResult
20	{
21		/// <summary>Get size of the buffers</summary>
22		[RetValIndex, EditorBrowsable( EditorBrowsableState.Never )]
23		public sTranscribeLength getSize();
24
25		/// <summary>Pointer to segment data, a vector of <see cref="sSegment" /> structures</summary>
26		[EditorBrowsable( EditorBrowsableState.Never )]
27		public IntPtr getSegments();
28
29		/// <summary>Pointer to tokens data, a vector of <see cref="sToken" /> structures</summary>
30		[EditorBrowsable( EditorBrowsableState.Never )]
31		public IntPtr getTokens();
32	}
33}
34
35namespace Whisper
36{
37	/// <summary>Start and end times of a segment or token</summary>
38	/// <remarks>The times are relative to the start of the media</remarks>
39	public readonly struct sTimeInterval
40	{
41		/// <summary>Start time</summary>
42		public readonly TimeSpan begin;
43		/// <summary>End time</summary>
44		public readonly TimeSpan end;
45	}
46
47	/// <summary>Segment data</summary>
48	public readonly struct sSegment
49	{
50		internal readonly IntPtr m_text;
51		/// <summary>Segment text</summary>
52		public string? text => Marshal.PtrToStringUTF8( m_text );
53		/// <summary>Start and end times of the segment</summary>
54		public readonly sTimeInterval time;
55		/// <summary>Slice of the tokens</summary>
56		public readonly int firstToken, countTokens;
57	}
58
59	/// <summary>Token flags</summary>
60	[Flags]
61	public enum eTokenFlags: uint
62	{
63		/// <summary>The token is special</summary>
64		Special = 1,
65	}
66
67	/// <summary>Token data</summary>
68	public readonly struct sToken
69	{
70		internal readonly IntPtr m_text;
71		/// <summary>Token text</summary>
72		public string? text => Marshal.PtrToStringUTF8( m_text );
73		/// <summary>Start and end times of the token</summary>
74		public readonly sTimeInterval time;
75		/// <summary>Probability of the token</summary>
76		public readonly float probability;
77		/// <summary>Probability of the timestamp token</summary>
78		public readonly float probabilityTimestamp;
79		/// <summary>Sum of probabilities of all timestamp tokens</summary>
80		public readonly float ptsum;
81		/// <summary>Voice length of the token</summary>
82		public readonly float vlen;
83		/// <summary>Token id</summary>
84		public readonly int id;
85		/// <summary>Token flags</summary>
86		readonly eTokenFlags flags;
87		/// <summary>True if the token flags has the specified bit set</summary>
88		public bool hasFlag( eTokenFlags bit ) => flags.HasFlag( bit );
89	}
90
91	/// <summary>Output data from the model</summary>
92	public readonly ref struct TranscribeResult
93	{
94		/// <summary>Segments in the results</summary>
95		public readonly ReadOnlySpan<sSegment> segments;
96		/// <summary>Tokens in the results, for all segments</summary>
97		public readonly ReadOnlySpan<sToken> tokens;
98
99		internal TranscribeResult( Internal.iTranscribeResult i )
100		{
101			Internal.sTranscribeLength len = i.getSize();
102			unsafe
103			{
104				// This does not copy the buffers to managed memory.
105				// Instead, the C# spans directly reference the native memory stored in these std::vectors
106				if( len.countSegments > 0 )
107					segments = new ReadOnlySpan<sSegment>( (void*)i.getSegments(), len.countSegments );
108				else
109					segments = ReadOnlySpan<sSegment>.Empty;
110
111				if( len.countTokens > 0 )
112					tokens = new ReadOnlySpan<sToken>( (void*)i.getTokens(), len.countTokens );
113				else
114					tokens = ReadOnlySpan<sToken>.Empty;
115			}
116		}
117
118		/// <summary>Get tokens for the specified segment</summary>
119		public ReadOnlySpan<sToken> getTokens( in sSegment seg ) =>
120			tokens.Slice( seg.firstToken, seg.countTokens );
121	}
122}