File size: 4,924 Bytes
98e7c3b
 
 
 
 
 
 
d2205ab
98e7c3b
 
 
 
77fd902
 
 
 
d2205ab
57146a1
77fd902
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab0a529
77fd902
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82046e5
 
 
 
 
 
 
 
 
 
 
 
77fd902
82046e5
77fd902
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72af62e
77fd902
 
 
 
 
 
 
 
98e7c3b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
---
license: apache-2.0
pipeline_tag: text-generation
tags:
- chat
- llm
- safetensors
base_model: Qwen/Qwen2.5-14B-Instruct
language:
- en
---

# ChatHLS-HLSFixer

## Model Details

- **Finetuned from model:** `Qwen/Qwen2.5-14B-Instruct`
- **Model source paper:** [ChatHLS: Towards Systematic Design Automation and Optimization for High-Level Synthesis](https://aclanthology.org/2026.acl-long.962/)
- **Project repository:** [GEAR-SEU/ChatHLS-ACL-26](https://github.com/GEAR-SEU/ChatHLS-ACL-26)

## Description

**ChatHLS-HLSFixer** is a specialized Large Language Model fine-tuned for High-Level Synthesis (HLS) C/C++ error correction. It acts as the core analysis agent within the ChatHLS framework. 

## Quickstart

### Prerequisites
```bash
pip install transformers torch accelerate
```

### Example

Here is a simple example of how to format your prompt and generate debugging instructions for buggy HLS code:

````python
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

model_name = "XXXiong/ChatHLS-HLSFixer"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    torch_dtype=torch.bfloat16,
    device_map="auto"
)

buggy_code = """
#include "ap_fixed.h"
void vector_mul(ap_fixed<32,16> a[8], ap_fixed<32,16> b[8], ap_fixed<32,16> c[8]) {
    #pragma HLS ARRAY_PARTITION variable=a complete dim=1
    #pragma HLS ARRAY_PARTITION variable=b complete dim=1
    #pragma HLS ARRAY_PARTITION variable=c complete dim=1
    for (int i = 0; i < 8; ++i) {
        #pragma HLS UNROLL
        #pragma HLS PIPELINE II=1
        c[i] = a[i] * b[i];
    }
}
"""

error_log = """
    "ERROR: [HLS 214-274] In 'VITIS_LOOP_7_1', Pragma conflict happens on 'UNROLL' and 'PIPELINE' pragmas: Complete unroll will break the target of other loop pragmas (vector_mul.cpp:7:18)",
    "ERROR: [HLS 200-1715] Encountered problem during source synthesis"
"""

prompt = f"""You are given a piece of HLS code that contains bugs, the compiler error log, and the description of the buggy code. 
Your task is to debug the buggy code by analyzing the differences between it and the original code, using the error log to guide you.
Generate the COT debugging strategy through the buggy code, the code's description and error log.

**Instructions:**

1. **Debug the Code**:
    - **Identify Errors**: List the erroneous code line in the buggy code.
    - **Analyze Reasons**: Explain why each bug occurred, referencing the error log and the differences from the original code.
    - **Propose Fixes**: Specify the exact changes needed to fix each bug, including code modifications.

2. **Construct a Chain of Thought (CoT)**:
    - **Sequential Reasoning**: Address one error at a time, starting with syntax errors from the compiler.
    - **Hypothesis Formation**: Hypothesize why logical errors occur based on the error log and code comparison.
    - **Testing and Verification**: Validate hypotheses through analysis.
    - **Reflection**: Reflect on the findings after each step and plan the next steps.

3. **Output**:
    - **Chain of Thought (CoT)**: Write a detailed explanation of your debugging process.
    - **Bug Analysis in JSON Format**: Provide a structured summary as per the format below.
    - **No other explanations or conclusions**

**Inputs:**

Buggy Code: 
```cpp
{buggy_code}
```

Vitis HLS Compiler Error Log: 
```text
{error_log}
```

**Expected Output Format:**

Bug Analysis:
```json
{{
    "bug_analysis": [
        {{
            "erroneous_code_line": "[code line]",
            "reason": "[Explanation of why the bug occurred]",
            "modification_actions": [
                {{
                    "action": "[Specific modification to be made]",
                    "reason": "[Reason for the modification]"
                }}
            ]
        }}
    ]
}}
```
Please ensure that the JSON is properly formatted and valid.
"""

messages = [
    {"role": "system", 
     "content": """You are an expert in High-Level Synthesis (HLS) programming. 
                Your primary task is to identify and debug HLS-incompatible errors in regular C/C++ programs. You will be provided with:
                - **Buggy Code**: A C/C++ program containing HLS-incompatible errors.
                - **Compiler Error Log**: The error messages generated by the Vitis HLS compiler."""},
    {"role": "user", "content": prompt}
]

text = tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    add_generation_prompt=True
)

model_inputs = tokenizer([text], return_tensors="pt").to(model.device)

generated_ids = model.generate(
    **model_inputs,
    max_new_tokens=2048,
    temperature=0.7
)
generated_ids = [
    output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]

response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(response)
````