Skip to content

Commit bb32e05

Browse files
Added readme section for kergraph
1 parent 8dfbe7f commit bb32e05

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed

p-isa_tools/kerngen/README.md

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,174 @@ following
248248
```bash
249249
pytest <test-directory>
250250
```
251+
252+
253+
<!-- These contents may have been developed with support from one or more Intel-operated generative artificial intelligence solutions. -->
254+
255+
# kerngraph.py
256+
257+
A command-line tool for parsing and optimizing kernel strings from the kerngen.py output, with support for loop interchange and reordering optimizations.
258+
259+
## Overview
260+
261+
`kerngraph.py` processes kernel strings (typically generated by `kerngen.py`) and applies various optimization transformations, including loop interchange based on primary and secondary loop keys. It can work standalone or as part of a pipeline with `kerngen.py`.
262+
263+
## Usage
264+
265+
### Basic Pipeline
266+
267+
The typical usage involves piping output from `kerngen.py` into `kerngraph.py`:
268+
269+
```bash
270+
cat input.high | ./kerngen.py | ./kerngraph.py [OPTIONS]
271+
```
272+
where `input.high` contains input kernel lanagauge.
273+
274+
### Standalone Usage
275+
276+
You can also provide kernel strings directly via stdin:
277+
278+
```bash
279+
./kerngraph.py [OPTIONS] < kernel_strings.txt
280+
```
281+
where `kernel_strings.txt` contains input kernel language.
282+
283+
## Command-Line Parameters
284+
285+
### General Options
286+
287+
- `-d, --debug`
288+
- Enable debug output for parsing errors and optimization information
289+
- Shows detailed information about loop ordering decisions
290+
291+
- `-l, --legacy`
292+
- Enable legacy mode for compatibility with older kernel formats
293+
294+
### Loop Reordering Options
295+
296+
- `-t, --target [TARGETS...]`
297+
- Specify which kernel operations to apply reordering optimizations
298+
- Choices: `add`, `sub`, `mul`, `muli`, `copy`, `ntt`, `intt`, `mod`, `relin`, `rotate`, `rescale`
299+
- Multiple targets can be specified (space-separated)
300+
- Example: `-t relin rotate mod`
301+
302+
- `-p, --primary {RNS,PART}`
303+
- Set the primary key for loop interchange
304+
- Default: `PART`
305+
- Options: `RNS` (RNS-based partitioning), `PART` (partition-based)
306+
307+
- `-s, --secondary {RNS,PART,None}`
308+
- Set the secondary key for loop interchange
309+
- Default: `None`
310+
- Options: `RNS`, `PART`, or `None`
311+
- Note: Primary and secondary keys cannot be the same
312+
313+
- `--optimal`
314+
- Automatically determine optimal primary and secondary loop order based on kernel configuration
315+
- Overrides `-p` and `-s` flags
316+
- Uses lookup tables based on scheme, kernel type, polynomial order, and RNS count
317+
318+
## Input Format
319+
320+
`kerngraph.py` expects kernel strings in the format produced by `kerngen.py`. Each line should be a valid kernel representation that can be parsed by the `KernelParser` class.
321+
322+
## Output Format
323+
324+
The tool outputs optimized kernel operations in p-isa format. When reordering is applied, loops are interchanged according to the specified primary and secondary keys.
325+
326+
## Examples
327+
328+
### Example 1: Basic Reordering
329+
330+
Apply reordering to relin kernels with RNS as primary and PART as secondary:
331+
332+
```bash
333+
cat bgv.relin.high | ./kerngen.py -l | ./kerngraph.py -l -t relin -p rns -s part
334+
```
335+
336+
### Example 2: Multiple Targets
337+
338+
Optimize multiple operation types:
339+
340+
```bash
341+
cat input.high | ./kerngen.py | ./kerngraph.py -t relin rotate mod -p part -s rns
342+
```
343+
344+
### Example 3: Optimal Loop Order
345+
346+
Use automatic optimal loop ordering:
347+
348+
```bash
349+
cat input.high | ./kerngen.py -l | ./kerngraph.py -l -t relin --optimal
350+
```
351+
352+
### Example 4: Debug Mode
353+
354+
Enable debug output to see optimization decisions:
355+
356+
```bash
357+
cat input.high | ./kerngen.py | ./kerngraph.py -d -t relin --optimal
358+
```
359+
360+
## Features
361+
362+
### Loop Interchange Optimization
363+
364+
The tool performs loop interchange based on the specified primary and secondary keys:
365+
- Splits kernels into reorderable and non-reorderable groups
366+
- Applies loop interchange only to reorderable sections
367+
- Preserves non-reorderable operations in their original order
368+
- Use `<reorderable>` and `</reorderable>` Comments within `kerngen` to indicate portions that are reorderable.
369+
370+
### RNS Label Reuse
371+
372+
When targeting `mod` operations with both primary and secondary keys specified, the tool applies RNS label reuse optimization to improve performance.
373+
374+
### Optimal Loop Order Lookup
375+
376+
With the `--optimal` flag, the tool automatically determines the best loop ordering based on:
377+
- Encryption scheme (e.g., BGV, CKKS)
378+
- Kernel operation type
379+
- Polynomial order
380+
- Maximum RNS count
381+
382+
For example, a configuration file `kerngen/kernel_optimization/loop_ordering_lookup.json:
383+
```
384+
{
385+
"bgv": {
386+
"mod": {
387+
"16384": {
388+
"2-4": ["part", "null"],
389+
"5-16": ["rns", "part"]
390+
}
391+
}
392+
}
393+
}
394+
```
395+
will optimize bgv mod swich with poly order 16384 with priormary loop ordering when RNS is between 2-4 and primary RNS and secondary loop as part when RNS is between 5-16. The configuration file allows a developer to manually select the optimial loop reordering given encryption scheme, kernel, etc.
396+
397+
398+
## Error Handling
399+
400+
- Invalid kernel strings are skipped with optional debug output
401+
- Validation ensures primary and secondary keys are different
402+
- Warns if optimal loop order cannot be determined (debug mode)
403+
404+
## Integration with kerngen.py
405+
406+
`kerngraph.py` is designed to work seamlessly with `kerngen.py`:
407+
408+
1. `kerngen.py` reads high-level operations and generates kernel strings
409+
2. `kerngraph.py` parses these kernel strings and applies optimizations
410+
3. The output is optimized p-isa operations ready for execution
411+
412+
## Notes
413+
414+
- The `-l, --legacy` flag should be used consistently across both `kerngen.py` and `kerngraph.py`
415+
- Reordering optimizations are only applied to kernels matching the specified targets
416+
- Non-targeted kernels are passed through with their p-isa operations printed directly
417+
418+
## See Also
419+
420+
- `kerngen.py` - Kernel generator that produces input for kerngraph.py
421+
- `KernelParser` - Parser class for kernel string format

0 commit comments

Comments
 (0)