shortconv

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 5 Imported by: ? 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 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

View Source
const DeviceEntry = "shortconv_bf16"

DeviceEntry is the entry point in DeviceSource.

Variables

View Source
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

type DeviceShape struct {
	Blocks, Threads           uint32
	WeightWords, HistoryWords int
}

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.

func New

func New(cfg Config, weights []uint16) (*Kernel, error)

New constructs a kernel and copies weights. Each channel has cfg.Taps BF16 words, ordered from the oldest input to the current input.

func (*Kernel) NewState

func (k *Kernel) NewState(history []uint16) (*State, error)

NewState constructs an independent state. A nil history starts with zero padding. A non-nil history supplies exactly Taps-1 complete BF16 input rows, oldest first, and is copied.

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

func (s *State) Forward(src []uint16) ([]uint16, error)

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.

func (*State) History

func (s *State) History() []uint16

History returns a copy of the retained BF16 inputs, in row order from oldest to newest. Output values are never stored in the history.

Jump to

Keyboard shortcuts

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