cogforge-engine 1.0.2__tar.gz → 1.0.4__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.2 → cogforge_engine-1.0.4}/PKG-INFO +1 -1
- {cogforge_engine-1.0.2 → cogforge_engine-1.0.4}/cogforge/app.py +12 -11
- {cogforge_engine-1.0.2 → cogforge_engine-1.0.4}/pyproject.toml +1 -1
- {cogforge_engine-1.0.2 → cogforge_engine-1.0.4}/.github/workflows/release.yml +0 -0
- {cogforge_engine-1.0.2 → cogforge_engine-1.0.4}/.gitignore +0 -0
- {cogforge_engine-1.0.2 → cogforge_engine-1.0.4}/LICENSE +0 -0
- {cogforge_engine-1.0.2 → cogforge_engine-1.0.4}/README.md +0 -0
- {cogforge_engine-1.0.2 → cogforge_engine-1.0.4}/cogforge/__init__.py +0 -0
- {cogforge_engine-1.0.2 → cogforge_engine-1.0.4}/cogforge/backend.py +0 -0
- {cogforge_engine-1.0.2 → cogforge_engine-1.0.4}/cogforge/models.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.4
|
|
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>
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import numpy
|
|
2
2
|
from . import backend
|
|
3
|
-
|
|
3
|
+
|
|
4
4
|
|
|
5
5
|
def to_cpu(a):
|
|
6
6
|
"""Return a numpy array regardless of backend."""
|
|
@@ -75,7 +75,7 @@ class Tensor:
|
|
|
75
75
|
arrays = tuple(t.data for t in tensors)
|
|
76
76
|
out = Tensor(backend.np.concatenate(arrays,axis=axis),children=tensors)
|
|
77
77
|
sizes = [a.shape[axis] for a in arrays[:-1]]
|
|
78
|
-
split_indices =
|
|
78
|
+
split_indices = numpy.cumsum(sizes).tolist()
|
|
79
79
|
|
|
80
80
|
|
|
81
81
|
def _backward():
|
|
@@ -383,21 +383,21 @@ class Tensor:
|
|
|
383
383
|
z = scores.data - scores.data.max(axis=-1, keepdims=True)
|
|
384
384
|
e = backend.np.exp(z)
|
|
385
385
|
p = e / e.sum(axis=-1, keepdims=True)
|
|
386
|
-
N = int(
|
|
386
|
+
N = int(numpy.prod(scores.shape[:-1]))
|
|
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()
|
|
391
390
|
g = p.reshape(N, -1)
|
|
392
|
-
g[rows, idx] -= 1.0
|
|
393
|
-
|
|
391
|
+
g[rows, idx] -= 1.0
|
|
392
|
+
coef = numpy.float32(float(loss.grad) / N)
|
|
393
|
+
_iadd_scaled(scores.grad, g.reshape(scores.shape), coef)
|
|
394
394
|
loss._backwards = _backward
|
|
395
395
|
return loss
|
|
396
396
|
|
|
397
397
|
@classmethod
|
|
398
398
|
def sparse_softmax_cross_entropy(cls, scores, target_ids):
|
|
399
399
|
p = _stable_softmax(scores.data)
|
|
400
|
-
N = int(
|
|
400
|
+
N = int(numpy.prod(scores.shape[:-1]))
|
|
401
401
|
flat, idx, rows = p.reshape(N, -1), target_ids.reshape(N), backend.np.arange(N)
|
|
402
402
|
loss = cls(-backend.np.log(backend.np.clip(flat[rows, idx], 1e-15, 1.0)).sum() / N, children=(scores,))
|
|
403
403
|
def _backward():
|
|
@@ -429,7 +429,7 @@ class Tensor:
|
|
|
429
429
|
z = z - z.max(axis=-1, keepdims=True)
|
|
430
430
|
e = backend.np.exp(z)
|
|
431
431
|
p = e / e.sum(axis=-1, keepdims=True)
|
|
432
|
-
N =
|
|
432
|
+
N = numpy.prod(scores.shape[:-1]) # B for 2D, B*T for 3D
|
|
433
433
|
loss = cls(-backend.np.sum(targets * backend.np.log(backend.np.clip(p, 1e-15, 1.0))) / N, children=(scores,))
|
|
434
434
|
def _backward():
|
|
435
435
|
scores.grad += (p - targets) / N * loss.grad
|
|
@@ -459,7 +459,8 @@ class Tensor:
|
|
|
459
459
|
|
|
460
460
|
def transpose(self,axes):
|
|
461
461
|
out = Tensor(backend.np.transpose(self.data,axes=axes),children=(self,))
|
|
462
|
-
inv = backend.np.argsort(axes)
|
|
462
|
+
# inv = backend.np.argsort(axes)
|
|
463
|
+
inv = tuple(int(i) for i in numpy.argsort(axes))
|
|
463
464
|
def _backward():
|
|
464
465
|
self.grad+=backend.np.transpose(out.grad,inv)
|
|
465
466
|
|
|
@@ -707,7 +708,7 @@ class BatchNorm1D:
|
|
|
707
708
|
self.std_inv = 1.0 / backend.np.sqrt(var + self.eps)
|
|
708
709
|
self.x_hat = self.x_centered * self.std_inv
|
|
709
710
|
|
|
710
|
-
N =
|
|
711
|
+
N = numpy.prod([x.data.shape[d] for d in reduce_dims])
|
|
711
712
|
unbiased_var = var.squeeze() * (N / (N - 1)) if N > 1 else var.squeeze()
|
|
712
713
|
|
|
713
714
|
self.running_mean = (1 - self.momentum) * self.running_mean + self.momentum * mean.squeeze()
|
|
@@ -728,7 +729,7 @@ class BatchNorm1D:
|
|
|
728
729
|
self.gamma.grad += backend.np.sum(dout * self.x_hat, axis=reduce_dims)
|
|
729
730
|
self.beta.grad += backend.np.sum(dout, axis=reduce_dims)
|
|
730
731
|
|
|
731
|
-
N =
|
|
732
|
+
N = numpy.prod([x.data.shape[d] for d in reduce_dims])
|
|
732
733
|
|
|
733
734
|
dx_hat = dout * self.gamma.data
|
|
734
735
|
dx = (1.0 / N) * self.std_inv * (
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|