Skip to content
aalramadanPublic

Latest commit

ย 

History

50 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

LaneScope

Find and fix shared-memory bank conflicts in CUDA kernels.

Tests Python 3.11+ License GPL v3 Live demo

LaneScope ranking the conflicts of the demo, explaining the bank conflict of a matrix transpose, showing its fix, and removing the conflicts of a matrix multiply tile with a swizzle

LaneScope records the memory accesses of a running CUDA program, shows which threads collide in shared memory, and ranks the padding or swizzle that removes each conflict.

๐ŸŒ Live Demo

Try LaneScope in your browser, with no install.

The demo opens on a summary that ranks the conflicts of the recordings below, largest first.

Example What it shows
Transpose A shared tile read down its columns, a 32-way bank conflict that padding or a swizzle removes
Row sums Threads that each read one 256-byte row of a shared tile, a conflict of the same kind
Column sums Threads that each read one column of a column-major matrix in global memory, one sector per thread
Matmul tile A tile copied with cp.async and read with ldmatrix, which needs the swizzle of CuTe
Histogram Bins of a shared histogram that the input values pick, a conflict that no padding or swizzle removes
llama.cpp indexer The conflicts LaneScope found in llama.cpp, before and after the fix that llama.cpp merged in pull request #31803 (details)

Note

The page runs the whole analysis inside your browser. Recording your own program needs the local install below.

โœจ Highlights

Feature Description
๐Ÿ“Š Run summary Every kernel and source line of a recording, ranked by the extra passes it causes
๐Ÿ”ฌ Thread view Every shared and global memory access, thread by thread, beside its source line, with its cost and its best fix in plain words
๐Ÿ“Š Nsight-matching counts The wavefronts and sectors that Nsight Compute reports, computed from the recorded addresses
๐Ÿ› ๏ธ Fix search Paddings and swizzles ranked by the conflicts each removes, written as code, with a warning where the input values decide the conflicts
๐Ÿ”Œ No rebuild Any CUDA program, including the Triton kernels that PyTorch compiles
๐Ÿ–ฅ๏ธ Three interfaces A browser GUI, a command line, and a Model Context Protocol (MCP) server for coding agents
๐ŸŽฎ Tested GPUs Counts checked against Nsight Compute on an RTX 4060 and an RTX 5070 Laptop GPU

๐Ÿ“ฆ Installation

git clone https://github.com/aalramadan/LaneScope.git
cd LaneScope
pip install .                # analysis, command line, and GUI
pip install ".[gpu]"         # recording on an NVIDIA GPU
pip install ".[gpu-cuda13]"  # recording with the CUDA 13 tools, as on RTX 50 GPUs

Tip

The analysis and the GUI run without a GPU. Only recording needs an NVIDIA GPU and g++. The gpu extra brings the CUDA tools that recording uses, with no CUDA Toolkit to install. On a driver for CUDA 13 or later, as on RTX 50 GPUs, the gpu-cuda13 extra brings the matching tools.

๐Ÿš€ Quick Start

lanescope gui --demo                          # opens http://localhost:8765 with the demo kernels
lanescope record --out run1 -- ./my_app       # record a CUDA program
lanescope summary run1                        # rank the lines of every launch by their conflicts
lanescope gui run1                            # explore the recording
lanescope suggest run1/trace-0.npz            # rank the fixes in the terminal

๐Ÿ“– Usage

๐Ÿ” Find the Conflicts

lanescope explain lists the memory instructions of a trace, worst first. It counts shared-memory wavefronts, the passes that the access of a warp takes, and global-memory sectors, the 32-byte blocks that it moves.

$ lanescope demo --out transpose.npz
$ lanescope explain transpose.npz
kernel transpose (synthetic), 512 warp-level accesses

pc       line  space  kind  width  requests  unit        achieved  ideal excessive
0x0120     10  shared load      4       128  wavefronts      4096    128      3968
0x00a0      5  global load      4       128  sectors          512    512         0
0x00b0      5  shared store     4       128  wavefronts       128    128         0
0x0130     10  global store     4       128  sectors          512    512         0

Important

The shared load at line 10 has a 32-way bank conflict. It takes 4,096 wavefronts, 32 times its ideal of 128.

๐Ÿ› ๏ธ Fix Them

lanescope suggest ranks the layouts of each buffer by the conflicts each layout removes.

$ lanescope suggest transpose.npz
buffer 0x0+4096
XOR swizzle Swizzle<5,2,5> on byte offsets: excessive wavefronts 3968 -> 0, 0 extra bytes
pad each 128-byte row by 4 bytes: excessive wavefronts 3968 -> 0, 128 extra bytes
...

For a float tile[32][32], the top-ranked swizzle changes one index. On float indices in place of byte offsets, Swizzle<5,2,5> becomes the CuTe Swizzle<5, 0, 5>.

tile[r][c ^ (r & 0x1f)]   // CuTe: Swizzle<5, 0, 5>

โบ๏ธ Record Your Own Program

Recording runs your program under LaneScope and saves the memory address of every thread at each memory access. The GUI and the other commands read these traces.

