gpugen

package
v0.206.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 14 Imported by: 0 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 gpugen is the shared Go exec wrapper for every LOCAL GPU generation runner (image / video / audio). It shells out to a render/*.mjs (or a python TTS worker) that takes the single-slot GPU lock + drives ComfyUI/Chatterbox, and wraps that child with the hard-won lifecycle guards the bare runners lack:

  • killTree on timeout/cancel — on Windows a bare node-kill ORPHANS the spawned ComfyUI python grandchild (pinning ~8GB VRAM) and skips node's JS finally (leaking the GPU lock); we taskkill the WHOLE process tree.
  • WaitDelay — a short grace window after Cancel before the pipe is force-closed.
  • defer freeComfyVRAM — belt-and-suspenders: however the child ended (clean exit, error, or a timeout-kill that skipped its finally), force-drop any ComfyUI VRAM so a render never leaves the GPU pinned (zero-always-warm; protects the load-bearing memory stack).

This was extracted from internal/imagegen.Generate so video + audio get the SAME process-tree-kill (they previously had no Go wrapper → no kill on timeout). Pure os/exec + net/http, no deps. The output-file stat is the success gate (a child can exit 0 yet produce nothing). Behavior for the image path is preserved byte-for-byte.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsInt

func AsInt(v any) int

AsInt is the exported coercion used by thin callers assembling Args.

func ClassifyErr

func ClassifyErr(err error) string

ClassifyErr maps a gen failure to a coarse class (oom|timeout|conn_refused|disk_full|other) for the ledger's ErrClass. Mirrors pipeline.classifyErr; nil => "".

func Generate

func Generate(ctx context.Context, spec Spec) (string, error)

Generate runs the spec's command, returning Out on success. A non-zero exit, a timeout (child + tree killed), or a missing/empty Out returns an error so the caller can map it to a clean defer. Never panics on a nil/absent process.

func IsDiskFull added in v0.206.0

func IsDiskFull(err error) bool

IsDiskFull reports whether err says the volume an output goes to cannot take another byte (full, over its quota, or remounted read-only): by the TYPED errno when the error came from a Go file call in this process (a *PathError, *LinkError or *SyscallError wrapping the syscall errno), and by the errno token in its text when it crossed a process boundary (a render helper's output tail). Its wording is never read: the text of an error that carries no token is not a full disk.

func KillTree added in v0.206.0

func KillTree(p *os.Process) error

KillTree is killTree for the lanes that spawn their own interactive child (the browse lane keeps a stdio conversation open, so it cannot go through Generate) and still need the same whole-tree kill on timeout/cancel.

func ResolveScript added in v0.6.1

func ResolveScript(script string) (string, error)

ResolveScript resolves a configured render-script path for a GPU-gen runner. A RELATIVE path (the shipped defaults are "render/*.mjs") is resolved against the EXECUTABLE's directory, NOT the process cwd: an MCP host (e.g. the ~/.claude.json registration) spawns the server with no meaningful cwd, so a cwd-relative default made node fail with MODULE_NOT_FOUND — an instant defer on every video/voice/music call. If the resolved file does not exist, the error reads "script not found at <absolute-path>": a distinct, actionable defer reason, unlike the generic runner failure.

func ResolveScriptIn added in v0.206.0

func ResolveScriptIn(script, exeDir string) (string, error)

ResolveScriptIn is ResolveScript against a caller-supplied executable dir. It exists so capability reporting (internal/mediacap) answers "will this script resolve?" with THIS rule rather than a second copy of it that can drift.

Types

type FootprintKey added in v0.22.0

type FootprintKey struct {
	Family string // e.g. "sdxl", "wan2.2", "whisper", "acestep"
	Quant  string // e.g. "bf16", "q8_0"; "" = node default
	Task   string // fleet task_type, e.g. "image-gen"
}

FootprintKey identifies which footprint-store entry a sampled render belongs to (mirrors the fleet contract's model_footprints identity: family + quant + task).

type RunError added in v0.206.0

type RunError struct {
	Class string
	// contains filtered or unexported fields
}

RunError is a failure whose class was decided structurally, not by the words of its message: the class the runner itself reported (the IGPU_CLASS= line), or "timeout" for the deadline and the cancel gpugen itself observed. ClassifyErr returns Class for it, whatever words the rest of the message (which embeds the child's last output) happens to contain.

func (*RunError) Error added in v0.206.0

func (e *RunError) Error() string

func (*RunError) Unwrap added in v0.206.0

func (e *RunError) Unwrap() error

type Sampling added in v0.22.0

type Sampling struct {
	Footprint   *FootprintKey
	SampleFunc  func(childPid int) (float64, error)
	OnFootprint func(peakGiB float64)
	// HostSampleFunc / OnHostFootprint: see Spec.
	HostSampleFunc  func(childPid int) (privateGiB, residentGiB float64, err error)
	OnHostFootprint func(privatePeakGiB, residentPeakGiB float64)
}

Sampling bundles Spec's passive footprint-sampling hook fields so the thin wrappers (imagegen, rungraph) can thread them opaquely without growing three parameters each. nil = no sampling (the legacy Spec path, byte-identical).

func (*Sampling) ApplyTo added in v0.22.0

func (s *Sampling) ApplyTo(spec *Spec)

ApplyTo copies s onto spec. nil-safe: a nil receiver is a no-op, so callers thread whatever the pipeline composed without a guard.

type Spec

type Spec struct {
	// Exe is the executable ("" => "node"). Script is its first argument (the
	// render/*.mjs path, or for `node -e` the verb). Args are the remaining argv.
	Exe    string
	Script string
	Args   []string
	// Dir is the child's working directory ("" = inherit the current process's
	// cwd, the pre-existing behavior for every caller that leaves it unset).
	// Added for Task 6 (pipeline-job): an externally-provided pipeline CLI is
	// invoked with its OWN repo root as cwd (PipelineSpec.Workdir), unlike the
	// bundled render/*.mjs scripts, which run from wherever the harness resolves
	// them and never need a cwd override.
	Dir string
	// Env are extra "K=V" entries appended to the current environment (e.g.
	// COMFY_DIR, MEMORY_STACK, GPU_LOCK_WAIT_MS). nil = inherit only.
	Env []string
	// EnvExact, when true, makes Env the child's COMPLETE environment: nothing
	// is inherited from this process. The compose route (ADR 0059) sets it so an
	// external CLI's runner starts from an allowlist — no cloud key, lease token
	// (GPU_LEASE_*) or NODE_OPTIONS in the server's env can reach it. false keeps
	// the inherit-and-append behavior every other runner relies on.
	EnvExact bool
	// Out is the file the runner must produce; a missing/empty Out after a clean
	// exit is treated as failure (the caller defers). Required.
	Out string
	// Timeout bounds the whole invocation (cold-start + render + margin).
	Timeout time.Duration
	// ComfyAPI is the ComfyUI endpoint freeComfyVRAM hits after the run. "" =>
	// the COMFY_API env or the 127.0.0.1:8188 default. Set "" to inherit; a runner
	// with no ComfyUI (TTS) can leave it — /free on a dead endpoint is a no-op.
	// When set (a per-card instance) it is also exported to the runner as COMFY_API,
	// so the runner talks to, launches and frees the same instance the /free targets.
	ComfyAPI string
	// CardUUID is the GPU uuid the runner pins its ComfyUI instance to (exported as
	// COMFY_CARD_UUID; the runner sets CUDA_VISIBLE_DEVICES from it, never an index).
	// "" = not card-bound. It also blanks COMFY_CUDA_DEVICE: a uuid pin replaces the
	// legacy index, and the runner refuses a launch that carries both.
	CardUUID string
	// SkipFreeComfy, when true, suppresses the post-run ComfyUI /free (the TTS/voice
	// path never starts ComfyUI, so there is nothing to free). The killTree + output
	// stat still apply — the python worker still gets process-tree-killed on timeout.
	SkipFreeComfy bool
	// OwnProcessGroup, when true (non-Windows only; a no-op on Windows, where taskkill /T
	// already reaps the tree), starts the runner as the leader of its own process group, so a
	// timeout or a cancel signals the WHOLE group instead of the bare node process: SIGTERM
	// first, so the runner can kill its engine and remove its temp dirs, then SIGKILL after a
	// grace. The iGPU media runners set it (their engines are spawn-per-job native binaries that
	// would otherwise keep the GPU after the lease is released). false keeps every other lane's
	// kill exactly as it was.
	OwnProcessGroup bool
	// Footprint, when non-nil, turns on passive per-render VRAM peak sampling for
	// the fleet-node footprint store (added 2026-07-17): while the child runs,
	// SampleFunc is polled and the max observation is reported via OnFootprint —
	// on SUCCESS only (a crashed/phantom run's peak may be partial). nil keeps the
	// legacy CombinedOutput path byte-identical.
	Footprint *FootprintKey
	// SampleFunc returns the current VRAM usage in GiB attributable to the render
	// rooted at childPid. The CALLER composes what a sample means (PDH process-tree
	// sum, or a global-delta closure) — gpugen stays dependency-free and only tracks
	// the peak. nil = no sampling (OnFootprint never fires).
	SampleFunc func(childPid int) (float64, error)
	// OnFootprint receives the observed peak (GiB) after a SUCCESSFUL render whose
	// sampled peak was > 0. nil = observations are discarded.
	OnFootprint func(peakGiB float64)
	// HostSampleFunc returns the HOST memory (GiB) of the process tree rooted at childPid right now: its
	// private bytes (the commit-side figure) and its resident set (the physical-RAM figure). It is polled on
	// the same tick as SampleFunc, only while Footprint is set, and the peak of each is reported once via
	// OnHostFootprint on a SUCCESSFUL run (G3 of the P0 plan: the host-RAM guard sizes a render from what
	// renders were measured to hold, not only from the size of their model files). The caller composes what a
	// sample means (gpulease.TreeMemory); gpugen stays dependency-free. nil = no host sampling.
	HostSampleFunc func(childPid int) (privateGiB, residentGiB float64, err error)
	// OnHostFootprint receives the peak private and the peak resident memory (GiB) after a SUCCESSFUL render
	// whose sampled private peak was > 0. nil = discarded.
	OnHostFootprint func(privatePeakGiB, residentPeakGiB float64)
}

Spec describes one GPU-gen invocation. The caller assembles Args (the runner's CLI flags) — gpugen owns only the cross-cutting lifecycle, not the per-task arg shape.

Jump to

Keyboard shortcuts

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