Skip to content

0.5.1: conversion routes say what happens; the observer belongs to its context - #14

Merged
trissim merged 1 commit into
mainfrom
refactor/conversion-routes-say-what-happens
Oct 12, 2026
Merged

trissim merged 1 commit into
mainfrom
refactor/conversion-routes-say-what-happens

Conversation

@trissim

@trissim trissim commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Three items reported by the OpenHCS consumer, for the still-unpublished 0.5.1 (details under "Conversion" in CHANGELOG.md).

  • ConversionObserver.current is a ContextVar: each thread and asyncio task has its own observer; a thread sees the spawner's only when run in a copied context. Tested with two threads interleaving enter/exit.
  • ConversionRoute.planned and the run share one rule for "nothing is converted": the route StaysInPlace ("stays in place"), which writes no record. planned(lib, dev, lib, dev) is no longer DeviceMove.
  • A host exchange that shares the buffer is its own route, SharedHostBuffer ("shared host buffer"), declared by the library and checked per array by data pointer. Measured on host: NumPy<->torch shares (a negative-stride view copies); JAX->NumPy shares; NumPy->JAX shares only for 64-byte-aligned buffers; TensorFlow copies both ways. When this array could not be shared the record says "host copy" with declined_mechanisms=("shared host buffer",).

Mechanism names, in trial order: stays in place, device move, dlpack, shared host buffer, host copy. No name removed.

Local: 374 passed on Python 3.10 and 3.13 (NumPy only); 462 passed, 48 skipped on 3.12 with torch, JAX and TensorFlow CPU wheels; mypy src/ stubs/, ruff, black clean. Not run on a GPU.

🤖 Generated with Claude Code

StaysInPlace ("stays in place") is the one statement of "same library, same
device: nothing is converted". planned returns it, the run goes through it,
and move_to_device, to_numpy and from_numpy ask it instead of restating it.
DeviceMove applies only when the device differs.

SharedHostBuffer ("shared host buffer") names a host exchange in which the
result is the source's own buffer. Libraries declare it by inheriting
NumpyBufferSharing (NumPy, torch, JAX; TensorFlow copies both ways, measured).
Whether one array was shared is read after the exchange from where the two
arrays begin; a copy is recorded as "host copy" with the declined route named.
to_numpy and from_numpy are conversions through ConversionRoute.run, so the
record is written in one place.

ConversionObserver.current is a ContextVar read through the class: each
thread and asyncio task has its own observer, and a thread run in a copied
context sees the spawner's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 12, 2026 20:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@trissim
trissim merged commit 6f69ea4 into main Oct 12, 2026
12 of 14 checks passed
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.

2 participants