* @brief Tensor factory function to create a tensor (a Tensor type is simply * an Array with an N-dimensional Shape specification) on the GPU. The tensor * is created with the given shape, data type, and usage flags, added to the * TensorPool, and returned. * * This is the core implementation which takes the minimal set of parameters in * terms of the raw WebGPU API, and is used by the othe
| 573 | * @endcode |
| 574 | */ |
| 575 | inline Tensor |
| 576 | createTensor(TensorPool &pool, WGPUDevice &device, const Shape &shape, |
| 577 | NumType dtype, |
| 578 | WGPUBufferUsageFlags usage = WGPUBufferUsage_Storage | |
| 579 | WGPUBufferUsage_CopyDst | |
| 580 | WGPUBufferUsage_CopySrc) { |
| 581 | LOG(kDefLog, kTrace, "Creating tensor"); |
| 582 | size_t numElements = size(shape); |
| 583 | size_t size = sizeBytes(dtype) * numElements; |
| 584 | WGPUBufferDescriptor bufferDesc = { |
| 585 | .usage = usage, |
| 586 | .size = size, |
| 587 | }; |
| 588 | WGPUBuffer buffer = wgpuDeviceCreateBuffer(device, &bufferDesc); |
| 589 | pool.data[buffer] = Tensor{ |
| 590 | .data = Array{.buffer = buffer, .usage = usage, .size = size}, |
| 591 | .shape = shape, |
| 592 | }; |
| 593 | return pool.data[buffer]; |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * @brief Overload of the tensor factory function to instantiate a tensor on |