Skip to content

A new AdapterOperator hierarchy, usable for convenient device/impl changes in operator pipelines. - #1722

Merged
leftaroundabout merged 19 commits into
odlgroup:masterfrom
leftaroundabout:adaptors/device-impl-change-operators
Feb 20, 2026
Merged

leftaroundabout merged 19 commits into
odlgroup:masterfrom
leftaroundabout:adaptors/device-impl-change-operators

Conversation

@leftaroundabout

Copy link
Copy Markdown
Contributor

No description provided.

@leftaroundabout
leftaroundabout force-pushed the adaptors/device-impl-change-operators branch from ca93aa8 to 59f96e4 Compare January 21, 2026 13:56
leftaroundabout and others added 17 commits January 29, 2026 14:42
This will be used for device changes etc. in pipelines that cannot
use a single backend / device throughout.
…ators for their `_call`s.

This avoids duplicate definition of the logic in the raw and operatorized form of an adapter.
… `@` over `*`.

We decided earlier that the use of `*` for composition is confusing / error prone because it
contradicts the behaviour of array libraries. The documentation should reflect that.
In 9a6761e that was accidentally changed, so for example the `repr` of an
`OperatorComp` would show the exact class for the outer constructor but then
defer to the shorter but less precise `str` for the operators being composed.
This lead to inconsistent appearances.
They can now be composed both with other adapters, and with ordinary operators
in arbitrary order.
These are unlikely to be useful per se, but make sense to include for completeness.
What actually will be used in practice are the inverses of the concrete operator
forms of the adapters (which is what `AdapterInverse` defers to).
…adapters.

This is useful for only changing one (or some) of the participants of a
product-space input.
As it turns out, using `odl_impl_device_pairs` directly in this function
would cause it to see always the same impl and device as the `tspace`,
since that itself is based on `odl_impl_device_pairs`.
In that case the adapters are of course all trivial!
Fortunately, the adapters themselves still work also under properly
scrutinizing tests.
@leftaroundabout
leftaroundabout force-pushed the adaptors/device-impl-change-operators branch from 44fdcee to 79ac0eb Compare January 29, 2026 18:41
@leftaroundabout
leftaroundabout marked this pull request as ready for review January 29, 2026 18:42
@leftaroundabout

Copy link
Copy Markdown
Contributor Author

I'd say this feature is complete enough now, all that's left to do is mentioning the adapters in the Readthedocs.

>>> odl.rn(2).element([8,9]).to_impl('pytorch')
rn(2, 'float64', 'pytorch').element([ 8., 9.])

These cannot be used as part of operator pipelines, though.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"These cannot be used as part of operator pipelines, though." Finishing the documentation with this sentence reads like the AdapterOperators cannot be used on pipelines. I would rephrase it as "Converting the device or implementation of a TensorSpace using the to_device/to_impl methods is not suited for operator composition, hence the need for the AdapterOperators."

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, this might be misunderstood.

Comment thread docs/source/guide/numpy_guide.rst Outdated
raise OpTypeError(
odl.OpTypeError: `range` rn(4, 'float64', 'pytorch') of the right operator x * [ 10., 20., 30., 40.] not equal to the domain rn(4) of the left operator x * [ 1., 2., 3., 4.]

In situations where you are sure you need a copying step, you can use adapter-operators.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"... which wrap backend-specific device and implementation conversion and Operator syntax."

Comment thread docs/source/guide/numpy_guide.rst Outdated
In situations where you are sure you need a copying step, you can use adapter-operators.
These are conceptually identity operators but with different backends / devices for their domain and range.
For example, to change the device from CPU to the GPU, use `DeviceChange` with ``domain_device='cpu'`` and ``range_device='cuda:0'``.
Beware that the domain device is the first/left argument here, whereas the `@` composition has a right-to-left dataflow!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Note that, unlike the @ composition, which has a right-to-left dataflow, the AdapterOperator has a domain-to-device syntax."

@Emvlt Emvlt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AdapterOperator class looks great! Good work @leftaroundabout :)

I would make some changes to the documentation (hence the Request Changes), but the tests pass on my env :)

Condition to these passing on the CUDA-equiped machines, we should be able to merge the changes.

>>> odl.rn(2).element([8,9]).to_impl('pytorch')
rn(2, 'float64', 'pytorch').element([ 8., 9.])

These cannot be used as part of operator pipelines, though.

@leftaroundabout leftaroundabout Feb 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
These cannot be used as part of operator pipelines, though.
These methods are not fully-fledged operators (with adjoint etc.) though, so for conversion within operator pipelines it is necessary to use `AdapterOperator`s.

Hm actually that would also be misleading because AdapterOperator does not itself offer adjoint, either...

Suggested change
These cannot be used as part of operator pipelines, though.
These methods are not standalone objects though, so for conversion within operator pipelines it is necessary to use `AdapterOperator`s.

Comment thread docs/source/guide/numpy_guide.rst Resolved
Comment thread docs/source/guide/numpy_guide.rst Resolved
Comment thread docs/source/guide/numpy_guide.rst Resolved
@leftaroundabout
leftaroundabout merged commit 3605751 into odlgroup:master Feb 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants