Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3import re
4
5model_name = "TabCanNotTab/SALV-Qwen2.5-Coder-7B-Instruct"
6model = AutoModelForCausalLM.from_pretrained(
7 model_name,
8 torch_dtype=torch.bfloat16,
9 device_map="auto",
10)
11tokenizer = AutoTokenizer.from_pretrained(model_name)
12
13prompt = """
14Please act as a professional verilog designer.
15
16Implement a module of an 8-bit adder with multiple bit-level adders in combinational logic.
17
18Module name:
19 adder_8bit
20Input ports:
21 a[7:0]: 8-bit input operand A.
22 b[7:0]: 8-bit input operand B.
23 cin: Carry-in input.
24Output ports:
25 sum[7:0]: 8-bit output representing the sum of A and B.
26 cout: Carry-out output.
27
28Implementation:
29The module utilizes a series of bit-level adders (full adders) to perform the addition operation.
30
31Give me the complete code.
32"""
33
34messages = [
35 {"role": "system", "content": "You are a helpful assistant."},
36 {"role": "user", "content": prompt}
37]
38
39text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
40model_inputs = tokenizer(text, return_tensors="pt").to(model.device)
41
42# inference
43outputs = model.generate(
44 **model_inputs,
45 max_new_tokens=2048,
46 do_sample=True,
47 temperature=0.5,
48 top_p=0.95
49)
50
51# get response text
52input_length = model_inputs.input_ids.shape[1]
53generated_tokens = outputs[0][input_length:]
54response = tokenizer.decode(generated_tokens, skip_special_tokens=True)
55
56# get code text
57pattern = r"```verilog\s*(.*?)\s*```"
58matches = re.findall(pattern, response, re.DOTALL)
59if matches:
60 code=matches[-1]
61 print(code)
62else:
63 print("No Verilog code found in the response!")