Help us shape the future of Go! Take the 2026 Go Developer Survey

autodiff

package
v0.9.24 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package autodiff provides automatic differentiation capabilities.

This package implements reverse-mode automatic differentiation (backpropagation) using a gradient tape. It wraps any backend to add autodiff capabilities.

Example:

import (
    "github.com/born-ml/born/autodiff"
    "github.com/born-ml/born/backend/cpu"
    "github.com/born-ml/born/tensor"
)

func main() {
    // Wrap CPU backend with autodiff
    base := cpu.New()
    backend := autodiff.New(base)

    // Use for training
    x := tensor.Zeros[float32](tensor.Shape{2, 3}, backend)
    y := x.Add(x)  // Operations recorded on tape

    // Compute gradients
    grads := backend.Backward(y.Raw())
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Backward

func Backward[T tensor.DType, B BackwardCapable](t *tensor.Tensor[T, B], backend B) map[*tensor.RawTensor]*tensor.RawTensor

Backward computes gradients via backpropagation.

func NoGrad added in v0.3.0

func NoGrad[B tensor.Backend](backend *Backend[B], fn func())

NoGrad disables gradient recording for operations within the function. Use this for inference or operations that shouldn't be differentiated.

Example:

backend := autodiff.New(cpu.New())
autodiff.NoGrad(backend, func() {
    // Operations here won't be recorded for gradients
    output := model.Forward(input)
})

func ReleaseGradients added in v0.9.1

func ReleaseGradients(grads map[*tensor.RawTensor]*tensor.RawTensor, backend ...tensor.Backend)

ReleaseGradients releases GPU buffers for all gradient tensors in the map. Call after optimizer.Step(grads) to free GPU memory immediately. Pass the backend so the releaser can schedule GPU buffer deferred destruction. Without a backend the function is a no-op (backwards-compatible default).

Types

type Backend

type Backend[B tensor.Backend] = autodiff.AutodiffBackend[B]

Backend is the autodiff-enabled backend.

func New

func New[B tensor.Backend](backend B) *Backend[B]

New creates a new autodiff backend wrapping the given backend.

Example:

base := cpu.New()
backend := autodiff.New(base)

type BackwardCapable

type BackwardCapable = autodiff.BackwardCapable

BackwardCapable interface for backends that support backpropagation.

type GradientTape

type GradientTape = autodiff.GradientTape

GradientTape records operations for automatic differentiation.

func NewGradientTape

func NewGradientTape() *GradientTape

NewGradientTape creates a new gradient tape.

Jump to

Keyboard shortcuts

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