|
| 1 | +import yaml |
| 2 | +import argparse |
| 3 | +import sys |
| 4 | + |
| 5 | +# Create a custom representer for block scalars |
| 6 | +def block_scalar_representer(dumper, value): |
| 7 | + # Remove any trailing newline characters to ensure YAML uses "|" |
| 8 | + value = value.rstrip('\n').rstrip() # Also remove trailing spaces |
| 9 | + return dumper.represent_scalar('tag:yaml.org,2002:str', value, style='|') |
| 10 | + |
| 11 | + |
| 12 | +def generate_yaml(input_file, model_name_version, output_file): |
| 13 | + try: |
| 14 | + |
| 15 | + with open(input_file, "r") as f: |
| 16 | + code_content = f.read() |
| 17 | + # Add the representer to handle multi-line strings as block scalars |
| 18 | + yaml.add_representer(str, block_scalar_representer) |
| 19 | + |
| 20 | + |
| 21 | + yaml_content = { |
| 22 | + "replicate_code": { |
| 23 | + "llm": {"model": model_name_version}, |
| 24 | + "replicate_code_prompt1": [ |
| 25 | + { |
| 26 | + "role": "system", |
| 27 | + "content": "You are a super smart Verilog and timing expert." |
| 28 | + "You have been tasked with improving the frequency of a verilog code." |
| 29 | + "You provide a higher frequency code which passes LEC." |
| 30 | + "If you cannot improve frequency any further, return the text \"no change possible\"." |
| 31 | + "However, make sure that you only return the code that passes LEC." |
| 32 | + "Take care that:" |
| 33 | + "The semantics are preserved exactly as in the original netlist (including word instantiation and sign‐extension)" |
| 34 | + "while breaking a long combinational critical path." |
| 35 | + "The resultant code is functionally equivalent to the original and passes LEC." |
| 36 | + }, |
| 37 | + { |
| 38 | + "role": "user", |
| 39 | + "content": f"This is the current Verilog:\n```\n{code_content}\n```\n" |
| 40 | + "The above code has comments with the word CRITICAL providing hints on the where the critical path resides. These are likely statements or related statements that need to be optimized." |
| 41 | + "Please do not change semantics, just split the always blocks in separate always blocks " |
| 42 | + "and try to improve the performance when possible." |
| 43 | + } |
| 44 | + ] |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if "o3" in model_name_version: |
| 49 | + yaml_content["replicate_code"]["threshold"] = 40 |
| 50 | + elif "o4" in model_name_version: |
| 51 | + yaml_content["replicate_code"]["temperature"] = 40 |
| 52 | + |
| 53 | + with open(output_file, "w") as f: |
| 54 | + yaml.dump(yaml_content, f, default_flow_style=False, sort_keys=False, allow_unicode=True, default_style=None, indent=2) |
| 55 | + |
| 56 | + print(f"YAML file '{output_file}' generated successfully.") |
| 57 | + |
| 58 | + except FileNotFoundError as e: |
| 59 | + print(f"Error: The input file '{input_file}' was not found.") |
| 60 | + sys.exit(1) |
| 61 | + except IOError as e: |
| 62 | + print(f"Error: There was an issue with reading or writing files: {e}") |
| 63 | + sys.exit(1) |
| 64 | + except TypeError as e: |
| 65 | + print(f"Error: Type error encountered: {e}") |
| 66 | + sys.exit(1) |
| 67 | + except Exception as e: |
| 68 | + print(f"An unexpected error occurred: {e}") |
| 69 | + sys.exit(1) |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + try: |
| 73 | + parser = argparse.ArgumentParser(description="Generate a YAML file from an input file and model name version.") |
| 74 | + parser.add_argument("input_file", help="Path to the input file containing code.") |
| 75 | + parser.add_argument("-m", "--model_name_version", required=True, help="Model name as string.") |
| 76 | + parser.add_argument("-o", "--output_file", required=True, help="Name of the output YAML file (as module name preferred).") |
| 77 | + |
| 78 | + args = parser.parse_args() |
| 79 | + generate_yaml(args.input_file, args.model_name_version, args.output_file) |
| 80 | + except TypeError as e: |
| 81 | + print(f"Error: Invalid argument type passed to argparse: {e}") |
| 82 | + sys.exit(1) |
| 83 | + except Exception as e: |
| 84 | + print(f"An unexpected error occurred while parsing arguments: {e}") |
| 85 | + sys.exit(1) |
| 86 | + |
0 commit comments