---
title: "Profiling in PyTorch (Part 3): Attention is all you profile"
description: "Profiling attention variants in PyTorch on an A100 shows how each implementation appears in profiler traces."
url: "https://swe.quest/articles/profiling-in-pytorch-part-3-attention-is-all-you-profile-44c4f73b7586bec2"
canonical: "https://huggingface.co/blog/torch-attention-profile"
source: "Hugging Face"
published: "2026-07-10"
difficulty: "medium"
kind: "Deep dive"
reading_minutes: 21
tags: ["performance","inference","python","hardware","llms"]
---

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

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2026-07-10, 21 min read, Deep dive. Original: [https://huggingface.co/blog/torch-attention-profile](https://huggingface.co/blog/torch-attention-profile)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)
- [Inference & serving](https://swe.quest/tags/inference.md) (AI & ML)
- [Python](https://swe.quest/tags/python.md) (Languages)
- [Hardware](https://swe.quest/tags/hardware.md) (Infrastructure)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)

## Prerequisites

- PyTorch basics
- attention mechanism
- torch.profiler traces
- GPU kernels ([easy posts on Hardware](https://swe.quest/tags/hardware.md?difficulty=easy))

## Keywords

PyTorch, torch.profiler, scaled\_dot\_product\_attention, FlashAttention, CUTLASS, NVIDIA A100, Tensor Cores, cuDNN

## Related posts

- [Profiling in PyTorch (Part 2): From nn.Linear to a Fused MLP](https://swe.quest/articles/profiling-in-pytorch-part-2-from-nn-linear-to-a-fused-mlp-9b220b5d3b1f868e.md): Hugging Face · 2026-06-11 · Hard · 19 min read · Performance engineering, Python, Machine learning, Compilers
  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.
- [We reverse-engineered Flash Attention 4](https://swe.quest/articles/we-reverse-engineered-flash-attention-4-e0ddc41706e090a5.md): Modal · 2025-09-26 · Hard · 18 min read · Inference & serving, Hardware, Performance engineering, Concurrency, LLMs
  Flash Attention 4, Tri Dao's attention kernel for Nvidia Blackwell GPUs, reportedly runs about 20% faster than cuDNN's closed-source attention kernels.
- [FlashAttention-4: Algorithm and Kernel Pipelining Co-Design for Asymmetric Hardware Scaling](https://swe.quest/articles/flashattention-4-algorithm-and-kernel-pipelining-co-design-for-asymmetric-67fe95e5005d9861.md): Together AI · 2026-03-05 · Hard · 11 min read · Inference & serving, Performance engineering, Hardware, Model training, LLMs
  FlashAttention-4 is an attention kernel co-designed for Blackwell GPUs, where tensor core throughput grew much faster than the exponential (SFU) units and shared memory bandwidth.
- [Making FlashAttention-4 faster for inference](https://swe.quest/articles/making-flashattention-4-faster-for-inference-dc917e70c20e0f22.md): Modal · 2026-06-11 · Hard · 14 min read · Inference & serving, Hardware, Performance engineering, LLMs
  Modal engineers extended the FlashAttention-4 kernel for decode-heavy LLM inference on Blackwell GPUs through two kinds of change: new parallelism strategies, such as split-KV parallelism across the key/value dimension, and support for irregular memory access via cp.async in place of TMA bulk loads.
- [Learn the Hugging Face Kernel Hub in 5 Minutes](https://swe.quest/articles/learn-the-hugging-face-kernel-hub-in-5-minutes-049114b1e85677a7.md): Hugging Face · 2025-06-12 · Medium · 13 min read · Inference & serving, Performance engineering, Python, Hardware
  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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
