question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Decorator factory pattern

See original GitHub issue

I 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:open
  • Created a year ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
jakevdpcommented, Mar 29, 2022

#10074 adds some documentation of the @partial(jit, ...) pattern in a couple places.

1reaction
msegadocommented, Aug 12, 2022

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 🙂

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found