forked from karpathy/nanoGPT
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathsample.py
More file actions
1936 lines (1634 loc) · 82.9 KB
/
Copy pathsample.py
File metadata and controls
1936 lines (1634 loc) · 82.9 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
# sample.py
import argparse
import csv
import importlib.util
import json
import math
import os
import pickle
import time
from inspect import signature
from contextlib import nullcontext
from datetime import datetime
# from __future__ import annotations
from pathlib import Path
from typing import Callable, Dict, List, Optional, Sequence, Union
import io
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import torch
import tiktoken
from collections import OrderedDict
from rich import print
from rich.console import Console
from rich.table import Table
from rich.text import Text
from torch.nn import functional as F
from model import GPT, GPTConfig
from sample_variations.numerical_multicontext import decode_numerical_series, write_plotly_report
from utils.model_info import print_summary, print_module_structure, print_model_blocks
from variations.model_variations import model_variation_dictionary
from benchmarks import run_all
def parse_args():
parser = argparse.ArgumentParser(description="Inference from trained models")
parser.add_argument("--device", type=str, default="cuda", help="Device to run inference (e.g., 'cpu', 'cuda', 'cuda:0', 'cuda:1')")
parser.add_argument("--out_dir", type=str, default="out", help="Directory to load checkpoint from")
parser.add_argument("--quantization_data_file", type=str, default=None, help="File name to export the quantized weights/activations, scale factor, and zero point")
parser.add_argument("--init_from", type=str, default="resume", help="Either 'resume' (from an out_dir) or a GPT-2 variant (e.g., 'gpt2-xl')")
parser.add_argument("--start", type=str, default="\n", help="Start text for generation. Can specify a file using 'FILE:prompt.txt'")
parser.add_argument("--num_samples", type=int, default=3, help="Number of inference streams to draw")
parser.add_argument("--max_new_tokens", type=int, default=500, help="Number of tokens to generate in each sample")
parser.add_argument("--temperature", type=float, default=0.8, help="Temperature for predictions (1.0 = no change, < 1.0 = less random, > 1.0 = more random)")
parser.add_argument("--top_k", type=int, nargs='+', default=[1, 200], help="Retain only the top_k most likely tokens")
parser.add_argument("--seed", type=int, default=1337, help="Seed for pseudorandom number generator")
parser.add_argument("--dtype", type=str, default="bfloat16", choices=["bfloat16", "float16", "float32"], help="Torch data type for inference")
parser.add_argument('--compile', action=argparse.BooleanOptionalAction, help="Compile the model (requires PyTorch 2.0)")
parser.add_argument('--sample_file', type=str, default=None, help="Output file for inference")
parser.add_argument('--interactive', action=argparse.BooleanOptionalAction, help="Enable interactive generation")
parser.add_argument('--stop_strings', nargs='+', type=str, default=['~W'], help="One or more strings to stop generation and allow user input. ""E.g. --stop_strings \"\n\n\" \".\"")
parser.add_argument('--last_k_tokens', type=int, default=10, help="Number of last tokens to display in heatmaps")
parser.add_argument('--chart_type', type=str, default='heatmap', choices=['heatmap', 'barchart'], help="Type of chart to display: 'heatmap' or 'barchart'")
parser.add_argument('--block_size', type=int, default=None, help="Block size for context length, default is model's block size")
parser.add_argument('--sym_rot_num_angles', type=int, default=None, help="Number of angles for symmetrical rotary embedding")
parser.add_argument('--rope_length', type=int, default=None, help="Number of embeddings to rotate (must be an even number <= total embedding size)")
parser.add_argument('--token_boundary', type=str, default=None, help="optional separator between emitted tokens")
parser.add_argument('--print_model_info', default=True, action=argparse.BooleanOptionalAction, help="print info about model before inference")
parser.add_argument('--weights_only', default=False, action=argparse.BooleanOptionalAction, help="disable to allow full pickle loading for legacy checkpoints")
parser.add_argument(
'--cosine_penalty',
type=float,
nargs='*',
default=None,
help="Apply a penalty to logits based on cosine similarity to recent tokens. "
"Use alone for defaults (N=5, alpha=1.0). "
"Optionally provide lookback window N and penalty strength alpha. Ex: --cosine_penalty 5 1.5"
)
# Output Confidence
parser.add_argument('--colorize_mode', type=str, default='minmax',
choices=['minmax', 'softmax', 'softmax_top_k', 'rank', 'dot_product', 'topk', 'all'],
help="Mode to colorize text: 'minmax' (default), 'softmax', 'softmax_top_k' for softmax over top-k values,"
" 'rank', 'dot_product', or 'topk' to display a prediction table. "
"Requires --colorize_output (enabled by default).")
parser.add_argument('--colorize_topk', type=int, default=10,
help="Number of top predictions to display when colorize_mode='topk'")
parser.add_argument('--colorize_output', default=False, action=argparse.BooleanOptionalAction,
help="Colorize tokens based on their predicted probabilities. Default = True. "
"Disable with --no-colorize-output.")
# Visualizations
parser.add_argument('--show_heatmaps', default=False, action=argparse.BooleanOptionalAction, help="Show heatmaps of top-k choices for each token")
parser.add_argument('--show_minmax_chart', default=False, action=argparse.BooleanOptionalAction, help="Output a line chart of the chosen-token logits used for minmax colorization")
parser.add_argument(
'--softmax_threshold',
type=float,
nargs='?',
const=0.5, # default value if flag is present without a value
default=None, # default value if flag is not present
help="Enable softmax threshold sampling. Only considers tokens with a probability within this percentage of the top probability. "
"Use without a value for default 50%% (0.5), or provide one e.g. '--softmax_threshold 0.2'. Overrides --top_k.")
# Steering Vector Related
parser.add_argument('--save_avg_vector', type=str, default=None, help="Path to save the average vector of the start text to an .npy file")
parser.add_argument('--apply_vector_file1', type=str, default=None, help="First .npy file to load the vector for subtraction")
parser.add_argument('--apply_vector_file2', type=str, default=None, help="Second .npy file to load the vector for subtraction")
parser.add_argument('--steering_vector_scaling_factor', type=float, default=1.0, help="Scaling factor to apply after subtracting vectors")
parser.add_argument('--apply_to_layer_idx', type=int, default=None, help="Layer index at which to apply the resulting vector")
# Leanred Steering Vector Related
parser.add_argument('--use_lsv', default=False, action=argparse.BooleanOptionalAction)
parser.add_argument('--lsv_size', type=int, default=1, help="Number of vectors to test")
parser.add_argument('--lsv_scaling_factor', type=float, default=None, help="scaling factor")
parser.add_argument('--lsv_mixture', type=float, nargs='+', default=None, help="scaling factor mixture")
# Multicontext Related
parser.add_argument('--multicontext', action=argparse.BooleanOptionalAction, help="multicontext mode inference")
parser.add_argument('--multicontext_datasets', type=str, nargs='+', default=None, help="list of dataset names")
parser.add_argument('--multicontext_start', type=str, nargs='+', default=None,
help="List of start strings, one for each context, if using --multicontext. "
"Must match the number/order of --multicontext_datasets.")
parser.add_argument('--multicontext_start_files', type=str, nargs='+', default=None,
help="Optional list of .bin files (one per multicontext dataset) used as start token sequences.")
parser.add_argument('--multicontext_start_file_dtype', type=str, default='uint16', choices=['uint16', 'uint32'],
help="Element dtype for --multicontext_start_files.")
parser.add_argument('--multicontext_start_file_max_tokens', type=int, default=None,
help="If set, keep only the last N tokens from each --multicontext_start_files input.")
parser.add_argument('--multicontext_csv_input', type=str, default=None,
help="Optional CSV prompt for multicontext sampling. Columns are matched to --multicontext_datasets by metadata/name/order.")
parser.add_argument('--multicontext_csv_has_header', default=True, action=argparse.BooleanOptionalAction,
help="Whether --multicontext_csv_input has a header row. Disable with --no-multicontext_csv_has_header.")
parser.add_argument('--multicontext_csv_output_dir', type=str, default=None,
help="Directory for timestamped CSV outputs generated from --multicontext_csv_input. Defaults to --out_dir/csv_samples.")
parser.add_argument('--multicontext_csv_output_file', type=str, default=None,
help="Optional explicit CSV output path. Only valid with --num_samples 1.")
parser.add_argument('--multicontext_csv_output_include_prompt', default=True, action=argparse.BooleanOptionalAction,
help="Include prompt rows in generated multicontext CSV output. Disable to write only continuation rows.")
parser.add_argument(
'--numerical_multicontext_plotly',
action=argparse.BooleanOptionalAction,
help="Generate a single Plotly HTML report with one graph per multicontext channel.",
)
parser.add_argument(
'--numerical_multicontext_plotly_file',
type=str,
default='numerical_multicontext_samples.html',
help="Output HTML path for --numerical_multicontext_plotly.",
)
parser.add_argument(
'--numerical_multicontext_plotly_include_prompt',
default=True,
action=argparse.BooleanOptionalAction,
help="Include prompt values in Plotly traces. Disable to plot only generated continuation.",
)
parser.add_argument("--eval_only", action=argparse.BooleanOptionalAction, help="Enable evaluation only mode to calculate and print validation loss")
parser.add_argument("--eval_iters", type=int, default=250, help="iterations for evaluation")
parser.add_argument("--eval_dataset", type=str, default=None, help="dataset for evaluation")
parser.add_argument(
"--eval_output_dir",
type=str,
default=None,
help="Optional directory to also write eval_loss.txt when running --eval_only.",
)
parser.add_argument(
"--embedding_gaussian_noise_std",
type=float,
default=None,
help="Override embedding gaussian noise scale at inference time (None uses checkpoint value).",
)
parser.add_argument('--batch_size', type=int, default=1,
help="Batch size to use for evaluation")
return parser.parse_args()
def convert_rich_renderable_to_ansi(renderable) -> str:
"""Convert any Rich renderable (Text, Table, etc.) into an ANSI string."""
buffer = io.StringIO()
temp_console = Console(
file=buffer,
force_terminal=True,
color_system="truecolor",
)
temp_console.print(renderable)
return buffer.getvalue()
def append_to_sample_file(sample_file, output_line, start_token, k_tag, iter_num=None, best_val_loss=None, run_name=None):
to_print = {
"run_name": run_name,
"iter_num": iter_num,
"best_val_loss": best_val_loss,
"top_k": k_tag,
}
with open(sample_file, "a", encoding="utf-8", errors="replace") as file:
header = '\n---------------'
# Print remaining available statistics
for name, value in to_print.items():
if value is not None:
header += f"\n {name}: {value} \n"
# Handle start token as special case due to special chars
if start_token is not None:
header += f"\n start_token: {repr(start_token)} \n"
header += '---------------\n'
# If it's a Rich renderable, convert it to an ANSI string
if not isinstance(output_line, str):
output_line = convert_rich_renderable_to_ansi(output_line)
file.write(header + output_line + '\n\n')
def colorize_text(tokens, data_for_color, decode, colorize_mode='minmax'):
"""
Colorizes each token according to one of two modes:
- 'minmax': data_for_color is a 1D list/array of chosen-token logits.
We min-max normalize them across time, then map to R->G colors.
- 'softmax': data_for_color is a 2D list/array (T, vocab_size) containing
the *full* distribution at each step. We extract the chosen
token's probability for each step, then min-max normalize.
"""
text = Text()
norm_values = None
if colorize_mode == 'softmax' or colorize_mode == 'softmax_top_k':
# data_for_color is shape (T, vocab_size) per step
# gather the chosen token's probability each step
# then apply min–max to those probabilities
dist_tensor = torch.stack(data_for_color, dim=0) # shape (T, vocab_size)
chosen_probs = []
for i, dist_row in enumerate(dist_tensor):
# print(dist_row)
prob_dist = F.softmax(dist_row, dim=-1)
# print(prob_dist)
# input()
chosen_probs.append(prob_dist[tokens[i]])
values = torch.stack(chosen_probs)
norm_values = values
if colorize_mode == 'minmax' or colorize_mode == 'dot_product':
# data_for_color is shape (T,) with each chosen-token score (logit or dot product)
values = torch.tensor(data_for_color, dtype=torch.float32)
# Normalize the chosen values (probabilities or logits) to [0..1]
norm_values = (values - values.min()) / (values.max() - values.min() + 1e-6)
segments = _token_segments(tokens, decode)
for i, token_str in enumerate(segments):
color_val = norm_values[i].item() # 0..1
r = int((1 - color_val) * 255)
g = int(color_val * 255)
text.append(token_str, style=f"bold #{r:02x}{g:02x}00")
return text
def _escape_ws(text: str) -> str:
return text.replace("\n", "\\n").replace("\r", "\\r").replace("\t", "\\t")
def _token_segments(tokens: List[int], decode: Callable) -> List[str]:
"""Return per-token text segments via incremental prefix decoding.
Some tokenizers (notably HuggingFace byte-level BPE) apply cleanup or
joining logic that makes ``decode([single_id])`` differ from the
corresponding slice of ``decode(full_sequence)``. Incremental prefix
decoding avoids this: the text contributed by token *i* is defined as
``decode(tokens[:i+1])[len(decode(tokens[:i])):]``, which exactly
partitions ``decode(tokens)`` into per-token segments.
"""
if not tokens:
return []
segments: List[str] = []
prev = decode([]) # baseline: empty sequence → ""
for i in range(len(tokens)):
cur = decode(list(tokens[: i + 1]))
segments.append(cur[len(prev):])
prev = cur
return segments
def _topk_table(
token_ids: List[int],
rows: List[torch.Tensor],
decode: Callable[[Sequence[int]], str],
k: int,
max_token_chars: int = 20,
escape_ws: bool = True,
) -> Table:
"""Return a Rich table showing top-k predictions for each token."""
table = Table(show_header=False, box=None, pad_edge=False)
table.add_column("target", no_wrap=True)
table.add_column("xent", justify="right", no_wrap=True)
table.add_column("rank", justify="right", no_wrap=True)
table.add_column("p_tgt", justify="right", no_wrap=True)
table.add_column("p_left", justify="right", no_wrap=True)
for _ in range(k):
table.add_column(justify="center", no_wrap=True)
for tid, row in zip(token_ids, rows):
probs = F.softmax(row, dim=-1)
tgt_prob = probs[tid].item()
rank = int((row > row[tid]).sum().item()) + 1
prob_left = probs[row > row[tid]].sum().item()
ce = -math.log(tgt_prob + 1e-12)
topv, topi = row.topk(k)
norm = (topv - topv.min()) / (topv.max() - topv.min() + 1e-6)
words: List[Text] = []
for idx, v in zip(topi.tolist(), norm.tolist()):
r = int((1 - v) * 255); g = int(v * 255)
style = f"#{r:02x}{g:02x}00"
token = decode([idx])
if max_token_chars >= 0:
token = token[:max_token_chars]
if escape_ws:
token = _escape_ws(token)
if idx == tid:
words.append(Text(token, style="bold cyan"))
else:
words.append(Text(token, style=style))
rank_norm = 1 - (min(rank, 100) - 1) / 99
r = int((1 - rank_norm) * 255); g = int(rank_norm * 255)
rank_text = Text(str(rank), style=f"bold #{r:02x}{g:02x}00")
v = tgt_prob
r = int((1 - v) * 255); g = int(v * 255)
p_tgt_text = Text(f"{tgt_prob:.4f}", style=f"bold #{r:02x}{g:02x}00")
v = 1 - prob_left
r = int((1 - v) * 255); g = int(v * 255)
p_left_text = Text(f"{prob_left:.4f}", style=f"bold #{r:02x}{g:02x}00")
target_word = decode([tid])
if max_token_chars >= 0:
target_word = target_word[:max_token_chars]
if escape_ws:
target_word = _escape_ws(target_word)
table.add_row(Text(target_word, style="bold cyan"), f"{ce:.4f}", rank_text, p_tgt_text, p_left_text, *words)
return table
def save_chart(probs, idx, decode, step, out_dir, last_k_tokens, chart_type, selected_token, top_k_value, args):
"""
Generates and saves a chart of token probabilities for a single generation step.
This function adapts its visualization based on the sampling method specified in `args`.
- If `softmax_threshold` is used, it visualizes the actual pool of candidate tokens.
- If `top_k` is used, it visualizes the top `k` most likely tokens.
Args:
probs (torch.Tensor): The final probability distribution tensor (shape: 1, vocab_size)
used for sampling the next token.
idx (torch.Tensor): The tensor of all generated token IDs so far.
decode (function): A function to decode a list of token IDs into a string.
step (int): The current generation step number.
out_dir (str): The base output directory to save charts into.
last_k_tokens (int): The number of recent tokens to show in the chart's context label.
chart_type (str): The type of chart to generate ('heatmap' or 'barchart').
selected_token (str): The string representation of the token that was actually chosen.
top_k_value (int or None): The `k` value used for top-k sampling.
args (argparse.Namespace): The command-line arguments, used to check the sampling mode.
"""
# --- 1. Determine Visualization Parameters based on Sampling Mode ---
vocab_size = probs.size(-1)
chart_title = ""
num_candidates = 0
if args.softmax_threshold is not None:
# Mode: Softmax Threshold Sampling
# Visualize the actual candidate pool (tokens with non-zero probability).
num_candidates = torch.count_nonzero(probs).item()
# Cap the number of plotted tokens for readability.
k_to_plot = min(num_candidates, 60)
chart_title = f"Top {k_to_plot} of {num_candidates} Candidates (Softmax Threshold)"
else:
# Mode: Top-K or No Sampling Truncation
# Use the provided top_k_value to determine how many tokens to show.
k_to_plot = top_k_value
if k_to_plot is None:
# If no top_k was specified, use a reasonable default for visualization.
k_to_plot = 40
k_to_plot = min(k_to_plot, vocab_size)
chart_title = f"Top-{k_to_plot} Probabilities (Top-K Setting: {top_k_value})"
# --- 2. Prepare Data for Plotting ---
# Get the top k probabilities and their corresponding indices from the final distribution.
# This works for both modes because we want to see the most likely candidates.
top_probs, top_indices = torch.topk(probs.flatten(), k=k_to_plot)
top_tokens = [decode([i.item()]) for i in top_indices]
# --- 3. Generate and Save the Chart ---
plt.figure(figsize=(16, 9))
if chart_type == 'heatmap':
annot_data = np.array(top_tokens).reshape(1, -1)
sns.heatmap(
top_probs.cpu().numpy().reshape(1, -1),
annot=annot_data,
fmt='',
cmap='viridis',
cbar_kws={'label': 'Probability'}
)
plt.yticks([]) # Hide y-axis ticks as they are not meaningful here.
plt.title(f"Step {step}: {chart_title} (Heatmap)")
elif chart_type == 'barchart':
colors = sns.color_palette('viridis', n_colors=k_to_plot)
bars = plt.bar(top_tokens, top_probs.cpu().numpy(), color=colors)
plt.ylabel("Probability")
plt.ylim(0.0, 1.0) # Ensure a consistent y-axis scale for probabilities.
plt.xticks(rotation=45, ha="right") # Prevent x-axis label overlap.
# Highlight the bar for the token that was actually selected.
try:
selected_token_index = top_tokens.index(selected_token)
bars[selected_token_index].set_edgecolor('red')
bars[selected_token_index].set_linewidth(2)
except ValueError:
# This can happen if the selected token is not in the top k_to_plot,
# which is unlikely but possible with unusual settings.
print(f"Note: Selected token '{selected_token}' not in top {k_to_plot} for visualization at step {step}.")
plt.title(f"Step {step}: {chart_title} (Bar Chart)")
# Add a descriptive x-axis label showing the recent generation context.
last_tokens_decoded = decode(idx[0, -last_k_tokens:].tolist())
plt.xlabel(f"Token Candidates (Context: ...{last_tokens_decoded})")
# --- 4. Save to File ---
# Ensure the 'charts' subdirectory exists.
charts_dir = os.path.join(out_dir, 'charts')
os.makedirs(charts_dir, exist_ok=True)
# Use a high-resolution timestamp to prevent filename collisions.
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
out_path = os.path.join(charts_dir, f"step_{step}_{timestamp}.png")
plt.tight_layout() # Adjust layout to prevent labels from being cut off.
plt.savefig(out_path)
plt.close() # Close the plot to free up memory.
def save_raw_logits_chart(raw_logit_values, out_dir, k_tag, sample_idx):
"""
Generates and saves a line chart of the raw, pre-temperature chosen-token logits over time.
"""
# Ensure there's data to plot
if not raw_logit_values:
return
# Convert list of single-item tensors to a numpy array
logits_np = torch.tensor(raw_logit_values).cpu().numpy()
steps = np.arange(len(logits_np))
plt.figure(figsize=(16, 9))
plt.plot(steps, logits_np, marker='o', linestyle='-', label=f'Sample {sample_idx+1}, K-Setting: {k_tag}')
# The Y-axis is automatically scaled by matplotlib to the min and max of the data
plt.ylabel("Raw Model Logit (Pre-Temperature)")
plt.xlabel("Generation Step")
plt.title(f"Raw Chosen-Token Model Logits Over Time")
plt.grid(True)
plt.legend()
# Save the figure to the 'charts' subdirectory
charts_dir = os.path.join(out_dir, 'charts')
os.makedirs(charts_dir, exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
out_path = os.path.join(charts_dir, f"raw_logits_k{k_tag}_sample{sample_idx+1}_{timestamp}.png")
plt.tight_layout()
plt.savefig(out_path)
plt.close()
def _colorize_rank(
token_ids: List[int],
ranks: List[int],
decode: Callable[[Sequence[int]], str],
k: Optional[int],
) -> Text:
"""
Return a Rich Text object whose colours encode rank:
• rank == 1 → no colour (default terminal fg)
• rank 2..k → gradient green -> yellow -> red
• rank > k → no colour
"""
text = Text()
max_rank = max(k or 0, 2) # guarantees divisor ≥ 1
segments = _token_segments(list(token_ids), decode)
for token_str, rnk in zip(segments, ranks):
if rnk == 1:
# best-rank token: leave unstyled
text.append(token_str)
elif 2 <= rnk <= max_rank:
ratio = (rnk - 2) / (max_rank - 2) if max_rank > 2 else 1.0
r = int(255 * ratio) # 0 → green, 1 → red
g = int(255 * (1 - ratio))
# style string identical to your colorize_text template
text.append(token_str, style=f"bold #{r:02x}{g:02x}00")
else:
text.append(token_str) # ranks outside 1..k
return text
def sample_with_existing_model(
model: torch.nn.Module,
start_ids: torch.Tensor,
decode: Callable[[Sequence[int]], str],
device: str = "cuda",
max_new_tokens: int = 200,
temperature: float = 0.8,
top_k: Union[int, Sequence[int], None] = 200, # list allowed
start_tokens: Optional[Sequence[int]] = None,
num_samples: int = 1,
# Additional Args
args=None,
# ── visual / logging flags ────────────────────────────────────────────
colorize_output: bool = False,
colorize_mode: str = "minmax", # "rank", "topk" & "all" supported
token_boundary: Optional[str] = None,
show_heatmaps: bool = False,
chart_type: str = "heatmap",
last_k_tokens: int = 10,
out_dir: Union[str, Path] = "out",
sample_file: Optional[str] = None,
iter_num: Optional[int] = None,
best_val_loss: Optional[float] = None,
run_name: Optional[str] = None,
writer: Optional[object] = None,
dataset_idx: Optional[int] = None,
console: Console | None = None,
):
"""
Generate text from an already-loaded GPT model.
Parameters
----------
top_k : int | list[int] | None
• int – sample from top-k.
• None – no truncation.
• list – run once per k in the list (duplicates filtered).
colorize_mode :
"minmax" | "softmax" | "softmax_top_k" | "dot_product" | "rank" | "topk" | "all"
writer : torch.utils.tensorboard.SummaryWriter | None
When provided, dataset metrics for each top-k sample will be logged to TensorBoard.
"""
console = console or Console()
# Determine sampling strategy. Softmax threshold overrides top_k.
if args.softmax_threshold is not None:
console.print(f"[yellow]Info:[/yellow] Using softmax threshold sampling ({args.softmax_threshold:.2f}). --top_k will be ignored.")
# Force the loop to run once with a null k-value
k_values: List[Optional[int]] = [None]
else:
# Use the standard top_k logic
if top_k is None or isinstance(top_k, int):
k_values: List[Optional[int]] = [top_k]
else:
k_values = list(dict.fromkeys(top_k)) # Deduplicate
console = Console()
model.eval()
valid_modes = ["minmax", "softmax", "softmax_top_k", "dot_product", "rank", "topk"]
modes_to_apply = valid_modes if colorize_mode == "all" else [colorize_mode]
for current_k in k_values:
# Set a tag for logging/filenames based on the active sampling mode
if args.softmax_threshold is not None:
k_tag = f"sm_thresh_{args.softmax_threshold:.2f}"
else:
k_tag = "no_topk" if current_k is None else f"top_k_{current_k}"
for sample_idx in range(num_samples):
# ------------- LSV per-sample section -------------------
kl_divergences = [] # To store the impact of the cosine penalty
if args is not None:
# This block handles LSV for standalone sampling. When called from train.py,
# lsv_size is not an arg, so we skip this to avoid an AttributeError and
# to respect the index already set by the trainer.
if args.use_lsv and hasattr(args, 'lsv_size'):
model.set_lsv_index(sample_idx % args.lsv_size)
if args.lsv_scaling_factor is not None:
model.set_lsv_scaling_factor(args.lsv_scaling_factor)
if args.lsv_mixture is not None:
model.set_lsv_mode(2)
model.set_lsv_mixture(args.lsv_mixture)
else:
model.set_lsv_mode(1)
console.print(f"[green]LSV[/green] idx={sample_idx % args.lsv_size} "
f"scale={args.lsv_scaling_factor} "
f"mixture={args.lsv_mixture}")
# ------------- END LSV per-sample section -------------------
x = start_ids.clone()
# storage for colouring
tokens_for_color: List[int] = []
full_rows: List[torch.Tensor] = []
topk_rows: List[torch.Tensor] = []
pre_temp_scalar_rows: List[torch.Tensor] = []
scalar_rows: List[torch.Tensor] = []
ranks_list: List[int] = [] # NEW
with torch.no_grad():
for _step in range(max_new_tokens):
idx_cond = (
x
if x.size(1) <= model.config.block_size
else x[:, -model.config.block_size :]
)
model_logits, _ = model(idx_cond, dataset_idx=dataset_idx)
raw_logits_row = model_logits[:, -1, :] # Raw logits from model
# --- Apply Cosine Similarity Penalty (if enabled) ---
if args.cosine_penalty is not None:
N = 5 if len(args.cosine_penalty) < 1 else int(args.cosine_penalty[0])
alpha = 1.0 if len(args.cosine_penalty) < 2 else args.cosine_penalty[1]
# Calculate original probabilities for comparison
probs_before = F.softmax(raw_logits_row / temperature, dim=-1)
# Apply penalty as long as there are tokens in the context and N > 0
if x.size(1) > 0 and N > 0:
# Python's negative slicing gracefully handles cases where x.size(1) < N
last_n_tokens = x[0, -N:]
embedding_matrix = model.transformer.wte.weight
# Normalize embeddings
last_n_embeds = F.normalize(embedding_matrix[last_n_tokens], p=2, dim=1)
all_embeds = F.normalize(embedding_matrix, p=2, dim=1)
# Calculate max cosine similarity for each candidate against the last N tokens
sim_matrix = torch.matmul(all_embeds, last_n_embeds.T)
max_sim_per_candidate, _ = torch.max(sim_matrix, dim=1)
penalty = alpha * max_sim_per_candidate
raw_logits_row = raw_logits_row - penalty
# Calculate KL divergence to measure the change
probs_after = F.softmax(raw_logits_row / temperature, dim=-1)
# Add a small epsilon to avoid log(0)
kl_div = F.kl_div(torch.log(probs_after + 1e-9), probs_before, reduction='sum')
kl_divergences.append(kl_div.item())
logits = raw_logits_row / temperature # Scaled logits for sampling
full_row = logits[0].clone() # pre-mask
# Apply the selected truncation logic
if args.softmax_threshold is not None:
# Calculate probabilities and find the threshold
probs = F.softmax(logits, dim=-1)
max_prob = torch.max(probs)
prob_threshold = max_prob * args.softmax_threshold
# Set probabilities of tokens below the threshold to 0
probs[probs < prob_threshold] = 0
topk_row = logits[0].clone() # post-mask
if args.softmax_threshold is not None:
# Calculate probabilities and find the threshold
probs = F.softmax(logits, dim=-1)
max_prob = torch.max(probs)
prob_threshold = max_prob * args.softmax_threshold
# Set probabilities of tokens below the threshold to 0
probs[probs < prob_threshold] = 0
# Sample from the modified, unnormalized distribution of probabilities
idx_next = torch.multinomial(probs, num_samples=1)
# For colorization, we can still use the unmasked logits
topk_row = logits[0].clone()
elif current_k is not None:
v, _ = torch.topk(logits, min(current_k, logits.size(-1)))
logits[logits < v[:, [-1]]] = -float("inf")
topk_row = logits[0].clone() # post-mask
probs = F.softmax(logits, dim=-1) # Re-softmax after masking
idx_next = torch.multinomial(probs, num_samples=1)
else: # No truncation / default case
topk_row = logits[0].clone()
probs = F.softmax(logits, dim=-1)
idx_next = torch.multinomial(probs, num_samples=1)
x = torch.cat((x, idx_next), dim=1)
if colorize_output:
chosen = idx_next.item()
# rank: 1 = best
rank = (full_row > full_row[chosen]).sum().item() + 1
tokens_for_color.append(chosen)
full_rows.append(full_row)
topk_rows.append(topk_row)
scalar_rows.append(full_row[chosen])
if args.show_minmax_chart:
pre_temp_scalar_rows.append(raw_logits_row[0, chosen])
ranks_list.append(rank)
if show_heatmaps:
sel_txt = decode([idx_next.item()])
save_chart( # type: ignore
probs,
x,
decode,
_step,
out_dir,
last_k_tokens,
chart_type,
sel_txt,
current_k,
args,
)
# ---------- Print summary statistics for this sample ------------------
if kl_divergences:
avg_kl = np.mean(kl_divergences)
console.print(f"\n[bold yellow]Cosine Penalty Impact (Avg KL Divergence):[/bold yellow] [bold cyan]{avg_kl:.4f}[/bold cyan]")
# ---------- save minmax chart if requested ----------------------
if args.show_minmax_chart and pre_temp_scalar_rows:
save_raw_logits_chart(
pre_temp_scalar_rows, out_dir, k_tag, sample_idx
)
# ---------- decode plain text -----------------------------------
plain_text = decode(x[0].tolist())
if token_boundary is not None:
plain_text = plain_text.replace(token_boundary, " ")
if args and getattr(args, "sample_metrics", False):
metrics = run_all(plain_text)
metric_str = ", ".join(f"{k}={v:.3f}" for k, v in metrics.items())
console.print(
f"\n[bold magenta]Metrics ({k_tag}, sample {sample_idx+1}):[/bold magenta] {metric_str}"
)
if writer is not None and getattr(args, "tensorboard_log", False):
for mk, mv in metrics.items():
# group top-k runs on a single chart per metric
writer.add_scalars(f"sample_metrics/{mk}", {k_tag: mv}, iter_num or 0)
# ---------- colourised outputs ----------------------------------
if colorize_output:
# --- Pre-calculate any special data sources for colorization ---
dot_product_values = None
if 'dot_product' in modes_to_apply and len(tokens_for_color) > 1:
dot_product_values = [0.0] # First token has no prior, assign neutral value.
embedding_matrix = model.transformer.wte.weight
for i in range(1, len(tokens_for_color)):
prev_vec = F.normalize(embedding_matrix[tokens_for_color[i-1]], p=2, dim=0)
current_vec = F.normalize(embedding_matrix[tokens_for_color[i]], p=2, dim=0)
# The dot product of two unit vectors is their cosine similarity.
dot_product_values.append(torch.dot(prev_vec, current_vec).item())
for cm in modes_to_apply:
# Select the appropriate data source for the current colorization mode
data_for_color = None
if cm == "minmax":
data_for_color = scalar_rows
elif cm == "softmax":
data_for_color = full_rows
elif cm == "softmax_top_k":
data_for_color = topk_rows
elif cm == "dot_product":
data_for_color = dot_product_values
if data_for_color is not None:
coloured = colorize_text( # type: ignore
tokens_for_color,
data_for_color,
decode,
colorize_mode=cm,
)
elif cm == "rank":
coloured = _colorize_rank(
tokens_for_color, ranks_list, decode, current_k
)
elif cm == "topk":
coloured = _topk_table(
tokens_for_color, full_rows, decode, args.colorize_topk
)
else:
continue # Should not happen if data_for_color is None
fgcolor="bold light_slate_blue"
bgcolor="bold cyan"
console.print(f"\n\n[{bgcolor}]--- tokens=[/{bgcolor}][{fgcolor}]{max_new_tokens}[/{fgcolor}][{bgcolor}], top_k=[/{bgcolor}][{fgcolor}]{k_tag}[/{fgcolor}][{bgcolor}], colorization=[/{bgcolor}][{fgcolor}]{cm}[/{fgcolor}][{bgcolor}] ---[/{bgcolor}]\n")
console.print(coloured)
if sample_file:
append_to_sample_file( # type: ignore
sample_file,
coloured,
start_tokens,
k_tag,
iter_num,
best_val_loss,
f"{run_name}_{k_tag}_{cm}" if run_name else f"{k_tag}_{cm}",
)
else:
console.print(f"[bold cyan]--- {k_tag} ---[/bold cyan]")
console.print("[bold green]" + plain_text + "[/bold green]")
# ---------- always store plain text once ------------------------
if sample_file:
append_to_sample_file( # type: ignore
sample_file,
plain_text,
start_tokens,
k_tag,
iter_num,
best_val_loss,
f"{run_name}_{k_tag}" if run_name else k_tag,
)
def interactive_generation(model, start_ids, device, max_new_tokens, temperature, top_k, stop_string, decode, encode):
x = torch.tensor(start_ids, dtype=torch.long, device=device)[None, ...]
while True:
x, generated_text = model.generate_with_stop(x, max_new_tokens, stop_string, decode, temperature, top_k)
print("[bold green]" + generated_text)
user_input = input("User input (or 'exit' to quit): ")
if user_input.lower() == 'exit':
break
# Append the user input directly after the stop string
x = torch.cat((x, torch.tensor(encode(user_input), dtype=torch.long, device=device)[None, ...]), dim=1)
def save_args(args, out_dir):
with open(os.path.join(out_dir, 'args.json'), 'w') as f:
json.dump(vars(args), f, indent=4)
def write_eval_summary(
out_dir: Union[str, os.PathLike[str], None],
summary: Dict[str, object],
*,
extra_dirs: Optional[Sequence[Union[str, os.PathLike[str]]]] = None,
) -> None:
if not summary:
return
def _convert(value):
if isinstance(value, torch.Tensor):
value = value.detach()
if value.numel() == 1:
return float(value.item())
return value.cpu().tolist()
if isinstance(value, np.generic):
return value.item()
if isinstance(value, np.ndarray):
return value.tolist()
if isinstance(value, Path):
return str(value)
if isinstance(value, (float, int)) and not isinstance(value, bool):
return float(value)
if isinstance(value, (list, tuple)):
return [_convert(v) for v in value]
if isinstance(value, dict):
return {k: _convert(v) for k, v in value.items()}
return value
destinations: List[str] = []
if out_dir:
destinations.append(os.fspath(out_dir))
if extra_dirs:
for path in extra_dirs:
if path:
destinations.append(os.fspath(path))
if not destinations:
return
serializable = {
key: _convert(value)
for key, value in summary.items()
if value is not None
}
saved_paths: List[str] = []
seen_dirs: set[str] = set()
for directory in destinations:
normalized = os.path.normpath(directory)
if normalized in seen_dirs:
continue
seen_dirs.add(normalized)
os.makedirs(normalized, exist_ok=True)
eval_path = os.path.join(normalized, "eval_loss.txt")
with open(eval_path, "w", encoding="utf-8") as eval_file:
json.dump(serializable, eval_file, indent=2, sort_keys=True)
eval_file.write("\n")
saved_paths.append(eval_path)
if saved_paths:
if len(saved_paths) == 1:
print(f"Saved evaluation metrics to {saved_paths[0]}")
else:
print("Saved evaluation metrics to:")
for path in saved_paths:
print(f" {path}")
#TODO: Rename to reflect general purpose
def save_quantized_data(state_dict, out_file):
to_save = OrderedDict()
for k, v in list(state_dict.items()):
# if "mlp_act" in k or "attn_act" in k or k.endswith("quantized_bias") or k.endswith("bias_norm") or k.endswith("zero_point") or k.endswith("quantized_weight") or k.endswith("weight_norm"):
to_save[k] = v.cpu().numpy()
with open(f"{out_file}.pkl", 'wb') as f:
pickle.dump(to_save, f)
def load_validation_data(block_size, eval_dataset):
# Load validation data similar to how train data is handled
val_path = os.path.join('data', eval_dataset, 'val.bin')
assert os.path.exists(val_path), f"Validation data file {val_path} not found."
meta_path = os.path.join('data', eval_dataset, 'meta.pkl')
dtype = np.uint16
if os.path.exists(meta_path):
with open(meta_path, 'rb') as f:
meta = pickle.load(f)
vocab_size = meta.get('vocab_size')
if vocab_size is not None and int(vocab_size) > np.iinfo(np.uint16).max:
dtype = np.uint32
val_data = np.memmap(val_path, dtype=dtype, mode='r')
return val_data
def get_batch(data, block_size, device):
# Create a random batch from the dataset
ix = torch.randint(len(data) - block_size, (1,))
x = torch.stack([torch.from_numpy((data[i:i + block_size]).astype(np.int64)) for i in ix])
y = torch.stack([torch.from_numpy((data[i + 1:i + 1 + block_size]).astype(np.int64)) for i in ix])
return x.to(device), y.to(device)
def calculate_validation_loss(model, val_data, block_size, eval_iters, device, dtype, dataset_idx: Optional[int] = None):
model.eval()
losses: List[float] = []
total_time = 0.0
with torch.no_grad():
for _ in range(eval_iters):
X, Y = get_batch(val_data, block_size, device)
with torch.amp.autocast(device_type=device, dtype=dtype):
start = time.perf_counter()
logits, loss = model(X, Y, dataset_idx=dataset_idx)
end = time.perf_counter()
total_time += (end - start)
losses.append(float(loss.item()))
if losses:
mean_loss = float(np.mean(losses))
std_loss = float(np.std(losses)) if len(losses) > 1 else 0.0
else:
mean_loss = float("nan")
std_loss = float("nan")
return {
"val": mean_loss,
"val_std": std_loss,
"eval_iters": int(eval_iters),
"num_batches": len(losses),
"elapsed_time_s": float(total_time),
}
def custom_char_with_byte_fallback_encode(text: str, stoi: dict) -> list[int]:
"""Encode ``text`` using a byte-level vocabulary with optional custom tokens.
This mirrors the logic in ``CustomCharTokenizerWithByteFallback``. For each
position in the UTF-8 byte stream we try each custom token (in the order
they were defined) and fall back to emitting the raw byte ID when none
match.
"""
custom_token_bytes = [