activations

package
v0.1.33 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 11 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

Index

Constants

View Source
const DefaultMinParallelChunk = 32768

DefaultMinParallelChunk is the default minimum number of elements per parallel worker chunk.

View Source
const PriorityNoSIMD = gobackend.PriorityGeneric

Variables

View Source
var (
	// NoSIMDSimpleMetadata holds metadata for lightweight element-wise non-SIMD kernels
	// (e.g., Relu, LeakyRelu, HardSigmoid, HardSwish).
	NoSIMDSimpleMetadata = KernelMetadata{MinParallelChunk: 8192}

	// NoSIMDTranscendentalMetadata holds metadata for transcendental non-SIMD kernels
	// (e.g., Gelu, Sigmoid, Tanh, Silu, Selu, SwiGLU) that call math.Erf / math.Exp / math.Tanh.
	NoSIMDTranscendentalMetadata = KernelMetadata{MinParallelChunk: 2048}
)
View Source
var DefaultKernelMetadata = KernelMetadata{
	MinParallelChunk: DefaultMinParallelChunk,
}

DefaultKernelMetadata is the default metadata used when nil is passed to Register*.

Functions

func Apply

func Apply[T gotype.Supported](backend *gobackend.Backend, act compute.ActivationType, data []T)

Apply applies the given activation function in-place across the slice. If the slice is large and workers are available, work is parallelized in chunks.

func ApplyBiasTileNoSIMD added in v0.1.26

func ApplyBiasTileNoSIMD[T float32 | float64](actFn KernelFn[T], out []T, rowStride, numRows, width int, bias []T)

ApplyBiasTileNoSIMD is the reference scalar implementation of BiasTileFn using actFn.

func Execute

func Execute[T gotype.Supported](backend *gobackend.Backend, act compute.ActivationType, in, out []T)

Execute applies the given activation function from in to out (in and out can be the same slice).

func ExecuteSwiGLU

func ExecuteSwiGLU[T gotype.Supported](backend *gobackend.Backend, in, out []T, numRows, hiddenDim int)

ExecuteSwiGLU applies SwiGLU activation across numRows rows of width 2*hiddenDim to out of width hiddenDim.

func ExecuteSwiGLUVJP

func ExecuteSwiGLUVJP[T gotype.Supported](backend *gobackend.Backend, x, dOutput, dx []T, numRows, hiddenDim int)

ExecuteSwiGLUVJP applies SwiGLU VJP across numRows rows.

func ExecuteVJP

func ExecuteVJP[T gotype.Supported](backend *gobackend.Backend, act compute.ActivationType, y, x, dOutput, dx []T)

ExecuteVJP applies the VJP of the given activation function:

dx = dOutput * f'(y or x)

Note: For activations where act.VJPRequiresInput() == false (e.g. Relu, Sigmoid, HardSigmoid, LeakyRelu, Selu, Tanh, None), the parameter x is optional (can be nil or empty), and the calculation will use only y.

func ExecuteVJPFromOutput

func ExecuteVJPFromOutput[T gotype.Supported](backend *gobackend.Backend, act compute.ActivationType, y, dOutput, dz []T) error

ExecuteVJPFromOutput applies the VJP of an activation that only depends on the forward output y:

dz = dOutput * f'(y)

It requires that act.VJPRequiresInput() == false. dz and dOutput can be the same slice (in-place).

func GeluApproxNoSIMD

func GeluApproxNoSIMD[T float32 | float64](in, out []T)

func GeluExactNoSIMD added in v0.1.23

func GeluExactNoSIMD[T float32 | float64](in, out []T)

func GetKernelWithMetadata added in v0.1.17

func GetKernelWithMetadata[T gotype.Supported](act compute.ActivationType) (KernelFn[T], *KernelMetadata)

GetKernelWithMetadata returns the registered kernel function and its metadata.

func GetSwiGLUVJPWithMetadata added in v0.1.17

func GetSwiGLUVJPWithMetadata[T gotype.Supported]() (SwiGLUVJPFn[T], *KernelMetadata)

GetSwiGLUVJPWithMetadata returns the registered SwiGLU VJP function and its metadata for data type T.

func GetSwiGLUWithMetadata added in v0.1.17

func GetSwiGLUWithMetadata[T gotype.Supported]() (SwiGLUFn[T], *KernelMetadata)

GetSwiGLUWithMetadata returns the registered SwiGLU function and its metadata for data type T.

func GetVJPKernelWithMetadata added in v0.1.17

