Decorator factory pattern
See original GitHub issueI couldn’t find a pre-existing issue for this, but I figure it might be useful to add support for the decorator factory pattern for the most common transformations (grad, jit, vmap, pmap
, etc).
Currently people wanting to decorate functions but also pass parameters to the transformation have to either do:
from functools import partial
from jax import jit
@partial(jit, param1=..., param2=...)
def foo(bar):
...
Or
from jax import jit
def foo(bar):
...
foo = jit(foo, param1=..., param2=...)
This seems needlessly verbose, but I am wondering if perhaps there is some rationale behind this that I might not be aware of. Being used to numba
’s jit
decorator, I would expect the decorator factory pattern to work.
I think it would be super neat if the following pattern was supported:
from jax import jit
@jit(param1=..., param2=...)
def foo(bar):
...
Issue Analytics
- State:
- Created a year ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
What are the benefits of using a 'decorator factory' that ...
Therefore I say using a factory to decorate your objects is a good thing. Maybe even a Builder pattern. Please be aware I...
Read more >oop - Use decorator and factory together to add properties or ...
Design pattern Adaptor/Proxy is basically this: Create a new class that has the desired interface, and implements some (or all) of its methods...
Read more >Decorator Factory in Python - Medium
A decorator factory is a function that returns a decorator. Instead of returning a function (inner in our case), we will return a...
Read more >C# Decorator Design Pattern - Dofactory
The Decorator design pattern attaches additional responsibilities to an object dynamically. This pattern provide a flexible alternative to subclassing for ...
Read more >Decorator and Factory - Semantic Scholar
The Decorator Pattern provides a powerful mechanism for adding new behaviors to an object at run-time. • The mechanism is based on the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
#10074 adds some documentation of the
@partial(jit, ...)
pattern in a couple places.Came here to suggest this and found @JeppeKlitgaard had beat me to it… don’t want to bikeshed though so please close this if you prefer to focus on other things now that the current behavior is documented!
That said, one argument in favor of supporting the decorator-or-factory pattern is that the Python standard library does so in several places:
I’d argue that the convenience is worth it given that the jax team itself makes use of both
@dataclass
and@dataclass(...)
in their source code 🙂