# tensorplay.export API Source: https://www.tensorplay.cn/docs/api/tensorplay.export.html ## Functions 12 [#](#api-tensorplay.export.default_decompositions) ### default_decompositions function[Full reference ↗](/docs/generated/tensorplay.export.default_decompositions.html) ```python tensorplay.export.default_decompositions() → Any ``` Return the mutable table of built-in graph rewrites. [#](#api-tensorplay.export.dims) ### dims function[Full reference ↗](/docs/generated/tensorplay.export.dims.html) ```python tensorplay.export.dims(*names: str, min: int | None = None, max: int | None = None) → tuple[Dim, ...] ``` Construct several named dimensions with shared bounds. [#](#api-tensorplay.export.draft_export) ### draft_export function[Full reference ↗](/docs/generated/tensorplay.export.draft_export.html) ```python tensorplay.export.draft_export(model: Any, *args: Any, dynamic_shapes: Any = None, **kwargs: Any) → DraftExportReport ``` Capture a program, reporting failures instead of raising them. On success the returned report carries the captured program, checked two ways: structural validation of the graph, and a numerical comparison of the captured program against eager execution on the example inputs. When capture fails, the report records the failure and exported_program stays None; call raise_on_failure() to surface the errors. [#](#api-tensorplay.export.export_for_training) ### export_for_training function[Full reference ↗](/docs/generated/tensorplay.export.export_for_training.html) ```python tensorplay.export.export_for_training(model: Callable[[...], Any], *args: Any, dynamic_shapes: Any = None, **kwargs: Any) → ExportedProgram ``` Capture a callable while retaining its mutable training state. [#](#api-tensorplay.export.export) ### export function[Full reference ↗](/docs/generated/tensorplay.export.export.html) ```python tensorplay.export.export(model: Callable[[...], Any], *args: Any, dynamic_shapes: Any = None, strict: bool = False, preserve_module_call_signature: Any = (), **kwargs: Any) → ExportedProgram ``` Capture a callable and return an executable graph program. Parameters: - model – an nn.Module or plain callable; child modules are inlined. - args/kwargs – example inputs binding argument defaults. - dynamic_shapes – dimension specification per argument (dict, sequence, [ShapesCollection](/docs/generated/tensorplay.export.ShapesCollection.html#tensorplay.export.ShapesCollection), or [AdditionalInputs](/docs/generated/tensorplay.export.AdditionalInputs.html#tensorplay.export.AdditionalInputs)). - strict – reserved for callers of the strict capture contract; capture validation is identical in both modes. - preserve_module_call_signature – submodule paths whose call metadata is recorded in module_call_graph for module-level tooling. [#](#api-tensorplay.export.load_pt2) ### load_pt2 function[Full reference ↗](/docs/generated/tensorplay.export.load_pt2.html) ```python tensorplay.export.load_pt2(f: Any, *, expected_opset_version: dict[str, int] | None = None, run_single_threaded: bool = False, num_runners: int = 1, device_index: int = -1, load_weights_from_disk: bool = False) → PT2ArchiveContents ``` [#](#api-tensorplay.export.load) ### load function[Full reference ↗](/docs/generated/tensorplay.export.load.html) ```python tensorplay.export.load(f: Any, *, extra_files: dict[str, Any] | None = None, expected_opset_version: dict[str, int] | None = None) → Any ``` Load an [ExportedProgram](/docs/generated/tensorplay.export.ExportedProgram.html#tensorplay.export.ExportedProgram) previously written by [save()](/docs/generated/tensorplay.export.save.html#tensorplay.export.save). [#](#api-tensorplay.export.package_pt2) ### package_pt2 function[Full reference ↗](/docs/generated/tensorplay.export.package_pt2.html) ```python tensorplay.export.package_pt2(f: Any, *, exported_programs: Any = None, tp_files: Any = None, extra_files: dict[str, Any] | None = None, opset_version: dict[str, int] | None = None, pickle_protocol: int = 4, executorch_files: dict[str, bytes] | None = None) → Any ``` [#](#api-tensorplay.export.refine_dynamic_shapes_from_suggested_fixes) ### refine_dynamic_shapes_from_suggested_fixes function[Full reference ↗](/docs/generated/tensorplay.export.refine_dynamic_shapes_from_suggested_fixes.html) ```python tensorplay.export.refine_dynamic_shapes_from_suggested_fixes(message: str, dynamic_shapes: Any) → Any ``` Apply suggested fixes (range refinements, specializations, relations). Supported fix forms: ``` name = Dim('name', min=..., max=...) # refine a range name = 4 # specialize to a constant dy = dx + 1 # tie a dim to another with a relation dy = 2*dx # positive multiple of another dim ``` dx must name a dimension already present in dynamic_shapes or be defined by an earlier fix line. [#](#api-tensorplay.export.register_dataclass) ### register_dataclass function[Full reference ↗](/docs/generated/tensorplay.export.register_dataclass.html) ```python tensorplay.export.register_dataclass(cls: type[Any], *, serialized_type_name: str | None = None, return_none_fields: bool = True) → type[Any] ``` Register a dataclass as a flattenable graph value. serialized_type_name pins the qualified name recorded in serialized artifacts; it must resolve back to cls when the program is loaded. return_none_fields keeps None-valued fields as single None leaves instead of flattening them by field. [#](#api-tensorplay.export.save) ### save function[Full reference ↗](/docs/generated/tensorplay.export.save.html) ```python tensorplay.export.save(ep: Any, f: Any, *, extra_files: dict[str, Any] | None = None, opset_version: dict[str, int] | None = None, pickle_protocol: int = 4) → None ``` Save an [ExportedProgram](/docs/generated/tensorplay.export.ExportedProgram.html#tensorplay.export.ExportedProgram) to a file or writable buffer. [#](#api-tensorplay.export.unflatten) ### unflatten function[Full reference ↗](/docs/generated/tensorplay.export.unflatten.html) ```python tensorplay.export.unflatten(module: ExportedProgram, flat_args_adapter: FlatArgsAdapter | None = None, preserve_ops: Any = ()) → UnflattenedModule ``` Build an executable module view from an exported program. When the capture recorded module call boundaries, the view reconstructs the original module hierarchy (attribute access and submodule calls work as in the source model). Otherwise it falls back to the flat view. ## Classes 27 [#](#api-tensorplay.export.AdditionalInputs) ### AdditionalInputs class[Full reference ↗](/docs/generated/tensorplay.export.AdditionalInputs.html) ```python class tensorplay.export.AdditionalInputs ``` Infer dynamic shape markers from representative input sets. [#](#api-tensorplay.export.ConstantArgument) ### ConstantArgument class[Full reference ↗](/docs/generated/tensorplay.export.ConstantArgument.html) ```python class tensorplay.export.ConstantArgument(name: 'str', value: 'int | float | bool | str | None') ``` [#](#api-tensorplay.export.Constraint) ### Constraint class[Full reference ↗](/docs/generated/tensorplay.export.Constraint.html) ```python class tensorplay.export.Constraint(source: Any, dim: int, name: str | None = None, min: int | None = None, max: int | None = None, warn_only: bool = False, root: str | None = None, scale: int = 1, offset: int = 0) ``` A range restriction attached to one input dimension. name ties the constraint to a named [Dim](/docs/generated/tensorplay.export.Dim.html#tensorplay.export.Dim) shared across inputs (equalities are implied). root/scale/offset describe a derived dimension whose size equals scale * root_size + offset. Constraints without a name come from dim hints or static entries. [#](#api-tensorplay.export.CustomDecompTable) ### CustomDecompTable class[Full reference ↗](/docs/generated/tensorplay.export.CustomDecompTable.html) ```python class tensorplay.export.CustomDecompTable(entries: Mapping[Any, Callable[[...], Any]] | Iterable[tuple[Any, Callable[[...], Any]]] | None = None, *, defaults: bool = True) ``` A validated mutable mapping from graph targets to replacement callables. Entries are keyed by graph target (callable or method name). Removing a key preserves the op from rewriting; materialize returns a plain dict for consumers that require one. ```python clear() → None.  Remove all items from D. ``` ```python classmethod fromkeys(iterable, value=None, /) ``` Create a new dictionary with keys from iterable and values set to value. ```python materialize() → dict[Any, Callable[[...], Any]] ``` Return a plain dict of effective entries, resolving defaults. ```python popitem() ``` Remove and return a (key, value) pair as a 2-tuple. Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty. ```python remove(target: Any) → Callable[[...], Any] ``` Preserve target by deleting its decomposition entry. ```python setdefault(key, default=None, /) ``` Insert key with a value of default if key is not in the dictionary. Return the value for key if key is in the dictionary, else default. [#](#api-tensorplay.export.CustomObjArgument) ### CustomObjArgument class[Full reference ↗](/docs/generated/tensorplay.export.CustomObjArgument.html) ```python class tensorplay.export.CustomObjArgument(name: 'str', class_fqn: 'str', fake_val: 'Any' = None) ``` [#](#api-tensorplay.export.Dim) ### Dim class[Full reference ↗](/docs/generated/tensorplay.export.Dim.html) ```python class tensorplay.export.Dim(name: str, *, min: int | None = None, max: int | None = None) ``` A named symbolic dimension with an optional finite range. [#](#api-tensorplay.export.EqualityConstraint) ### EqualityConstraint class[Full reference ↗](/docs/generated/tensorplay.export.EqualityConstraint.html) ```python class tensorplay.export.EqualityConstraint(sites: tuple[tuple[str, int], ...], name: str | None = None) ``` Ties several input sites to one shared dimension size. sites lists every (input placeholder name, dim index) pair whose runtime sizes must stay equal; name is the symbolic dimension they implement when the tie comes from a shared [Dim](/docs/generated/tensorplay.export.Dim.html#tensorplay.export.Dim), else None. [#](#api-tensorplay.export.ExportBackwardSignature) ### ExportBackwardSignature class[Full reference ↗](/docs/generated/tensorplay.export.ExportBackwardSignature.html) ```python class tensorplay.export.ExportBackwardSignature(gradients_to_parameters: 'dict[str, str]', gradients_to_user_inputs: 'dict[str, str]', loss_output: 'str') ``` [#](#api-tensorplay.export.ExportedProgram) ### ExportedProgram class[Full reference ↗](/docs/generated/tensorplay.export.ExportedProgram.html) ```python class tensorplay.export.ExportedProgram(graph_module: GraphModule, graph_signature: ExportGraphSignature | GraphSignature, example_inputs: dict[str, ~typing.Any]=, dynamic_shapes: Any = None, module_call_graph: list[ModuleCallEntry] = , range_constraints: dict[~typing.Any, ~typing.Any]=, equality_constraints: list[EqualityConstraint] = , verifier: Any = None) ``` A validated graph together with its state and example bindings. ```python buffers() → Iterator[Any] ``` Iterate over the captured module’s buffers. ```python static call_exported(program: ExportedProgram) → Callable[[...], Any] ``` Return a callable executing the flat contract on user arguments. ```python property code: str ``` Python source of the captured graph’s generated forward. ```python classmethod deserialize(artifact: Any, state_dict: Any = None, constants: Any = None, example_inputs: Any = None) → ExportedProgram ``` Rebuild a program from [serialize()](#tensorplay.export.ExportedProgram.serialize) artifacts. ```python invalidate_unlifted() → None ``` Drop the cached unlifted module so the next call rebuilds it. ```python module() → GraphModule ``` Return a self-contained module with lifted state folded back in. The returned module takes only the user arguments: state placeholders are rewritten into attribute reads on a fresh module that owns the parameter, buffer, and constant values. The result is cached; pass rebind to drop state changes made through this program view. ```python parameters() → Iterator[Any] ``` Iterate over the captured module’s parameters. ```python run_decompositions(decomp_table: Any = None) → ExportedProgram ``` Return a copied program after applying registered graph rewrites. Entries map a graph target (a callable, a method name, or a target string) to a builder invoked as builder(graph, node); the builder creates the replacement nodes and returns the value users should consume. Nodes whose target has no entry are left untouched. ```python serialize(opset_version: Any = None, pickle_protocol: int = 4) → Any ``` Return serialized program artifacts (JSON program + example inputs). ```python property state_dict: dict[str, Any] ``` Tensor values of the lifted parameters and persistent buffers. ```python property tensor_constants: dict[str, Any] ``` Lifted non-parameter, non-buffer tensor values. [#](#api-tensorplay.export.ExportGraphSignature) ### ExportGraphSignature class[Full reference ↗](/docs/generated/tensorplay.export.ExportGraphSignature.html) ```python class tensorplay.export.ExportGraphSignature(input_specs: list[InputSpec], output_specs: list[OutputSpec]) ``` Describe lifted state, user values, mutations, and graph outputs. ```python property assertion_dep_token: Mapping[int, str] | None ``` Position of the assertion dependency token output, if present. ```python clone() → ExportGraphSignature ``` Deep copy: specs and argument records are duplicated, not shared. ```python get_param_to_buffer() → Mapping[str, str] ``` Map parameter targets to the buffer targets holding their gradients. Gradients are declared as GRADIENT_TO_PARAMETER outputs; a gradient for a parameter whose optimizer state lives in a buffer binds the two targets under the parameter’s FQN. ```python get_replace_hook(replace_inputs: bool = False) ``` Build a rename hook suitable for graph rewriting passes. ```python is_buffer(name: str) → bool ``` Whether name is a placeholder carrying a lifted buffer. ```python is_param(name: str) → bool ``` Whether name is a placeholder carrying a lifted parameter. ```python replace_all_uses(old: str, new: str) → None ``` Rename a graph value across every input and output spec. [#](#api-tensorplay.export.FlatArgsAdapter) ### FlatArgsAdapter class[Full reference ↗](/docs/generated/tensorplay.export.FlatArgsAdapter.html) ```python class tensorplay.export.FlatArgsAdapter ``` Adapt one flattened argument layout into another layout. [#](#api-tensorplay.export.GraphSignature) ### GraphSignature class[Full reference ↗](/docs/generated/tensorplay.export.GraphSignature.html) ```python class tensorplay.export.GraphSignature(parameters: tuple[str, ...], buffers: tuple[str, ...], non_persistent_buffers: tuple[str, ...], user_inputs: tuple[str, ...]) ``` Compact signature retained for callers that build signatures directly. [#](#api-tensorplay.export.InputKind) ### InputKind class[Full reference ↗](/docs/generated/tensorplay.export.InputKind.html) ```python class tensorplay.export.InputKind(*values) ``` [#](#api-tensorplay.export.InputSpec) ### InputSpec class[Full reference ↗](/docs/generated/tensorplay.export.InputSpec.html) ```python class tensorplay.export.InputSpec(kind: 'InputKind', arg: 'ArgumentSpec', target: 'str | None' = None, persistent: 'bool | None' = None) ``` [#](#api-tensorplay.export.ModuleCallEntry) ### ModuleCallEntry class[Full reference ↗](/docs/generated/tensorplay.export.ModuleCallEntry.html) ```python class tensorplay.export.ModuleCallEntry(fqn: 'str', signature: 'ModuleCallSignature | None' = None) ``` [#](#api-tensorplay.export.ModuleCallSignature) ### ModuleCallSignature class[Full reference ↗](/docs/generated/tensorplay.export.ModuleCallSignature.html) ```python class tensorplay.export.ModuleCallSignature(inputs: 'list[ArgumentSpec]', outputs: 'list[ArgumentSpec]', in_spec: 'TreeSpec | None' = None, out_spec: 'TreeSpec | None' = None, forward_arg_names: 'list[str] | None' = None) ``` [#](#api-tensorplay.export.OutputKind) ### OutputKind class[Full reference ↗](/docs/generated/tensorplay.export.OutputKind.html) ```python class tensorplay.export.OutputKind(*values) ``` [#](#api-tensorplay.export.OutputSpec) ### OutputSpec class[Full reference ↗](/docs/generated/tensorplay.export.OutputSpec.html) ```python class tensorplay.export.OutputSpec(kind: 'OutputKind', arg: 'ArgumentSpec', target: 'str | None' = None) ``` [#](#api-tensorplay.export.PT2ArchiveContents) ### PT2ArchiveContents class[Full reference ↗](/docs/generated/tensorplay.export.PT2ArchiveContents.html) ```python class tensorplay.export.PT2ArchiveContents(exported_programs: 'dict[str, Any]'=, tp_runners: 'dict[str, Any]'=, extra_files: 'dict[str, Any]'=) ``` [#](#api-tensorplay.export.ShapesCollection) ### ShapesCollection class[Full reference ↗](/docs/generated/tensorplay.export.ShapesCollection.html) ```python class tensorplay.export.ShapesCollection ``` Associate shape specifications with tensor objects by identity. [#](#api-tensorplay.export.SymBoolArgument) ### SymBoolArgument class[Full reference ↗](/docs/generated/tensorplay.export.SymBoolArgument.html) ```python class tensorplay.export.SymBoolArgument(name: 'str') ``` [#](#api-tensorplay.export.SymFloatArgument) ### SymFloatArgument class[Full reference ↗](/docs/generated/tensorplay.export.SymFloatArgument.html) ```python class tensorplay.export.SymFloatArgument(name: 'str') ``` [#](#api-tensorplay.export.SymIntArgument) ### SymIntArgument class[Full reference ↗](/docs/generated/tensorplay.export.SymIntArgument.html) ```python class tensorplay.export.SymIntArgument(name: 'str') ``` [#](#api-tensorplay.export.TensorArgument) ### TensorArgument class[Full reference ↗](/docs/generated/tensorplay.export.TensorArgument.html) ```python class tensorplay.export.TensorArgument(name: 'str') ``` [#](#api-tensorplay.export.TokenArgument) ### TokenArgument class[Full reference ↗](/docs/generated/tensorplay.export.TokenArgument.html) ```python class tensorplay.export.TokenArgument(name: 'str') ``` [#](#api-tensorplay.export.UnflattenedModule) ### UnflattenedModule class[Full reference ↗](/docs/generated/tensorplay.export.UnflattenedModule.html) ```python class tensorplay.export.UnflattenedModule(export_module: ExportedProgram, flat_args_adapter: FlatArgsAdapter | None = None) ``` Executable module view retaining the captured root module hierarchy. [#](#api-tensorplay.export.WeightType) ### WeightType class[Full reference ↗](/docs/generated/tensorplay.export.WeightType.html) ```python class tensorplay.export.WeightType(*values) ``` Role a packaged weight plays in the captured program. ```python as_integer_ratio() ``` Return a pair of integers, whose ratio is equal to the original int. The ratio is in lowest terms and has a positive denominator. ``` >>> (10).as_integer_ratio() (10, 1) >>> (-10).as_integer_ratio() (-10, 1) >>> (0).as_integer_ratio() (0, 1) ``` ```python bit_count() ``` Number of ones in the binary representation of the absolute value of self. Also known as the population count. ``` >>> bin(13) '0b1101' >>> (13).bit_count() 3 ``` ```python bit_length() ``` Number of bits necessary to represent self in binary. ``` >>> bin(37) '0b100101' >>> (37).bit_length() 6 ``` ```python conjugate() ``` Returns self, the complex conjugate of any int. ```python denominator ``` the denominator of a rational number in lowest terms ```python classmethod from_bytes(bytes, byteorder='big', *, signed=False) ``` ```python imag ``` the imaginary part of a complex number ```python is_integer() ``` Returns True. Exists for duck type compatibility with float.is_integer. ```python numerator ``` the numerator of a rational number in lowest terms ```python real ``` the real part of a complex number ```python to_bytes(length=1, byteorder='big', *, signed=False) ``` ## Attributes 1 [#](#api-tensorplay.export.DerivedDim) ### DerivedDim attribute[Full reference ↗](/docs/generated/tensorplay.export.DerivedDim.html) ```python tensorplay.export.DerivedDim ``` alias of _DerivedDim ## Exceptions 1 [#](#api-tensorplay.export.ConstraintsExceededError) ### ConstraintsExceededError exception[Full reference ↗](/docs/generated/tensorplay.export.ConstraintsExceededError.html) ```python exception tensorplay.export.ConstraintsExceededError ``` A runtime input violated the declared dynamic-shape contract. Raised by the assertions inserted into captured graphs and by export-time validation when an example input falls outside a declared range. It is a RuntimeError so callers written against plain runtime failures keep working.