You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: p-isa_tools/kerngen/README.md
+171Lines changed: 171 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -248,3 +248,174 @@ following
248
248
```bash
249
249
pytest <test-directory>
250
250
```
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`:
- 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:
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