torch.compile (inductor)
PyTorch · python · MIT
Kernel source · 84 lines ↓holds 2 records
Use it
Vendorable · source mirrored · MITView source →
No package. Vendor the mirrored source: 84 lines, MIT.
28_VisionTransformer.py
curl "https://kernelindex.com/api/v1/implementations/kernelbench-l3-28-visiontransformer-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:36443ef327180c1d7eb07fac2f3d4f88a859aff6fc76c8a92a6a210e031ef79c
license declaredMIT
license concludedMIT
imported2026-08-26
Kernel source
28_VisionTransformer.py84 lines
import torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self, image_size, patch_size, num_classes, dim, depth, heads, mlp_dim, channels=3, dropout=0.1, emb_dropout=0.1):
"""
Vision Transformer (ViT) model.
:param image_size: The size of the input image (assumed to be square).
:param patch_size: The size of each patch (assumed to be square).
:param num_classes: The number of output classes.
:param dim: The dimensionality of the embedding space.
:param depth: The number of transformer layers.
:param heads: The number of attention heads.
:param mlp_dim: The dimensionality of the MLP (Multi-Layer Perceptron) in the transformer.
:param channels: The number of channels in the input image (default is 3 for RGB).
:param dropout: Dropout rate applied in the MLP.
:param emb_dropout: Dropout rate applied to the embedded patches.
"""
super(Model, self).__init__()
assert image_size % patch_size == 0, "Image dimensions must be divisible by the patch size."
num_patches = (image_size // patch_size) ** 2
patch_dim = channels * patch_size ** 2
self.patch_size = patch_size
self.pos_embedding = nn.Parameter(torch.randn(1, num_patches + 1, dim))
self.patch_to_embedding = nn.Linear(patch_dim, dim)
self.cls_token = nn.Parameter(torch.randn(1, 1, dim))
self.dropout = nn.Dropout(emb_dropout)
self.transformer = nn.TransformerEncoder(
nn.TransformerEncoderLayer(d_model=dim, nhead=heads, dim_feedforward=mlp_dim, dropout=dropout),
num_layers=depth
)
self.to_cls_token = nn.Identity()
self.mlp_head = nn.Sequential(
nn.Linear(dim, mlp_dim),
nn.GELU(),
nn.Dropout(dropout),
nn.Linear(mlp_dim, num_classes)
)
def forward(self, img):
"""
Forward pass of the Vision Transformer.
:param img: The input image tensor, shape (batch_size, channels, image_size, image_size).
:return: The output tensor, shape (batch_size, num_classes).
"""
p = self.patch_size
x = img.unfold(2, p, p).unfold(3, p, p).reshape(img.shape[0], -1, p*p*img.shape[1])
x = self.patch_to_embedding(x)
cls_tokens = self.cls_token.expand(img.shape[0], -1, -1)
x = torch.cat((cls_tokens, x), dim=1)
x += self.pos_embedding
x = self.dropout(x)
x = self.transformer(x)
x = self.to_cls_token(x[:, 0])
return self.mlp_head(x)
# Test code
image_size = 224
patch_size = 16
num_classes = 10
dim = 512
depth = 6
heads = 8
mlp_dim = 2048
channels = 3
dropout = 0.0
emb_dropout = 0.0
def get_inputs():
return [torch.rand(2, channels, image_size, image_size)]
def get_init_inputs():
return [image_size, patch_size, num_classes, dim, depth, heads, mlp_dim, channels, dropout, emb_dropout]scrolls · 84 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