168 lines
4.0 KiB
Markdown
168 lines
4.0 KiB
Markdown
---
|
|
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)
|
|
````
|