fix: PyTorch 2.x compatibility for GPU inference#44
Open
jimmyag2026-prog wants to merge 1 commit into
Open
Conversation
Fix 18 device mismatch issues that crash DI-star under PyTorch 2.x: - torch._six module removed in PyTorch 1.11: replace with stdlib equivalents (optimizer_util.py, grad_clip.py, collate_fn.py) - SELECTED_UNITS_MASK on CPU while action tensors on GPU (policy.py:35) - torch.arange(bs) creates CPU tensors used to index GPU tensors (action_arg_head.py: all 16 occurrences) - Selected unit indice on GPU while observation on CPU in post_process (agent.py:404, 407) Tested: rl_model (Zerg) vs Elite bot (Zerg) on 3 random maps, all wins. GPU inference ~3.9x faster than CPU mode with identical results.
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
This PR fixes 18 device mismatch issues that prevent DI-star from running with PyTorch ≥2.0 on GPU. DI-star was originally developed for PyTorch 1.7.1, and PyTorch 2.x changed the behavior of:
torch._sixmodule removed (PyTorch 1.11+) — imports replaced with stdlib equivalentstorch.arange(bs)defaults to CPU but is used to index GPU tensorsChanges
distar/ctools/— torch._six compatibility (3 files)torch_utils/optimizer_util.pytorch._six.inf→math.inftorch_utils/grad_clip.pytorch._six.inf→math.infdata/collate_fn.pytorch._six.string_classes→(str, bytes)distar/agent/default/model/policy.py— GPU tensor indexingLine 35:
SELECTED_UNITS_MASK[action[...]]→SELECTED_UNITS_MASK.to(action[...].device)[action[...]]distar/agent/default/model/head/action_arg_head.py— All 16torch.arange(bs)callsReplaced
torch.arange(bs)→torch.arange(bs, device=ae.device)(withflag.devicein_get_key_maskscope whereaeis not available).distar/agent/default/agent.py— Post-processing device mismatchLines 404, 407: Added
.cpu()when indexing CPU observations with GPU model output tensors.Testing
Compatibility
Tested with:
The original PyTorch 1.7.1 behavior is preserved — this PR only ensures the same code works correctly under newer PyTorch versions.