torch.compile (inductor)
PyTorch · python · MIT
Kernel source · 40 lines ↓holds 2 records
Use it
Vendorable · source mirrored · MITView source →
No package. Vendor the mirrored source: 40 lines, MIT.
31_VisionAttention.py
curl "https://kernelindex.com/api/v1/implementations/kernelbench-l3-31-visionattention-torch-compile-inductor?include=source"interfacepython · torch_compile_inductor
symbolModel.forward
Compatibility
measured onNVIDIA H100
declared hardwaredeclared only
architectures—
dtypes
Benchmark evidence
2 measurements across 1 GPU, fastest first.
Operation / workload
Hardware
Latency
Rank
Observed
Reported · How evidence levels are derived →
Source and license
sourceavailable
revision digestsha256:7be813cb89461ce85f85a3105f0e1d318d2f5353198a4b45cf24b38695beea22
license declaredMIT
license concludedMIT
imported2026-08-26
Kernel source
31_VisionAttention.py40 lines
import torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self, embed_dim, num_heads):
"""
Attention Block using Multihead Self-Attention.
:param embed_dim: Embedding dimension (the number of channels)
:param num_heads: Number of attention heads
"""
super(Model, self).__init__()
self.attn = nn.MultiheadAttention(embed_dim, num_heads)
self.norm = nn.LayerNorm(embed_dim)
def forward(self, x):
"""
Forward pass of the AttentionBlock.
:param x: Input tensor of shape (B, C, H, W)
:return: Output tensor of the same shape (B, C, H, W)
"""
B, C, H, W = x.shape
x = x.view(B, C, H * W).permute(2, 0, 1) # (seq_len, batch_size, embed_dim)
attn_output, _ = self.attn(x, x, x)
x = self.norm(attn_output + x) # (seq_len, batch_size, embed_dim)
x = x.permute(1, 2, 0).view(B, C, H, W)
return x
embed_dim = 128
num_heads = 4
batch_size = 2
num_channels = embed_dim
image_height = 128
image_width = 128
def get_inputs():
return [torch.rand(batch_size, num_channels, image_height, image_width)]
def get_init_inputs():
return [embed_dim, num_heads]scrolls · 40 lines total
Source code from KernelBench, © 2023 Anne Ouyang, Simon Guo, Azalia Mirhoseini (Scaling Intelligence Lab, Stanford University), MIT License · MIT
Best evidence level for this revision: reported
JSON