IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).

Python module

max.experimental.realization_context

Implementations of various realization strategies.

“Eager” execution: tensors are realized as soon as the realization context exits. This is the default behavior.

This has a huge concrete advantage over eagerly executing one operation at a time: by controlling the boundary of where the eager context starts and ends, we can give advanced users a tool to enable fine-grained bounds for automatic fusion.

In practice the easiest way to do this is to mark a function as F.functional. This function is then assumed to be “atomic” for the purposes of eager execution. All ops within the function execute as part of the same graph, meaning the compiler is free to fuse operations and generate fused kernels within this region.

“Lazy” execution: tensors are realized only when code later tries to use them.

This enables a class of interface design common in the ML world, in which layers are constructed with randomized weights which are never used. Lazy execution neatly allows constructing entire models, only performing the weight initialization and allocating memory for them if and when those weights are actually used.

Graph compilation: tensors must never be realized.

This allows tensor operations to be composed with direct usage of the Graph API, for instance Module.compile, or using F.* operations in another Graph API usage.

Contexts​

EagerRealizationContextComputation graph for managing tensor operations.
GraphRealizationContextA realization context for ahead-of-time graph compilation.
LazyRealizationContextA realization context that defers execution until explicitly requested.

Default context​

default_realization_contextConstructs a context for ops realized outside any explicit context.
ensure_contextEnsures a realization context exists for Tensor / TensorValue conversion.
in_graph_contextReturns True when executing inside a Graph context.
lazyDefers tensor realization until explicitly awaited.
set_default_realization_contextSets the constructor used by default_realization_context().

Seeding​

seedGets the global random seed tensor used in eager execution mode.
set_seedSets the global random seed value.