• bitcoinBitcoin(BTC)$77,107.00-1.92%
  • ethereumEthereum(ETH)$2,406.29-2.47%
  • tetherTether(USDT)$1.00-0.02%
  • binancecoinBNB(BNB)$679.29-1.64%
  • rippleXRP(XRP)$1.34-2.78%
  • usd-coinUSDC(USDC)$1.00-0.01%
  • solanaSolana(SOL)$99.46-3.54%
  • tronTRON(TRX)$0.322563-3.02%
  • Figure HelocFigure Heloc(FIGR_HELOC)$1.01-3.08%
  • HyperliquidHyperliquid(HYPE)$82.15-2.93%
  • zcashZcash(ZEC)$825.60-2.63%
  • dogecoinDogecoin(DOGE)$0.081276-2.03%
  • RainRain(RAIN)$0.016575-0.99%
  • USDSUSDS(USDS)$1.00-0.01%
  • moneroMonero(XMR)$493.99-5.64%
  • leo-tokenLEO Token(LEO)$9.38-2.91%
  • whitebitWhiteBIT Coin(WBT)$70.96-2.07%
  • chainlinkChainlink(LINK)$11.16-1.45%
  • cardanoCardano(ADA)$0.194702-2.06%
  • stellarStellar(XLM)$0.174657-1.58%
  • bitcoin-cashBitcoin Cash(BCH)$244.46-0.97%
  • daiDai(DAI)$1.00-0.03%
  • CantonCanton(CC)$0.113654-6.74%
  • Ethena USDeEthena USDe(USDE)$1.00-0.03%
  • USD1USD1(USD1)$1.00-0.02%
  • litecoinLitecoin(LTC)$49.562.06%
  • the-open-networkGram (prev. Toncoin)(GRAM)$1.31-6.65%
  • uniswapUniswap(UNI)$5.7710.35%
  • Global DollarGlobal Dollar(USDG)$1.00-0.02%
  • hedera-hashgraphHedera(HBAR)$0.0737670.19%
  • avalanche-2Avalanche(AVAX)$7.18-0.63%
  • shiba-inuShiba Inu(SHIB)$0.0000050.79%
  • suiSui(SUI)$0.72-1.31%
  • BlackRock USD Institutional Digital Liquidity FundBlackRock USD Institutional Digital Liquidity Fund(BUIDL)$1.000.00%
  • paypal-usdPayPal USD(PYUSD)$1.00-0.04%
  • Circle USYCCircle USYC(USYC)$1.140.01%
  • crypto-com-chainCronos(CRO)$0.054695-3.58%
  • tether-goldTether Gold(XAUT)$4,330.52-2.43%
  • nearNEAR Protocol(NEAR)$1.88-0.12%
  • MemeCoreMemeCore(M)$1.06-3.46%
  • Ripple USDRipple USD(RLUSD)$1.00-0.01%
  • okbOKB(OKB)$110.08-2.16%
  • Ondo US Dollar YieldOndo US Dollar Yield(USDY)$1.14-0.08%
  • BittensorBittensor(TAO)$219.63-4.40%
  • aaveAave(AAVE)$126.061.49%
  • pax-goldPAX Gold(PAXG)$4,337.76-2.42%
  • AsterAster(ASTER)$0.69-1.64%
  • World Liberty FinancialWorld Liberty Financial(WLFI)$0.056642-1.60%
  • MorphoMorpho(MORPHO)$2.583.08%
  • mantleMantle(MNT)$0.53-4.91%
TradePoint.io
  • Main
  • AI & Technology
  • Stock Charts
  • Market & News
  • Business
  • Finance Tips
  • Trade Tube
  • Blog
  • Shop
No Result
View All Result
TradePoint.io
No Result
View All Result

A Coding Guide to NVIDIA’s Tile-Based GPU Programming: From cuTile and Triton Kernels to Flash Attention

July 12, 2026
in AI & Technology
Reading Time: 4 mins read
A A
A Coding Guide to NVIDIA’s Tile-Based GPU Programming: From cuTile and Triton Kernels to Flash Attention
ShareShareShareShareShare

YOU MAY ALSO LIKE

Anthropic Releases Claude Fable 5.1 and Claude Mythos 5.1: 52.6% on Terminal-Bench-Science and 75% Cheaper Cache Reads

Frontier models can recover up to 65% of facts they can’t directly recall — just by thinking longer

