@neuralforge/core 0.9.0
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/package.json +19 -0
- package/src/backend/cpu.ts +22 -0
- package/src/backend/index.ts +9 -0
- package/src/backend/wasm.ts +22 -0
- package/src/backend/webgl.ts +22 -0
- package/src/backend/webgpu.ts +22 -0
- package/src/index.ts +8 -0
- package/src/model.ts +28 -0
- package/src/ops/activation.ts +7 -0
- package/src/ops/arithmetic.ts +8 -0
- package/src/ops/conv.ts +6 -0
- package/src/ops/index.ts +9 -0
- package/src/ops/matrix.ts +6 -0
- package/src/session.ts +23 -0
- package/src/tensor.ts +23 -0
- package/src/utils/dtype.ts +7 -0
- package/src/utils/index.ts +7 -0
- package/src/utils/shape.ts +7 -0
- package/tests/model.test.ts +13 -0
- package/tests/ops.test.ts +13 -0
- package/tests/tensor.test.ts +13 -0
- package/tsconfig.json +7 -0
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@neuralforge/core",
|
|
3
|
+
"version": "0.9.0",
|
|
4
|
+
"description": "ML inference SDK for JavaScript",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"test": "vitest",
|
|
10
|
+
"lint": "eslint src/"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"typescript": "^5.4.0"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"vitest": "^1.6.0",
|
|
17
|
+
"eslint": "^8.57.0"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CPU backend
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export interface BackendOptions {
|
|
6
|
+
device?: string;
|
|
7
|
+
precision?: 'float32' | 'float16';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class CPUBackend {
|
|
11
|
+
private options: BackendOptions;
|
|
12
|
+
|
|
13
|
+
constructor(options: BackendOptions = {}) {
|
|
14
|
+
this.options = options;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async initialize(): Promise<void> { /* CPU init */ }
|
|
18
|
+
isSupported(): boolean { return true; }
|
|
19
|
+
getName(): string { return 'CPU'; }
|
|
20
|
+
dispose(): void { /* cleanup */ }
|
|
21
|
+
}
|
|
22
|
+
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WASM backend
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export interface BackendOptions {
|
|
6
|
+
device?: string;
|
|
7
|
+
precision?: 'float32' | 'float16';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class WasmBackend {
|
|
11
|
+
private options: BackendOptions;
|
|
12
|
+
|
|
13
|
+
constructor(options: BackendOptions = {}) {
|
|
14
|
+
this.options = options;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async initialize(): Promise<void> { /* Wasm init */ }
|
|
18
|
+
isSupported(): boolean { return true; }
|
|
19
|
+
getName(): string { return 'Wasm'; }
|
|
20
|
+
dispose(): void { /* cleanup */ }
|
|
21
|
+
}
|
|
22
|
+
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebGL backend
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export interface BackendOptions {
|
|
6
|
+
device?: string;
|
|
7
|
+
precision?: 'float32' | 'float16';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class WebGLBackend {
|
|
11
|
+
private options: BackendOptions;
|
|
12
|
+
|
|
13
|
+
constructor(options: BackendOptions = {}) {
|
|
14
|
+
this.options = options;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async initialize(): Promise<void> { /* WebGL init */ }
|
|
18
|
+
isSupported(): boolean { return true; }
|
|
19
|
+
getName(): string { return 'WebGL'; }
|
|
20
|
+
dispose(): void { /* cleanup */ }
|
|
21
|
+
}
|
|
22
|
+
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebGPU backend
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export interface BackendOptions {
|
|
6
|
+
device?: string;
|
|
7
|
+
precision?: 'float32' | 'float16';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class WebGPUBackend {
|
|
11
|
+
private options: BackendOptions;
|
|
12
|
+
|
|
13
|
+
constructor(options: BackendOptions = {}) {
|
|
14
|
+
this.options = options;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async initialize(): Promise<void> { /* WebGPU init */ }
|
|
18
|
+
isSupported(): boolean { return true; }
|
|
19
|
+
getName(): string { return 'WebGPU'; }
|
|
20
|
+
dispose(): void { /* cleanup */ }
|
|
21
|
+
}
|
|
22
|
+
|
package/src/index.ts
ADDED
package/src/model.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model loading and management
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { Tensor } from './tensor';
|
|
6
|
+
|
|
7
|
+
export interface ModelConfig {
|
|
8
|
+
inputShape: number[];
|
|
9
|
+
outputShape: number[];
|
|
10
|
+
weights?: Record<string, Tensor>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class Model {
|
|
14
|
+
private config: ModelConfig;
|
|
15
|
+
private weights: Map<string, Tensor> = new Map();
|
|
16
|
+
|
|
17
|
+
constructor(config: ModelConfig) {
|
|
18
|
+
this.config = config;
|
|
19
|
+
if (config.weights) {
|
|
20
|
+
Object.entries(config.weights).forEach(([k, v]) => this.weights.set(k, v));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
predict(input: Tensor): Tensor { return input; }
|
|
25
|
+
getWeight(name: string): Tensor | undefined { return this.weights.get(name); }
|
|
26
|
+
summary(): string { return JSON.stringify(this.config); }
|
|
27
|
+
}
|
|
28
|
+
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Arithmetic ops
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export function add(a: Float32Array, b: Float32Array): Float32Array { return a; }
|
|
6
|
+
export function mul(a: Float32Array, b: Float32Array): Float32Array { return a; }
|
|
7
|
+
export function sub(a: Float32Array, b: Float32Array): Float32Array { return a; }
|
|
8
|
+
|
package/src/ops/conv.ts
ADDED
package/src/ops/index.ts
ADDED
package/src/session.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inference session
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { Model, ModelConfig } from './model';
|
|
6
|
+
import { Tensor } from './tensor';
|
|
7
|
+
|
|
8
|
+
export class InferenceSession {
|
|
9
|
+
private model: Model;
|
|
10
|
+
private initialized = false;
|
|
11
|
+
|
|
12
|
+
constructor(config: ModelConfig) {
|
|
13
|
+
this.model = new Model(config);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async initialize(): Promise<void> { this.initialized = true; }
|
|
17
|
+
run(inputs: Record<string, Tensor>): Record<string, Tensor> {
|
|
18
|
+
if (!this.initialized) throw new Error('Session not initialized');
|
|
19
|
+
return { output: this.model.predict(Object.values(inputs)[0]) };
|
|
20
|
+
}
|
|
21
|
+
dispose(): void { this.initialized = false; }
|
|
22
|
+
}
|
|
23
|
+
|
package/src/tensor.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tensor implementation
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export type DType = 'float32' | 'float16' | 'int32';
|
|
6
|
+
|
|
7
|
+
export class Tensor {
|
|
8
|
+
readonly data: Float32Array;
|
|
9
|
+
readonly shape: number[];
|
|
10
|
+
readonly dtype: DType;
|
|
11
|
+
|
|
12
|
+
constructor(data: Float32Array | number[], shape?: number[], dtype: DType = 'float32') {
|
|
13
|
+
this.data = data instanceof Float32Array ? data : new Float32Array(data);
|
|
14
|
+
this.shape = shape || [this.data.length];
|
|
15
|
+
this.dtype = dtype;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
get size(): number { return this.data.length; }
|
|
19
|
+
reshape(newShape: number[]): Tensor { return new Tensor(this.data, newShape, this.dtype); }
|
|
20
|
+
slice(start: number, end: number): Tensor { return new Tensor(this.data.slice(start, end)); }
|
|
21
|
+
clone(): Tensor { return new Tensor(new Float32Array(this.data), [...this.shape], this.dtype); }
|
|
22
|
+
}
|
|
23
|
+
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { Model } from '../src';
|
|
3
|
+
|
|
4
|
+
describe('Model', () => {
|
|
5
|
+
it('should be defined', () => {
|
|
6
|
+
expect(Model).toBeDefined();
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it('should initialize correctly', () => {
|
|
10
|
+
const instance = new Model();
|
|
11
|
+
expect(instance).toBeInstanceOf(Model);
|
|
12
|
+
});
|
|
13
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { add } from '../src';
|
|
3
|
+
|
|
4
|
+
describe('add', () => {
|
|
5
|
+
it('should be defined', () => {
|
|
6
|
+
expect(add).toBeDefined();
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it('should initialize correctly', () => {
|
|
10
|
+
const instance = new add();
|
|
11
|
+
expect(instance).toBeInstanceOf(add);
|
|
12
|
+
});
|
|
13
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { Tensor } from '../src';
|
|
3
|
+
|
|
4
|
+
describe('Tensor', () => {
|
|
5
|
+
it('should be defined', () => {
|
|
6
|
+
expect(Tensor).toBeDefined();
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it('should initialize correctly', () => {
|
|
10
|
+
const instance = new Tensor();
|
|
11
|
+
expect(instance).toBeInstanceOf(Tensor);
|
|
12
|
+
});
|
|
13
|
+
});
|