• bitcoinBitcoin(BTC)$60,682.001.32%
  • ethereumEthereum(ETH)$1,702.131.25%
  • tetherTether(USDT)$1.000.00%
  • binancecoinBNB(BNB)$555.300.69%
  • usd-coinUSDC(USDC)$1.000.00%
  • rippleXRP(XRP)$0.951.87%
  • solanaSolana(SOL)$71.341.62%
  • Figure HelocFigure Heloc(FIGR_HELOC)$1.062.07%
  • tronTRON(TRX)$0.3038450.20%
  • HyperliquidHyperliquid(HYPE)$63.293.31%
  • zcashZcash(ZEC)$719.15-4.53%
  • dogecoinDogecoin(DOGE)$0.0569902.46%
  • moneroMonero(XMR)$448.38-13.59%
  • leo-tokenLEO Token(LEO)$9.091.26%
  • whitebitWhiteBIT Coin(WBT)$61.305.52%
  • chainlinkChainlink(LINK)$9.272.17%
  • USDSUSDS(USDS)$1.00-0.01%
  • cardanoCardano(ADA)$0.1507903.00%
  • Ethena USDeEthena USDe(USDE)$1.00-0.06%
  • stellarStellar(XLM)$0.1275050.00%
  • daiDai(DAI)$1.000.02%
  • bitcoin-cashBitcoin Cash(BCH)$208.972.73%
  • nearNEAR Protocol(NEAR)$3.083.13%
  • litecoinLitecoin(LTC)$43.753.05%
  • CantonCanton(CC)$0.076827-0.64%
  • avalanche-2Avalanche(AVAX)$6.650.69%
  • tether-goldTether Gold(XAUT)$3,564.540.04%
  • uniswapUniswap(UNI)$4.70-0.25%
  • USD1USD1(USD1)$1.00-0.04%
  • Ripple USDRipple USD(RLUSD)$1.000.01%
  • suiSui(SUI)$0.671.91%
  • the-open-networkGram (prev. Toncoin)(GRAM)$0.961.14%
  • Global DollarGlobal Dollar(USDG)$1.00-0.01%
  • hedera-hashgraphHedera(HBAR)$0.0551386.69%
  • Ondo US Dollar YieldOndo US Dollar Yield(USDY)$1.15-0.05%
  • paypal-usdPayPal USD(PYUSD)$1.000.00%
  • BittensorBittensor(TAO)$184.871.44%
  • quant-networkQuant(QNT)$148.982.33%
  • shiba-inuShiba Inu(SHIB)$0.0000042.17%
  • Circle USYCCircle USYC(USYC)$1.150.00%
  • Spiko Amundi Overnight Swap Fund (EUR)Spiko Amundi Overnight Swap Fund (EUR)(EURSAFO)$1.12-0.07%
  • crypto-com-chainCronos(CRO)$0.0397890.09%
  • MemeCoreMemeCore(M)$0.835.53%
  • RainRain(RAIN)$0.002539-7.53%
  • okbOKB(OKB)$82.931.73%
  • Falcon USDFalcon USD(USDF)$1.000.18%
  • United StablesUnited Stables(U)$1.00-0.02%
  • pax-goldPAX Gold(PAXG)$3,562.07-0.07%
  • BlackRock USD Institutional Digital Liquidity FundBlackRock USD Institutional Digital Liquidity Fund(BUIDL)$1.000.00%
  • BitwayBitway(BTW)$0.9910.67%
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

How to Build Memory-Efficient Transformers with xFormers Using Packed Sequences, GQA, ALiBi, SwiGLU, and Causal Attention

