Signature converting functionality for (eventual) backend compatibility #31
+723
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Introduces the
convert_signature
function, which (given suitable information) can be used to convert the call signature of one function to another. This is a stepping stone towards theTranslation
andTranslator
classes that build on top of this, see #33.convert_signature
allows us to automate the process of mapping the signatures of some backend object to the signatures our frontend objects expect. It provides a means for a user to programmatically incorporate an unsupported backend into the framework thatcausalprog
requires for solving problems.TLDR / example
Suppose some custom library offers a means of drawing samples from a normal distribution, using the syntax
backend_sample(number, seed)
. This function can be used to draw samples, in place of our native distributions. However, whilst the functionality is provided, the syntax is incorrect; our frontend expects to draw samples viasample(rng_key, sample_shape)
.All that needs to be associated is that the
rng_key
parameter in the frontend should be passed as theseed
parameter to the backend, and similarly thesample_shape
parameter needs to be passed as thenumber
parameter. This can be done viaconvert_signature
, which essentially will return the functionCaveats
There are certain assumptions that
convert_signature
has to make in order to cast one signature into another. The function also needs to be provided with the mappings between parameter names, and (optionally) how to handle parameters that might need to be dropped.convert_signature
(and what is built on top of it in #33) is effective for those who want a "plug-and-go" style approach to the package, and is quite effective for just "inserting" a backend to be used in one-or-two places. If a user is planning to exclusively use a particular backend, they are likely better off creating their own custom classes to automate the process (to save repeat-passing of the information into the variousDistribution
s they'll need to create).