Views
No views yet
Qwen/Qwen2.5-Coder-14B-Instructpip install transformers torch accelerate1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4model_name = "XXXiong/ChatHLS-HLSTuner"
5tokenizer = AutoTokenizer.from_pretrained(model_name)
6model = AutoModelForCausalLM.from_pretrained(
7 model_name,
8 torch_dtype=torch.bfloat16,
9 device_map="auto"
10)
11
12hls_code = """
13#include "ap_fixed.h"
14#include "hls_math.h"
15
16typedef ap_fixed<32,16> t_ap_fixed;
17
18void gemm(
19 t_ap_fixed alpha,
20 t_ap_fixed beta,
21 t_ap_fixed C[ 20 + 0][25 + 0],
22 t_ap_fixed A[ 20 + 0][30 + 0],
23 t_ap_fixed B[ 30 + 0][25 + 0])
24{
25 #pragma HLS top name=gemm
26
27 const int ni = 20;
28 const int nj = 25;
29 const int nk = 30;
30
31 int i, j, k;
32L1: for (i = 0; i < ni; i++) {
33L2: for (j = 0; j < nj; j++)
34 C[i][j] *= beta;
35L3: for (k = 0; k < nk; k++) {
36L4: for (j = 0; j < nj; j++)
37 C[i][j] += alpha * A[i][k] * B[k][j];
38 }
39 }
40}
41"""
42
43hls_code_ppa = """
44latency: 15661 cycles
45util_dsp: 10.5%
46util_ff: 0.4%
47util_lut: 2.1%
48"""
49
50loop_array_info = """
51kernel_gemm
52L1,loop,20
53L2,loop,25
54L3,loop,30
55L4,loop,25
56C,array,20/25
57A,array,20/30
58B,array,30/25
59"""
60
61prompt = f"""Your task is to optimize the following HLS kernel by inserting `#pragma` directives
62to enhance loops and arrays for lower latency and relatively low resource utilization.
63You may use the following pragma types:
64
65- Loop Pipeline
66- Loop Unroll
67- Array Partition
68
69Provided code:
70{hls_code}
71
72Current latency and hardware resource utilization:
73{hls_code_ppa}
74
75Loop and array information:
76{loop_array_info}
77
78Please suggest pragma modifications, specifying where to insert them and the reasoning behind each.
79Provide only the suggestions without additional explanations or formatting.
80
81**Expected Output Format**:
82
83Detailed Reasoning:
84```text
85[Your step-by-step analysis here]
86```
87
88Optimization Strategies:
89```json
90{{
91 "Optimization Strategies": [
92 {{
93 "Code Segment Before Optimization (Loop/Array)": "[Code line before optimization]",
94 "Code Segment After Optimization (Loop/Array)": "[Code line after inserting the pragma]",
95 "Reason": "[Explanation of why this pragma was inserted]",
96 "Result Analysis": {{
97 "Impact of Inserting This Pragma on PPA": "[Description of the impact on Performance, Power, and Area]",
98 "Reason": "[Explanation of why this impact occurred based on the performance analysis]"
99 }}
100 }}
101 ]
102}}
103```
104"""
105
106messages = [
107 {"role": "system", "content": """You are an expert proficient in HLS algorithm optimization,
108 improving algorithm PPA through inserting pragmas into arrays and loops in HLS code."""},
109 {"role": "user", "content": prompt}
110]
111
112text = tokenizer.apply_chat_template(
113 messages,
114 tokenize=False,
115 add_generation_prompt=True
116)
117
118model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
119
120generated_ids = model.generate(
121 **model_inputs,
122 max_new_tokens=2048,
123 temperature=0.7
124)
125generated_ids = [
126 output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
127]
128
129response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
130print(response)