(zeroCopy = false)
| 560 | } |
| 561 | |
| 562 | function createTensorFromGPUTest(zeroCopy = false) { |
| 563 | it('use default shape and data type(float32)', async () => { |
| 564 | await testCreateTensorFromGPUBuffer('float32', true, zeroCopy); |
| 565 | }); |
| 566 | |
| 567 | it('work for float32', async () => { |
| 568 | await testCreateTensorFromGPUBuffer('float32', false, zeroCopy); |
| 569 | }); |
| 570 | |
| 571 | it('work for int32', async () => { |
| 572 | await testCreateTensorFromGPUBuffer('int32', false, zeroCopy); |
| 573 | }); |
| 574 | |
| 575 | it('work for read', async () => { |
| 576 | const webGPUBackend = tf.backend() as WebGPUBackend; |
| 577 | const device = webGPUBackend.device; |
| 578 | const aData = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; |
| 579 | const dtype = 'float32'; |
| 580 | const aBuffer = createGPUBufferFromData(device, aData, dtype); |
| 581 | const shape: number[] = [aData.length]; |
| 582 | const a = tf.tensor({buffer: aBuffer, zeroCopy}, shape, dtype); |
| 583 | if (zeroCopy !== true) { |
| 584 | aBuffer.destroy(); |
| 585 | } |
| 586 | await a.data(); |
| 587 | if (zeroCopy === true) { |
| 588 | aBuffer.destroy(); |
| 589 | } |
| 590 | }); |
| 591 | |
| 592 | it('two tensors share the same GPUBuffer', async () => { |
| 593 | const webGPUBackend = tf.backend() as WebGPUBackend; |
| 594 | const device = webGPUBackend.device; |
| 595 | const aData = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; |
| 596 | const dtype = 'float32'; |
| 597 | const aBuffer = createGPUBufferFromData(device, aData, dtype); |
| 598 | const startNumBytes = tf.memory().numBytes; |
| 599 | const startNumTensors = tf.memory().numTensors; |
| 600 | const shape: number[] = [aData.length]; |
| 601 | const webGPUData = {buffer: aBuffer, zeroCopy}; |
| 602 | const a = tf.tensor(webGPUData, shape, dtype); |
| 603 | const b = tf.tensor(webGPUData, shape, dtype); |
| 604 | if (zeroCopy !== true) { |
| 605 | aBuffer.destroy(); |
| 606 | } |
| 607 | const result = tf.add(a, b); |
| 608 | const expected = |
| 609 | [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32]; |
| 610 | tf.test_util.expectArraysClose(await result.data(), expected); |
| 611 | a.dispose(); |
| 612 | b.dispose(); |
| 613 | result.dispose(); |
| 614 | const endNumBytes = tf.memory().numBytes; |
| 615 | const endNumTensors = tf.memory().numTensors; |
| 616 | expect(endNumBytes - startNumBytes).toEqual(0); |
| 617 | expect(endNumTensors - startNumTensors).toEqual(0); |
| 618 | if (zeroCopy === true) { |
| 619 | aBuffer.destroy(); |
no test coverage detected
searching dependent graphs…