MCPcopy Create free account
hub / github.com/pytorch/executorch / capture

Function capture

exir/capture/_capture.py:165–367  ·  view source on GitHub ↗
(  # noqa: C901
    f: Callable[..., Any],
    args: Tuple[Value, ...],
    config: Optional[CaptureConfig] = None,
    dynamic_shapes: Optional[List[Any]] = None,
)

Source from the content-addressed store, hash-verified

163
164@compatibility(is_backward_compatible=False)
165def capture( # noqa: C901
166 f: Callable[..., Any],
167 args: Tuple[Value, ...],
168 config: Optional[CaptureConfig] = None,
169 dynamic_shapes: Optional[List[Any]] = None,
170) -> ExirExportedProgram:
171 warnings.warn(
172 "This function is now deprecated, please use `torch.export and exir.to_edge` instead. ",
173 DeprecationWarning,
174 stacklevel=1,
175 )
176 if not isinstance(args, tuple):
177 raise ExportError(
178 ExportErrorType.INVALID_INPUT_TYPE,
179 f"Expect `args` to be a tuple, got type: {type(args)}.",
180 )
181
182 config = config or CaptureConfig()
183 out_spec = None
184 # TODO (zhxchen17) Always functionalize in a second pass no matter which path is taken.
185 flat_args = tuple(pytree.tree_flatten(args)[0])
186 if not config.enable_aot:
187 if config._unlift:
188 raise ExportError(
189 ExportErrorType.NOT_SUPPORTED,
190 "_unlift config doesn't do anything without enable_aot enabled. Please do not set it",
191 )
192 if config.pt2_mode:
193 if config.enable_aot:
194 if config.enable_dynamic_shape:
195 raise ExportError(
196 ExportErrorType.NOT_SUPPORTED,
197 "Under enable_aot, enable_dynamic_shapes flag doesn't do anything. Please do not set it",
198 )
199 if not config.enable_functionalization:
200 raise ExportError(
201 ExportErrorType.NOT_SUPPORTED,
202 "Functionalization is required for enable_aot.",
203 )
204
205 # If trying to capture a method and the bound class instance is a
206 # Module, then export the module while patching in that method.
207 if isinstance(f, MethodType) and isinstance(f.__self__, torch.nn.Module):
208 with patch_forward(f.__self__, f):
209 ep = export(
210 cast(torch.nn.Module, f.__self__),
211 args,
212 dynamic_shapes=dynamic_shapes,
213 strict=True,
214 )
215 else:
216 mod = f if isinstance(f, torch.nn.Module) else WrapperModule(f)
217 ep = export(mod, args, dynamic_shapes=dynamic_shapes, strict=True)
218
219 ep = ep.run_decompositions(_default_decomposition_table())
220 ep = _transform(ep, ReplaceViewOpsWithViewCopyOpsPass())
221 if not config._unlift:
222 return ExirExportedProgram(ep, False)

Callers

nothing calls this directly

Calls 15

ExportErrorClass · 0.90
CaptureConfigClass · 0.90
exportFunction · 0.90
_transformFunction · 0.90
ExirExportedProgramClass · 0.90
dynamo_traceFunction · 0.90
flatten_outputFunction · 0.90
InternalErrorClass · 0.90
InputSpecClass · 0.85
TensorArgumentClass · 0.85

Tested by

no test coverage detected