Skip to content

[Common] Add caller-owned workspaces with per-stream resource isolation - #3766

Open
Oleg-Goncharov wants to merge 20 commits into
NVIDIA:mainfrom
Oleg-Goncharov:pr_resource_workspace
Open

Oleg-Goncharov wants to merge 20 commits into
NVIDIA:mainfrom
Oleg-Goncharov:pr_resource_workspace

Conversation

@Oleg-Goncharov

@Oleg-Goncharov Oleg-Goncharov commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Replace global mutable TMA descriptor arrays in grouped MXFP8 quantization, activation forward/backward, and dequantization with caller-owned workspace. Use ordinary NVTETensor handles and the existing empty-tensor size-query protocol. Reconstruct the layout from the group count and rebuild descriptors and metadata on each invocation, without persistent host state or workspace ownership attached to tensors. Remove the fixed 64-group descriptor limit.

Callers provide separate mutable allocations for independently executing CUDA streams and CUDA Graphs, order reuse of a shared buffer, and keep captured allocations alive for all graph executions that reference them.

Compatibility change: the original workspace-less grouped C APIs now reject MXFP8 layouts with varying last dimensions. C/C++ callers must use the corresponding _v2 query/allocate/execute APIs for those layouts. Constant-last-dimension layouts retain support through the original entry points.

PyTorch grouped MXFP8 quantize/dequantize use the workspace-enabled APIs and framework allocations. The JAX grouped MXFP8 FFI keeps its existing call: its constant last dimension uses launch-local descriptors and does not require descriptor-array workspace.

Also fix PyTorch grouped DBias scratch lifetime and preserve supported partial-row MXFP8 dequantization. DBias retains its existing backend-specific scratch protocol.

CUDA resource lifecycle and reset changes belong to a follow-up PR #3779, which is currently based on this PR.

Workspace size queries perform the same host-side validation as execution, including non-MXFP8 forward formats and block-scaled dequantization, without launching GPU work or modifying outputs.

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

Area Change
Grouped TMA workspace Add a stateless layout for four tensor-map arrays and three metadata arrays, each starting on a 128-byte boundary. Query and execute through ordinary NVTETensor handles, including different handles for query and execution. Validate declared capacity and base alignment; centralize size-overflow checks in the layout calculation.
Grouped MXFP8 kernels Pass caller-owned storage to descriptor updaters and consumers for quantize, dequantize, and all five grouped activation forward/backward families. Rebuild descriptors and metadata per invocation; remove the fixed 64-group limit.
Workspace reuse and concurrency Require separate mutable storage for independently executing streams and graphs. Leave allocation ownership, reuse ordering, and graph lifetime with the caller/framework.
C API and compatibility Add workspace-aware _v2 grouped entry points. Original workspace-less APIs reject varying-last-dimension MXFP8 layouts; constant-last-dimension layouts remain supported. Document the migration.
PyTorch integration Use _v2 for grouped MXFP8 quantize/dequantize and allocate workspace through the framework, including graph-pool allocations during capture. Keep other grouped formats on their existing calls.
JAX compatibility Keep the existing grouped MXFP8 FFI call unchanged: its constant-last-dimension layout uses launch-local descriptors.
DBias workspace lifetime fix Keep the PyTorch grouped DBias scratch allocation alive through operation submission. Retain the existing DBias APIs and backend-specific scratch protocol.
Partial-row dequantization fix Remove the inappropriate 128-row alignment check from supported grouped MXFP8 dequantization paths, preserving cases such as M=96 and M=160.
Tests and documentation Cover fresh query/execution handles, capacity/alignment validation, more than 64 groups, numerical results, separate-buffer concurrency, and CUDA Graph replay. Document allocation ownership, reuse ordering, and captured-buffer lifetime.
Workspace query validation Run the same host-side checks for query and execution in non-MXFP8 forward formats and block-scaled dequantization. Reject unsupported operations, configurations, dtypes, and layouts before returning the size. Preserve the one-byte requirement for supported paths without descriptor storage.
Tests and documentation Cover fresh query/execution handles, capacity/alignment checks, more than 64 groups, numerical results, independent streams, and CUDA Graph replay. Add regressions for rejected queries and verify that valid queries leave buffers unchanged and capture zero graph nodes. Document query validation, ownership, reuse ordering, and graph lifetime.

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@timmoon10 timmoon10 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This workspace infrastructure may be overengineered: #3779 (comment)

@Oleg-Goncharov

Copy link
Copy Markdown
Collaborator Author

