Skip to content

Commit

Permalink
Updates (#51)
Browse files Browse the repository at this point in the history
* add `graph_invisible_attrs`

* define `ProgressBar` using integer

* add initial version of event `CSR` and `CSC`

* updates
  • Loading branch information
chaoming0625 authored Dec 19, 2024
1 parent ee6300a commit a4e64c2
Show file tree
Hide file tree
Showing 20 changed files with 1,229 additions and 58 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</a>
<a href="https://badge.fury.io/py/brainstate"><img alt="PyPI version" src="https://badge.fury.io/py/brainstate.svg"></a>
<a href="https://github.com/chaobrain/brainstate/actions/workflows/CI.yml"><img alt="Continuous Integration" src="https://github.com/chaobrain/brainstate/actions/workflows/CI.yml/badge.svg"></a>
<a href="https://pepy.tech/projects/brainstate"><img src="https://static.pepy.tech/badge/brainstate" alt="PyPI Downloads"></a>
</p>


Expand Down
20 changes: 14 additions & 6 deletions brainstate/compile/_loop_collect_return.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def scan(
length: int | None = None,
reverse: bool = False,
unroll: int | bool = 1,
pbar: ProgressBar | None = None,
pbar: ProgressBar | int | None = None,
) -> Tuple[Carry, Y]:
"""
Scan a function over leading array axes while carrying along state.
Expand Down Expand Up @@ -177,7 +177,13 @@ def scan(f, init, xs, length=None):
has_pbar = False
if pbar is not None:
has_pbar = True
f = _wrap_fun_with_pbar(f, pbar.init(length))
if isinstance(pbar, ProgressBar):
pbar_runner = pbar.init(length)
elif isinstance(pbar, int):
pbar_runner = ProgressBar(freq=pbar).init(length)
else:
raise TypeError("pbar argument should be a ProgressBar instance or an integer.")
f = _wrap_fun_with_pbar(f, pbar_runner)
init = (0, init) if pbar else init

# not jit
Expand Down Expand Up @@ -230,7 +236,7 @@ def checkpointed_scan(
xs: X,
length: Optional[int] = None,
base: int = 16,
pbar: Optional[ProgressBar] = None,
pbar: Optional[ProgressBar | int] = None,
) -> Tuple[Carry, Y]:
"""
Scan a function over leading array axes while carrying along state.
Expand Down Expand Up @@ -288,8 +294,10 @@ def checkpointed_scan(
length, = unique_lengths

# function with progress bar
if pbar is not None:
if isinstance(pbar, ProgressBar):
pbar_runner = pbar.init(length)
elif isinstance(pbar, int):
pbar_runner = ProgressBar(freq=pbar).init(length)
else:
pbar_runner = None

Expand Down Expand Up @@ -380,7 +388,7 @@ def for_loop(
length: Optional[int] = None,
reverse: bool = False,
unroll: int | bool = 1,
pbar: Optional[ProgressBar] = None
pbar: Optional[ProgressBar | int] = None
) -> Y:
"""
``for-loop`` control flow with :py:class:`~.State`.
Expand Down Expand Up @@ -432,7 +440,7 @@ def checkpointed_for_loop(
*xs: X,
length: Optional[int] = None,
base: int = 16,
pbar: Optional[ProgressBar] = None,
pbar: Optional[ProgressBar | int] = None,
) -> Y:
"""
``for-loop`` control flow with :py:class:`~.State` with a checkpointed version, similar to :py:func:`for_loop`.
Expand Down
2 changes: 2 additions & 0 deletions brainstate/compile/_progress_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class ProgressBar(object):

def __init__(self, freq: Optional[int] = None, count: Optional[int] = None, **kwargs):
self.print_freq = freq
if isinstance(freq, int):
assert freq > 0, "Print rate should be > 0."
self.print_count = count
if self.print_freq is not None and self.print_count is not None:
raise ValueError("Cannot specify both count and freq.")
Expand Down
14 changes: 8 additions & 6 deletions brainstate/event/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
# ==============================================================================


from ._csr import *
from ._csr_mv import *
from ._csr_mv import __all__ as __all_csr
from ._fixedprob_mv import *
from ._fixedprob_mv import __all__ as __all_fixed_probability
from ._linear_mv import *
from ._xla_custom_op import *
from ._xla_custom_op import __all__ as __all_xla_custom_op
from ._linear_mv import __all__ as __all_linear

__all__ = __all_fixed_probability + __all_linear + __all_csr + __all_xla_custom_op
del __all_fixed_probability, __all_linear, __all_csr, __all_xla_custom_op
__all__ = [
'CSRLinear',
'FixedProb',
'XLACustomOp',
'CSR',
'CSC',
]
Loading

0 comments on commit a4e64c2

Please sign in to comment.