launchplan

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: 25 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 launchplan captures a GPU program's launches as a declarative plan and replays the plan without the program.

A plan is the receipt contract's execution form (receipt contract v2 §6.2): named buffers, and ordered fill, copy, write and launch steps on one stream, each launch naming its module by digest, its entry, its geometry and the full byte image of its parameters with every device pointer replaced by a buffer and an offset. Build makes a plan from a capture of the program under the cudainject agent with CUDAPROF_PLAN=1. Compare checks two captures made at different allocation layouts against each other, which is what separates a pointer from a scalar that happens to equal an address. Replay runs a plan on the local device from the module images alone.

Check replays a plan at two or more seeds, and a plan passes only if every seed's outputs equal the recorded ones. Each seed poisons the buffers and pads the layout differently. One seed is not enough: a plan that reads memory nothing wrote can match the recording by chance at one poison, as the calibration's uninitialized-read subject did at seed 1. Nor is Compare enough without a replay: a scalar derived from an address can give the same plan at both capture layouts, and only the replay's outputs show it.

A plan that the capture cannot show to be replayable says so: Replayable is false and Unreplayable names every reason. Such a plan can still be compared, but it is never replayed as a strong subject.

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

View Source
const (
	RoleInput     = "input"
	RoleOutput    = "output"
	RoleWorkspace = "workspace"
)

Buffer roles.

View Source
const (
	OpFill   = "fill"
	OpCopy   = "copy"
	OpWrite  = "write"
	OpLaunch = "launch"
)

Step ops.

View Source
const (
	KindPtr   = "ptr"
	KindBytes = "bytes_hex"
)

Field kinds of a parameter image.

View Source
const BundleFormat = "launchplan-bundle/v0"

BundleFormat names the on-disk layout WriteBundle writes.

View Source
const MaxWriteBytes = 1 << 20

MaxWriteBytes is the largest write step a plan carries inline.

Variables

View Source
var ErrNotReplayable = errors.New("plan is not replayable")

ErrNotReplayable reports a plan whose capture did not show it replayable.

Functions

func CheckStrings

func CheckStrings(raw []byte, id Identity) error

CheckStrings walks a JSON document and fails if any string in it, key or value, names id. Digests are skipped and hex payloads are decoded and scanned as bytes (SLICE1 §20.4, what the residual check scans). A bundle is never written with such a string: there is no normalization to fall back on, so the check fails closed.

func Compare

func Compare(a, b *Bundle) error

Compare checks two captures of the same program made at different allocation layouts (the second under CUDAPROF_PLAN_SKEW). They must give the same plan: the same buffers, every literal parameter byte equal, and every pointer slot naming the same buffer and offset. A slot that was a pointer in one capture and a scalar in the other changes the plan, so it fails here. Compare also fails if the layouts did not in fact differ in the distances between buffers, since then the check proves nothing.

func ModuleLayout

func ModuleLayout(cubin []byte) (map[string][]ParamSpan, error)

ModuleLayout reads every entry's parameter layout from a module image.

func Mutate

func Mutate(p *Plan, name string) (string, error)

Mutate plants one named fault in a plan, for calibrating replay. It chooses a site where the fault changes behaviour, not a padding byte or a pair of steps that commute, and returns a description of it.

zero-byte     clear the first nonzero literal parameter byte
drop-byte     drop the last byte of the first literal field
swap-binding  swap the buffers of a launch's first two pointer fields
              that name different buffers
reorder       swap the first two adjacent launches that share a buffer
tensor-bytes  shrink the first input's tensor_bytes by 4
no-reason     mark the plan unreplayable with no reason
bad-reason    mark the plan unreplayable for a reason SPEC §6.8 lacks

func Replay

func Replay(ctx context.Context, p *Plan, opt ReplayOptions) (map[string][]byte, error)

Replay runs a plan on device 0 and returns each output tensor's bytes. It runs no producer code: it loads the plan's modules, allocates its buffers, and runs its steps in order on one stream. The caller must hold an inherited gpu-lock lease.

func ValidateEncoding

func ValidateEncoding(raw []byte) error

ValidateEncoding checks a plan's JSON text for a second encoding: an optional field present with its zero value. A field's offset inside params is not optional and is exempt.

Types

type ArgImage

