import sys import torch from transformers import AutoTokenizer import transformers HF_MODEL = "facebook/KernelLLM" REPL_INSTRUCTIONS = """ You can paste or write your nn.Module code below (and finish with Ctrl+D). The model will try to optimize it with Triton kernels. Make sure that you provide a `get_inputs()` and `get_init_inputs()` function such that your model can be run like this args, kwargs = get_inputs() model = ModelNew(*args, **kwargs) out = model(get_inputs()) >>> """ DEFAULT_MODEL_CODE = """ import torch import torch.nn as nn class Model(nn.Module): \"\"\" A model that computes Hinge Loss for binary classification tasks. Parameters: None \"\"\" def __init__(self): super(Model, self).__init__() def forward(self, predictions, targets): return torch.mean(torch.clamp(1 - predictions * targets, min=0)) batch_size = 128 input_shape = (1,) dim = 1 def get_inputs(): return [torch.randn(batch_size, *input_shape), torch.randint(0, 2, (batch_size, 1)).float() * 2 - 1] def get_init_inputs(): return [] """ PROMPT_TEMPLATE = """ <|begin_of_text|>You write custom Triton 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 Triton 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. Here's an example to show you the syntax of inline embedding custom operators from the Triton DSL in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom Triton kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F import triton import triton.language as tl @triton.jit def add_kernel( x_ptr, # Pointer to first input y_ptr, # Pointer to second input out_ptr, # Pointer to output n_elements, # Total number of elements in input/output BLOCK_SIZE: tl.constexpr, ): # Each program handles a contiguous block of data of size BLOCK_SIZE block_start = tl.program_id(0) * BLOCK_SIZE # Create a range of offsets [0..BLOCK_SIZE-1] offsets = block_start + tl.arange(0, BLOCK_SIZE) # Mask to ensure we don't go out of bounds mask = offsets < n_elements # Load input values x = tl.load(x_ptr + offsets, mask=mask, other=0.0) y = tl.load(y_ptr + offsets, mask=mask, other=0.0) # Perform the elementwise addition out = x + y # Store the result tl.store(out_ptr + offsets, out, mask=mask) def triton_add(x: torch.Tensor, y: torch.Tensor): \"\"\" This function wraps the Triton kernel call. It: 1. Ensures the inputs are contiguous on GPU. 2. Calculates the grid (blocks) needed. 3. Launches the Triton kernel. \"\"\" assert x.is_cuda and y.is_cuda, "Tensors must be on CUDA." x = x.contiguous() y = y.contiguous() # Prepare output tensor out = torch.empty_like(x) # Number of elements in the tensor n_elements = x.numel() BLOCK_SIZE = 128 # Tunable parameter for block size # Determine the number of blocks needed grid = lambda meta: ((n_elements + meta["BLOCK_SIZE"] - 1) // meta["BLOCK_SIZE"],) # Launch the Triton kernel add_kernel[grid](x, y, out, n_elements, BLOCK_SIZE=BLOCK_SIZE) return out class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): # Instead of "return a + b", call our Triton-based addition return triton_add(a, b) ``` You are given the following architecture: ``` {} ``` Optimize the architecture named Model with custom Triton 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! """ def main(): tokenizer = AutoTokenizer.from_pretrained(HF_MODEL) pipeline = transformers.pipeline( "text-generation", model=HF_MODEL, torch_dtype=torch.float16, device_map="auto", ) while True: try: print(REPL_INSTRUCTIONS) prompt = sys.stdin.read().strip() if prompt.lower() == 'exit': exit() except EOFError: pass if not prompt: print(f"Using default prompt:\n{DEFAULT_MODEL_CODE}") prompt = PROMPT_TEMPLATE.format(DEFAULT_MODEL_CODE) response = pipeline( prompt, do_sample=True, top_k=0, temperature=0.6, top_p=0.95, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id, max_length=2048, truncation=True, )[0] print("Response:", response['generated_text']) if __name__ == "__main__": main()