Fix GPSampler crash when default torch device is CUDA#6397
Merged
kAIto47802 merged 1 commit intooptuna:masterfrom Jan 7, 2026
Merged
Fix GPSampler crash when default torch device is CUDA#6397kAIto47802 merged 1 commit intooptuna:masterfrom
kAIto47802 merged 1 commit intooptuna:masterfrom
Conversation
Member
|
Thanks for your PR! The changes look good, but I'm still checking to make sure all issues have been resolved. |
Member
|
@kAIto47802 Could you review this PR? |
Member
|
I think this PR can be merged, but please give us some time to confirm. Since we're approaching the year-end and New Year holidays, we'll review the details after New Year's Day. |
Contributor
Author
|
Thank you for the update! I completely understand the timing around the year-end holidays. |
Contributor
|
This pull request has not seen any recent activity. |
kAIto47802
approved these changes
Jan 7, 2026
Collaborator
kAIto47802
left a comment
There was a problem hiding this comment.
Thank you for the PR. LGTM.
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.
Related to #6113
Motivation
When users set
torch.set_default_device("cuda"),GPSamplerinternal tensors are created directly on the GPU. The internal Gaussian Process implementation currently attempts to call.numpy()directly on these tensors (e.g., in_cache_matrix).This raises:
TypeError: can't convert cuda:0 device type tensor to numpyThe reporter (#6113) identified this issue 6 months ago but did not submit a PR.
Description of the changes
I have modified
optuna/_gp/gp.pyto ensure robust handling of GPU-resident tensors..detach().numpy()with.detach().cpu().numpy()in 5 critical locations:GPRegressor.length_scalesGPRegressor._cache_matrix(twice)GPRegressor._fit_kernel_paramsloss_funcThis change safely moves tensors to host memory before conversion. It is a no-op for tensors already on the CPU, ensuring zero regression for standard use cases.
Verification:
torch.set_default_device("cuda")and confirmed the crash is resolved.pytest tests/gp_tests/test_gp.pyandtests/samplers_tests/test_gp.py), achieving 51/51 passes.