Skip to content

Latest commit

 

History

History

samples

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

FE - Programming Samples

Python Interface Samples

Samples leveraging FE's Python interface are located in samples/python. Where a notebook needs a specific GPU, cuDNN version or driver, it checks this itself and skips the affected cells; those requirements are noted below.

FROST Python Samples

Standalone scripts for the FROST (CuTeDSL) engines, pure cuDNN frontend Python API, each checked against a torch or fp64 reference:

  • frost/gemm: matmul across dtypes, fused epilogues, mainloop fusion, mixed input types, dual-GEMM SwiGLU, MoE grouped matmul. Needs a data-center Blackwell-family GPU (100 <= SM < 120); on other parts the scripts print [NN] SKIP needs a data-center Blackwell-family GPU (100 <= SM < 120) and exit 0.
  • frost/linear_attention: GDN, KDA, GDN-2, GDP and Mamba-2 prefill and backward, plus the per-span summary nodes (forward and backward) used for context parallelism. 27_gdn_forward_context.py to 30_gdp_forward_context.py run the forward with output_context=True and hand its context to the backward, under autograd and through the backward op directly. 26_gdn_decode_fused.py drives the fused GDN decode step (cudnn.GdnDecodeFused) from a slot pool for 32 steps at 1 and 4 rows, checked against an fp64-accumulated PyTorch step; the entry serves compute capability 10.x and 12.x GPUs, and elsewhere the script prints [26] SKIP with the requirement and exits 0.
  • frost/gated_attention_block: the gated attention block's training forward (save_for_backward=True) -- the caller-owned SavedForBackward record and the zero-copy saved_slab_views of its projection slab, checked against torch (Rubin only).
python samples/frost/linear_attention/01_gdn_prefill.py

Model Samples

End-to-end samples of open-weight model families in samples/models: one process, a printed table of which provider serves every block of the model, and a self-check against a plain-PyTorch reference. They are not a serving engine and carry no performance table; see models/README.md.

  • models/qwen_dense: the dense Qwen 3.5-architecture model. 00_decode.py runs a plain greedy decode on the reduced seeded model (2 layers at the published widths, no checkpoint, no extra package), token ids in and token ids out, with the linear-attention step on cudnn.GdnDecodeFused. Needs a GPU of compute capability 10.x or 12.x; elsewhere it prints [00] SKIP <requirement> and exits 0. With --model DIR it runs a checkpoint from local disk, text in and text out, with the checkpoint's own tokenizer and chat template: from a prompt, chat messages or the request body a chat client sends (--messages), with the generated text on standard output and every other line of the run on standard error; that needs the packages of samples/models/requirements.txt, the checkpoint on local disk (your own download: the sample fetches nothing) and a GPU with room for a bf16 copy of every weight, about 57 GiB free for the 27B checkpoint. 02_speculative_decode.py runs speculative decoding with the checkpoint's drafter for one request: the prompt through decode steps, then per step the drafter's passes on reference blocks, one verify step on cudnn.GdnVerifyFused and one host read; by default on the reduced seeded model with a seeded drafter, self-checking against decode steps of the same tokens; with --model DIR, a draft vocabulary (--draft-vocab FILE) and, as a JSON object, the speculative configuration of a serving engine (--speculative-config) it writes the text of a checkpoint and reports the tokens a step kept.
python samples/models/qwen_dense/00_decode.py
python samples/models/qwen_dense/02_speculative_decode.py
pip install -r samples/models/requirements.txt
hf download nvidia/Qwen3.8-27B-NVFP4 --local-dir /data/Qwen3.8-27B-NVFP4
python samples/models/qwen_dense/00_decode.py --model /data/Qwen3.8-27B-NVFP4 --prompt "Why is the sky blue?"
python samples/models/qwen_dense/00_decode.py --model /data/Qwen3.8-27B-NVFP4 --messages request.json --max-tokens 256 > answer.txt

C++ Interface Samples

Samples leveraging FE's C++ interface are located in samples/cpp.

Building the samples

mkdir build
cd build
cmake -DCUDNN_PATH=/path/to/cudnn -DCUDAToolkit_ROOT=/path/to/cuda  ../
cmake --build . -j16
bin/samples

To run a single sample, for eg. TEST_CASE("Cached sdpa", "[graph][sdpa][flash]")

./bin/samples "Cached sdpa"

Scaled dot product attention SDPA examples

samples/cpp/sdpa shows how to use cudnn's sdpa operation.

Users are expected to build a graph once and then execute it multiple times. This example shows how to cache cudnn sdpa graph building.

cudnn's sdpa operation enables various customizations on itself. These examples show how to build a graph with sdpa operation for your own custom sdpa needs.

Similar to Fwd SDPA, but here with the ability to use non contiguous K and V caches in combination with page tables, as described in the PagedAttention paper.

Extends the sdpa sample to fp8 precision.

Demonstrates the building and execution of a CUDA graph representing the SDPA operation, followed by the update (and another execution) of the CUDA graph with new variant pointers.

The same CUDA graph build-and-update flow for the SDPA backward pass. Needs cuDNN 9.6.0 or newer.

SDPA with score modifiers (softcap and bias) built from graph operations. Needs cuDNN 9.4.0 or newer; the backward sample needs Hopper or newer and is not yet supported on SM120 and above.

Also returns the row max and the sum of exponentials of the attention scores. Needs cuDNN 9.13 or newer.

Attention with a learned sink token. Needs cuDNN 9.13 or newer.

Passes a caller-provided dropout mask and scale. Not supported on Blackwell.

Variable-length sequences described by cumulative sequence lengths. Needs cuDNN 9.24.0 or newer.

Decode and prefill over the same paged K/V caches. Needs cuDNN 9.8.0 or newer.

