Documentation
¶
Overview ¶
Package shortconv implements static causal depthwise convolution over BF16 input rows on the host. A kernel contains immutable taps. Each independent sequence has its own state, which stores a fixed number of recent inputs.
Taps are channel-major, with the oldest input first and the current input last. Inputs and history are row-major. State starts with zero padding unless initialized with preceding inputs.
This package is outside the compatibility promise of github.com/tmc/lib/nvidia. Its API may change or be removed in patch releases.
Example ¶
package main
import (
"fmt"
"log"
"github.com/tmc/lib/nvidia/x/cuda"
"github.com/tmc/lib/nvidia/x/nn/shortconv"
)
func main() {
weights := []uint16{cuda.BF16bits(2), cuda.BF16bits(1)}
k, err := shortconv.New(shortconv.Config{Channels: 1, Taps: 2, Scale: 1}, weights)
if err != nil {
log.Fatal(err)
}
s, err := k.NewState(nil)
if err != nil {
log.Fatal(err)
}
y, err := s.Forward([]uint16{cuda.BF16bits(3), cuda.BF16bits(4)})
if err != nil {
log.Fatal(err)
}
fmt.Println(cuda.BF16frombits(y[0]), cuda.BF16frombits(y[1]), cuda.BF16frombits(s.History()[0]))
}
Output: 3 10 4
Index ¶
Examples ¶
Constants ¶
const DeviceEntry = "shortconv_bf16"
DeviceEntry is the entry point in DeviceSource.
Variables ¶
var DeviceSource string
DeviceSource is CUDA C++ for a one-row convolution. Compile without flush-to-zero. Its scalar arguments are channels, taps and residual, all uint32, followed by an FP32 scale. Residual is 0 or 1.
The first five arguments are device pointers to BF16 words in this order: input, channel-major weights, oldest-first history, output, updated history. Input and output each have Channels words, and the other lengths are given by DeviceLayout. Callers keep output and updated history disjoint from each other and from all inputs. Empty histories may use null pointers. The kernel publishes a new history without modifying the old one. After completion, the caller may swap the two history buffers for the next row on the same stream.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Channels int
Taps int
// Residual adds the current input after accumulating the weighted taps.
Residual bool
// Scale multiplies the result before rounding to BF16. Use 1 for identity.
Scale float32
}
Config describes a static depthwise convolution over BF16 inputs.
type DeviceShape ¶
DeviceShape describes the one-dimensional CUDA launch and buffer extents in BF16 words. Compile DeviceSource and launch DeviceEntry with its Blocks and Threads, leaving both other dimensions at 1.
func DeviceLayout ¶
func DeviceLayout(channels, taps int) (DeviceShape, error)
DeviceLayout computes launch geometry and exact weight and history lengths for the CUDA ABI. The kernel accepts any positive dimensions that fit.
Example ¶
package main
import (
"fmt"
"log"
"github.com/tmc/lib/nvidia/x/nn/shortconv"
)
func main() {
shape, err := shortconv.DeviceLayout(129, 3)
if err != nil {
log.Fatal(err)
}
fmt.Println(shortconv.DeviceEntry, shape.Blocks, shape.Threads, shape.HistoryWords)
}
Output: shortconv_bf16 2 128 258
type Kernel ¶
type Kernel struct {
// contains filtered or unexported fields
}
Kernel holds immutable channel-major taps and may be shared by independent sequences. Construct a kernel with New.
type State ¶
type State struct {
// contains filtered or unexported fields
}
State owns the convolution history of one sequence. Construct it with Kernel.NewState. A state must not be copied or used concurrently.
func (*State) Forward ¶
Forward consumes complete BF16 input rows following the current history. It returns new BF16 output rows and advances history only on success. An empty input leaves history unchanged.
Each tap uses a fused multiply-add rounded to FP32. The optional residual addition and the scale multiplication each round separately to FP32. The output rounds to nearest even in BF16.