nn

package
v0.21.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2026 License: Apache-2.0 Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package nn runs neural networks published as PyTorch checkpoints: their weights read from the .pth file as it is, and the layers they are made of (linear, convolutions, attention, normalisations, resizing) on float32 tensors, laid out as PyTorch lays them out (row major, the last dimension fastest).

Index

Constants

View Source
const SGMDisparities = 64

SGMDisparities is the most a match searches each way (half resolution pixels): stereo's matchRange, which kernels.cu's SGM_R must equal.

Variables

This section is empty.

Functions

func EyesSize

func EyesSize(w, h int) int

EyesSize is the bytes of a w x h picture's two eyes (Eyes').

func LoadPTH

func LoadPTH(file string) (map[string]*Tensor, error)

LoadPTH reads the tensors of a checkpoint's state dict, by name. A checkpoint whose state dict is under a key ("state_dict", "model") gives that one's.

func Numel

func Numel(shape []int) int

Numel is the number of elements of a shape.

func Parallel

func Parallel(n int, f func(i int))

Parallel runs f(i) for i < n over the CPUs.

func TemporalAttentionBackward added in v0.19.0

func TemporalAttentionBackward(q, peQ, peK, peV *Tensor, ks, vs []*Tensor, heads int, scale float32, dy *Tensor) (dq, dpeQ, dpeK, dpeV *Tensor, dks, dvs []*Tensor)

TemporalAttentionBackward is the gradients of the CPU's TemporalAttention(q, peQ, peK, peV, ks, vs, heads, scale) given dy: at q and peQ, at peK and peV, and at every frame's keys and values.

Types

type Backend

type Backend interface {
	// Upload is t as the backend's tensor (the CPU's: t itself).
	Upload(t *Tensor) *Tensor
	// Download is the backend's tensor in memory (waiting for the ops
	// before to have run).
	Download(t *Tensor) (*Tensor, error)

	Linear(x, w, b *Tensor) *Tensor
	Conv2d(x, w, b *Tensor, stride, pad int) *Tensor
	ConvTranspose2dK(x, w, b *Tensor) *Tensor
	LayerNorm(x, w, b *Tensor, eps float32) *Tensor
	GroupNorm(x *Tensor, groups int, w, b *Tensor, eps float32) *Tensor
	GELU(x *Tensor) *Tensor   // in place
	ReLU(x *Tensor) *Tensor   // in place
	Add(a, b *Tensor) *Tensor // in place
	Clone(x *Tensor) *Tensor
	Attention(q, k, v *Tensor, heads int, scale float32) *Tensor
	Bilinear(x *Tensor, oh, ow int) *Tensor

	// Tokens is a vision transformer's tokens: a class token cls [D]
	// first, then the patches [D, N] (channels first) as rows, plus the
	// position embedding pos [1+N, D].
	Tokens(patches, cls, pos *Tensor) *Tensor
	// Split3 splits [N, 3D] rows into their three [N, D] thirds.
	Split3(x *Tensor) (q, k, v *Tensor)
	// AddScaled adds y times a per-channel g [D] to x [N, D], in place.
	AddScaled(x, y, g *Tensor) *Tensor
	// Transpose is x [R, C] as [C, R].
	Transpose(x *Tensor) *Tensor
	// GEGLU is a GEGLU's activation: x [N, 2D]'s first half of each row
	// times the GELU of its second ([N, D]).
	GEGLU(x *Tensor) *Tensor
	// Rows is x's rows from the from-th on ([N-from, D]).
	Rows(x *Tensor, from int) *Tensor
	// TemporalAttention attends, at each of D positions, from the query
	// q [D, C] plus peQ [C] over T frames' keys ks[f] [D, C] plus peK[f]
	// and values vs[f] plus peV[f] (peK, peV [T, C]), the C channels in
	// heads.
	TemporalAttention(q, peQ, peK, peV *Tensor, ks, vs []*Tensor, heads int, scale float32) *Tensor

	// Keep keeps a tensor past Release (the GPU's are otherwise reused).
	Keep(t *Tensor)
	// Free frees a kept tensor.
	Free(t *Tensor)
	// Release gives back every tensor made since the last Release that is
	// not kept: a network's intermediates, once its output is downloaded.
	Release()
}

