Skip to content

Commit 19db489

Browse files
authored
Merge branch 'main' into joserochh/assembler_testing_2
2 parents 0775441 + 2b4e64c commit 19db489

File tree

15 files changed

+1048
-73
lines changed

15 files changed

+1048
-73
lines changed

assembler_tools/hec-assembler-tools/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# HERACLES Code Generation Framework (Reference Implementation)
22

3-
The tools in this directory are the reference implementation of and Assembler codegenerator that takes a pre-generated Polynomial Instruction Set Architecture (P-ISA) program containing instructions that use an abstract, flat memory model for polynomial operations, such as those applied in homomorphic encryption (HE), and maps them to a corresponding set of instructions compatible with the HERACLES architecture, accounting for hardware restrictions, including memory management for the HERACLES memory model.
3+
The tools in this directory are the reference implementation of an Assembler codegenerator that takes a pre-generated Polynomial Instruction Set Architecture (P-ISA) program containing instructions that use an abstract, flat memory model for polynomial operations, such as those applied in homomorphic encryption (HE), and maps them to a corresponding set of instructions compatible with the HERACLES architecture, accounting for hardware restrictions, including memory management for the HERACLES memory model.
44

55
## Table of Contents
66
1. [Dependencies](#dependencies)
@@ -65,8 +65,8 @@ Given a P-ISA kernel (`filename.csv`) and corresponding memory mapping file (`fi
6565
python3 he_prep.py filename.csv
6666
```
6767

68-
The preprocessing program can split kernels (see he_prep.py -h for details). Use the split options if you encounter physical memory constraints.
69-
68+
* The preprocessing program can split kernels (see he_prep.py -h for details). Use the split options if you encounter physical memory constraints.
69+
```bash
7070
# pre-process kernel: outputs filename1.tw_<#>.csv & filename1.tw_deps_<#>.csv
7171
python3 he_prep.py filename1.csv --mem_file filename0.mem --split_vars_limit <val> --split_inst_limit <val>
7272
```

assembler_tools/hec-assembler-tools/assembler/common/constants.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class Constants:
2828

2929
__MAX_BUNDLE_SIZE: int
3030
__XINSTRUCTION_SIZE_BYTES: int
31+
__CINSTRUCTION_SIZE_BYTES: int
32+
__MINSTRUCTION_SIZE_BYTES: int
3133

3234
# Data Constants
3335
# --------------
@@ -78,6 +80,16 @@ def XINSTRUCTION_SIZE_BYTES(cls) -> int:
7880
"""Size of an x-instruction in bytes."""
7981
return cls.__XINSTRUCTION_SIZE_BYTES
8082

83+
@classproperty
84+
def CINSTRUCTION_SIZE_BYTES(cls) -> int:
85+
"""Size of a c-instruction in bytes."""
86+
return cls.__CINSTRUCTION_SIZE_BYTES
87+
88+
@classproperty
89+
def MINSTRUCTION_SIZE_BYTES(cls) -> int:
90+
"""Size of a m-instruction in bytes."""
91+
return cls.__MINSTRUCTION_SIZE_BYTES
92+
8193
@classproperty
8294
def MAX_BUNDLE_SIZE(cls) -> int:
8395
"""Maximum number of instructions in a bundle."""
@@ -124,7 +136,12 @@ def hw_spec_as_dict(cls) -> dict:
124136
"""
125137
Returns hw configurable attributes as dictionary.
126138
"""
127-
return {"bytes_per_xinstruction": cls.XINSTRUCTION_SIZE_BYTES, "max_instructions_per_bundle": cls.MAX_BUNDLE_SIZE}
139+
return {
140+
"bytes_per_xinstruction": cls.XINSTRUCTION_SIZE_BYTES,
141+
"bytes_per_cinstruction": cls.CINSTRUCTION_SIZE_BYTES,
142+
"bytes_per_minstruction": cls.MINSTRUCTION_SIZE_BYTES,
143+
"max_instructions_per_bundle": cls.MAX_BUNDLE_SIZE,
144+
}
128145

129146
@classmethod
130147
def setMaxBundleSize(cls, val: int):
@@ -136,6 +153,16 @@ def setXInstructionSizeBytes(cls, val: int):
136153
"""Updates size of single XInstruction"""
137154
cls.__XINSTRUCTION_SIZE_BYTES = val
138155

156+
@classmethod
157+
def setCInstructionSizeBytes(cls, val: int):
158+
"""Updates size of single CInstruction"""
159+
cls.__CINSTRUCTION_SIZE_BYTES = val
160+
161+
@classmethod
162+
def setMInstructionSizeBytes(cls, val: int):
163+
"""Updates size of single MInstruction"""
164+
cls.__MINSTRUCTION_SIZE_BYTES = val
165+
139166

140167
def convertBytes2Words(bytes_in: int) -> int:
141168
"""
@@ -363,6 +390,11 @@ def XINST_QUEUE_MAX_CAPACITY_WORDS(cls):
363390
"""Maximum capacity of the XINST queue in words."""
364391
return convertBytes2Words(cls.__XINST_QUEUE_MAX_CAPACITY)
365392

393+
@classproperty
394+
def XINST_QUEUE_MAX_CAPACITY_ENTRIES(cls):
395+
"""Maximum number of entries in the XINST queue."""
396+
return cls.__XINST_QUEUE_MAX_CAPACITY // Constants.XINSTRUCTION_SIZE_BYTES
397+
366398
@classproperty
367399
def CINST_QUEUE_MAX_CAPACITY(cls):
368400
"""Maximum capacity of the CINST queue in bytes."""
@@ -373,6 +405,11 @@ def CINST_QUEUE_MAX_CAPACITY_WORDS(cls):
373405
"""Maximum capacity of the CINST queue in words."""
374406
return convertBytes2Words(cls.__CINST_QUEUE_MAX_CAPACITY)
375407

408+
@classproperty
409+
def CINST_QUEUE_MAX_CAPACITY_ENTRIES(cls):
410+
"""Maximum number of entries in the CINST queue."""
411+
return cls.__CINST_QUEUE_MAX_CAPACITY // Constants.CINSTRUCTION_SIZE_BYTES
412+
376413
@classproperty
377414
def MINST_QUEUE_MAX_CAPACITY(cls):
378415
"""Maximum capacity of the MINST queue in bytes."""
@@ -383,6 +420,11 @@ def MINST_QUEUE_MAX_CAPACITY_WORDS(cls):
383420
"""Maximum capacity of the MINST queue in words."""
384421
return convertBytes2Words(cls.__MINST_QUEUE_MAX_CAPACITY)
385422

423+
@classproperty
424+
def MINST_QUEUE_MAX_CAPACITY_ENTRIES(cls):
425+
"""Maximum number of entries in the MINST queue."""
426+
return cls.__MINST_QUEUE_MAX_CAPACITY // Constants.MINSTRUCTION_SIZE_BYTES
427+
386428
@classproperty
387429
def STORE_BUFFER_MAX_CAPACITY(cls):
388430
"""Maximum capacity of the store buffer in bytes."""

assembler_tools/hec-assembler-tools/assembler/spec_config/mem_spec.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
class MemSpecConfig:
1212
_target_attributes = {
1313
"bytes_per_xinstruction": Constants.setXInstructionSizeBytes,
14+
"bytes_per_cinstruction": Constants.setCInstructionSizeBytes,
15+
"bytes_per_minstruction": Constants.setMInstructionSizeBytes,
1416
"max_instructions_per_bundle": Constants.setMaxBundleSize,
1517
"max_xinst_queue_size_in_bytes": MemoryModel.setMaxXInstQueueCapacity,
1618
"max_cinst_queue_size_in_bytes": MemoryModel.setMaxCInstQueueCapacity,

assembler_tools/hec-assembler-tools/assembler/stages/scheduler.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import networkx as nx
77
from typing import NamedTuple
88

9+
from assembler.common import constants
10+
911
from . import buildVarAccessListFromTopoSort
1012
from assembler.common.cycle_tracking import PrioritizedPlaceholder, CycleType
1113
from assembler.instructions import xinst, cinst, minst
@@ -150,6 +152,11 @@ def enforceKeygenOrdering(deps_graph: nx.DiGraph, mem_model: MemoryModel, verbos
150152
comment=f"injected copy to generate keygen var {kg_var_name} (seed = {seed_idx}, key = {key_idx})",
151153
)
152154
deps_graph.add_node(copy_instr.id, instruction=copy_instr)
155+
156+
assert (
157+
deps_graph.number_of_nodes() <= constants.MemoryModel.XINST_QUEUE_MAX_CAPACITY_ENTRIES
158+
), f"Exceeded maximum number of instructions in XInstQ while injecting keygen copy instructions: {constants.MemoryModel.XINST_QUEUE_MAX_CAPACITY_ENTRIES} Entries."
159+
153160
# Enforce ordering of copies based on ordering of keygen
154161
if last_copy_id is not None:
155162
# Last copy -> current copy

assembler_tools/hec-assembler-tools/config/mem_spec.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"num_register_banks": 4,
1010
"num_registers_per_bank": 72,
1111
"bytes_per_xinstruction": 8,
12+
"bytes_per_cinstruction": 8,
13+
"bytes_per_minstruction": 8,
1214
"max_instructions_per_bundle": 64,
1315
"num_blocks_per_twid_meta_word": 4,
1416
"num_blocks_per_kgseed_meta_word": 4,

assembler_tools/hec-assembler-tools/he_as.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ def asmisaAssemble(
235235

236236
insts_listing += parsed_insts
237237

238+
assert (
239+
len(insts_listing) <= constants.MemoryModel.XINST_QUEUE_MAX_CAPACITY_ENTRIES
240+
), f"Line {line_no}: Exceeded maximum number of instructions in XInstQ: {constants.MemoryModel.XINST_QUEUE_MAX_CAPACITY_ENTRIES} Entries."
241+
238242
if b_verbose:
239243
print("Interpreting variable meta information...")
240244
with open(mem_filename, "r") as mem_ifnum:
@@ -259,6 +263,7 @@ def asmisaAssemble(
259263
b_verbose,
260264
)
261265
sched_end = time.time() - start_time
266+
262267
num_nops = 0
263268
num_xinsts = 0
264269
for bundle_xinsts, *_ in xinsts:
@@ -269,6 +274,15 @@ def asmisaAssemble(
269274
if isinstance(xinstr, xinst.Nop):
270275
num_nops += 1
271276

277+
# Final xinst count assertion
278+
assert (
279+
(num_xinsts + num_nops) <= constants.MemoryModel.XINST_QUEUE_MAX_CAPACITY_ENTRIES
280+
), f"Exceeded maximum number of instructions in XInstQ: {(num_xinsts + num_nops)} > {constants.MemoryModel.XINST_QUEUE_MAX_CAPACITY_ENTRIES} Entries."
281+
282+
# Minst count assertion
283+
assert (
284+
len(minsts) <= constants.MemoryModel.MINST_QUEUE_MAX_CAPACITY_ENTRIES
285+
), f"Exceeded maximum number of instructions in MInstQ: {len(minsts)} > {constants.MemoryModel.MINST_QUEUE_MAX_CAPACITY_ENTRIES} Entries."
272286
if b_verbose:
273287
print("Saving minst...")
274288
with open(output_minst_filename, "w") as outnum:
@@ -277,6 +291,10 @@ def asmisaAssemble(
277291
if inst_line:
278292
print(f"{idx}, {inst_line}", file=outnum)
279293

294+
# Cinst count assertion
295+
assert (
296+
len(cinsts) <= constants.MemoryModel.CINST_QUEUE_MAX_CAPACITY_ENTRIES
297+
), f"Exceeded maximum number of instructions in CInstQ: {len(cinsts)} > {constants.MemoryModel.CINST_QUEUE_MAX_CAPACITY_ENTRIES} Entries."
280298
if b_verbose:
281299
print("Saving cinst...")
282300
with open(output_cinst_filename, "w") as outnum:

0 commit comments

Comments
 (0)