Triton Language and Compiler

repository·main·Indexed 12 days ago

https://github.com/triton-lang/triton

A language and compiler for writing highly efficient custom Deep-Learning primitives, designed to provide higher productivity than CUDA and more flexibility than other DSLs. It features a custom MLIR level (TTIR and TTGIR) with a plugin system for out-of-tree pass development and an experimental concurrency sanitizer, GSan, for detecting global memory data races.

Tokens
51.8K
Snippets
128
Records
234
Agent score
98%

What's inside Triton

  1. Overview of Gluon Core APIs

    main

    Gluon provides two primary layers of APIs for kernel development:

    1. Runtime API: Handles the execution lifecycle, including JIT decorators, result handling, and host-side tensor descriptors required to launch Gluon kernels.
    2. Language API: Defines the core programming model, including memory operations, math operations, layouts, and compile-time helpers.
  2. Overview of Triton Concurrency Sanitizer (ConSan)

    main

    ConSan is a tool that instruments Triton IR with runtime checks to detect illegal concurrent access to shared memory and tensor memory. It tracks read and write frontiers over buffer state lanes and models synchronization for various operations, including mbarrier synchronization and commit-count synchronization for asynchronous operations like cp.async, WGMMA, TMA stores, and AMD TDM copies.

    ConSan is target-hook based:

    • cuda:* uses NVIDIA hooks.
    • hip:* uses AMD hooks.

    It uses BufferRegion analysis to collect exact shared-memory and tensor-memory address sets and barrier allocations, creating auxiliary state in distributed tensors and shared-cluster global scratch memory.

  3. Use the NVIDIA Rubin Gluon language module

    main
    The triton.experimental.gluon.language.nvidia.rubin module provides low-level primitives and operations specifically designed for the NVIDIA Rubin architecture. This module includes support for asynchronous memory operations, cluster management, barriers, and specialized tensor core instructions (TMA, MMA, etc.).
  4. Explore the Triton Python API

    main

    Triton provides several Python modules for kernel development, testing, and semantics:

    • triton: The core entry point.
    • triton.language: The primary module for writing Triton kernels.
    • triton.testing: Utilities for testing Triton code.
    • triton.language.extra.cuda: CUDA-specific extensions for the language module.
    • Triton Semantics: Documentation regarding the formal behavior and semantics of the language.
  5. Use f2reduce for Gaussian elimination over GF(2)

    main

    f2reduce is a lightweight library for converting a binary matrix to row reduced echelon form (RREF) using Gaussian elimination over GF(2). It is optimized for autovectorization (GCC/LLVM) and high instruction-level parallelism, and it can utilize AVX512's vpternlogq instruction if available.

    To use the library, you can call the primary RREF function or use the utility function to determine an optimal stride for your matrix width.

    // Primary function to perform in-place RREF
    void inplace_rref_strided(uint64_t *matrix, uint64_t rows, uint64_t cols, uint64_t stride);
    
    // Utility to get a recommended stride based on column count
    uint64_t get_recommended_stride(uint64_t cols);
  6. Triton-shared: Lowering Triton IR to MLIR

    main

    Triton-shared is a project aimed at lowering Triton IR to core MLIR dialects (such as linalg and memref) to facilitate running Triton on CPUs and improving compiler modularity.

    Current Capabilities:

    • Modular Compiler Passes: Decoupled data extraction from lowering via paths like Triton-to-structured $\rightarrow$ triton-arith-to-linalg $\rightarrow$ Structured-to-memref.
    • Improved Pointer Analysis: Supports nested loops and non-contiguous memory access.
    • Unstructured Access: Supports lowering unstructured access using a single base pointer.
    • Op Support: Lowers Triton operations like split, join, and cat to MLIR/linalg.

    Roadmap Goals:

    • Complete support for non-contiguous pointers.
    • Detection of complex memory access patterns (e.g., row-gather/scatter sequences).
    • Extension to control flow operations.
  7. Use the Gluon Language API

    main
    The Gluon Language API, located in triton.experimental.gluon.language, provides a set of primitives for defining tensor operations, memory management, and programming models within the Triton ecosystem. It includes specialized operations for layout manipulation, shape transformations, and hardware-specific optimizations like atomic and linear algebra operations.
  8. Triton Compatibility and Supported Platforms

    main

    Triton is designed for high-performance GPU computing with the following support:

    Supported Platforms

    • Linux

    Supported Hardware

    • NVIDIA GPUs: Compute Capability 8.0+
    • AMD GPUs: ROCm 6.2+
    • CPUs: Under development
  9. Core Triton Python API modules

    main

    The triton Python package provides several key modules for writing and optimizing GPU kernels:

    • triton.jit: The primary decorator used to compile Python functions into Triton kernels.
    • triton.autotune: A module used to automatically find the best configuration (e.g., block sizes) for a kernel by running it with multiple parameter sets.
    • triton.heuristics: Provides mechanisms for making compile-time decisions based on input parameters.
    • triton.Config: A class used to define specific configurations (like BLOCK_SIZE) for use with the autotuner.
  10. Explore Proton documentation and guides

    main

    Proton provides deep profiling capabilities for Triton. Detailed documentation is organized into several specialized guides:

    • Python profiling API: Covers sessions, decorators, scopes, states, CPU timed scopes, and custom metrics.
    • Backends and modes: Explains backend selection (NVIDIA and AMD support), instruction sampling, periodic flushing, and instrumentation modes.
    • Periodic profiling: Details periodic_flushing, phase advancement, partial output files, and in-memory phase APIs.
    • Intra-kernel profiling: Covers Proton DSL instrumentation and TTGIR override workflows.
    • Command line and viewer: Instructions for using the proton and proton-viewer tools, including metrics, filters, sorted output, trace visualization, and diff profiles.
    • Advanced features: Information on CUDA graphs, knobs, thread safety, known issues, and third-party backend registration.
  11. Use triton.testing for performance benchmarking and verification

    main

    The triton.testing module provides utilities for benchmarking Triton kernels, comparing numerical results, and generating performance reports. It is primarily used to validate the correctness of kernels via assert_close and to measure execution performance using various benchmarking functions like do_bench and do_bench_proton.

    import triton.testing as tt
    
    # Example usage pattern for benchmarking
    # tt.do_bench(lambda: my_kernel(...))
    
    # Example usage pattern for numerical verification
    # tt.assert_close(actual, expected)
  12. What is Gluon and when should I use it?

    main

    Gluon is Triton's lower-level GPU programming model. It is designed for advanced kernel development where developers need to trade the convenience of high-level Triton abstractions for direct control over hardware-specific features.

    Use Gluon when you need to explicitly manage:

    • Layouts: Controlling how data is organized in memory.
    • Shared Memory: Manual management of on-chip memory.
    • Warp Specialization: Assigning specific tasks to different warps within a thread block.
    • Target-specific features: Leveraging unique hardware capabilities of specific GPU architectures.