June 17, 2026
in AI & Technology
Reading Time: 2 mins read
A A
How to Build Memory-Efficient Transformers with xFormers Using Packed Sequences, GQA, ALiBi, SwiGLU, and Causal Attention
ShareShareShareShareShare
print("\n" + "="*70 + "\n4. Variable-length packed batch — no padding waste\n" + "="*70)
seqlens = [37, 120, 8, 200]
total = sum(seqlens)
H, K = 8, 64
q = torch.randn(1, total, H, K, device=device, dtype=torch.float16)
k = torch.randn(1, total, H, K, device=device, dtype=torch.float16)
v = torch.randn(1, total, H, K, device=device, dtype=torch.float16)
try:
   bias = ab.BlockDiagonalMask.from_seqlens(seqlens)
   out_packed = xops.memory_efficient_attention(q, k, v, attn_bias=bias)
   s0 = seqlens[0]
   ref0 = vanilla_attention(q[:, :s0], k[:, :s0], v[:, :s0]).half()
   print("packed shape         :", tuple(out_packed.shape), "(all", total, "tokens, no pad)")
   print("segment-0 max diff   : {:.2e}".format((out_packed[:, :s0] - ref0).abs().max().item()))
   cbias = ab.BlockDiagonalCausalMask.from_seqlens(seqlens)
   _ = xops.memory_efficient_attention(q, k, v, attn_bias=cbias)
   print("-> also did a packed CAUSAL pass. This is how vLLM-style engines")
   print("   batch requests of different lengths with zero padding overhead.")
   splits = bias.split(out_packed)
   print("recovered segments   :", [tuple(t.shape) for t in splits])
except Exception as e:
   print("BlockDiagonalMask path skipped on this version/backend:", repr(e))
print("\n" + "="*70 + "\n5. Grouped-query attention (5-D BMGHK layout)\n" + "="*70)
B, M, K = 2, 256, 64
n_q_heads, n_kv_heads = 8, 2
G, Hq = n_kv_heads, n_q_heads // n_kv_heads
try:
   qg = torch.randn(B, M, G, Hq, K, device=device, dtype=torch.float16)
   kg = torch.randn(B, M, G, 1,  K, device=device, dtype=torch.float16)
   vg = torch.randn(B, M, G, 1,  K, device=device, dtype=torch.float16)
   out_gqa = xops.memory_efficient_attention(qg, kg, vg)
   print("GQA output shape     :", tuple(out_gqa.shape), "= [B, M, G, Hq, K]")
   print(f"-> {n_q_heads} query heads, only {n_kv_heads} KV heads: smaller KV-cache,")
   print("   which is exactly what Llama-/Mistral-class models use at inference.")
except Exception as e:
   print("GQA 5-D path skipped on this version/backend:", repr(e))

Credit: Source link

YOU MAY ALSO LIKE

1Password Says It Was Hacked But Users’ Vaults Are Safe

Canada Invests $700 Million More to Expand AI Compute Access Fund – Unite.AI

ShareTweetSendSharePin

Related Posts

1Password Says It Was Hacked But Users’ Vaults Are Safe
AI & Technology

1Password Says It Was Hacked But Users’ Vaults Are Safe

November 26, 2026
Canada Invests 0 Million More to Expand AI Compute Access Fund – Unite.AI
AI & Technology

Canada Invests $700 Million More to Expand AI Compute Access Fund – Unite.AI

November 26, 2026
Workers At GTA 6 Studio Rockstar Games Vote Overwhelmingly Against Union Recognition
AI & Technology

Workers At GTA 6 Studio Rockstar Games Vote Overwhelmingly Against Union Recognition

November 26, 2026
AI Could Soon Edit Live Videos in Real Time – Unite.AI
AI & Technology

AI Could Soon Edit Live Videos in Real Time – Unite.AI

November 26, 2026
Next Post
Invesco Short Duration High Yield Municipal Fund Q1 2026 Commentary (Mutual Fund:ISHAX)

Invesco Short Duration High Yield Municipal Fund Q1 2026 Commentary (Mutual Fund:ISHAX)

Leave a Reply Cancel reply

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

Search

No Result
View All Result
Apple: The Hidden Risk Behind My Downgrade (NASDAQ:AAPL)

Apple: The Hidden Risk Behind My Downgrade (NASDAQ:AAPL)

November 21, 2026
Canada Invests 0 Million More to Expand AI Compute Access Fund – Unite.AI

Canada Invests $700 Million More to Expand AI Compute Access Fund – Unite.AI

November 26, 2026
My Daughter Has Spending Episodes

My Daughter Has Spending Episodes

November 26, 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!