Commit deb1275
committed
feat: lower the Nx.Block.LinAlg.* family in the native Expr compiler
Closes the LinAlg-block cluster on the Expr op-coverage checklist
(#188) — seven new lowerings: Cholesky, Solve, QR, Eigh, LU, SVD, and
Determinant.
Two new infrastructure pieces unlock multi-output and nested-block
lowering:
- replay_program in c_src/program.cpp grows a switch for the four
multi-output linalg opcodes (LinalgQR / LinalgEigh / LinalgLU /
LinalgSVD) that pushes N values per instruction, the same shape
`while` already uses. compile_program's per-instruction arity
calculation extends to match.
- The block-parameter collector goes "shallow": when it encounters a
nested `:block` Expr node it walks only the block's `in_args` (which
reference outer-scope tensors), not the inner `expr` whose fresh
inner block-params live in a different scope. Determinant's
expansion calls Nx.LinAlg.lu internally, so without this fix the
outer block's parameter seeding would leak into LU's inner
parameters and the binding would mis-resolve.
Single-output ops (LinalgCholesky, LinalgSolve) dispatch through the
standard `dispatch_op` path; multi-output ops use emit_multi/6 with
arity 2 (QR, Eigh) or 3 (LU, SVD), returning {:multi_refs, ...} so
the existing `:elem` projection works unchanged.
LU's s32 perm vector is post-processed into a permutation matrix via
`take(eye(n), perm, 0)`, mirroring Emily.Backend.native_lu/3. SVD in
`full_matrices?: false` mode is sliced from the full output to the
requested shape via the same `maybe_slice_svd/4` shape comparison as
Emily.Backend. Determinant has no fused kernel — both paths run the
block's composed 2x2 / 3x3 / NxN expansion via TopK-style parameter
seeding, bit-identical to the Evaluator. All seven dispatch
mx::linalg::* on the CPU stream per call (MLX's linalg is CPU-only),
matching c_src/ops/linalg.cpp's eager NIFs.
Tests in compiler_equivalence_test.exs cover each block on small
inputs (SPD for Cholesky, well-conditioned for Solve, mixed shapes
for QR/SVD, symmetric for Eigh, square for LU/Determinant). SVD's
sign convention is mathematically non-unique and MLX's S also drifts
by ~1 ULP across separate calls (the Backend property tests
acknowledge this at backend_test.exs:771), so SVD tests assert
shapes + singular values within tolerance instead of bit-exact;
every other LinAlg block matches the evaluator bit-for-bit.
Probe drops from 9 → 2 misses; only `count_leading_zeros` and
`population_count` remain (no MLX primitive — by design).1 parent 8c3b15f commit deb1275
5 files changed
Lines changed: 379 additions & 7 deletions
File tree
- c_src
- emily
- lib/emily
- test/emily
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
91 | 113 | | |
92 | 114 | | |
93 | 115 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
218 | 218 | | |
219 | 219 | | |
220 | 220 | | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
221 | 244 | | |
222 | 245 | | |
223 | | - | |
| 246 | + | |
224 | 247 | | |
225 | 248 | | |
226 | 249 | | |
| |||
801 | 824 | | |
802 | 825 | | |
803 | 826 | | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
804 | 848 | | |
805 | 849 | | |
806 | 850 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
242 | 242 | | |
243 | 243 | | |
244 | 244 | | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
245 | 290 | | |
246 | 291 | | |
247 | 292 | | |
| |||
318 | 363 | | |
319 | 364 | | |
320 | 365 | | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
321 | 370 | | |
322 | | - | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
323 | 375 | | |
324 | 376 | | |
325 | 377 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
196 | 196 | | |
197 | 197 | | |
198 | 198 | | |
199 | | - | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
200 | 212 | | |
201 | 213 | | |
202 | 214 | | |
| |||
1428 | 1440 | | |
1429 | 1441 | | |
1430 | 1442 | | |
| 1443 | + | |
| 1444 | + | |
| 1445 | + | |
| 1446 | + | |
| 1447 | + | |
| 1448 | + | |
| 1449 | + | |
| 1450 | + | |
| 1451 | + | |
| 1452 | + | |
| 1453 | + | |
| 1454 | + | |
| 1455 | + | |
| 1456 | + | |
| 1457 | + | |
| 1458 | + | |
| 1459 | + | |
| 1460 | + | |
| 1461 | + | |
| 1462 | + | |
| 1463 | + | |
| 1464 | + | |
| 1465 | + | |
| 1466 | + | |
| 1467 | + | |
| 1468 | + | |
| 1469 | + | |
| 1470 | + | |
| 1471 | + | |
| 1472 | + | |
| 1473 | + | |
| 1474 | + | |
| 1475 | + | |
| 1476 | + | |
| 1477 | + | |
| 1478 | + | |
| 1479 | + | |
| 1480 | + | |
| 1481 | + | |
| 1482 | + | |
| 1483 | + | |
| 1484 | + | |
| 1485 | + | |
| 1486 | + | |
| 1487 | + | |
| 1488 | + | |
| 1489 | + | |
| 1490 | + | |
| 1491 | + | |
| 1492 | + | |
| 1493 | + | |
| 1494 | + | |
| 1495 | + | |
| 1496 | + | |
| 1497 | + | |
| 1498 | + | |
| 1499 | + | |
| 1500 | + | |
| 1501 | + | |
| 1502 | + | |
| 1503 | + | |
| 1504 | + | |
| 1505 | + | |
| 1506 | + | |
| 1507 | + | |
| 1508 | + | |
| 1509 | + | |
| 1510 | + | |
| 1511 | + | |
| 1512 | + | |
| 1513 | + | |
| 1514 | + | |
| 1515 | + | |
| 1516 | + | |
| 1517 | + | |
| 1518 | + | |
| 1519 | + | |
| 1520 | + | |
| 1521 | + | |
| 1522 | + | |
| 1523 | + | |
| 1524 | + | |
| 1525 | + | |
| 1526 | + | |
| 1527 | + | |
| 1528 | + | |
| 1529 | + | |
| 1530 | + | |
| 1531 | + | |
| 1532 | + | |
| 1533 | + | |
| 1534 | + | |
| 1535 | + | |
| 1536 | + | |
| 1537 | + | |
| 1538 | + | |
| 1539 | + | |
| 1540 | + | |
| 1541 | + | |
| 1542 | + | |
| 1543 | + | |
| 1544 | + | |
| 1545 | + | |
| 1546 | + | |
| 1547 | + | |
| 1548 | + | |
| 1549 | + | |
| 1550 | + | |
| 1551 | + | |
| 1552 | + | |
| 1553 | + | |
| 1554 | + | |
| 1555 | + | |
1431 | 1556 | | |
1432 | 1557 | | |
1433 | 1558 | | |
| |||
1492 | 1617 | | |
1493 | 1618 | | |
1494 | 1619 | | |
1495 | | - | |
1496 | | - | |
1497 | | - | |
1498 | | - | |
| 1620 | + | |
| 1621 | + | |
| 1622 | + | |
| 1623 | + | |
| 1624 | + | |
| 1625 | + | |
| 1626 | + | |
1499 | 1627 | | |
1500 | 1628 | | |
1501 | 1629 | | |
| 1630 | + | |
| 1631 | + | |
| 1632 | + | |
| 1633 | + | |
| 1634 | + | |
| 1635 | + | |
1502 | 1636 | | |
1503 | 1637 | | |
1504 | 1638 | | |
| |||
1529 | 1663 | | |
1530 | 1664 | | |
1531 | 1665 | | |
| 1666 | + | |
| 1667 | + | |
| 1668 | + | |
| 1669 | + | |
| 1670 | + | |
| 1671 | + | |
| 1672 | + | |
| 1673 | + | |
| 1674 | + | |
| 1675 | + | |
| 1676 | + | |
| 1677 | + | |
| 1678 | + | |
| 1679 | + | |
| 1680 | + | |
| 1681 | + | |
| 1682 | + | |
1532 | 1683 | | |
1533 | 1684 | | |
1534 | 1685 | | |
| |||
0 commit comments