Block-sparse masking. Needs compute capability 10.x and cuDNN 9.26 or newer.

One graph built with shape overrides enabled and executed for different problem sizes. Needs cuDNN 9.21 or newer at build time.

Measures graph API runtimes. Long-running, so it is off by default.

FP8 attention with a bottom-right aligned causal mask. The forward sample needs a Hopper or data-center Blackwell GPU; the backward sample needs compute capability 10.x.

FP8 attention whose scales are computed from the current tensors. Needs compute capability 10.x and cuDNN 9.13.0 or newer.

MXFP8 block-scaled attention; the backward file covers MHA, GQA and MLA. Needs a data-center Blackwell GPU and cuDNN 9.21.0 or newer.

Convolution fusion examples

samples/cpp/convolution shows how to use cudnn fprop, dgrad, wgrad operation and some fusions with them.

Showcases a simple fprop, fprop with pointwise fusion of scale bias and relu, fprop with bias and relu for channels first layout and fusions before convolution in the form of scale bias relu conv and stats. Also epilogue fusion of concatenate.

Showcases fp8 convolution with scaling and amax reduction.

Showcases Int8 convolution.

Has samples for simple dgrad, fusion for dgrad + drelu and Dgrad + Drelu + DBNweight fused operation.

Similar to dgrad was simple wgrad and scale+bias+relu+wgrad fused operation.

Measures convolution graph API runtimes across shapes. This long-running benchmark is included in the samples executable and runs in an unfiltered invocation on supported setups; select tests explicitly to avoid running it unintentionally.

Matmul fusion examples

Matmul showcases different matmul samples.

Has samples for simple Matmul, matmul fusions like matmul+abs, matmul+bias and matmul+scale+bias+relu operation.

Showcases fp8 matmul with scaling and amax reduction.

Showcases Int8 matmul.

Mixed precision multiplication between int8 and bf16 data-type with int8 operand being upcasted to bf16

Complex-valued FP32 matmul. Needs cuDNN 9.14.0 or newer.

Matmul with block-scaled operands. Needs cuDNN 9.11.0 or newer.

Hardware-accelerated NVFP4 and MXFP8 block-scaled matmul, with quantize and SwiGLU epilogues and dynamic shape overrides. Needs Blackwell or newer; the quantize epilogue needs compute capability 10.x.

Normalization examples

Norm showcases different normalization samples.

Eg for layernorm training, inference and back propagation

Eg for adaptive layernorm training, inference and back propagation

Eg for rmsnorm training, inference and back propagation. Needs Ampere or newer.

Shows different fusions in batch norm fprop and bprop. And split batch norm fusions.

Showcases normalization with block scale quantize epilogue fusion.

Showcases layer normalization with zero centered gamma usage.

Showcases layer normalization and relu with bitmask.

Custom reduction examples

samples/cpp/membound shows how to use the custom reduction (cuDNN 9.29 or later, data center Blackwell).
  • Custom reduction: a sum with a helper, mixed precisions, several streams, a body with a constant, and plan serialization.
  • argmax and argmin rules: reduction_modifiers::argmax and argmin with each NaN rule, checked against a host reference.
  • LLM token selection: greedy decoding, speculative-decoding verify, masked argmax and Gumbel-max sampling.
  • XLA HLO examples: the argmax that XLA emits, at all 31 distinct argmax operand shapes of an XLA HLO corpus, with the XLA and the torch NaN rule. The shapes and their source models are listed in docs/operations/CustomReduction.md.

Miscellaneous examples

Misc Miscellaneous samples

pointwise fusions with scalar are shown in this sample.

resample fprop operation with different resampling modes.

How to serialize a graph into a file and read it back on another thread/process.

How to choose the best performing plan among multiple plans suggested by the heuristics.

Shows how to use the native cuda graph API. The samples show how to create cudnn's cuda graph, and how to repeatedly update it with new device buffers for multiple execution.

Showcases a Batch norm example, where only a partial number of SMs participate in executing the kernel.

Showcases how to do deviceless ahead-of-time compilation with the device property descriptor (instead of a cuDNN handle).

Enumerates the engines and their knobs and builds a plan from an explicit engine configuration instead of the heuristics.

Builds a graph's plans from several threads. Long-running, so it is off by default.

A slice of an input tensor feeding a matmul.

Matmul on tensors placed in one locality domain of the GPU. Needs CUDA 13.4 or newer and a driver that provides locality domains.

Scalars baked into the graph at compile time. Needs cuDNN 9.22.0 or newer and a data-center Blackwell GPU.

Reproducer for the CUDA graph topology and lifecycle questions in #186. The multi-device cases need two visible GPUs.

Membound examples

Membound shows the TensorIR memory-bound engine. These samples need a data-center Blackwell GPU and cuDNN 9.22.0 or newer.

Single data-movement operations.

Data movement fused with pointwise operations (reshape then ReLU, transpose then bias add).

Comparison and logical operations fused into one kernel.

Causal conv1d examples

Causal conv1d shows the causal conv1d operations. They are not supported on Windows (MSVC).

Forward and backward in the default and NWH layouts. The NWH sample needs cuDNN 9.24.0 or newer.

Two causal conv1d operations fused back to back, forward and backward. Needs cuDNN 9.24.0 or newer.

FFT-based causal conv1d for medium and long sequences, forward and backward. Needs cuDNN 9.26.0 or newer.

MoE grouped matmul examples

Weight-only-quantized grouped matmul over experts, and the BF16 backward. Needs cuDNN 9.18.0 or newer (9.22.0 for the backward).

[Deprecated] C++ v0.x Interface Samples

Samples leveraging FE's C++ 0.x interface are located in samples/legacy_samples.