PyTorch eager
PyTorch · python · MIT
Use it
Vendorable · source mirrored · MITView source →
No package. Vendor the mirrored source: 71 lines, MIT.
21_EfficientNetMBConv.py
curl "https://kernelindex.com/api/v1/implementations/kernelbench-l3-21-efficientnetmbconv-torch?include=source"interfacepython · torch_eager
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:9fbabc0a4f552630a25724b182c63957a4db4dc192769aa20aac73159ba38798
license declaredMIT
license concludedMIT
imported2026-08-26
Kernel source
21_EfficientNetMBConv.py71 lines
import torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride, expand_ratio):
"""
MBConv block implementation.
:param in_channels: Number of input channels.
:param out_channels: Number of output channels.
:param kernel_size: Kernel size for the depthwise convolution.
:param stride: Stride for the depthwise convolution.
:param expand_ratio: Expansion ratio for the intermediate channels.
"""
super(Model, self).__init__()
self.use_residual = (stride == 1 and in_channels == out_channels)
hidden_dim = in_channels * expand_ratio
if expand_ratio != 1:
self.expand_conv = nn.Sequential(
nn.Conv2d(in_channels, hidden_dim, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(hidden_dim),
nn.ReLU6(inplace=True)
)
self.depthwise_conv = nn.Sequential(
nn.Conv2d(hidden_dim, hidden_dim, kernel_size=kernel_size, stride=stride, padding=(kernel_size-1)//2, groups=hidden_dim, bias=False),
nn.BatchNorm2d(hidden_dim),
nn.ReLU6(inplace=True)
)
self.project_conv = nn.Sequential(
nn.Conv2d(hidden_dim, out_channels, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(out_channels)
)
def forward(self, x):
"""
Forward pass of the MBConv block.
:param x: The input tensor, shape (batch_size, in_channels, H, W)
:return: The output tensor, shape (batch_size, out_channels, H', W')
"""
identity = x
if hasattr(self, 'expand_conv'):
x = self.expand_conv(x)
x = self.depthwise_conv(x)
x = self.project_conv(x)
if self.use_residual:
x += identity
return x
# Test code
batch_size = 10
in_channels = 112
out_channels = 192
kernel_size = 5
stride = 2
expand_ratio = 6
def get_inputs():
return [torch.rand(batch_size, in_channels, 224, 224)]
def get_init_inputs():
return [in_channels, out_channels, kernel_size, stride, expand_ratio]scrolls · 71 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