Skip to content

Standardize docs across nx, exla, torchx#1770

Open
Chapaman wants to merge 5 commits into
elixir-nx:mainfrom
Chapaman:backend-docs-standard
Open

Standardize docs across nx, exla, torchx#1770
Chapaman wants to merge 5 commits into
elixir-nx:mainfrom
Chapaman:backend-docs-standard

Conversation

@Chapaman

Copy link
Copy Markdown
Contributor

Closes #1759

  • Add Nx.Docs to define the convention for backend-specific documentation modules (Kernel.SpecialForms-style ghost modules that mirror the Nx API).
  • Add EXLA.Docs.Nx, EXLA.Docs.Nx.LinAlg, Torchx.Docs.Nx, and Torchx.Docs.Nx.LinAlg documenting how each backend lowers or executes block-backed and backend-specific operations.
  • Document all current Nx.block/4 entry points: top-level Nx (take, FFT, cumulative ops, transfers, runtime_call, etc.) and Nx.LinAlg (cholesky, QR, SVD, …).
  • Add ## Backend notes sections on 29 Nx / Nx.LinAlg functions linking to the EXLA and Torchx docs modules.
  • Wire discoverability through Nx, Nx.LinAlg, Nx.Backend, Nx.block/4, EXLA, and Torchx moduledocs, plus Hex “Backend documentation” groups in each package.

@josevalim

Copy link
Copy Markdown
Contributor

Apologies but I am not a fan of this approach. We are introducing a bunch of modules, which have a runtime cost, to document APIs that do not really exist and people should not invoke. Otherwise this will appear as suggestions on LSP, ExDoc autocompletes, etc.

We should figure out how to structure this using regular pages/guides as much as possible.

@polvalente

Copy link
Copy Markdown
Contributor

Apologies but I am not a fan of this approach. We are introducing a bunch of modules, which have a runtime cost, to document APIs that do not really exist and people should not invoke. Otherwise this will appear as suggestions on LSP, ExDoc autocompletes, etc.

We should figure out how to structure this using regular pages/guides as much as possible.

I think we can keep the same general idea where we have a standardized nested structure that corresponds to the Nx modules (e.g. EXLA > Backend Documentation > Nx > LinAlg) which is written as Markdown, Livemd or cheatsheets, as relevant. The most important part of this PR for me is having an easy way to look things up structurally.

I suspect livebooks will provide the closest experience to regular modules, and cheatsheets can be complimentary documentation to that. We might also want some sort of CI check to ensure the documentation gets updated if we add or remove functions to the original Nx modules, but we can leave this as a follow-up.

@josevalim

Copy link
Copy Markdown
Contributor

I think you can pass files to doctest to make sure their content is correct. But that means using .cheatmd or .md files. Any should be fine!

@Chapaman Chapaman force-pushed the backend-docs-standard branch from 38b13b5 to 05d66a1 Compare July 2, 2026 01:23

These guides describe how EXLA lowers and executes Nx operations. They mirror
the structure of the Nx API (see the [backend documentation convention](https://hexdocs.pm/nx/backend_documentation-convention.html))
but are **not callable** — use `Nx` and `compiler: EXLA` in your code.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong now, as it is only markdown files.

Comment thread exla/guides/backend_documentation/nx.md Outdated
Comment on lines +22 to +36
## take/3

Tensor indexing (`%Nx.Block.Take{}`).

### Lowering

Lowered to StableHLO `gather` in `EXLA.Defn` (not a custom call).

### Options

* `:axis` — honoured; indices are gathered along this axis

### Platforms

All EXLA clients.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: what documenting this particular behaviour is giving us? What it becomes in StableHLO is not necessarily relevant. If it is, we will probably benefit more from having a debug_stablehlo option that prints it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do have this in EXLA.to_mlir_module

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now to think out loud about lowering patterns, this function is implemented as an Nx.Block, so. users might want to override the imolementation.

Though at that point they might as well look at the source code.

Not to decide anything, but maybe. what we want here is to scope documenting divergent behavior and backend specific options only?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Divergent behavior or specific trade-offs. Yes. Most of what EXLA does is already abstracted several layers away, saying we use gather won’t add much. But saying function X does not support A option or has a special flag can help. The question is what users of this library may want to know. If you are implementing custom blocks, then the source is already the best source of truth anyway.

Comment thread exla/mix.exs Outdated
],
"guides/backend_documentation/nx_lin_alg.md": [
filename: "backend_documentation-nx_lin_alg",
title: "Nx.LinAlg (EXLA)"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already the title, do we need to give it again? Same above.

@Chapaman Chapaman marked this pull request as ready for review July 6, 2026 20:34

@polvalente polvalente left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this looks great, I just think we need to clean up some of the things I pointed out

Comment thread exla/guides/backend_documentation/index.md Outdated
Comment thread exla/guides/backend_documentation/nx.md Outdated
Comment thread exla/guides/backend_documentation/nx_lin_alg.md Outdated
Comment thread nx/lib/nx/lin_alg.ex Outdated
Comment on lines +139 to +142

## Backend notes

See [Torchx](https://hexdocs.pm/torchx/backend_documentation-nx_lin_alg.html#cholesky-1).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should reference backends from Nx itself

Comment thread nx/lib/nx.ex Outdated
Comment thread torchx/guides/backend_documentation/index.md Outdated
Comment thread torchx/guides/backend_documentation/nx.md Outdated
Comment thread torchx/guides/backend_documentation/nx_lin_alg.md Outdated
Chapaman and others added 2 commits July 7, 2026 09:02
Co-authored-by: Paulo Valente <16843419+polvalente@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduce documentation standard for backends

3 participants