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 ¶
- Handle.AddCubin: Handle.AddCubin adds a CUDA binary to the fatbinary.
- Handle.AddIndex: Handle.AddIndex adds an index file to the fatbinary.
- Handle.AddLTOIR: Handle.AddLTOIR adds LTOIR to the fatbinary.
- Handle.AddPTX: Handle.AddPTX adds PTX to the fatbinary.
- Handle.AddReloc: Handle.AddReloc adds relocatable PTX entries from a host object to the fatbinary.
- Handle.AddTileIR: Handle.AddTileIR adds Tile IR to the fatbinary.
- Create: Create creates a new handle
- Handle.Destroy: Handle.Destroy destroys the handle.
- Handle.Get: Handle.Get returns the completed fatbinary.
- Handle.Size: Handle.Size returns the fatbinary's size.
- Version: Version returns the current version of nvFatbin
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")
}
Output:
Index ¶
- func Close() error
- func Has(symbol string) bool
- func HasAddCubin() bool
- func HasAddIndex() bool
- func HasAddLTOIR() bool
- func HasAddPTX() bool
- func HasAddReloc() bool
- func HasAddTileIR() bool
- func HasCreate() bool
- func HasDestroy() bool
- func HasGet() bool
- func HasGetErrorString() bool
- func HasSize() bool
- func HasVersion() bool
- func Load(path string) error
- func Loaded() bool
- func Path() string
- func Version() (major uint32, minor uint32, err error)
- type BindFailure
- type CallError
- type Handle
- func (h Handle) AddCubin(code unsafe.Pointer, size uint, arch string, identifier string) error
- func (h Handle) AddIndex(code unsafe.Pointer, size uint, identifier string) error
- func (h Handle) AddLTOIR(code unsafe.Pointer, size uint, arch string, identifier string, ...) error
- func (h Handle) AddPTX(code unsafe.Pointer, size uint, arch string, identifier string, ...) error
- func (h Handle) AddPTXString(code, arch, identifier, options string) error
- func (h Handle) AddReloc(code unsafe.Pointer, size uint) error
- func (h Handle) AddTileIR(code unsafe.Pointer, size uint, identifier string, optionsCmdLine string) error
- func (h *Handle) Destroy() error
- func (h Handle) Get() ([]byte, error)
- func (h Handle) Size() (uint, error)
- type LibraryAttempt
- type LibraryConflictError
- type LibraryError
- type Result
- type SymbolError
- type VersionMismatchError
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 ¶
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 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 ¶
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.
Types ¶
type BindFailure ¶
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 ¶
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.
type Handle ¶
type Handle uintptr
Handle is an opaque handle for nvFatbinHandle.
func Create ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Destroy destroys the handle.
It is not named Close because it takes a pointer to the handle.
Wraps nvFatbinDestroy.
type LibraryAttempt ¶
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.
type SymbolError ¶
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.