Thanks for the detailed review, @timmoon10! I have narrowed this PR to caller-owned TMA descriptor storage for grouped MXFP8 operations. Below are responses to your comments in #3779

  1. The scope is too large, and the code needs human review.
    The generic workspace catalog and framework workspace owner have been removed. This PR now covers grouped TMA storage and its callers.

  2. Is there value in forcing all workspaces to share the same infrastructure?
    Agreed. The generic infrastructure has been removed. GroupedTmaWorkspaceLayout only describes the descriptor and metadata arrays needed by grouped TMA kernels.

  3. Do the scattered size_t overflow checks accomplish much?
    The remaining checks are centralized in reserve(). I kept checks for multiplication, padding, and accumulated size because the allocation depends on the group count, which is no longer capped at 64. They prevent an overflowing size calculation from producing an undersized allocation.

  4. Why require power-of-two alignment?
    The configurable alignment interface and its power-of-two restriction have been removed. The remaining layout has one fixed alignment convention.

  5. Why disallow alignments smaller than 128 bytes, especially for metadata?
    The 128-byte convention is now local to the grouped TMA layout. It currently applies to both descriptor and metadata arrays for simplicity, but reducing their padding would be a possible memory-saving improvement.

  6. Why require workspace elements to be trivially copyable?
    That restriction was part of the generic infrastructure and has been removed.

  7. Why does the rowwise setter receive special treatment?
    The setter hooks have been removed. An ordinary NVTETensor handle carries the workspace pointer and byte capacity using its rowwise data field. This is the representation of the raw buffer, independent of whether the operation produces rowwise or columnwise output.

  8. Does shared_ptr<Workspace> make every Tensor more expensive and non-POD?
    The field has been removed. The common Tensor definition and setters now match main.

  9. Why does supposedly general infrastructure know about MXFP8?
    The helper is now explicitly specialized for grouped TMA storage. It is no longer presented as general quantization infrastructure. The diagnostic still mentions MXFP8 and gives quantize as an example.

  10. Could a Resource allocate the buffer internally and preserve the existing APIs?
    Internal allocation is a possible alternative, but it still needs rules for concurrent use and buffer lifetime. One shared mutable allocation would allow independently executing streams to overwrite each other's descriptors. A host mutex around submission would not protect overlapping GPU execution, and captured graphs also need their storage to remain valid.
    The current design keeps allocation with the framework. Independent streams and graphs receive separate buffers, and the caller orders reuse. There are no persistent activation tables, singleton workspace managers, or allocation generations in this PR anymore.

Comments 11-16 concern the resource lifecycle changes in the follow-up PR #3779 which is currently based on this PR. I’ll address those comments separately in that PR.

@Oleg-Goncharov
Oleg-Goncharov marked this pull request as ready for review November 3, 2026 15:39
@greptile-apps

greptile-apps Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge based on the code reviewed.

Summary

This PR moves mutable grouped MXFP8 descriptors into caller-owned workspace and updates PyTorch to allocate that storage.

  • Grouped MXFP8 calls use caller-owned workspace for varying-last-dimension groups.
  • Grouped workspace queries check the operation before returning a size.
  • PyTorch grouped MXFP8 calls allocate workspace through the framework.
  • PyTorch keeps grouped DBias scratch alive through operation submission.
  • Grouped MXFP8 dequantization accepts supported partial-row shapes.

Both earlier query-validation concerns are addressed. No new actionable issues were found. GPU tests were not run during this review.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Caller supplies empty workspace handle] --> B[Validate request and return size]
  B --> C[Caller allocates workspace]
  C --> D[Validate capacity and alignment]
  D --> E[Rebuild descriptors on the supplied stream]
  E --> F[Run grouped operation]
  C -. Separate storage for independent streams and graphs .-> C
Loading

Reviews (9) · Last reviewed commit: "Merge branch 'main' into pr_resource_wor..." · Reviewed by Greptile

Signed-off-by: Oleg Goncharov <ogoncharov@nvidia.com>
Signed-off-by: Oleg Goncharov <ogoncharov@nvidia.com>
Signed-off-by: Oleg Goncharov <ogoncharov@nvidia.com>
Signed-off-by: Oleg Goncharov <ogoncharov@nvidia.com>
Signed-off-by: Oleg Goncharov <ogoncharov@nvidia.com>
Signed-off-by: Oleg Goncharov <ogoncharov@nvidia.com>
Signed-off-by: Oleg Goncharov <ogoncharov@nvidia.com>
Signed-off-by: Oleg Goncharov <ogoncharov@nvidia.com>
Signed-off-by: Oleg Goncharov <ogoncharov@nvidia.com>
…st thread.

Signed-off-by: Oleg Goncharov <ogoncharov@nvidia.com>
Signed-off-by: Oleg Goncharov <ogoncharov@nvidia.com>
Signed-off-by: Oleg Goncharov <ogoncharov@nvidia.com>
Signed-off-by: Oleg Goncharov <ogoncharov@nvidia.com>
Signed-off-by: Oleg Goncharov <ogoncharov@nvidia.com>
Signed-off-by: Oleg Goncharov <ogoncharov@nvidia.com>
Signed-off-by: Oleg Goncharov <ogoncharov@nvidia.com>
Pass workspace TMA storage to the grouped MXFP8 requantization updater and
consumer introduced on main. Add a v2 query/allocate/execute entry point
and keep constant-last-dimension calls working through the original API.