type ArgImage struct {
	Image  []byte
	Params []ParamSpan
	Absent string
	Size   int
}

An ArgImage is one launch's parameter bytes and layout, from the agent's kind 14 records.

type Buffer

type Buffer struct {
	Name       string `json:"name"`
	Bytes      int64  `json:"bytes"`
	AlignBytes int64  `json:"align_bytes"`
	Role       string `json:"role"`
	// Tensor names the case tensor an input or output buffer holds, in its
	// first TensorBytes bytes.
	Tensor      string `json:"tensor,omitempty"`
	TensorBytes int64  `json:"tensor_bytes,omitempty"`
}

A Buffer is one device allocation the plan uses.

type Built

type Built struct {
	Plan    *Plan
	Bases   map[string]uint64
	Modules []string // digests the plan launches, in first-use order
	Notes   []string
}

A Built plan carries, beside the plan, what Compare needs from the capture: the captured base address of each buffer.

func Build

func Build(c *Capture, sc *Sidecar, id string) (*Built, error)

Build makes the plan for the one profiler range in a capture. Each buffer is one allocation the range's steps touch. A sidecar tensor whose address is an allocation's base names that buffer and gives its role; every other buffer is workspace. Build fails on anything it cannot represent; a plan it can represent but cannot show to be replayable is returned with Replayable false and its reasons.

type Bundle

type Bundle struct {
	Manifest Manifest
	Plan     *Plan
	Modules  map[string][]byte
	Inputs   map[string][]byte
	Outputs  map[string][]byte
}

A Bundle is a plan with everything a replay of it needs.

func NewBundle

func NewBundle(b *Built, c *Capture, sc *Sidecar, sidecarDir string) (*Bundle, error)

NewBundle collects a built plan's modules from its capture and its tensors' bytes from the files the sidecar names, relative to sidecarDir.

func ReadBundle

func ReadBundle(dir string) (*Bundle, error)

ReadBundle reads a bundle and checks every digest in its manifest before returning it: a tampered file is reported, never used.

func (*Bundle) Write

func (bd *Bundle) Write(dir string) error

Write writes the bundle to dir, which must not exist. It refuses to write a manifest or plan that names the current user, home directory or host (SLICE1 §20.4); the capture journal, which holds driver-reported paths, is never part of a bundle.

type Capture

type Capture struct {
	Dir     string
	Lines   []Line
	Args    map[uint32]*ArgImage // by correlation
	Kernels map[uint32]string    // kernel name by correlation
	Modules map[string]*Module   // by sha256
	Entries map[string][]*Module // modules declaring each entry
}

A Capture is what the agent wrote for one process: its plan lines, the parameter images of its launches, its kernels' names, and its modules.

func ReadCapture

func ReadCapture(dir string) (*Capture, error)

ReadCapture reads the agent's output for one process directory.

type CheckResult

type CheckResult struct {
	Seeds     []uint64
	Mismatch  []string // "<tensor> seed <s>: first differing byte <i>"
	Dependent []string // outputs that differ between seeds
}

A CheckResult is the outcome of replaying a bundle against its recorded outputs.

func Check

func Check(ctx context.Context, bd *Bundle, seeds []uint64) (*CheckResult, error)

Check replays a bundle once per seed and compares every output with the bundle's recorded bytes, and the replays with each other. Different seeds poison workspace and outputs differently and lay buffers out differently, so a plan that reads memory it never wrote, or a pointer the capture misclassified, shows up as a mismatch.

func (*CheckResult) OK

func (r *CheckResult) OK() bool

OK reports whether every replay matched the recorded outputs.

type Field

type Field struct {
	Offset       int    `json:"offset"`
	Kind         string `json:"kind"`
	Buffer       string `json:"buffer,omitempty"`
	BufferOffset int64  `json:"buffer_offset,omitempty"`
	Hex          string `json:"hex,omitempty"`
}

A Field is a span of the parameter image: a device pointer into a buffer, or literal bytes.

type Identity

type Identity struct {
	Home, User, Host string
}

An Identity is what a bundle must not name: the producing user's home directory, user name and host name (SLICE1 §20).

func CurrentIdentity

func CurrentIdentity() Identity

CurrentIdentity returns the identity of the running process.

type Line

