-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain_t.py
More file actions
1177 lines (972 loc) · 53.2 KB
/
main_t.py
File metadata and controls
1177 lines (972 loc) · 53.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
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import torch
import torch.nn.functional as F
from transformers import AutoModelForCausalLM, AutoTokenizer
import logging
import os
from enum import Enum
from typing import List, Tuple, Optional, Dict, Union, NamedTuple
import time
from collections import Counter, deque
import math
import numpy as np
from graphics import integrate_visualization
import gc
from tqdm import tqdm
from datetime import datetime
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# Device selection with Apple Silicon support
if torch.backends.mps.is_available():
device = torch.device("mps")
elif torch.cuda.is_available():
device = torch.device("cuda")
else:
device = torch.device("cpu")
# Set higher precision for matrix multiplication
torch.set_float32_matmul_precision('high')
LN_2 = 0.69314718056 # ln(2) = 1.0 / LOG2_E
# Constants for attention masking
MASK_VALUE = -0.7 * float(torch.finfo(torch.float32).max)
class LayerWeights(NamedTuple):
wq: torch.Tensor
wk: torch.Tensor
wv: torch.Tensor
wo: torch.Tensor
w1: torch.Tensor
w2: torch.Tensor
w3: torch.Tensor
ffn_norm: torch.Tensor
attention_norm: torch.Tensor
class GQAConfig(NamedTuple):
n_heads: int
n_kv_heads: int
head_dim: int
class AttnStats(NamedTuple):
entropy: torch.Tensor # Shape: [batch_size, n_layers, num_heads]
varentropy: torch.Tensor # Shape: [batch_size, n_layers, num_heads]
rolling_entropy: deque # Rolling window of entropy values
rolling_varentropy: deque # Rolling window of varentropy values
window_size: int
@classmethod
def new(cls, bsz: int, n_layers: int, n_heads: int, window_size: int = 100) -> 'AttnStats':
return cls(
entropy=torch.zeros((bsz, n_layers, n_heads), dtype=torch.float32, device=device),
varentropy=torch.zeros((bsz, n_layers, n_heads), dtype=torch.float32, device=device),
rolling_entropy=deque(maxlen=window_size),
rolling_varentropy=deque(maxlen=window_size),
window_size=window_size
)
def update(self, attention: torch.Tensor, layer_idx: int) -> 'AttnStats':
"""Update statistics with proper dimension handling"""
# Use attention scores directly - they're already normalized
# Calculate entropy per head
entropy = -torch.sum(
attention * torch.log2(torch.clamp(attention, min=1e-10)),
dim=-1
).mean(dim=-1) # Average over sequence length
# Calculate varentropy per head
mean_entropy = entropy.mean(dim=-1, keepdim=True)
varentropy = torch.pow(entropy - mean_entropy, 2).mean(dim=-1)
# Update tensors
self.entropy[:, layer_idx, :] = entropy
self.varentropy[:, layer_idx, :] = varentropy
# Update rolling statistics
self.rolling_entropy.append(entropy.mean().item())
self.rolling_varentropy.append(varentropy.mean().item())
return self
@property
def avg_entropy(self) -> float:
return sum(self.rolling_entropy) / len(self.rolling_entropy) if self.rolling_entropy else 0.0
@property
def avg_varentropy(self) -> float:
return sum(self.rolling_varentropy) / len(self.rolling_varentropy) if self.rolling_varentropy else 0.0
@property
def std_error(self) -> float:
if len(self.rolling_entropy) < 2:
return 0.0
return torch.tensor(list(self.rolling_entropy)).std().item() / math.sqrt(len(self.rolling_entropy))
class KVCache:
"""Enhanced Key-Value cache with memory optimization"""
def __init__(self, max_seq_len: int, n_layers: int, n_heads: int, head_dim: int):
self.max_seq_len = max_seq_len
self.n_layers = n_layers
self.n_heads = n_heads
self.head_dim = head_dim
# Initialize with smaller chunks and use sparse storage
chunk_size = 1024 # Smaller initial allocation
self.k_chunks = [torch.zeros((n_layers, chunk_size, n_heads, head_dim),
dtype=torch.bfloat16, device=device)]
self.v_chunks = [torch.zeros((n_layers, chunk_size, n_heads, head_dim),
dtype=torch.bfloat16, device=device)]
self.current_pos = 0
def extend_if_needed(self, needed_size: int):
# Current implementation may lead to memory fragmentation
# Improved version:
current_size = sum(chunk.size(1) for chunk in self.k_chunks)
if current_size >= needed_size:
return
# Allocate new size with some padding to reduce frequent resizing
new_size = max(needed_size * 1.5, current_size + 1024)
new_k_chunk = torch.zeros((self.n_layers, int(new_size - current_size),
self.n_heads, self.head_dim),
dtype=torch.bfloat16, device=device)
new_v_chunk = torch.zeros_like(new_k_chunk)
self.k_chunks.append(new_k_chunk)
self.v_chunks.append(new_v_chunk)
def update(self, k: torch.Tensor, v: torch.Tensor, layer_idx: int, pos: int) -> Tuple[torch.Tensor, torch.Tensor]:
"""Update cache with new key-value pairs"""
self.extend_if_needed(pos + k.size(1))
# Find correct chunk and position
total_pos = 0
for k_chunk, v_chunk in zip(self.k_chunks, self.v_chunks):
chunk_size = k_chunk.size(1)
if total_pos <= pos < total_pos + chunk_size:
start_idx = pos - total_pos
end_idx = start_idx + k.size(1)
k_chunk[layer_idx, start_idx:end_idx] = k
v_chunk[layer_idx, start_idx:end_idx] = v
break
total_pos += chunk_size
# Concatenate all chunks for return
keys = torch.cat([chunk[layer_idx] for chunk in self.k_chunks], dim=0)[:pos + k.size(1)]
values = torch.cat([chunk[layer_idx] for chunk in self.v_chunks], dim=0)[:pos + v.size(1)]
return keys, values
def clear(self):
"""Clear the cache"""
for chunk in self.k_chunks + self.v_chunks:
chunk.zero_()
self.current_pos = 0
class SamplerState(Enum):
ARGMAX = 0
SAMPLE = 1
INSERT_COT = 2
RESAMPLE = 3
ADAPTIVE = 4
EOT = 5
class SamplerConfig:
def __init__(self, tokenizer=None):
# Parameter validation methods
self.temp = self._validate_float("temp", 0.666, min_val=0.1, max_val=2.0)
self.top_p = self._validate_float("top_p", 0.90, min_val=0.0, max_val=1.0)
self.top_k = self._validate_int("top_k", 27, min_val=1)
self.min_p = self._validate_float("min_p", 0.05, min_val=0.0, max_val=1.0)
# Architecture parameters (will be updated with actual model values)
self.n_layers = self._validate_int("n_layers", 16, min_val=1)
self.n_heads = self._validate_int("n_heads", 32, min_val=1)
self.head_dim = self._validate_int("head_dim", 64, min_val=1)
# Strategy-specific thresholds with expanded ranges
self.argmax_entropy_thresh = self._validate_float("argmax_entropy_thresh", 0.5, min_val=0.0, max_val=5.0)
self.sample_min_entropy_thresh = self._validate_float("sample_min_entropy_thresh", 0.5, min_val=0.0, max_val=5.0)
self.sample_max_entropy_thresh = self._validate_float("sample_max_entropy_thresh", 1.5, min_val=0.0, max_val=7.0)
self.sample_varentropy_thresh = self._validate_float("sample_varentropy_thresh", 2.0, min_val=0.0, max_val=8.0)
self.cot_min_entropy_thresh = self._validate_float("cot_min_entropy_thresh", 1.5, min_val=0.0, max_val=6.0)
self.cot_max_entropy_thresh = self._validate_float("cot_max_entropy_thresh", 2.5, min_val=0.0, max_val=8.0)
self.cot_varentropy_thresh = self._validate_float("cot_varentropy_thresh", 2.0, min_val=0.0, max_val=8.0)
self.resample_min_entropy_thresh = self._validate_float("resample_min_entropy_thresh", 1.0, min_val=0.0, max_val=6.0)
self.resample_max_entropy_thresh = self._validate_float("resample_max_entropy_thresh", 2.5, min_val=0.0, max_val=8.0)
self.resample_varentropy_thresh = self._validate_float("resample_varentropy_thresh", 4.0, min_val=0.0, max_val=10.0)
self.adaptive_entropy_thresh = self._validate_float("adaptive_entropy_thresh", 2.0, min_val=0.0, max_val=8.0)
self.adaptive_varentropy_thresh = self._validate_float("adaptive_varentropy_thresh", 4.0, min_val=0.0, max_val=10.0)
# Adaptive center point parameters with expanded ranges
self.adaptive_entropy_center = self._validate_float("adaptive_entropy_center", 2.0, min_val=0.0, max_val=8.0)
self.adaptive_varentropy_center = self._validate_float("adaptive_varentropy_center", 2.0, min_val=0.0, max_val=10.0)
self.adaptive_radius = self._validate_float("adaptive_radius", 0.5, min_val=0.1, max_val=4.0)
# Strategy positioning parameters with expanded ranges
self.quadrant_separation = self._validate_float("quadrant_separation", 1.0, min_val=0.5, max_val=4.0)
self.boundary_softness = self._validate_float("boundary_softness", 0.3, min_val=0.1, max_val=2.0)
# Strategy-specific parameters with expanded ranges
self.argmax_range = self._validate_float("argmax_range", 0.3, min_val=0.1, max_val=3.0)
self.sample_variance_threshold = self._validate_float("sample_variance_threshold", 0.5, min_val=0.1, max_val=5.0)
self.cot_entropy_range = self._validate_float("cot_entropy_range", 0.8, min_val=0.1, max_val=5.0)
self.resample_min_variance = self._validate_float("resample_min_variance", 1.0, min_val=0.5, max_val=6.0)
# Enhanced RoPE parameters
self.max_seq_len = self._validate_int("max_seq_len", 4096, min_val=1)
self.rope_theta = self._validate_float("rope_theta", 10000.0, min_val=1.0)
self.rope_scaling = self._validate_float("rope_scaling", 1.0, min_val=0.0)
self.use_scaled_rope = True
self.rope_scale_base = self._validate_float("rope_scale_base", 8.0, min_val=1.0)
self.rope_scale_factor = self._validate_float("rope_scale_factor", 0.25, min_val=0.0)
# Attention coefficients
self.helv_attn_ent_offset = self._validate_float("helv_attn_ent_offset", 1.3)
self.helv_attn_ent_coef = self._validate_float("helv_attn_ent_coef", 0.2)
self.lehv_interaction_strength_offset = self._validate_float("lehv_interaction_strength_offset", 1.2)
self.lehv_interaction_strength_coef = self._validate_float("lehv_interaction_strength_coef", 0.3)
self.hehv_attn_ent_coef = self._validate_float("hehv_attn_ent_coef", 0.2)
self.hehv_attn_vent_offset = self._validate_float("hehv_attn_vent_offset", 2.0)
self.hehv_attn_vent_coef = self._validate_float("hehv_attn_vent_coef", 0.5)
# Enhanced adaptive sampling parameters
self.n_adaptive_samples = self._validate_int("n_adaptive_samples", 5, min_val=1)
self.ada_temp_logits = self._validate_float("ada_temp_logits", 0.3)
self.ada_temp_attn = self._validate_float("ada_temp_attn", 0.2)
self.ada_temp_agree = self._validate_float("ada_temp_agree", 0.2)
self.ada_top_p = self._validate_float("ada_top_p", 0.1)
self.ada_top_k_int = self._validate_float("ada_top_k_int", 0.3)
self.ada_top_k_agree = self._validate_float("ada_top_k_agree", 0.2)
self.ada_min_p = self._validate_float("ada_min_p", 0.5)
# Scoring coefficients
self.ada_score_logits_ent = self._validate_float("ada_score_logits_ent", 0.1)
self.ada_score_attn_ent = self._validate_float("ada_score_attn_ent", 0.2)
self.ada_score_logits_vent = self._validate_float("ada_score_logits_vent", 0.3)
self.ada_score_attn_vent = self._validate_float("ada_score_attn_vent", 0.4)
self.ada_score_agree = self._validate_float("ada_score_agree", 0.5)
self.ada_score_int = self._validate_float("ada_score_int", 0.6)
# Memory and window parameters
self.repetition_penalty = self._validate_float("repetition_penalty", 1.2, min_val=1.0)
self.max_ngram_size = self._validate_int("max_ngram_size", 5, min_val=1)
self.max_ngram_repeat = self._validate_int("max_ngram_repeat", 3, min_val=1)
self.strategy_change_batch_size = self._validate_int("strategy_change_batch_size", 1, min_val=1)
self.window_size = self._validate_int("window_size", 50, min_val=1)
self.long_window_size = self._validate_int("long_window_size", 500, min_val=1)
self.decay_factor = self._validate_float("decay_factor", 0.95, min_val=0.0, max_val=1.0)
self.long_decay_factor = self._validate_float("long_decay_factor", 0.95, min_val=0.0, max_val=1.0)
# Statistics tracking
self.stats_window_size = self._validate_int("stats_window_size", 100, min_val=1)
# Special tokens initialization
if tokenizer:
try:
self.initialize_special_tokens(tokenizer)
except Exception as e:
logger.warning(f"Could not encode special tokens, using defaults: {str(e)}")
self.use_default_tokens()
else:
self.use_default_tokens()
self.generator = torch.Generator(device=device).manual_seed(1337)
def _validate_float(self, name: str, value: float, min_val: float = None,
max_val: float = None) -> float:
"""Validate float parameters"""
try:
value = float(value)
if min_val is not None and value < min_val:
raise ValueError(f"{name} must be >= {min_val}")
if max_val is not None and value > max_val:
raise ValueError(f"{name} must be <= {max_val}")
return value
except (TypeError, ValueError) as e:
raise ValueError(f"Invalid value for {name}: {value}. {str(e)}")
def _validate_int(self, name: str, value: int, min_val: int = None,
max_val: int = None) -> int:
"""Validate integer parameters"""
try:
value = int(value)
if min_val is not None and value < min_val:
raise ValueError(f"{name} must be >= {min_val}")
if max_val is not None and value > max_val:
raise ValueError(f"{name} must be <= {max_val}")
return value
except (TypeError, ValueError) as e:
raise ValueError(f"Invalid value for {name}: {value}. {str(e)}")
def initialize_special_tokens(self, tokenizer):
"""Initialize special tokens from tokenizer"""
self.cot_token = tokenizer.encode("[COT]", add_special_tokens=False)[0]
self.eos_token = tokenizer.eos_token_id
self.bos_token = tokenizer.bos_token_id
self.pad_token = tokenizer.pad_token_id
self.stop_tokens = [
self.eos_token,
tokenizer.encode("</s>", add_special_tokens=False)[0] if "</s>" in tokenizer.get_vocab() else None,
tokenizer.encode("<|endoftext|>", add_special_tokens=False)[0] if "<|endoftext|>" in tokenizer.get_vocab() else None
]
self.stop_tokens = [token for token in self.stop_tokens if token is not None]
logger.info(f"Initialized stop tokens: {self.stop_tokens}")
def use_default_tokens(self):
"""Use default token values"""
self.cot_token = 2564
self.eos_token = 0
self.bos_token = 1
self.pad_token = 2
self.stop_tokens = [0, 2]
class EntropixSampler:
def __init__(self, config: SamplerConfig):
self.config = config
self.strategy_counter = Counter()
self.recent_tokens = deque(maxlen=200)
self.current_batch = []
self.current_strategy = SamplerState.SAMPLE
self.tokens_since_last_change = 0
# Enhanced metric tracking
self.entropy_window = deque(maxlen=self.config.window_size)
self.varentropy_window = deque(maxlen=self.config.window_size)
self.attention_entropy_window = deque(maxlen=self.config.window_size)
self.long_entropy_window = deque(maxlen=self.config.long_window_size)
self.long_varentropy_window = deque(maxlen=self.config.long_window_size)
self.ngram_counts = {}
self.sliding_window = 100
# Initialize attention stats
self.attn_stats = None
@staticmethod
def apply_rotary_emb(xq: torch.Tensor, xk: torch.Tensor, freqs_cis: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
"""Apply rotary embeddings with improved numerical stability."""
xq_r = xq[..., ::2]
xq_i = xq[..., 1::2]
xk_r = xk[..., ::2]
xk_i = xk[..., 1::2]
# Reshape freqs_cis for proper broadcasting
freqs_cis = freqs_cis.view(*([1] * (xq_r.dim() - freqs_cis.dim())), *freqs_cis.shape)
# Complex multiplication with better numerical stability
xq_out_r = xq_r * freqs_cis.real - xq_i * freqs_cis.imag
xq_out_i = xq_r * freqs_cis.imag + xq_i * freqs_cis.real
xk_out_r = xk_r * freqs_cis.real - xk_i * freqs_cis.imag
xk_out_i = xk_r * freqs_cis.imag + xk_i * freqs_cis.real
# Interleave real and imaginary parts
xq_out = torch.stack([xq_out_r, xq_out_i], dim=-1).flatten(-2)
xk_out = torch.stack([xk_out_r, xk_out_i], dim=-1).flatten(-2)
return xq_out, xk_out
def apply_scaled_rope(self, xq: torch.Tensor, xk: torch.Tensor, freqs_cis: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
"""Apply scaled rotary positional embeddings"""
if not self.config.use_scaled_rope:
return self.apply_rotary_emb(xq, xk, freqs_cis)
scale = self.config.rope_scale_base ** (self.config.rope_scale_factor *
(torch.arange(xq.size(-2), device=device) / xq.size(-2)))
scaled_freqs = freqs_cis * scale.unsqueeze(-1)
return self.apply_rotary_emb(xq, xk, scaled_freqs)
@staticmethod
def build_attention_mask(seqlen: int, start_pos: int, dtype: torch.dtype = torch.float32) -> Optional[torch.Tensor]:
"""Generate an improved causal attention mask with proper scaling."""
if seqlen <= 1:
return None
mask = torch.full((seqlen, seqlen), MASK_VALUE, dtype=dtype, device=device)
mask = torch.triu(mask, diagonal=1)
if start_pos > 0:
prefix_mask = torch.zeros((seqlen, start_pos), dtype=dtype, device=device)
mask = torch.cat([prefix_mask, mask], dim=1)
mask = torch.where(mask == MASK_VALUE, mask, torch.zeros_like(mask))
return mask
@staticmethod
def grouped_query_attention(
x: torch.Tensor,
layer_weights: LayerWeights,
config: GQAConfig,
cur_pos: int,
freqs_cis: torch.Tensor,
kvcache: KVCache,
attn_mask: Optional[torch.Tensor] = None
) -> Tuple[torch.Tensor, KVCache, torch.Tensor]:
"""Improved attention implementation with grouped-query attention support."""
bsz, seqlen, _ = x.shape
n_rep = config.n_heads // config.n_kv_heads
xq = F.linear(x, layer_weights.wq)
xk = F.linear(x, layer_weights.wk)
xv = F.linear(x, layer_weights.wv)
xq = xq.view(bsz, seqlen, config.n_heads, config.head_dim)
xk = xk.view(bsz, seqlen, config.n_kv_heads, config.head_dim)
xv = xv.view(bsz, seqlen, config.n_kv_heads, config.head_dim)
xq, xk = EntropixSampler.apply_rotary_emb(xq, xk, freqs_cis)
keys, values, kvcache = kvcache.update(xk, xv, cur_pos, n_rep)
xq = xq.transpose(1, 2)
keys = keys.transpose(1, 2).transpose(2, 3)
values = values.transpose(1, 2)
keys = keys.repeat_interleave(n_rep, dim=1)
values = values.repeat_interleave(n_rep, dim=1)
scores = torch.matmul(xq, keys) / math.sqrt(config.head_dim)
scores = scores.to(torch.float32)
if attn_mask is not None:
scores = scores + attn_mask
scores = torch.where(
scores <= MASK_VALUE * 0.5,
torch.full_like(scores, float('-inf')),
scores
)
attention_weights = F.softmax(scores, dim=-1, dtype=torch.float32)
attention_weights = attention_weights.to(x.dtype)
output = torch.matmul(attention_weights, values)
output = output.transpose(1, 2).contiguous()
output = output.view(bsz, seqlen, -1)
output = F.linear(output, layer_weights.wo)
return output, kvcache, scores
def calculate_metrics(self, logits: torch.Tensor, attention: torch.Tensor) -> Dict[str, float]:
"""Calculate metrics with separate tracking for logits and attention-based metrics"""
if self.attn_stats is None:
self.attn_stats = AttnStats.new(
bsz=attention.size(0),
n_layers=1,
n_heads=attention.size(1),
window_size=self.config.stats_window_size
)
# Calculate logits-based metrics
entropy, varentropy = self.calculate_varentropy_logsoftmax(logits)
# Calculate attention-based metrics
attn_entropy = self.calculate_attention_entropy(attention)
attn_varentropy = self.calculate_attention_varentropy(attention)
agreement = self.calculate_agreement(attention)
interaction_strength = self.calculate_interaction_strength(attention)
# Update attention stats
self.attn_stats = self.attn_stats.update(attention, 0)
# Store both raw and rolling metrics
return {
"logits_entropy": entropy.mean().item(),
"logits_varentropy": varentropy.mean().item(),
"attn_entropy": attn_entropy.mean().item(),
"attn_varentropy": attn_varentropy.item(), # Now correctly calculated
"agreement": agreement.item(),
"interaction_strength": interaction_strength.item(),
"rolling_entropy": self.attn_stats.avg_entropy,
"rolling_varentropy": self.attn_stats.avg_varentropy,
"std_error": self.attn_stats.std_error
}
def calculate_varentropy_logsoftmax(self, logits: torch.Tensor, axis: int = -1) -> Tuple[torch.Tensor, torch.Tensor]:
"""Calculate entropy and varentropy using numerically stable methods"""
log_probs = F.log_softmax(logits, dim=axis)
probs = torch.exp(log_probs)
entropy = -torch.sum(probs * log_probs, dim=axis) / LN_2
varentropy = torch.sum(probs * (log_probs / LN_2 + entropy.unsqueeze(-1))**2, dim=axis)
return entropy, varentropy
def calculate_attention_entropy(self, attention: torch.Tensor) -> torch.Tensor:
"""Calculate attention entropy with fixed dimension handling"""
# Use attention scores directly
entropy = -torch.sum(
attention * torch.log2(torch.clamp(attention, min=1e-10)),
dim=-1
)
return entropy.mean(dim=1) # Average over batch dimension, preserving head dimension
def calculate_attention_varentropy(self, attention: torch.Tensor) -> torch.Tensor:
"""Calculate variance of attention entropy with fixed dimension handling"""
entropy = self.calculate_attention_entropy(attention) # Get entropy per head
varentropy = torch.var(entropy, dim=-1) # Calculate variance across heads
# Handle any NaN values by replacing with zeros
varentropy = torch.where(torch.isnan(varentropy), torch.zeros_like(varentropy), varentropy)
return varentropy.mean() # Return mean varentropy
def calculate_agreement(self, attention: torch.Tensor) -> torch.Tensor:
"""Calculate agreement between attention heads"""
# Use attention scores directly
mean_attention = attention.mean(dim=1, keepdim=True)
head_agreement = 1 - torch.abs(attention - mean_attention).mean()
return head_agreement
def calculate_interaction_strength(self, attention: torch.Tensor) -> torch.Tensor:
"""Calculate interaction strength between attention heads"""
# Use attention scores directly
batch_size, num_heads, seq_len, _ = attention.shape
attention_flat = attention.view(batch_size, num_heads, -1)
norm = torch.norm(attention_flat, dim=2, keepdim=True)
normalized = attention_flat / (norm + 1e-8)
interaction = torch.matmul(normalized, normalized.transpose(-1, -2))
return torch.mean(torch.abs(interaction))
def determine_strategy(self, entropy: float, varentropy: float, attention_entropy: float) -> SamplerState:
"""Enhanced strategy determination using configurable relative positioning"""
recent_tokens = list(self.recent_tokens)[-1:] if self.recent_tokens else []
if recent_tokens and self.config.stop_tokens and any(token in self.config.stop_tokens for token in recent_tokens):
return SamplerState.EOT
# Calculate distances from adaptive center
entropy_dist = entropy - self.config.adaptive_entropy_center
varentropy_dist = varentropy - self.config.adaptive_varentropy_center
distance_from_center = math.sqrt(entropy_dist**2 + varentropy_dist**2)
# Check if we're in the adaptive region
if distance_from_center <= self.config.adaptive_radius:
logger.debug("ADAPTIVE triggered (within center radius)")
return SamplerState.ADAPTIVE
# Calculate normalized position relative to center
angle = math.atan2(varentropy_dist, entropy_dist)
quadrant = (math.degrees(angle) + 360) % 360 // 90
# Apply quadrant separation scaling
scaled_distance = distance_from_center * self.config.quadrant_separation
# Calculate strategy weights based on position and boundary softness
weights = {
SamplerState.ARGMAX: 0.0,
SamplerState.SAMPLE: 0.0,
SamplerState.INSERT_COT: 0.0,
SamplerState.RESAMPLE: 0.0
}
# ARGMAX (bottom-left quadrant)
if entropy < self.config.adaptive_entropy_center and varentropy < self.config.adaptive_varentropy_center:
argmax_weight = math.exp(-scaled_distance / self.config.argmax_range)
weights[SamplerState.ARGMAX] = argmax_weight
# SAMPLE (bottom-right quadrant)
if varentropy >= self.config.sample_variance_threshold:
sample_weight = 1.0 - math.exp(-varentropy / self.config.sample_variance_threshold)
weights[SamplerState.SAMPLE] = sample_weight
# INSERT_COT (top-left quadrant)
if abs(entropy - (self.config.adaptive_entropy_center + self.config.cot_entropy_range)) < self.config.boundary_softness:
cot_weight = math.exp(-abs(entropy - (self.config.adaptive_entropy_center + self.config.cot_entropy_range)) / self.config.boundary_softness)
weights[SamplerState.INSERT_COT] = cot_weight
# RESAMPLE (top-right quadrant)
if varentropy >= self.config.resample_min_variance:
resample_weight = 1.0 - math.exp(-(varentropy - self.config.resample_min_variance))
weights[SamplerState.RESAMPLE] = resample_weight
# Apply boundary softness to all weights
weights = {k: v * math.exp(-scaled_distance * (1 - self.config.boundary_softness)) for k, v in weights.items()}
# Choose strategy with highest weight
selected_strategy = max(weights.items(), key=lambda x: x[1])[0]
logger.debug(f"Strategy weights: {weights}")
logger.debug(f"Selected strategy: {selected_strategy}")
return selected_strategy
def score_sample(self, sample: torch.Tensor, logits: torch.Tensor, metrics: Dict[str, float]) -> float:
"""
Score a sample based on log probability and metrics.
Args:
sample: Generated token
logits: Model logits
metrics: Dictionary of current metrics
Returns:
float: Combined score for the sample
"""
try:
# Calculate log probability score
sample_flat = sample.flatten().to(torch.long)
one_hot = F.one_hot(sample_flat, logits.shape[-1])
log_probs = F.log_softmax(logits, dim=-1).view(-1, logits.shape[-1])
log_prob = torch.sum(log_probs * one_hot).item()
# Calculate confidence score based on multiple metrics
confidence_score = (
(1 - metrics["logits_entropy"]) * self.config.ada_score_logits_ent +
(1 - metrics["attn_entropy"]) * self.config.ada_score_attn_ent +
(1 - metrics["logits_varentropy"]) * self.config.ada_score_logits_vent +
(1 - metrics["attn_varentropy"]) * self.config.ada_score_attn_vent +
metrics["agreement"] * self.config.ada_score_agree +
metrics["interaction_strength"] * self.config.ada_score_int
)
return log_prob + confidence_score
except Exception as e:
logger.error(f"Error in score_sample: {str(e)}")
return float('-inf')
def sample(self, logits: torch.Tensor, attention: torch.Tensor) -> Tuple[torch.Tensor, SamplerState]:
"""Main sampling function with strategy selection, metric tracking and sampling"""
# Calculate metrics
metrics = self.calculate_metrics(logits, attention)
# Update metric windows
self.entropy_window.append(metrics["logits_entropy"])
self.varentropy_window.append(metrics["logits_varentropy"])
self.attention_entropy_window.append(metrics["attn_entropy"])
self.long_entropy_window.append(metrics["logits_entropy"])
self.long_varentropy_window.append(metrics["logits_varentropy"])
# Calculate weighted averages
avg_entropy = self.weighted_average(list(self.entropy_window), self.config.decay_factor)
avg_varentropy = self.weighted_average(list(self.varentropy_window), self.config.decay_factor)
avg_attention_entropy = self.weighted_average(list(self.attention_entropy_window), self.config.decay_factor)
# Long-term metrics
long_avg_entropy = self.weighted_average(list(self.long_entropy_window), self.config.long_decay_factor)
long_avg_varentropy = self.weighted_average(list(self.long_varentropy_window), self.config.long_decay_factor)
# Combine short and long-term metrics
combined_entropy = (avg_entropy + long_avg_entropy) / 2
combined_varentropy = (avg_varentropy + long_avg_varentropy) / 2
# Determine sampling strategy
self.current_strategy = self.determine_strategy(
combined_entropy,
combined_varentropy,
avg_attention_entropy
)
# Initialize sampling parameters
params = {
"temperature": self.config.temp,
"top_p": self.config.top_p,
"top_k": self.config.top_k,
"min_p": self.config.min_p
}
# Early returns for special cases
if self.current_strategy == SamplerState.ARGMAX:
sampled_token = torch.argmax(logits[:, -1], dim=-1, keepdim=True)
return sampled_token, self.current_strategy
elif self.current_strategy == SamplerState.INSERT_COT:
# Only insert CoT token if it hasn't been used recently
if self.config.cot_token not in self.recent_tokens:
sampled_token = torch.tensor([[self.config.cot_token]], device=device)
return sampled_token, self.current_strategy
else:
# If we can't insert CoT, fall back to SAMPLE strategy
self.current_strategy = SamplerState.SAMPLE
# Adjust parameters based on strategy
if self.current_strategy == SamplerState.RESAMPLE:
params["temperature"] = min(1.5, self.config.temp * (
self.config.lehv_interaction_strength_offset +
self.config.lehv_interaction_strength_coef * metrics["interaction_strength"]
))
params["top_k"] = max(5, int(self.config.top_k * (
1 + 0.5 * (1 - metrics["agreement"])
)))
elif self.current_strategy == SamplerState.ADAPTIVE:
logits_uncertainty = metrics["logits_entropy"] + metrics["logits_varentropy"]
attn_uncertainty = metrics["attn_entropy"] + metrics["attn_varentropy"]
params["temperature"] = self.config.temp * (
1 + self.config.ada_temp_logits * logits_uncertainty +
self.config.ada_temp_attn * attn_uncertainty -
self.config.ada_temp_agree * metrics["agreement"]
)
params["top_p"] = min(max(
self.config.top_p * (1 + self.config.ada_top_p * metrics["attn_varentropy"]),
0.1
), 1.0)
params["top_k"] = int(min(max(
self.config.top_k * (
1 + self.config.ada_top_k_int * metrics["interaction_strength"] -
self.config.ada_top_k_agree * metrics["agreement"]
),
5
), 100))
params["min_p"] = min(max(
self.config.min_p * (1 - self.config.ada_min_p * logits_uncertainty),
0.01
), 0.5)
# Handle logits shape
if logits.dim() == 3:
logits = logits[:, -1, :]
elif logits.dim() == 1:
logits = logits.unsqueeze(0)
# Apply repetition penalty
if len(self.recent_tokens) > 0:
for token in set(self.recent_tokens):
logits[:, token] = logits[:, token] / self.config.repetition_penalty
# Apply temperature
logits = logits / params["temperature"]
# Apply min-p filtering
if params["min_p"] > 0.0:
sorted_logits, _ = torch.sort(logits, descending=True, dim=-1)
cumulative_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
sorted_indices_to_remove = cumulative_probs > (1 - params["min_p"])
sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
sorted_indices_to_remove[..., 0] = 0
indices_to_remove = sorted_indices_to_remove.scatter(
dim=1,
index=torch.argsort(logits, descending=True),
src=sorted_indices_to_remove
)
logits = logits.masked_fill(indices_to_remove, float('-inf'))
# Apply top-k filtering
top_k = min(params["top_k"], logits.size(-1))
top_k_logits, top_k_indices = torch.topk(logits, k=top_k, dim=-1)
# Apply top-p filtering to top-k candidates
cumulative_probs = torch.cumsum(F.softmax(top_k_logits, dim=-1), dim=-1)
sorted_indices_to_remove = cumulative_probs > params["top_p"]
sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
sorted_indices_to_remove[..., 0] = 0
top_k_logits = top_k_logits.masked_fill(sorted_indices_to_remove, float('-inf'))
# Sample
probs = F.softmax(top_k_logits, dim=-1)
if self.current_strategy == SamplerState.ADAPTIVE:
# Generate multiple samples for adaptive strategy
samples = []
scores = []
for _ in range(self.config.n_adaptive_samples):
idx = torch.multinomial(probs, num_samples=1, generator=self.config.generator)
sample = torch.gather(top_k_indices, -1, idx)
samples.append(sample)
scores.append(self.score_sample(sample, logits, metrics))
sampled_token = samples[torch.argmax(torch.tensor(scores))]
else:
idx = torch.multinomial(probs, num_samples=1, generator=self.config.generator)
sampled_token = torch.gather(top_k_indices, -1, idx)
# Update tracking
self.strategy_counter[self.current_strategy.name] += 1
self.tokens_since_last_change += 1
self.current_batch.append(sampled_token.item())
self.recent_tokens.append(sampled_token.item())
# Handle repetition (resampling with fixed parameters if needed)
if self.check_ngram_repetition(list(self.recent_tokens)):
if logits.dim() == 3:
logits = logits[:, -1, :]
elif logits.dim() == 1:
logits = logits.unsqueeze(0)
# Apply fixed parameters for repetition handling
logits = logits / 1.2 # Fixed temperature
top_k_logits, top_k_indices = torch.topk(logits, k=100, dim=-1) # Fixed top_k
probs = F.softmax(top_k_logits, dim=-1)
idx = torch.multinomial(probs, num_samples=1, generator=self.config.generator)
sampled_token = torch.gather(top_k_indices, -1, idx)
# Reset batch if needed
if len(self.current_batch) >= self.config.strategy_change_batch_size:
self.current_batch = []
self.tokens_since_last_change = 0
return sampled_token, self.current_strategy
def check_ngram_repetition(self, tokens: List[int]) -> bool:
"""Check for repeated n-grams in recent tokens"""
if len(tokens) < self.config.max_ngram_size:
return False
window = tokens[-self.sliding_window:]
if len(self.ngram_counts) > 10000:
self.ngram_counts = {}
for n in range(2, min(self.config.max_ngram_size + 1, len(window))):
ngrams = [tuple(window[i:i+n]) for i in range(len(window)-n+1)]
for ngram in ngrams:
if ngram in self.ngram_counts:
self.ngram_counts[ngram] += 1
if self.ngram_counts[ngram] > self.config.max_ngram_repeat:
return True
else:
self.ngram_counts[ngram] = 1
return False
def reset_ngram_counts(self):
"""Reset the n-gram counter"""
self.ngram_counts = {}
def weighted_average(self, values: List[float], decay_factor: float) -> float:
"""Calculate weighted average with exponential decay"""
if not values:
return 0.0
weights = [decay_factor ** i for i in range(len(values) - 1, -1, -1)]
weighted_sum = sum(w * v for w, v in zip(weights, values))
weight_sum = sum(weights)
return weighted_sum / weight_sum if weight_sum > 0 else 0.0
def normalize_metrics(self, metrics: Dict[str, float]) -> Dict[str, float]:
"""
Normalize metrics to [0,1] range using rolling statistics.
Args:
metrics (Dict[str, float]): Raw metrics from current step
Returns:
Dict[str, float]: Normalized metrics
"""
# Get mean and std from rolling windows
means = {
"logits_entropy": np.mean(list(self.entropy_window)) if self.entropy_window else 0,
"logits_varentropy": np.mean(list(self.varentropy_window)) if self.varentropy_window else 0,
"attn_entropy": np.mean(list(self.attention_entropy_window)) if self.attention_entropy_window else 0,
"attn_varentropy": self.attn_stats.avg_varentropy if self.attn_stats else 0
}
stds = {
"logits_entropy": np.std(list(self.entropy_window)) if len(self.entropy_window) > 1 else 1,
"logits_varentropy": np.std(list(self.varentropy_window)) if len(self.varentropy_window) > 1 else 1,
"attn_entropy": np.std(list(self.attention_entropy_window)) if len(self.attention_entropy_window) > 1 else 1,
"attn_varentropy": self.attn_stats.std_error if self.attn_stats else 1
}
# Z-score normalization then sigmoid scaling to [0,1]
normalized = {}
for metric, value in metrics.items():
z_score = (value - means[metric]) / max(stds[metric], 1e-8)
normalized[metric] = 1 / (1 + np.exp(-z_score))
return normalized
def calculate_weighted_score(self, metrics: Dict[str, float], weights: Dict[str, float]) -> float:
"""
Calculate weighted score from normalized metrics.
Args:
metrics (Dict[str, float]): Normalized metrics
weights (Dict[str, float]): Weight factors for each metric
Returns:
float: Combined weighted score
"""
score = 0.0
for metric, weight in weights.items():
score += metrics[metric] * weight
return score
def determine_strategy(self, entropy: float, varentropy: float, attention_entropy: float) -> SamplerState:
"""
Determine sampling strategy using weighted metric combinations.
Args:
entropy (float): Current logits entropy
varentropy (float): Current logits varentropy
attention_entropy (float): Current attention entropy
Returns:
SamplerState: Selected sampling strategy
"""
# Check for stop condition
recent_tokens = list(self.recent_tokens)[-1:] if self.recent_tokens else []
if recent_tokens and self.config.stop_tokens and any(token in self.config.stop_tokens for token in recent_tokens):
return SamplerState.EOT
# Current metrics
metrics = {
"logits_entropy": entropy,
"logits_varentropy": varentropy,
"attn_entropy": attention_entropy,
"attn_varentropy": self.attn_stats.avg_varentropy if self.attn_stats else 0
}
# Primary weights for each factor
weights = {
"logits_entropy": 0.4, # Highest weight - direct measure of token uncertainty
"logits_varentropy": 0.2, # Lower - secondary token distribution characteristic
"attn_entropy": 0.3, # Moderate - important context processing signal
"attn_varentropy": 0.1 # Lowest - supplementary attention pattern info
}
# Normalize metrics
normalized_metrics = self.normalize_metrics(metrics)
# Calculate weighted score
score = self.calculate_weighted_score(normalized_metrics, weights)
# Define strategy regions based on score ranges
if score < 0.3: # Low uncertainty
return SamplerState.ARGMAX
elif 0.3 <= score < 0.5: # Moderate uncertainty
return SamplerState.SAMPLE if normalized_metrics["logits_varentropy"] > 0.5 else SamplerState.ARGMAX
elif 0.5 <= score < 0.7: # High uncertainty
# Check for CoT insertion
if (normalized_metrics["logits_entropy"] > 0.6 and
normalized_metrics["attn_entropy"] > 0.6 and
self.config.cot_token not in self.recent_tokens):
return SamplerState.INSERT_COT
return SamplerState.ADAPTIVE
else: # Very high uncertainty
if normalized_metrics["logits_varentropy"] > 0.7 and normalized_metrics["attn_varentropy"] > 0.7:
return SamplerState.RESAMPLE
return SamplerState.ADAPTIVE
def calculate_strategy_confidence(self, metrics: Dict[str, float], weights: Dict[str, float]) -> Dict[str, float]:
"""
Calculate confidence scores for each strategy based on weighted metrics.
Args:
metrics (Dict[str, float]): Current normalized metrics
weights (Dict[str, float]): Weight factors for metrics
Returns:
Dict[str, float]: Confidence score for each strategy
"""
weighted_score = self.calculate_weighted_score(metrics, weights)
# Calculate base confidences from weighted score
confidences = {
SamplerState.ARGMAX.name: max(0, 0.3 - weighted_score) / 0.3,
SamplerState.SAMPLE.name: max(0, min(weighted_score - 0.3, 0.2)) / 0.2,
SamplerState.ADAPTIVE.name: max(0, min(weighted_score - 0.5, 0.2)) / 0.2,
SamplerState.INSERT_COT.name: max(0, min(weighted_score - 0.5, 0.2)) / 0.2
if metrics["logits_entropy"] > 0.6 and metrics["attn_entropy"] > 0.6 else 0,
SamplerState.RESAMPLE.name: max(0, weighted_score - 0.7) / 0.3
if metrics["logits_varentropy"] > 0.7 and metrics["attn_varentropy"] > 0.7 else 0
}
# Normalize confidences to sum to 1
total = sum(confidences.values())
if total > 0:
confidences = {k: v/total for k, v in confidences.items()}
return confidences
def generate_response(model, tokenizer, prompt: str, max_tokens: int = None, viz_manager=None) -> str:
"""Generate response with visualization support"""
# Create sampler config and sampler
sampler_config = SamplerConfig(tokenizer) # Make sure this is imported
sampler = EntropixSampler(sampler_config) # Make sure this is imported
inputs = tokenizer(
prompt,
return_tensors="pt",
truncation=True,
max_length=model.config.max_position_embeddings - 1000
).to(device)
input_ids = inputs["input_ids"]
attention_mask = torch.ones_like(input_ids)
max_tokens = max_tokens or 2048