global parameters vs. pure functions #6754
Unanswered
mkolodziejczyk-piap
asked this question in
Q&A
Replies: 1 comment
-
If you have global parameters that you can guarantee will stay constant, it's fine to reference them in compiled functions. For example: from jax import jit
y = 1
@jit
def f(x):
return x + y
print(f(1))
# 2 Such parameters are effectively treated as compile-time constants. Note however that if you mutate the parameters, it does not trigger a re-compile: y = 10
print(f(1))
# 2 If you want mutations of your global state to be reflected in function calls, it's best to pass those parameters directly; e.g. @jit
def f(x, y):
return x + y
print(f(1, y))
# 11 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, I'd like to ask what is the recommended way to use global parameters (constants) in functions. As stated in the docs:
But usually we have a bunch of parameters, sometimes gathered in some kind of config object (e.g. dict). Do we have to pass it in each function to remain "pure"?
Here is an example from google mppi controller that use class constrants directly:
BTW. Does "self" reference in class functions cause any problems?
Regards,
Beta Was this translation helpful? Give feedback.
All reactions