• bitcoinBitcoin(BTC)$81,808.00-1.79%
  • ethereumEthereum(ETH)$2,420.76-3.45%
  • tetherTether(USDT)$1.00-0.03%
  • binancecoinBNB(BNB)$728.35-1.78%
  • rippleXRP(XRP)$1.33-2.66%
  • usd-coinUSDC(USDC)$1.00-0.01%
  • solanaSolana(SOL)$104.35-2.39%
  • tronTRON(TRX)$0.3327431.42%
  • Figure HelocFigure Heloc(FIGR_HELOC)$1.040.79%
  • zcashZcash(ZEC)$1,137.86-6.68%
  • HyperliquidHyperliquid(HYPE)$80.87-0.85%
  • dogecoinDogecoin(DOGE)$0.081695-3.35%
  • moneroMonero(XMR)$552.242.14%
  • USDSUSDS(USDS)$1.00-0.03%
  • chainlinkChainlink(LINK)$12.42-3.79%
  • whitebitWhiteBIT Coin(WBT)$80.04-2.16%
  • cardanoCardano(ADA)$0.228477-3.48%
  • leo-tokenLEO Token(LEO)$9.011.10%
  • RainRain(RAIN)$0.009139-8.66%
  • stellarStellar(XLM)$0.183472-4.17%
  • nearNEAR Protocol(NEAR)$4.81-5.07%
  • bitcoin-cashBitcoin Cash(BCH)$268.88-1.76%
  • Ethena USDeEthena USDe(USDE)$1.00-0.02%
  • litecoinLitecoin(LTC)$59.48-3.39%
  • CantonCanton(CC)$0.115891-9.62%
  • daiDai(DAI)$1.000.03%
  • uniswapUniswap(UNI)$7.11-6.06%
  • avalanche-2Avalanche(AVAX)$9.95-3.20%
  • suiSui(SUI)$1.02-2.84%
  • USD1USD1(USD1)$1.00-0.01%
  • the-open-networkGram (prev. Toncoin)(GRAM)$1.36-1.39%
  • hedera-hashgraphHedera(HBAR)$0.084499-3.61%
  • BitwayBitway(BTW)$2.2816.15%
  • tether-goldTether Gold(XAUT)$4,101.05-1.41%
  • Global DollarGlobal Dollar(USDG)$1.000.01%
  • BittensorBittensor(TAO)$269.52-3.84%
  • quant-networkQuant(QNT)$206.80-3.53%
  • shiba-inuShiba Inu(SHIB)$0.000005-2.85%
  • paypal-usdPayPal USD(PYUSD)$1.00-0.02%
  • crypto-com-chainCronos(CRO)$0.057964-2.14%
  • EthenaEthena(ENA)$0.195972-7.39%
  • okbOKB(OKB)$122.680.75%
  • Ripple USDRipple USD(RLUSD)$1.00-0.01%
  • Circle USYCCircle USYC(USYC)$1.140.01%
  • aaveAave(AAVE)$153.83-4.01%
  • Ondo US Dollar YieldOndo US Dollar Yield(USDY)$1.150.01%
  • Pump.funPump.fun(PUMP)$0.004762-5.44%
  • BlackRock USD Institutional Digital Liquidity FundBlackRock USD Institutional Digital Liquidity Fund(BUIDL)$1.000.00%
  • OndoOndo(ONDO)$0.445016-4.59%
  • polkadotPolkadot(DOT)$1.14-2.81%
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

The Differences Beyond A Screen

Anthropic Strengthens Election Safeguards Ahead of 2026 US Midterms – Unite.AI

ShareTweetSendSharePin

Related Posts

The Differences Beyond A Screen
AI & Technology

The Differences Beyond A Screen

October 14, 2026
Anthropic Strengthens Election Safeguards Ahead of 2026 US Midterms – Unite.AI
AI & Technology

Anthropic Strengthens Election Safeguards Ahead of 2026 US Midterms – Unite.AI

October 14, 2026
Deezer Will Let Listeners Completely Filter Out AI Music
AI & Technology

Deezer Will Let Listeners Completely Filter Out AI Music

October 14, 2026
YouTube Music Labels Songs Made Primarily with Generative AI – Unite.AI
AI & Technology

YouTube Music Labels Songs Made Primarily with Generative AI – Unite.AI

October 14, 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
Shocking incident on United Airlines flight

Shocking incident on United Airlines flight

October 9, 2026
F-16 jet crash triggers evacuations

F-16 jet crash triggers evacuations

October 12, 2026
Other IPOs on ice as investors await Anthropic’s public debut

Other IPOs on ice as investors await Anthropic’s public debut

October 9, 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!