Skip to content

Commit

Permalink
Add delay profiler, align profiler output format
Browse files Browse the repository at this point in the history
  • Loading branch information
picoliu committed Feb 24, 2025
1 parent 3fc04f0 commit a42ec2c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Diagnostics;

class DelayProfiler : Profiler
{
Stopwatch _stopwatch = new Stopwatch();
public DelayProfiler() { }

public void Tick()
{
_stopwatch.Restart();
}

public void Tock()
{
_stopwatch.Stop();
}

public string Stats()
{
return $"Delay: {_stopwatch.ElapsedMilliseconds:F2}ms";
}
}
17 changes: 11 additions & 6 deletions recipes/llm-voice-assistant/dotnet/LLMVoiceAssistant/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class LLMVoiceAssistant
static RTFProfiler? porcupineProfiler;
static RTFProfiler? cheetahProfiler;
static TPSProfiler? pllmProfiler;
static event EventHandler<Profiler?>? ProfilerEvent;
static DelayProfiler? delayProfiler;
static event EventHandler<Profiler?>? profilerEvent;

public class CompletionText
{
Expand Down Expand Up @@ -181,7 +182,7 @@ private void CheetahOnAudioReceived(object? sender, short[] pcm)

private void OnWakeWordDetected(object? sender, EventArgs e)
{
ProfilerEvent?.Invoke(null, porcupineProfiler);
profilerEvent?.Invoke(null, porcupineProfiler);
porcupineProfiler?.Reset();
cheetahProfiler?.Reset();
Console.WriteLine(
Expand All @@ -193,7 +194,8 @@ private void OnWakeWordDetected(object? sender, EventArgs e)

private void OnUserInputReceived(object? sender, string userInput)
{
ProfilerEvent?.Invoke(null, cheetahProfiler);
profilerEvent?.Invoke(null, cheetahProfiler);
delayProfiler?.Tick();
_transcript = "";
AudioReceived -= CheetahOnAudioReceived;
AudioReceived += PorcupineOnAudioReceived;
Expand All @@ -215,7 +217,7 @@ public Generator(Listener listener)
listener.UserInputReceived += OnUserInputReceived;
listener.WakeWordDetected += OnWakeWordDetected;
CompletionGenerationCompleted += (_, endpoint) =>
ProfilerEvent?.Invoke(null, pllmProfiler);
profilerEvent?.Invoke(null, pllmProfiler);
Task.Run(llmWorker).ContinueWith((t) => Console.WriteLine(t.Exception));
}

Expand Down Expand Up @@ -327,7 +329,7 @@ void OnCompletionGenerationCompleted(object? _, PicoLLMEndpoint endpoint)
orcaProfiler?.Tick();
var pcm = orcaStream.Flush();
orcaProfiler?.Tock(pcm);
ProfilerEvent?.Invoke(null, orcaProfiler);
profilerEvent?.Invoke(null, orcaProfiler);
if (pcm != null)
{
audioChannel.Writer.TryWrite(pcm);
Expand All @@ -337,6 +339,7 @@ void OnCompletionGenerationCompleted(object? _, PicoLLMEndpoint endpoint)
generator.PartialCompletionGenerated -= OnPartialCompletion;
generator.CompletionGenerationCompleted -=
OnCompletionGenerationCompleted;
profilerEvent?.Invoke(null, delayProfiler);
}
generator.PartialCompletionGenerated += OnPartialCompletion;
generator.CompletionGenerationCompleted +=
Expand All @@ -356,6 +359,7 @@ void OnCompletionGenerationCompleted(object? _, PicoLLMEndpoint endpoint)
TTS_WARMUP_SECONDS * _orca.SampleRate ||
audioChannel.Reader.Completion.IsCompleted))
{
delayProfiler?.Tock();
isStarted = true;
}
if (isStarted)
Expand Down Expand Up @@ -581,12 +585,13 @@ static void Main(string[] args)

if (PROFILE)
{
ProfilerEvent += (_, profiler) =>
profilerEvent += (_, profiler) =>
Console.WriteLine($"[{profiler?.Stats()}]");
orcaProfiler = new RTFProfiler("Orca");
porcupineProfiler = new RTFProfiler("Porcupine");
cheetahProfiler = new RTFProfiler("Cheetah");
pllmProfiler = new TPSProfiler("PicoLLM");
delayProfiler = new DelayProfiler();
}
Listener listener = new Listener();
Generator generator = new Generator(listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public double RTF()

public string Stats()
{
return $"{Name} RTF: {RTF():F3}"; // Format to 3 decimal places
return $"{Name} RTF: {RTF():F2}"; // Format to 3 decimal places
}

public void Reset()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public double TPS()

public string Stats()
{
return $"{Name} TPS: {TPS()}";
return $"{Name} TPS: {TPS():F2}";
}

public void Reset()
Expand Down

0 comments on commit a42ec2c

Please sign in to comment.