Documentation
¶
Overview ¶
Package webgpu implements the WebGPU backend for GPU-accelerated tensor operations. Uses go-webgpu (github.com/go-webgpu/webgpu) for zero-CGO WebGPU bindings.
Package webgpu provides embedded WGSL compute shaders for tensor operations.
Index ¶
- func IsAvailable() (available bool)
- func ListAdapters() (adapters []*wgpu.AdapterInfoGo, err error)
- type Backend
- func (b *Backend) AdapterInfo() *wgpu.AdapterInfoGo
- func (b *Backend) Add(a, other *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) AddScalar(_ *tensor.RawTensor, _ any) *tensor.RawTensor
- func (b *Backend) And(_, _ *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) Argmax(_ *tensor.RawTensor, _ int) *tensor.RawTensor
- func (b *Backend) BatchMatMul(_, _ *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) Cast(_ *tensor.RawTensor, _ tensor.DataType) *tensor.RawTensor
- func (b *Backend) Cat(_ []*tensor.RawTensor, _ int) *tensor.RawTensor
- func (b *Backend) Chunk(_ *tensor.RawTensor, _, _ int) []*tensor.RawTensor
- func (b *Backend) Conv2D(_, _ *tensor.RawTensor, _, _ int) *tensor.RawTensor
- func (b *Backend) Cos(_ *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) Device() tensor.Device
- func (b *Backend) Div(a, other *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) DivScalar(_ *tensor.RawTensor, _ any) *tensor.RawTensor
- func (b *Backend) Equal(_, _ *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) Exp(_ *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) Expand(_ *tensor.RawTensor, _ tensor.Shape) *tensor.RawTensor
- func (b *Backend) Gather(_ *tensor.RawTensor, _ int, _ *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) Greater(_, _ *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) GreaterEqual(_, _ *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) Log(_ *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) Lower(_, _ *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) LowerEqual(_, _ *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) MatMul(a, other *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) MaxPool2D(_ *tensor.RawTensor, _, _ int) *tensor.RawTensor
- func (b *Backend) MeanDim(_ *tensor.RawTensor, _ int, _ bool) *tensor.RawTensor
- func (b *Backend) MemoryStats() MemoryStats
- func (b *Backend) Mul(a, other *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) MulScalar(_ *tensor.RawTensor, _ any) *tensor.RawTensor
- func (b *Backend) Name() string
- func (b *Backend) Not(_ *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) NotEqual(_, _ *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) Or(_, _ *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) ReLU(x *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) Release()
- func (b *Backend) Reshape(t *tensor.RawTensor, newShape tensor.Shape) *tensor.RawTensor
- func (b *Backend) Rsqrt(_ *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) Sigmoid(x *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) Sin(_ *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) Softmax(x *tensor.RawTensor, dim int) *tensor.RawTensor
- func (b *Backend) Sqrt(_ *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) Squeeze(_ *tensor.RawTensor, _ int) *tensor.RawTensor
- func (b *Backend) Sub(a, other *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) SubScalar(_ *tensor.RawTensor, _ any) *tensor.RawTensor
- func (b *Backend) Sum(_ *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) SumDim(_ *tensor.RawTensor, _ int, _ bool) *tensor.RawTensor
- func (b *Backend) Tanh(x *tensor.RawTensor) *tensor.RawTensor
- func (b *Backend) Transpose(t *tensor.RawTensor, axes ...int) *tensor.RawTensor
- func (b *Backend) Unsqueeze(_ *tensor.RawTensor, _ int) *tensor.RawTensor
- func (b *Backend) Where(_, _, _ *tensor.RawTensor) *tensor.RawTensor
- type BufferPool
- type BufferSize
- type MemoryStats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsAvailable ¶
func IsAvailable() (available bool)
IsAvailable checks if WebGPU is available on this system.
func ListAdapters ¶
func ListAdapters() (adapters []*wgpu.AdapterInfoGo, err error)
ListAdapters returns information about all available GPU adapters.
Types ¶
type Backend ¶
type Backend struct {
// contains filtered or unexported fields
}
Backend implements tensor operations on GPU using WebGPU.
func New ¶
New creates a new WebGPU backend. Returns an error if WebGPU is not available or initialization fails.
func (*Backend) AdapterInfo ¶
func (b *Backend) AdapterInfo() *wgpu.AdapterInfoGo
AdapterInfo returns information about the GPU adapter.
func (*Backend) AddScalar ¶ added in v0.3.0
AddScalar adds a scalar to tensor elements (not yet implemented).
func (*Backend) Argmax ¶ added in v0.3.0
Argmax returns indices of maximum values along dimension (not yet implemented).
func (*Backend) BatchMatMul ¶ added in v0.4.0
BatchMatMul performs batched matrix multiplication on GPU. TODO: Implement WGSL compute shader for batched matmul.
func (*Backend) Cast ¶ added in v0.3.0
Cast converts tensor to different data type (not yet implemented).
func (*Backend) Cat ¶ added in v0.3.0
Cat concatenates tensors (stub - not implemented for WebGPU yet).
func (*Backend) Chunk ¶ added in v0.3.0
Chunk splits tensor (stub - not implemented for WebGPU yet).
func (*Backend) Conv2D ¶
Conv2D performs 2D convolution on GPU. TODO: Implement WGSL compute shader for convolution.
func (*Backend) Cos ¶ added in v0.3.0
Cos computes element-wise cosine (stub - not implemented for WebGPU yet).
func (*Backend) DivScalar ¶ added in v0.3.0
DivScalar divides tensor elements by a scalar (not yet implemented).
func (*Backend) Equal ¶ added in v0.3.0
Equal performs element-wise equality comparison (not yet implemented).
func (*Backend) Exp ¶ added in v0.3.0
Exp computes element-wise exponential (stub - not implemented for WebGPU yet).
func (*Backend) Expand ¶ added in v0.3.0
Expand broadcasts tensor to new shape (not yet implemented).
func (*Backend) Gather ¶ added in v0.3.0
Gather selects elements along dim using index tensor (not implemented yet).
func (*Backend) Greater ¶ added in v0.3.0
Greater performs element-wise greater-than comparison (not yet implemented).
func (*Backend) GreaterEqual ¶ added in v0.3.0
GreaterEqual performs element-wise greater-or-equal comparison (not yet implemented).
func (*Backend) Log ¶ added in v0.3.0
Log computes natural logarithm element-wise (not yet implemented).
func (*Backend) Lower ¶ added in v0.3.0
Lower performs element-wise less-than comparison (not yet implemented).
func (*Backend) LowerEqual ¶ added in v0.3.0
LowerEqual performs element-wise less-or-equal comparison (not yet implemented).
func (*Backend) MaxPool2D ¶
MaxPool2D performs 2D max pooling on GPU. TODO: Implement WGSL compute shader for max pooling.
func (*Backend) MeanDim ¶ added in v0.3.0
MeanDim computes mean along a dimension (stub - not implemented for WebGPU yet).
func (*Backend) MemoryStats ¶
func (b *Backend) MemoryStats() MemoryStats
MemoryStats returns current GPU memory usage statistics.
func (*Backend) MulScalar ¶ added in v0.3.0
MulScalar multiplies tensor elements by a scalar (not yet implemented).
func (*Backend) NotEqual ¶ added in v0.3.0
NotEqual performs element-wise inequality comparison (not yet implemented).
func (*Backend) Release ¶
func (b *Backend) Release()
Release releases all WebGPU resources. Must be called when the backend is no longer needed.
func (*Backend) Reshape ¶
Reshape returns a tensor with new shape. This is typically a metadata-only operation (zero-copy).
func (*Backend) Rsqrt ¶ added in v0.3.0
Rsqrt computes element-wise reciprocal square root (stub - not implemented for WebGPU yet).
func (*Backend) Sin ¶ added in v0.3.0
Sin computes element-wise sine (stub - not implemented for WebGPU yet).
func (*Backend) Softmax ¶
Softmax applies softmax along the specified dimension. Currently supports 2D tensors with dim=-1 (last dimension).
func (*Backend) Sqrt ¶ added in v0.3.0
Sqrt computes element-wise square root (stub - not implemented for WebGPU yet).
func (*Backend) Squeeze ¶ added in v0.3.0
Squeeze removes dimension (stub - not implemented for WebGPU yet).
func (*Backend) SubScalar ¶ added in v0.3.0
SubScalar subtracts a scalar from tensor elements (not yet implemented).
func (*Backend) SumDim ¶ added in v0.3.0
SumDim sums along a dimension (stub - not implemented for WebGPU yet).
func (*Backend) Transpose ¶
Transpose transposes the tensor by permuting its dimensions. Currently supports only 2D tensors (matrix transpose). For multi-dimensional transpose with custom axes, use the general case (TODO).
type BufferPool ¶
type BufferPool struct {
// contains filtered or unexported fields
}
BufferPool manages GPU buffer reuse to reduce allocation overhead. Buffers are categorized by size and usage flags.
func NewBufferPool ¶
func NewBufferPool(device *wgpu.Device) *BufferPool
NewBufferPool creates a new buffer pool for the given device.
func (*BufferPool) Acquire ¶
func (p *BufferPool) Acquire(size uint64, usage wgpu.BufferUsage) *wgpu.Buffer
Acquire gets a buffer from the pool or creates a new one. Returns a buffer that matches or exceeds the requested size and usage.
func (*BufferPool) Clear ¶
func (p *BufferPool) Clear()
Clear releases all pooled buffers. Should be called when the backend is released.
func (*BufferPool) Release ¶
func (p *BufferPool) Release(buffer *wgpu.Buffer, size uint64, usage wgpu.BufferUsage)
Release returns a buffer to the pool for reuse. If the pool is full, the buffer is immediately released.
func (*BufferPool) Stats ¶
func (p *BufferPool) Stats() (allocated, released, hits, misses uint64, pooledCount int)
Stats returns statistics about buffer pool usage.
type BufferSize ¶
type BufferSize int
BufferSize represents different buffer size categories for pooling.
const ( // SmallBuffer for tensors < 4KB. SmallBuffer BufferSize = iota // MediumBuffer for tensors 4KB-1MB. MediumBuffer // LargeBuffer for tensors > 1MB. LargeBuffer )
type MemoryStats ¶
type MemoryStats struct {
// Total bytes allocated since backend creation
TotalAllocatedBytes uint64
// Peak memory usage in bytes
PeakMemoryBytes uint64
// Number of currently active buffers
ActiveBuffers int64
// Buffer pool statistics
PoolAllocated uint64
PoolReleased uint64
PoolHits uint64
PoolMisses uint64
PooledBuffers int
}
MemoryStats represents GPU memory usage statistics.