forked from zhongkaifu/TensorSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGemma3Model.cs
More file actions
677 lines (579 loc) · 26.8 KB
/
Gemma3Model.cs
File metadata and controls
677 lines (579 loc) · 26.8 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
// Copyright (c) Zhongkai Fu. All rights reserved.
// https://github.com/zhongkaifu/TensorSharp
//
// This file is part of TensorSharp.
//
// TensorSharp is licensed under the BSD-3-Clause license found in the LICENSE file in the root directory of this source tree.
//
// TensorSharp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the BSD-3-Clause License for more details.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using TensorSharp;
using TensorSharp.GGML;
namespace TensorSharp.Models
{
/// <summary>
/// Gemma 3 model architecture.
/// Key features:
/// - Alternating sliding-window (local) and full causal (global) attention every 6 layers
/// - NeoX-style RoPE with different bases for local/global layers
/// - GELU activation in MLP (GeGLU: GELU(Gate) * Up)
/// - 4 RMSNorms per layer: attn_norm, post_attention_norm, ffn_norm, post_ffw_norm
/// - QK-norm (per-head RMSNorm on Q and K)
/// - Embedding scaling by sqrt(hidden_size)
/// - Tied token embedding / output weights
/// - Final logit softcapping (model-dependent)
/// </summary>
public class Gemma3Model : ModelBase
{
private const int GlobalCacheInterval = 6;
private Tensor[] _kvCacheK;
private Tensor[] _kvCacheV;
private float[] _ropeFreqsLocal;
private float[] _ropeFreqsGlobal;
private int _slidingWindow;
private int[] _cachedSWAMaskWidths;
private int _cachedSWAMaskQueryLen;
private int _cachedSWAMaskStartPos = -1;
private float _ropeLocalBase;
private float _ropeGlobalBase;
private float _ropeScale;
private float _finalLogitSoftcap;
private int _attnKeyLen;
private int _attnValLen;
private bool _hasTiedOutput;
private Gemma3VisionEncoder _visionEncoder;
private List<(Tensor embeddings, int position)> _pendingVisionEmbeddingsList = new();
public Gemma3Model(string ggufPath, BackendType backend) : base(ggufPath, backend)
{
Config = new ModelConfig { Architecture = _gguf.GetString("general.architecture") };
ParseBaseConfig();
_attnKeyLen = Config.KeyLength > 0 ? Config.KeyLength : 256;
_attnValLen = Config.ValueLength > 0 ? Config.ValueLength : 256;
_slidingWindow = (int)_gguf.GetUint32($"{Config.Architecture}.attention.sliding_window", 1024);
Config.SlidingWindow = _slidingWindow;
_ropeLocalBase = _gguf.GetFloat32($"{Config.Architecture}.rope.local.freq_base", 10000f);
_ropeGlobalBase = Config.RopeBase;
_ropeScale = Config.RopeScale;
_finalLogitSoftcap = _gguf.GetFloat32($"{Config.Architecture}.final_logit_softcapping", 0f);
if (Config.NumLayers == 34)
_ropeScale = 8.0f;
Console.WriteLine($"Model: {Config.Architecture}, Layers={Config.NumLayers}, " +
$"Hidden={Config.HiddenSize}, Heads={Config.NumHeads}, KVHeads={Config.NumKVHeads}, " +
$"KeyLen={_attnKeyLen}, ValLen={_attnValLen}, Vocab={Config.VocabSize}");
Console.WriteLine($"RoPE global={_ropeGlobalBase} local={_ropeLocalBase} scale={_ropeScale}");
Console.WriteLine($"Sliding window={_slidingWindow}, Softcap={_finalLogitSoftcap}");
int globalCount = 0;
for (int i = 0; i < Config.NumLayers; i++)
if (IsGlobalLayer(i)) globalCount++;
Console.WriteLine($"Layer types: {globalCount} global (causal), {Config.NumLayers - globalCount} local (SWA)");
ParseTokenizer();
LoadWeights();
_hasTiedOutput = !_weights.ContainsKey("output.weight") && !_quantWeights.ContainsKey("output.weight");
if (_hasTiedOutput)
Console.WriteLine(" Output tied to token_embd.weight");
FuseGateUpWeights();
PrepareCudaQuantizedWeightsForInference();
PrecomputeRoPE();
InitKVCache(ResolveConfiguredContextLength());
}
private bool IsGlobalLayer(int layer) => (layer + 1) % GlobalCacheInterval == 0;
private void PrecomputeRoPE()
{
int halfDim = _attnKeyLen / 2;
_ropeFreqsLocal = new float[halfDim];
_ropeFreqsGlobal = new float[halfDim];
for (int i = 0; i < halfDim; i++)
{
double freqLocal = 1.0 / Math.Pow(_ropeLocalBase, 2.0 * i / _attnKeyLen);
_ropeFreqsLocal[i] = (float)freqLocal;
double freqGlobal = 1.0 / Math.Pow(_ropeGlobalBase, 2.0 * i / _attnKeyLen);
_ropeFreqsGlobal[i] = (float)(freqGlobal / _ropeScale);
}
}
private void InitKVCache(int maxSeqLen)
{
_maxContextLength = maxSeqLen;
ApplyModelAlignedKvCacheDefault(_quantWeights);
DType kvDtype = _kvCacheDtype.ToDType();
_kvCacheK = new Tensor[Config.NumLayers];
_kvCacheV = new Tensor[Config.NumLayers];
for (int l = 0; l < Config.NumLayers; l++)
{
_kvCacheK[l] = new Tensor(_allocator, kvDtype, Config.NumKVHeads, maxSeqLen, _attnKeyLen);
_kvCacheV[l] = new Tensor(_allocator, kvDtype, Config.NumKVHeads, maxSeqLen, _attnValLen);
InitializeCacheTensor(_kvCacheK[l]);
InitializeCacheTensor(_kvCacheV[l]);
}
}
public override void ResetKVCache()
{
_cacheSeqLen = 0;
if (_kvCacheK != null)
{
foreach (var k in _kvCacheK)
{
ResetCacheTensor(k);
}
foreach (var v in _kvCacheV)
{
ResetCacheTensor(v);
}
}
}
public override void TruncateKVCache(int tokenCount)
{
base.TruncateKVCache(tokenCount);
if (_kvCacheK != null)
{
foreach (var k in _kvCacheK)
InvalidateTensorDeviceCache(k);
foreach (var v in _kvCacheV)
InvalidateTensorDeviceCache(v);
}
}
public void LoadVisionEncoder(string mmProjPath)
{
_visionEncoder = new Gemma3VisionEncoder(mmProjPath, _allocator);
}
public void SetVisionEmbeddings(Tensor embeddings, int insertPosition)
{
_pendingVisionEmbeddingsList.Add((embeddings, insertPosition));
}
public Gemma3VisionEncoder VisionEncoder => _visionEncoder;
public override float[] Forward(int[] tokens)
{
_forwardSw.Start();
int seqLen = tokens.Length;
int startPos = _cacheSeqLen;
long t0 = Stopwatch.GetTimestamp();
Tensor hidden = Embedding(tokens);
_embTicks += Stopwatch.GetTimestamp() - t0;
ScaleEmbedding(hidden);
if (_pendingVisionEmbeddingsList.Count > 0)
{
foreach (var (embeddings, position) in _pendingVisionEmbeddingsList)
{
InjectVisionEmbeddings(hidden, embeddings, position);
embeddings.Dispose();
}
_pendingVisionEmbeddingsList.Clear();
}
bool dumpLayers = Environment.GetEnvironmentVariable("DUMP_LAYERS") == "1";
if (seqLen > 1 && Environment.GetEnvironmentVariable("TEST_MATMUL") == "1")
{
TestMatmulPrecision(hidden, seqLen);
}
for (int l = 0; l < Config.NumLayers; l++)
{
hidden = TransformerBlock(hidden, l, seqLen, startPos);
if (dumpLayers)
DumpHiddenState(hidden, seqLen, l);
}
Tensor normed = RMSNormOp(hidden, "output_norm.weight");
hidden.Dispose();
Tensor lastHidden;
if (seqLen > 1)
{
using var lastRow = normed.Narrow(0, seqLen - 1, 1);
lastHidden = Ops.NewContiguous(lastRow);
}
else
{
lastHidden = normed.CopyRef();
}
normed.Dispose();
t0 = Stopwatch.GetTimestamp();
string outputWeight = _hasTiedOutput ? "token_embd.weight" : "output.weight";
Tensor logitsTensor = LinearForward(lastHidden, outputWeight);
_lmHeadTicks += Stopwatch.GetTimestamp() - t0;
lastHidden.Dispose();
if (_finalLogitSoftcap > 0f)
ApplyLogitSoftcap(logitsTensor);
t0 = Stopwatch.GetTimestamp();
if (_logitsBuffer == null || _logitsBuffer.Length != Config.VocabSize)
_logitsBuffer = new float[Config.VocabSize];
unsafe
{
float* ptr = GetFloatPtr(logitsTensor);
fixed (float* dst = _logitsBuffer)
Buffer.MemoryCopy(ptr, dst, Config.VocabSize * 4, Config.VocabSize * 4);
}
logitsTensor.Dispose();
_logitsCopyTicks += Stopwatch.GetTimestamp() - t0;
_cacheSeqLen += seqLen;
_forwardCount++;
_forwardSw.Stop();
return _logitsBuffer;
}
/// <summary>
/// Replace token embeddings at image placeholder positions with vision encoder output.
/// The vision embeddings tensor has shape [numTokens, projDim] where projDim == hiddenSize.
/// </summary>
private unsafe void InjectVisionEmbeddings(Tensor hidden, Tensor visionEmbeddings, int insertPos)
{
int numVisionTokens = (int)visionEmbeddings.Sizes[0];
int dim = Config.HiddenSize;
float* hPtr = GetFloatPtr(hidden);
float* vPtr = GetFloatPtr(visionEmbeddings);
for (int t = 0; t < numVisionTokens; t++)
{
float* dst = hPtr + (long)(insertPos + t) * dim;
float* src = vPtr + (long)t * dim;
Buffer.MemoryCopy(src, dst, dim * sizeof(float), dim * sizeof(float));
}
Console.WriteLine($"Injected {numVisionTokens} vision tokens at position {insertPos}");
}
private void ScaleEmbedding(Tensor hidden)
{
float scale = MathF.Sqrt(Config.HiddenSize);
Ops.Mul(hidden, hidden, scale);
}
private void ApplyLogitSoftcap(Tensor logits)
{
float cap = _finalLogitSoftcap;
Ops.Mul(logits, logits, 1f / cap);
Ops.Tanh(logits, logits);
Ops.Mul(logits, logits, cap);
}
private Tensor TransformerBlock(Tensor hidden, int layer, int seqLen, int startPos)
{
string prefix = $"blk.{layer}";
// Pre-attention norm
using var attnNormed = RMSNormOp(hidden, $"{prefix}.attn_norm.weight");
// Self-attention
using var attnOut = Attention(attnNormed, layer, prefix, seqLen, startPos);
// Post-attention norm
using var postAttnNormed = RMSNormOp(attnOut, $"{prefix}.post_attention_norm.weight");
// Residual connection
Ops.Add(postAttnNormed, postAttnNormed, hidden);
hidden.Dispose();
// Pre-FFN norm
using var ffnNormed = RMSNormOp(postAttnNormed, $"{prefix}.ffn_norm.weight");
// FFN (GeGLU with GELU activation)
using var ffnOut = FFNGelu(ffnNormed, $"{prefix}.ffn_gate_up.weight",
$"{prefix}.ffn_down.weight", seqLen);
// Post-FFN norm
using var postFfnNormed = RMSNormOp(ffnOut, $"{prefix}.post_ffw_norm.weight");
// Residual connection
var result = new Tensor(_allocator, DType.Float32, postAttnNormed.Sizes);
Ops.Add(result, postAttnNormed, postFfnNormed);
return result;
}
private Tensor FFNGelu(Tensor input, string gateUpWeightName, string downWeightName, int seqLen)
{
Tensor gateUp = LinearForward(input, gateUpWeightName);
int intermSize = Config.IntermediateSize;
int halfDim = intermSize > 0 ? intermSize : (int)(gateUp.Sizes[1] / 2);
Tensor gate, up;
if (seqLen == 1)
{
gate = gateUp.Narrow(1, 0, halfDim);
up = gateUp.Narrow(1, halfDim, halfDim);
}
else
{
using (var gView = gateUp.Narrow(1, 0, halfDim))
gate = Ops.NewContiguous(gView);
using (var uView = gateUp.Narrow(1, halfDim, halfDim))
up = Ops.NewContiguous(uView);
}
gateUp.Dispose();
Ops.GELUMul(gate, gate, up);
up.Dispose();
Tensor down = LinearForward(gate, downWeightName);
gate.Dispose();
return down;
}
private Tensor Attention(Tensor input, int layer, string prefix, int seqLen, int startPos)
{
long t0 = Stopwatch.GetTimestamp();
bool isGlobal = IsGlobalLayer(layer);
Tensor q = LinearForward(input, $"{prefix}.attn_q.weight");
Tensor k = LinearForward(input, $"{prefix}.attn_k.weight");
Tensor v = LinearForward(input, $"{prefix}.attn_v.weight");
// QK norm
if (seqLen == 1)
{
RMSNormInPlace(q, _weights[$"{prefix}.attn_q_norm.weight"], Config.NumHeads, _attnKeyLen, Config.Eps);
RMSNormInPlace(k, _weights[$"{prefix}.attn_k_norm.weight"], Config.NumKVHeads, _attnKeyLen, Config.Eps);
}
else
{
q = ApplyBatchRMSNorm(q, $"{prefix}.attn_q_norm.weight", Config.NumHeads, seqLen, _attnKeyLen);
k = ApplyBatchRMSNorm(k, $"{prefix}.attn_k_norm.weight", Config.NumKVHeads, seqLen, _attnKeyLen);
}
// Apply NeoX-style RoPE
if (seqLen == 1)
{
float[] freqs = isGlobal ? _ropeFreqsGlobal : _ropeFreqsLocal;
ApplyNeoXRoPEDecode(q, Config.NumHeads, _attnKeyLen, startPos, freqs);
ApplyNeoXRoPEDecode(k, Config.NumKVHeads, _attnKeyLen, startPos, freqs);
}
else
{
float ropeBase = isGlobal ? _ropeGlobalBase : _ropeLocalBase;
float freqScale = isGlobal ? (1.0f / _ropeScale) : 1.0f;
q = ApplyRoPEPrefill(q, Config.NumHeads, _attnKeyLen, seqLen, startPos, ropeBase, freqScale);
k = ApplyRoPEPrefill(k, Config.NumKVHeads, _attnKeyLen, seqLen, startPos, ropeBase, freqScale);
}
// Q scaling
float qScale = 1f / MathF.Sqrt(_attnKeyLen);
ScaleTensor(q, qScale);
int totalSeqLen = startPos + seqLen;
Tensor result;
if (seqLen == 1)
{
CopyToCacheDecode(_kvCacheK[layer], k, _kvCacheV[layer], v,
Config.NumKVHeads, _attnKeyLen, startPos);
int attendLen = isGlobal ? totalSeqLen : Math.Min(totalSeqLen, _slidingWindow);
int attendStart = totalSeqLen - attendLen;
result = new Tensor(_allocator, DType.Float32, 1, Config.NumHeads * _attnValLen);
AttentionDecodeWithWindow(q, _kvCacheK[layer], _kvCacheV[layer], result,
Config.NumHeads, Config.NumKVHeads, _attnKeyLen, _attnValLen,
attendStart, totalSeqLen, 1f);
}
else
{
Tensor qHeads = ReshapeToHeads(q, Config.NumHeads, seqLen, _attnKeyLen);
Tensor kHeads = ReshapeToHeads(k, Config.NumKVHeads, seqLen, _attnKeyLen);
Tensor vHeads = ReshapeToHeads(v, Config.NumKVHeads, seqLen, _attnValLen);
CopyToCache(_kvCacheK[layer], kHeads, startPos, seqLen);
CopyToCache(_kvCacheV[layer], vHeads, startPos, seqLen);
kHeads.Dispose();
vHeads.Dispose();
int groupSize = Config.NumHeads / Config.NumKVHeads;
Tensor kExpanded = ExpandKVHeads(_kvCacheK[layer], groupSize, totalSeqLen);
Tensor vExpanded = ExpandKVHeads(_kvCacheV[layer], groupSize, totalSeqLen);
using var kT = kExpanded.Transpose(1, 2);
var scores = new Tensor(_allocator, DType.Float32, Config.NumHeads, seqLen, totalSeqLen);
Ops.AddmmBatch(scores, 0, scores, 1f, qHeads, kT);
qHeads.Dispose();
kExpanded.Dispose();
int windowSize = isGlobal ? 0 : _slidingWindow;
ApplyCausalMask(scores, seqLen, totalSeqLen, windowSize);
Ops.Softmax(scores, scores);
var attnOut = new Tensor(_allocator, DType.Float32, Config.NumHeads, seqLen, _attnValLen);
Ops.AddmmBatch(attnOut, 0, attnOut, 1.0f, scores, vExpanded);
scores.Dispose();
vExpanded.Dispose();
result = ReshapeFromHeads(attnOut, Config.NumHeads, seqLen, _attnValLen);
attnOut.Dispose();
}
q.Dispose();
k.Dispose();
v.Dispose();
_attnTicks += Stopwatch.GetTimestamp() - t0;
using (result)
{
return LinearForward(result, $"{prefix}.attn_output.weight");
}
}
private Tensor ApplyBatchRMSNorm(Tensor data, string weightName, int numHeads, int seqLen, int headDim)
{
var alpha = _weights[weightName];
using var reshaped = data.View(seqLen * numHeads, headDim);
Tensor normed = Ops.RMSNorm(null, reshaped, alpha, null, Config.Eps);
data.Dispose();
Tensor flat = normed.View(seqLen, numHeads * headDim);
normed.Dispose();
return flat;
}
/// <summary>
/// NeoX-style RoPE: pairs (x[j], x[j + d/2]) — first half with second half.
/// GGML GGML_ROPE_TYPE_NEOX uses n_offset = n_dims/2.
/// </summary>
private unsafe void ApplyNeoXRoPEDecode(Tensor data, int numHeads, int headDim, int position, float[] freqs)
{
float* ptr = GetFloatPtr(data);
int halfDim = headDim / 2;
for (int h = 0; h < numHeads; h++)
{
float* head = ptr + h * headDim;
for (int j = 0; j < halfDim; j++)
{
float angle = position * freqs[j];
float cos = MathF.Cos(angle);
float sin = MathF.Sin(angle);
float x0 = head[j];
float x1 = head[j + halfDim];
head[j] = x0 * cos - x1 * sin;
head[j + halfDim] = x0 * sin + x1 * cos;
}
}
}
private Tensor ApplyRoPEPrefill(Tensor data, int numHeads, int headDim,
int seqLen, int startPos, float ropeBase, float freqScale)
{
int totalRows = seqLen * numHeads;
int[] positions = new int[totalRows];
for (int s = 0; s < seqLen; s++)
for (int h = 0; h < numHeads; h++)
positions[s * numHeads + h] = startPos + s;
using var posTensor = CreateIntTensor(positions, totalRows);
using var reshaped = data.View(1, seqLen, numHeads, headDim);
Tensor result = Ops.RoPEEx(
null, reshaped, posTensor, headDim, 2, 0,
ropeBase, freqScale,
0.0f, 1.0f, 0.0f, 0.0f);
data.Dispose();
Tensor flat = result.View(seqLen, numHeads * headDim);
result.Dispose();
return flat;
}
private void ScaleTensor(Tensor t, float scale)
{
Ops.Mul(t, t, scale);
}
/// <summary>
/// Attention decode with optional sliding window.
/// attendStart..totalSeqLen-1 is the window of positions to attend to.
/// </summary>
private unsafe void AttentionDecodeWithWindow(Tensor q, Tensor kCache, Tensor vCache,
Tensor result, int numHeads, int numKVHeads, int keyDim, int valDim,
int attendStart, int totalSeqLen, float scale)
{
float* qPtr = GetFloatPtr(q);
float* kPtr = GetFloatPtr(kCache);
float* vPtr = GetFloatPtr(vCache);
float* rPtr = GetFloatPtr(result);
int maxSeqLen = (int)kCache.Sizes[1];
int groupSize = numHeads / numKVHeads;
int attendLen = totalSeqLen - attendStart;
float* scores = stackalloc float[attendLen];
for (int h = 0; h < numHeads; h++)
{
float* qHead = qPtr + h * keyDim;
int kvHead = h / groupSize;
float* kHead = kPtr + kvHead * maxSeqLen * keyDim;
float* vHead = vPtr + kvHead * maxSeqLen * valDim;
float maxScore = float.NegativeInfinity;
for (int t = 0; t < attendLen; t++)
{
float s = VecDot(qHead, kHead + (attendStart + t) * keyDim, keyDim) * scale;
scores[t] = s;
if (s > maxScore) maxScore = s;
}
float sumExp = 0;
for (int t = 0; t < attendLen; t++)
{
float e = MathF.Exp(scores[t] - maxScore);
scores[t] = e;
sumExp += e;
}
float invSum = 1f / sumExp;
for (int t = 0; t < attendLen; t++)
scores[t] *= invSum;
float* rHead = rPtr + h * valDim;
VecZero(rHead, valDim);
for (int t = 0; t < attendLen; t++)
VecScaleAdd(rHead, vHead + (attendStart + t) * valDim, scores[t], valDim);
}
}
/// <summary>
/// Apply causal mask to attention scores with optional sliding window.
/// For sliding window, only positions within windowSize of the query are attended to.
/// </summary>
private unsafe void ApplyCausalMask(Tensor scores, int queryLen, int totalKVLen, int windowSize)
{
int startPos = totalKVLen - queryLen;
// Causal mask via native op (upper triangle)
Ops.AddCausalMask(scores, queryLen, startPos, float.NegativeInfinity);
// Sliding window mask with cached per-row widths + vectorized fill
if (windowSize > 0)
{
if (_cachedSWAMaskWidths == null ||
_cachedSWAMaskQueryLen != queryLen ||
_cachedSWAMaskStartPos != startPos)
{
_cachedSWAMaskWidths = new int[queryLen];
_cachedSWAMaskQueryLen = queryLen;
_cachedSWAMaskStartPos = startPos;
for (int q = 0; q < queryLen; q++)
_cachedSWAMaskWidths[q] = Math.Max(0, startPos + q - windowSize + 1);
}
float* sPtr = GetFloatPtr(scores);
int numHeads = (int)scores.Sizes[0];
int rowStride = queryLen * totalKVLen;
for (int h = 0; h < numHeads; h++)
{
float* headScores = sPtr + h * rowStride;
for (int q = 0; q < queryLen; q++)
{
int width = _cachedSWAMaskWidths[q];
if (width > 0)
new Span<float>(headScores + q * totalKVLen, width).Fill(float.NegativeInfinity);
}
}
InvalidateTensorDeviceCache(scores);
}
}
private unsafe void TestMatmulPrecision(Tensor hidden, int seqLen)
{
int dim = Config.HiddenSize;
Console.WriteLine($"=== Precision test: seqLen={seqLen}, dim={dim} ===");
// Test RMSNorm: batch vs single row
string normWeight = "blk.0.attn_norm.weight";
using var batchNorm = RMSNormOp(hidden, normWeight);
using var lastRow = hidden.Narrow(0, seqLen - 1, 1);
using var lastRowContig = Ops.NewContiguous(lastRow);
using var singleNorm = RMSNormOp(lastRowContig, normWeight);
CompareRows(batchNorm, singleNorm, seqLen - 1, dim, "RMSNorm");
// Test Linear on RMSNorm output
string qWeight = "blk.0.attn_q.weight";
using var batchQ = LinearForward(batchNorm, qWeight);
using var singleQ = LinearForward(singleNorm, qWeight);
int qDim = (int)batchQ.Sizes[1];
CompareRows(batchQ, singleQ, seqLen - 1, qDim, "Linear(Q)");
}
private unsafe void CompareRows(Tensor batch, Tensor single, int rowIdx, int dim, string label)
{
float* batchPtr = GetFloatPtr(batch);
float* singlePtr = GetFloatPtr(single);
float* batchRow = batchPtr + rowIdx * dim;
float maxDiff = 0;
double sumDiff = 0;
int diffCount = 0;
for (int d = 0; d < dim; d++)
{
float diff = MathF.Abs(batchRow[d] - singlePtr[d]);
if (diff > 0) { diffCount++; sumDiff += diff; }
if (diff > maxDiff) maxDiff = diff;
}
Console.WriteLine($" {label} row[{rowIdx}]: maxDiff={maxDiff:E6}, avgDiff={sumDiff / dim:E6}, nonZero={diffCount}/{dim}");
Console.Write($" batch: ");
for (int i = 0; i < 5; i++) Console.Write($"{batchRow[i]:F8} ");
Console.Write($"\n single: ");
for (int i = 0; i < 5; i++) Console.Write($"{singlePtr[i]:F8} ");
Console.WriteLine();
}
private unsafe void DumpHiddenState(Tensor hidden, int seqLen, int layer)
{
float* ptr = GetFloatPtr(hidden);
int lastPos = seqLen - 1;
int dim = Config.HiddenSize;
float* row = ptr + lastPos * dim;
Console.Write($" L{layer:D2} pos{lastPos}: ");
for (int i = 0; i < 5; i++)
Console.Write($"{row[i]:F6} ");
float sum = 0;
for (int i = 0; i < dim; i++)
sum += row[i] * row[i];
Console.WriteLine($" norm={MathF.Sqrt(sum):F6}");
}
public override void Dispose()
{
_visionEncoder?.Dispose();
foreach (var (embeddings, _) in _pendingVisionEmbeddingsList)
embeddings?.Dispose();
_pendingVisionEmbeddingsList.Clear();
if (_kvCacheK != null)
foreach (var k in _kvCacheK) k?.Dispose();
if (_kvCacheV != null)
foreach (var v in _kvCacheV) v?.Dispose();
base.Dispose();
}
}
}