mini-jstorch 1.2.2 → 1.3.2

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.
package/src/MainEngine.js CHANGED
@@ -1 +1,560 @@
1
- console.log("MAJOR UPDATE IN 2:22 AM BE READY GUYS! this was empty because this versions only for an major note.")
1
+
2
+ // MINI JSTORCH ENGINE
3
+ // LICENSE: MIT (C) Rizal 2025
4
+ // 1.2.3 ENGINE VERSIONS
5
+ // IMPORTANT: CORE ENGINE DO NOT EDIT THIS FILES VERY SENSITIVE IT WILL CRASHING YOUR ENGINE SYSTEMS!
6
+
7
+ // ---------------------- Utilities ----------------------
8
+ function zeros(rows, cols) { return Array.from({length:rows},()=>Array(cols).fill(0)); }
9
+ function ones(rows, cols) { return Array.from({length:rows},()=>Array(cols).fill(1)); }
10
+ function randomMatrix(rows, cols, scale=0.1){ return Array.from({length:rows},()=>Array.from({length:cols},()=> (Math.random()*2-1)*scale)); }
11
+ function transpose(matrix){ return matrix[0].map((_,i)=>matrix.map(row=>row[i])); }
12
+ function addMatrices(a,b){ return a.map((row,i)=>row.map((v,j)=>v+(b[i] && b[i][j]!==undefined?b[i][j]:0))); }
13
+ function dot(a,b){ const res=zeros(a.length,b[0].length); for(let i=0;i<a.length;i++) for(let j=0;j<b[0].length;j++) for(let k=0;k<a[0].length;k++) res[i][j]+=a[i][k]*b[k][j]; return res; }
14
+ function softmax(x){ const m=Math.max(...x); const exps=x.map(v=>Math.exp(v-m)); const s=exps.reduce((a,b)=>a+b,0); return exps.map(v=>v/s); }
15
+ function crossEntropy(pred,target){ const eps=1e-12; return -target.reduce((sum,t,i)=>sum+t*Math.log(pred[i]+eps),0); }
16
+
17
+ // ---------------------- Tensor ----------------------
18
+ export class Tensor {
19
+ constructor(data){ this.data=data; this.grad=zeros(data.length,data[0].length); }
20
+ shape(){ return [this.data.length,this.data[0].length]; }
21
+ add(t){ return t instanceof Tensor?this.data.map((r,i)=>r.map((v,j)=>v+t.data[i][j])):this.data.map(r=>r.map(v=>v+t)); }
22
+ sub(t){ return t instanceof Tensor?this.data.map((r,i)=>r.map((v,j)=>v-t.data[i][j])):this.data.map(r=>r.map(v=>v-t)); }
23
+ mul(t){ return t instanceof Tensor?this.data.map((r,i)=>r.map((v,j)=>v*t.data[i][j])):this.data.map(r=>r.map(v=>v*t)); }
24
+ matmul(t){ if(t instanceof Tensor) return dot(this.data,t.data); else throw new Error("matmul requires Tensor"); }
25
+ transpose(){ return transpose(this.data); }
26
+ flatten(){ return this.data.flat(); }
27
+ static zeros(r,c){ return new Tensor(zeros(r,c)); }
28
+ static ones(r,c){ return new Tensor(ones(r,c)); }
29
+ static random(r,c,scale=0.1){ return new Tensor(randomMatrix(r,c,scale)); }
30
+ }
31
+
32
+ // ---------------------- Layers ----------------------
33
+ export class Linear {
34
+ constructor(inputDim,outputDim){
35
+ this.W=randomMatrix(inputDim,outputDim);
36
+ this.b=Array(outputDim).fill(0);
37
+ this.gradW=zeros(inputDim,outputDim);
38
+ this.gradb=Array(outputDim).fill(0);
39
+ this.x=null;
40
+ }
41
+
42
+ forward(x){
43
+ this.x=x;
44
+ const out=dot(x,this.W);
45
+ return out.map((row,i)=>row.map((v,j)=>v+this.b[j]));
46
+ }
47
+
48
+ backward(grad){
49
+ for(let i=0;i<this.W.length;i++) for(let j=0;j<this.W[0].length;j++)
50
+ this.gradW[i][j]=this.x.reduce((sum,row,k)=>sum+row[i]*grad[k][j],0);
51
+ for(let j=0;j<this.b.length;j++)
52
+ this.gradb[j]=grad.reduce((sum,row)=>sum+row[j],0);
53
+
54
+ const gradInput=zeros(this.x.length,this.W.length);
55
+ for(let i=0;i<this.x.length;i++)
56
+ for(let j=0;j<this.W.length;j++)
57
+ for(let k=0;k<this.W[0].length;k++)
58
+ gradInput[i][j]+=grad[i][k]*this.W[j][k];
59
+ return gradInput;
60
+ }
61
+
62
+ parameters(){ return [ {param:this.W,grad:this.gradW}, {param:[this.b],grad:[this.gradb]} ]; }
63
+ }
64
+
65
+ // ---------------------- Conv2D ----------------------
66
+ export class Conv2D {
67
+ constructor(inC,outC,kernel,stride=1,padding=0){
68
+ this.inC=inC; this.outC=outC; this.kernel=kernel;
69
+ this.stride=stride; this.padding=padding;
70
+ this.W=Array(outC).fill(0).map(()=>Array(inC).fill(0).map(()=>randomMatrix(kernel,kernel)));
71
+ this.gradW=Array(outC).fill(0).map(()=>Array(inC).fill(0).map(()=>zeros(kernel,kernel)));
72
+ this.x=null;
73
+ }
74
+
75
+ pad2D(input,pad){
76
+ return input.map(channel=>{
77
+ const rows=channel.length+2*pad;
78
+ const cols=channel[0].length+2*pad;
79
+ const out=Array.from({length:rows},()=>Array(cols).fill(0));
80
+ for(let i=0;i<channel.length;i++) for(let j=0;j<channel[0].length;j++) out[i+pad][j+pad]=channel[i][j];
81
+ return out;
82
+ });
83
+ }
84
+
85
+ conv2DSingle(input,kernel){
86
+ const rows=input.length-kernel.length+1;
87
+ const cols=input[0].length-kernel[0].length+1;
88
+ const out=zeros(rows,cols);
89
+ for(let i=0;i<rows;i++) for(let j=0;j<cols;j++)
90
+ for(let ki=0;ki<kernel.length;ki++) for(let kj=0;kj<kernel[0].length;kj++)
91
+ out[i][j]+=input[i+ki][j+kj]*kernel[ki][kj];
92
+ return out;
93
+ }
94
+
95
+ forward(batch){
96
+ this.x=batch;
97
+ return batch.map(sample=>{
98
+ const channelsOut=[];
99
+ for(let oc=0;oc<this.outC;oc++){
100
+ let outChan=zeros(sample[0].length,sample[0][0].length);
101
+ for(let ic=0;ic<this.inC;ic++){
102
+ let inputChan=sample[ic];
103
+ if(this.padding>0) inputChan=this.pad2D([inputChan],this.padding)[0];
104
+ const conv=this.conv2DSingle(inputChan,this.W[oc][ic]);
105
+ outChan=addMatrices(outChan,conv);
106
+ }
107
+ channelsOut.push(outChan);
108
+ }
109
+ return channelsOut;
110
+ });
111
+ }
112
+
113
+ backward(grad) {
114
+ const batchSize = this.x.length;
115
+ const gradInput = this.x.map(sample => sample.map(chan => zeros(chan.length, chan[0].length)));
116
+ const gradW = this.W.map(oc => oc.map(ic => zeros(this.kernel,this.kernel)));
117
+
118
+ for (let b = 0; b < batchSize; b++) {
119
+ const xPadded = this.pad2D(this.x[b], this.padding);
120
+ const gradInputPadded = xPadded.map(chan => zeros(chan.length, chan[0].length));
121
+
122
+ for (let oc = 0; oc < this.outC; oc++) {
123
+ for (let ic = 0; ic < this.inC; ic++) {
124
+ const outGrad = grad[b][oc];
125
+ const inChan = xPadded[ic];
126
+
127
+ // Compute gradW
128
+ for (let i = 0; i < this.kernel; i++) {
129
+ for (let j = 0; j < this.kernel; j++) {
130
+ let sum = 0;
131
+ for (let y = 0; y < outGrad.length; y++) {
132
+ for (let x = 0; x < outGrad[0].length; x++) {
133
+ const inY = y * this.stride + i;
134
+ const inX = x * this.stride + j;
135
+ if (inY < inChan.length && inX < inChan[0].length) {
136
+ sum += inChan[inY][inX] * outGrad[y][x];
137
+ }
138
+ }
139
+ }
140
+ gradW[oc][ic][i][j] += sum;
141
+ }
142
+ }
143
+
144
+ // Compute gradInput
145
+ const flippedKernel = this.W[oc][ic].map(row => [...row].reverse()).reverse();
146
+ for (let y = 0; y < outGrad.length; y++) {
147
+ for (let x = 0; x < outGrad[0].length; x++) {
148
+ for (let i = 0; i < this.kernel; i++) {
149
+ for (let j = 0; j < this.kernel; j++) {
150
+ const inY = y * this.stride + i;
151
+ const inX = x * this.stride + j;
152
+ if (inY < gradInputPadded[ic].length && inX < gradInputPadded[ic][0].length) {
153
+ gradInputPadded[ic][inY][inX] += flippedKernel[i][j] * outGrad[y][x];
154
+ }
155
+ }
156
+ }
157
+ }
158
+ }
159
+ }
160
+ }
161
+
162
+ // Remove padding from gradInput
163
+ if (this.padding > 0) {
164
+ for (let ic = 0; ic < this.inC; ic++) {
165
+ const padded = gradInputPadded[ic];
166
+ const cropped = padded.slice(this.padding, padded.length - this.padding)
167
+ .map(row => row.slice(this.padding, row.length - this.padding));
168
+ gradInput[b][ic] = cropped;
169
+ }
170
+ } else {
171
+ for (let ic = 0; ic < this.inC; ic++) gradInput[b][ic] = gradInputPadded[ic];
172
+ }
173
+ }
174
+
175
+ this.gradW = gradW;
176
+ return gradInput;
177
+ }
178
+
179
+ parameters(){ return this.W.flatMap((w,oc)=>w.map((wc,ic)=>({param:wc,grad:this.gradW[oc][ic]}))); }
180
+ }
181
+
182
+ // ---------------------- Sequential ----------------------
183
+ export class Sequential {
184
+ constructor(layers=[]){ this.layers=layers; }
185
+ forward(x){ return this.layers.reduce((acc,l)=>l.forward(acc), x); }
186
+ backward(grad){ return this.layers.reduceRight((g,l)=>l.backward(g), grad); }
187
+ parameters(){ return this.layers.flatMap(l=>l.parameters?l.parameters():[]); }
188
+ }
189
+
190
+ // ---------------------- Activations ----------------------
191
+ export class ReLU{ constructor(){ this.out=null; } forward(x){ this.out=x.map(r=>r.map(v=>Math.max(0,v))); return this.out; } backward(grad){ return grad.map((r,i)=>r.map((v,j)=>v*(this.out[i][j]>0?1:0))); } }
192
+ export class Sigmoid{ constructor(){ this.out=null; } forward(x){ const fn=v=>1/(1+Math.exp(-v)); this.out=x.map(r=>r.map(fn)); return this.out; } backward(grad){ return grad.map((r,i)=>r.map((v,j)=>v*this.out[i][j]*(1-this.out[i][j]))); } }
193
+ export class Tanh{ constructor(){ this.out=null; } forward(x){ this.out=x.map(r=>r.map(v=>Math.tanh(v))); return this.out; } backward(grad){ return grad.map((r,i)=>r.map((v,j)=>v*(1-this.out[i][j]**2))); } }
194
+ export class LeakyReLU{ constructor(alpha=0.01){ this.alpha=alpha; this.out=null; } forward(x){ this.out=x.map(r=>r.map(v=>v>0?v:v*this.alpha)); return this.out; } backward(grad){ return grad.map((r,i)=>r.map((v,j)=>v*(this.out[i][j]>0?1:this.alpha))); } }
195
+ export class GELU{ constructor(){ this.out=null; } forward(x){ const fn=v=>0.5*v*(1+Math.tanh(Math.sqrt(2/Math.PI)*(v+0.044715*v**3))); this.out=x.map(r=>r.map(fn)); return this.out; } backward(grad){ return grad.map((r,i)=>r.map(v=>v*1)); } }
196
+
197
+ // ---------------------- Dropout ----------------------
198
+ export class Dropout{ constructor(p=0.5){ this.p=p; } forward(x){ return x.map(r=>r.map(v=>v*Math.random()>=this.p?v:0)); } backward(grad){ return grad.map(r=>r.map(v=>v*(1-this.p))); } }
199
+
200
+ // ---------------------- Losses ----------------------
201
+ export class MSELoss{ forward(pred,target){ this.pred=pred; this.target=target; const losses=pred.map((row,i)=>row.reduce((sum,v,j)=>sum+(v-target[i][j])**2,0)/row.length); return losses.reduce((a,b)=>a+b,0)/pred.length; } backward(){ return this.pred.map((row,i)=>row.map((v,j)=>2*(v-this.target[i][j])/row.length)); } }
202
+ export class CrossEntropyLoss{ forward(pred,target){ this.pred=pred; this.target=target; const losses=pred.map((p,i)=>crossEntropy(softmax(p),target[i])); return losses.reduce((a,b)=>a+b,0)/pred.length; } backward(){ return this.pred.map((p,i)=>{ const s=softmax(p); return s.map((v,j)=>(v-this.target[i][j])/this.pred.length); }); } }
203
+
204
+ // ---------------------- Optimizers ----------------------
205
+ export class Adam{
206
+ constructor(params,lr=0.001,b1=0.9,b2=0.999,eps=1e-8){
207
+ this.params=params; this.lr=lr; this.beta1=b1; this.beta2=b2; this.eps=eps;
208
+ this.m=params.map(p=>zeros(p.param.length,p.param[0].length||1));
209
+ this.v=params.map(p=>zeros(p.param.length,p.param[0].length||1));
210
+ this.t=0;
211
+ }
212
+ step(){
213
+ this.t++;
214
+ this.params.forEach((p,idx)=>{
215
+ for(let i=0;i<p.param.length;i++)
216
+ for(let j=0;j<(p.param[0].length||1);j++){
217
+ const g=p.grad[i][j];
218
+ this.m[idx][i][j]=this.beta1*this.m[idx][i][j]+(1-this.beta1)*g;
219
+ this.v[idx][i][j]=this.beta2*this.v[idx][i][j]+(1-this.beta2)*g*g;
220
+ const mHat=this.m[idx][i][j]/(1-Math.pow(this.beta1,this.t));
221
+ const vHat=this.v[idx][i][j]/(1-Math.pow(this.beta2,this.t));
222
+ p.param[i][j]-=this.lr*mHat/(Math.sqrt(vHat)+this.eps);
223
+ }
224
+ });
225
+ }
226
+ }
227
+
228
+ // ---------------------- ELU Activation ----------------------
229
+ export class ELU {
230
+ constructor(alpha=1.0) {
231
+ this.alpha = alpha;
232
+ this.out = null;
233
+ }
234
+
235
+ forward(x) {
236
+ this.out = x.map(row =>
237
+ row.map(v => v > 0 ? v : this.alpha * (Math.exp(v) - 1))
238
+ );
239
+ return this.out;
240
+ }
241
+
242
+ backward(grad) {
243
+ return grad.map((row, i) =>
244
+ row.map((v, j) =>
245
+ v * (this.out[i][j] > 0 ? 1 : this.alpha * Math.exp(this.out[i][j]))
246
+ )
247
+ );
248
+ }
249
+ }
250
+
251
+ // ---------------------- Mish Activation ----------------------
252
+ export class Mish {
253
+ constructor() {
254
+ this.x = null;
255
+ }
256
+
257
+ forward(x) {
258
+ this.x = x;
259
+ return x.map(row =>
260
+ row.map(v => {
261
+ // Mish(x) = x * tanh(softplus(x)) = x * tanh(ln(1 + e^x))
262
+ const softplus = Math.log(1 + Math.exp(v));
263
+ return v * Math.tanh(softplus);
264
+ })
265
+ );
266
+ }
267
+
268
+ backward(grad) {
269
+ return grad.map((row, i) =>
270
+ row.map((v, j) => {
271
+ const x_val = this.x[i][j];
272
+
273
+ // Gradient of Mish:
274
+ // δ = ω * (4(x+1) + 4e^2x + e^3x + e^x(4x+6)) / (2e^x + e^2x + 2)^2
275
+ // where ω = sech^2(softplus(x))
276
+
277
+ const exp_x = Math.exp(x_val);
278
+ const exp_2x = Math.exp(2 * x_val);
279
+ const exp_3x = Math.exp(3 * x_val);
280
+ const softplus = Math.log(1 + exp_x);
281
+
282
+ const sech_softplus = 1 / Math.cosh(softplus);
283
+ const numerator = 4 * (x_val + 1) + 4 * exp_2x + exp_3x + exp_x * (4 * x_val + 6);
284
+ const denominator = Math.pow(2 * exp_x + exp_2x + 2, 2);
285
+
286
+ const mish_grad = (sech_softplus * sech_softplus) * (numerator / denominator);
287
+ return v * mish_grad;
288
+ })
289
+ );
290
+ }
291
+ }
292
+
293
+ // ---------------------- SiLU Activation ----------------------
294
+ export class SiLU {
295
+ constructor() {
296
+ this.x = null;
297
+ }
298
+
299
+ forward(x) {
300
+ this.x = x;
301
+ return x.map(row =>
302
+ row.map(v => v / (1 + Math.exp(-v))) // x * sigmoid(x)
303
+ );
304
+ }
305
+
306
+ backward(grad) {
307
+ return grad.map((row, i) =>
308
+ row.map((v, j) => {
309
+ const x_val = this.x[i][j];
310
+ const sigmoid = 1 / (1 + Math.exp(-x_val));
311
+ return v * (sigmoid * (1 + x_val * (1 - sigmoid)));
312
+ })
313
+ );
314
+ }
315
+ }
316
+
317
+ export class SGD{ constructor(params,lr=0.01){ this.params=params; this.lr=lr; } step(){ this.params.forEach(p=>{ for(let i=0;i<p.param.length;i++) for(let j=0;j<(p.param[0].length||1);j++) p.param[i][j]-=this.lr*p.grad[i][j]; }); } }
318
+
319
+ // ---------------------- BatchNorm2D ----------------------
320
+ export class BatchNorm2d {
321
+ constructor(numFeatures, eps=1e-5, momentum=0.1, affine=true) {
322
+ this.numFeatures = numFeatures;
323
+ this.eps = eps;
324
+ this.momentum = momentum;
325
+ this.affine = affine;
326
+
327
+ // Parameters
328
+ if (affine) {
329
+ this.weight = Array(numFeatures).fill(1);
330
+ this.bias = Array(numFeatures).fill(0);
331
+ this.gradWeight = Array(numFeatures).fill(0);
332
+ this.gradBias = Array(numFeatures).fill(0);
333
+ }
334
+
335
+ // Running statistics
336
+ this.runningMean = Array(numFeatures).fill(0);
337
+ this.runningVar = Array(numFeatures).fill(1);
338
+
339
+ // Training state
340
+ this.training = true;
341
+ this.x = null;
342
+ this.xCentered = null;
343
+ this.std = null;
344
+ }
345
+
346
+ forward(x) {
347
+ // x shape: [batch, channels, height, width]
348
+ this.x = x;
349
+ const batchSize = x.length;
350
+ const channels = x[0].length;
351
+
352
+ if (this.training) {
353
+ // Calculate mean per channel
354
+ const means = Array(channels).fill(0);
355
+ for (let b = 0; b < batchSize; b++) {
356
+ for (let c = 0; c < channels; c++) {
357
+ const channelData = x[b][c];
358
+ let sum = 0;
359
+ for (let i = 0; i < channelData.length; i++) {
360
+ for (let j = 0; j < channelData[0].length; j++) {
361
+ sum += channelData[i][j];
362
+ }
363
+ }
364
+ means[c] += sum / (channelData.length * channelData[0].length);
365
+ }
366
+ }
367
+ means.forEach((_, c) => means[c] /= batchSize);
368
+
369
+ // Calculate variance per channel
370
+ const variances = Array(channels).fill(0);
371
+ for (let b = 0; b < batchSize; b++) {
372
+ for (let c = 0; c < channels; c++) {
373
+ const channelData = x[b][c];
374
+ let sum = 0;
375
+ for (let i = 0; i < channelData.length; i++) {
376
+ for (let j = 0; j < channelData[0].length; j++) {
377
+ sum += Math.pow(channelData[i][j] - means[c], 2);
378
+ }
379
+ }
380
+ variances[c] += sum / (channelData.length * channelData[0].length);
381
+ }
382
+ }
383
+ variances.forEach((_, c) => variances[c] /= batchSize);
384
+
385
+ // Update running statistics
386
+ for (let c = 0; c < channels; c++) {
387
+ this.runningMean[c] = this.momentum * means[c] + (1 - this.momentum) * this.runningMean[c];
388
+ this.runningVar[c] = this.momentum * variances[c] + (1 - this.momentum) * this.runningVar[c];
389
+ }
390
+
391
+ // Normalize
392
+ this.xCentered = [];
393
+ this.std = Array(channels).fill(0).map(() => []);
394
+
395
+ const output = [];
396
+ for (let b = 0; b < batchSize; b++) {
397
+ const batchOut = [];
398
+ for (let c = 0; c < channels; c++) {
399
+ const channelData = x[b][c];
400
+ const channelOut = zeros(channelData.length, channelData[0].length);
401
+ const channelCentered = zeros(channelData.length, channelData[0].length);
402
+ const channelStd = Math.sqrt(variances[c] + this.eps);
403
+ this.std[c].push(channelStd);
404
+
405
+ for (let i = 0; i < channelData.length; i++) {
406
+ for (let j = 0; j < channelData[0].length; j++) {
407
+ channelCentered[i][j] = channelData[i][j] - means[c];
408
+ channelOut[i][j] = channelCentered[i][j] / channelStd;
409
+
410
+ // Apply affine transformation if enabled
411
+ if (this.affine) {
412
+ channelOut[i][j] = channelOut[i][j] * this.weight[c] + this.bias[c];
413
+ }
414
+ }
415
+ }
416
+
417
+ batchOut.push(channelOut);
418
+ if (b === 0) this.xCentered.push(channelCentered);
419
+ else this.xCentered[c] = addMatrices(this.xCentered[c], channelCentered);
420
+ }
421
+ output.push(batchOut);
422
+ }
423
+
424
+ return output;
425
+ } else {
426
+ // Inference mode - use running statistics
427
+ const output = [];
428
+ for (let b = 0; b < batchSize; b++) {
429
+ const batchOut = [];
430
+ for (let c = 0; c < channels; c++) {
431
+ const channelData = x[b][c];
432
+ const channelOut = zeros(channelData.length, channelData[0].length);
433
+ const channelStd = Math.sqrt(this.runningVar[c] + this.eps);
434
+
435
+ for (let i = 0; i < channelData.length; i++) {
436
+ for (let j = 0; j < channelData[0].length; j++) {
437
+ channelOut[i][j] = (channelData[i][j] - this.runningMean[c]) / channelStd;
438
+
439
+ // Apply affine transformation if enabled
440
+ if (this.affine) {
441
+ channelOut[i][j] = channelOut[i][j] * this.weight[c] + this.bias[c];
442
+ }
443
+ }
444
+ }
445
+
446
+ batchOut.push(channelOut);
447
+ }
448
+ output.push(batchOut);
449
+ }
450
+
451
+ return output;
452
+ }
453
+ }
454
+
455
+ backward(gradOutput) {
456
+ if (!this.training) {
457
+ throw new Error("Backward should only be called in training mode");
458
+ }
459
+
460
+ const batchSize = gradOutput.length;
461
+ const channels = gradOutput[0].length;
462
+
463
+ // Initialize gradients
464
+ const gradInput = this.x.map(batch =>
465
+ batch.map(channel =>
466
+ zeros(channel.length, channel[0].length)
467
+ )
468
+ );
469
+
470
+ if (this.affine) {
471
+ this.gradWeight.fill(0);
472
+ this.gradBias.fill(0);
473
+ }
474
+
475
+ for (let c = 0; c < channels; c++) {
476
+ let sumGradWeight = 0;
477
+ let sumGradBias = 0;
478
+
479
+ for (let b = 0; b < batchSize; b++) {
480
+ const channelGrad = gradOutput[b][c];
481
+ const channelData = this.x[b][c];
482
+
483
+ // Calculate gradients for bias and weight
484
+ if (this.affine) {
485
+ for (let i = 0; i < channelGrad.length; i++) {
486
+ for (let j = 0; j < channelGrad[0].length; j++) {
487
+ sumGradBias += channelGrad[i][j];
488
+ sumGradWeight += channelGrad[i][j] * (this.xCentered[c][i][j] / this.std[c][b]);
489
+ }
490
+ }
491
+ }
492
+
493
+ // Calculate gradient for input
494
+ const n = channelData.length * channelData[0].length;
495
+ const stdInv = 1 / this.std[c][b];
496
+
497
+ for (let i = 0; i < channelGrad.length; i++) {
498
+ for (let j = 0; j < channelGrad[0].length; j++) {
499
+ let grad = channelGrad[i][j];
500
+
501
+ if (this.affine) {
502
+ grad *= this.weight[c];
503
+ }
504
+
505
+ grad *= stdInv;
506
+ gradInput[b][c][i][j] = grad;
507
+ }
508
+ }
509
+ }
510
+
511
+ if (this.affine) {
512
+ this.gradWeight[c] = sumGradWeight / batchSize;
513
+ this.gradBias[c] = sumGradBias / batchSize;
514
+ }
515
+ }
516
+
517
+ return gradInput;
518
+ }
519
+
520
+ parameters() {
521
+ if (!this.affine) return [];
522
+ return [
523
+ { param: [this.weight], grad: [this.gradWeight] },
524
+ { param: [this.bias], grad: [this.gradBias] }
525
+ ];
526
+ }
527
+
528
+ train() { this.training = true; }
529
+ eval() { this.training = false; }
530
+ }
531
+
532
+ // ---------------------- Model Save/Load ----------------------
533
+ export function saveModel(model){
534
+ if(!(model instanceof Sequential)) throw new Error("saveModel supports only Sequential");
535
+ const weights=model.layers.map(layer=>({weights:layer.W||null,biases:layer.b||null}));
536
+ return JSON.stringify(weights);
537
+ }
538
+
539
+ export function loadModel(model,json){
540
+ if(!(model instanceof Sequential)) throw new Error("loadModel supports only Sequential");
541
+ const weights=JSON.parse(json);
542
+ model.layers.forEach((layer,i)=>{
543
+ if(layer.W && weights[i].weights) layer.W=weights[i].weights;
544
+ if(layer.b && weights[i].biases) layer.b=weights[i].biases;
545
+ });
546
+ }
547
+
548
+ // ---------------------- Advanced Utils ----------------------
549
+ export function flattenBatch(batch){ return batch.flat(2); }
550
+ export function stack(tensors){ return tensors.map(t=>t.data); }
551
+ export function eye(n){ return Array.from({length:n},(_,i)=>Array.from({length:n},(_,j)=>i===j?1:0)); }
552
+ export function concat(a,b,axis=0){ /* concat along axis */ if(axis===0) return [...a,...b]; if(axis===1) return a.map((row,i)=>[...row,...b[i]]); }
553
+ export function reshape(tensor, rows, cols) {
554
+ let flat = tensor.data.flat(); // flatten dulu
555
+ if(flat.length < rows*cols) throw new Error("reshape size mismatch");
556
+ const out = Array.from({length: rows}, (_, i) =>
557
+ flat.slice(i*cols, i*cols + cols)
558
+ );
559
+ return out;
560
+ }
package/src/startup.cpu CHANGED
@@ -1,12 +1,15 @@
1
1
  // you can delete this files this files are not important for the engine runtime.
2
2
 
3
3
  e=run=[cpu[runtime]]
4
- e.set.runtime('beta')
5
- e.rnt()
6
- e.set()
7
- e.register('vanilla',expe='Experiments.js',main='MainEngine.js')
8
- l=e.prog('asm')
4
+ devices=e.getdata[devices[5]]
5
+ env.set.runtime('beta')
6
+ env.rnt()
7
+ env.set()
8
+ env.register('vanilla',expe='Experiments.js',main='MainEngine.js',tgver=latest)
9
+ resources=e.find(tag='resources')
10
+ resources.ld(env)
11
+ l=env.prog('asm')
9
12
  r=l.gv=[0xCAFEBABE]
10
- eng=e.load(register,r,'vanilla')
11
- eng.boot(e,r,'dp')
12
- eng.load()
13
+ eng=env.load(register,r,'vanilla')
14
+ eng.boot(env,r,'dp')
15
+ eng.load(resources,runtime,devices)