# tensorplay.compiler Source: https://www.tensorplay.cn/docs/compiler.html ## Shape policy A compile call chooses how tensor extents are treated: - With no shape argument, capture fixes the example sizes. If a later call misses only because a size changed (and nothing else in the guard set did), the frontend clears the concrete specialization and captures the region again with every extent that is not 0 or 1 symbolic, so one artifact then serves the sizes already seen and the ones that follow. This upgrade needs a backend that declares supports_symbolic_shapes and is controlled by tensorplay.compiler.config.automatic_dynamic_shapes. - dynamic=True captures extents symbolically from the first call. It keeps the rank in the specialization key and lets the backend accept the shape range. - dynamic_shapes names the axes that may vary, per argument: ``` tensorplay.compile( fn, dynamic_shapes={"x": {0: tensorplay.export.Dim("batch")}}, ) ``` Unnamed axes keep the example size and are checked before a cached region runs, so changing one creates a new specialization instead of silently reusing a region sized for the example. A dimension may carry min/max bounds; an example outside them is rejected before capture. The specification needs a backend that declares supports_symbolic_shapes. tensorplay.compiler.mark_static(tensor, dim=None) pins a tensor’s extents (or one axis) to the example sizes. Called before compile, it removes those axes from the shape policy; called inside the captured function, it makes reads of that extent answer with the example size and keeps a condition on it, so a changed size recompiles rather than reusing a region that assumed the old one. tensorplay.compiler.disallow_in_graph(fn) declares that a callable must not be absorbed into a captured region. A capture that reaches it raises GraphCaptureError instead of recording the call: the region is one graph, so there is no partial capture to resume after the call, and recording a call whose effect the graph cannot carry would repeat that effect once per compiled call. ## Autograd limitations In v1.1.0, regions compiled with STAX or an AOT autograd backend support first-order reverse-mode gradients. They do not support second-order gradients (double backward). A first gradient computed with create_graph=True has the correct value, but differentiating it again raises RuntimeError with an explicit message about this limitation. Run the original function without compilation when computing Hessians, Hessian-vector products, or other higher-order derivatives. Operator support for higher-order gradients in eager execution does not extend to compiled regions. ## Classes | tensorplay.compiler.GraphCaptureError |Raised when Python code cannot be represented by the current graph. | | --- | --- | | tensorplay.compiler.InvalidBackend | A backend name that no registered, built-in or entry-point backend owns. | | tensorplay.compiler.NestedCompileRegionOptions | Backend selections for a nested compiler region. | ## Functions | tensorplay.compiler.allow_in_graph |Capture a callable as one graph operation without entering its body. | | --- | --- | | tensorplay.compiler.assume_constant_result | Declare that a callable returns one stable value across captures. | | tensorplay.compiler.compile | Compile a callable through the TensorPlay capture and lowering pipeline. | | tensorplay.compiler.disallow_in_graph | Declare that a callable must not be absorbed into a captured graph. | | tensorplay.compiler.disable | Prevent capture from entering a callable. | | tensorplay.compiler.disable_capture | Suspend the public capture state for a dynamic Python region. | | tensorplay.compiler.export | Mark a module method as an entry point visible to graph capture. | | tensorplay.compiler.get_backend_capabilities | Return the BackendCapabilities a backend declares. | | tensorplay.compiler.get_default_backend | Return the currently selected default backend. | | tensorplay.compiler.is_compiling | Return whether the current Python frame is being captured. | | tensorplay.compiler.is_exporting | Return whether an export capture session is active. | | tensorplay.compiler.list_backends | Return registered backend names accepted by compile(). | | tensorplay.compiler.list_mode_options | Return the optimization options each compile mode selects. | | tensorplay.compiler.lookup_backend | Resolve a backend name or validate a backend callable. | | tensorplay.compiler.mark_static | Mark a tensor (or one dimension of it) as fixed for shape policies. | | tensorplay.compiler.nested_compile_region | Mark a callable as a reusable nested graph region. | | tensorplay.compiler.nonstrict_trace | Capture a callable as an opaque operation with structured arguments. | | tensorplay.compiler.overload_method | Mark a method declaration as an overload during inspection. | | tensorplay.compiler.register_backend | Register a backend in the TensorPlay compiler registry. | | tensorplay.compiler.register_debug_backend | Register a backend tagged for diagnostics. | | tensorplay.compiler.register_experimental_backend | Register a backend tagged for experimental use. | | tensorplay.compiler.reset | Clear compiler specializations and capture-time constant values. | | tensorplay.compiler.set_default_backend | Set the backend used when compile() receives no backend name. | | tensorplay.compiler.substitute_in_graph | Register a graph-time implementation for a callable. | | tensorplay.compiler.unregister_backend | Remove a named backend from the compiler registry. | | tensorplay.compiler.unused | Mark a callable as an eager-only helper. |