Skip to content

Commit

Permalink
Improve compatibility with CP-SAT via configurable model export
Browse files Browse the repository at this point in the history
  • Loading branch information
hanno-becker committed Mar 31, 2024
1 parent 71ad977 commit eaee634
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
11 changes: 9 additions & 2 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def __init__(self, infile, name=None, funcname=None, suffix="opt",
def core(self, slothy):
slothy.optimize()

def run(self, debug=False, dry_run=False, silent=False, timeout=0):
def run(self, debug=False, log_model=False, dry_run=False, silent=False, timeout=0):

if dry_run is True:
annotation = " (dry run only)"
Expand Down Expand Up @@ -140,6 +140,9 @@ def run(self, debug=False, dry_run=False, silent=False, timeout=0):
slothy.config.constraints.allow_renaming = False
slothy.config.variable_size = True

if log_model is True:
slothy.config.log_model = f"{self.name}_model.txt"

# On Apple M1, we must not use x18
if "m1" in target_label_dict[self.target]:
self.target_reserved = ["x18"]
Expand Down Expand Up @@ -1066,6 +1069,8 @@ def __init__(self, var="", arch=AArch64_Neon, target=Target_CortexA55):
def core(self, slothy):
slothy.config.sw_pipelining.enabled = True
slothy.config.inputs_are_outputs = True
slothy.config.constraints.stalls_first_attempt = 160
slothy.config.constraints.stalls_minimum_attempt = 160
slothy.config.sw_pipelining.minimize_overlapping = False
slothy.config.sw_pipelining.optimize_preamble = False
slothy.config.sw_pipelining.optimize_postamble = False
Expand Down Expand Up @@ -1329,6 +1334,7 @@ def main():
ntt_dilithium_123_456_78(False, target=Target_CortexM85r1),
ntt_dilithium_123_456_78(True, target=Target_CortexM85r1),
# Cortex-A55
ntt_dilithium_45678(),
ntt_dilithium_123_45678(),
ntt_dilithium_123_45678(var="w_scalar"),
ntt_dilithium_123_45678(var="manual_st4"),
Expand Down Expand Up @@ -1377,6 +1383,7 @@ def main():
parser.add_argument("--silent", default=False, action="store_true")
parser.add_argument("--iterations", type=int, default=1)
parser.add_argument("--timeout", type=int, default=0)
parser.add_argument("--log-model", default=False, action="store_true")

args = parser.parse_args()
if args.examples != "all":
Expand All @@ -1398,7 +1405,7 @@ def run_example(name, **kwargs):
for e in todo:
for _ in range(iterations):
run_example(e, debug=args.debug, dry_run=args.dry_run,
silent=args.silent, timeout=args.timeout)
silent=args.silent, log_model=args.log_model, timeout=args.timeout)

if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions slothy/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,8 @@ def __init__(self, Arch, Target):

self.solver_random_seed = 42

# TODO: Document log_dir and log_model
self.log_model = None
self.log_dir = "logs/"
if not os.path.exists(self.log_dir):
os.makedirs(self.log_dir)
Expand Down
11 changes: 6 additions & 5 deletions slothy/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,7 @@ def optimize(self, source, prefix_len=0, suffix_len=0, log_model=None, retry=Fal
# - Objective
self._add_objective()
# - Export (optional)
self._export_model(log_model)
self._export_model()

self._result = Result(self.config)

Expand Down Expand Up @@ -2926,11 +2926,12 @@ def _AddHint(self,var,val): # pylint:disable=invalid-name
def _AddNoOverlap(self,interval_list): # pylint:disable=invalid-name
return self._model.cp_model.AddNoOverlap(interval_list)

def _export_model(self, log_model):
if log_model is None:
def _export_model(self):
if self.config.log_model is None:
return
self.logger.info("Writing model to %s...", log_model)
assert self._model.cp_model.ExportToFile(self.config.log_dir + "/" + log_model)
log_file = self.config.log_dir + "/" + self.config.log_model
self.logger.info("Writing model to %s ...", log_file)
assert self._model.cp_model.ExportToFile(log_file)

def _solve(self):

Expand Down

0 comments on commit eaee634

Please sign in to comment.