# Torch This page describes the optional `jcmsuite.torch` namespace. See {func}`jcmsuite.solve` for the shared solve options. See the [Result API](results.md) for result navigation and submission behavior. For examples, see [Torch Integration](../derivatives/torch.md). Install PyTorch before you call {func}`jcmsuite.torch.solve` or {func}`jcmsuite.torch.validate_inputs`. Ordinary `jcmsuite` operations do not require PyTorch. ```{admonition} Experimental feature :class: warning The `jcmsuite.torch` integration is experimental. Its API and behavior can change in future releases. Do not depend on its current interface for production workflows without testing each package update. ``` ## Bind Inputs To Solver Derivatives Use {class}`RealDerivative ` for a real input. Use {class}`ComplexDerivative ` when the real and imaginary parts of a complex input have separate solver derivatives. Both types are {class}`DerivativeBinding ` values. ```{eval-rst} .. autoclass:: jcmsuite.torch.DerivativeBinding ``` ```{eval-rst} .. autoclass:: jcmsuite.torch.SolverDerivative ``` ```{eval-rst} .. autoclass:: jcmsuite.torch.RealDerivative :members: label ``` ```{eval-rst} .. autoclass:: jcmsuite.torch.ComplexDerivative :members: real, imaginary ``` Call {func}`jcmsuite.torch.validate_inputs` to check one set of {term}`project keys` and its derivative bindings before a solve. ```{eval-rst} .. autofunction:: jcmsuite.torch.validate_inputs ``` ## Solve {func}`jcmsuite.torch.solve` accepts the parameters of {func}`jcmsuite.solve`. You can also pass Torch tensors as `keys` and use `key_derivative_map` to connect them to solver derivatives. The function returns a Torch result or submission object. See {func}`jcmsuite.solve` for shared options and errors. The type hints show the return object for each input shape. ```{eval-rst} .. autofunction:: jcmsuite.torch.solve ``` ## Result And Submission Types The {term}`project` and {term}`project keys` determine the result type. The table shows each ordinary result, its Torch wrapper, and the method that returns the ordinary result without solving again. | Ordinary result | Torch result | Get ordinary result | | --------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | | {class}`SolveResult ` | {class}`TorchSolveResult ` | {meth}`untorch() ` | | {class}`BatchSolveResult ` | {class}`TorchBatchSolveResult ` | {meth}`untorch() ` | | {class}`BundledSolveResult ` | {class}`TorchBundledSolveResult ` | {meth}`untorch() ` | | {class}`BundledBatchSolveResult ` | {class}`TorchBundledBatchSolveResult ` | {meth}`untorch() ` | With `dispatch="submit"`, the solve returns a Torch {term}`submission`. Call {meth}`wait() ` to get a Torch result of the same shape. Each `wait()` accepts the options of the matching [ordinary submission](results.md). With `asynchronous=True`, await the returned coroutine. Each submission's `untorch()` returns its ordinary submission without running the solve again. The result wrappers' `untorch()` methods return ordinary results. | Torch submission | Wait for a Torch result | Get ordinary submission | | --------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------- | ----------------------------------------------------------------------------------- | | {class}`TorchSolveSubmission ` | {meth}`wait() ` | {meth}`untorch() ` | | {class}`TorchBatchSolveSubmission ` | {meth}`wait() ` | {meth}`untorch() ` | | {class}`TorchBundledSolveSubmission ` | {meth}`wait() ` | {meth}`untorch() ` | | {class}`TorchBundledBatchSolveSubmission ` | {meth}`wait() ` | {meth}`untorch() ` | ```{eval-rst} .. autoclass:: jcmsuite.torch.results.TorchSolveResult :members: postprocesses, untorch ``` ```{eval-rst} .. autoclass:: jcmsuite.torch.results.TorchBatchSolveResult :members: postprocesses, untorch ``` ```{eval-rst} .. autoclass:: jcmsuite.torch.results.TorchBundledSolveResult :members: untorch ``` ```{eval-rst} .. autoclass:: jcmsuite.torch.results.TorchBundledBatchSolveResult :members: untorch ``` ```{eval-rst} .. autoclass:: jcmsuite.torch.results.TorchSolveSubmission :members: untorch, wait ``` ```{eval-rst} .. autoclass:: jcmsuite.torch.results.TorchBatchSolveSubmission :members: untorch, wait ``` ```{eval-rst} .. autoclass:: jcmsuite.torch.results.TorchBundledSolveSubmission :members: untorch, wait ``` ```{eval-rst} .. autoclass:: jcmsuite.torch.results.TorchBundledBatchSolveSubmission :members: untorch, wait ``` ## Export Limits {class}`TorchPostprocessRecord ` and {class}`TorchFieldExportPayload ` reject {meth}`to_df() ` and {meth}`to_xarray() ` because these exports cannot preserve Torch autograd connections. Call {meth}`untorch() ` on the solve result, then use the ordinary result export methods. ## Navigate Postprocess Data ```{eval-rst} .. automodule:: jcmsuite.torch.results ``` The postprocess collection and record wrappers mirror the corresponding ordinary result interfaces. Batch wrappers also preserve the ordinary batch navigation and field operations. Select a project before accessing results from an ordered project bundle. Other result data, such as `keys` and `costs`, keeps its ordinary form. The single-result collection returns {class}`TorchPostprocessRecord ` values. A batch collection returns {class}`TorchBatchPostprocessRecord ` values. Batch dictionary exports keep original batch indices. Each available entry returns Torch tensors connected to that entry's input tensors when the solver provides the required derivatives. Transformed batch field exports use the Torch field wrapper's derivative rules. Postprocess dictionaries omit raw derivative branches. Numeric values without solver derivative data become detached Torch tensors. | Expression | Returned object | | ---------------------------------- | -------------------------------------------------------------------------------------------------------------------------- | | {attr}`TorchSolveResult.postprocesses ` | {class}`TorchPostprocessCollection ` | | {meth}`TorchPostprocessCollection[index] ` | {class}`TorchPostprocessRecord ` | | {attr}`TorchPostprocessRecord.fields ` | {class}`TorchFieldExportPayload ` when the postprocess contains field data | ```{eval-rst} .. autoclass:: jcmsuite.torch.results.TorchPostprocessCollection :members: __getitem__, __iter__, __len__, titles ``` ```{eval-rst} .. autoclass:: jcmsuite.torch.results.TorchPostprocessRecord :members: fields, derivatives, to_dict, to_df, to_xarray ``` ## Navigate Field Data And Derivatives Field views mirror the ordinary {class}`field navigation operations `. You can chain selection and transformation operations in either order. Torch field transforms apply derivative rules so the exported derivative tensor represents the transformed field. | Expression | Returned object | | ----------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | {attr}`field.base `, {meth}`field.select(index) `, or {meth}`field.transform(name) ` | {class}`TorchFieldExportPayload ` | | {attr}`field.intensity ` | {class}`TorchIntensityFieldExportPayload ` | | {attr}`field.derivatives ` | {class}`TorchFieldDerivativeCollection ` | | {meth}`field.derivatives[name] ` for a real input | {class}`TorchFieldDerivativePayload ` | | {meth}`field.derivatives[name] ` for a complex input | {class}`TorchComplexFieldDerivativePayload `; {attr}`.real ` and {attr}`.imag ` each return a {class}`TorchFieldDerivativePayload ` when available | The batch derivative collection applies each entry's bindings and preserves original batch indices. Its {meth}`to_dict() ` exports first sensitivities that can be differentiated when all required second-order pairs are available. Without second-order data, first sensitivities are detached. Partial pair data raises an error when exporting a first sensitivity. Selected second-order values are detached. If no differentiable {term}`project keys ` are bound, the single-result and batch collections have no keys. Neither collection falls back to ordinary derivative data. The batch derivative classes compose the single-result Torch derivative records with ordinary batch index alignment. A lookup returns one derivative record for each available entry; {meth}`to_dict() ` keeps those records keyed by original batch index. Selecting a field position applies the selection to every available entry. For complex inputs, the batch complex payload groups the real and optional imaginary output-component derivatives. Derivative payloads support dictionary export only. ```{eval-rst} .. autoclass:: jcmsuite.torch.results.TorchFieldExportPayload :members: base, derivatives, select, intensity, transform, to_dict, to_df, to_xarray, __getitem__ ``` ```{eval-rst} .. autoclass:: jcmsuite.torch.results.TorchIntensityFieldExportPayload :members: base, derivatives, select, to_dict, to_df, to_xarray, __getitem__ ``` Call {meth}`select(index) ` on a field view or derivative payload to select a field entry. For a {class}`TorchComplexFieldDerivativePayload `, selection applies to both component payloads. Derivative payloads do not support transforms or intensity. Intensity views do not support further transforms. Transform a field view before requesting intensity or selecting its derivative on a single-result Torch field view. See the {ref}`Complex Input Convention `. ```{eval-rst} .. autoclass:: jcmsuite.torch.results.TorchFieldDerivativeCollection :members: keys, __getitem__ ``` ```{eval-rst} .. autoclass:: jcmsuite.torch.results.TorchFieldDerivativePayload :members: derivatives, select, to_dict ``` ```{eval-rst} .. autoclass:: jcmsuite.torch.results.TorchComplexFieldDerivativePayload :members: real, imag, select ``` ## Batch Postprocess and Field Classes | Expression | Returned object | | --------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- | | {attr}`TorchBatchSolveResult.postprocesses ` | {class}`TorchBatchPostprocessCollection ` | | {meth}`TorchBatchPostprocessCollection[index] ` | {class}`TorchBatchPostprocessRecord ` | | {attr}`batch record.fields ` | {class}`TorchBatchFieldExportPayload ` when the postprocess contains field data | | {attr}`batch field.base `, {meth}`field.select(index) `, or {meth}`field.transform(name) ` | {class}`TorchBatchFieldExportPayload ` | | {attr}`batch field.intensity ` | {class}`TorchBatchIntensityFieldExportPayload ` | | {attr}`batch field.derivatives ` | {class}`TorchBatchFieldDerivativeCollection ` | | {meth}`batch field.derivatives[name] ` for a real input | {class}`TorchBatchFieldDerivativePayload ` | | {meth}`batch field.derivatives[name] ` for a complex input | {class}`TorchBatchComplexFieldDerivativePayload ` with {attr}`.real ` and optional {attr}`.imag ` batch payloads | ```{eval-rst} .. autoclass:: jcmsuite.torch.results.TorchBatchPostprocessCollection :members: __getitem__, __iter__, __len__, titles ``` ```{eval-rst} .. autoclass:: jcmsuite.torch.results.TorchBatchPostprocessRecord :members: fields, to_dict, to_df, to_xarray ``` ```{eval-rst} .. autoclass:: jcmsuite.torch.results.TorchBatchFieldExportPayload :members: base, derivatives, select, intensity, transform, to_dict, to_df, to_xarray, __getitem__ ``` ```{eval-rst} .. autoclass:: jcmsuite.torch.results.TorchBatchIntensityFieldExportPayload :members: base, derivatives, select, to_dict, to_df, to_xarray, __getitem__ ``` ```{eval-rst} .. autoclass:: jcmsuite.torch.results.TorchBatchFieldDerivativeCollection :members: keys, __getitem__ ``` ```{eval-rst} .. autoclass:: jcmsuite.torch.results.TorchBatchFieldDerivativePayload :members: derivatives, select, to_dict ``` ```{eval-rst} .. autoclass:: jcmsuite.torch.results.TorchBatchComplexFieldDerivativePayload :members: real, imag, derivatives, select ```