UNPKG

neuronetwork

Version:

A hybrid math & neural engine with classic functions and AI computations

490 lines (347 loc) 14.7 kB
const Activation_Module = await import('../wasm/NeuroMath/build/Activation.js'); const LossModule = await import('../wasm/NeuroMath/build/Loss.js'); const LayersModule = await import('../wasm/NeuroMath/build/Layers.js'); const Module = await import('../wasm/NeuroMath/build/Optimizers.js'); //* Activation functions export async function NeuroMath_Activation_ReLu({ input = [], } = {}) { const mod = await Activation_Module.default(); // ده الكائن الحقيقي اللي عليه HEAP والميموري const length = input.length; const inputPtr = mod._my_malloc(length * 4); const outputPtr = mod._my_malloc(length * 4); // 👇 أنشئ HEAP يدويًا const heapF32 = new Float32Array(mod.HEAPU8.buffer); // 👇 اكتب البيانات heapF32.set(input, inputPtr / 4); // 👇 استدعاء الدالة mod._Activation_ReLu(inputPtr, length, outputPtr); // 👇 اقرأ البيانات const output = Array.from(heapF32.slice(outputPtr / 4, outputPtr / 4 + length)); // 👇 Free mod._my_free(inputPtr); mod._my_free(outputPtr); return output; } export async function NeuroMath_Activation_Sigmoid({ input }) { const instance = await Activation_Module.default(); const length = input.length; const inputPtr = instance._my_malloc(length * 4); const outputPtr = instance._my_malloc(length * 4); const heap = new Float32Array(instance.HEAPU8.buffer); heap.set(input, inputPtr / 4); instance._Activation_Sigmoid(inputPtr, length, outputPtr); const result = Array.from(heap.slice(outputPtr / 4, outputPtr / 4 + length)); instance._my_free(inputPtr); instance._my_free(outputPtr); return result; } export async function NeuroMath_Activation_Tanh({ input }) { const instance = await Activation_Module.default(); const length = input.length; const inputPtr = instance._my_malloc(length * 4); const outputPtr = instance._my_malloc(length * 4); const heap = new Float32Array(instance.HEAPU8.buffer); heap.set(input, inputPtr / 4); instance._Activation_Tanh(inputPtr, length, outputPtr); const result = Array.from(heap.slice(outputPtr / 4, outputPtr / 4 + length)); instance._my_free(inputPtr); instance._my_free(outputPtr); return result; } export async function NeuroMath_Activation_Softmax({ input }) { const instance = await Activation_Module.default(); const length = input.length; const inputPtr = instance._my_malloc(length * 4); const outputPtr = instance._my_malloc(length * 4); const heap = new Float32Array(instance.HEAPU8.buffer); heap.set(input, inputPtr / 4); instance._Activation_Softmax(inputPtr, length, outputPtr); const result = Array.from(heap.slice(outputPtr / 4, outputPtr / 4 + length)); instance._my_free(inputPtr); instance._my_free(outputPtr); return result; } export async function NeuroMath_Activation_LeakyReLU({ input }) { const instance = await Activation_Module.default(); const length = input.length; const inputPtr = instance._my_malloc(length * 4); const outputPtr = instance._my_malloc(length * 4); const heap = new Float32Array(instance.HEAPU8.buffer); heap.set(input, inputPtr / 4); instance._Activation_LeakyReLU(inputPtr, length, outputPtr); const result = Array.from(heap.slice(outputPtr / 4, outputPtr / 4 + length)); instance._my_free(inputPtr); instance._my_free(outputPtr); return result; } export async function NeuroMath_Activation_Swish({ input }) { const instance = await Activation_Module.default(); const length = input.length; const inputPtr = instance._my_malloc(length * 4); const outputPtr = instance._my_malloc(length * 4); const heap = new Float32Array(instance.HEAPU8.buffer); heap.set(input, inputPtr / 4); instance._Activation_Swish(inputPtr, length, outputPtr); const result = Array.from(heap.slice(outputPtr / 4, outputPtr / 4 + length)); instance._my_free(inputPtr); instance._my_free(outputPtr); return result; } export async function NeuroMath_Activation_GELU({ input }) { const instance = await Activation_Module.default(); const length = input.length; const inputPtr = instance._my_malloc(length * 4); const outputPtr = instance._my_malloc(length * 4); const heap = new Float32Array(instance.HEAPU8.buffer); heap.set(input, inputPtr / 4); instance._Activation_GELU(inputPtr, length, outputPtr); const result = Array.from(heap.slice(outputPtr / 4, outputPtr / 4 + length)); instance._my_free(inputPtr); instance._my_free(outputPtr); return result; } export async function NeuroMath_Activation_ELU({ input, alpha = 1.0 }) { const instance = await Activation_Module.default(); const length = input.length; const inputPtr = instance._my_malloc(length * 4); const outputPtr = instance._my_malloc(length * 4); const heap = new Float32Array(instance.HEAPU8.buffer); heap.set(input, inputPtr / 4); // ملاحظة: لازم تكون عامل overload للدالة في C++ أو عامل alpha ثابت = 1 instance._Activation_ELU(inputPtr, length, outputPtr, alpha); const result = Array.from(heap.slice(outputPtr / 4, outputPtr / 4 + length)); instance._my_free(inputPtr); instance._my_free(outputPtr); return result; } //* Loss functions export async function NeuroMath_Loss_MSE({ predicted, target }) { const mod = await LossModule.default(); const length = predicted.length; const predictedPtr = mod._my_malloc(length * 4); const targetPtr = mod._my_malloc(length * 4); const heap = new Float32Array(mod.HEAPU8.buffer); heap.set(predicted, predictedPtr / 4); heap.set(target, targetPtr / 4); const loss = mod._Loss_MSE(predictedPtr, targetPtr, length); mod._my_free(predictedPtr); mod._my_free(targetPtr); return loss; } export async function NeuroMath_Loss_BCE({ predicted, actual }) { const mod = await LossModule.default(); const length = predicted.length; const predictedPtr = mod._my_malloc(length * 4); const actualPtr = mod._my_malloc(length * 4); const heap = new Float32Array(mod.HEAPU8.buffer); heap.set(predicted, predictedPtr / 4); heap.set(actual, actualPtr / 4); const loss = mod._Loss_BinaryCrossEntropy(predictedPtr, actualPtr, length); mod._my_free(predictedPtr); mod._my_free(actualPtr); return loss; } export async function NeuroMath_Loss_CCE({ predicted, actual }) { const mod = await LossModule.default(); const length = predicted.length; const predictedPtr = mod._my_malloc(length * 4); const actualPtr = mod._my_malloc(length * 4); const heap = new Float32Array(mod.HEAPU8.buffer); heap.set(predicted, predictedPtr / 4); heap.set(actual, actualPtr / 4); const loss = mod._Loss_CategoricalCrossEntropy(predictedPtr, actualPtr, length); mod._my_free(predictedPtr); mod._my_free(actualPtr); return loss; } export async function NeuroMath_Loss_MAE({ predicted, actual }) { const mod = await LossModule.default(); const length = predicted.length; const predictedPtr = mod._my_malloc(length * 4); const actualPtr = mod._my_malloc(length * 4); const heap = new Float32Array(mod.HEAPU8.buffer); heap.set(predicted, predictedPtr / 4); heap.set(actual, actualPtr / 4); const loss = mod._Loss_MAE(predictedPtr, actualPtr, length); mod._my_free(predictedPtr); mod._my_free(actualPtr); return loss; } //* Layer functions export async function NeuroMath_Layer_Dense({ input = [], weights = [], bias = [] }) { const mod = await LayersModule.default(); const inputSize = input.length; const outputSize = bias.length; const inputPtr = mod._my_malloc(inputSize * 4); const weightsPtr = mod._my_malloc(inputSize * outputSize * 4); const biasPtr = mod._my_malloc(outputSize * 4); const outputPtr = mod._my_malloc(outputSize * 4); const heap = new Float32Array(mod.HEAPU8.buffer); heap.set(input, inputPtr / 4); heap.set(weights, weightsPtr / 4); heap.set(bias, biasPtr / 4); mod._Layer_Dense(inputPtr, weightsPtr, biasPtr, inputSize, outputSize, outputPtr); const output = Array.from(heap.slice(outputPtr / 4, outputPtr / 4 + outputSize)); mod._my_free(inputPtr); mod._my_free(weightsPtr); mod._my_free(biasPtr); mod._my_free(outputPtr); return output; } export async function NeuroMath_Layer_Dropout({ input = [], rate = 0.5, seed = Date.now() }) { const mod = await LayersModule.default(); const length = input.length; const ptr = mod._my_malloc(length * 4); const heap = new Float32Array(mod.HEAPU8.buffer); heap.set(input, ptr / 4); mod._Layer_Dropout(ptr, length, rate, seed); const output = Array.from(heap.slice(ptr / 4, ptr / 4 + length)); mod._my_free(ptr); return output; } export async function NeuroMath_Layer_Flatten({ input = [] }) { const mod = await LayersModule.default(); const length = input.length; const ptr = mod._my_malloc(length * 4); const outPtr = mod._my_malloc(length * 4); const heap = new Float32Array(mod.HEAPU8.buffer); heap.set(input, ptr / 4); mod._Layer_Flatten(ptr, length, outPtr); const output = Array.from(heap.slice(outPtr / 4, outPtr / 4 + length)); mod._my_free(ptr); mod._my_free(outPtr); return output; } export async function NeuroMath_Layer_Conv2D({ input, inputWidth, inputHeight, kernel, kernelSize = 3, stride = 1, padding = 0 }) { const mod = await LayersModule.default(); const inputLength = input.length; const kernelLength = kernel.length; const outputSize = Math.floor((inputWidth - kernelSize + 2 * padding) / stride) + 1; const outputLength = outputSize * outputSize; const inputPtr = mod._my_malloc(inputLength * 4); const kernelPtr = mod._my_malloc(kernelLength * 4); const outputPtr = mod._my_malloc(outputLength * 4); const heap = new Float32Array(mod.HEAPU8.buffer); heap.set(input, inputPtr / 4); heap.set(kernel, kernelPtr / 4); mod._Layer_Conv2D(inputPtr, inputWidth, inputHeight, kernelPtr, kernelSize, stride, padding, outputPtr); const output = Array.from(heap.slice(outputPtr / 4, outputPtr / 4 + outputLength)); mod._my_free(inputPtr); mod._my_free(kernelPtr); mod._my_free(outputPtr); return output; } export async function NeuroMath_Layer_Embedding({ input, embeddingMatrix, vocabSize, embeddingDim }) { const mod = await LayersModule.default(); const inputPtr = mod._my_malloc(input.length * 4); const embPtr = mod._my_malloc(embeddingMatrix.length * 4); const outputPtr = mod._my_malloc(input.length * embeddingDim * 4); const heap = new Float32Array(mod.HEAPU8.buffer); new Int32Array(mod.HEAPU8.buffer).set(input, inputPtr / 4); heap.set(embeddingMatrix, embPtr / 4); mod._Layer_Embedding(inputPtr, input.length, embPtr, vocabSize, embeddingDim, outputPtr); const result = Array.from(heap.slice(outputPtr / 4, outputPtr / 4 + input.length * embeddingDim)); mod._my_free(inputPtr); mod._my_free(embPtr); mod._my_free(outputPtr); return result; } export async function NeuroMath_Layer_BatchNormalization({ input, gamma, beta, epsilon = 1e-5 }) { const mod = await LayersModule.default(); const length = input.length; const inputPtr = mod._my_malloc(length * 4); const gammaPtr = mod._my_malloc(length * 4); const betaPtr = mod._my_malloc(length * 4); const outputPtr = mod._my_malloc(length * 4); const heap = new Float32Array(mod.HEAPU8.buffer); heap.set(input, inputPtr / 4); heap.set(gamma, gammaPtr / 4); heap.set(beta, betaPtr / 4); mod._Layer_BatchNormalization( inputPtr, gammaPtr, betaPtr, length, epsilon, outputPtr ); const result = Array.from(heap.slice(outputPtr / 4, outputPtr / 4 + length)); mod._my_free(inputPtr); mod._my_free(gammaPtr); mod._my_free(betaPtr); mod._my_free(outputPtr); return result; } //* Optimizer functions export async function NeuroMath_Optimizer_SGD({ weights, gradients, learningRate }) { const mod = await Module.default(); const length = weights.length; const weightsPtr = mod._my_malloc(length * 4); const gradientsPtr = mod._my_malloc(length * 4); const heap = new Float32Array(mod.HEAPU8.buffer); heap.set(weights, weightsPtr / 4); heap.set(gradients, gradientsPtr / 4); mod._Optimizer_SGD(weightsPtr, gradientsPtr, length, learningRate); const updated = Array.from(heap.slice(weightsPtr / 4, weightsPtr / 4 + length)); mod._my_free(weightsPtr); mod._my_free(gradientsPtr); return updated; } export async function NeuroMath_Optimizer_Adam({ weights, gradients, m, v, learningRate, beta1, beta2, epsilon, timestep }) { const mod = await Module.default(); const length = weights.length; const heap = new Float32Array(mod.HEAPU8.buffer); const weightsPtr = mod._my_malloc(length * 4); const gradientsPtr = mod._my_malloc(length * 4); const mPtr = mod._my_malloc(length * 4); const vPtr = mod._my_malloc(length * 4); heap.set(weights, weightsPtr / 4); heap.set(gradients, gradientsPtr / 4); heap.set(m, mPtr / 4); heap.set(v, vPtr / 4); mod._Optimizer_Adam( weightsPtr, gradientsPtr, mPtr, vPtr, learningRate, beta1, beta2, epsilon, timestep, length ); const updatedWeights = Array.from(heap.slice(weightsPtr / 4, weightsPtr / 4 + length)); const updatedM = Array.from(heap.slice(mPtr / 4, mPtr / 4 + length)); const updatedV = Array.from(heap.slice(vPtr / 4, vPtr / 4 + length)); mod._my_free(weightsPtr); mod._my_free(gradientsPtr); mod._my_free(mPtr); mod._my_free(vPtr); return { weights: updatedWeights, m: updatedM, v: updatedV }; }