Prefetch#

class brainstate.nn.Prefetch(module, item)[source]#

Prefetch a state or variable in a module before it is initialized.

This class provides a mechanism to reference a module’s state or attribute that may not have been initialized yet. It acts as a placeholder or reference that will be resolved when called.

Use cases: - Access variables within dynamics modules that will be defined later - Create references to states across module boundaries - Enable access to delayed states through the .delay property

Parameters:
  • module (Dynamics) – The module that contains or will contain the referenced item.

  • item (str) – The attribute name of the state or variable to prefetch.

Examples

>>> import brainstate
>>> import brainunit as u
>>> neuron = brainpy.state.LIF(...)
>>> v_reference = neuron.prefetch('V')  # Reference to voltage before initialization
>>> v_value = v_reference()  # Get the current value
>>> delay_ref = v_reference.delay.at(5.0 * u.ms)  # Reference voltage delayed by 5ms

Notes

When called, this class retrieves the current value of the referenced item. Use the .delay property to access delayed versions of the state.

property delay#

Access delayed versions of the prefetched item.

Returns:

An object that provides access to delayed versions of the prefetched item.

Return type:

PrefetchDelay

get_item()[source]#

Get the referenced item object itself, not its value.

Returns:

The actual referenced item from the module, which could be a State object or any other attribute.

Return type:

Any

get_item_value()[source]#

Get the current value of the prefetched item.

Similar to __call__, but explicitly named for clarity.

Returns:

The current value of the referenced item. If the item is a State object, returns its value attribute, otherwise returns the item itself.

Return type:

Any