-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrmVlmBenchmark.cs
More file actions
944 lines (821 loc) · 42.2 KB
/
FrmVlmBenchmark.cs
File metadata and controls
944 lines (821 loc) · 42.2 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
using LLama;
using LLama.Common;
using LLama.Native;
using LLama.Sampling;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.Text;
using System.Text.Json;
namespace VLMImageFidelityBenchmarkTool
{
public partial class FrmVlmBenchmark : Form
{
private string modelPath = string.Empty;
private string multiModalProj = string.Empty;
private string imagePath = string.Empty;
// Loaded model state (null until Load Model is called)
private LLamaWeights? _model;
private MtmdWeights? _clipModel;
private ModelParams? _modelParams;
private MtmdContextParams _mtmdParameters;
private readonly System.Windows.Forms.Timer _statsTimer = new() { Interval = 2000 };
private float _lastCpuPct = 0f;
private TimeSpan _lastCpuTime = TimeSpan.Zero;
private DateTime _lastCpuSample = DateTime.UtcNow;
private bool _isBusy = false;
private readonly List<string[]> _runLog = new();
private int _sortCol = -1;
private bool _sortAsc = true;
// Run log column indices
private const int ColTime = 0, ColFile = 1, ColTokens = 2, ColTps = 3,
ColTtft = 4, ColSize = 5, ColScale = 6, ColQuality = 7,
ColTemp = 8, ColStopped = 9;
public FrmVlmBenchmark()
{
InitializeComponent();
}
private void FrmApiTests_Load(object sender, EventArgs e)
{
modelToolStripMenuItem.Click += ModelMenuItem_Click;
projToolStripMenuItem.Click += ProjMenuItem_Click;
loadModelToolStripMenuItem.Click += async (s, ev) => await LoadModelAsync();
resetDefaultsToolStripMenuItem.Click += ResetDefaultsMenuItem_Click;
btnImage.Click += BtnImage_Click;
btnResetImage.Click += (s, ev) => { trkScale.Value = 100; trkQuality.Value = 95; UpdatePreview(); SaveSettings(); };
btnSaveReport.Click += BtnSaveReport_Click;
lvRunLog.ColumnClick += LvRunLog_ColumnClick;
chkSweepScale.CheckedChanged += (s, ev) => SaveSettings();
chkSweepQuality.CheckedChanged += (s, ev) => SaveSettings();
chkSweepTemp.CheckedChanged += (s, ev) => SaveSettings();
nudTempStart.ValueChanged += (s, ev) => SaveSettings();
nudTempEnd.ValueChanged += (s, ev) => SaveSettings();
nudTempStep.ValueChanged += (s, ev) => SaveSettings();
btnRunTest.Click += async (s, ev) => await RunAutoTestAsync();
btnStopTest.Click += (s, ev) => { _testCts?.Cancel(); SetStatus("Stopping test…"); };
trkScale.Scroll += (s, ev) => { UpdatePreview(); SaveSettings(); };
trkQuality.Scroll += (s, ev) => { UpdatePreview(); SaveSettings(); };
// Save on any text/value change so a crash loses nothing
txtSystemPrompt.TextChanged += (s, ev) => SaveSettings();
txtTestPrompt.TextChanged += (s, ev) => SaveSettings();
txtMessage.TextChanged += (s, ev) => SaveSettings();
nudScaleStart.ValueChanged += (s, ev) => SaveSettings();
nudScaleEnd.ValueChanged += (s, ev) => SaveSettings();
nudScaleStep.ValueChanged += (s, ev) => SaveSettings();
nudQualityStart.ValueChanged += (s, ev) => SaveSettings();
nudQualityEnd.ValueChanged += (s, ev) => SaveSettings();
nudQualityStep.ValueChanged += (s, ev) => SaveSettings();
nudScalePasses.ValueChanged += (s, ev) => SaveSettings();
nudQualityPasses.ValueChanged += (s, ev) => SaveSettings();
_statsTimer.Tick += StatsTimer_Tick;
_statsTimer.Start();
RestoreSettings();
SetStatus("Ready");
}
private void ResetDefaultsMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Reset all settings to defaults?", "Reset Defaults",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) return;
txtSystemPrompt.Text = "You are a helpful assistant.";
txtTestPrompt.Text = "Describe what is in this image.";
txtMessage.Text = string.Empty;
trkScale.Value = 100;
trkQuality.Value = 95;
nudScaleStart.Value = 100;
nudScaleEnd.Value = 10;
nudScaleStep.Value = 25;
nudQualityStart.Value = 95;
nudQualityEnd.Value = 1;
nudQualityStep.Value = 10;
nudScalePasses.Value = 4;
nudQualityPasses.Value = 4;
chkSweepScale.Checked = true;
chkSweepQuality.Checked = true;
chkSweepTemp.Checked = false;
nudTempStart.Value = nudTempStart.Maximum >= 1.00m ? 1.00m : nudTempStart.Maximum;
nudTempEnd.Value = nudTempEnd.Minimum;
nudTempStep.Value = nudTempStep.Minimum <= 0.10m && nudTempStep.Maximum >= 0.10m ? 0.10m : nudTempStep.Minimum;
UpdatePreview();
SaveSettings();
SetStatus("Settings reset to defaults.");
}
// ── Settings ─────────────────────────────────────────────────────────
private static string SettingsPath =>
Path.Combine(AppContext.BaseDirectory, "VLMImageFidelityBenchmarkTool.settings.json");
private void SaveSettings()
{
try
{
var d = new Dictionary<string, string>
{
["modelPath"] = modelPath,
["multiModalProj"] = multiModalProj,
["imagePath"] = imagePath,
["systemPrompt"] = txtSystemPrompt.Text,
["testPrompt"] = txtTestPrompt.Text,
["lastMessage"] = txtMessage.Text,
["trkScale"] = trkScale.Value.ToString(),
["trkQuality"] = trkQuality.Value.ToString(),
["nudScaleStart"] = nudScaleStart.Value.ToString(),
["nudScaleEnd"] = nudScaleEnd.Value.ToString(),
["nudScaleStep"] = nudScaleStep.Value.ToString(),
["nudQualityStart"] = nudQualityStart.Value.ToString(),
["nudQualityEnd"] = nudQualityEnd.Value.ToString(),
["nudQualityStep"] = nudQualityStep.Value.ToString(),
["nudScalePasses"] = nudScalePasses.Value.ToString(),
["nudQualityPasses"] = nudQualityPasses.Value.ToString(),
["chkSweepScale"] = chkSweepScale.Checked.ToString(),
["chkSweepQuality"] = chkSweepQuality.Checked.ToString(),
["chkSweepTemp"] = chkSweepTemp.Checked.ToString(),
["nudTempStart"] = nudTempStart.Value.ToString(),
["nudTempEnd"] = nudTempEnd.Value.ToString(),
["nudTempStep"] = nudTempStep.Value.ToString(),
};
File.WriteAllText(SettingsPath,
JsonSerializer.Serialize(d, new JsonSerializerOptions { WriteIndented = true }));
}
catch { }
}
private void RestoreSettings()
{
try
{
if (!File.Exists(SettingsPath)) return;
var d = JsonSerializer.Deserialize<Dictionary<string, string>>(
File.ReadAllText(SettingsPath));
if (d == null) return;
if (d.TryGetValue("modelPath", out var mp)) modelPath = mp;
if (d.TryGetValue("multiModalProj", out var mmp)) multiModalProj = mmp;
if (d.TryGetValue("systemPrompt", out var sp) && !string.IsNullOrEmpty(sp))
txtSystemPrompt.Text = sp;
if (d.TryGetValue("testPrompt", out var tp) && !string.IsNullOrEmpty(tp))
txtTestPrompt.Text = tp;
if (d.TryGetValue("lastMessage", out var lm))
txtMessage.Text = lm;
if (d.TryGetValue("trkScale", out var trkScaleVal) && int.TryParse(trkScaleVal, out int tsv))
trkScale.Value = Math.Clamp(tsv, trkScale.Minimum, trkScale.Maximum);
if (d.TryGetValue("trkQuality", out var trkQualVal) && int.TryParse(trkQualVal, out int tqv))
trkQuality.Value = Math.Clamp(tqv, trkQuality.Minimum, trkQuality.Maximum);
RestoreNud(d, "nudScaleStart", nudScaleStart);
RestoreNud(d, "nudScaleEnd", nudScaleEnd);
RestoreNud(d, "nudScaleStep", nudScaleStep);
RestoreNud(d, "nudQualityStart", nudQualityStart);
RestoreNud(d, "nudQualityEnd", nudQualityEnd);
RestoreNud(d, "nudQualityStep", nudQualityStep);
RestoreNud(d, "nudScalePasses", nudScalePasses);
RestoreNud(d, "nudQualityPasses", nudQualityPasses);
if (d.TryGetValue("chkSweepScale", out var css) && bool.TryParse(css, out bool bss)) chkSweepScale.Checked = bss;
if (d.TryGetValue("chkSweepQuality", out var csq) && bool.TryParse(csq, out bool bsq)) chkSweepQuality.Checked = bsq;
if (d.TryGetValue("chkSweepTemp", out var cst) && bool.TryParse(cst, out bool bst)) chkSweepTemp.Checked = bst;
RestoreNud(d, "nudTempStart", nudTempStart);
RestoreNud(d, "nudTempEnd", nudTempEnd);
RestoreNud(d, "nudTempStep", nudTempStep);
if (d.TryGetValue("imagePath", out var ip) && !string.IsNullOrEmpty(ip) && File.Exists(ip))
{
imagePath = ip;
txtImagePath.Text = ip;
pbOriginal.Image?.Dispose();
pbOriginal.Image = Image.FromFile(ip);
pbOriginal.SizeMode = PictureBoxSizeMode.Zoom;
UpdatePreview();
}
}
catch { }
}
private static void RestoreNud(Dictionary<string, string> d, string key, NumericUpDown nud)
{
if (d.TryGetValue(key, out var val) && decimal.TryParse(val, out decimal v))
nud.Value = Math.Clamp(v, nud.Minimum, nud.Maximum);
}
// ── Menu: Select Model ──────────────────────────────────────────────
private void ModelMenuItem_Click(object sender, EventArgs e)
{
using var dlg = new OpenFileDialog
{
Title = "Select Model (.gguf)",
Filter = "GGUF files (*.gguf)|*.gguf|All files (*.*)|*.*"
};
if (dlg.ShowDialog() == DialogResult.OK)
{
modelPath = dlg.FileName;
SaveSettings();
SetStatus($"Model selected: {Path.GetFileName(modelPath)}");
}
}
private void ProjMenuItem_Click(object sender, EventArgs e)
{
using var dlg = new OpenFileDialog
{
Title = "Select Multimodal Projection (.gguf)",
Filter = "GGUF files (*.gguf)|*.gguf|All files (*.*)|*.*"
};
if (dlg.ShowDialog() == DialogResult.OK)
{
multiModalProj = dlg.FileName;
SaveSettings();
SetStatus($"Proj selected: {Path.GetFileName(multiModalProj)}");
}
}
// ── Menu: Load Model ────────────────────────────────────────────────
private async Task LoadModelAsync()
{
if (string.IsNullOrEmpty(modelPath) || string.IsNullOrEmpty(multiModalProj))
{
MessageBox.Show("Please select both a model and a projection file first.",
"Missing files", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
SetBusy(true, "Loading model…");
try
{
// Dispose any previously loaded model
DisposeModel();
_modelParams = new ModelParams(modelPath)
{
Threads = 9,
ContextSize = 1024 * 8,
GpuLayerCount = 0,
UseMemoryLock = true,
FlashAttention = true,
};
_mtmdParameters = MtmdContextParams.Default();
_mtmdParameters.UseGpu = false;
_mtmdParameters.NThreads = 9;
// These are already async — do not wrap in Task.Run
_model = await LLamaWeights.LoadFromFileAsync(_modelParams);
_clipModel = await MtmdWeights.LoadFromFileAsync(multiModalProj, _model, _mtmdParameters);
if (_clipModel != null && !_clipModel.SupportsVision)
{
SetStatus("Warning: model does not support vision.");
}
else
{
SetStatus("Model loaded and ready.");
}
}
catch (Exception ex)
{
SetStatus($"Error loading model: {ex.Message}");
MessageBox.Show(ex.Message, "Load Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
SetBusy(false);
}
}
// ── Image Button ────────────────────────────────────────────────────
private void BtnImage_Click(object sender, EventArgs e)
{
using var dlg = new OpenFileDialog
{
Title = "Select Image",
Filter = "Image files (*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp|All files (*.*)|*.*"
};
if (dlg.ShowDialog() != DialogResult.OK) return;
imagePath = dlg.FileName;
txtImagePath.Text = imagePath;
SaveSettings();
try
{
pbOriginal.Image?.Dispose();
pbOriginal.Image = Image.FromFile(imagePath);
pbOriginal.SizeMode = PictureBoxSizeMode.Zoom;
UpdatePreview();
SetStatus($"Image loaded: {Path.GetFileName(imagePath)}");
}
catch (Exception ex)
{
SetStatus($"Error loading image: {ex.Message}");
}
}
// ── Image preview ────────────────────────────────────────────────────
private string? _previewImagePath;
private void UpdatePreview()
{
if (pbOriginal.Image == null) return;
try
{
int scale = trkScale.Value;
int quality = trkQuality.Value;
int w = Math.Max(1, (int)(pbOriginal.Image.Width * scale / 100.0));
int h = Math.Max(1, (int)(pbOriginal.Image.Height * scale / 100.0));
var bmp = new Bitmap(w, h);
using (var g = Graphics.FromImage(bmp))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(pbOriginal.Image, 0, 0, w, h);
}
var encoder = ImageCodecInfo.GetImageEncoders().First(c => c.MimeType == "image/jpeg");
var encParams = new EncoderParameters(1);
encParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);
string newPath = Path.Combine(Path.GetTempPath(), $"apex_preview_{Guid.NewGuid():N}.jpg");
bmp.Save(newPath, encoder, encParams);
bmp.Dispose();
var newImage = Image.FromFile(newPath);
var oldImage = pbPreview.Image;
pbPreview.Image = newImage;
pbPreview.SizeMode = PictureBoxSizeMode.Zoom;
oldImage?.Dispose();
if (_previewImagePath != null && File.Exists(_previewImagePath))
try { File.Delete(_previewImagePath); } catch { }
_previewImagePath = newPath;
lblScale.Text = $"Scale: {scale}% ({w}×{h})";
lblQuality.Text = $"Quality: {quality}";
}
catch (Exception ex)
{
SetStatus($"Preview error: {ex.Message}");
}
}
// ── Scroll helper ────────────────────────────────────────────────────
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
private const int WM_VSCROLL = 0x115;
private const int SB_BOTTOM = 7;
private void ScrollChatToBottom() =>
SendMessage(rtbChat.Handle, WM_VSCROLL, SB_BOTTOM, 0);
// ── Colored chat helpers ─────────────────────────────────────────────
private static readonly Color ChatColorUser = Color.FromArgb(0, 70, 160); // rich blue
private static readonly Color ChatColorAssistant = Color.FromArgb(20, 120, 50); // forest green
private static readonly Color ChatColorMeta = Color.FromArgb(120, 80, 160); // soft purple for test headers
private static readonly Color ChatColorError = Color.FromArgb(180, 30, 30); // red
private void AppendChat(string text, Color color, bool bold = false)
{
rtbChat.SelectionStart = rtbChat.TextLength;
rtbChat.SelectionLength = 0;
rtbChat.SelectionColor = color;
rtbChat.SelectionFont = bold
? new Font(rtbChat.Font, FontStyle.Bold)
: rtbChat.Font;
rtbChat.AppendText(text);
rtbChat.SelectionColor = rtbChat.ForeColor;
rtbChat.SelectionFont = rtbChat.Font;
}
// ── Send Button ─────────────────────────────────────────────────────
private async void button1_Click(object sender, EventArgs e)
{
if (_model == null || _clipModel == null || _modelParams == null)
{
MessageBox.Show("Please load a model first via File > Load model.",
"No model", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
string userMessage = txtMessage.Text.Trim();
if (string.IsNullOrEmpty(userMessage))
return;
SetBusy(true, "Generating…");
AppendChat("You: ", ChatColorUser, bold: true);
AppendChat(userMessage + "\r\n", Color.Black);
ScrollChatToBottom();
txtMessage.Clear();
bool firstToken = true;
try
{
const int maxTokens = 8096;
// Capture locals for use inside Task.Run
string capturedImagePath = _previewImagePath ?? imagePath;
var mediaMarker = _mtmdParameters.MediaMarker ?? NativeApi.MtmdDefaultMarker() ?? "[img-1]";
bool hasImage = !string.IsNullOrEmpty(capturedImagePath) && File.Exists(capturedImagePath);
string imageTag = hasImage ? mediaMarker : string.Empty;
string systemPrompt = txtSystemPrompt.Text.Trim();
if (string.IsNullOrEmpty(systemPrompt)) systemPrompt = "You are a helpful assistant.";
string prompt =
$"<|startoftext|><|im_start|>system\n{systemPrompt}<|im_end|>\n" +
$"<|im_start|>user\n{userMessage}{imageTag}\n<|im_end|>\n" +
$"<|im_start|>assistant\n";
var inferenceParams = new InferenceParams
{
SamplingPipeline = new DefaultSamplingPipeline
{
Temperature = 1.0f,
//MinKeep = 100,
//Seed = 8675309,
//TopP = 0.95f,
//MinP = 0.10f,
//TopK = .75,
// Number between -2.0 and 2.0. Positive values penalize new tokens based on whether
// they appear in the text so far, increasing the model's likelihood to talk about
// new topics.
//PresencePenalty = 0, //
// Number between -2.0 and 2.0. Positive values penalize new tokens based on their
// existing frequency in the text so far, decreasing the model's likelihood to repeat
// the same line verbatim.
//FrequencyPenalty = 0,
//GrammarOptimization
// None = 0, No grammar optimization, slow because it has to apply the grammar to the entire vocab.
// Basic = 1, Attempts to return early by only applying the grammar to the selected token and checking if it's valid.
// Extended = 2, Attempts to return early by applying the grammar to the top K tokens and checking if the selected token is valid.
GrammarOptimization = DefaultSamplingPipeline.GrammarOptimizationMode.Basic
},
MaxTokens = maxTokens,
AntiPrompts = new List<string> { "User:\n" }
};
await Task.Run(async () =>
{
// Fresh context per inference — matches the original code's behaviour
using var context = _model!.CreateContext(_modelParams!);
var executor = new InteractiveExecutor(context, _clipModel!);
if (hasImage)
{
executor.Embeds.Clear();
var embed = _clipModel!.LoadMedia(capturedImagePath);
executor.Embeds.Add(embed);
}
await foreach (var token in executor.InferAsync(prompt, inferenceParams))
{
if (!token.Contains("User:\n", StringComparison.OrdinalIgnoreCase))
{
Invoke(() =>
{
if (firstToken)
{
AppendChat("Assistant: ", ChatColorAssistant, bold: true);
firstToken = false;
ScrollChatToBottom();
}
AppendChat(token, Color.Black);
});
}
}
});
Invoke(() => { AppendChat("\r\n", Color.Black); ScrollChatToBottom(); });
SetStatus("Done.");
}
catch (Exception ex)
{
SetStatus($"Inference error: {ex.Message}");
Invoke(() => AppendChat($"\r\n[Error: {ex.Message}]\r\n", ChatColorError));
}
finally
{
SetBusy(false);
}
}
// ── Status helpers ──────────────────────────────────────────────────
private void SetStatus(string message) =>
Invoke(() => statusLabel.Text = message);
private void SetBusy(bool busy, string? message = null)
{
_isBusy = busy;
Invoke(() =>
{
btnSend.Enabled = !busy;
loadModelToolStripMenuItem.Enabled = !busy;
if (message != null)
statusLabel.Text = message;
});
}
private void StatsTimer_Tick(object? sender, EventArgs e)
{
if (_isBusy) return;
try
{
var proc = Process.GetCurrentProcess();
var now = DateTime.UtcNow;
var cpu = proc.TotalProcessorTime;
var elapsed = (now - _lastCpuSample).TotalSeconds;
if (elapsed > 0.1)
{
_lastCpuPct = (float)((cpu - _lastCpuTime).TotalSeconds / elapsed / Environment.ProcessorCount * 100.0);
_lastCpuTime = cpu;
_lastCpuSample = now;
}
long procRamMb = proc.WorkingSet64 / (1024 * 1024);
long totalRamMb = (long)(GC.GetGCMemoryInfo().TotalAvailableMemoryBytes / (1024 * 1024));
var text = $"CPU {_lastCpuPct:F1}% Proc RAM: {procRamMb} MB System RAM: {totalRamMb:N0} MB";
var cpuColor = _lastCpuPct > 70f ? Color.OrangeRed
: _lastCpuPct > 40f ? Color.DarkOrange
: SystemColors.ControlText;
Invoke(() =>
{
statsLabel.Text = text;
statsLabel.ForeColor = cpuColor;
});
}
catch { /* non-critical */ }
}
// ── Auto-test ────────────────────────────────────────────────────────
private CancellationTokenSource? _testCts;
private async Task RunAutoTestAsync()
{
if (_model == null || _clipModel == null || _modelParams == null)
{
MessageBox.Show("Please load a model first.", "No model",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (string.IsNullOrEmpty(imagePath) || !File.Exists(imagePath))
{
MessageBox.Show("Please load an image first.", "No image",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
bool sweepScale = chkSweepScale.Checked;
bool sweepQuality = chkSweepQuality.Checked;
bool sweepTemp = chkSweepTemp.Checked;
if (!sweepScale && !sweepQuality && !sweepTemp)
{
MessageBox.Show("Enable at least one sweep axis (Scale, Quality, or Temperature).",
"Nothing to sweep", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
string testPrompt = txtTestPrompt.Text.Trim();
if (string.IsNullOrEmpty(testPrompt)) testPrompt = "Describe what is in this image.";
// Build sweep lists — always high-to-low
var scales = sweepScale ? BuildIntList((int)nudScaleStart.Value, (int)nudScaleEnd.Value, (int)nudScaleStep.Value) : new List<int> { (int)trkScale.Value };
var qualities = sweepQuality ? BuildIntList((int)nudQualityStart.Value, (int)nudQualityEnd.Value, (int)nudQualityStep.Value) : new List<int> { (int)trkQuality.Value };
var temps = sweepTemp ? BuildDecimalList(nudTempStart.Value, nudTempEnd.Value, nudTempStep.Value) : new List<decimal> { 1.0m };
int total = scales.Count * qualities.Count * temps.Count;
int run = 0;
_testCts = new CancellationTokenSource();
var ct = _testCts.Token;
Invoke(() => { btnRunTest.Enabled = false; btnStopTest.Enabled = true; });
SetBusy(true, $"Auto-test: 0/{total}");
try
{
foreach (int scale in scales)
{
if (ct.IsCancellationRequested) break;
foreach (int quality in qualities)
{
if (ct.IsCancellationRequested) break;
string? testImg = BuildTestImage(scale, quality);
bool hasImg = testImg != null && File.Exists(testImg);
foreach (decimal temp in temps)
{
if (ct.IsCancellationRequested) break;
run++;
SetStatus($"Auto-test: {run}/{total} — Scale {scale}% Q {quality} Temp {temp:F2}");
bool firstToken = true;
string header = $"[Test {run}/{total}]";
if (sweepScale) header += $" Scale:{scale}%";
if (sweepQuality) header += $" Q:{quality}";
if (sweepTemp) header += $" Temp:{temp:F2}";
Invoke(() => { AppendChat(header + "\r\n", ChatColorMeta, bold: true); ScrollChatToBottom(); });
await Task.Run(async () =>
{
var mediaMarker = _mtmdParameters.MediaMarker ?? NativeApi.MtmdDefaultMarker() ?? "[img-1]";
string imageTag = hasImg ? mediaMarker : string.Empty;
string sysPrompt = string.Empty;
Invoke(() => { sysPrompt = txtSystemPrompt.Text.Trim(); });
if (string.IsNullOrEmpty(sysPrompt)) sysPrompt = "You are a helpful assistant.";
string prompt =
$"<|startoftext|><|im_start|>system\n{sysPrompt}<|im_end|>\n" +
$"<|im_start|>user\n{testPrompt}{imageTag}\n<|im_end|>\n" +
$"<|im_start|>assistant\n";
var inferenceParams = new InferenceParams
{
SamplingPipeline = new DefaultSamplingPipeline
{
Temperature = (float)temp,
GrammarOptimization = DefaultSamplingPipeline.GrammarOptimizationMode.Basic
},
MaxTokens = 8096,
AntiPrompts = new List<string> { "User:\n" }
};
var sw = Stopwatch.StartNew();
long ttft = -1;
int tokens = 0;
using var context = _model!.CreateContext(_modelParams!);
var executor = new InteractiveExecutor(context, _clipModel!);
if (hasImg)
{
executor.Embeds.Clear();
executor.Embeds.Add(_clipModel!.LoadMedia(testImg!));
}
await foreach (var tok in executor.InferAsync(prompt, inferenceParams, ct))
{
if (tok.Contains("User:\n", StringComparison.OrdinalIgnoreCase)) break;
if (ttft < 0) ttft = sw.ElapsedMilliseconds;
tokens++;
Invoke(() =>
{
if (firstToken) { AppendChat("Response: ", ChatColorAssistant, bold: true); firstToken = false; ScrollChatToBottom(); }
AppendChat(tok, Color.Black);
});
}
sw.Stop();
bool stopped = ct.IsCancellationRequested;
long ttftMs = ttft >= 0 ? ttft : 0;
double genMs = ttft >= 0 ? sw.ElapsedMilliseconds - ttft : 0;
double tps = genMs > 0 ? tokens / (genMs / 1000.0) : 0;
int w = Math.Max(1, (int)(pbOriginal.Image!.Width * scale / 100.0));
int h = Math.Max(1, (int)(pbOriginal.Image!.Height * scale / 100.0));
Invoke(() =>
{
AppendChat($"\r\n[{tokens} tokens {tps:F1} tok/s TTFT {ttftMs}ms{(stopped ? " stopped" : "")}]\r\n", ChatColorMeta);
AddRunLogEntry(DateTime.Now.ToString("HH:mm:ss"),
Path.GetFileName(imagePath), tokens, tps, ttftMs,
$"{w}×{h}", scale, quality, temp, stopped);
ScrollChatToBottom();
});
}, ct);
}
if (testImg != null && File.Exists(testImg))
try { File.Delete(testImg); } catch { }
}
}
}
catch (OperationCanceledException) { }
finally
{
_testCts?.Dispose();
_testCts = null;
Invoke(() => { btnRunTest.Enabled = true; btnStopTest.Enabled = false; });
SetBusy(false, "Auto-test complete.");
AutoSaveTestResults();
}
}
private static List<int> BuildIntList(int start, int end, int step)
{
int from = Math.Max(start, end);
int to = Math.Min(start, end);
step = Math.Max(1, step);
var list = new List<int>();
for (int v = from; v >= to; v -= step)
list.Add(Math.Max(v, to));
if (list.Count == 0 || list[^1] != to) list.Add(to);
return list;
}
private static List<decimal> BuildDecimalList(decimal start, decimal end, decimal step)
{
decimal from = Math.Max(start, end);
decimal to = Math.Min(start, end);
step = Math.Max(0.01m, step);
var list = new List<decimal>();
for (decimal v = from; v >= to; v -= step)
list.Add(Math.Max(decimal.Round(v, 2), to));
if (list.Count == 0 || list[^1] != to) list.Add(to);
return list;
}
private void AutoSaveTestResults()
{
if (_runLog.Count == 0) return;
try
{
string resultsDir = Path.Combine(AppContext.BaseDirectory, "TestResults");
Directory.CreateDirectory(resultsDir);
string modelName = string.IsNullOrEmpty(modelPath)
? "unknown"
: Path.GetFileNameWithoutExtension(modelPath);
// Sanitise for filename
foreach (char c in Path.GetInvalidFileNameChars())
modelName = modelName.Replace(c, '_');
string fileName = $"{modelName}_{DateTime.Now:yyyyMMdd_HHmmss}.csv";
string filePath = Path.Combine(resultsDir, fileName);
WriteRunLogCsv(filePath, _runLog);
SetStatus($"Auto-test complete. Results saved: TestResults\\{fileName}");
}
catch { /* non-critical */ }
}
private static void WriteRunLogCsv(string filePath, List<string[]> log)
{
string[] headers = ["Time", "File", "Tokens", "Tok/s", "TTFT (ms)", "Size", "Scale %", "Quality", "Temp", "Stopped"];
var sb = new StringBuilder();
sb.AppendLine(string.Join(",", headers.Select(h => $"\"{h}\"")));
foreach (var r in log)
sb.AppendLine(string.Join(",", r.Select(v => $"\"{v.Replace("\"", "\"\"")}\"")));
File.WriteAllText(filePath, sb.ToString(), Encoding.UTF8);
}
private string? BuildTestImage(int scale, int quality)
{
if (pbOriginal.Image == null) return null;
try
{
int w = Math.Max(1, (int)(pbOriginal.Image.Width * scale / 100.0));
int h = Math.Max(1, (int)(pbOriginal.Image.Height * scale / 100.0));
var bmp = new Bitmap(w, h);
using (var g = Graphics.FromImage(bmp))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(pbOriginal.Image, 0, 0, w, h);
}
var encoder = ImageCodecInfo.GetImageEncoders().First(c => c.MimeType == "image/jpeg");
var encParams = new EncoderParameters(1);
encParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);
string path = Path.Combine(Path.GetTempPath(), $"apex_test_{Guid.NewGuid():N}.jpg");
bmp.Save(path, encoder, encParams);
bmp.Dispose();
return path;
}
catch { return null; }
}
// ── Run log ─────────────────────────────────────────────────────────
private void LvRunLog_ColumnClick(object? sender, ColumnClickEventArgs e)
{
if (_sortCol == e.Column)
_sortAsc = !_sortAsc;
else
{
_sortCol = e.Column;
_sortAsc = true;
}
RefreshListView();
}
// Numeric columns — sort as number, fall back to string
private static readonly HashSet<int> NumericCols = [ColTokens, ColTps, ColTtft, ColScale, ColQuality, ColTemp];
private void RefreshListView()
{
IEnumerable<string[]> ordered = _runLog;
if (_sortCol >= 0)
{
if (NumericCols.Contains(_sortCol))
ordered = _sortAsc
? _runLog.OrderBy(r => double.TryParse(r[_sortCol], out double v) ? v : 0)
: _runLog.OrderByDescending(r => double.TryParse(r[_sortCol], out double v) ? v : 0);
else
ordered = _sortAsc
? _runLog.OrderBy(r => r[_sortCol], StringComparer.OrdinalIgnoreCase)
: _runLog.OrderByDescending(r => r[_sortCol], StringComparer.OrdinalIgnoreCase);
}
lvRunLog.BeginUpdate();
lvRunLog.Items.Clear();
foreach (var row in ordered)
{
var item = new ListViewItem(row[ColTime]);
for (int i = 1; i < row.Length; i++) item.SubItems.Add(row[i]);
lvRunLog.Items.Add(item);
}
lvRunLog.EndUpdate();
}
private void AddRunLogEntry(string time, string file, int tokens, double tps, long ttft,
string size, int scale, int quality, decimal temp, bool stopped)
{
string[] row = [
time, file, tokens.ToString(), tps.ToString("F1"),
ttft.ToString(), size, scale.ToString(), quality.ToString(),
temp.ToString("F2"), stopped ? "Yes" : "No"
];
_runLog.Insert(0, row);
RefreshListView();
}
private void BtnSaveReport_Click(object sender, EventArgs e)
{
if (_runLog.Count == 0)
{
MessageBox.Show("No run data to save.", "Empty log",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
using var dlg = new SaveFileDialog
{
Title = "Save Run Report",
Filter = "CSV files (*.csv)|*.csv|JSON files (*.json)|*.json",
FileName = $"apex_report_{DateTime.Now:yyyyMMdd_HHmmss}",
DefaultExt = "csv",
};
if (dlg.ShowDialog() != DialogResult.OK) return;
try
{
string[] headers = ["Time", "File", "Tokens", "Tok/s", "TTFT (ms)", "Size", "Scale %", "Quality", "Temp", "Stopped"];
var sorted = _runLog
.OrderByDescending(r => double.TryParse(r[ColTps], out double v) ? v : 0)
.ToList();
bool json = dlg.FilterIndex == 2;
if (json)
{
var rows = sorted.Select(r =>
{
var d = new Dictionary<string, string>();
for (int i = 0; i < headers.Length; i++) d[headers[i]] = r[i];
return d;
}).ToList();
File.WriteAllText(dlg.FileName,
JsonSerializer.Serialize(rows, new JsonSerializerOptions { WriteIndented = true }),
Encoding.UTF8);
}
else
{
WriteRunLogCsv(dlg.FileName, sorted);
}
SetStatus($"Report saved: {Path.GetFileName(dlg.FileName)}");
}
catch (Exception ex)
{
MessageBox.Show($"Failed to save: {ex.Message}", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// ── Cleanup ─────────────────────────────────────────────────────────
private void DisposeModel()
{
_clipModel?.Dispose();
_model?.Dispose();
_clipModel = null;
_model = null;
_modelParams = null;
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
_statsTimer.Stop();
_statsTimer.Dispose();
DisposeModel();
var oldPreview = pbPreview.Image;
pbPreview.Image = null;
oldPreview?.Dispose();
pbOriginal.Image?.Dispose();
if (_previewImagePath != null && File.Exists(_previewImagePath))
try { File.Delete(_previewImagePath); } catch { }
base.OnFormClosed(e);
}
}
}