if BACKEND == "triton":
   @triton.jit
   def _vadd_kernel(a_ptr, b_ptr, c_ptr, n, BLOCK: tl.constexpr):
       pid  = tl.program_id(0)
       offs = pid * BLOCK + tl.arange(0, BLOCK)
       mask = offs < n
       a = tl.load(a_ptr + offs, mask=mask)
       b = tl.load(b_ptr + offs, mask=mask)
       tl.store(c_ptr + offs, a + b, mask=mask)
   @triton.jit
   def _fused_gelu_kernel(x_ptr, w_ptr, b_ptr, o_ptr, n, BLOCK: tl.constexpr):
       pid  = tl.program_id(0)
       offs = pid * BLOCK + tl.arange(0, BLOCK)
       mask = offs < n
       x = tl.load(x_ptr + offs, mask=mask)
       w = tl.load(w_ptr + offs, mask=mask)
       b = tl.load(b_ptr + offs, mask=mask)
       h = x * w + b
       c = 0.7978845608028654
       z = c * (h + 0.044715 * h * h * h)
       e = tl.exp(-2.0 * z)
       tanh = (1.0 - e) / (1.0 + e)
       g = 0.5 * h * (1.0 + tanh)
       tl.store(o_ptr + offs, g, mask=mask)
   @triton.jit
   def _softmax_kernel(x_ptr, o_ptr, stride, n_cols, BLOCK: tl.constexpr):
       row  = tl.program_id(0)
       cols = tl.arange(0, BLOCK)
       mask = cols < n_cols
       ptr  = x_ptr + row * stride + cols
       x    = tl.load(ptr, mask=mask, other=-float("inf"))
       x    = x - tl.max(x, axis=0)
       num  = tl.exp(x)
       den  = tl.sum(num, axis=0)
       tl.store(o_ptr + row * stride + cols, num / den, mask=mask)
   @triton.jit
   def _matmul_kernel(A, B, C, M, N, K,
                      sam, sak, sbk, sbn, scm, scn,
                      BM: tl.constexpr, BN: tl.constexpr, BK: tl.constexpr):
       pid_m = tl.program_id(0)
       pid_n = tl.program_id(1)
       offs_m = pid_m * BM + tl.arange(0, BM)
       offs_n = pid_n * BN + tl.arange(0, BN)
       offs_k = tl.arange(0, BK)
       a_ptr = A + offs_m[:, None] * sam + offs_k[None, :] * sak
       b_ptr = B + offs_k[:, None] * sbk + offs_n[None, :] * sbn
       acc = tl.zeros((BM, BN), dtype=tl.float32)
       for k in range(0, K, BK):
           a = tl.load(a_ptr, mask=offs_k[None, :] < K - k, other=0.0)
           b = tl.load(b_ptr, mask=offs_k[:, None] < K - k, other=0.0)
           acc += tl.dot(a, b)
           a_ptr += BK * sak
           b_ptr += BK * sbk
       c_ptr = C + offs_m[:, None] * scm + offs_n[None, :] * scn
       cmask = (offs_m[:, None] < M) & (offs_n[None, :] < N)
       tl.store(c_ptr, acc.to(C.dtype.element_ty), mask=cmask)
   @triton.jit
   def _flash_kernel(Q, K, V, O, sqz, skz, svz, soz,
                     L, D, scale,
                     BL: tl.constexpr, BD: tl.constexpr):
       pid_l = tl.program_id(0)
       z     = tl.program_id(1)
       offs_l = pid_l * BL + tl.arange(0, BL)
       offs_d = tl.arange(0, BD)
       q_ptr = Q + z * sqz + offs_l[:, None] * D + offs_d[None, :]
       q = tl.load(q_ptr, mask=offs_l[:, None] < L, other=0.0)
       m_i = tl.full((BL,), -float("inf"), dtype=tl.float32)
       l_i = tl.zeros((BL,), dtype=tl.float32)
       acc = tl.zeros((BL, BD), dtype=tl.float32)
       for start in range(0, L, BL):
           offs_k = start + tl.arange(0, BL)
           k_ptr = K + z * skz + offs_k[:, None] * D + offs_d[None, :]
           v_ptr = V + z * svz + offs_k[:, None] * D + offs_d[None, :]
           k = tl.load(k_ptr, mask=offs_k[:, None] < L, other=0.0)
           v = tl.load(v_ptr, mask=offs_k[:, None] < L, other=0.0)
           s = tl.dot(q, tl.trans(k)) * scale
           s = tl.where(offs_k[None, :] < L, s, -float("inf"))
           m_ij = tl.maximum(m_i, tl.max(s, axis=1))
           p    = tl.exp(s - m_ij[:, None])
           alpha = tl.exp(m_i - m_ij)
           l_i  = l_i * alpha + tl.sum(p, axis=1)
           acc  = acc * alpha[:, None] + tl.dot(p.to(v.dtype), v)
           m_i  = m_ij
       acc = acc / l_i[:, None]
       o_ptr = O + z * soz + offs_l[:, None] * D + offs_d[None, :]
       tl.store(o_ptr, acc.to(O.dtype.element_ty), mask=offs_l[:, None] < L)
   def run_vadd(a, b):
       c = torch.empty_like(a); n = a.numel()
       grid = (triton.cdiv(n, 1024),)
       _vadd_kernel[grid](a, b, c, n, BLOCK=1024)
       return c
   def run_fused_gelu(x, w, b):
       o = torch.empty_like(x); n = x.numel()
       grid = (triton.cdiv(n, 1024),)
       _fused_gelu_kernel[grid](x, w, b, o, n, BLOCK=1024)
       return o
   def run_softmax(x):
       m, ncols = x.shape
       o = torch.empty_like(x)
       BLOCK = triton.next_power_of_2(ncols)
       _softmax_kernel[(m,)](x, o, x.stride(0), ncols, BLOCK=BLOCK)
       return o
   def run_matmul(a, b):
       M, K = a.shape; K2, N = b.shape
       c = torch.empty((M, N), device=a.device, dtype=a.dtype)
       BM = BN = 64; BK = 32
       grid = (triton.cdiv(M, BM), triton.cdiv(N, BN))
       _matmul_kernel[grid](a, b, c, M, N, K,
                            a.stride(0), a.stride(1), b.stride(0), b.stride(1),
                            c.stride(0), c.stride(1), BM=BM, BN=BN, BK=BK)
       return c
   def run_flash(q, k, v):
       Z, L, D = q.shape
       o = torch.empty_like(q)
       scale = 1.0 / math.sqrt(D)
       BL = 64
       grid = (triton.cdiv(L, BL), Z)
       _flash_kernel[grid](q, k, v, o,
                           q.stride(0), k.stride(0), v.stride(0), o.stride(0),
                           L, D, scale, BL=BL, BD=D)
       return o
