@@ -3,14 +3,19 @@ Backend API
33
44Backends have to do the heavy lifting of using spatch.
55At the moment we suggest to check the
6- `example <https://github.com/scientific-python/spatch/tree/main/spatch/_spatch_example >`_.
6+ `example <https://github.com/scientific-python/spatch/tree/main/src/ spatch/_spatch_example >`_.
77
88Entry point definition
99----------------------
10- To extend an existing library with a backend, you need to define a `Python entry-point <https://packaging.python.org/en/latest/specifications/entry-points/ >`_.
10+ To extend an existing library with a backend, you need to define a
11+ `Python entry-point <https://packaging.python.org/en/latest/specifications/entry-points/ >`_.
1112This entry point includes the necessary information for spatch to find and
1213dispatch to your backend.
1314
15+ ``spatch `` entry-points are TOML files and *not * Python objects.
16+ The entry-point value is ``module:path/to/entrypoint.toml `` rather than
17+ the typical pattern of ``module.submodule:value ``.
18+
1419Before writing a backend, you need to think about a few things:
1520
1621* Which types do you accept? This could be NumPy, dask, jax, etc. arrays.
@@ -25,11 +30,11 @@ Before writing a backend, you need to think about a few things:
2530 In that case, your backend should likely only be used if prioritized
2631 by the user.
2732
28- Please check the example linked above. These example entry-points include
29- code that means running them modifies them in-place if the ``@implements ``
30- decorator is used (see next section).
33+ Please check the example linked above. `` spatch `` can automatically update
34+ the functions entries in these entry-points if the ``@implements `` decorator
35+ is used (see next section).
3136
32- Some of the most important things are:
37+ Some of the most important fields are:
3338
3439``name ``
3540^^^^^^^^
@@ -55,13 +60,14 @@ However, we do support the following, e.g.:
5560- ``"~numpy:ndarray" `` to match any subclass of NumPy arrays
5661- ``"@module:qualname" `` to match any subclass of an abstract base class
5762
58- If you use an abstract base class, note that you must take a lot of care:
63+ .. warning ::
64+ If you use an abstract base class, note that you must take additional care:
5965
60- - The abstract base class must be cheap to import, because we cannot avoid
61- importing it.
62- - Since we can't import all classes, ``spatch `` has no ability to order abstract
63- classes correctly (but we order them last if a primary type, which is typically right).
64- - ``spatch `` will not guarantee correct behavior if an ABC is mutated at runtime.
66+ - The abstract base class must be * cheap to import * , because we cannot avoid
67+ importing it.
68+ - Since we can't import all classes, ``spatch `` has no ability to order abstract
69+ classes correctly (but we order them last if a primary type, which is typically right).
70+ - ``spatch `` will not guarantee correct behavior if an ABC is mutated at runtime.
6571
6672``requires_opt_in ``
6773^^^^^^^^^^^^^^^^^^^
@@ -93,19 +99,36 @@ functions
9399^^^^^^^^^
94100
95101A mapping of library functions to your implementations. All fields use
96- the ``__module__:__qualname__ `` identifiers to avoid immediate import .
102+ the ``__module__:__qualname__ `` identifiers.
97103The following fields are supported for each function:
98104
99105- ``function ``: The implementation to dispatch to.
100106- ``should_run `` (optional): A function that gets all inputs (and context)
101107 and can decide to defer. Unless you know things will error, try to make sure
102108 that this function is light-weight.
103- - ``uses_context ``: Whether the implementation needs a ``DispatchContext ``.
109+ - ``uses_context `` (optional) : Whether the implementation needs a ``DispatchContext ``.
104110- ``additional_docs `` (optional): Brief text to add to the documentation
105111 of the original function. We suggest keeping this short but including a
106112 link, but the library guidance should be followed.
107113
108- ``spatch `` provides tooling to help create this mapping.
114+ A typical part of the entry-point TOML will look like this::
115+
116+ [functions."skimage.filters:gaussian"]
117+ function = "cucim.skimage.filters:gaussian"
118+ uses_context = true
119+ additional_docs = "CUDA enabled version..."
120+
121+ An additional ``[functions.defaults] `` key can be added to set defaults for all
122+ functions and avoid repeating e.g. ``uses_context ``.
123+
124+ ``spatch `` provides tooling to help create this mapping. This tooling uses the
125+ additional fields::
126+
127+ [functions.auto-generation]
128+ # where to find the BackendImplementation:
129+ backend = "module.submodule:backend_name"
130+ # Additional modules to be imported (to ensure all functions are found):
131+ modules = ["spatch._spatch_example.backend"]
109132
110133Manual backend prioritization
111134^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0 commit comments