lanescope record --out run1 -- ./my_app --size 4096
lanescope record --out triton --kernel matmul_kernel -- python examples/triton_matmul.py
lanescope gui run1

Tip

--kernel records only the kernels whose names contain the given text. --ctas records a range of thread blocks. Compiling with -lineinfo adds source lines and is optional. A program that queues many launches without synchronizing gets every launch recorded, since each launch saves its records once its threads finish.

๐Ÿ“Š Rank Every Launch

A real program launches many kernels. lanescope summary adds up the conflicts of every launch by kernel and by source line. The program examples/conflict_kinds.py runs a transpose, a histogram, a tree sum, and a scale, each with a different kind of conflict.

$ lanescope record --out run1 -- python examples/conflict_kinds.py
...
$ lanescope summary run1
run1: 8 launches of 4 kernels
Shared memory: 59,563 passes where 10,576 would do. Bank conflicts add 48,987 of them (82.2%).

Kernels by extra passes
  extra passes  of kernel  launches  kernel
        31,744      93.9%         2  transpose
        15,563      67.0%         3  histogram
         1,680      70.0%         2  tree_sum
             0       0.0%         1  scale

Lines by extra passes
  extra passes   ratio  of kernel  launches  line                              kernel
        31,744   32.0x      93.9%         2  conflict_kinds.cu:23 load         transpose
        15,563    3.5x      67.0%         3  conflict_kinds.cu:8 atomic *      histogram
         1,120    4.2x      46.7%         2  conflict_kinds.cu:35 load         tree_sum
           560    4.2x      23.3%         2  conflict_kinds.cu:35 store        tree_sum

* The conflicts of this line follow the values that the kernel reads, such as the bins of a histogram. No padding or swizzle removes them.

Worst line: lanescope suggest run1/trace-3.npz --pc 0x1b0 ranks its fixes, and lanescope gui run1 opens it in the browser.

The column of kernel gives the extra passes of a line as a part of all the passes of its kernel. A fix of the line cuts the passes of the kernel by this part. A star marks a line whose banks follow the values that the kernel reads, such as the bins of a histogram. No padding or swizzle removes such conflicts. lanescope gui run1 opens on the same ranking. A click on a line opens the launch where the line wastes the most passes.

๐Ÿค– Use with Coding Agents

pip install ".[mcp]"
lanescope mcp

An MCP client adds the server with the command lanescope and the argument mcp.

๐Ÿงฐ Commands

Command Description
record Records the memory accesses of a CUDA or Python program
gui Opens the browser GUI
summary Ranks the kernels and source lines of a recording by their extra passes
explain Lists the memory instructions of a trace, worst first
lanes Shows where each thread of a warp lands
suggest Ranks the paddings and swizzles of each buffer
validate Compares the counts with Nsight Compute
get-ncu Installs Nsight Compute for the local driver
demo Writes the trace of a demo kernel
bench Runs the built-in test kernels and their timings
mcp Serves the analyses to coding agents

Run lanescope <command> --help for the options of each command.

๐ŸŽฏ Accuracy

On an RTX 4060 Laptop GPU, the counts of LaneScope match those of Nsight Compute in all 2,710 counts of its built-in test kernels and in 2,027 of the 2,040 counts of the PyTorch and Triton examples. On an RTX 5070 Laptop GPU, LaneScope matches 2,819 of the 2,826 counts of the test kernels. lanescope validate runs the same comparison on any GPU or program.

lanescope get-ncu                 # installs Nsight Compute for the local driver
lanescope validate --out check

โšก Performance

On the RTX 4060, the swizzle that LaneScope ranks first makes its built-in transpose 1.13 to 1.19 times faster than the plain layout and its matrix multiply 1.72 to 1.77 times faster. lanescope bench build and lanescope bench time repeat these timings.

๐Ÿ“‹ Requirements

  • Linux or WSL2 with Python 3.11 or later
  • For recording, g++ and an NVIDIA GPU with a driver for CUDA 12.4 or later
  • Counts validated on an RTX 4060 and an RTX 5070 Laptop GPU. lanescope validate checks them on any other GPU.

โš™๏ธ How It Works

Step Module Description
1 recorder/ Records the address of each thread at every memory access, through Compute Sanitizer
2 model/ Counts wavefronts and sectors from the addresses and marks the conflicts that the input values decide
3 summary.py Adds up the counts of every launch by kernel and by source line
4 fixes/ Tries paddings and swizzles on the recorded addresses and ranks them
5 gui/, cli.py, mcp_server.py Show the results
6 nsight/ Checks the counts against Nsight Compute

๐Ÿง‘โ€๐Ÿ’ป Development

pip install -e ".[gpu,mcp]" pytest ruff pyright playwright
python -m playwright install chromium-headless-shell
pytest

๐Ÿ™ Acknowledgments

The swizzle notation follows CuTe from NVIDIA CUTLASS. The rules for H100 and newer GPUs come from the NVIDIA developer forum.

๐Ÿ“„ License

LaneScope is released under the GNU General Public License v3.0 or later. See LICENSE.

Releases

Packages

Contributors

Languages