Views
No views yet
1from vllm import LLM, SamplingParams
2from transformers import AutoTokenizer
3import torch
4import re
5from typing import List, Tuple
6from string import Template
7PROMPT_TEMPLATE = Template('''
8You are a Machine Learning Engineer trying to write custom cuda kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom cuda kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination.
9
10For [Imports], you will likely need but not limited to the following libraries:
11```python
12import torch
13import torch.nn as nn
14import torch.nn.functional as F
15import math
16```
17
18Here’s an example to show you the syntax of inline embedding custom operators from the cuda kernel in torch:
19
20The pytorch module needed to be optimize is:
21```python
22$ref_arch_torch
23```
24
25The example new arch with custom cuda kernels looks like this:
26```python
27$ref_arch_kernel
28```
29
30And the PyTorch code you need to optimize is:
31```python
32$code
33```
34
35Optimize the architecture named Model with custom cuda kernels! Optimize the architecture named Model with custom cuda kernels! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
36
37''')
38
39class KernelCoder:
40
41def __init__(self, model_name="lkongam/KernelCoder", tensor_parallel_size=1, gpu_memory_utilization=0.9):
42
43self.model_name = model_name
44
45self.llm = LLM(
46model=model_name,
47tensor_parallel_size=tensor_parallel_size,
48gpu_memory_utilization=gpu_memory_utilization,
49trust_remote_code=True,
50dtype="auto"
51)
52
53self.tokenizer = self.llm.get_tokenizer()
54self.device = torch.device("cuda")
55
56def generate_raw(self, prompt, temperature=1.0):
57messages = [
58{"role": "user", "content": prompt}
59]
60text = self.tokenizer.apply_chat_template(
61messages,
62tokenize=False,
63add_generation_prompt=True,
64enable_thinking=True
65)
66return text
67
68def extract_last_code_block(text):
69code_blocks = re.findall(r"```(?:python)?\n(.*?)```", text, re.DOTALL)
70if code_blocks:
71return code_blocks[-1].strip()
72match = re.search(r"</think>(.*)", text, re.S)
73after_think = match.group(1).strip() if match else text
74if not after_think:
75return None
76import_match = re.search(r"\bimport\b", after_think)
77if import_match:
78return after_think[import_match.start():].strip()
79return after_think.strip()
80
81origin_code = """
82"""
83
84model = KernelCoder(model_name="lkongam/KernelCoder")
85
86prompt = PROMPT_TEMPLATE.substitute(code=origin_code)
87code_output = model.generate_raw(prompt)
88code = extract_last_code_block(code_output)
89print(code)