Skip to content

Commit d8d7f81

Browse files
committed
updating variables names to match naming convention + adding site_bus_compatibility to arch
1 parent d2845de commit d8d7f81

File tree

6 files changed

+144
-161
lines changed

6 files changed

+144
-161
lines changed

src/bloqade/lanes/gemini.py

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,67 @@
11
import numpy as np
22

3-
from bloqade.lanes.types.arch import ArchSpec, Lane
4-
from bloqade.lanes.types.block import Block
3+
from bloqade.lanes.types.arch import ArchSpec, Bus
54
from bloqade.lanes.types.grid import Grid
65
from bloqade.lanes.types.numpy_compat import as_flat_tuple_int
6+
from bloqade.lanes.types.word import Word
77

88

9-
def intra_moves(block_size_y: int):
10-
intra_ids = np.arange(block_size_y * 2).reshape((block_size_y, 2))
9+
def site_buses(word_size_y: int):
10+
site_addresses = np.arange(word_size_y * 2).reshape((word_size_y, 2))
1111

12-
intra_lanes: list[Lane] = []
13-
for shift in range(block_size_y):
14-
intra_lanes.append(
15-
Lane(
16-
src=as_flat_tuple_int(intra_ids[: block_size_y - shift, 0]),
17-
dst=as_flat_tuple_int(intra_ids[shift:, 1]),
12+
site_buses: list[Bus] = []
13+
for shift in range(word_size_y):
14+
site_buses.append(
15+
Bus(
16+
src=as_flat_tuple_int(site_addresses[: word_size_y - shift, 0]),
17+
dst=as_flat_tuple_int(site_addresses[shift:, 1]),
1818
)
1919
)
2020

21-
return tuple(intra_lanes)
21+
return tuple(site_buses)
2222

2323

2424
def generate_arch():
2525

26-
block_size_y = 5
27-
block_size_x = 2
28-
num_block_x = 16
26+
word_size_y = 5
27+
word_size_x = 2
28+
num_word_x = 16
2929

3030
x_positions = (0.0, 2.0)
31-
y_positions = tuple(10.0 * i for i in range(block_size_y))
31+
y_positions = tuple(10.0 * i for i in range(word_size_y))
3232

3333
grid = Grid(x_positions, y_positions)
3434

35-
blocks = tuple(
36-
Block(grid.shift(10.0 * ix, 0.0).positions) for ix in range(num_block_x)
35+
words = tuple(
36+
Word(grid.shift(10.0 * ix, 0.0).positions) for ix in range(num_word_x)
3737
)
3838

39-
inter_lanes: list[Lane] = []
39+
word_buses: list[Bus] = []
4040
for shift in range(4):
4141
m = 1 << shift
4242

4343
srcs = []
4444
dsts = []
45-
for src in range(num_block_x):
45+
for src in range(num_word_x):
4646
if src & m != 0:
4747
continue
4848

4949
dst = src | m
5050
srcs.append(src)
5151
dsts.append(dst)
5252

53-
inter_lanes.append(Lane(tuple(srcs), tuple(dsts)))
53+
word_buses.append(Bus(tuple(srcs), tuple(dsts)))
5454

55-
block_ids = np.arange(block_size_x * block_size_y).reshape(
56-
block_size_y, block_size_x
57-
)
55+
word_ids = np.arange(word_size_x * word_size_y).reshape(word_size_y, word_size_x)
5856

57+
site_bus_compatibility = tuple(
58+
frozenset(j for j in range(num_word_x) if j != i) for i in range(num_word_x)
59+
)
5960
return ArchSpec(
60-
blocks=blocks,
61-
has_intra_lanes=frozenset(range(num_block_x)),
62-
has_inter_lanes=frozenset(as_flat_tuple_int(block_ids[:, 1])),
63-
intra_lanes=intra_moves(block_size_y),
64-
inter_lanes=tuple(inter_lanes),
61+
words,
62+
frozenset(range(num_word_x)),
63+
frozenset(as_flat_tuple_int(word_ids[:, 1])),
64+
site_buses(word_size_y),
65+
tuple(word_buses),
66+
site_bus_compatibility,
6567
)

src/bloqade/lanes/types/arch.py

Lines changed: 34 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
from dataclasses import dataclass
2-
from itertools import chain
32
from typing import Generic, Sequence
43

54
import numpy as np
6-
import rustworkx as nx
75

8-
from .block import Block, SiteType
6+
from .word import SiteType, Word
97

108

119
@dataclass(frozen=True)
12-
class Lane:
10+
class Bus:
1311
"""A group of inter-lanes that can be executed in parallel.
1412
15-
For inter-lanes, src and dst are the block indices involved in the inter-lane.
13+
For inter-lanes, src and dst are the word indices involved in the inter-lane.
1614
For intra-lanes, src are the source site indices and dst are the destination site indices.
1715
1816
"""
@@ -23,55 +21,23 @@ class Lane:
2321

2422
@dataclass(frozen=True)
2523
class ArchSpec(Generic[SiteType]):
26-
blocks: tuple[Block[SiteType], ...]
27-
"""List of all blocks in the architecture."""
28-
has_intra_lanes: frozenset[int]
29-
"""Set of blocks that have intra-lane moves."""
30-
has_inter_lanes: frozenset[int] # set of sites in block that have inter-lanes moves
31-
"""Set of sites (by index) that have inter-lane moves. These sites are the same across all blocks."""
32-
intra_lanes: tuple[Lane, ...]
33-
"""List of all intra-lanes in the architecture by site index."""
34-
inter_lanes: tuple[Lane, ...]
35-
"""List of all inter-lanes in the architecture by block index."""
36-
37-
def get_graphs(self):
38-
39-
block_intra_graph = nx.PyDiGraph()
40-
intra_move_compat = nx.PyGraph()
41-
42-
all_moves = list(
43-
chain.from_iterable(
44-
(list(zip(lane.src, lane.dst)) for lane in self.intra_lanes)
45-
)
46-
)
47-
48-
move_map = {move: i for i, move in enumerate(all_moves)}
49-
50-
for lane in self.intra_lanes:
51-
moves = list(zip(lane.src, lane.dst))
52-
for src, dst in moves:
53-
block_intra_graph.add_edge(src, dst, None)
54-
55-
for i, move_i in enumerate(moves):
56-
for j, move_j in enumerate(moves[i + 1 :], i + 1):
57-
intra_move_compat.add_edge(move_map[move_i], move_map[move_j], None)
58-
59-
inter_block_graph = nx.PyDiGraph()
60-
inter_move_compat = nx.PyGraph()
61-
62-
for lane in self.inter_lanes:
63-
moves = list(zip(lane.src, lane.dst))
64-
for src, dst in moves:
65-
inter_block_graph.add_edge(src, dst, None)
66-
67-
for i, move_i in enumerate(moves):
68-
for j, move_j in enumerate(moves[i + 1 :], i + 1):
69-
inter_move_compat.add_edge(i, j, None)
24+
words: tuple[Word[SiteType], ...]
25+
"""tuple of all words in the architecture. words[i] gives the word at word address i."""
26+
has_site_buses: frozenset[int]
27+
"""Set of words that have site-lane moves."""
28+
has_word_buses: frozenset[int] # set of sites in word that have inter-lanes moves
29+
"""Set of sites (by index) that have word-lane moves. These sites are the same across all words."""
30+
site_buses: tuple[Bus, ...]
31+
"""List of all site buses in the architecture by site address."""
32+
word_buses: tuple[Bus, ...]
33+
"""List of all word buses in the architecture by word address."""
34+
site_bus_compatibility: tuple[frozenset[int], ...]
35+
"""Mapping from word id indicating which other word ids can execute site-buses in parallel."""
7036

7137
def plot(
7238
self,
7339
ax=None,
74-
show_blocks: Sequence[int] = (),
40+
show_words: Sequence[int] = (),
7541
show_intra: Sequence[int] = (),
7642
show_inter: Sequence[int] = (),
7743
**scatter_kwargs,
@@ -82,9 +48,9 @@ def plot(
8248
if ax is None:
8349
ax = plt.gca()
8450

85-
for block_id in show_blocks:
86-
block = self.blocks[block_id]
87-
block.plot(ax)
51+
for word_id in show_words:
52+
word = self.words[word_id]
53+
word.plot(ax)
8854

8955
x_min, x_max = ax.get_xlim()
9056
y_min, y_max = ax.get_ylim()
@@ -93,14 +59,14 @@ def plot(
9359
bow_x = (x_max - x_min) * 0.025
9460

9561
colors = {}
96-
for block_id in show_blocks:
97-
block = self.blocks[block_id]
62+
for word_id in show_words:
63+
word = self.words[word_id]
9864
for lane_id in show_intra:
99-
lane = self.intra_lanes[lane_id]
65+
lane = self.site_buses[lane_id]
10066

10167
for start, end in zip(lane.src, lane.dst):
102-
start = block[start]
103-
end = block[end]
68+
start = word[start]
69+
end = word[end]
10470

10571
for (x_start, y_start), (x_end, y_end) in zip(
10672
start.positions(), end.positions()
@@ -127,14 +93,14 @@ def plot(
12793
colors[lane] = ln.get_color()
12894

12995
for lane in show_inter:
130-
lane = self.inter_lanes[lane]
131-
for start_block_id, end_block_id in zip(lane.src, lane.dst):
132-
start_block = self.blocks[start_block_id]
133-
end_block = self.blocks[end_block_id]
134-
135-
for site in self.has_inter_lanes:
136-
start = start_block[site]
137-
end = end_block[site]
96+
lane = self.word_buses[lane]
97+
for start_word_id, end_word_id in zip(lane.src, lane.dst):
98+
start_word = self.words[start_word_id]
99+
end_word = self.words[end_word_id]
100+
101+
for site in self.has_word_buses:
102+
start = start_word[site]
103+
end = end_word[site]
138104
for (x_start, y_start), (x_end, y_end) in zip(
139105
start.positions(), end.positions()
140106
):
@@ -164,7 +130,7 @@ def plot(
164130
def show(
165131
self,
166132
ax=None,
167-
show_blocks: Sequence[int] = (),
133+
show_words: Sequence[int] = (),
168134
show_intra: Sequence[int] = (),
169135
show_inter: Sequence[int] = (),
170136
**scatter_kwargs,
@@ -173,7 +139,7 @@ def show(
173139

174140
self.plot(
175141
ax,
176-
show_blocks=show_blocks,
142+
show_words=show_words,
177143
show_intra=show_intra,
178144
show_inter=show_inter,
179145
**scatter_kwargs,

src/bloqade/lanes/types/encoding.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ def reverse(self):
4141

4242
@dataclass(frozen=True)
4343
class IntraMove(MoveType):
44-
block_id: int
44+
word_id: int
4545
site_id: int
4646
lane_id: int
4747

4848
def src_site(self) -> tuple[int, int]:
49-
return self.block_id, self.site_id
49+
return self.word_id, self.site_id
5050

5151
def get_address(self, encoding: EncodingType) -> int:
5252
if encoding == EncodingType.BIT32:
@@ -60,17 +60,17 @@ def get_address(self, encoding: EncodingType) -> int:
6060
else:
6161
raise ValueError("Unsupported encoding type")
6262

63-
block_id_enc = mask & self.block_id
63+
word_id_enc = mask & self.word_id
6464
lane_id_enc = mask & self.lane_id
6565
site_id_enc = mask & self.site_id
6666

67-
assert block_id_enc == self.block_id, "Block ID too large to encode"
67+
assert word_id_enc == self.word_id, "word ID too large to encode"
6868
assert lane_id_enc == self.lane_id, "Lane ID too large to encode"
6969
assert site_id_enc == self.site_id, "Site ID too large to encode"
7070

7171
address = lane_id_enc
7272
address |= site_id_enc << shift
73-
address |= block_id_enc << (2 * shift)
73+
address |= word_id_enc << (2 * shift)
7474
address |= MoveTypeEnum.INTRA.value << (3 * shift + padding)
7575
address |= self.direction.value << (3 * shift + padding + 1)
7676
assert address.bit_length() <= (
@@ -81,12 +81,12 @@ def get_address(self, encoding: EncodingType) -> int:
8181

8282
@dataclass(frozen=True)
8383
class InterMove(MoveType):
84-
start_block_id: int
85-
end_block_id: int
84+
start_word_id: int
85+
end_word_id: int
8686
lane_id: int
8787

8888
def src_site(self) -> tuple[int, int]:
89-
return self.start_block_id, self.lane_id
89+
return self.start_word_id, self.lane_id
9090

9191
def get_address(self, encoding: EncodingType) -> int:
9292
if encoding == EncodingType.BIT32:
@@ -101,18 +101,16 @@ def get_address(self, encoding: EncodingType) -> int:
101101
raise ValueError("Unsupported encoding type")
102102

103103
lane_id_enc = mask & self.lane_id
104-
start_block_id = mask & self.start_block_id
105-
end_block_id = mask & self.end_block_id
104+
start_word_id = mask & self.start_word_id
105+
end_word_id = mask & self.end_word_id
106106

107107
assert lane_id_enc == self.lane_id, "Lane ID too large to encode"
108-
assert (
109-
start_block_id == self.start_block_id
110-
), "Start Block ID too large to encode"
111-
assert end_block_id == self.end_block_id, "End Block ID too large to encode"
108+
assert start_word_id == self.start_word_id, "Start word ID too large to encode"
109+
assert end_word_id == self.end_word_id, "End word ID too large to encode"
112110

113111
address = lane_id_enc
114-
address |= end_block_id << shift
115-
address |= start_block_id << (2 * shift)
112+
address |= end_word_id << shift
113+
address |= start_word_id << (2 * shift)
116114
address |= MoveTypeEnum.INTER.value << (3 * shift + padding)
117115
address |= self.direction.value << (3 * shift + padding + 1)
118116
assert address.bit_length() <= (

0 commit comments

Comments
 (0)