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 InputBF16 ¶
InputBF16 passes graph input i as a buffer of bfloat16 bit patterns (unsigned short). The input's values must be representable in bfloat16.
func OutputBF16 ¶
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
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 ¶
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)
}
Output:
func (*Launch) Host ¶
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