type Line struct {
	Seq         int64     `json:"seq"`
	Op          string    `json:"op"`
	Correlation uint32    `json:"correlation"`
	CBID        uint32    `json:"cbid"`
	Ptr         uint64    `json:"ptr"`
	Bytes       uint64    `json:"bytes"`
	Kind        string    `json:"kind"`
	Dir         string    `json:"dir"`
	Dst         uint64    `json:"dst"`
	Src         uint64    `json:"src"`
	Stream      uint64    `json:"stream"`
	Hex         string    `json:"hex"`
	Host        string    `json:"host"`
	Width       int       `json:"width"`
	Value       int64     `json:"value"`
	Attr        int       `json:"attr"`
	Count       uint64    `json:"count"`
	Function    uint64    `json:"function"`
	Grid        [3]uint32 `json:"grid"`
	Block       [3]uint32 `json:"block"`
	Smem        uint32    `json:"smem"`
	Attrs       []struct {
		ID  int    `json:"id"`
		Hex string `json:"hex"`
	} `json:"attrs"`
	What   string `json:"what"`
	Result int    `json:"result"`
	Lost   uint64 `json:"lost"`
}

A Line is one line of plan.jsonl.

type Manifest

type Manifest struct {
	Format  string           `json:"format"`
	Plan    string           `json:"plan_sha256"`
	Modules []ManifestModule `json:"modules"`
	Tensors []ManifestTensor `json:"tensors"`
	Notes   []string         `json:"notes,omitempty"`
	// CaptureBases are the buffers' addresses in the captured run, which
	// Compare uses to check that two captures' layouts differed.
	CaptureBases map[string]uint64 `json:"capture_bases"`
}

A Manifest describes a bundle: the plan, the module images it launches, and the tensors of the captured run, all by digest.

type ManifestModule

type ManifestModule struct {
	SHA256 string `json:"sha256"`
	File   string `json:"file"`
	Source string `json:"source"`
}

A ManifestModule is one module image. Source is "environment": the image was taken from the producer's process and its licence is the producer's.

type ManifestTensor

type ManifestTensor struct {
	Name   string `json:"name"`
	Role   string `json:"role"`
	Bytes  int64  `json:"bytes"`
	SHA256 string `json:"sha256"`
	File   string `json:"file"`
}

A ManifestTensor is one input or output of the captured run.

type Module

type Module struct {
	SHA256 string // "sha256:<hex>"
	Path   string
	Bytes  []byte
	Layout map[string][]ParamSpan // entry → parameter layout
}

A Module is one module image the process loaded.

type ParamSpan

type ParamSpan struct{ Offset, Size int }

ParamSpan is one parameter of a kernel's ABI: its offset and size in the parameter buffer.

type Params

type Params struct {
	SizeBytes int     `json:"size_bytes"`
	Fields    []Field `json:"fields"`
}

Params is the complete byte image of a launch's parameter buffer. Bytes no field covers are zero.

func (*Params) CheckABI

func (ps *Params) CheckABI(layout []ParamSpan) error

CheckABI checks a launch's parameter image against the entry's parameter layout as the module declares it: the image must be exactly as long as the layout, and every field must lie inside one parameter.

func (*Params) Image

func (ps *Params) Image(addr func(buffer string, offset int64) (uint64, error)) ([]byte, error)

Image returns the parameter image with each pointer field set by addr, which maps a buffer name and offset to a device address.

Example

A launch's parameters are literal bytes and pointer slots that name a buffer; Image lays them out for a replay's buffer addresses.

package main

import (
	"encoding/hex"
	"fmt"

	"github.com/tmc/lib/nvidia/x/obs/diag/kernelcheck/launchplan"
)