func GetVJPKernelWithMetadata[T gotype.Supported](act compute.ActivationType) (VJPKernelFn[T], *KernelMetadata)

GetVJPKernelWithMetadata returns the registered VJP kernel function and its metadata.

func GetWithMetadata added in v0.1.17

func GetWithMetadata[T gotype.Supported](act compute.ActivationType) (InPlaceFn[T], *KernelMetadata)

GetWithMetadata returns the registered in-place activation function and its metadata. Returns (nil, nil) if activation is ActivationNone or if no implementation is registered.

func HardSigmoidNoSIMD

func HardSigmoidNoSIMD[T float32 | float64](in, out []T)

func HardSwishNoSIMD

func HardSwishNoSIMD[T float32 | float64](in, out []T)

func LeakyReluNoSIMD

func LeakyReluNoSIMD[T float32 | float64](in, out []T)

func Register

func Register[T gotype.Supported](name string, act compute.ActivationType, fn InPlaceFn[T], priority gobackend.RegisterPriority, meta *KernelMetadata)

Register registers an in-place activation function for a specific activation type and data type. If an implementation is already registered with a higher priority, this call is ignored.

func RegisterBiasTile added in v0.1.25

func RegisterBiasTile[T gotype.Supported](name string, act compute.ActivationType, fn BiasTileFn[T], priority gobackend.RegisterPriority)

RegisterBiasTile registers a fused bias + activation 2D tile kernel for a specific activation type and data type.

func RegisterKernel

func RegisterKernel[T gotype.Supported](name string, act compute.ActivationType, fn KernelFn[T], priority gobackend.RegisterPriority, meta *KernelMetadata)

RegisterKernel registers a kernel function (supporting both in-place and out-of-place).

func RegisterSwiGLU

func RegisterSwiGLU[T gotype.Supported](name string, fn SwiGLUFn[T], priority gobackend.RegisterPriority, meta *KernelMetadata)

RegisterSwiGLU registers a SwiGLU kernel for a specific data type.

func RegisterSwiGLUVJP

func RegisterSwiGLUVJP[T gotype.Supported](name string, fn SwiGLUVJPFn[T], priority gobackend.RegisterPriority, meta *KernelMetadata)

RegisterSwiGLUVJP registers a SwiGLU VJP kernel for a specific data type.

func RegisterVJPKernel

func RegisterVJPKernel[T gotype.Supported](name string, act compute.ActivationType, fn VJPKernelFn[T], priority gobackend.RegisterPriority, meta *KernelMetadata)

RegisterVJPKernel registers a VJP kernel function for a specific activation type and data type.

func ReluBF16NoSIMD

func ReluBF16NoSIMD(in, out []bfloat16.BFloat16)

func ReluF16NoSIMD

func ReluF16NoSIMD(in, out []float16.Float16)

func ReluNoSIMD

func ReluNoSIMD[T float32 | float64](in, out []T)

func SeluNoSIMD

func SeluNoSIMD[T float32 | float64](in, out []T)

func SigmoidNoSIMD

func SigmoidNoSIMD[T float32 | float64](in, out []T)

func SiluBF16NoSIMD

func SiluBF16NoSIMD(in, out []bfloat16.BFloat16)

func SiluF16NoSIMD

func SiluF16NoSIMD(in, out []float16.Float16)

func SiluNoSIMD

func SiluNoSIMD[T float32 | float64](in, out []T)

func TanhNoSIMD

func TanhNoSIMD[T float32 | float64](in, out []T)

func VJPSiluNoSIMD added in v0.1.26

func VJPSiluNoSIMD[T float32 | float64](y, x, dOutput, dx []T)

Types

type BiasTileFn added in v0.1.25

type BiasTileFn[T any] func(out []T, rowStride, numRows, width int, bias []T)

BiasTileFn applies bias addition and activation in-place to a 2D sub-tile of output rows:

for r := 0; r < numRows; r++ {
    for c := 0; c < width; c++ {
        out[r*rowStride + c] = act(out[r*rowStride + c] + bias[c])
    }
}

out must have length at least (numRows-1)*rowStride + width, and bias must have length at least width.

func GetBiasTile added in v0.1.25

func GetBiasTile[T gotype.Supported](act compute.ActivationType) BiasTileFn[T]

GetBiasTile returns the registered fused bias + activation 2D tile kernel for the given activation type and data type T. Returns nil if none is registered.

type InPlaceFn

type InPlaceFn[T any] func(data []T)

