Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Hugging Face 19 min read Deep dive hardMaster

Profiling in PyTorch (Part 2): From nn.Linear to a Fused MLP

Profiling an nn.Linear on an A100 shows a single cuBLAS/CUTLASS GEMM kernel with the bias folded in as an epilogue via aten::addmm, while aten::t only rewrites strides on the CPU and launches nothing. As a result, torch.compile has little to fuse for a lone Linear; it mainly removes CPU dispatch overhead by hard-coding strides, and the kernel's layout suffix (e.g. tn) shows how the transpose is absorbed. Stacking three Linears into a GeGLU MLP yields five kernels per forward pass: three GEMMs, each preceded by an occupancy query, plus separate GELU and multiply pointwise kernels. Reading kernel names and the profiler table shows which ops actually reach the GPU and where fusion could help.

Assumes: PyTorch basicsGPU kernels and CUDA launchestorch.profiler tracesmatrix multiplication

Related posts

  • Profiling in PyTorch (Part 3): Attention is all you profile

    Hugging Face mediumAdept

    Profiling attention variants in PyTorch on an A100 shows how each implementation appears in profiler traces. Naive attention launches matmul, mul, masking, softmax and matmul kernels plus a hidden Memcpy from out-of-place masked_fill; switching to in-place masked_fill_ removes that kernel under no_grad. SDPA's math backend turns out 3.7x slower than the naive version: it launches 20 kernels, upcasts to FP32 sgemm on CUDA cores instead of bf16 Tensor Cores, rebuilds the causal mask every call, and uses a NaN-safe softmax. Fused backends such as the efficient (CUTLASS fmha) backend collapse attention into a single kernel, and kernel names can be read to tell which hardware path each backend takes.

  • Profiling in PyTorch (Part 1): A Beginner's Guide to torch.profiler

    Hugging Face mediumAdept

    torch.profiler produces two artifacts: a statistical table showing where time goes and a Chrome/Perfetto trace showing when CPU and GPU events happen. A small 64x64 bf16 matmul+add is overhead-bound, with GPU time under 1% of CPU time, while 4096x4096 matrices shift the work to compute-bound GEMM kernels. The trace walkthrough explains a cold-start dead window of about 228 µs before aten::matmul dispatch, a CPU-GPU lane offset, gaps caused by profiler Activity Buffer Requests, and the dispatch chain from Python down to CUDA kernels. The practical advice is to warm up, use profiler schedules, and read Self versus total times carefully before turning to torch.compile.

  • Learn the Hugging Face Kernel Hub in 5 Minutes

    Hugging Face mediumAdept

    Hugging Face's Kernel Hub and `kernels` library let PyTorch code download pre-compiled, optimized GPU kernels such as FlashAttention, activations, RMSNorm and quantization kernels with a single `get_kernel` call. The library matches the binary to the installed Python, PyTorch and CUDA versions, which avoids source builds that for FlashAttention can need about 96 GB of RAM and hours of compilation. The walkthrough checks a fast GELU kernel against PyTorch and swaps a Triton LlamaRMSNorm into a simple model with the `use_kernel_forward_from_hub` decorator. It then benchmarks the kernel against a plain PyTorch RMSNorm using CUDA events.

  • Profiling TorchNeuron (Part 1): System-level profiling on AWS Trainium.

    Hugging Face mediumAdept

    A beginner-friendly walkthrough of system-level profiling for PyTorch on AWS Trainium using TorchNeuron. It covers configuring torch.profiler with NeuronConfig and NeuronProfiler, reading the key_averages() table, and tracing a single operation through framework, runtime and hardware lanes in Neuron Explorer. Three RMSNorm implementations are profiled. Naive eager ops take 2,355 µs of device time across 6 dispatches. torch.rms_norm lowers to one fused kernel at 403 µs, and torch.compile discovers the same fusion at 376 µs, roughly one-sixth of the naive cost because gaps between dispatches and intermediate tensors are eliminated.

  • Custom Kernels for All from Codex and Claude

    Hugging Face mediumAdept

    Hugging Face packaged CUDA kernel expertise (GPU architecture guides for H100/A100/T4, diffusers and transformers integration patterns, vectorized kernel templates, benchmark scripts) into an agent skill that lets Claude and Codex generate complete kernel-builder projects with PyTorch bindings. Agent-written RMSNorm kernels averaged 1.88x over PyTorch for LTX-Video and 1.94x for Qwen3-8B on H100 in BF16, with Qwen3 speedups growing from 1.58x at 128 tokens to 2.47x at 8192. End to end, LTX-Video gained only 6% (1.43x with torch.compile), consistent with RMSNorm taking about 5% of compute. The post also walks through building all PyTorch/CUDA variants with Nix and publishing to the Kernel Hub for one-line loading via get_kernel.

The quest for software engineering knowledge.

Look:
Layout: