UNPKG

@hoff97/tensor-js

Version:

PyTorch like deep learning inferrence library

38 lines 1.71 kB
import { CPUTensor } from '../../../../tensor/cpu/tensor'; import { SparseTensor } from '../../../../tensor/sparse/tensor'; import { WASMTensor } from '../../../../tensor/wasm/tensor'; import { multiplyDenseCPU, multiplySparseCPU } from './cpu'; import { multiplyDenseWASM, multiplySparseWASM } from './wasm'; export function multiply(a, b, resultShape, alpha) { if (b instanceof SparseTensor) { return multiplySparse(a, b, resultShape, alpha); } else { return multiplyDense(a, b, resultShape, alpha); } } function multiplySparse(a, b, resultShape, alpha) { if (a.nnz !== b.nnz) { throw new Error('Element wise multiplication 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('Element wise multiplication with two sparse tensors expects the same number of sparse and dense dimensions in both tensors'); } if (a.values instanceof CPUTensor) { return multiplySparseCPU(a, b, resultShape, alpha); } else if (a.values instanceof WASMTensor) { return multiplySparseWASM(a, b, resultShape, alpha); } throw new Error('Sparse-sparse matrix addition not supported on WebGL backend'); } function multiplyDense(a, b, resultShape, alpha) { if (b instanceof CPUTensor) { return multiplyDenseCPU(a, b, resultShape, alpha); } else if (b instanceof WASMTensor) { return multiplyDenseWASM(a, b, resultShape, alpha); } throw new Error('Sparse-dense matrix element wise multiplication not supported on WASM/WebGL backend'); } //# sourceMappingURL=multiply.js.map