UNPKG

@hoff97/tensor-js

Version:

PyTorch like deep learning inferrence library

38 lines 1.72 kB
import { CPUTensor } from '../../../../tensor/cpu/tensor'; import { SparseTensor } from '../../../../tensor/sparse/tensor'; import { WASMTensor } from '../../../../tensor/wasm/tensor'; import { subtractDenseCPU, subtractSparseCPU } from './cpu'; import { subtractDenseWASM, subtractSparseWASM } from './wasm'; export function subtract(a, b, resultShape, alpha, beta) { if (b instanceof SparseTensor) { return subtractSparse(a, b, resultShape, alpha, beta); } else { return subtractDense(a, b, resultShape, alpha, beta); } } function subtractSparse(a, b, resultShape, alpha, beta) { if (a.nnz !== b.nnz) { throw new Error('Subtraction 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('Subtraction with two sparse tensors expects the same number of sparse and dense dimensions in both tensors'); } if (a.values instanceof CPUTensor) { return subtractSparseCPU(a, b, resultShape, alpha, beta); } else if (a.values instanceof WASMTensor) { return subtractSparseWASM(a, b, resultShape, alpha, beta); } throw new Error('Sparse-sparse matrix subtraction not supported on WebGL backend'); } function subtractDense(a, b, resultShape, alpha, beta) { if (b instanceof CPUTensor) { return subtractDenseCPU(a, b, resultShape, alpha, beta); } else if (b instanceof WASMTensor) { return subtractDenseWASM(a, b, resultShape, alpha, beta); } throw new Error('Sparse-dense matrix addition not supported on WASM/WebGL backend'); } //# sourceMappingURL=subtract.js.map