cogforge-engine 1.0.4__tar.gz → 1.0.6__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {cogforge_engine-1.0.4 → cogforge_engine-1.0.6}/PKG-INFO +1 -1
- {cogforge_engine-1.0.4 → cogforge_engine-1.0.6}/cogforge/app.py +5 -4
- {cogforge_engine-1.0.4 → cogforge_engine-1.0.6}/cogforge/models.py +21 -25
- {cogforge_engine-1.0.4 → cogforge_engine-1.0.6}/pyproject.toml +1 -1
- {cogforge_engine-1.0.4 → cogforge_engine-1.0.6}/.github/workflows/release.yml +0 -0
- {cogforge_engine-1.0.4 → cogforge_engine-1.0.6}/.gitignore +0 -0
- {cogforge_engine-1.0.4 → cogforge_engine-1.0.6}/LICENSE +0 -0
- {cogforge_engine-1.0.4 → cogforge_engine-1.0.6}/README.md +0 -0
- {cogforge_engine-1.0.4 → cogforge_engine-1.0.6}/cogforge/__init__.py +0 -0
- {cogforge_engine-1.0.4 → cogforge_engine-1.0.6}/cogforge/backend.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: cogforge-engine
|
|
3
|
-
Version: 1.0.
|
|
3
|
+
Version: 1.0.6
|
|
4
4
|
Summary: A custom autograd engine and Transformer block built from scratch.
|
|
5
5
|
Project-URL: Homepage, https://github.com/avikmjd2/cogforge
|
|
6
6
|
Author-email: Avik Majumder <avikmjd2@gmail.com>
|
|
@@ -387,10 +387,10 @@ class Tensor:
|
|
|
387
387
|
flat, idx, rows = p.reshape(N, -1), target_ids.reshape(N), backend.np.arange(N)
|
|
388
388
|
loss = cls(-backend.np.log(backend.np.clip(flat[rows, idx], 1e-15, 1.0)).sum() / N, children=(scores,))
|
|
389
389
|
def _backward():
|
|
390
|
+
# g = p.reshape(N, -1).copy()
|
|
390
391
|
g = p.reshape(N, -1)
|
|
391
|
-
g[rows, idx] -= 1.0
|
|
392
|
-
|
|
393
|
-
_iadd_scaled(scores.grad, g.reshape(scores.shape), coef)
|
|
392
|
+
g[rows, idx] -= 1.0 # (softmax - onehot)
|
|
393
|
+
scores.grad += (g.reshape(scores.shape) / N) * loss.grad
|
|
394
394
|
loss._backwards = _backward
|
|
395
395
|
return loss
|
|
396
396
|
|
|
@@ -403,7 +403,8 @@ class Tensor:
|
|
|
403
403
|
def _backward():
|
|
404
404
|
g = p.reshape(N, -1)
|
|
405
405
|
g[rows, idx] -= 1.0
|
|
406
|
-
|
|
406
|
+
coef = numpy.float32(float(loss.grad) / N)
|
|
407
|
+
_iadd_scaled(scores.grad, g.reshape(scores.shape), coef)
|
|
407
408
|
loss._backwards = _backward
|
|
408
409
|
return loss
|
|
409
410
|
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
from . import backend
|
|
2
|
-
from .app import Embedding, PositionalEncoding,Transformer,LayerNorm,Linear,RotatoryPositionalEncoding
|
|
2
|
+
from .app import Embedding, PositionalEncoding,Transformer,LayerNorm,Linear,RotatoryPositionalEncoding,to_cpu
|
|
3
|
+
import numpy
|
|
4
|
+
|
|
3
5
|
|
|
4
6
|
class GPTV1:
|
|
5
7
|
def __init__(self, vocab, d_model, n_heads, n_layers, max_len, d_ff=None):
|
|
@@ -26,21 +28,18 @@ class GPTV1:
|
|
|
26
28
|
return ps
|
|
27
29
|
|
|
28
30
|
def generate(self, idx, n_new, temperature=1.0, top_k=None):
|
|
29
|
-
|
|
30
|
-
idx: (B, T) int array — the prompt (B is usually 1)
|
|
31
|
-
returns: (B, T + n_new) int array
|
|
32
|
-
"""
|
|
31
|
+
idx = numpy.asarray(idx)
|
|
33
32
|
for _ in range(n_new):
|
|
34
|
-
cond = idx[:, -self.max_len:]
|
|
35
|
-
logits = self(cond).data[:, -1, :] / temperature
|
|
33
|
+
cond = idx[:, -self.max_len:]
|
|
34
|
+
logits = to_cpu(self(cond).data)[:, -1, :] / temperature
|
|
36
35
|
if top_k is not None:
|
|
37
|
-
kth =
|
|
38
|
-
logits =
|
|
36
|
+
kth = numpy.sort(logits, axis=-1)[:, -top_k][:, None]
|
|
37
|
+
logits = numpy.where(logits < kth, -1e9, logits)
|
|
39
38
|
z = logits - logits.max(-1, keepdims=True)
|
|
40
|
-
p =
|
|
41
|
-
nxt =
|
|
42
|
-
idx =
|
|
43
|
-
return idx
|
|
39
|
+
p = numpy.exp(z).astype(numpy.float64); p /= p.sum(-1, keepdims=True)
|
|
40
|
+
nxt = numpy.array([[numpy.random.choice(len(pr), p=pr)] for pr in p])
|
|
41
|
+
idx = numpy.concatenate([idx, nxt], axis=1)
|
|
42
|
+
return idx # <-- was a bare `return`
|
|
44
43
|
|
|
45
44
|
|
|
46
45
|
|
|
@@ -71,18 +70,15 @@ class GPT2:
|
|
|
71
70
|
return ps
|
|
72
71
|
|
|
73
72
|
def generate(self, idx, n_new, temperature=1.0, top_k=None):
|
|
74
|
-
|
|
75
|
-
idx: (B, T) int array — the prompt (B is usually 1)
|
|
76
|
-
returns: (B, T + n_new) int array
|
|
77
|
-
"""
|
|
73
|
+
idx = numpy.asarray(idx) # token ids live on host
|
|
78
74
|
for _ in range(n_new):
|
|
79
|
-
cond = idx[:, -self.max_len:]
|
|
80
|
-
logits = self(cond).data[:, -1, :] / temperature
|
|
75
|
+
cond = idx[:, -self.max_len:]
|
|
76
|
+
logits = to_cpu(self(cond).data)[:, -1, :] / temperature # forward on GPU, logits to host
|
|
81
77
|
if top_k is not None:
|
|
82
|
-
kth =
|
|
83
|
-
logits =
|
|
78
|
+
kth = numpy.sort(logits, axis=-1)[:, -top_k][:, None]
|
|
79
|
+
logits = numpy.where(logits < kth, -1e9, logits)
|
|
84
80
|
z = logits - logits.max(-1, keepdims=True)
|
|
85
|
-
p =
|
|
86
|
-
nxt =
|
|
87
|
-
idx =
|
|
88
|
-
return idx
|
|
81
|
+
p = numpy.exp(z).astype(numpy.float64); p /= p.sum(-1, keepdims=True)
|
|
82
|
+
nxt = numpy.array([[numpy.random.choice(len(pr), p=pr)] for pr in p])
|
|
83
|
+
idx = numpy.concatenate([idx, nxt], axis=1)
|
|
84
|
+
return idx
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|