else:
   def run_vadd(a, b):            return a + b
   def run_fused_gelu(x, w, b):   return torch.nn.functional.gelu(x * w + b, approximate="tanh")
   def run_softmax(x):            return torch.softmax(x, dim=-1)
   def run_matmul(a, b):          return a @ b
   def run_flash(q, k, v):        return torch.nn.functional.scaled_dot_product_attention(q, k, v)

Credit: Source link

ShareTweetSendSharePin

Related Posts

Anthropic Releases Claude Fable 5.1 and Claude Mythos 5.1: 52.6% on Terminal-Bench-Science and 75% Cheaper Cache Reads
AI & Technology

Anthropic Releases Claude Fable 5.1 and Claude Mythos 5.1: 52.6% on Terminal-Bench-Science and 75% Cheaper Cache Reads

September 1, 2026
Frontier models can recover up to 65% of facts they can’t directly recall — just by thinking longer
AI & Technology

Frontier models can recover up to 65% of facts they can’t directly recall — just by thinking longer

September 1, 2026
The New Street Fighter Movie Trailer Looks Fun In All The Right Ways
AI & Technology

The New Street Fighter Movie Trailer Looks Fun In All The Right Ways

September 1, 2026
Anthropic Announces Enterprise Frontier Safeguards, Customer-Held Data – Unite.AI
AI & Technology

Anthropic Announces Enterprise Frontier Safeguards, Customer-Held Data – Unite.AI

September 1, 2026
Next Post
Illinois representative talks bill that would regulate AI companies

Illinois representative talks bill that would regulate AI companies

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search

No Result
View All Result
Strait of Hormuz could reopen ‘today or tomorrow,’ says U.S. Treasury Secy. Bessent

Strait of Hormuz could reopen ‘today or tomorrow,’ says U.S. Treasury Secy. Bessent

August 29, 2026
What’s Actually Worth Buying Over Labor Day – and What to Skip

What’s Actually Worth Buying Over Labor Day – and What to Skip

August 31, 2026
Senate Republicans vote to hold Fauci in contempt of Congress

Senate Republicans vote to hold Fauci in contempt of Congress

August 28, 2026

About

Learn more

Our Services

Legal

Privacy Policy

Terms of Use

Bloggers

Learn more

Article Links

Contact

Advertise

Ask us anything

©2020- TradePoint.io - All rights reserved!

Tradepoint.io, being just a publishing and technology platform, is not a registered broker-dealer or investment adviser. So we do not provide investment advice. Rather, brokerage services are provided to clients of Tradepoint.io by independent SEC-registered broker-dealers and members of FINRA/SIPC. Every form of investing carries some risk and past performance is not a guarantee of future results. “Tradepoint.io“, “Instant Investing” and “My Trading Tools” are registered trademarks of Apperbuild, LLC.

This website is operated by Apperbuild, LLC. We have no link to any brokerage firm and we do not provide investment advice. Every information and resource we provide is solely for the education of our readers. © 2020 Apperbuild, LLC. All rights reserved.

No Result
View All Result
  • Main
  • AI & Technology
  • Stock Charts
  • Market & News
  • Business
  • Finance Tips
  • Trade Tube
  • Blog
  • Shop

© 2023 - TradePoint.io - All Rights Reserved!