pytorch/docs/source
ydwu4 6abb8c382c [export] add kwargs support for export. (#105337)
Solving #105242.

During export, the exported function's signature changes multiple times. Suppose we'd like to export f as shown in following example:
```python
def f(arg1, arg2, kw1, kw2):
  pass

args = (arg1, arg2)
kwargs =  {"kw2":arg3, "kw1":arg4}

torch.export(f, args, kwargs)
```
The signature changes mutiple times during export process in the following order:
1. **gm_torch_level = dynamo.export(f, *args, \*\*kwargs)**. In this step, we turn all  kinds of parameters such as **postional_only**, **var_positioinal**, **kw_only**, and **var_kwargs** into **positional_or_kw**.It also preserves the positional and kword argument names in original function (i.e. f in this example) [here](https://github.com/pytorch/pytorch/blob/main/torch/_dynamo/export.py#L546C13-L546C27). The order of kwargs will be the **key order** of kwargs (after python 3.6, the order is the insertion of order of keys) instead of the original function signature and the order is baked into a _orig_args varaible of gm_torch_level's pytree info. So we'll have:
```python
def gm_torch_level(arg1, arg2, kw2, kw1)
```
Such difference is acceptable as it's transparent to users of export.

2. **gm_aot_export = aot_export_module(gm_torch_level, pos_or_kw_args)**. In this step, we need to turn kwargs into positional args in the order of how gm_torch_level expected, which is stored in _orig_args. The returned gm_aot_export has the graph signature of flat_args, in_spec = pytree.tree_flatten(pos_or_kw_args):
``` python
flat_args, _ = pytree.tree_flatten(pos_or_kw_args)
def gm_aot_export(*flat_args)
```

3. **exported_program(*args, \*\*kwargs)**. The epxorted artifact is exported_program, which is a wrapper over gm_aot_export and has the same calling convention as the original function "f". To do this, we need to 1. specialize the order of kwargs into pos_or_kw_args and 2. flatten the pos_or_kw_args into what gm_aot_export expected.  We can combine the two steps into one with :
```python
_, in_spec = pytree.tree_flatten((args, kwargs))

# Then during exported_program.__call__(*args, **kwargs)
flat_args  = fx_pytree.tree_flatten_spec((args, kwargs), in_spec)
```
, where kwargs is treated as a normal pytree whose keyorder is preserved in in_spec.

Implementation-wise, we treat _orig_args in dynamo exported graph module as single source of truth and kwags are ordered following it.

Test plan:
See added tests in test_export.py.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/105337
Approved by: https://github.com/angelayi, https://github.com/tugsbayasgalan
2023-07-20 19:53:08 +00:00
..
_static torch.compile docs: "Profiling to understand torch.compile performance (#102862) 2023-06-06 22:00:36 +00:00
_templates Replace master with main in links and docs/conf.py (#100176) 2023-05-02 18:20:32 +00:00
community Update CODEOWNERS (#103934) 2023-06-26 19:29:29 +00:00
compile Update troubleshooting.rst (#105018) 2023-07-12 21:42:53 +00:00
elastic [BE] Prefer dash over underscore in command-line options (#94505) 2023-02-09 20:16:49 +00:00
notes Add autograd modes table to docs (#104774) 2023-07-08 03:14:10 +00:00
rpc
scripts [export] add kwargs support for export. (#105337) 2023-07-20 19:53:08 +00:00
amp.rst [core] Bring cpu device module closer to cuda's. (#103172) 2023-07-12 19:43:22 +00:00
autograd.rst [docs] Add missing functions to autograd.rst (#98854) 2023-04-11 20:45:49 +00:00
backends.rst Fix Backend docs search items (#101214) 2023-05-22 14:58:38 +00:00
benchmark_utils.rst
bottleneck.rst
checkpoint.rst add checkpoint support for custom device (#99626) 2023-05-04 00:23:42 +00:00
compiler.rst torch.compiler public namespace (#102182) 2023-06-13 19:52:17 +00:00
complex_numbers.rst Remove CUDA 11.6 note from complex docs (#100118) 2023-04-27 16:26:27 +00:00
conf.py [BE] Enable ruff's UP rules and autoformat tools and scripts (#105428) 2023-07-19 01:24:44 +00:00
config_mod.rst
cpp_extension.rst
cpp_index.rst
cpu.rst [core] Bring cpu device module closer to cuda's. (#103172) 2023-07-12 19:43:22 +00:00
cuda._sanitizer.rst
cuda.rst Add more GPU metric instrumentation (#91717) 2023-02-24 00:38:03 +00:00
cudnn_persistent_rnn.rst
cudnn_rnn_determinism.rst
data.rst missed StackDataset documentation (#101927) 2023-05-22 21:12:16 +00:00
ddp_comm_hooks.rst [DOCS][DDP]Fix the simple of saving and reloading PowerSGD state and hook. (#102721) 2023-06-10 00:15:00 +00:00
deploy.rst
distributed.algorithms.join.rst
distributed.checkpoint.rst Replace master with main in links and docs/conf.py (#100176) 2023-05-02 18:20:32 +00:00
distributed.elastic.rst
distributed.optim.rst
distributed.rst Replace master with main in links and docs/conf.py (#100176) 2023-05-02 18:20:32 +00:00
distributed.tensor.parallel.rst [TP] Enable more generic attn in Tensor Parallelism (#100508) 2023-05-07 18:15:49 +00:00
distributions.rst
dlpack.rst
docutils.conf
export.rst [exportdb] Setup website (#104288) 2023-07-01 01:03:56 +00:00
fft.rst
fsdp.rst
func.api.rst [functorch] linearize (#94173) 2023-02-09 15:45:08 +00:00
func.batch_norm.rst Fix typo under docs directory (#97202) 2023-03-21 01:24:10 +00:00
func.migrating.rst [torch.func] Add migration guide from functorch (#91811) 2023-01-17 22:14:42 +00:00
func.rst Fix typo under docs directory (#92762) 2023-01-23 18:07:22 +00:00
func.ux_limitations.rst [torch.func] Add docs (#91319) 2022-12-30 02:51:18 +00:00
func.whirlwind_tour.rst [torch.func] Add docs (#91319) 2022-12-30 02:51:18 +00:00
futures.rst
fx.rst [fx] change from #users to num_users in graph printout (#101140) 2023-06-20 21:24:32 +00:00
hub.rst Fix typo under docs directory (#92762) 2023-01-23 18:07:22 +00:00
index.rst [core] Bring cpu device module closer to cuda's. (#103172) 2023-07-12 19:43:22 +00:00
ir.rst Rename Canonical Aten IR to Core Aten IR (#92904) 2023-01-25 05:12:23 +00:00
jit_builtin_functions.rst
jit_language_reference_v2.rst Fix typo under docs directory (#97202) 2023-03-21 01:24:10 +00:00
jit_language_reference.rst [BE] [1/3] Rewrite super() calls in caffe2 and benchmarks (#94587) 2023-02-11 18:19:48 +00:00
jit_python_reference.rst
jit_unsupported.rst
jit_utils.rst
jit.rst Replace master with main in links and docs/conf.py (#100176) 2023-05-02 18:20:32 +00:00
library.rst
linalg.rst
logging.rst Add graph break logging option instead of config flag (#103202) 2023-06-12 19:52:31 +00:00
masked.rst Fix link in docs (#94686) 2023-02-13 20:42:24 +00:00
math-quantizer-equation.png
mobile_optimizer.rst [Reland] Clean Up MobileOptimizerType Rewrite Flags Public API and Documentation (#92081) 2023-01-14 17:06:00 +00:00
model_zoo.rst
monitor.rst
mps.rst [MPS] Add support for MPSProfiler Python bindings (#101002) 2023-05-12 21:55:34 +00:00
multiprocessing.rst
name_inference.rst Add itemsize and nbytes properties to Tensor (#98322) 2023-04-05 12:11:55 +00:00
named_tensor.rst
nested.rst Replace master with main in links and docs/conf.py (#100176) 2023-05-02 18:20:32 +00:00
nn.functional.rst [SDPA] update type hint for scaled_dot_product_attention and documentation (#94008) 2023-02-10 18:02:43 +00:00
nn.init.rst
nn.rst Back out "Make adding buffers more like adding parameters (#104069)" (#105581) 2023-07-20 03:39:53 +00:00
onnx_diagnostics.rst [ONNX] Introduce 'diagnostics' to 'dynamo_export' api (#99668) 2023-05-01 19:58:49 +00:00
onnx_supported_aten_ops.rst
onnx.rst [ONNX] Add fake tensor support to torch.onnx.dynamo_export (#103865) 2023-07-11 03:17:17 +00:00
optim.rst Optimized EMA implementation (#94820) 2023-04-26 18:02:11 +00:00
package.rst
pipeline.rst docs: Linking ResNeXt PyTorch Hub Pipeline (#98689) 2023-04-11 02:20:26 +00:00
profiler.rst
quantization-accuracy-debugging.rst
quantization-backend-configuration.rst
quantization-support.rst [quant][pt2e] Rename _pt2e to pt2e (#104668) 2023-07-15 06:34:17 +00:00
quantization.rst Quantization oneDNN backend only support VNNI CPU (#103653) 2023-06-19 09:50:07 +00:00
random.rst
rpc.rst Replace master with main in links and docs/conf.py (#100176) 2023-05-02 18:20:32 +00:00
signal.rst
sparse.rst Update sparse semi-structured linear operator (#104608) 2023-07-13 23:52:39 +00:00
special.rst
storage.rst
tensor_attributes.rst Add a warning about performance cost of set_default_device (#92703) 2023-01-21 02:23:13 +00:00
tensor_view.rst
tensorboard.rst
tensors.rst Add itemsize and nbytes properties to Tensor (#98322) 2023-04-05 12:11:55 +00:00
testing.rst
torch.ao.ns._numeric_suite_fx.rst
torch.ao.ns._numeric_suite.rst
torch.overrides.rst Add torch_dispatch and modes to extending.rst note (#102087) 2023-06-22 12:56:35 +00:00
torch.rst Re-land _cycleviz.py: visualize reference cycles holding cuda memory (#104051) 2023-06-23 13:44:58 +00:00
type_info.rst