Skip to content

Commit

Permalink
Merge pull request #101 from PAICookers/bugfix
Browse files Browse the repository at this point in the history
bugfix
  • Loading branch information
KafCoppelia authored May 21, 2024
2 parents 4c4fae1 + ab5fab3 commit 3778111
Show file tree
Hide file tree
Showing 18 changed files with 118 additions and 127 deletions.
2 changes: 1 addition & 1 deletion .github/release-drafter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ template: |
$CHANGES
Changelog: https://github.com/PAICookers/PAIBox/compare/$PREVIOUS_TAG...v$RESOLVED_VERSION
**Full Changelog**: https://github.com/PAICookers/PAIBox/compare/$PREVIOUS_TAG...v$RESOLVED_VERSION
14 changes: 11 additions & 3 deletions .github/workflows/pytest-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ name: Python CI with pytest

on:
pull_request:
branches: [master, dev]
branches:
- master
- dev
types: [opened, synchronize, reopened]

permissions:
contents: read

jobs:
pytest-ci:
Expand All @@ -14,17 +20,19 @@ jobs:

steps:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install poetry
uses: abatilo/actions-poetry@v2
with:
poetry-version: "1.8.2"

- name: Install test dependencies
run: |
poetry install --with test
- name: Run pytest
uses: pavelzw/pytest-action@v2
with:
Expand Down
6 changes: 2 additions & 4 deletions .github/workflows/release-drafter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@ on:
pull_request_target:
branches:
- master
types:
- closed
types: [opened, reopened, synchronize]

permissions:
contents: read

jobs:
update_release_draft:
permissions:
# write permission is required to create a github release
contents: write
pull-requests: write
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ default_install_hook_types: [pre-commit, prepare-commit-msg]
ci:
autofix_commit_msg: ":rotating_light: auto fix by pre-commit hooks"
autofix_prs: true
autoupdate_branch: master
autoupdate_branch: dev
autoupdate_schedule: monthly
autoupdate_commit_msg: ":arrow_up: auto update by pre-commit hooks"
repos:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<a href="https://github.com/PAICookers/PAIBox/blob/master/pyproject.toml">
<img src="https://img.shields.io/pypi/pyversions/paibox">
</a>
<a href="https://github.com/PAICookers/PAIBox/releases/tag/v1.1.0a1">
<img src="https://img.shields.io/github/v/release/PAICookers/PAIBox&color=orange">
<a href="https://github.com/PAICookers/PAIBox/releases/tag/v1.1.0a2">
<img src="https://img.shields.io/github/v/release/PAICookers/PAIBox?color=orange">
</a>
<a href="https://www.codefactor.io/repository/github/PAICookers/PAIBox">
<img src="https://img.shields.io/codefactor/grade/github/PAICookers/PAIBox/master?color=red">
Expand Down
25 changes: 13 additions & 12 deletions paibox/backend/conf_template.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from enum import Enum
from pathlib import Path
from typing import Any, ClassVar, Dict, List, Literal, NamedTuple, TypedDict
from typing import Any, ClassVar, Dict, List, NamedTuple, TypedDict

import numpy as np
from numpy.typing import NDArray
Expand Down Expand Up @@ -335,7 +335,7 @@ def gen_config_frames_by_coreconf(
write_to_file: bool,
fp: Path,
split_by_coord: bool,
format: Literal["txt", "bin", "npy"],
formats: List[str],
) -> Dict[Coord, FrameArrayType]:
"""Generate configuration frames by given the `CorePlmConfig`.
Expand All @@ -344,17 +344,18 @@ def gen_config_frames_by_coreconf(
- write_to_file: whether to write frames to file.
- fp: If `write_to_file` is `True`, specify the path.
- split_by_coord: whether to split the generated frames file by the core coordinates.
- format: `txt`, `bin`, or `npy`.
- format: a list of formats to export.
"""

def _write_to_f(name: str, array: FrameArrayType) -> None:
_fp = fp / (name + f".{format}")
if format == "npy":
np2npy(_fp, array)
elif format == "bin":
np2bin(_fp, array)
else:
np2txt(_fp, array)
for format in formats:
_fp = fp / (name + f".{format}")
if format == "npy":
np2npy(_fp, array)
elif format == "bin":
np2bin(_fp, array)
else:
np2txt(_fp, array)

_default_rid = RId(0, 0)
_debug_dict: Dict[Coord, Dict[str, Any]] = dict()
Expand Down Expand Up @@ -443,10 +444,10 @@ def _write_to_f(name: str, array: FrameArrayType) -> None:
addr = core_coord.address
_write_to_f(f"config_core{addr}", f)
else:
_f = np.concatenate(
f = np.concatenate(
list(frame_arrays_on_core.values()), dtype=FRAME_DTYPE, casting="no"
)
_write_to_f(f"config_cores_all", _f)
_write_to_f(f"config_cores_all", f)

return frame_arrays_on_core

Expand Down
8 changes: 6 additions & 2 deletions paibox/backend/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


class _BackendContext(_Context):
_DefaultContext = {
_default = {
"output_chip_addr": DEFAULT_OUTPUT_CHIP_ADDR, # RO mostly
"target_chip_addr": DEFAULT_LOCAL_CHIP_ADDR, # RO mostly
"build_directory": Path.cwd(), # R/W
Expand All @@ -31,7 +31,7 @@ class _BackendContext(_Context):

def __init__(self) -> None:
super().__init__()
self.update(self._DefaultContext)
self.update(self._default)

@property
def target_chip_addr(self) -> List[ChipCoord]:
Expand Down Expand Up @@ -87,6 +87,10 @@ def cflags(self) -> Dict[str, Any]:
"""Compilation options."""
return self["cflags"]

def set_default(self) -> None:
self.clear_all()
self.update(self._default)


_BACKEND_CONTEXT = _BackendContext()

Expand Down
14 changes: 13 additions & 1 deletion paibox/backend/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ def export(
format: Literal["txt", "bin", "npy"] = "bin",
split_by_coord: bool = False,
export_core_params: bool = False,
use_hw_sim: bool = True,
) -> Dict[Coord, Any]:
"""Generate configuration frames & export to file.
Expand All @@ -511,15 +512,26 @@ def export(
- format: `txt`, `bin`, or `npy`. `bin` is recommended.
- split_by_coord: whether to split the generated frames file by the core coordinates.
- export_core_params: whether to export the parameters of occupied cores.
- use_hw_sim: whether to use hardware simulator. If use, '.txt' will be exported.
Return: a dictionary of configurations.
"""
if format not in ("bin", "npy", "txt"):
raise ValueError(f"format {format} is not supported.")

formats = [format]
if use_hw_sim:
formats.append("txt")

formats = list(set(formats))

_fp = _fp_check(fp)
config_dict = gen_config_frames_by_coreconf(
self.graph_info["members"], write_to_file, _fp, split_by_coord, format
self.graph_info["members"],
write_to_file,
_fp,
split_by_coord,
formats,
)

if export_core_params:
Expand Down
2 changes: 1 addition & 1 deletion paibox/components/neuron/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def __init__(
"""Stateless attributes. Scalar."""
# Basic attributes.
self.keep_shape = keep_shape
self._n_neuron = shape2num(shape)
self._shape = as_shape(shape)
self._n_neuron = shape2num(self._shape)

# DO NOT modify the names of the following variables.
# They will be exported to the parameter verification model.
Expand Down
4 changes: 2 additions & 2 deletions paibox/components/synapses/synapses.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ def __init__(
kernel,
_single(stride),
_single(padding),
_single(output_padding),
_single(1),
_single(output_padding),
kernel_order,
name,
)
Expand Down Expand Up @@ -241,8 +241,8 @@ def __init__(
kernel,
_pair(stride),
_pair(padding),
_pair(output_padding),
_pair(1),
_pair(output_padding),
kernel_order,
name,
)
2 changes: 1 addition & 1 deletion paibox/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from numpy.typing import NDArray
from typing_extensions import TypeAlias

Shape = TypeVar("Shape", int, Tuple[int, ...], List[int])
Shape = TypeVar("Shape", int, Tuple[int, ...], List[int], np.ndarray)
ArrayType = TypeVar("ArrayType", List[int], Tuple[int, ...], np.ndarray)
Scalar = TypeVar("Scalar", int, float, np.generic)
IntScalarType = TypeVar("IntScalarType", int, np.bool_, np.integer)
Expand Down
92 changes: 5 additions & 87 deletions paibox/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Iterable, List, Optional, Sequence, Tuple, Union
from typing import Any, Iterable, List, Sequence, Tuple

import numpy as np

Expand Down Expand Up @@ -72,6 +72,8 @@ def shape2num(shape: Shape) -> int:
"""Convert a shape to a number"""
if isinstance(shape, int):
return shape
elif isinstance(shape, np.ndarray):
return int(np.prod(shape))
else:
a = 1
for b in shape:
Expand All @@ -83,12 +85,9 @@ def shape2num(shape: Shape) -> int:
def as_shape(x, min_dim: int = 0) -> Tuple[int, ...]:
"""Return a tuple if `x` is iterable or `(x,)` if `x` is integer."""
if is_integer(x):
_shape = (x,)
_shape = (int(x),)
elif is_iterable(x):
if isinstance(x, np.ndarray):
_shape = tuple(x.astype(int))
else:
_shape = tuple(x)
_shape = tuple(int(e) for e in x)
else:
raise ValueError(f"{x} cannot be safely converted to a shape.")

Expand Down Expand Up @@ -131,84 +130,3 @@ def is_iterable(obj: Any) -> bool:
def fn_sgn(a, b) -> int:
"""Signal function."""
return 1 if a > b else -1 if a < b else 0


def bin_split(x: int, pos: int, high_mask: Optional[int] = None) -> Tuple[int, int]:
"""Split an integer, return the high and low part.
Argument:
- x: the integer
- pos: the position (LSB) to split the binary.
- high_mask: mask for the high part. Optional.
Example::
>>> bin_split(0b1100001001, 3)
97(0b1100001), 1
"""
low = x & ((1 << pos) - 1)

if isinstance(high_mask, int):
high = (x >> pos) & high_mask
else:
high = x >> pos

return high, low


def bin_combine(high: int, low: int, pos: int) -> int:
"""Combine two integers, return the result.
Argument:
- high: the integer on the high bit.
- low: the integer on the low bit.
- pos: the combination bit if provided. Must be equal or greater than `low.bit_length()`.
Example::
>>> bin_combine(0b11000, 0b101, 5)
773(0b11000_00101)
"""
if pos < 0:
raise ValueError("position must be greater than 0")

if low > 0 and pos < low.bit_length():
raise ValueError(
f"Postion of combination must be greater than the bit length of low({low.bit_length()})"
)

return (high << pos) + low


def bin_combine_x(*components: int, pos: Union[int, List[int], Tuple[int, ...]]) -> int:
"""Combine more than two integers, return the result.
Argument:
- components: the list of integers to be combined.
- pos: the combination bit(s) if provided. Every bit must be equal or greater than `low.bit_length()`.
Example::
>>> bin_combine_x(0b11000, 0b101, 0b1011, pos=[10, 5])
24747(0b11000_00101_01011)
"""
if isinstance(pos, (list, tuple)):
if len(components) != len(pos) + 1:
raise ValueError(
f"Length of components and positions illegal: {len(components)}, {len(pos)}"
)
else:
if len(components) != 2:
raise ValueError(
f"Length of components must be 2: {len(components)} when position is an integer."
)

return bin_combine(*components, pos=pos)

result = components[-1]

# Traverse every position from the end to the start
for i in range(len(pos) - 1, -1, -1):
result = bin_combine(components[i], result, pos[i])

return result
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3778111

Please sign in to comment.