nvfatbin

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: 15 Imported by: 1 module, 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 nvfatbin provides Go bindings for NVIDIA fatbinary packaging.

Fatbinaries store alternative device images; they do not link device code. Destroy each handle after use. Optional APIs such as TileIR packaging require the corresponding symbol in the loaded library.

Other

Example

Example packages PTX for two architectures into one fatbinary. It returns early when the nvFatbin library is not available. The bytes Get returns are what the driver package's ModuleLoadFatBinary expects; the driver picks the image matching the device it loads on.

package main

import (
	"fmt"

	"github.com/tmc/lib/nvidia/nvfatbin"
)

func main() {
	if _, _, err := nvfatbin.Version(); err != nil {
		fmt.Println("nvFatbin unavailable:", err)
		return
	}

	const ptx = `
.version 7.0
.target sm_70
.address_size 64
.visible .entry noop() { ret; }
`
	h, err := nvfatbin.Create("-cuda")
	if err != nil {
		fmt.Println("create:", err)
		return
	}
	defer h.Destroy()

	for _, arch := range []string{"compute_70", "compute_80"} {
		if err := h.AddPTXString(ptx, arch, "noop.ptx", ""); err != nil {
			fmt.Println("add", arch+":", err)
			return
		}
	}
	fatbin, err := h.Get()
	if err != nil {
		fmt.Println("get:", err)
		return
	}
	fmt.Println("fatbinary of", len(fatbin), "bytes")
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close() error

Close unloads the library and clears all bound function pointers.

The function pointers are package-level state, so Close must not be called concurrently with any other call into this package: doing so is a use-after-free. It is intended for test teardown, not for reclaiming a live library.

func Has

func Has(symbol string) bool

Has reports whether the named C symbol resolved in the loaded library, loading it first if necessary. It returns false if the library failed to load. Use it to gate on capabilities before calling into them.

func HasAddCubin

func HasAddCubin() bool

HasAddCubin reports whether nvFatbinAddCubin resolved in the loaded library.

func HasAddIndex

func HasAddIndex() bool

HasAddIndex reports whether nvFatbinAddIndex resolved in the loaded library.

func HasAddLTOIR

func HasAddLTOIR() bool

HasAddLTOIR reports whether nvFatbinAddLTOIR resolved in the loaded library.

func HasAddPTX

func HasAddPTX() bool

HasAddPTX reports whether nvFatbinAddPTX resolved in the loaded library.

func HasAddReloc

func HasAddReloc() bool

HasAddReloc reports whether nvFatbinAddReloc resolved in the loaded library.

func HasAddTileIR

func HasAddTileIR() bool

HasAddTileIR reports whether nvFatbinAddTileIR resolved in the loaded library.

func HasCreate

func HasCreate() bool

HasCreate reports whether nvFatbinCreate resolved in the loaded library.

func HasDestroy

func HasDestroy() bool

HasDestroy reports whether nvFatbinDestroy resolved in the loaded library.

func HasGet

func HasGet() bool

HasGet reports whether nvFatbinGet resolved in the loaded library.

func HasGetErrorString

func HasGetErrorString() bool

HasGetErrorString reports whether nvFatbinGetErrorString resolved in the loaded library.

func HasSize

func HasSize() bool

HasSize reports whether nvFatbinSize resolved in the loaded library.

func HasVersion

func HasVersion() bool

HasVersion reports whether nvFatbinVersion resolved in the loaded library.

func Load

func Load(path string) error

Load opens the shared library from path, or from the default search list when path is empty, and binds all symbols. Callers that need to direct loading should call Load before any other function; otherwise the first call loads from the default search list.

Load is idempotent: calling it again with the same path, or with an empty path, returns nil. Calling it with a different path after a successful load returns *LibraryConflictError rather than replacing the library. After a failed load, Load retries.

func Loaded

func Loaded() bool

Loaded reports whether the library is currently loaded. It does not trigger a load.

func Path

func Path() string

Path returns the path or name the loaded library was opened from, or the empty string if no library is loaded.

func Version

func Version() (major uint32, minor uint32, err error)

Version returns the current version of nvFatbin

  • major (out): The major version
  • minor (out): The minor version

Wraps nvFatbinVersion.

Types

type BindFailure

type BindFailure struct {
	Symbol string
	Err    error
}

BindFailure names a symbol the loaded library exports but that could not be bound, and why.

func BindFailures

func BindFailures() []BindFailure

BindFailures returns the symbols the loaded library exports that could not be bound, sorted by symbol name, or nil if every exported symbol bound. Calling one of these reports *SymbolError with the same Err.

A load that reports no error can still leave symbols unbound: the library is fine and the signatures are not representable here. Call this after Load to find that out at once rather than one call site at a time. It does not trigger a load, and returns nil if none has happened.

type CallError

type CallError struct {
	Func   string // the C symbol called
	Result Result
}

CallError reports that a library call returned a failure result.

Unwrap returns Result, so errors.Is(err, c) reports whether the call returned the Result constant c, and errors.As(err, &r) with r of type Result recovers the code.

func (*CallError) Error

func (e *CallError) Error() string

Error reports the C symbol called and the result it returned.

func (*CallError) Unwrap

func (e *CallError) Unwrap() error

Unwrap returns the result the call reported.

type Handle

type Handle uintptr

Handle is an opaque handle for nvFatbinHandle.

func Create

func Create(options ...string) (Handle, error)

Create creates a new handle

  • options (in): An array of strings, each containing a single option
  • optionsCount (in): Number of options

Wraps nvFatbinCreate.

func (Handle) AddCubin

func (h Handle) AddCubin(code unsafe.Pointer, size uint, arch string, identifier string) error

AddCubin adds a CUDA binary to the fatbinary.

  • code (in): The cubin
  • size (in): The size of the cubin
  • arch (in): The numerical architecture that this cubin is for (the XX of any sm_XX, lto_XX, or compute_XX)
  • identifier (in): Name of the cubin, useful when extracting the fatbin with tools like cuobjdump

Wraps nvFatbinAddCubin.

func (Handle) AddIndex

func (h Handle) AddIndex(code unsafe.Pointer, size uint, identifier string) error

AddIndex adds an index file to the fatbinary.

Requires CUDA 12.5 or later; see HasAddIndex.

  • code (in): The index
  • size (in): The size of the index
  • identifier (in): Name of the index, useful when extracting the fatbin with tools like cuobjdump

Wraps nvFatbinAddIndex.

func (Handle) AddLTOIR

func (h Handle) AddLTOIR(code unsafe.Pointer, size uint, arch string, identifier string, optionsCmdLine string) error

AddLTOIR adds LTOIR to the fatbinary.

  • code (in): The LTOIR code
  • size (in): The size of the LTOIR code
  • arch (in): The numerical architecture that this LTOIR is for (the XX of any sm_XX, lto_XX, or compute_XX)
  • identifier (in): Name of the LTOIR, useful when extracting the fatbin with tools like cuobjdump
  • optionsCmdLine (in): Options used during JIT compilation

Wraps nvFatbinAddLTOIR.

func (Handle) AddPTX

func (h Handle) AddPTX(code unsafe.Pointer, size uint, arch string, identifier string, optionsCmdLine string) error

AddPTX adds PTX to the fatbinary.

  • code (in): The PTX code
  • size (in): The size of the PTX code
  • arch (in): The numerical architecture that this PTX is for (the XX of any sm_XX, lto_XX, or compute_XX)
  • identifier (in): Name of the PTX, useful when extracting the fatbin with tools like cuobjdump
  • optionsCmdLine (in): Options used during JIT compilation

Wraps nvFatbinAddPTX.

func (Handle) AddPTXString

func (h Handle) AddPTXString(code, arch, identifier, options string) error

AddPTXString adds PTX source text to the fatbinary.

It exists because nvFatbinAddPTX's size is documented to include the terminating NUL, and a Go string has none. Passing len(code) where the library expects len(code)+1 is not an error the library reports: the header says it appends the NUL itself, copying the code to do so, so the mistake costs a copy and silently changes what was packaged. This function appends the NUL and passes the inclusive length, which is the only spelling of the call that is right by construction.

arch is the numeric architecture, the XX of sm_XX, lto_XX or compute_XX -- "90", not "sm_90". identifier names the entry for tools like cuobjdump. options are the ptxas options applied during JIT compilation, as one command-line string; pass "" for none.

Use Handle.AddPTX directly for PTX that is already NUL-terminated in memory the caller owns, or that is not a Go string.

func (Handle) AddReloc

func (h Handle) AddReloc(code unsafe.Pointer, size uint) error

AddReloc adds relocatable PTX entries from a host object to the fatbinary.

Requires CUDA 12.5 or later; see HasAddReloc.

  • code (in): The host object image
  • size (in): The size of the host object image code

Wraps nvFatbinAddReloc.

func (Handle) AddTileIR

func (h Handle) AddTileIR(code unsafe.Pointer, size uint, identifier string, optionsCmdLine string) error

AddTileIR adds Tile IR to the fatbinary.

Requires CUDA 13.1 or later; see HasAddTileIR.

  • code (in): The Tile IR
  • size (in): The size of the Tile IR
  • identifier (in): Name of the Tile IR, useful when extracting the fatbin with tools like cuobjdump
  • optionsCmdLine (in): Options used during JIT compilation

Wraps nvFatbinAddTileIR.

func (*Handle) Destroy

func (h *Handle) Destroy() error

Destroy destroys the handle.

It is not named Close because it takes a pointer to the handle.

Wraps nvFatbinDestroy.

func (Handle) Get

func (h Handle) Get() ([]byte, error)

Get returns the completed fatbinary.

Wraps nvFatbinGet.

func (Handle) Size

func (h Handle) Size() (uint, error)

Size returns the fatbinary's size.

Wraps nvFatbinSize.

type LibraryAttempt

type LibraryAttempt struct {
	Name string // path or soname passed to dlopen
	Err  error
}

LibraryAttempt records one failed attempt to open the shared library.

type LibraryConflictError

type LibraryConflictError struct {
	Loaded    string
	Requested string

	// Implicit reports that the loaded library was opened lazily and no
	// successful Load has since acknowledged it. A
	// caller that sees this called some
	// other function first, and that call took the default search list.
	Implicit bool
}

LibraryConflictError reports a Load call naming a different library than the one already loaded. Only one library is loaded per process.

func (*LibraryConflictError) Error

func (e *LibraryConflictError) Error() string

Error reports the conflicting library paths, and whether the library that is already loaded got there without a Load call.

type LibraryError

type LibraryError struct {
	Library  string
	Env      string
	Path     string
	Attempts []LibraryAttempt
	Hint     string
}

LibraryError reports that libnvfatbin could not be opened. Attempts lists every name tried in order. Path is set when an explicit path, from Load or NVFATBIN_LIB_PATH, was requested and failed. Hint reports pkg-config discovery results and query failures, when discovery ran.

func (*LibraryError) Error

func (e *LibraryError) Error() string

Error describes the library load attempts and their errors.

func (*LibraryError) Unwrap

func (e *LibraryError) Unwrap() []error

Unwrap returns the error from each attempt.

type Result

type Result int32

Result represents a nvfatbin API error code (nvFatbinResult).

When the library is loaded, Result.Error returns rich messages from GetErrorString. When the library is not loaded, it falls back to the numeric error code and Go-defined name.

const (
	Success                   Result = 0
	ErrInternal               Result = 1
	ErrElfArchMismatch        Result = 2
	ErrElfSizeMismatch        Result = 3
	ErrMissingPtxVersion      Result = 4
	ErrNullPointer            Result = 5
	ErrCompressionFailed      Result = 6
	ErrCompressedSizeExceeded Result = 7
	ErrUnrecognizedOption     Result = 8
	ErrInvalidArch            Result = 9
	ErrInvalidNvvm            Result = 10
	ErrEmptyInput             Result = 11
	ErrMissingPtxArch         Result = 12
	ErrPtxArchMismatch        Result = 13
	ErrMissingFatbin          Result = 14
	ErrInvalidIndex           Result = 15
	ErrIdentifierReuse        Result = 16
	ErrInternalPtxOption      Result = 17
)

nvfatbin error codes from nvFatbin.h.

func (Result) Code

func (r Result) Code() int32

Code returns the underlying result code for programmatic error handling.

func (Result) Error

func (r Result) Error() string

Error implements the error interface. When the library is loaded, it returns the error description from GetErrorString. Otherwise it returns a fallback string with the Go constant name and numeric code.

func (Result) String

func (r Result) String() string

String returns the Go constant name of the result.

type SymbolError

type SymbolError struct {
	Symbol string
	Err    error
}

SymbolError reports that a symbol is not callable.

Err distinguishes the two ways that happens. A nil Err means the loaded library does not export the symbol, which is what an installation older than the symbol looks like. A non-nil Err means the library does export it and the binding itself failed; the library is not at fault.

func (*SymbolError) Error

func (e *SymbolError) Error() string

Error reports the uncallable library symbol and, when the symbol was present but could not be bound, why.

func (*SymbolError) Unwrap

func (e *SymbolError) Unwrap() error

Unwrap returns the reason the symbol could not be bound, or nil when the loaded library does not export it.

type VersionMismatchError

type VersionMismatchError struct {
	Path string // the library that was opened
	Want int    // major version these bindings were generated for
	Got  int    // major version the library reports
	Raw  int    // the version value the library reported, undecoded
}

VersionMismatchError reports that the shared library that opened is not from the version train nvfatbin was generated for. dlsym matches on name alone, so a library from a neighbouring train opens and resolves nearly every symbol; asking it its own version is what tells the two apart.

func (*VersionMismatchError) Error

func (e *VersionMismatchError) Error() string

Error reports which library was opened and the two majors that disagree.

A decoder is only a decoder for the version schemes it was measured against, and a library from a train that predates the scheme can report a value it does not decode. Naming a major of 0 in that case would attribute to the library a claim it never made, so the raw value is reported instead.

Jump to

Keyboard shortcuts

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