* Copy data from raw bytes. * @param data Uint8Array of bytes. * @returns this
(data: Uint8Array)
| 648 | * @returns this |
| 649 | */ |
| 650 | copyFromRawBytes(data: Uint8Array): this { |
| 651 | // short cut for gpu copy |
| 652 | if (this.device.deviceType === DeviceStrToEnum.webgpu) { |
| 653 | this.lib.webGPUContext?.copyRawBytesToBuffer(data, this.getDataPtr(), 0, data.length); |
| 654 | return this; |
| 655 | } |
| 656 | // CPU copy |
| 657 | const size = this.shape.reduce((a, b) => { |
| 658 | return a * b; |
| 659 | }, 1); |
| 660 | const nbytes = this.dlDataType.numStorageBytes() * size; |
| 661 | if (nbytes != data.length) { |
| 662 | throw new Error("Expect the data's length equals nbytes=" + nbytes); |
| 663 | } |
| 664 | this.ctx.tensorCopyFromJSBytes(this, data); |
| 665 | return this; |
| 666 | } |
| 667 | /** |
| 668 | * Return a copied Uint8Array of the raw bytes in the Tensor. |
| 669 | * @returns The result array. |
no test coverage detected