errboundcuda

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: 16 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 errboundcuda runs CUDA kernels as errbound.Kernel values, on a GPU through NVRTC or on the host CPU through cudahostemu, so that errbound.Check can certify them.

A Launch gives the kernel source, its entry point and launch shape, and binds each kernel argument to a graph input (Input, InputBF16), a graph output (Output), a scalar (Int, Float) or a null pointer (Null).

This package is outside the compatibility promise of github.com/tmc/lib/nvidia. Its API may change or be removed in patch releases.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Arg

type Arg struct {
	// contains filtered or unexported fields
}

An Arg is one kernel argument.

func Float

func Float(v float32) Arg

Float passes a float scalar.

func Input

func Input(i int) Arg

Input passes graph input i as a float32 buffer.

func InputBF16

func InputBF16(i int) Arg

InputBF16 passes graph input i as a buffer of bfloat16 bit patterns (unsigned short). The input's values must be representable in bfloat16.

func Int

func Int(v int) Arg

Int passes an int scalar.

func Null

func Null() Arg

Null passes a null pointer, for an optional buffer argument.

func Output

func Output(i int) Arg

Output passes graph output i as a float32 buffer.

func OutputBF16

func OutputBF16(i int) Arg

OutputBF16 passes graph output i as a buffer of bfloat16 bit patterns (unsigned short), for a kernel declared to write bfloat16.

Example
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/tmc/lib/nvidia/x/obs/diag/errbound"
	"github.com/tmc/lib/nvidia/x/obs/diag/errbound/errboundcuda"
)

func main() {
	// A kernel declared to read and write bfloat16: y = 2x.
	g := errbound.NewGraph()
	x := g.Input("x", 64)
	g.Output(x.Mul(g.Const(2)))
	l := &errboundcuda.Launch{
		Source: `extern "C" __global__ void twice(const unsigned short* x, unsigned short* y, int n) {
    int i = threadIdx.x;
    if (i < n) y[i] = (unsigned short)(__float_as_uint(2.0f * __uint_as_float((unsigned)x[i] << 16)) >> 16);
}`,
		Entry: "twice",
		Block: [3]int{64},
		Args:  []errboundcuda.Arg{errboundcuda.InputBF16(0), errboundcuda.OutputBF16(0), errboundcuda.Int(64)},
	}
	dir, err := os.MkdirTemp("", "errboundcuda")
	if err != nil {
		log.Fatal(err)
	}
	defer os.RemoveAll(dir)
	k, err := l.Host(context.Background(), dir, g)
	if err != nil {
		log.Fatal(err)
	}
	rep, err := errbound.Check(context.Background(), errbound.Task{Name: "twice", Graph: g, Precision: errbound.BF16}, k, nil)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(rep.Verdict)
}
Output:
accept

type Launch

type Launch struct {
	Source string // CUDA C++ source
	Entry  string // extern "C" __global__ function name

	Grid, Block [3]int // launch shape; zero dimensions count as 1
	Shared      int    // dynamic shared memory bytes

	Args []Arg
}

A Launch describes how to run one CUDA kernel on a graph's inputs: its source, entry point, launch shape and argument list.

func (*Launch) Device

func (l *Launch) Device(dev *cuda.Device, g *errbound.Graph) (errbound.Kernel, func() error, error)

Device compiles l with NVRTC for dev and returns a kernel that runs it on the GPU for graph g, and a function that frees its device memory. The device buffers live across calls, as a caching allocator's do, and each call uploads the output buffers as the caller passes them, so a kernel that does not write its output returns that.

Example
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/tmc/lib/nvidia/x/cuda"
	"github.com/tmc/lib/nvidia/x/obs/diag/errbound"
	"github.com/tmc/lib/nvidia/x/obs/diag/errbound/errboundcuda"
)

func main() {
	dev, err := cuda.NewDevice(0)
	if err != nil {
		fmt.Println("no GPU")
		return
	}
	defer dev.Close()
	g := errbound.NewGraph()
	g.Output(g.Input("x", 256).Exp())
	l := &errboundcuda.Launch{
		Source: `extern "C" __global__ void expk(const float* x, float* y) { y[threadIdx.x] = expf(x[threadIdx.x]); }`,
		Entry:  "expk",
		Block:  [3]int{256},
		Args:   []errboundcuda.Arg{errboundcuda.Input(0), errboundcuda.Output(0)},
	}
	k, free, err := l.Device(dev, g)
	if err != nil {
		log.Fatal(err)
	}
	defer free()
	rep, err := errbound.Check(context.Background(), errbound.Task{Name: "exp", Graph: g, Precision: errbound.FP32}, k, nil)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(rep.Verdict)
}

func (*Launch) Host

func (l *Launch) Host(ctx context.Context, dir string, g *errbound.Graph) (errbound.Kernel, error)

Host compiles l once with cudahostemu in dir, a scratch directory, and returns a kernel that runs it on the host CPU for graph g. Each call passes the output buffers in as the caller left them, so a kernel that does not write its output returns that.

Example
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/tmc/lib/nvidia/x/obs/diag/errbound"
	"github.com/tmc/lib/nvidia/x/obs/diag/errbound/errboundcuda"
)

func main() {
	// The reference: y = exp(x) over 128 elements.
	g := errbound.NewGraph()
	x := g.Input("x", 128)
	g.Output(x.Exp())

	l := &errboundcuda.Launch{
		Source: `extern "C" __global__ void expk(const float* x, float* y, int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) y[i] = __expf(x[i]);
}`,
		Entry: "expk",
		Grid:  [3]int{1},
		Block: [3]int{128},
		Args:  []errboundcuda.Arg{errboundcuda.Input(0), errboundcuda.Output(0), errboundcuda.Int(128)},
	}
	dir, err := os.MkdirTemp("", "errboundcuda")
	if err != nil {
		log.Fatal(err)
	}
	defer os.RemoveAll(dir)
	k, err := l.Host(context.Background(), dir, g)
	if err != nil {
		log.Fatal(err)
	}
	rep, err := errbound.Check(context.Background(), errbound.Task{Name: "exp", Graph: g, Precision: errbound.FP32}, k, nil)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(rep.Verdict)
}
Output:
accept

Jump to

Keyboard shortcuts

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