mycai 1.0.0 → 1.0.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/core/spectral_decoder.js +1 -1
- package/core/weight_analyzer.js +164 -0
- package/index.js +4 -3
- package/package.json +1 -1
package/core/spectral_decoder.js
CHANGED
|
@@ -11,7 +11,7 @@ import { fileURLToPath } from 'url';
|
|
|
11
11
|
import { Representation, HDCEngine } from './hdc.js';
|
|
12
12
|
import { TurkishMorphology } from './morphology.js';
|
|
13
13
|
import { SafetensorsParser } from './safetensors.js';
|
|
14
|
-
import { WeightSpectralAnalyzer } from '
|
|
14
|
+
import { WeightSpectralAnalyzer } from './weight_analyzer.js';
|
|
15
15
|
|
|
16
16
|
const __filename = fileURLToPath(import.meta.url);
|
|
17
17
|
const __dirname = path.dirname(__filename);
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Weight Spectral Analyzer Module
|
|
3
|
+
* Analyzes deep learning model weights using spectral methods (FFT/IFFT) in a memory-efficient, block-wise manner.
|
|
4
|
+
* Supports compressing weights, reconstructing weights, and measuring error matrices.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { SpectralEngine } from './spectral.js';
|
|
8
|
+
import { Representation } from './hdc.js';
|
|
9
|
+
|
|
10
|
+
export class WeightSpectralAnalyzer {
|
|
11
|
+
constructor(blockSize = 4096) {
|
|
12
|
+
this.blockSize = blockSize;
|
|
13
|
+
this.spectralEngine = new SpectralEngine();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// ── ANALYSIS OPERATIONS ────────────────────────────────────
|
|
17
|
+
|
|
18
|
+
analyzeBlock(weightsBlock) {
|
|
19
|
+
// Ensure size fits block size by padding with zeros if necessary
|
|
20
|
+
const padded = new Float32Array(this.blockSize);
|
|
21
|
+
padded.set(weightsBlock.slice(0, this.blockSize));
|
|
22
|
+
|
|
23
|
+
// Construct a representation object to feed the spectral engine
|
|
24
|
+
const representation = new Representation('real', padded, this.blockSize);
|
|
25
|
+
const spectrum = this.spectralEngine.spectralTransform(representation);
|
|
26
|
+
|
|
27
|
+
const energy = this.spectralEngine.spectralEnergy(spectrum);
|
|
28
|
+
const entropy = this.spectralEngine.spectralEntropy(spectrum);
|
|
29
|
+
const sparsity = this.spectralEngine.spectralSparsity(spectrum);
|
|
30
|
+
const dominant = this.spectralEngine.extractDominantFrequencies(spectrum, 10);
|
|
31
|
+
|
|
32
|
+
return {
|
|
33
|
+
spectrum,
|
|
34
|
+
metrics: {
|
|
35
|
+
energy,
|
|
36
|
+
entropy,
|
|
37
|
+
sparsity,
|
|
38
|
+
dominantFrequencies: dominant
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Stream-based model weight analyzer to support 1T parameters
|
|
44
|
+
// Executes block-wise computation on a generator or array of weights
|
|
45
|
+
// Includes strict pre-emptive OOM protection guards
|
|
46
|
+
async analyzeStream(weightGenerator) {
|
|
47
|
+
let totalEnergy = 0;
|
|
48
|
+
let blockCount = 0;
|
|
49
|
+
let cumulativeEntropy = 0;
|
|
50
|
+
let cumulativeSparsity = 0;
|
|
51
|
+
|
|
52
|
+
// Track dominant frequencies globally
|
|
53
|
+
const freqAccumulator = {};
|
|
54
|
+
|
|
55
|
+
// Get current engine configuration limits
|
|
56
|
+
const maxAllowedHeapMb = 1000; // Keep node process memory low (1 GB safety threshold)
|
|
57
|
+
|
|
58
|
+
for await (const block of weightGenerator) {
|
|
59
|
+
// ── OOM SAFETY GUARD ──
|
|
60
|
+
const currentHeapMb = process.memoryUsage().heapUsed / 1024 / 1024;
|
|
61
|
+
if (currentHeapMb > maxAllowedHeapMb) {
|
|
62
|
+
throw new Error(`[OOM GUARD TRIGGERED] Memory safety ceiling reached: ${currentHeapMb.toFixed(2)} MB used. Halting stream ingestion to prevent process crash.`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const res = this.analyzeBlock(block);
|
|
66
|
+
totalEnergy += res.metrics.energy;
|
|
67
|
+
cumulativeEntropy += res.metrics.entropy;
|
|
68
|
+
cumulativeSparsity += res.metrics.sparsity;
|
|
69
|
+
blockCount++;
|
|
70
|
+
|
|
71
|
+
// Accumulate dominant frequencies
|
|
72
|
+
for (const dom of res.metrics.dominantFrequencies) {
|
|
73
|
+
freqAccumulator[dom.frequency] = (freqAccumulator[dom.frequency] || 0) + dom.magnitude;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (blockCount === 0) {
|
|
78
|
+
return {
|
|
79
|
+
totalBlocks: 0,
|
|
80
|
+
averageEnergy: 0,
|
|
81
|
+
averageEntropy: 0,
|
|
82
|
+
averageSparsity: 0,
|
|
83
|
+
globalDominantFrequencies: []
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Sort global dominant frequencies
|
|
88
|
+
const globalDominant = Object.entries(freqAccumulator)
|
|
89
|
+
.map(([frequency, magnitude]) => ({ frequency: parseInt(frequency), magnitude }))
|
|
90
|
+
.sort((a, b) => b.magnitude - a.magnitude)
|
|
91
|
+
.slice(0, 10);
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
totalBlocks: blockCount,
|
|
95
|
+
averageEnergy: totalEnergy / blockCount,
|
|
96
|
+
averageEntropy: cumulativeEntropy / blockCount,
|
|
97
|
+
averageSparsity: cumulativeSparsity / blockCount,
|
|
98
|
+
globalDominantFrequencies: globalDominant
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// ── COMPRESSION / RECONSTRUCTION ────────────────────────────
|
|
103
|
+
|
|
104
|
+
compressBlock(weightsBlock, threshold = 0.05) {
|
|
105
|
+
const analysis = this.analyzeBlock(weightsBlock);
|
|
106
|
+
const spectrum = analysis.spectrum;
|
|
107
|
+
const D = spectrum.length;
|
|
108
|
+
|
|
109
|
+
// Zero out components with magnitude below threshold ratio of max magnitude
|
|
110
|
+
let maxMag = 0;
|
|
111
|
+
for (let i = 0; i < D; i++) {
|
|
112
|
+
const mag = Math.sqrt(spectrum[i].real ** 2 + spectrum[i].imag ** 2);
|
|
113
|
+
if (mag > maxMag) maxMag = mag;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const cutoff = maxMag * threshold;
|
|
117
|
+
const sparseSpectrum = new Array(D);
|
|
118
|
+
let retainedCount = 0;
|
|
119
|
+
|
|
120
|
+
for (let i = 0; i < D; i++) {
|
|
121
|
+
const mag = Math.sqrt(spectrum[i].real ** 2 + spectrum[i].imag ** 2);
|
|
122
|
+
if (mag >= cutoff) {
|
|
123
|
+
sparseSpectrum[i] = spectrum[i];
|
|
124
|
+
retainedCount++;
|
|
125
|
+
} else {
|
|
126
|
+
sparseSpectrum[i] = { real: 0, imag: 0 };
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
sparseSpectrum,
|
|
132
|
+
compressionRatio: D / (retainedCount || 1),
|
|
133
|
+
retainedFrequencies: retainedCount
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
reconstructBlock(sparseSpectrum) {
|
|
138
|
+
return this.spectralEngine.inverseSpectralTransform(sparseSpectrum);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// ── ERROR CALCULATION ───────────────────────────────────────
|
|
142
|
+
|
|
143
|
+
computeReconstructionError(originalBlock, reconstructedBlock) {
|
|
144
|
+
const D = Math.min(originalBlock.length, reconstructedBlock.length);
|
|
145
|
+
let sumSquaredError = 0;
|
|
146
|
+
let sumOrigSquared = 0;
|
|
147
|
+
|
|
148
|
+
for (let i = 0; i < D; i++) {
|
|
149
|
+
const diff = originalBlock[i] - reconstructedBlock[i];
|
|
150
|
+
sumSquaredError += diff * diff;
|
|
151
|
+
sumOrigSquared += originalBlock[i] * originalBlock[i];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const mse = sumSquaredError / D;
|
|
155
|
+
const rmse = Math.sqrt(mse);
|
|
156
|
+
const snr = sumOrigSquared > 0 ? 10 * Math.log10(sumOrigSquared / (sumSquaredError + 1e-12)) : 0;
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
mse,
|
|
160
|
+
rmse,
|
|
161
|
+
snrDb: snr
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
}
|
package/index.js
CHANGED
|
@@ -11,15 +11,16 @@
|
|
|
11
11
|
export { TurkishMorphology } from './core/morphology.js';
|
|
12
12
|
export { GHRLatticeMemory } from './core/ghr_lattice_memory.js';
|
|
13
13
|
export { HDCEngine, Representation } from './core/hdc.js';
|
|
14
|
-
export { SpectralDecoder } from './core/spectral_decoder.js';
|
|
14
|
+
export { SpectralSLM, SpectralSLM as SpectralDecoder } from './core/spectral_decoder.js';
|
|
15
|
+
export { WeightSpectralAnalyzer } from './core/weight_analyzer.js';
|
|
15
16
|
export { ReasoningRouter } from './core/reasoning_router.js';
|
|
16
17
|
export { SovereignAgent } from './core/sovereign_agent.js';
|
|
17
18
|
|
|
18
19
|
export { ResonanceEngine } from './sdk/resonance_engine.js';
|
|
19
20
|
export { ResonanceSDK } from './sdk/resonance_sdk.js';
|
|
20
21
|
export { LicenseManager, LICENSE_TIERS } from './sdk/licensing.js';
|
|
21
|
-
export { IngestionPipeline } from './sdk/ingestion.js';
|
|
22
|
-
export {
|
|
22
|
+
export { IngestionEngine, IngestionEngine as IngestionPipeline } from './sdk/ingestion.js';
|
|
23
|
+
export { BaseStorageAdapter, MemoryStorageAdapter, FileStorageAdapter, IndexedDBStorageAdapter } from './sdk/storage.js';
|
|
23
24
|
|
|
24
25
|
// Default export
|
|
25
26
|
import { ResonanceEngine } from './sdk/resonance_engine.js';
|