Backend runs the layers somewhere: on the CPU (CPU, the package's functions) or on a GPU (NewGPU). A backend's ops take and give its own tensors: Upload makes one of a tensor in memory, Download the reverse. The ops that say "in place" change their first operand and give it back.

var CPU Backend = cpu{}

CPU is the CPU backend: the package's functions.

type GPU

type GPU struct {

	// Half rounds the matrix products' inputs to half precision, for the
	// tensor cores (their sums stay single precision).
	Half bool
	// contains filtered or unexported fields
}

GPU is the CUDA backend: tensors in the GPU's memory, the ops its kernels (kernels.cu). Memory is reused: what an op allocates is given back at Release unless kept, to be handed out again to the next op asking for as many bytes (a network's frames ask for the same each time); what no op asked for again between two Releases is freed (the sizes change with the picture's). An op's failure is kept and reported by the next Download.

func NewGPU

func NewGPU() (*GPU, error)

NewGPU is a backend on the first CUDA GPU (cuda.ErrUnavailable when there is none).

func (*GPU) Add

func (g *GPU) Add(a, b *Tensor) *Tensor

func (*GPU) AddScaled

func (g *GPU) AddScaled(x, y, s *Tensor) *Tensor

func (*GPU) AddScaledBackward added in v0.19.0

func (g *GPU) AddScaledBackward(y, s, dy *Tensor) (dyIn, ds *Tensor)

AddScaledBackward is the gradients of AddScaled(x, y, s) = x + y s (s a channel's) given dy: at y and at s (at x it is dy itself).

func (*GPU) Attention

func (g *GPU) Attention(q, k, v *Tensor, heads int, scale float32) *Tensor

func (*GPU) AttentionBackward added in v0.19.0

func (g *GPU) AttentionBackward(q, k, v *Tensor, heads int, scale float32, dy *Tensor) (dq, dk, dv *Tensor)

AttentionBackward is the gradients of Attention(q, k, v, heads, scale) given dy: at q, at k and at v. The scores are made again (never kept by the forward pass).

func (*GPU) Bilinear

func (g *GPU) Bilinear(x *Tensor, oh, ow int) *Tensor

func (*GPU) BilinearBackward

func (g *GPU) BilinearBackward(dy *Tensor, h, w int) *Tensor

BilinearBackward is the gradient of Bilinear(x, oh, ow) at x ([c, h, w]) given dy, the gradient at its output.

func (*GPU) Clone

func (g *GPU) Clone(x *Tensor) *Tensor

func (*GPU) Close

func (g *GPU) Close()

Close frees the GPU's memory and context.

func (*GPU) Conv2d

func (g *GPU) Conv2d(x, w, b *Tensor, stride, pad int) *Tensor

func (*GPU) Conv2dBackward

func (g *GPU) Conv2dBackward(x, w, dy *Tensor, stride, pad int) (dx, dw, db *Tensor)

Conv2dBackward is the gradients of Conv2d(x, w, b, stride, pad) given dy, the gradient at its output: at x, at w and at b. The input's patches as columns, dw is dy times them, the columns' gradient w's transpose times dy, summed back into the input's.

func (*GPU) ConvTranspose2dK

func (g *GPU) ConvTranspose2dK(x, w, b *Tensor) *Tensor

func (*GPU) ConvTranspose2dKBackward

func (g *GPU) ConvTranspose2dKBackward(x, w, dy *Tensor) (dx, dw, db *Tensor)

ConvTranspose2dKBackward is the gradients of ConvTranspose2dK(x, w, b) given dy, the gradient at its output: at x, at w and at b.

func (*GPU) Disparity

func (g *GPU) Disparity(up *Tensor, w, h, top, bottom int, a, b, far, knee, lim, clo, cspan, curve float32) *Tensor

Disparity is the disparity of a w x h picture from the depth up of its rows [top, bottom) (w a row): a times the depth plus b, kept within lim either way; the other rows 0. [h, w]. With curve above 0 the depth is first bent within the shot's range, clo its farthest and cspan its span, as stereo.Curved does (stereo.Shaper.CurveArgs's).

func (*GPU) Download

func (g *GPU) Download(t *Tensor) (*Tensor, error)

func (*GPU) DownloadBytes

func (g *GPU) DownloadBytes(dst []byte, t *Tensor) error

DownloadBytes copies the start of the GPU's tensor t to dst (waiting for the ops before to have run).

func (*GPU) DownloadPlanes added in v0.15.1

func (g *GPU) DownloadPlanes(dst []byte, p *Planes) error

DownloadPlanes copies p's planes into dst (Size bytes: Y's rows, then the chroma's), waiting for the ops before to have run.

func (*GPU) Eyes

func (g *GPU) Eyes(p *Picture, d *Tensor, gap float32) *Tensor

Eyes renders the picture's two eyes for its disparity d [h, w], as stereo.Pair does (gap its stereo.gapPixels): EyesSize bytes, the left eye's Y, Cb and Cr planes then the right's, their rows w and (w+1)/2 apart.

func (*GPU) EyesMask added in v0.21.0

func (g *GPU) EyesMask(p *Picture, d *Tensor, gap float32, withMask bool) (eyes, mask *Tensor)

EyesMask is Eyes and, when asked, the eyes' holes (what the eye saw nothing of, filled from the side): 2*w*h bytes, the left eye's luma rows then the right's, 1 in a hole.

func (*GPU) Free

func (g *GPU) Free(t *Tensor)

func (*GPU) GEGLU

func (g *GPU) GEGLU(x *Tensor) *Tensor

func (*GPU) GEGLUBackward added in v0.19.0

func (g *GPU) GEGLUBackward(x, dy *Tensor) *Tensor

GEGLUBackward is the gradient of GEGLU(x) at x given dy.

func (*GPU) GELU

func (g *GPU) GELU(x *Tensor) *Tensor

func (*GPU) GELUBackward added in v0.19.0

func (g *GPU) GELUBackward(x, dy *Tensor) *Tensor

GELUBackward is the gradient of GELU at its input x given dy.

func (*GPU) GroupNorm

func (g *GPU) GroupNorm(x *Tensor, groups int, w, b *Tensor, eps float32) *Tensor

func (*GPU) GroupNormBackward added in v0.19.0

func (g *GPU) GroupNormBackward(x *Tensor, groups int, w, dy *Tensor, eps float32) (dx, dw, db *Tensor)

GroupNormBackward is the gradients of GroupNorm(x, groups, w, b, eps) given dy: at x, at w and at b.

func (*GPU) InpaintApply added in v0.21.0

func (g *GPU) InpaintApply(eyes, mask *Tensor, eye, w, h, top, bottom int, d *Tensor)

InpaintApply takes the filler's corrections d ([3, ph, pw], InpaintInput's padding) into an eye of Eyes' output, in place, where the mask says hole.

