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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cogforge-engine
3
- Version: 1.0.4
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
- coef = numpy.float32(float(loss.grad) / N)
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
- _iadd_scaled(scores.grad, g.reshape(scores.shape), backend.np.float32(loss.grad / N))
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:] # crop to context window
35
- logits = self(cond).data[:, -1, :] / temperature # (B, V) — last position only
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 = backend.np.sort(logits, axis=-1)[:, -top_k][:, None]
38
- logits = backend.np.where(logits < kth, -1e9, logits) # keep only top-k choices
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 = backend.np.exp(z).astype(backend.np.float64); p /= p.sum(-1, keepdims=True) # softmax → probabilities
41
- nxt = backend.np.array([[backend.np.random.choice(len(pr), p=pr)] for pr in p]) # sample
42
- idx = backend.np.concatenate([idx, nxt], axis=1) # append, feed back in
43
- return idx # <-- was a bare `return`
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:] # crop to context window
80
- logits = self(cond).data[:, -1, :] / temperature # (B, V) last position only
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 = backend.np.sort(logits, axis=-1)[:, -top_k][:, None]
83
- logits = backend.np.where(logits < kth, -1e9, logits) # keep only top-k choices
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 = backend.np.exp(z).astype(backend.np.float64); p /= p.sum(-1, keepdims=True) # softmax → probabilities
86
- nxt = backend.np.array([[backend.np.random.choice(len(pr), p=pr)] for pr in p]) # sample
87
- idx = backend.np.concatenate([idx, nxt], axis=1) # append, feed back in
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
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "cogforge-engine"
7
- version = "1.0.4"
7
+ version = "1.0.6"
8
8
  authors = [
9
9
  { name="Avik Majumder", email="avikmjd2@gmail.com" },
10
10
  ]
File without changes