|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +from jinja2 import Environment, PackageLoader, select_autoescape |
| 4 | +from numpy import concatenate, hstack, newaxis |
| 5 | + |
| 6 | +from cvxpygen.hwgen.firmware_models import ( |
| 7 | + ExecutionEnvironment, |
| 8 | +) |
| 9 | +from cvxpygen.hwgen.hw_models import PDAQPHWConfig |
| 10 | +from cvxpygen.hwgen.models import PDAQPAlgoConfig |
| 11 | + |
| 12 | +env = Environment( |
| 13 | + loader=PackageLoader("cvxpygen.hwgen"), autoescape=select_autoescape() |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +def generateHWAccCode(config: PDAQPHWConfig, target_dir: Path) -> Path: |
| 18 | + """Given the high-level hardware accelerator design for |
| 19 | + PDA-QP solver, generate the design in the Chisel language.""" |
| 20 | + |
| 21 | + template = env.get_template("hwacc/Constants.scala.j2") |
| 22 | + rendered = template.render(config=config) |
| 23 | + |
| 24 | + outfile_path = target_dir / "Constants.scala" |
| 25 | + with open(outfile_path, "w") as f: |
| 26 | + f.write(rendered) |
| 27 | + |
| 28 | + return outfile_path |
| 29 | + |
| 30 | + |
| 31 | +def generateTestbenchCode( |
| 32 | + config: PDAQPAlgoConfig, |
| 33 | + fixed_point_precision: int, |
| 34 | + execution_environment: ExecutionEnvironment, |
| 35 | + target_dir: Path, |
| 36 | +) -> tuple[Path, Path]: |
| 37 | + """Given the high-level algorithm design for |
| 38 | + PDA-QP solver, generate the design in the Chisel language.""" |
| 39 | + |
| 40 | + constants_hpp_path = target_dir / "constants.hpp" |
| 41 | + with open(constants_hpp_path, "w") as f: |
| 42 | + rendered = env.get_template("testbench/constants.hpp.j2").render( |
| 43 | + problem_size=config.problem_size, |
| 44 | + Q=fixed_point_precision, |
| 45 | + ) |
| 46 | + f.write(rendered) |
| 47 | + |
| 48 | + problem_def_hpp_path = target_dir / "problem-def.hpp" |
| 49 | + with open(problem_def_hpp_path, "w") as f: |
| 50 | + # TODO (antonysigma): The shape of the halfplanes normal and |
| 51 | + # offset are erased. Modify the C++ project to preserve the shape. |
| 52 | + |
| 53 | + assert config.feedbacks.scale.shape[0] == config.feedbacks.offset.shape[0] |
| 54 | + assert config.feedbacks.scale.shape[1] == config.feedbacks.offset.shape[1] |
| 55 | + assert config.feedbacks.scale.shape[2] == config.problem_size.n_parameter |
| 56 | + |
| 57 | + rendered = env.get_template("testbench/problem-def.hpp.j2").render( |
| 58 | + half_planes=hstack( |
| 59 | + (config.half_planes.scale, config.half_planes.offset[:, newaxis]) |
| 60 | + ), |
| 61 | + feedbacks=concatenate( |
| 62 | + (config.feedbacks.scale, config.feedbacks.offset[..., newaxis]), |
| 63 | + axis=-1, |
| 64 | + ), |
| 65 | + tree_nodes=config.tree_nodes, |
| 66 | + ) |
| 67 | + f.write(rendered) |
| 68 | + |
| 69 | + return constants_hpp_path, problem_def_hpp_path |
0 commit comments