InPlaceFn applies an activation function in-place to a contiguous slice of elements.

func Get

Get returns the registered in-place activation function for the given activation type and data type T. Returns nil if activation is ActivationNone or if no implementation is registered.

type KernelFn

type KernelFn[T any] func(in, out []T)

KernelFn applies an activation function from in to out (in and out can be the same slice).

func GetKernel

func GetKernel[T gotype.Supported](act compute.ActivationType) KernelFn[T]

GetKernel returns the registered kernel function for the given activation type and data type T.

type KernelMetadata added in v0.1.17

type KernelMetadata struct {
	// MinParallelChunk is the minimum number of elements per parallel worker chunk.
	// If <= 0, DefaultMinParallelChunk is used.
	MinParallelChunk int
}

KernelMetadata holds properties and platform-tunable parameters for a registered activation kernel. Because registrations store a *KernelMetadata pointer, package-level metadata structs can be initialized or adjusted at runtime for the target platform without re-registering kernels.

func (*KernelMetadata) EffectiveMinParallelChunk added in v0.1.17

func (m *KernelMetadata) EffectiveMinParallelChunk() int

EffectiveMinParallelChunk returns MinParallelChunk if positive, or DefaultMinParallelChunk.

type SwiGLUFn

type SwiGLUFn[T any] func(in, out []T, numRows, hiddenDim int)

SwiGLUFn applies SwiGLU activation: out[m*H + j] = silu(gate[m*2H + j]) * val[m*2H + H + j].

func GetSwiGLU

func GetSwiGLU[T gotype.Supported]() SwiGLUFn[T]

GetSwiGLU returns the registered SwiGLU function for data type T.

type SwiGLUVJPFn

type SwiGLUVJPFn[T any] func(x, dOutput, dx []T, numRows, hiddenDim int)

SwiGLUVJPFn applies SwiGLU VJP: x: input of shape [numRows, 2*hiddenDim] dOutput: adjoint gradient of shape [numRows, hiddenDim] dx: resulting gradient of shape [numRows, 2*hiddenDim]

func GetSwiGLUVJP

func GetSwiGLUVJP[T gotype.Supported]() SwiGLUVJPFn[T]

GetSwiGLUVJP returns the registered SwiGLU VJP function for data type T.

func MakeBF16SwiGLUVJPFromF32

func MakeBF16SwiGLUVJPFromF32(f32SwiGLUVJP SwiGLUVJPFn[float32]) SwiGLUVJPFn[bfloat16.BFloat16]

MakeBF16SwiGLUVJPFromF32 wraps a float32 SwiGLU VJP kernel for BFloat16.

func MakeF16SwiGLUVJPFromF32

func MakeF16SwiGLUVJPFromF32(f32SwiGLUVJP SwiGLUVJPFn[float32]) SwiGLUVJPFn[float16.Float16]

MakeF16SwiGLUVJPFromF32 wraps a float32 SwiGLU VJP kernel for Float16.

type VJPKernelFn

type VJPKernelFn[T any] func(y, x, dOutput, dx []T)

VJPKernelFn applies the VJP of an activation: dx = dOutput * f'(y or x). Note: y and/or x may be empty/nil depending on whether the activation requires x.

func GetVJPKernel

func GetVJPKernel[T gotype.Supported](act compute.ActivationType) VJPKernelFn[T]

GetVJPKernel returns the registered VJP kernel function for the given activation type and data type T.

func MakeBF16VJPKernelFromF32

func MakeBF16VJPKernelFromF32(f32Kernel VJPKernelFn[float32]) VJPKernelFn[bfloat16.BFloat16]

MakeBF16VJPKernelFromF32 wraps a float32 VJP kernel into a BFloat16 VJP kernel using chunked conversion.

func MakeF16VJPKernelFromF32

func MakeF16VJPKernelFromF32(f32Kernel VJPKernelFn[float32]) VJPKernelFn[float16.Float16]

MakeF16VJPKernelFromF32 wraps a float32 VJP kernel into a Float16 VJP kernel using chunked conversion.

Directories

Path Synopsis
Package avx2 provides AVX2 SIMD implementations for activations.
Package avx2 provides AVX2 SIMD implementations for activations.
Package avx512 provides AVX-512 SIMD implementations for activations.
Package avx512 provides AVX-512 SIMD implementations for activations.
Package neon provides ARM64 AdvSIMD (NEON) implementations for activations.
Package neon provides ARM64 AdvSIMD (NEON) implementations for activations.

Jump to

Keyboard shortcuts

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