# xarray-sql - Automatic FlashAttention kernel deduction from SQL

Original title: Show HN: Automatic deduction of FlashAttention kernel from ~25 lines of SQL

> Hacker News story 50309479 · language: en · submitted 2026-10-30
> Web version: https://zeli.app/story/50309479
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 27 · by [alxmrs](<https://news.ycombinator.com/user?id=alxmrs>) · 💬 1
[original](<https://github.com/xqlsystems/xarray-sql/blob/6debe2495dcb4e8fac20ab97df5b89baae2a31a6/benchmarks/ml/01_attention.py>) · [HN discussion](<https://news.ycombinator.com/item?id=50309479>)

## Summary

xarray-sql lets you express causal attention as plain SQL, and its optimizer einfold automatically rewrites the query into a fused FlashAttention-style kernel—no attention-specific rules required. In the demo, ~25 lines of SQL define attention in two different spellings, run across DataFusion, einfold, and DuckDB, and are verified against JAX. A single SQL statement also computes gradients for Q, K, and V, checked by jax.grad. The same fusion rule even accelerates a non-attention query, the Nadaraya–Watson estimator, proving the optimization is general rather than hand-tuned.

## Key quote

> einfold finds that plan from this SQL, in both spellings, with no rule written for attention: its rule fuses any softmax-weighted average whose scores are computed from a join.

---

## Cite

- Page: https://zeli.app/story/50309479
- Date: 2026-10-30
- Source: github.com (https://github.com/xqlsystems/xarray-sql/blob/6debe2495dcb4e8fac20ab97df5b89baae2a31a6/benchmarks/ml/01_attention.py)
- Hacker News: https://news.ycombinator.com/item?id=50309479

## Other languages

- zh: https://zeli.app/zh/story/50309479 · markdown: https://zeli.app/zh/story/50309479.md
- ja: https://zeli.app/ja/story/50309479 · markdown: https://zeli.app/ja/story/50309479.md
- ko: https://zeli.app/ko/story/50309479 · markdown: https://zeli.app/ko/story/50309479.md
- de: https://zeli.app/de/story/50309479 · markdown: https://zeli.app/de/story/50309479.md
- es: https://zeli.app/es/story/50309479 · markdown: https://zeli.app/es/story/50309479.md
- ru: https://zeli.app/ru/story/50309479 · markdown: https://zeli.app/ru/story/50309479.md
