Skip to content

Configuration

The options that decide where, in which precision and in how much memory a computation runs. See Estimators for how they combine.

config

config(**options)

Bases: ContextDecorator

Temporarily override options.

Usable as a context manager (with optix.config(device="cuda"):) or as a decorator. Overrides apply to the current thread / asyncio task only: threads started inside the block see the process-wide defaults.

Note

Nesting works, and an instance may be re-entered (but not used concurrently from several threads).

Validate the overrides.

Parameters:

Name Type Description Default
**options object

Any field of Config. Values are validated immediately.

{}

set_config

set_config(**options)

Set process-wide defaults.

Note

An active with config(...) block keeps shadowing the options it overrides.

Parameters:

Name Type Description Default
**options object

Any field of Config. Values are validated and normalised ("float64" → torch.float64, "1GiB" → bytes, ...).

{}

Raises:

Type Description
TypeError

If an option name is unknown.

ValueError

If a value is invalid.

get_config

get_config()

Return the active configuration.

Returns:

Type Description
Config

The innermost with config(...) override of the current context, else the process-wide defaults.

Config dataclass

Config(dtype=None, device=None, memory=256 * 2 ** 20, batch=None, compile=True, prefetch=0)

Immutable set of options.

Attributes:

Name Type Description
dtype dtype or None

Working floating-point dtype. None means torch.get_default_dtype().

device device or None

Working device. None means the device of a Tensor input, else the torch default device.

memory int

Budget, in bytes, of one loaded chunk on the working device.

batch int or None

Explicit number of frames per chunk. Overrides memory.

compile bool

Whether kernels decorated with maybe_compile go through torch.compile.

prefetch int

Number of chunks read ahead in a background thread (0 = off).

Backend

Bases: TypedDict

Resolved working dtype and device, usable as x.to(**backend).

resolve_backend

resolve_backend(dtype=None, device=None, like=None)

Resolve the working dtype and device.

Parameters:

Name Type Description Default
dtype dtype or str

Explicit dtype. Default: configuration, else torch.get_default_dtype(). Never follows like.

None
device device or str

Explicit device. Default: configuration, else the device of like if it is a Tensor, else the torch default device.

None
like object

Input data whose device is followed when no device is set.

None

Returns:

Type Description
Backend

{"dtype": ..., "device": ...}, usable as x.to(**backend).

Raises:

Type Description
ValueError

If float64 is requested on an MPS device, or a value is invalid.

parse_bytes

parse_bytes(value)

Convert a memory size to a number of bytes.

Parameters:

Name Type Description Default
value int or float or str

A number of bytes (1e9) or a string with an optional unit ("512MiB", "2GB", "1.5 GiB"). Decimal units (kB, MB, GB, TB) are powers of 1000, binary units (KiB, MiB, GiB, TiB) are powers of 1024. Units are case-insensitive.

required

Returns:

Type Description
int

Number of bytes (strictly positive).

Raises:

Type Description
ValueError

If the value cannot be parsed or is not positive.