Repository navigation
Abstract CUDA hardcodes into configurable te_device_type / te_platform - #3513
Conversation
…rectory coverage)
This commit completes the CUDA plugin compatibility patches across the entire codebase:
1. Debug directory (transformer_engine/debug/):
- fake_quant.py: Replace is_cuda checks with te_device_type()
- log_fp8_tensor_stats.py: Replace torch.device('cuda') and torch.cuda.get_device_capability()
- per_tensor_scaling.py: Replace is_cuda checks with te_device_type()
- utils/stats_buffer.py: Replace .cuda() calls with .to(te_device_type())
2. Additional missing files from cuda_patch_rc2.14:
- permutation.py: Replace all is_cuda checks with device.type comparisons
- tensor/float8_tensor.py: Replace is_cuda checks with device.type comparisons
- tensor/float8_blockwise_tensor.py: Replace is_cuda checks with device.type comparisons
- tensor/mxfp8_tensor.py: Replace is_cuda checks with device.type comparisons
3. Core infrastructure:
- transformer_engine/__init__.py: Export te_platform and te_device_type from pytorch module
- transformer_engine/pytorch/__init__.py: Define te_platform() and te_device_type() functions
All runtime CUDA hardcodes have been replaced with plugin-compatible abstractions.
Total: 64 files modified (+405/-289 lines)
Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
[High impact] The PR changes device selection across the Transformer Engine PyTorch layer. The test change is safe, but the two outstanding findings should be addressed before merging. FindingsSummaryThe PR adds configurable device helpers and an optional plugin patch hook across the PyTorch code.
Diagram%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Load PyTorch extension] --> B{Plugin configured?}
B -- Yes --> C[Register original extension alias]
C --> D[Load plugin]
D --> E[Apply optional device patches]
B -- No --> F[Set device defaults]
E --> F
F --> G[Import PyTorch modules]
Reviews (23) · Last reviewed commit: "Fix regex in test_cross_entropy" · Reviewed by Greptile |
The te_platform and te_device_type functions are dynamically assigned to the transformer_engine module namespace via: transformer_engine.te_platform = te_platform transformer_engine.te_platform = te_platform in transformer_engine/pytorch/__init__.py, so explicit imports in transformer_engine/__init__.py cause black to fail parsing due to circular import detection. Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
Move te_platform and te_device_type imports outside of parenthesized import lists to fix SyntaxError. The imports were incorrectly placed inside multi-line import statements, preventing the modules from loading. Fixed files: - transformer_engine/debug/features/log_fp8_tensor_stats.py - transformer_engine/pytorch/attention/dot_product_attention/backends.py - transformer_engine/pytorch/attention/dot_product_attention/context_parallel.py - transformer_engine/pytorch/attention/dot_product_attention/dot_product_attention.py - transformer_engine/pytorch/attention/inference.py - transformer_engine/pytorch/graph.py - transformer_engine/pytorch/quantization.py - transformer_engine/pytorch/quantized_tensor.py - transformer_engine/pytorch/transformer.py Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
Several files imported only te_platform but also called te_device_type(), causing NameError at runtime. Add te_device_type to imports. Fixed files: - transformer_engine/pytorch/attention/dot_product_attention/flex_attention.py - transformer_engine/pytorch/attention/rope.py - transformer_engine/pytorch/cpp_extensions/gemm.py - transformer_engine/pytorch/optimizers/fused_adam.py - transformer_engine/pytorch/utils.py Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
Move te_platform and te_device_type imports outside of parenthesized import lists to fix SyntaxError across 20 more files. Fixed files: - transformer_engine/pytorch/attention/dot_product_attention/utils.py - transformer_engine/pytorch/attention/multi_head_attention.py - transformer_engine/pytorch/cpu_offload.py - transformer_engine/pytorch/module/base.py - transformer_engine/pytorch/module/grouped_linear.py - transformer_engine/pytorch/module/layernorm_linear.py - transformer_engine/pytorch/module/layernorm_mlp.py - transformer_engine/pytorch/module/linear.py - transformer_engine/pytorch/ops/basic/basic_linear.py - transformer_engine/pytorch/ops/basic/grouped_linear.py - transformer_engine/pytorch/ops/basic/l2normalization.py - transformer_engine/pytorch/ops/fused/forward_linear_scale_add.py - transformer_engine/pytorch/ops/fused/grouped_mlp.py - transformer_engine/pytorch/ops/fused/userbuffers_backward_linear.py - transformer_engine/pytorch/ops/fused/userbuffers_forward_linear.py - transformer_engine/pytorch/ops/op.py - transformer_engine/pytorch/tensor/nvfp4_tensor.py - transformer_engine/pytorch/tensor/storage/mxfp8_tensor_storage.py - transformer_engine/pytorch/triton/mhc.py - transformer_engine/pytorch/triton/permutation.py Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
In make_row_id_map, use routing_map.device for tensor allocation instead of te_device_type() to respect the input tensor's device. Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
Revert all te_platform() method calls back to torch.cuda.* calls. Keep te_device_type() string replacements for device type parameters. Preserve te_platform definition in transformer_engine/pytorch/__init__.py. Changes: - Reverted 146 te_platform() calls back to torch.cuda.* - Removed 'from transformer_engine import te_platform' imports - Kept all te_device_type() replacements intact - Kept te_platform definition in __init__.py for future use Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
…istributed Replace remaining te_platform() calls with torch.cuda equivalents: - torch.cuda.nvtx.range() / range_push() / range_pop() for NVTX profiling - torch.cuda.amp.autocast for autocast context manager - Update docstring reference in fused_adam.py All te_platform() usage has been removed except for the definition in transformer_engine/pytorch/__init__.py. Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn> # Conflicts: # transformer_engine/pytorch/module/layernorm_mlp.py
Comments Outside DiffThese findings could not be posted inline.
|
Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
|
@lxd-cumt Please fix the lint errors (not sure about those documentation errors, maybe they are also caused by the undefined variable?). Generally LGTM afterwards |
Add the missing `te_device_type` import to eight modules that called it
without importing it, which would raise NameError at runtime: jit,
attention/inference, attention/dot_product_attention/{backends,
context_parallel,utils}, ops/fused/{grouped_mlp,
userbuffers_backward_linear}, and triton/mhc.
Move the `te_device_type` import ahead of relative imports where pylint
classifies it as a first-party import that must precede local ones. In
jit and distributed, place it so it does not split the torch import
group. Order `transformer_engine_torch` before `transformer_engine` in
ops/_common.
Add docstrings to `te_device_type` and `te_platform`.
Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
Resolve conflicts in two PyTorch modules where upstream touched the same lines as the device-type patch: - module/base.py: upstream reworked `get_dummy_wgrad` to add a ZeroTensor path and hoisted the target device into a local. Apply the device-type substitution to that local so the cache key, the ZeroTensor path, and the `torch.empty` call all share one device. - module/grouped_linear.py: upstream refactored the forward and backward implementations. Take the refactored file and re-apply the device-type substitution to the `m_splits` device check and the `device` constructor default and its docstring. Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
| dtype: torch.dtype | ||
| if torch.is_autocast_enabled(): | ||
| dtype = torch.get_autocast_dtype("cuda") | ||
| dtype = torch.get_autocast_dtype(te_device_type()) |
There was a problem hiding this comment.
With a non-CUDA TE_DEVICE_TYPE, this code reads the configured device’s autocast dtype but still checks CUDA’s enabled state through torch.is_autocast_enabled(). When vendor autocast is on and CUDA autocast is off, the operation keeps the input dtype instead of the requested dtype.
Pass the configured device to both checks, with the needed version guard. The same mismatch appears in SwiGLU, grouped linear operations, and fused linear operations.
The device-type patch had changed the autocast dtype lookups to the configured device while leaving the enabled checks on CUDA. With a non-CUDA device type, vendor autocast enabled, and CUDA autocast disabled, these sites took the non-autocast branch and kept the input dtype instead of the requested autocast dtype. Route the operation sites through `maybe_autocast_dtype`, which already queries one device for both calls and carries the torch version guard. This covers the activation, SwiGLU, grouped linear, basic linear, and fused linear forward operations. Add `torch_is_autocast_gpu_enabled` next to `torch_get_autocast_gpu_dtype` for the two sites outside the operation layer that had the same mismatch: `set_activation_dtype`, which sets the activation dtype for every module, and the TransformerLayer forward cast. Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
`tex.thd_get_partitioned_indices` allocates a CUDA output tensor and launches a CUDA kernel on the current CUDA stream, so it runs only on CUDA. The device-type patch had changed its dispatch condition to the configured device type, which sent packed sequences into that kernel on a non-CUDA device instead of the device-capable fallback, failing the `is_cuda` assertion before the fallback could run. Restore the CUDA condition in `build_rank_indices` and in `_get_thd_partition_cu_seqlens`, which had the same problem when normalizing the boundaries that feed the kernel, and record why both are gated on CUDA rather than on the configured device. Keep the configured device in the NO_LOAD_BALANCE branch, which builds indices with plain tensor operations and runs no CUDA kernel. Restore the assertion message, which named the device type that triggers it. Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
The top-level hook in transformer_engine/__init__.py imported <plugin>.patches before load_framework_extension() registered transformer_engine_torch_nv. A plugin that imports that alias at package level - a case the loader explicitly supports - failed there, and the enclosing except only warned, so apply_patches() never ran. The loader later imported the plugin successfully but never retried the patches, leaving device patches unapplied while vendor calls still went through CUDA. Move the call into load_framework_extension(), after the alias is registered and before the PyTorch submodules capture references to torch.cuda entry points. Probe the submodule with find_spec so a plugin that needs no patches may omit it, without masking real import failures inside an existing patches module. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
The modules whose `device` argument now defaults to `te_device_type()` allocate their parameters on the configured device, but the `prepare_forward()` input check still rejected anything whose `is_cuda` was false. That property is a tensor attribute, so patching the `torch.cuda` namespace does not change it, and forward raised before a plugin's native operation could run. Compare `device.type` against `te_device_type()` instead, and report the configured device type in the error message. One check covers Linear, LayerNormLinear, GroupedLinear, and LayerNormMLP, which share this base method. Behavior on CUDA is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
The remote branch carried an equivalent merge of upstream main. Both sides already contain 6b5cd7e, so this reconciles the histories without changing any file content. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
Removing the plugin hook left an extra blank line behind. Drop it so the file matches main and no longer appears in the diff. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
find_spec() raises ModuleNotFoundError instead of returning None when the parent module is not a package, so a single-file plugin that provides load_plugins() failed the optional-patches probe. The exception handler then disabled the plugin and restored the original extension. Check for __path__ before searching for the submodule. Genuine import failures inside an existing patches module still propagate. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
|
Fixed the issues raised in the Greptile Robot review above. |
The merged upstream GroupedTensor stream-tracking handler checked `tensor.is_cuda`, which pins the path to CUDA. Match the device abstraction used by the other quantized-tensor `record_stream` handlers so plugin device types propagate allocator stream tracking. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
The attention entry points asserted `is_cuda` on queries, keys, and values, so a non-CUDA configured device failed validation before any attention implementation could run, even though the surrounding state was already created on the configured device. Check the configured device type instead, in DotProductAttention and in the matching FlashAttention and FusedAttention assertions. Apply the same change to the per-document THD partitioning check in context parallelism, and drop the redundant CUDA half of the mixed-THD sequence_ids check, where the comparison against the Q/K/V device already enforces the documented requirement. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
The paged KV cache page table and the EpBuffer default device fed tensors into torch.library custom ops registered with device_types="cuda". Allocating them on a configured non-CUDA device left no matching registered implementation, so the dispatcher failed the call before the wrapped tex function could run. Restore the CUDA allocations so these tensors match their op registrations, and drop the now-unused imports. Supporting another device on these paths requires changing the op registrations, which depends on how the device is registered with PyTorch and on when the device type is configured relative to module import. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
b733b81 to
4cc584f
Compare
| importlib.import_module(f"{_nvte_plugin}.patches").apply_patches() | ||
| _plugin.load_plugins() |
There was a problem hiding this comment.
Failed plugins leave patches active
If apply_patches() changes torch.cuda or sets TE_DEVICE_TYPE, then load_plugins() raises, the loader restores only the extension entries in sys.modules. The device patches and settings stay active. Import continues with the original CUDA extension but vendor device calls and allocations, so later model construction or execution can fail instead of falling back to CUDA.
Stop initialization on this failure, or undo the patches and device settings before restoring the CUDA extension.
apply_patches() rewrites process-global state in place - torch.cuda entry points, TE_DEVICE_TYPE and TE_PLATFORM - with no way to undo it. Running it before load_plugins() meant a load_plugins() failure left those patches active while the except block restored only sys.modules, so import continued with the CUDA extension under a vendor device type and failed later, far from the cause. Run load_plugins() first so everything that can fail is rolled back completely. The patches still precede the PyTorch submodules that capture torch.cuda entry points, and still follow the _nv alias registration that a plugin package may need to import itself. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Xianduo Li <lixianduo@mail.nankai.edu.cn>
|
@ptrendx hi, I have fixed the previous CI errors. Please approve and rerun CI. Thanks. |
|
/te-ci pytorch |
Signed-off-by: Przemyslaw Tredak <ptrendx@gmail.com>
flex_attention.py conflicted: NVIDIA#3513 replaced its "cuda" literals with te_device_type(), and this branch had moved those three spots into cudnn_pygraph.py. Took the refactor and applied the same substitution there, so the handle check, the device cache key and the workspace allocation all read te_device_type() in their new home. The import NVIDIA#3513 added to flex_attention.py moved with them. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Signed-off-by: Nitin Vegesna <nvegesna@nvidia.com>
|
This is second time some PR broke docs build in last week. Can we ensure that it is impossible to merge something if docs check does not pass? @ksivaman @ptrendx @fheinecke |
Source: NVIDIA#3513, head a417941, merge f8f34c6. Backport only common optional patch hook and PyTorch device globals/accessors. Move existing FL vendor patch loop behind plugin.patches.apply_patches and support the hook in FL-only initialization. Reconcile these blocks when upgrading past v2.20.2.
FlagOS Proposal: Plugin Architecture & Device-Agnostic Abstraction for TransformerEngine
Device-Type Abstraction: Replacing Hardcoded
"cuda"ReferencesThe current TE PyTorch layer contains ~100 hardcoded
"cuda"string literals and ~165torch.cuda.*API calls. These span device placement (device="cuda"), autocast context (device_type="cuda"), device-type guards (device.type == "cuda"), and RNG state management (torch.cuda.CUDAGraph,torch.cuda._lazy_call). This makes TE non-functional on alternative accelerator platforms without invasive patching.Proposed Design
Soft abstraction – A global
te_device_type()/te_platform()accessor replaces ~200 literal"cuda"strings across the Python codebase.Platform monkey-patch – A vendor-provided
apply_patch()hook runs at import time to directly remaptorch.cuda.*APIs (e.g.torch.cuda.device,torch.cuda.current_device,torch.cuda.current_stream) to the vendor equivalents (e.g.torch.other_vendor.*).