-
Notifications
You must be signed in to change notification settings - Fork 15
[Type Tensor 1: Add qd.Backend IntEnum #521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
hughperkins
wants to merge
3
commits into
main
Choose a base branch
from
hp/tensor-stork-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+138
−0
Draft
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| % Note for contributors: this page grows incrementally. Each PR in the | ||
| % flexible-tensors series (`hp/tensor-stork-N`) adds the section that | ||
| % documents whatever new user-visible behaviour landed in that PR. | ||
| % Sections describe only currently-shipped functionality. | ||
|
|
||
| # Flexible tensors | ||
|
|
||
| Quadrants offers two underlying tensor implementations, [`qd.field`](#fields) | ||
| and [`qd.ndarray`](#ndarrays). They have different runtime/compile-time | ||
| trade-offs, and different physical memory layouts can suit different kernels. | ||
|
|
||
| The flexible-tensors API lets you pick both the **backend** and (in a future | ||
| release) the **physical layout** on a per-tensor basis at allocation time. | ||
| The rest of the system (kernels, fastcache, autograd) stays out of the way. | ||
|
|
||
| This page documents the user-facing API as it lands. See | ||
| [`tensor_types`](tensor_types.md), [`scalar_tensors`](scalar_tensors.md), | ||
| and [`matrix_vector`](matrix_vector.md) for the underlying tensor primitives. | ||
|
|
||
| ## Choosing a backend: `qd.Backend` | ||
|
|
||
| `qd.Backend` is an `IntEnum` with two members: | ||
|
|
||
| | Member | Underlying type | When to prefer | | ||
| |---|---|---| | ||
| | `qd.Backend.FIELD` | `qd.field` | Faster at runtime; recompiles when any dimension size changes. Best for tensors whose shape is effectively static across a run. | | ||
| | `qd.Backend.NDARRAY` | `qd.ndarray` | Slightly slower at runtime but avoids recompilation when sizes change. Best for tensors whose shape varies frequently (dynamic batch sizes, growing buffers). | | ||
|
|
||
| ```python | ||
| import quadrants as qd | ||
|
|
||
| qd.Backend.FIELD # IntEnum member, value 0 | ||
| qd.Backend.NDARRAY # IntEnum member, value 1 | ||
|
|
||
| int(qd.Backend.FIELD) # 0 | ||
| qd.Backend["FIELD"] # qd.Backend.FIELD | ||
| qd.Backend(1) # qd.Backend.NDARRAY | ||
| ``` | ||
|
|
||
| The choice is per tensor: a single program can freely mix backends. | ||
|
|
||
| Subsequent releases will use this enum to drive the `qd.tensor(...)` factory | ||
| and per-tensor layout selection. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| """Flexible tensors: per-tensor backend and (later) layout. | ||
|
|
||
| This module is the user-facing entry point for selecting a tensor backend | ||
| (``qd.field`` vs ``qd.ndarray``) on a per-tensor basis. Currently it exports | ||
| only the :class:`Backend` enum; the ``qd.tensor(...)`` factory and | ||
| ``layout=`` support land in subsequent PRs. | ||
|
|
||
| See ``docs/source/user_guide/flexible_tensors.md`` for the user guide. | ||
| """ | ||
|
|
||
| from enum import IntEnum | ||
|
|
||
| __all__ = ["Backend"] | ||
|
|
||
|
|
||
| class Backend(IntEnum): | ||
| """Tensor storage backend. | ||
|
|
||
| Each value selects one of Quadrants' two underlying tensor implementations: | ||
|
|
||
| - :attr:`FIELD` (``qd.field``): faster at runtime; recompiles kernels | ||
| whenever any dimension size changes. Best for tensors whose shape is | ||
| effectively static across a run. | ||
| - :attr:`NDARRAY` (``qd.ndarray``): slightly slower at runtime but avoids | ||
| kernel recompilation when sizes change. Best for tensors whose shape | ||
| varies frequently (e.g. dynamic batch sizes, growing buffers). | ||
|
|
||
| The choice is made per tensor at allocation time. A single program can | ||
| freely mix both backends. | ||
| """ | ||
|
|
||
| FIELD = 0 | ||
| NDARRAY = 1 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| """Tests for ``qd.Backend`` (PR 1 of the flexible-tensors plan). | ||
|
|
||
| Scope: this PR only ships the enum. No factory, no layout, no kernel | ||
| integration. Tests cover symbol export, value, name, ordering, and IntEnum | ||
| semantics so that downstream PRs can rely on stable behaviour. | ||
| """ | ||
|
|
||
| from enum import IntEnum | ||
|
|
||
| import pytest | ||
|
|
||
| import quadrants as qd | ||
|
|
||
|
|
||
| def test_backend_is_exported(): | ||
| assert hasattr(qd, "Backend") | ||
|
|
||
|
|
||
| def test_backend_is_intenum(): | ||
| assert issubclass(qd.Backend, IntEnum) | ||
|
|
||
|
|
||
| def test_backend_values(): | ||
| assert int(qd.Backend.FIELD) == 0 | ||
| assert int(qd.Backend.NDARRAY) == 1 | ||
|
|
||
|
|
||
| def test_backend_names(): | ||
| assert qd.Backend.FIELD.name == "FIELD" | ||
| assert qd.Backend.NDARRAY.name == "NDARRAY" | ||
|
|
||
|
|
||
| def test_backend_lookup_by_name(): | ||
| assert qd.Backend["FIELD"] is qd.Backend.FIELD | ||
| assert qd.Backend["NDARRAY"] is qd.Backend.NDARRAY | ||
|
|
||
|
|
||
| def test_backend_lookup_by_value(): | ||
| assert qd.Backend(0) is qd.Backend.FIELD | ||
| assert qd.Backend(1) is qd.Backend.NDARRAY | ||
|
|
||
|
|
||
| def test_backend_int_compare(): | ||
| assert qd.Backend.FIELD == 0 | ||
| assert qd.Backend.NDARRAY == 1 | ||
| assert qd.Backend.FIELD < qd.Backend.NDARRAY | ||
|
|
||
|
|
||
| def test_backend_members_are_distinct(): | ||
| members = list(qd.Backend) | ||
| assert len(members) == 2 | ||
| assert qd.Backend.FIELD in members | ||
| assert qd.Backend.NDARRAY in members | ||
|
|
||
|
|
||
| def test_backend_invalid_value_raises(): | ||
| with pytest.raises(ValueError): | ||
| qd.Backend(2) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Flexible tensors" => "Tensor"