• bitcoinBitcoin(BTC)$65,393.00-0.15%
  • ethereumEthereum(ETH)$1,891.56-1.41%
  • tetherTether(USDT)$1.00-0.01%
  • binancecoinBNB(BNB)$568.18-0.29%
  • usd-coinUSDC(USDC)$1.000.00%
  • rippleXRP(XRP)$1.11-1.90%
  • solanaSolana(SOL)$75.94-1.70%
  • tronTRON(TRX)$0.3313270.87%
  • Figure HelocFigure Heloc(FIGR_HELOC)$1.043.72%
  • HyperliquidHyperliquid(HYPE)$58.860.13%
  • dogecoinDogecoin(DOGE)$0.069928-3.24%
  • USDSUSDS(USDS)$1.000.00%
  • RainRain(RAIN)$0.013932-2.35%
  • leo-tokenLEO Token(LEO)$9.59-1.14%
  • zcashZcash(ZEC)$510.81-0.96%
  • moneroMonero(XMR)$360.672.81%
  • whitebitWhiteBIT Coin(WBT)$56.96-0.47%
  • chainlinkChainlink(LINK)$8.54-0.15%
  • stellarStellar(XLM)$0.184062-0.03%
  • cardanoCardano(ADA)$0.167740-3.14%
  • CantonCanton(CC)$0.1215440.85%
  • daiDai(DAI)$1.000.00%
  • bitcoin-cashBitcoin Cash(BCH)$212.12-2.07%
  • USD1USD1(USD1)$1.000.00%
  • the-open-networkGram (prev. Toncoin)(GRAM)$1.47-3.09%
  • Ethena USDeEthena USDe(USDE)$1.000.01%
  • litecoinLitecoin(LTC)$46.76-0.54%
  • Global DollarGlobal Dollar(USDG)$1.00-0.04%
  • hedera-hashgraphHedera(HBAR)$0.070859-3.98%
  • suiSui(SUI)$0.74-3.66%
  • Circle USYCCircle USYC(USYC)$1.130.00%
  • avalanche-2Avalanche(AVAX)$6.27-4.54%
  • crypto-com-chainCronos(CRO)$0.057200-0.74%
  • paypal-usdPayPal USD(PYUSD)$1.000.00%
  • BlackRock USD Institutional Digital Liquidity FundBlackRock USD Institutional Digital Liquidity Fund(BUIDL)$1.000.00%
  • tether-goldTether Gold(XAUT)$4,042.75-1.12%
  • nearNEAR Protocol(NEAR)$1.901.66%
  • shiba-inuShiba Inu(SHIB)$0.000004-1.34%
  • uniswapUniswap(UNI)$3.850.80%
  • Ondo US Dollar YieldOndo US Dollar Yield(USDY)$1.13-0.43%
  • OndoOndo(ONDO)$0.398574-1.89%
  • BittensorBittensor(TAO)$193.81-1.31%
  • pax-goldPAX Gold(PAXG)$4,037.92-1.15%
  • World Liberty FinancialWorld Liberty Financial(WLFI)$0.056540-9.01%
  • okbOKB(OKB)$82.250.10%
  • AsterAster(ASTER)$0.630.40%
  • HTX DAOHTX DAO(HTX)$0.000002-0.11%
  • Ripple USDRipple USD(RLUSD)$1.000.01%
  • usddUSDD(USDD)$1.00-0.01%
  • MemeCoreMemeCore(M)$1.161.15%
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

YOU MAY ALSO LIKE

Tesla Misses Profit Estimates, Leans Into AI

Palm Beach Rejects Data Center Near Mar-a-Lago

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

ShareTweetSendSharePin

Related Posts

Tesla Misses Profit Estimates, Leans Into AI
AI & Technology

Tesla Misses Profit Estimates, Leans Into AI

July 24, 2026
Palm Beach Rejects Data Center Near Mar-a-Lago
AI & Technology

Palm Beach Rejects Data Center Near Mar-a-Lago

July 24, 2026
Apple Prepping iMac, MacBooks Overhaul to Meet AI Demand
AI & Technology

Apple Prepping iMac, MacBooks Overhaul to Meet AI Demand

July 24, 2026
How to Build an End-to-End OCR Pipeline with Baidu’s Unlimited-OCR for High-Resolution Images and Multi-Page PDF Parsing
AI & Technology

How to Build an End-to-End OCR Pipeline with Baidu’s Unlimited-OCR for High-Resolution Images and Multi-Page PDF Parsing

July 24, 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
Morning News NOW Full Episode – July 20

Morning News NOW Full Episode – July 20

July 22, 2026
Paddleboarders rescue raccoon from Texas lake

Paddleboarders rescue raccoon from Texas lake

July 23, 2026
Air quality hazards continue as Canadian wildfire smoke blankets parts of U.S.

Air quality hazards continue as Canadian wildfire smoke blankets parts of U.S.

July 23, 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!