Cover query validation, independent streams and graphs, and the grouped
requantization numerical matrix without restoring global descriptors or
the 64-group limit.

Signed-off-by: Oleg Goncharov <ogoncharov@nvidia.com>
Signed-off-by: Oleg Goncharov <ogoncharov@nvidia.com>
@Oleg-Goncharov

Copy link
Copy Markdown
Collaborator Author

/te-ci

Comment on lines +113 to +114
* Query and execution reconstruct the same TMA-specific layout from the group count.
* They may use different tensor handles; no persistent host metadata is attached to either.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The workspace should be considered opaque and the caller shouldn't need to know that it's for TMA descriptors. What we should focus on is the contract we make with the caller. I suppose we could mention that the workspace size does not depend on the data pointers, so you can reuse the same workspace for different tensors with the same shape... but that's also pretty obvious and bringing it up is mostly confusing.

Suggested change
* Query and execution reconstruct the same TMA-specific layout from the group count.
* They may use different tensor handles; no persistent host metadata is attached to either.

* They may use different tensor handles; no persistent host metadata is attached to either.
* The workspace is a one-dimensional byte tensor and requires at least one byte even when
* the selected kernel does not need temporary storage.
* Execution validates the tensor's declared byte capacity and base alignment.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is basic error checking. No point bringing it up except for Codex to show off that it thought of an edge case.

Suggested change
* Execution validates the tensor's declared byte capacity and base alignment.

* The workspace is a one-dimensional byte tensor and requires at least one byte even when
* the selected kernel does not need temporary storage.
* Execution validates the tensor's declared byte capacity and base alignment.
* The caller is responsible for the actual allocation behind the supplied pointer.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is true for all NVTETensors. No point bringing it up here.

Suggested change
* The caller is responsible for the actual allocation behind the supplied pointer.

Comment on lines +122 to +127
* Contents are rebuilt on every invocation; no initialization is required. Keep the memory
* alive until submitted work completes, and for the lifetime of captured graphs that use it.
* Each workspace is used by one CUDA stream. The caller supplies separate allocations
* for independently executing streams and serializes complete host submissions using the same buffer.
* Independent graphs require separate mutable storage, even if captured on the same stream.
* Query and allocate the workspace before CUDA graph capture.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How is this different from basic CUDA stream semantics and CUDA Graph usage?

Suggested change
* Contents are rebuilt on every invocation; no initialization is required. Keep the memory
* alive until submitted work completes, and for the lifetime of captured graphs that use it.
* Each workspace is used by one CUDA stream. The caller supplies separate allocations
* for independently executing streams and serializes complete host submissions using the same buffer.
* Independent graphs require separate mutable storage, even if captured on the same stream.
* Query and allocate the workspace before CUDA graph capture.

*
* MXFP8 groups with varying last dimensions use the workspace for TMA descriptors and
* metadata, without a fixed group-count limit. Its data pointer must be 128-byte aligned.
* Contents are rebuilt on every invocation; no initialization is required. Keep the memory

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The caller doesn't need to know how the workspace is used internally.

Comment thread docs/api/c/cast.rst

@timmoon10 timmoon10 Nov 18, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These docs have a lot of of internal implementation details, irrelevant info, and excessive details.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These are tests written for the sake of writing tests. GroupedTmaWorkspaceLayout is an internal helper class that very tightly integrated with the implementation of the grouped MXFP8 kernels. It is not written generally and has little abstraction, so there isn't much value in testing it independently from the rest of the grouped MXFP8 infrastructure.

/*rowwise=*/true, /*columnwise=*/false);
fill_output_sentinel(dequantized);
nvte_group_dequantize(reference_input.data(), dequantized.data(), 0);
nvte_group_dequantize_v2(reference_input.data(), dequantized.data(), workspace.data(), 0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should make sure the workspace is large enough rather than assuming nvte_group_quantize_v2 and nvte_group_dequantize_v2 have the same workspace size.

Comment on lines +460 to +462
nvte_group_requantize_v2(input.data(), actual.data(),
return_dequantized ? dequantized_nvte.data() : nullptr,
workspace.data(), nullptr, 0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should make sure the workspace is large enough rather than assuming nvte_group_quantize_v2 and nvte_group_requantize_v2 have the same workspace size.

Comment on lines +233 to +237
if (shape_rep == SAME_BOTH_DIMS || shape_rep == VARYING_FIRST_DIM) {
nvte_group_dequantize(in_group_tensor, out_group_tensor, 0);
} else {
EXPECT_THROW(nvte_group_dequantize(in_group_tensor, out_group_tensor, 0), std::runtime_error);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Making nvte_group_dequantize and nvte_group_dequantize_v2 output to the same buffer means that the test could miss a bug where one silently skips.

This branch has not been deployed

No deployments
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