Documentation
¶
Index ¶
- Variables
- func AddBiasLastDimInGPU(out, bias *Tensor) bool
- func ClearPersistentGPU()
- func CopyData(dst, src []float32)
- func InitGPU() error
- func ShutdownGPU()
- type AllocReporter
- type Backend
- type ElemFunc
- type Tensor
- func New(shape ...int) *Tensor
- func NewGPU(shape []int, data []float32, rep AllocReporter) *Tensor
- func NewRandom(shape []int, scale float32) *Tensor
- func NewReported(rep AllocReporter, shape ...int) *Tensor
- func NewWithData(shape []int, data []float32) *Tensor
- func UnmarshalBinary(data []byte) (*Tensor, error)
- func (a *Tensor) Add(b *Tensor) *Tensor
- func (a *Tensor) AddIn(b *Tensor)
- func (a *Tensor) AddMulNumIn(b *Tensor, n float32)
- func (a *Tensor) AddNumIn(n float32)
- func (a *Tensor) AddNumInAt(p []int, n float32)
- func (a *Tensor) Apply(fn ElemFunc) *Tensor
- func (a *Tensor) ApplyIn(fn ElemFunc)
- func (a *Tensor) ArgMax() int
- func (a *Tensor) CheckData()
- func (a *Tensor) Clear()
- func (a *Tensor) Copy() *Tensor
- func (t *Tensor) Data() []float32
- func (a *Tensor) Dim() int
- func (t *Tensor) EnsureCPU()
- func (t *Tensor) EnsureGPU() (cuda.DevicePtr, error)
- func (a *Tensor) Exp() *Tensor
- func (a *Tensor) ExpectShape(shape ...int)
- func (a *Tensor) Fill(value float32)
- func (t *Tensor) Free()
- func (t *Tensor) GPUResident() bool
- func (a *Tensor) Get(p ...int) float32
- func (a *Tensor) Get2D(row, col int) float32
- func (a *Tensor) IsSameShape(b *Tensor) bool
- func (a *Tensor) Load(file io.Reader) error
- func (a *Tensor) Log() *Tensor
- func (t *Tensor) MarkCPUModified()
- func (t *Tensor) MarkGPUModified()
- func (a *Tensor) MarshalBinary() []byte
- func (a *Tensor) MatMul(b *Tensor) *Tensor
- func (a *Tensor) MatMulBatch(b *Tensor) *Tensor
- func (a *Tensor) MatMulLastDim(b *Tensor) *Tensor
- func (a *Tensor) Max() float32
- func (a *Tensor) Mean() float32
- func (a *Tensor) Mul(b *Tensor) *Tensor
- func (a *Tensor) MulIn(b *Tensor)
- func (a *Tensor) MulNum(n float32) *Tensor
- func (a *Tensor) MulNumIn(n float32)
- func (a *Tensor) Numel() int
- func (a *Tensor) Permute(axes ...int) *Tensor
- func (a *Tensor) ReduceToShape(shape []int) *Tensor
- func (a *Tensor) Reshape(shape ...int) *Tensor
- func (logits *Tensor) SampleSoftmax(temperature float32) int
- func (a *Tensor) Save(file io.Writer) error
- func (a *Tensor) SelectDim(dim, index int) *Tensor
- func (a *Tensor) Set(p []int, value float32)
- func (a *Tensor) Set1D(row int, value float32)
- func (a *Tensor) Set2D(row, col int, value float32)
- func (a *Tensor) SetData(b *Tensor)
- func (a *Tensor) SliceDim(dim, start, end int) *Tensor
- func (a *Tensor) Softmax() *Tensor
- func (a *Tensor) Sub(b *Tensor) *Tensor
- func (a *Tensor) SubIn(b *Tensor)
- func (a *Tensor) SubNum(n float32) *Tensor
- func (a *Tensor) Sum() float32
- func (a *Tensor) SumDim(dim int) *Tensor
- func (a *Tensor) Transpose() *Tensor
- func (a *Tensor) TransposeInto(dst *Tensor)
- func (a *Tensor) View(p ...int) *Tensor
Constants ¶
This section is empty.
Variables ¶
var ( Threads = max(1, runtime.NumCPU()/3*2) // number of threads used when possible Checks = false // for development, will slow down )
var ( // UseGPU enables CUDA GEMM when InitGPU succeeded. UseGPU = false )
Functions ¶
func AddBiasLastDimInGPU ¶ added in v1.3.0
AddBiasLastDimInGPU adds 1D bias to last dim on GPU when possible.
func ClearPersistentGPU ¶ added in v1.3.0
func ClearPersistentGPU()
ClearPersistentGPU frees all persistent GPU allocations.
func CopyData ¶ added in v1.3.0
func CopyData(dst, src []float32)
CopyData copies src into dst using multiple threads for large buffers.
func InitGPU ¶ added in v1.3.0
func InitGPU() error
InitGPU loads libgoneural_cuda.so and enables GPU matmul.
func ShutdownGPU ¶ added in v1.3.0
func ShutdownGPU()
ShutdownGPU releases CUDA resources and disables GPU matmul.
Types ¶
type AllocReporter ¶ added in v1.3.0
type AllocReporter interface {
ReportAlloc(t *Tensor)
}
AllocReporter receives byte-size updates as tensors are allocated.
func FirstReporter ¶ added in v1.3.0
func FirstReporter(rep ...AllocReporter) AllocReporter
FirstReporter returns the optional reporter from a variadic argument list.
type Backend ¶ added in v1.3.0
type Backend interface {
Init() error
Shutdown()
GemmF32(A, B, C []float32, M, N, K int) error
GemmDeviceAt(aDev, bDev, cDev cuda.DevicePtr, aOff, bOff, cOff, M, N, K int) error
GemmStridedBatchedAt(aDev, bDev, cDev cuda.DevicePtr, aOff, bOff, cOff, M, N, K, batchCount int) error
}
Backend runs GEMM on CPU or CUDA.
type Tensor ¶
type Tensor struct {
Shape []int // row-major dimensions
// contains filtered or unexported fields
}
Tensor is a shaped view into shared buffer storage.
func NewGPU ¶ added in v1.3.0
func NewGPU(shape []int, data []float32, rep AllocReporter) *Tensor
NewGPU creates a tensor with persistent GPU storage policy.
func NewRandom ¶
New tensor of specified shape, random values standard normal (Gaussian) distribution
func NewReported ¶ added in v1.3.0
func NewReported(rep AllocReporter, shape ...int) *Tensor
NewReported allocates a tensor and reports its size to rep when non-nil.
func NewWithData ¶ added in v1.3.0
New tensor of specified shape and data (does not copy data)
func UnmarshalBinary ¶
func (*Tensor) AddMulNumIn ¶
func (*Tensor) AddNumInAt ¶
func (*Tensor) EnsureCPU ¶ added in v1.3.0
func (t *Tensor) EnsureCPU()
Ensures CPU buffer has valid data, downloads from GPU when needed
func (*Tensor) EnsureGPU ¶ added in v1.3.0
Ensures GPU buffer has valid data, uploads from CPU when needed
func (*Tensor) ExpectShape ¶
Checks that tensor has exactly specified shape
func (*Tensor) Free ¶
func (t *Tensor) Free()
Free releases this tensor handle; frees GPU when last handle is gone.
func (*Tensor) GPUResident ¶ added in v1.3.0
GPUResident reports whether this tensor's buffer has valid GPU data.
func (*Tensor) IsSameShape ¶
Tells if tensors have exactly same shape
func (*Tensor) MarkCPUModified ¶ added in v1.3.0
func (t *Tensor) MarkCPUModified()
Marks CPU buffer modified, GPU buffer becomes invalid
func (*Tensor) MarkGPUModified ¶ added in v1.3.0
func (t *Tensor) MarkGPUModified()
Marks GPU buffer modified, CPU buffer becomes invalid
func (*Tensor) MarshalBinary ¶
func (*Tensor) MatMul ¶
Matrix multiplication between tensors. Cross-dimension only happens between two inner dimensions [row,col], and above dimensions must match exactly. No automatic reshape is made, because would not be explicit, caller must reshape and potentially have inner dimension "1". Outer dimension broadcasting is supported.
func (*Tensor) MatMulBatch ¶
Performs per-batch matrix multiplication for 3D tensors. For each batch: out[b] = a[b] @ b[b]
func (*Tensor) MatMulLastDim ¶
Matrix multiplication applied to the last dimension of A with 2D matrix B
func (*Tensor) Permute ¶
Reorders tensor according to specified axes. Axes must contain each dimension index exactly once, defining how the original axes are mapped into the output tensor.
func (*Tensor) ReduceToShape ¶
Reduces tensor to match specified lower shape
func (*Tensor) SampleSoftmax ¶
func (*Tensor) SelectDim ¶
Selects the slice at the given index along the specified dimension and removes that dimension. Returns a new tensor. Example: [B,T,D].SelectDim(1,t) -> [B,D] where T=t
func (*Tensor) SliceDim ¶
Returns a slice (copy) on the tensor sliced along the given dimension in the half-open range [start:end]. In other words restricts one dimension (dim) to range [start:end] while keeping all other dimensions unchanged.
func (*Tensor) Softmax ¶
Turns a vector of arbitrary real numbers (logits) into a probability distribution: all values in [0,1] and sum to 1
func (*Tensor) TransposeInto ¶ added in v1.3.0
TransposeInto writes src^T into dst (swaps the two innermost dimensions).