* Read tensor to a new GPUBuffer. * @param dataId The source tensor.
(dataId: DataId)
| 594 | * @param dataId The source tensor. |
| 595 | */ |
| 596 | override readToGPU(dataId: DataId): GPUData { |
| 597 | const srcTensorData = this.tensorMap.get(dataId); |
| 598 | const {values, dtype, shape, resource} = srcTensorData; |
| 599 | |
| 600 | if (dtype === 'complex64') { |
| 601 | throw new Error('Does not support reading buffer for complex64 dtype.'); |
| 602 | } |
| 603 | |
| 604 | if (resource == null) { |
| 605 | if (values != null) { |
| 606 | throw new Error('Data is not on GPU but on CPU.'); |
| 607 | } else { |
| 608 | throw new Error('There is no data on GPU or CPU.'); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | const srcBuffer = resource as GPUBuffer; |
| 613 | const size = srcBuffer.size; |
| 614 | const usage = srcBuffer.usage; |
| 615 | const buffer = this.bufferManager.acquireBuffer(size, usage); |
| 616 | this.ensureCommandEncoderReady(); |
| 617 | this.endComputePassEncoder(); |
| 618 | this.commandEncoder.copyBufferToBuffer( |
| 619 | resource as GPUBuffer, 0, buffer, 0, size); |
| 620 | this.submitQueue(); |
| 621 | |
| 622 | const tensorInfo = this.makeTensorInfo(shape, dtype); |
| 623 | // Make engine track this tensor, so that we can dispose it later. |
| 624 | const tensorRef = engine().makeTensorFromTensorInfo(tensorInfo); |
| 625 | |
| 626 | const tensorData = this.tensorMap.get(tensorInfo.dataId); |
| 627 | tensorData.resource = buffer; |
| 628 | |
| 629 | return {tensorRef, buffer}; |
| 630 | } |
| 631 | |
| 632 | bufferSync<R extends Rank, D extends DataType>(t: TensorInfo): |
| 633 | TensorBuffer<R, D> { |
nothing calls this directly
no test coverage detected