feat(compile): torch.compile support for flagos as a first-class inductor GPU device - #41
Open
lvyufeng wants to merge 1 commit into
Open
feat(compile): torch.compile support for flagos as a first-class inductor GPU device#41lvyufeng wants to merge 1 commit into
lvyufeng wants to merge 1 commit into
Conversation
Wires torch.compile into the flagos (PrivateUse1) backend by registering flagos with TorchInductor as a real GPU device, so the traced graph is handed to compile_fx unchanged and inductor emits Triton kernels that operate on flagos tensors directly. The earlier approach rewrote the graph and its example inputs to cuda before compiling. That is not just a copy per call: at::getAccelerator() is PrivateUse1/flagos in this build, and torch::autograd::Node::stream() only yields a stream when a node's input device type equals the accelerator. A cuda-rewritten graph therefore produces stream-less autograd nodes, and AOT autograd's backward trace inside compile_fx trips opt_ready_stream && opt_parent_stream (engine.cpp:1085) -- the cause of 8 of the 11 test failures. Verified by differential test: eager backward on plain cuda fails identically with torch.compile never involved. Registration surface (device_interface.py, inductor_codegen.py): * GPU_TYPES gains "flagos" in place -- is_gpu() is a membership test on that list object, and without it inductor takes the C++/CPU codegen path and never emits Triton. get_gpu_type()'s functools cache is primed while the list is narrowed, since it asserts at most one GPU type is available and the torch.cuda shim reports available too. * DeviceInterface subclass: device state from torch.flagos, hardware properties from torch.cuda (same physical GPU, same allocator). * DeviceProperties.create reports flagos as cuda at the Triton boundary. Triton's NVIDIA backend hard-checks target.backend == "cuda", so a literal "flagos" finds 0 compatible backends. Inductor already does this rewrite in the opposite direction for ROCm (hints.py:149). * Device op overrides + scheduling/wrapper codegen: the stock CUDA/Triton pipeline under the "flagos" key, also published on torch.flagos for inductor's official PrivateUse1 hook. Two generated-kernel bugs that only surface under compilation: * detach re-dispatched into itself. The kernel called at::detach(self), also registered on PrivateUse1, so it dispatched straight back. Eager hid the recursion because DeviceBoxingGuard rewrites self's device metadata first; under FakeTensor it cannot, since the Python dispatch key sits above the backend key. Dynamo traces every nn.Linear through detach, so this was a stack-overflow segfault at trace time. Now emits at::native::detach (NATIVE_DIRECT_VIEW_OPS). * gen_inplace passed only plain at::Tensor args to DeviceBoxingGuard, so clamp_.Tensor handed unboxed flagos min/max to a CUDA self and crashed. Optionals are now materialized into holders, matching gen_functional_pure. Both regressions are covered by tests that were confirmed to fail (segfault at the exact asserting line) against a build with the fixes reverted. CPU-torch wheel accommodations, since torch.cuda's Python layer was frozen without CUDA: re-attach CudaInterface.get_raw_stream (binding exists, the import-time _is_compiled() probe left it None), route torch.cuda.memory_* to the flagos allocator that backs the same pool, hand out flagos Event/Stream in place of the dummy base classes, force triton.cudagraphs off (torch.cuda.CUDAGraph raises on construction) and use_static_cuda_launcher off (not built). flagos_compile_backend now accepts the mode/options/dynamic kwargs dynamo forwards to named backends and expands them into compile_fx config_patches, rather than mutating inductor's global config. Tests: test_compile.py 12 passed / 1 skipped (was 2 passed / 8 failed); test_clamp_dispatch.py 15 passed; ops dispatch sweep 358 passed; test_ops.py 58 passed; allocator/factory/fallback/unit 73 passed. Docs updated to drop the device-aliasing description and the unmeasured performance-parity figures; benchmarking remains open work. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Enables
torch.compileon the flagos device by registering flagos withTorchInductor as a first-class GPU device. The traced graph is handed to
compile_fxunchanged, and inductor emits Triton kernels that operate on flagostensors directly — no conversion to cuda, no copy at the graph boundary.
This works because flagos runs on the physical GPU that
torch.cudadescribes:its allocator delegates to
c10::cuda::CUDACachingAllocator, so a flagostensor's storage already is CUDA memory.
Why not the device-aliasing approach
The first revision of this PR rewrote the graph and its example inputs to cuda
before calling
compile_fx. That is not just a copy per call — it breaksbackward.
at::getAccelerator()is PrivateUse1/flagos in this build, andtorch::autograd::Node::stream()only yields a stream when a node's input devicetype equals the accelerator. A cuda-rewritten graph therefore produces
stream-less autograd nodes, and AOT autograd's backward trace inside
compile_fxtrips
opt_ready_stream && opt_parent_stream(engine.cpp:1085). That was thecause of 8 of the 11 test failures.
Confirmed by differential test: eager backward on plain cuda fails identically,
with
torch.compilenever involved.Registration surface
GPU_TYPES.append("flagos")is_gpu()is a membership test on that list object; without it inductor takes the C++/CPU codegen path and never emits Triton.get_gpu_type()'s cacheregister_interface_for_devicetorch.flagos, hardware properties fromtorch.cuda(same physical GPU).DeviceProperties.createwraptarget.backend == "cuda", so a literal"flagos"finds 0 compatible backends. Inductor already does this in reverse for ROCm (hints.py:149).register_device_op_overridesCUDADeviceOpOverrides— attributes present on the base class never reach__getattr__delegation.register_backend_for_device"flagos"key.Two generated-kernel bugs that only surface under compilation
detachre-dispatched into itself. The generated kernel calledat::detach(self), which is also registered on PrivateUse1, so it dispatchedstraight back — infinite recursion. Eager hid this because
DeviceBoxingGuardrewrites
self's device metadata first; under FakeTensor it cannot, since thePython dispatch key sits above the backend key. Dynamo traces every
nn.Linearthroughdetach, so this was a stack-overflow segfault at tracetime. Now emits
at::native::detach(NATIVE_DIRECT_VIEW_OPS).gen_inplacedidn't boxoptional<Tensor>.clamp_.Tensorhanded unboxedflagos
min/maxto a CUDAselfand crashed. Optionals are now materializedinto holders, matching
gen_functional_pure.Both are covered by tests confirmed to fail (segfault at the exact asserting
line) against a build with the fixes reverted.
CPU-torch wheel accommodations
This build pairs a CPU-only pip torch with an external
libtorch_cuda.so, soseveral
torch.cudaPython bindings are missing:CudaInterface.get_raw_streamre-attached — the binding exists, but theimport-time
_is_compiled()probe left itNone.torch.cuda.memory_*routed to the flagos allocator backing the same pool.Event/Streamin place of the dummy base classes.triton.cudagraphs = False(torch.cuda.CUDAGraphraises on construction) anduse_static_cuda_launcher = False(not built).flagos_compile_backendalso accepts themode/options/dynamickwargs dynamoforwards to named backends, expanding them into
compile_fxconfig_patchesrather than mutating inductor's global config.
Testing
Rebased onto
flagos/main(0700b61) and re-verified end to end on A100 in thetorch-fl-210env:tests/integration/test_compile.pytests/integration/ops/test_clamp_dispatch.pytests/integration/ops/(full sweep)tests/integration/ops/test_rng_dispatch.pytest_ops/allocator/factory/fallback_trace/clone_dispatchtests/unitThe rebase brought in the RNG generator-injection work (#39, #49), which touches
the same
scripts/codegen_ops.pytemplates; the conflict was resolved keepingboth, and
python scripts/codegen_ops.pywas verified to reproduce the committedcuda_kernels.ccbyte-for-byte.Open work
FLAGOS_USE_FLAGTREE=1, off by default)revision of this PR quoted speedup figures that were measured under the old
device-aliasing design, so they no longer describe this code and have been
dropped rather than restated.
🤖 Generated with Claude Code