func main() {
	p := launchplan.Params{SizeBytes: 12, Fields: []launchplan.Field{
		{Offset: 0, Kind: launchplan.KindPtr, Buffer: "x", BufferOffset: 16},
		{Offset: 8, Kind: launchplan.KindBytes, Hex: "00040000"},
	}}
	img, err := p.Image(func(buffer string, off int64) (uint64, error) {
		return 0x7f0000000000 + uint64(off), nil
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(hex.EncodeToString(img))
}
Output:
10000000007f000000040000

type Plan

type Plan struct {
	ID      string   `json:"id"`
	Buffers []Buffer `json:"buffers"`
	Steps   []Step   `json:"steps"`

	// Replayable reports whether the capture showed the plan to be
	// replayable from its modules alone. When it is false, Unreplayable
	// lists every reason, sorted.
	Replayable   bool     `json:"replayable"`
	Unreplayable []string `json:"unreplayable,omitempty"`
}

A Plan is an ordered sequence of steps over named buffers, run on one stream. Its JSON form is the receipt contract's plan object.

func (*Plan) Digest

func (p *Plan) Digest() string

Digest returns the sha256 of the plan's JSON encoding, as "sha256:<hex>". Field order is fixed by the type, so equal plans have equal digests.

The encoding is provisional: it is encoding/json's, not the receipt contract's canonical JSON (JCS), which it becomes when that lands. A digest is comparable only with one made by the same version.

func (*Plan) Validate

func (p *Plan) Validate() error

Validate checks that a plan is well formed: unique buffer names, steps that name existing buffers and stay inside them, and parameter fields that are aligned, inside the image, and non-overlapping. It does not check the image against a module's parameter layout; [CheckABI] does.

type Region

type Region struct {
	Tensor string
	Graded int64
	Bytes  int64
}

A Region is the part of an output buffer a check grades: bytes [0, Graded) of a buffer of Bytes bytes. The tail [Graded, Bytes) is poisoned workspace and never graded.

func GradedRegions

func GradedRegions(p *Plan) []Region

GradedRegions returns the region of each output buffer that Check compares, in plan order.

type ReplayOptions

type ReplayOptions struct {
	// Modules maps a module digest to its image. Every module a launch
	// names must be present, and its bytes must have that digest.
	Modules map[string][]byte
	// Inputs maps an input tensor's name to its bytes.
	Inputs map[string][]byte
	// Seed chooses the poison written to every byte of the buffers that
	// no input fills, and the sizes of the pad allocations made between
	// buffers, so two seeds give two layouts and two poisons.
	Seed uint64
}

ReplayOptions configure one replay.

type Sidecar

type Sidecar struct {
	Tensors []Tensor `json:"tensors"`
}

A Sidecar names the tensors the producer's harness passed in and read out, by their device addresses in the captured run.

func ReadSidecar

func ReadSidecar(path string) (*Sidecar, error)

ReadSidecar reads a sidecar JSON file.

type Step

type Step struct {
	Op string `json:"op"`

	// fill: Bytes bytes of Buffer from Offset, repeating Pattern (hex).
	// write: Hex at Offset of Buffer.
	Buffer  string `json:"buffer,omitempty"`
	Offset  int64  `json:"offset,omitempty"`
	Bytes   int64  `json:"bytes,omitempty"`
	Pattern string `json:"pattern_hex,omitempty"`
	Hex     string `json:"hex,omitempty"`

	// copy: Bytes bytes, device to device.
	Src       string `json:"src,omitempty"`
	Dst       string `json:"dst,omitempty"`
	SrcOffset int64  `json:"src_offset,omitempty"`
	DstOffset int64  `json:"dst_offset,omitempty"`

	// launch.
	Module         string     `json:"module,omitempty"`
	Entry          string     `json:"entry,omitempty"`
	Grid           *[3]uint32 `json:"grid,omitempty"`
	Block          *[3]uint32 `json:"block,omitempty"`
	Cluster        *[3]uint32 `json:"cluster,omitempty"`
	Cooperative    bool       `json:"cooperative,omitempty"`
	SharedMemBytes uint32     `json:"shared_mem_bytes,omitempty"`
	Params         *Params    `json:"params,omitempty"`
}

A Step is one operation of a plan. Which fields are set depends on Op.

type Tensor

type Tensor struct {
	Name  string `json:"name"`
	Ptr   uint64 `json:"ptr"`
	Bytes int64  `json:"bytes"`
	Role  string `json:"role"`
	Dtype string `json:"dtype,omitempty"`
	File  string `json:"file,omitempty"`
}

A Tensor is one named input or output of the captured run. File, if set, holds its bytes as the run wrote or read them, relative to the sidecar.

Directories

Path Synopsis
internal
lptool command
Lptool builds, compares, replays and mutates launch-plan bundles.
Lptool builds, compares, replays and mutates launch-plan bundles.

Jump to

Keyboard shortcuts

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