cds2.nlp
Educational NLP: BPE tokenizer, scalar autograd, attention, mini-GPT.
Educational NLP toolkit: BPE tokenizer, autograd, attention, mini-GPT.
AttentionWeights
dataclass
Attention output plus the weight matrix for inspection.
Source code in src/cds2/nlp/attention.py
27 28 29 30 31 32 | |
Value
A scalar that records the operations producing it for backprop.
Source code in src/cds2/nlp/autograd.py
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 | |
backward()
Backpropagate gradients through every ancestor operation.
Source code in src/cds2/nlp/autograd.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
TinyGPT
Untrained-but-deterministic mini GPT for studying the forward pass.
Source code in src/cds2/nlp/gpt.py
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 | |
forward(token_ids)
Return logits of shape (len(token_ids), vocab_size).
Source code in src/cds2/nlp/gpt.py
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 | |
sample(prompt_tokens, max_new_tokens=16, temperature=1.0, seed=None)
Autoregressively extend prompt_tokens by sampling the logits.
Source code in src/cds2/nlp/gpt.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
BPETokenizer
Trainable sub-word tokenizer using greedy byte-pair merges.
Source code in src/cds2/nlp/tokenizer.py
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 | |
train(corpus)
Learn merges pair rules from whitespace-split words.
Source code in src/cds2/nlp/tokenizer.py
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 | |
encode(word)
Split word into learned sub-word symbols.
Source code in src/cds2/nlp/tokenizer.py
74 75 76 77 78 79 80 81 82 | |
decode(tokens)
Reconstruct text by joining symbols and dropping end markers.
Source code in src/cds2/nlp/tokenizer.py
84 85 86 87 | |
multi_head_attention(queries, keys, values, heads, mask=None)
Split the last dimension into heads and attend independently.
Source code in src/cds2/nlp/attention.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
scaled_dot_product_attention(queries, keys, values, mask=None)
Classic scaled dot-product attention over a batch or 2-D input.
Source code in src/cds2/nlp/attention.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
softmax(values, axis=-1)
Numerically stable softmax along axis.
Source code in src/cds2/nlp/attention.py
20 21 22 23 24 | |