Skip to content

Pytorch backend#5

Open
mt-caret wants to merge 4 commits into
mainfrom
pytorch-backend
Open

Pytorch backend#5
mt-caret wants to merge 4 commits into
mainfrom
pytorch-backend

Conversation

@mt-caret

Copy link
Copy Markdown
Owner

No description provided.

mt-caret and others added 4 commits June 28, 2026 04:38
Add a torch backend that interprets fox tensor programs on Torch.Tensor.t,
mirroring the eager Tensor backend op-for-op via Op.Make_operators, primarily
for differential testing.

- lib/torch (fox_torch): Pytorch.Backend implements Operators_intf.S over
  Torch.Tensor.t; Pytorch.handle interprets each Fox_effect.Op through libtorch
  analogous to Handler.eval. fox float64 <-> torch Double and fox Bool <-> torch
  Uint8 (comparisons yield Bool), converted via bigarrays.
- lib/core: expose Fox_effect and Operators_intf from the fox_core wrapper (needed
  to author an external backend + handler); add Tensor.Private.to_char_bigarray
  symmetric to the existing float/of_char converters.
- test: quickcheck eager-vs-torch over single-op programs (relative tolerance,
  since torch transcendentals differ from libm by ~ULPs whereas the eager
  allclose is absolute), plus a deterministic Bool round-trip test.

Builds CPU-only against a manually supplied libtorch 2.7.1 and a CPU patch to
the torch package; see lib/torch/README.md and PLAN.md for the rationale.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Guarantee matmul/transpose/sum (Just/negative dims, keep_dims) and a
sum+broadcast+div composition (mean) are exercised against the eager backend,
rather than relying on quickcheck's random op draws.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
torch was pinned to an ephemeral local directory, so the CPU patch wasn't
reproducible. Capture it instead as:

- opam-overlay/: a local opam-repository whose only package,
  torch.v0.18~preview.130.91+190, is the ox repo's definition plus
  `patches: ["torch-cpu.patch"]`. It fetches the upstream tarball by checksum
  and applies files/torch-cpu.patch (the CUDA-removal + link-propagation diff),
  so no patched sources are vendored.
- fox.opam.locked regenerated to pin every dependency to exact versions,
  now including torch and the ctypes 0.23.x it pulls in.

Reproduce with the overlay out-ranking ox + `opam install . --locked`
(see lib/torch/README.md). Verified by re-deriving the patched torch from
upstream + the committed patch, installing it from the overlay, and running the
full test suite green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
CI hard-depends on torch now, but the runner had neither libtorch nor the
opam-overlay, so torch failed to compile (missing headers) / would have used
the unpatched upstream sources. Mirror the local setup: download CPU libtorch
2.7.1 and export LIBTORCH, register opam-overlay/ at rank 1 so its patched torch
wins for the shared version, and key the switch cache on the overlay too.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.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.

1 participant