func (*GPU) InpaintInput added in v0.21.0

func (g *GPU) InpaintInput(eyes, mask *Tensor, eye, w, h, top, bottom int) *Tensor

InpaintInput is the hole filler's input for an eye (0 the left, 1 the right) of Eyes' output of a w x h picture and EyesMask's mask, its rows [top, bottom): [4, ph, pw] (pw, ph the sides padded to a multiple of 8), as stereo.InpaintInput makes it.

func (*GPU) Keep

func (g *GPU) Keep(t *Tensor)

func (*GPU) LayerNorm

func (g *GPU) LayerNorm(x, w, b *Tensor, eps float32) *Tensor

func (*GPU) LayerNormBackward added in v0.19.0

func (g *GPU) LayerNormBackward(x, w, dy *Tensor, eps float32) (dx, dw, db *Tensor)

LayerNormBackward is the gradients of LayerNorm(x, w, b, eps) given dy: at x, at w and at b.

func (*GPU) Linear

func (g *GPU) Linear(x, w, b *Tensor) *Tensor

func (*GPU) LinearBackward added in v0.19.0

func (g *GPU) LinearBackward(x, w, dy *Tensor) (dx, dw, db *Tensor)

LinearBackward is the gradients of Linear(x, w, b) given dy, the gradient at its output: at x (x's shape), at w and at b.

func (*GPU) MatchBoth

func (g *GPU) MatchBoth(l, r *Picture, tex float64) (dl, dr []float32, vl, vr []byte, err error)

MatchBoth is stereo.MatchBoth on the GPU (kernels.cu's sgm_*: the same integers and doubles, so the same disparities) for two w x h pictures: at half their size, the left eye's disparity and the right's (full resolution pixels), and which of each are sure (tex the texture they need: stereo's matchTexture).

func (*GPU) Moments

func (g *GPU) Moments(x *Tensor, rows, cols, stride int) *Tensor

Moments is the sum of x's values and that of their squares (two float64s, in a tensor of four float32s): x's rows rows of cols values, stride apart.

func (*GPU) Name

func (g *GPU) Name() string

Name is the GPU's.

func (*GPU) NetInput

func (g *GPU) NetInput(p *Picture, top, ah, nw, nh int) *Tensor

NetInput is the picture's rows [top, top+ah) as the depth network takes them: RGB (BT.709, limited range), resized bilinearly to nw x nh, normalised by ImageNet's mean and deviation, [3, nh, nw].

func (*GPU) ReLU

func (g *GPU) ReLU(x *Tensor) *Tensor

func (*GPU) ReLUBackward

func (g *GPU) ReLUBackward(y, dy *Tensor) *Tensor

ReLUBackward zeroes dy, in place, where ReLU's output y is zero.

func (*GPU) RefineInput

func (g *GPU) RefineInput(p *Picture, top int, d, mom *Tensor, w, h, pw, ph int) *Tensor

RefineInput is the refinement's input, [4, ph, pw], for the picture's active rows from top at half size (w x h): their Y (2x2 averages), Cb and Cr, and the depth d [w x h] standardised by its moments mom (Moments'); the sides padded to pw x ph by repeating the last row and column.

func (*GPU) Release

func (g *GPU) Release()

func (*GPU) Renorm

func (g *GPU) Renorm(out *Tensor, w, h, pw int, want, got *Tensor) *Tensor

Renorm is out (w x h of rows pw apart, its moments got) put back to the mean and deviation of the moments want: [1, h, w].

func (*GPU) Rows

func (g *GPU) Rows(x *Tensor, from int) *Tensor

func (*GPU) Samples

func (g *GPU) Samples(d *Tensor, w, h, top, bottom, edge int) *Tensor

Samples gathers what of a w x h disparity d (whose active rows are [top, bottom)) the floating window and the subtitles read: its side strips edge wide on every other active row, the left's then the right's, then every fourth row and column of its lower half.

func (*GPU) Scale added in v0.15.1

func (g *GPU) Scale(p *Planes, ow, oh int, lx, ly, cx, cy *Taps) *Planes

Scale resamples p to ow x oh: lx and ly the luma's filters, cx and cy the chroma's (on chroma samples).

func (*GPU) Split3

func (g *GPU) Split3(x *Tensor) (*Tensor, *Tensor, *Tensor)

func (*GPU) TemporalAttention

func (g *GPU) TemporalAttention(q, peQ, peK, peV *Tensor, ks, vs []*Tensor, heads int, scale float32) *Tensor

func (*GPU) TemporalAttentionBackward added in v0.19.0

func (g *GPU) TemporalAttentionBackward(q, peQ, peK, peV *Tensor, ks, vs []*Tensor, heads int, scale float32, dy *Tensor,
	want []bool) (dq, dpeQ, dpeK, dpeV *Tensor, dks, dvs []*Tensor)

TemporalAttentionBackward is the gradients of TemporalAttention(q, peQ, peK, peV, ks, vs, heads, scale) given dy: at q and peQ, at peK and peV, and at the keys and values of the frames want says (nil for the others).

func (*GPU) Tokens

func (g *GPU) Tokens(patches, cls, pos *Tensor) *Tensor

func (*GPU) ToneMap added in v0.15.1

func (g *GPU) ToneMap(p *Planes, luts []float32, scale float32, full, hlg bool) *Planes

ToneMap is convert's tone mapping of p: luts its toLight, curve and gamma tables (4097 values each), scale the code values' over 8 bits', full whether p is full range, hlg whether it is HLG (else PQ).

func (*GPU) Transpose

func (g *GPU) Transpose(x *Tensor) *Tensor

func (*GPU) Upload

func (g *GPU) Upload(t *Tensor) *Tensor

func (*GPU) UploadPicture

func (g *GPU) UploadPicture(y, cb, cr []byte, w, h, strideY, strideC int) *Picture

UploadPicture puts a w x h picture's planes on the GPU (until Release): y's rows strideY apart, cb's and cr's strideC.

func (*GPU) UploadPlanes added in v0.15.1

func (g *GPU) UploadPlanes(y, uv []byte, w, h, pitch, depth int) *Planes

UploadPlanes puts a w x h picture's planes on the GPU (until Release): y's rows and uv's pitch bytes apart, from their slices' starts.

func (*GPU) UploadTaps added in v0.15.1

func (g *GPU) UploadTaps(first []int, w []int32, n int) *Taps

UploadTaps puts a filter on the GPU, kept until Close.

func (*GPU) Zeros

func (g *GPU) Zeros(shape ...int) *Tensor

Zeros is a new tensor of zeros on the GPU.

type Picture

type Picture struct {
	// contains filtered or unexported fields
}

Picture is an 8-bit 4:2:0 picture's planes on the GPU.

func (*Picture) Width

func (p *Picture) Width() int

Width is the picture's.

type Planes added in v0.15.1

type Planes struct {
	W, H, Pitch int
	Depth       int
	// contains filtered or unexported fields
}

Planes is a decoded picture on the GPU as the decoders give it: NV12, or P010 when Depth is over 8, Y's H rows then the interleaved chroma's H/2, Pitch bytes apart.

func (*Planes) Size added in v0.15.1

func (p *Planes) Size() int

Size is the bytes of the picture's planes, as DownloadPlanes gives them.

type Tape added in v0.19.0

type Tape struct {
	*GPU
	// contains filtered or unexported fields
}

A Tape records a forward pass on a GPU, op by op, to run it backwards for the gradients at the weights it is told are learnt (Param). Its ops are the GPU's, recorded, so a network written against an interface they satisfy runs on the GPU as it is for inference and on a Tape for learning. Two differences: the ops that are in place on a backend (ReLU, Add) give new tensors here and leave their operands as they were, so a network must use what they give; and the gradients are single precision whatever the GPU's Half says. An op none of whose inputs is on a path from a learnt weight is not recorded. The tensors are the GPU's: released at its Release, with the gradients.

func NewTape added in v0.19.0

func NewTape(g *GPU) *Tape

NewTape is a tape of g's ops.

func (*Tape) Add added in v0.19.0

func (tp *Tape) Add(a, b *Tensor) *Tensor

Add is the GPU's on a copy of a: a stays as it was.

func (*Tape) AddScaled added in v0.19.0

func (tp *Tape) AddScaled(x, y, s *Tensor) *Tensor

AddScaled is the GPU's on a copy of x: x + y s, x as it was.

func (*Tape) Attention added in v0.19.0

func (tp *Tape) Attention(q, k, v *Tensor, heads int, scale float32) *Tensor

Attention is the GPU's, recorded.

func (*Tape) Backward added in v0.19.0

func (tp *Tape) Backward(out, dy *Tensor)

Backward runs the pass backwards from out, dy the loss's gradient there (the GPU's tensor, out's shape).

func (*Tape) Bilinear added in v0.19.0

func (tp *Tape) Bilinear(x *Tensor, oh, ow int) *Tensor

Bilinear is the GPU's, recorded.

func (*Tape) Clone added in v0.19.0

func (tp *Tape) Clone(x *Tensor) *Tensor

Clone is the GPU's, recorded.

func (*Tape) Conv2d added in v0.19.0

func (tp *Tape) Conv2d(x, w, b *Tensor, stride, pad int) *Tensor

Conv2d is the GPU's, recorded.

func (*Tape) ConvTranspose2dK added in v0.19.0

func (tp *Tape) ConvTranspose2dK(x, w, b *Tensor) *Tensor

ConvTranspose2dK is the GPU's, recorded.

func (*Tape) GEGLU added in v0.19.0

func (tp *Tape) GEGLU(x *Tensor) *Tensor

GEGLU is the GPU's, recorded.

func (*Tape) GELU added in v0.19.0

func (tp *Tape) GELU(x *Tensor) *Tensor

GELU is the GPU's on a copy of x: x stays as it was.

func (*Tape) Grad added in v0.19.0

func (tp *Tape) Grad(t *Tensor) *Tensor

Grad is the gradient at a tensor of the pass after Backward (nil when none reached it), in the tensor's shape.

func (*Tape) Grads added in v0.19.0

func (tp *Tape) Grads() map[string]*Tensor

Grads are the learnt weights' gradients, by name, after Backward: one the pass never reached is absent.

func (*Tape) GroupNorm added in v0.19.0

func (tp *Tape) GroupNorm(x *Tensor, groups int, w, b *Tensor, eps float32) *Tensor

GroupNorm is the GPU's, recorded.

func (*Tape) LayerNorm added in v0.19.0

func (tp *Tape) LayerNorm(x, w, b *Tensor, eps float32) *Tensor

LayerNorm is the GPU's, recorded.

func (*Tape) Linear added in v0.19.0

func (tp *Tape) Linear(x, w, b *Tensor) *Tensor

Linear is the GPU's, recorded.

func (*Tape) Param added in v0.19.0

func (tp *Tape) Param(name string, w *Tensor) *Tensor

Param uploads a learnt weight: its gradient is gathered under name (Grads).

func (*Tape) ReLU added in v0.19.0

func (tp *Tape) ReLU(x *Tensor) *Tensor

ReLU is the GPU's on a copy: x stays as it was.

func (*Tape) Rows added in v0.19.0

func (tp *Tape) Rows(x *Tensor, from int) *Tensor

Rows is the GPU's view, recorded: the gradient at the view lands in the rows from on of the gradient at x.

func (*Tape) Split3 added in v0.19.0

func (tp *Tape) Split3(x *Tensor) (q, k, v *Tensor)

Split3 is the GPU's, recorded.

func (*Tape) TemporalAttention added in v0.19.0

func (tp *Tape) TemporalAttention(q, peQ, peK, peV *Tensor, ks, vs []*Tensor, heads int, scale float32) *Tensor

TemporalAttention is the GPU's, recorded: the gradients reach the query, the position encodings, and the keys and values of the frames on a path from a learnt weight.

func (*Tape) Tokens added in v0.19.0

func (tp *Tape) Tokens(patches, cls, pos *Tensor) *Tensor

Tokens is the GPU's, recorded for the patches (the class token and the position embedding get no gradient: they are not learnt).

func (*Tape) Transpose added in v0.19.0

func (tp *Tape) Transpose(x *Tensor) *Tensor

Transpose is the GPU's, recorded.

func (*Tape) Watch added in v0.19.0

func (tp *Tape) Watch(t *Tensor) *Tensor

Watch asks for the gradient at a tensor of the GPU's (Grad), as at a learnt weight.

type Taps added in v0.15.1

type Taps struct {
	N int
	// contains filtered or unexported fields
}

Taps is one dimension of a resampling filter, kept on the GPU: for each output sample, the first input sample it reads and the weights of the N it reads from there.

type Tensor

type Tensor struct {
	Shape []int
	Data  []float32
	// contains filtered or unexported fields
}

Tensor is a dense float32 array of a shape: in Data, or on a GPU (a GPU backend's tensors; Data nil).

func Add

func Add(a, b *Tensor) *Tensor

Add adds b to a, in place (the same shapes).

func AddScaledBackward added in v0.19.0

func AddScaledBackward(y, s, dy *Tensor) (dyIn, ds *Tensor)

AddScaledBackward is the gradients of AddScaled(x, y, s) = x + y s (s a channel's) given dy: at y and at s (at x it is dy itself).

func Attention

func Attention(q, k, v *Tensor, heads int, scale float32) *Tensor

Attention is multi-head scaled dot product attention: q [Nq, D], k and v [Nk, D], the D channels in heads of D/heads, each head's softmax(q k^T scale) v; out [Nq, D]. Each head's keys and values are packed once; the jobs are a head's 64 query rows each: their scores, softmax and output.

func AttentionBackward added in v0.19.0

func AttentionBackward(q, k, v *Tensor, heads int, scale float32, dy *Tensor) (dq, dk, dv *Tensor)

AttentionBackward is the gradients of Attention(q, k, v, heads, scale) given dy: at q, at k and at v.

func BicubicScale

func BicubicScale(x *Tensor, sy, sx float64) *Tensor

BicubicScale resizes x [C, H, W] by scale factors (F.interpolate mode "bicubic", align_corners false, a scale factor given): the output floor(H sy) x floor(W sx), each output sampled at (o + 0.5)/s - 0.5 with the cubic convolution kernel (A = -0.75), the edges repeated.

func Bilinear

func Bilinear(x *Tensor, oh, ow int) *Tensor

Bilinear resizes x [C, H, W] to oh x ow, align_corners true (F.interpolate mode "bilinear", align_corners=True).

func Conv2d

func Conv2d(x, w, b *Tensor, stride, pad int) *Tensor

Conv2d is nn.Conv2d on x [C, H, W] with w [O, C, kh, kw] and b [O] (nil for none), the stride and padding the same in both directions. A 1x1 convolution is a matrix product as it is; any other is done in bands of output rows, each band's patches gathered as columns ([C kh kw, the band's outputs]) and multiplied by w, so no buffer holds them all.

func Conv2dBackward

func Conv2dBackward(x, w, dy *Tensor, stride, pad int) (dx, dw, db *Tensor)

Conv2dBackward is the gradients of Conv2d(x, w, b, stride, pad) given dy, the gradient at its output: at x, at w and at b.

func ConvTranspose2dK

func ConvTranspose2dK(x, w, b *Tensor) *Tensor

ConvTranspose2dK is nn.ConvTranspose2d on x [C, H, W] for a kernel as large as its stride and no padding (each input value spreads to its own k x k outputs): w [C, O, k, k], b [O].

func ConvTranspose2dKBackward

func ConvTranspose2dKBackward(x, w, dy *Tensor) (dx, dw, db *Tensor)

ConvTranspose2dKBackward is the gradients of ConvTranspose2dK(x, w, b) given dy, the gradient at its output: at x, at w and at b.

func GEGLUBackward added in v0.19.0

func GEGLUBackward(x, dy *Tensor) *Tensor

GEGLUBackward is the gradient of GEGLU(x) at x given dy.

func GELU

func GELU(x *Tensor) *Tensor

GELU is the exact GELU, in place: x Φ(x).

func GELUBackward added in v0.19.0

func GELUBackward(x, dy *Tensor) *Tensor

GELUBackward is the gradient of GELU at its input x given dy.

func GroupNorm

func GroupNorm(x *Tensor, groups int, w, b *Tensor, eps float32) *Tensor

GroupNorm is nn.GroupNorm on x [C, ...]: the channels in groups, each normalised over its channels and positions, then w and b per channel.

func GroupNormBackward added in v0.19.0

func GroupNormBackward(x *Tensor, groups int, w, dy *Tensor, eps float32) (dx, dw, db *Tensor)

GroupNormBackward is the gradients of GroupNorm(x, groups, w, b, eps) given dy: at x, at w and at b.

func LayerNorm

func LayerNorm(x, w, b *Tensor, eps float32) *Tensor

LayerNorm normalises x's last dimension (nn.LayerNorm), w and b its affine parameters.

func LayerNormBackward added in v0.19.0

func LayerNormBackward(x, w, dy *Tensor, eps float32) (dx, dw, db *Tensor)

LayerNormBackward is the gradients of LayerNorm(x, w, b, eps) given dy: at x, at w and at b.

func Linear

func Linear(x, w, b *Tensor) *Tensor

Linear is x [N, in] times w [out, in] transposed, plus b [out] (nil for none): nn.Linear.

func LinearBackward added in v0.19.0

func LinearBackward(x, w, dy *Tensor) (dx, dw, db *Tensor)

LinearBackward is the gradients of Linear(x, w, b) given dy, the gradient at its output: at x (x's shape), at w and at b.

func New

func New(shape ...int) *Tensor

New is a zeroed tensor of a shape.

func ReLU

func ReLU(x *Tensor) *Tensor

ReLU is max(x, 0), in place.

func ReLUBackward

func ReLUBackward(y, dy *Tensor) *Tensor

ReLUBackward zeroes dy, in place, where ReLU's output y is zero.

func (*Tensor) Clone

func (t *Tensor) Clone() *Tensor

Clone is a copy of the tensor (in memory: a GPU tensor's is its backend's Clone).

func (*Tensor) Dim

func (t *Tensor) Dim(i int) int

Dim is the size of dimension i (negative from the end).

func (*Tensor) Reshape

func (t *Tensor) Reshape(shape ...int) *Tensor

Reshape is the tensor seen with another shape of as many elements (one dimension may be -1, inferred). The data is shared.

func (*Tensor) String

func (t *Tensor) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL