Welvet examples

30. runtime/backward

Open original example ↗Recorded results · not a live execution

Part: IV · Runtime
Package: github.com/openfluke/welvet/runtime/backward
Status: ok — ✅

When

Walking a Grid/Stack with the shared runtime/backward path.

Where

import "github.com/openfluke/welvet/runtime/backward"

cd 30-backward && source ../env.sh && go run .

Why

Training needs a reverse tape over the same ops forward used — no separate graph framework.

What

Backward[T](fwdResult, gradOut) walks the tape and returns per-op weight grads.

Sample output (captured)

true <nil>

Live capture from go run ./cmd/runall on local ../../welvet (exit 0).

Source

Copied from the Welvet feature book examples (openfluke.github.io/welvet/examples/30-backward).

main.go

Download source ↓
package main

import (
	"fmt"

	"github.com/openfluke/welvet/architecture"
	"github.com/openfluke/welvet/core"
	"github.com/openfluke/welvet/layers/dense"
	"github.com/openfluke/welvet/runtime/backward"
	"github.com/openfluke/welvet/runtime/forward"
)

func main() {
	g := architecture.NewGrid(1, 1, 1, 1)
	l, _ := dense.New(4, 4, core.ActivationLinear, core.DTypeFloat32)
	_ = dense.Place(g, 0, 0, 0, 0, l)
	fwd, _ := forward.Forward(g, core.NewTensor[float32](1, 4))
	bwd, err := backward.Backward(fwd, core.NewTensor[float32](1, 4))
	fmt.Println(bwd != nil, err)
}