@hoff97/tensor-js
Version:
PyTorch like deep learning inferrence library
38 lines • 1.63 kB
JavaScript
import { CPUTensor } from '../../../../tensor/cpu/tensor';
import { SparseTensor } from '../../../../tensor/sparse/tensor';
import { WASMTensor } from '../../../../tensor/wasm/tensor';
import { addDenseCPU, addSparseCPU } from './cpu';
import { addDenseWASM, addSparseWASM } from './wasm';
export function add(a, b, resultShape, alpha, beta) {
if (b instanceof SparseTensor) {
return addSparse(a, b, resultShape, alpha, beta);
}
else {
return addDense(a, b, resultShape, alpha, beta);
}
}
function addSparse(a, b, resultShape, alpha, beta) {
if (a.nnz !== b.nnz) {
throw new Error('Addition with two sparse tensors expects the same sparsity pattern, and thus the same number of nonzero entries in both tensors');
}
else if (a.denseDims !== b.denseDims) {
throw new Error('Addition with two sparse tensors expects the same number of sparse and dense dimensions in both tensors');
}
if (a.values instanceof CPUTensor) {
return addSparseCPU(a, b, resultShape, alpha, beta);
}
else if (a.values instanceof WASMTensor) {
return addSparseWASM(a, b, resultShape, alpha, beta);
}
throw new Error('Sparse-sparse matrix addition not supported on WebGL backend');
}
function addDense(a, b, resultShape, alpha, beta) {
if (b instanceof CPUTensor) {
return addDenseCPU(a, b, resultShape, alpha, beta);
}
else if (b instanceof WASMTensor) {
return addDenseWASM(a, b, resultShape, alpha, beta);
}
throw new Error('Sparse-dense matrix addition not supported on WebGL backend');
}
//# sourceMappingURL=add.js.map