File size: 4,121 Bytes
b5ec529
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfe4d8c
b5ec529
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e374421
b5ec529
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7db33bb
b5ec529
7db33bb
 
 
 
 
 
 
 
 
b5ec529
7db33bb
b5ec529
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
427dd4a
b5ec529
 
 
 
 
 
 
 
 
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
---
license: apache-2.0
pipeline_tag: text-generation
tags:
- chat
- llm
- safetensors
base_model: Qwen/Qwen2.5-14B-Instruct
language:
- en
---

# ChatHLS-HLSTuner

## Model Details

- **Finetuned from model:** `Qwen/Qwen2.5-Coder-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-HLSTuner** is a specialized Large Language Model fine-tuned for High-Level Synthesis (HLS) C/C++ code optimization. It acts as the optimization 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 optimization analysis for HLS code:

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

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

hls_code = """
#include "ap_fixed.h"
#include "hls_math.h"

typedef ap_fixed<32,16> t_ap_fixed;

void gemm(  
		 t_ap_fixed alpha,
		 t_ap_fixed beta,
		 t_ap_fixed C[ 20 + 0][25 + 0],
		 t_ap_fixed A[ 20 + 0][30 + 0],
		 t_ap_fixed B[ 30 + 0][25 + 0])
{
  #pragma HLS top name=gemm

    const int ni = 20;
    const int nj = 25;
    const int nk = 30;

  int i, j, k;
L1:  for (i = 0; i < ni; i++) {
L2:    for (j = 0; j < nj; j++)
	C[i][j] *= beta;
L3:    for (k = 0; k < nk; k++) {
L4:       for (j = 0; j < nj; j++)
	  C[i][j] += alpha * A[i][k] * B[k][j];
    }
  }
}
"""

hls_code_ppa = """
latency: 15661 cycles
util_dsp: 10.5%
util_ff: 0.4%
util_lut: 2.1%
"""

loop_array_info = """
kernel_gemm
L1,loop,20
L2,loop,25
L3,loop,30
L4,loop,25
C,array,20/25
A,array,20/30
B,array,30/25
"""

prompt = f"""Your task is to optimize the following HLS kernel by inserting `#pragma` directives 
to enhance loops and arrays for lower latency and relatively low resource utilization. 
You may use the following pragma types:

- Loop Pipeline
- Loop Unroll
- Array Partition

Provided code:
{hls_code}

Current latency and hardware resource utilization:
{hls_code_ppa}

Loop and array information:
{loop_array_info}

Please suggest pragma modifications, specifying where to insert them and the reasoning behind each. 
Provide only the suggestions without additional explanations or formatting.

**Expected Output Format**:

Detailed Reasoning:
```text
[Your step-by-step analysis here]
```

Optimization Strategies:
```json
{{
    "Optimization Strategies": [
        {{
            "Code Segment Before Optimization (Loop/Array)": "[Code line before optimization]",
            "Code Segment After Optimization (Loop/Array)": "[Code line after inserting the pragma]",
            "Reason": "[Explanation of why this pragma was inserted]",
            "Result Analysis": {{
                "Impact of Inserting This Pragma on PPA": "[Description of the impact on Performance, Power, and Area]",
                "Reason": "[Explanation of why this impact occurred based on the performance analysis]"
            }}
        }}
    ]
}}
```
"""

messages = [
    {"role": "system", "content": """You are an expert proficient in HLS algorithm optimization,
                improving algorithm PPA through inserting pragmas into arrays and loops in HLS code."""},
    {"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)
````