Skip to content

Commit e3b8dc9

Browse files
fabergaFlavio BergamaschiErnesto Zamora RamosChris WilkersonJose Rojas Chaves
authored andcommitted
Reference implementation of HERACLES Assembler tools (#75)
* Reference implementation of the HERACLES Assembler tools --------- Signed-off-by: Flavio Bergamaschi <[email protected]> Co-authored-by: Flavio Bergamaschi <[email protected]> Co-authored-by: Ernesto Zamora Ramos <[email protected]> Co-authored-by: Chris Wilkerson <[email protected]> Co-authored-by: Jose Rojas Chaves <[email protected]> Co-authored-by: Kylan Race <[email protected]> Co-authored-by: Christopher Gutierrez <[email protected]> Co-authored-by: Hamish Hunt <[email protected]> Co-authored-by: Jack Crawford <[email protected]> Co-authored-by: Minxuan Zhou <[email protected]> Co-authored-by: Wen Wang <[email protected]> Co-authored-by: Adish Vartak <[email protected]> Co-authored-by: Michael Steiner <[email protected]> Co-authored-by: Avinash Alevoor <[email protected]> Co-authored-by: Rashmi Agrawal <[email protected]>
1 parent ec0913b commit e3b8dc9

File tree

176 files changed

+26928
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

176 files changed

+26928
-0
lines changed

.gitignore

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
#========================
2+
# Intermediate and Output files
3+
#========================
4+
5+
*.tmp
6+
*.temp
7+
*.mem
8+
*.minst
9+
*.cinst
10+
*.xinst
11+
*.csv
12+
*.out
13+
#*#
14+
*~
15+
tmp/
16+
17+
18+
# Local files
19+
#========================
20+
21+
*.yml
22+
*.pyc
23+
*.bak
24+
*.pkl
25+
*.lock
26+
*.swp
27+
tfedlrn.egg-info/
28+
bin/out
29+
30+
#========================
31+
# Generated docs
32+
#========================
33+
34+
*.htm
35+
*.html
36+
*.pdf
37+
html
38+
latex
39+
[Dd]ocs/
40+
41+
42+
#========================
43+
# Eclipse & PyDev intermediate files
44+
#========================
45+
46+
.metadata/
47+
RemoteSystemsTempFiles/
48+
.settings
49+
.project
50+
.pydevproject
51+
52+
#========================
53+
# Visual Studio
54+
#========================
55+
56+
# User-specific files
57+
*.rsuser
58+
*.suo
59+
*.user
60+
*.userosscache
61+
*.sln.docstates
62+
63+
# User-specific files (MonoDevelop/Xamarin Studio)
64+
*.userprefs
65+
66+
# Mono auto generated files
67+
mono_crash.*
68+
69+
# Visual Studio cache/options directory
70+
.vs/
71+
.vscode/
72+
73+
#========================
74+
# Python general stuff
75+
#========================
76+
77+
# Byte-compiled / optimized / DLL files
78+
__pycache__/
79+
*.py[cod]
80+
*$py.class
81+
82+
# C extensions
83+
*.so
84+
85+
# Distribution / packaging
86+
.Python
87+
build/
88+
develop-eggs/
89+
dist/
90+
downloads/
91+
eggs/
92+
.eggs/
93+
lib/
94+
lib64/
95+
parts/
96+
sdist/
97+
var/
98+
wheels/
99+
pip-wheel-metadata/
100+
share/python-wheels/
101+
*.egg-info/
102+
.installed.cfg
103+
*.egg
104+
MANIFEST
105+
106+
# PyInstaller
107+
# Usually these files are written by a python script from a template
108+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
109+
*.manifest
110+
*.spec
111+
112+
# Installer logs
113+
pip-log.txt
114+
pip-delete-this-directory.txt
115+
116+
# Unit test / coverage reports
117+
htmlcov/
118+
.tox/
119+
.nox/
120+
.coverage
121+
.coverage.*
122+
.cache
123+
nosetests.xml
124+
coverage.xml
125+
*.cover
126+
*.py,cover
127+
.hypothesis/
128+
.pytest_cache/
129+
130+
# Translations
131+
*.mo
132+
*.pot
133+
134+
# Django stuff:
135+
*.log
136+
local_settings.py
137+
db.sqlite3
138+
db.sqlite3-journal
139+
140+
# Flask stuff:
141+
instance/
142+
.webassets-cache
143+
144+
# Scrapy stuff:
145+
.scrapy
146+
147+
# Sphinx documentation
148+
docs/_build/
149+
150+
# PyBuilder
151+
target/
152+
153+
# Jupyter Notebook
154+
.ipynb_checkpoints
155+
156+
# IPython
157+
profile_default/
158+
ipython_config.py
159+
160+
# pyenv
161+
.python-version
162+
163+
# pipenv
164+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
165+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
166+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
167+
# install all needed dependencies.
168+
#Pipfile.lock
169+
170+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
171+
__pypackages__/
172+
173+
# Celery stuff
174+
celerybeat-schedule
175+
celerybeat.pid
176+
177+
# SageMath parsed files
178+
*.sage.py
179+
180+
# Environments
181+
.env
182+
.venv
183+
env/
184+
venv/
185+
ENV/
186+
env.bak/
187+
venv.bak/
188+
189+
# Spyder project settings
190+
.spyderproject
191+
.spyproject
192+
193+
# Rope project settings
194+
.ropeproject
195+
196+
# mkdocs documentation
197+
/site
198+
199+
# mypy
200+
.mypy_cache/
201+
.dmypy.json
202+
dmypy.json
203+
204+
# Pyre type checker
205+
.pyre/

.pre-commit-config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ repos:
3838
- HEADER
3939
- --comment-style
4040
- // # defaults to: #
41+
- id: remove-tabs
42+
name: remove-tabs
43+
files: \.(py)$
44+
args: [--whitespaces-count, '4']
4145
- repo: https://github.com/psf/black-pre-commit-mirror
4246
rev: 24.4.0 # Updated 2024/04
4347
hooks:

assembler_tools/empty_file.txt

Whitespace-only changes.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# HERACLES Code Generation Framework (Reference Implementation)
2+
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.
4+
5+
## Table of Contents
6+
1. [Dependencies](#dependencies)
7+
2. [Inputs](#as_inputs)
8+
3. [Outputs](#as_outputs)
9+
1. [Assembler Instruction Specs](#asm_specs)
10+
4. [Executing the Assembler](#executing_asm)
11+
1. [Running for a Pre-Generated Kernel](#executing_single)
12+
5. [Debug Tools](./debug_tools/README.md)
13+
14+
## Dependencies <a name="dependencies"></a>
15+
16+
This project is Python based. Dependencies for the project are as follows:
17+
18+
- Python 3.10 or newer (Tested with 3.10)
19+
- Python pip 22.0 or newer
20+
- Requirements for the Python environment listed in file `requirements.txt`.
21+
22+
Install with `pip`:
23+
24+
```bash
25+
pip3 install -r requirements.txt
26+
```
27+
28+
It is recommended to install Python dependencies and run inside a [Python virtual environment](https://virtualenv.pypa.io/en/stable/index.html) to avoid polluting the global Python.
29+
30+
## Inputs <a name="as_inputs"></a>
31+
32+
The assembler framework requires two inputs:
33+
34+
- **Abstract P-ISA kernel**: a kernel of instructions in abstract P-ISA where the memory model is abstracted with a flat, infinite structure.
35+
36+
- **Memory mapping metadata**: metadata information indicating the location where variable identifiers from the kernel are stored in the HERACLES HBM.
37+
38+
Kernels and metadata are structured in comma-separated value (csv) files.
39+
40+
P-ISA kernels, along with corresponding memory metadata required as input to the assembler, are generated by the upper layers in the Encrypted Computing SDK stack, e.g. the [Program Mapper](../../README.md#encrypted-computing-sdk-phase-1-components-and-tasks) component of the [p-ISA tools](../../p-isa_tools).
41+
42+
## Outputs <a name="as_outputs"></a>
43+
44+
On a successful run, given a P-ISA kernel in file `filename.csv` (and corresponding memory metadata file), the assmebler generates three files:
45+
46+
- `filename.minst`: contains the list of instructions for the MINST queue.
47+
- `filename.cinst`: contains the list of instructions for the CINST queue.
48+
- `filename.xinst`: contains the list of instructions for the XINST queue.
49+
50+
### Assembler Output Instruction Specs <a name="asm_specs"></a>
51+
52+
The format for the output files and instruction set can be found at [HCGF Instruction Specification](docsrc/specs.md).
53+
54+
## Executing the Assembler <a name="executing_asm"></a>
55+
### Running for a Pre-Generated Kernel <a name="executing_single"></a>
56+
57+
Given a P-ISA kernel (`filename.csv`) and corresponding memory mapping file (`filename.mem`), there are three steps to assemble them into HERACLES code.
58+
59+
1. Pre-process the P-ISA input kernel using `he_prep.py`.
60+
61+
```bash
62+
# pre-process kernel: outputs filename.tw.csv
63+
python3 he_prep.py filename.csv
64+
```
65+
66+
2. Assemble the pre-processed result using `he_as.py`.
67+
68+
```bash
69+
# use output from pre-processing as input to asm:
70+
# outputs filename.tw.minst, filename.tw.cinst, filename.tw.xinst
71+
python3 he_as.py filename.tw.csv --input_mem_file filename.mem
72+
```
73+
74+
3. Link the assembler output into a HERACLES program using `he_link.py`.
75+
76+
```bash
77+
# link assembled output (input prefix: filename.tw)
78+
# outputs filename.minst, filename.cinst, filename.xinst
79+
python3 he_link.py filename.tw --input_mem_file filename.mem --output_prefix filename
80+
```
81+
82+
This will generate the main three output files in the same directory as the input file:
83+
84+
- `filename.minst`: contains the list of instructions for the MINST queue.
85+
- `filename.cinst`: contains the list of instructions for the CINST queue.
86+
- `filename.xinst`: contains the list of instructions for the XINST queue.
87+
88+
Intermediate files, if any, are kept as well.
89+
90+
The linker program is able to link several assembled kernels into a single HERACLES program, given a correct memory mapping for the resulting program.
91+
92+
This version of executing is intended for the assembler to be usable as part of a compilation pipeline.
93+
94+
Use commands below for more configuration information, including changing output directories, input and output filenames, etc.
95+
96+
```bash
97+
python3 he_prep.py -h
98+
python3 he_as.py -h
99+
python3 he_link.py -h
100+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import os
2+
3+
def makeUniquePath(path: str) -> str:
4+
"""
5+
Returns a unique, normalized, and absolute version of the given file path.
6+
7+
Args:
8+
path (str): The file path to be processed.
9+
10+
Returns:
11+
str: A unique, normalized, and absolute version of the input path.
12+
"""
13+
return os.path.normcase(os.path.realpath(os.path.expanduser(path)))
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+

2+
class GlobalConfig:
3+
"""
4+
A configuration class for controlling various aspects of the assembler's behavior.
5+
6+
Attributes:
7+
suppressComments (bool):
8+
If True, no comments will be emitted in the output generated by the assembler.
9+
10+
useHBMPlaceHolders (bool):
11+
Specifies whether to use placeholders (names) for variable locations in HBM
12+
or the actual variable locations.
13+
14+
useXInstFetch (bool):
15+
Specifies whether `xinstfetch` instructions should be added into CInstQ or not.
16+
When no `xinstfetch` instructions are added, it is assumed that the HERACLES
17+
automated mechanism for `xinstfetch` will be activated.
18+
19+
debugVerbose (int):
20+
If greater than 0, verbose prints will occur. Its value indicates how often to
21+
print within loops (every `debugVerbose` iterations). This is used for internal
22+
debugging purposes.
23+
hashHBM (bool): Specifies whether the target architecture has HBM or not.
24+
"""
25+
26+
suppressComments = False
27+
useHBMPlaceHolders = True
28+
useXInstFetch = True
29+
debugVerbose: int = 0
30+
hasHBM = True

0 commit comments

Comments
 (0)