cogforge-engine 2.0.0__tar.gz → 2.0.1__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: 2.0.0
3
+ Version: 2.0.1
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>
@@ -55,7 +55,9 @@ class Tensor:
55
55
  self.typed = typed
56
56
 
57
57
  def __getitem__(self, key):
58
- out_data = self.data[key]
58
+ out_data = backend.np.asarray(self.data[key]) # force a real ndarray
59
+ if backend.NO_GRAD:
60
+ return Tensor(out_data)
59
61
 
60
62
  out = Tensor(out_data,children=(self,))
61
63
 
@@ -455,6 +457,42 @@ class Tensor:
455
457
  loss._backwards = _backward
456
458
  return loss
457
459
 
460
+ @classmethod
461
+ def sparse_softmax_cross_entropy_index(cls, scores, labels, mask):
462
+ """
463
+ scores: Tensor (N, V) logits
464
+ labels: (N,) int correct class id per row
465
+ mask: (N,) {0,1} 1 = real token, 0 = padding
466
+ Same loss/gradient as the one-hot masked version, no (N, V) target built.
467
+ """
468
+ z = scores.data
469
+ z = z - z.max(axis=1, keepdims=True)
470
+ e = backend.np.exp(z)
471
+ p = e / e.sum(axis=1, keepdims=True) # (N, V)
472
+
473
+ labels = backend.np.asarray(labels).reshape(-1) # (N,)
474
+ mask = backend.np.asarray(mask).reshape(-1) # (N,)
475
+ N = p.shape[0]
476
+ rows = backend.np.arange(N)
477
+
478
+ n_real = mask.sum()
479
+ n_real = n_real if n_real > 0 else 1.0
480
+
481
+ logp_correct = backend.np.log(backend.np.clip(p[rows, labels], 1e-15, 1.0))
482
+ loss_data = -backend.np.sum(mask * logp_correct) / n_real
483
+
484
+ if backend.NO_GRAD:
485
+ return cls(loss_data)
486
+
487
+ loss = cls(loss_data, children=(scores,))
488
+ def _backward():
489
+ grad = p.copy()
490
+ grad[rows, labels] -= 1.0
491
+ grad *= (mask / n_real)[:, None]
492
+ scores.grad += grad * loss.grad
493
+ loss._backwards = _backward
494
+ return loss
495
+
458
496
  #Legacy
459
497
  @classmethod
460
498
  def softmax_cross_entropy_old(cls, scores, targets):
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "cogforge-engine"
7
- version = "2.0.0"
7
+ version = "2.0.1"
8
8
  authors = [
9
9
  { name="Avik Majumder", email="avikmjd2@gmail.com" },
10
10
  ]
File without changes
File without changes