class brainpy.math.cls_jit(func=None, static_argnums=None, static_argnames=None, inline=False, keep_unused=False, abstracted_axes=None, **kwargs)[source]#

Just-in-time compile a function and then the jitted function as the bound method for a class.


This transformation can be put on any class function. For example,

>>> import brainpy as bp
>>> import brainpy.math as bm
>>> class SomeProgram(bp.BrainPyObject):
>>>   def __init__(self):
>>>      super(SomeProgram, self).__init__()
>>>      self.a = bm.zeros(2)
>>>      self.b = bm.Variable(bm.ones(2))
>>>   @bm.cls_jit(inline=True)
>>>   def __call__(self, *args, **kwargs):
>>>      a = bm.random.uniform(size=2)
>>>      a =[0].set(1.)
>>>      self.b += a
>>> program = SomeProgram()
>>> program()
  • func (BrainPyObject, function, callable) – The instance of Base or a function.

  • static_argnums (optional, int, sequence of int) – An optional int or collection of ints that specify which positional arguments to treat as static (compile-time constant). Operations that only depend on static arguments will be constant-folded in Python (during tracing), and so the corresponding argument values can be any Python object.

  • static_argnames (optional, str, list, tuple, dict) – An optional string or collection of strings specifying which named arguments to treat as static (compile-time constant). See the comment on static_argnums for details. If not provided but static_argnums is set, the default is based on calling inspect.signature(fun) to find corresponding named arguments.

  • donate_argnums (int, sequence of int) – Specify which positional argument buffers are “donated” to the computation. It is safe to donate argument buffers if you no longer need them once the computation has finished. In some cases XLA can make use of donated buffers to reduce the amount of memory needed to perform a computation, for example recycling one of your input buffers to store a result. You should not reuse buffers that you donate to a computation, JAX will raise an error if you try to. By default, no argument buffers are donated. Note that donate_argnums only work for positional arguments, and keyword arguments will not be donated.

  • device (optional, Any) – This is an experimental feature and the API is likely to change. Optional, the Device the jitted function will run on. (Available devices can be retrieved via jax.devices().) The default is inherited from XLA’s DeviceAssignment logic and is usually to use jax.devices()[0].

  • keep_unused (bool) – If False (the default), arguments that JAX determines to be unused by fun may be dropped from resulting compiled XLA executables. Such arguments will not be transferred to the device nor provided to the underlying executable. If True, unused arguments will not be pruned.

  • backend (optional, str) – This is an experimental feature and the API is likely to change. Optional, a string representing the XLA backend: 'cpu', 'gpu', or 'tpu'.

  • inline (bool) – Specify whether this function should be inlined into enclosing jaxprs (rather than being represented as an application of the xla_call primitive with its own subjaxpr). Default False.


func – A callable jitted function, set up for just-in-time compilation.

Return type: