Skip to content

Commit

Permalink
reformat with black
Browse files Browse the repository at this point in the history
  • Loading branch information
vighneshiyer committed Feb 20, 2024
1 parent 1cfd2cc commit 579e7bd
Show file tree
Hide file tree
Showing 29 changed files with 1,137 additions and 639 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@
- `+perf-file` is where performance statistics are dumped to (CSV format)
- `+max-instructions` terminates the simulation after the specified number of instructions have committed

## Dev Notes

- To run unittests: `pytest`
- To run a specific test: `pytest tests/test_mtr.py -rA -k "with_data"` (`-k` specifies a string fragment of test function you want to run, `-rA` shows the test stdout output)
- Some tests use temporary directories to do their work, those directories are persisted here: `ls /tmp/pytest-of-<user>/pytest-current/<test function>/`
- To typecheck: `poetry run pyright`
- To format: `poetry run black tidalsim`, `poetry run black tests`

---

---
Expand Down
75 changes: 73 additions & 2 deletions poetry.lock

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

8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pyarrow = "^15.0.0"
pytest = "^8.0.0"
pyright = "^1.1.350"
setuptools = "^69.1.0"
black = "^24.2.0"

[build-system]
requires = ["poetry-core"]
Expand All @@ -42,3 +43,10 @@ exclude = ["tidalsim/archive/**"]
include = ["tidalsim", "tests"]
reportMissingImports = true
typeCheckingMode = "standard"

[tool.black]
line-length = 100
include = "tidalsim|tests"
extend-exclude = "tidalsim/archive/.*"
unstable = true
preview = true
3 changes: 0 additions & 3 deletions run_pyright.sh

This file was deleted.

33 changes: 16 additions & 17 deletions tests/test_bbv_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,32 @@

from tidalsim.bb.spike import *


class TestBBVEmbedding:
def test_embedding(self) -> None:
trace = [
SpikeTraceEntry(0x4, "", 0),
SpikeTraceEntry(0x8, "", 1),
SpikeTraceEntry(0xc, "", 2),
SpikeTraceEntry(0x4, "", 0),
SpikeTraceEntry(0x8, "", 1),
SpikeTraceEntry(0xC, "", 2),
SpikeTraceEntry(0x10, "", 3),
SpikeTraceEntry(0x18, "", 4),
SpikeTraceEntry(0x4, "", 5),
SpikeTraceEntry(0x8, "", 6),
SpikeTraceEntry(0x4, "", 5),
SpikeTraceEntry(0x8, "", 6),
]
extracted_bb = BasicBlocks(
markers=[(0, 0), (0x8+1, None), (0xc, 1), (0x18+1, None)]
)
extracted_bb = BasicBlocks(markers=[(0, 0), (0x8 + 1, None), (0xC, 1), (0x18 + 1, None)])

df = spike_trace_to_embedding_df(iter(trace), extracted_bb, 2)
ref = DataFrame[EmbeddingSchema]({
'instret': [2, 2, 2, 1],
'inst_count': [2, 4, 6, 7],
'inst_start': [0, 2, 4, 6],
'embedding': [
np.array([1., 0.]),
np.array([0., 1.]),
"instret": [2, 2, 2, 1],
"inst_count": [2, 4, 6, 7],
"inst_start": [0, 2, 4, 6],
"embedding": [
np.array([1.0, 0.0]),
np.array([0.0, 1.0]),
np.array([0.5, 0.5]),
np.array([1., 0.])
]
np.array([1.0, 0.0]),
],
})
# [spike_trace_to_embedding_df] returns the embedding already with unit L2 norm per row
ref['embedding'] = ref['embedding'].transform(lambda x: x / np.linalg.norm(x))
ref["embedding"] = ref["embedding"].transform(lambda x: x / np.linalg.norm(x))
assert ref.equals(df)
38 changes: 25 additions & 13 deletions tests/test_cache_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

from tidalsim.cache_model.cache import *


class TestCacheModel:
params = CacheParams(phys_addr_bits=32, block_size_bytes=64, n_sets=64, n_ways=4)
state = CacheState(params)
state.fill_with_structured_data()

def check_tag_bin_file(self, file_contents: List[str], way_idx: int) -> None:
for set_idx in range(self.params.n_sets):
assert (int(file_contents[set_idx], 2) & self.params.tag_mask) == self.state.array[way_idx][set_idx].tag
assert (int(file_contents[set_idx], 2) & self.params.tag_mask) == self.state.array[
way_idx
][set_idx].tag

# Run these using pytest -rA to show the printed arrays for manual inspection
def test_tag_array_pretty_printing(self) -> None:
Expand All @@ -19,18 +22,18 @@ def test_tag_array_pretty_printing(self) -> None:
def test_tag_array_bin(self) -> None:
for way_idx in range(self.params.n_ways):
s = self.state.tag_array_binary_str(way_idx)
s_split = s.split('\n')
self.check_tag_bin_file(list(s.split('\n')), way_idx)
s_split = s.split("\n")
self.check_tag_bin_file(list(s.split("\n")), way_idx)
print(self.state.tag_array_binary_str(0))

def test_dump_tag_arrays(self, tmp_path: Path) -> None:
prefix = "dcache_tag_array"
self.state.dump_tag_arrays(tmp_path, prefix)
assert (tmp_path / f"{prefix}.pretty").exists()
for way_idx in range(self.params.n_ways):
bin_file = tmp_path / f'{prefix}{way_idx}.bin'
bin_file = tmp_path / f"{prefix}{way_idx}.bin"
assert bin_file.exists()
with bin_file.open('r') as f:
with bin_file.open("r") as f:
lines = [line.rstrip() for line in f]
self.check_tag_bin_file(lines, way_idx)

Expand All @@ -42,24 +45,30 @@ def test_data_array_bin(self) -> None:
data_bus_bytes = 8
for way_idx in range(self.params.n_ways):
s = self.state.data_array_binary_str(way_idx)
s_split = s.split('\n')
s_split = s.split("\n")
for set_idx in range(self.params.n_sets):
data_from_bin = s_split[data_bus_bytes*set_idx:data_bus_bytes*(set_idx+1)]
assert int(''.join(reversed(data_from_bin)), 2) == self.state.array[way_idx][set_idx].data
data_from_bin = s_split[data_bus_bytes * set_idx : data_bus_bytes * (set_idx + 1)]
assert (
int("".join(reversed(data_from_bin)), 2)
== self.state.array[way_idx][set_idx].data
)
print(self.state.data_array_binary_str(way_idx=0))

def test_dump_data_arrays(self, tmp_path: Path) -> None:
prefix = "dcache_data_array"
self.state.dump_data_arrays(tmp_path, prefix)
assert (tmp_path / f"{prefix}.pretty").exists()

bin_files = [(tmp_path / f"{prefix}{i}.bin") for i in range(self.params.data_bus_bytes * self.params.n_ways)]
bin_files = [
(tmp_path / f"{prefix}{i}.bin")
for i in range(self.params.data_bus_bytes * self.params.n_ways)
]
assert all([x.exists() for x in bin_files])

# Read every .bin file into a list that's first indexed by byte # then row #
bin_file_data: List[List[str]] = []
for filename in bin_files:
with filename.open('r') as f:
with filename.open("r") as f:
lines = [line.rstrip() for line in f]
bin_file_data.append(lines)
assert len(bin_file_data) == self.params.data_bus_bytes * self.params.n_ways
Expand All @@ -69,13 +78,16 @@ def read_row(way_idx: int, row_idx: int) -> str:
start = way_idx * self.params.data_bus_bytes
end = start + self.params.data_bus_bytes
row = [bin_file_data[byte_idx][row_idx] for byte_idx in range(start, end)]
row_str = ''.join(reversed(row))
row_str = "".join(reversed(row))
return row_str

# Get out the full data word for a set
def read_set(way_idx: int, set_idx: int) -> int:
set_data = [read_row(way_idx, set_idx*self.params.data_rows_per_set + beat) for beat in range(self.params.data_bus_bytes)]
return int(''.join(reversed(set_data)), 2)
set_data = [
read_row(way_idx, set_idx * self.params.data_rows_per_set + beat)
for beat in range(self.params.data_bus_bytes)
]
return int("".join(reversed(set_data)), 2)

for way_idx in range(self.params.n_ways):
for set_idx in range(self.params.n_sets):
Expand Down
13 changes: 5 additions & 8 deletions tests/test_clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@

from tidalsim.modeling.clustering import *


class TestClustering:
def test_get_closest_samples_to_centroids(self) -> None:
centroids = np.array([
[1, 10, 50],
[0, 0, 1],
[10, 100, 40]
])
centroids = np.array([[1, 10, 50], [0, 0, 1], [10, 100, 40]])
samples = np.array([
[0.1, 0.5, 1], # closest to centroid 1
[10, 90, 30], # closest to centroid 2
[0.1, 0.5, 1], # closest to centroid 1
[10, 90, 30], # closest to centroid 2
[0, 10, 45],
[1, 10, 47] # closest to centroid 0
[1, 10, 47], # closest to centroid 0
])
argmin, distances = get_closest_samples_to_centroids(centroids, samples)
assert argmin[0] == 3
Expand Down
Loading

0 comments on commit 579e7bd

Please sign in to comment.