cogforge-engine 1.0.5__tar.gz → 1.0.7__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.5
3
+ Version: 1.0.7
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>
@@ -316,6 +316,9 @@ class Tensor:
316
316
 
317
317
  for node in reversed(topo):
318
318
  node._backwards()
319
+ #TRIAL
320
+ node._backwards = lambda: None
321
+ node._children.clear()
319
322
 
320
323
 
321
324
  def flatten(self):
@@ -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,16 @@ 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
+ # backend._cp.get_default_memory_pool().free_all_blocks() if backend.USE_GPU else None
85
+ return idx
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "cogforge-engine"
7
- version = "1.0.5"
7
+ version = "1.0.7"
8
8
  authors = [
9
9
  { name="Avik Majumder", email="avikmjd2@gmail.com" },
10
10
  ]
File without changes