Close Menu
NCIJ Network NCIJ Network
    What's Hot

    China-Nexus JadeProx Uses New TriBack Loader in Government and Healthcare Attacks

    July 25, 2026

    World Foundation Raises $52.5M to Scale Sam Altman’s ‘Proof of Human’ ID

    July 25, 2026

    Ukraine’s Zelensky struggles to fix crisis over removal of defence minister Fedorov

    July 25, 2026
    Facebook X (Twitter) Instagram
    Trending
    • China-Nexus JadeProx Uses New TriBack Loader in Government and Healthcare Attacks
    • World Foundation Raises $52.5M to Scale Sam Altman’s ‘Proof of Human’ ID
    • Ukraine’s Zelensky struggles to fix crisis over removal of defence minister Fedorov
    • Warner Bros. lawsuit accuses Amazon of illegally poaching executives
    • Rockwell Patches Code Execution Flaws in Arena Simulation Software
    • Democratizing weather derivatives through tokenization could be blockchain industry’s most important real-world use case
    • Head of Hamas-led police in northern Gaza killed by Israeli strike | Hamas News
    • Police investigate payments to Reform UK first revealed by the Guardian | Nigel Farage
    • About
      • Our Team
      • Editorial Policy
      • Editorial Independence
      • International Support
    • Trust & Standards
      • AI Usage Policy
      • Conflict of Interest Policy
      • Corrections Policy
      • Ethics Policy
      • Fact-Checking Policy
      • Source Protection
    • Get Involved
      • Guide for Sources
      • Support Independent Journalism
    • Legal
      • Cookie Policy
      • Privacy Policy
      • Terms of Use
    Facebook X (Twitter) Instagram
    NCIJ Network NCIJ Network
    Saturday, July 25
    • Home
    • World
    • Ai
    • Business
    • Politics
    • Health
    • Crypto
    • Science
    • Technology
    • Cybersecurity
    • Defense & Security
    • Economy
    • Energy
    • Europe
    • More
      • Fact Check
      • Investigations
      • Opinion & Analysis
      • Environment
    NCIJ Network NCIJ Network
    Home»Artificial Intelligence

    Designing High-Performance GPU Kernels with TileLang: Tensor-Core GEMM, Fused Softmax, FlashAttention, and Autotuning

    NCIJ NETWNCIJ NETWORKBy NCIJ NETWNCIJ NETWORKJuly 25, 2026 Artificial Intelligence No Comments3 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    @tilelang.jit(out_idx=[-1])
    def make_matmul(M: int, N: int, K: int,
                   block_M: int = 128, block_N: int = 128, block_K: int = 32,
                   num_stages: int = 3, threads: int = 128,
                   use_swizzle: bool = False,
                   dtype: str = "float16", accum_dtype: str = "float"):
       @T.prim_func
       def main(A: T.Tensor((M, K), dtype),
                B: T.Tensor((K, N), dtype),
                C: T.Tensor((M, N), dtype)):
           with T.Kernel(T.ceildiv(N, block_N),
                         T.ceildiv(M, block_M),
                         threads=threads) as (bx, by):
               A_shared = T.alloc_shared((block_M, block_K), dtype)
               B_shared = T.alloc_shared((block_K, block_N), dtype)
               C_local = T.alloc_fragment((block_M, block_N), accum_dtype)
               if use_swizzle:
                   T.use_swizzle(panel_size=10, enable=True)
               T.clear(C_local)
               for ko in T.Pipelined(T.ceildiv(K, block_K), num_stages=num_stages):
                   T.copy(A[by * block_M, ko * block_K], A_shared)
                   T.copy(B[ko * block_K, bx * block_N], B_shared)
                   T.gemm(A_shared, B_shared, C_local)
               T.copy(C_local, C[by * block_M, bx * block_N])
       return main
    def smem_bytes(block_M, block_N, block_K, stages, itemsize=2):
       return (block_M * block_K + block_K * block_N) * itemsize * stages
    def section_2():
       banner("2. MEMORY HIERARCHY — tiled tensor-core GEMM")
       M = N = K = 2048
       bm, bn, bk, st = 128, 128, 32, DEFAULT_STAGES
       while smem_bytes(bm, bn, bk, st) > SMEM_CAP and st > 1:
           st -= 1
       print(f"   config: {bm}x{bn}x{bk}, num_stages={st}, "
             f"smem={smem_bytes(bm,bn,bk,st)/1024:.0f} KB")
       kernel = make_matmul(M, N, K, bm, bn, bk, num_stages=st, threads=128)
       a = torch.randn(M, K, device=DEV, dtype=torch.float16)
       b = torch.randn(K, N, device=DEV, dtype=torch.float16)
       c = kernel(a, b)
       check(c, a @ b, "matmul 2048^3")
       flops = 2 * M * N * K
       ms = bench(lambda: kernel(a, b))
       ms_ref = bench(lambda: a @ b)
       print(f"   tilelang : {ms:7.3f} ms  ->  {flops/(ms*1e-3)/1e12:6.2f} TFLOP/s")
       print(f"   cuBLAS   : {ms_ref:7.3f} ms  ->  {flops/(ms_ref*1e-3)/1e12:6.2f} TFLOP/s")
       print(f"   ratio    : {ms_ref/ms*100:5.1f}% of cuBLAS from ~20 lines of Python")
       src = kernel.get_kernel_source()
       for needle in ("mma.sync", "wgmma", "ldmatrix", "cp.async", "tl::gemm"):
           if needle in src:
               print(f"   emitted: {needle}")
       return kernel
    def section_3():
       banner("3. KNOBS — sweeping the schedule by hand")
       M = N = K = 2048
       a = torch.randn(M, K, device=DEV, dtype=torch.float16)
       b = torch.randn(K, N, device=DEV, dtype=torch.float16)
       flops = 2 * M * N * K
       candidates = [
           (64,  64,  32, 2, 128, False),
           (128, 128, 32, 2, 128, False),
           (128, 128, 32, 2, 128, True),
           (128, 128, 32, 3, 128, False),
           (128, 128, 64, 2, 256, False),
           (128, 256, 32, 2, 256, False),
       ]
       print(f"   {'tile':>16} {'stg':>4} {'thr':>4} {'swz':>4} {'smem':>7} "
             f"{'ms':>8} {'TFLOP/s':>9}")
       results = []
       for bm, bn, bk, st, thr, swz in candidates:
           need = smem_bytes(bm, bn, bk, st)
           tag = f"{bm}x{bn}x{bk}"
           if need > SMEM_CAP:
               print(f"   {tag:>16} {st:>4} {thr:>4} {str(swz):>4} {need//1024:>5}KB "
                     f"  SKIPPED (over smem budget)")
               continue
           try:
               k = make_matmul(M, N, K, bm, bn, bk, num_stages=st,
                               threads=thr, use_swizzle=swz)
               c = k(a, b)
               ok = (c - (a @ b)).float().norm() / (a @ b).float().norm() < 2e-2
               ms = bench(lambda: k(a, b), warmup=5, rep=20)
               results.append((ms, tag, st, thr, swz))
               print(f"   {tag:>16} {st:>4} {thr:>4} {str(swz):>4} {need//1024:>5}KB "
                     f"{ms:>8.3f} {flops/(ms*1e-3)/1e12:>9.2f}"
                     f"{'' if ok else '   <- NUMERICALLY WRONG'}")
           except Exception as e:
               print(f"   {tag:>16} {st:>4} {thr:>4} {str(swz):>4}     -  "
                     f"failed: {type(e).__name__}")
       if results:
           best = min(results)
           print(f"n   winner: {best[1]}, stages={best[2]}, threads={best[3]}, "
                 f"swizzle={best[4]}  ({best[0]:.3f} ms)")
       print("   Takeaway: the best schedule is arch- and shape-dependent, which is")
       print("   exactly why section 7 exists.")
    
    Autotuning Designing FlashAttention Fused GEMM GPU HighPerformance Kernels Softmax TensorCore TileLang
    NCIJ NETWNCIJ NETWORK
    • Website

    Keep Reading

    Meet Open Dreamer: A JAX/Flax Reproduction of the Dreamer 4 World Model Pipeline, With the Full Training Recipe Published

    Building Self-Evolving AI Agents with OpenSpace Using Skills, MCP, Lineage, and Low-Cost Reuse

    Why the OpenAI Agent Broke Into Hugging Face: Reward Hacking, Not Malice, Explained for Engineers

    Datalab Marker v2 vs MinerU, Docling, and Liteparse: Benchmark Breakdown

    Meet the New Claude Opus 5: Frontier-Class Agentic Coding and Computer Use at Unchanged Opus Pricing

    Meta, Microsoft, Nvidia, IBM, and others back open-weight AI

    Add A Comment
    Leave A Reply Cancel Reply

    Editors Picks

    China-Nexus JadeProx Uses New TriBack Loader in Government and Healthcare Attacks

    July 25, 2026

    World Foundation Raises $52.5M to Scale Sam Altman’s ‘Proof of Human’ ID

    July 25, 2026

    Ukraine’s Zelensky struggles to fix crisis over removal of defence minister Fedorov

    July 25, 2026

    Warner Bros. lawsuit accuses Amazon of illegally poaching executives

    July 25, 2026
    Latest Posts

    Trump slaps 50% tariffs on Canada and Carney vows to ‘intensify’ trade talks

    July 21, 2026

    How Two Brothers Dug for Dead Relatives: With a Shovel and a Kitchen Knife

    July 21, 2026

    Chile floods: Towns evacuated following heavy rain in Coquimbo

    July 21, 2026

    Subscribe to News

    Get the latest sports news from NewsSite about world, sports and politics.

    NCIJ Network is an independent digital news platform delivering trusted investigative journalism, European and global news, in-depth analysis, and fact-based reporting with accuracy, transparency, and integrity.

    Facebook X (Twitter) Instagram Pinterest YouTube

    China-Nexus JadeProx Uses New TriBack Loader in Government and Healthcare Attacks

    July 25, 2026

    World Foundation Raises $52.5M to Scale Sam Altman’s ‘Proof of Human’ ID

    July 25, 2026

    Ukraine’s Zelensky struggles to fix crisis over removal of defence minister Fedorov

    July 25, 2026

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    Type above and press Enter to search. Press Esc to cancel.