Skip to content

Add xla random generator. #9539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Add xla random generator. #9539

wants to merge 3 commits into from

Conversation

iwknow
Copy link
Contributor

@iwknow iwknow commented Aug 5, 2025

This is the very first PR for #9159. It purely add the generator without any utilization of it. #9159 (comment) comment outlines the steps for entire change.

@iwknow
Copy link
Contributor Author

iwknow commented Aug 5, 2025

@qihqi please review

@iwknow
Copy link
Contributor Author

iwknow commented Aug 7, 2025

a kindly ping @qihqi

@qihqi qihqi self-requested a review August 12, 2025 20:14
@qihqi
Copy link
Collaborator

qihqi commented Aug 12, 2025

Just to understand: so the GeneratorImpl doesn't actually implement the RNG algorithm only manages the state?


auto state_tensor = at::empty({(int64_t)total_size}, at::TensorOptions().dtype(at::kByte).device(at::kCPU));
uint8_t* data_ptr = state_tensor.data_ptr<uint8_t>();
memcpy(data_ptr, &state_->seed_, seed_size);
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: can probably write to state_tensor.index(0).fill_( ...) to write to the tensor.

@iwknow
Copy link
Contributor Author

iwknow commented Aug 12, 2025

Just to understand: so the GeneratorImpl doesn't actually implement the RNG algorithm only manages the state?

that is correct. it is exactly what https://github.com/pytorch/pytorch/blob/main/aten/src/ATen/cuda/CUDAGeneratorImpl.h does. the idea is that the generator manages the state and different RNG algorithm generates the random numbers based on the state obtained from the generator.

@iwknow
Copy link
Contributor Author

iwknow commented Aug 12, 2025

@qihqi is there anyway to auto-format the cpp code? i know we use yapf for python files in this project.

@iwknow
Copy link
Contributor Author

iwknow commented Aug 12, 2025

@qihqi is there anyway to auto-format the cpp code? i know we use yapf for python files in this project.

nvm, i found the linter uses clang-format-16. i will update the contribution instruction for cpp formatting later. a more important question that needs your attention is #9159 (comment). but if you don't have a strong opinion on it, i will do on my discretion.

@iwknow
Copy link
Contributor Author

iwknow commented Aug 13, 2025

it seems that the failed tests are not related to this change. i've formatted the files.

I don't have the permission to run the tests, please trigger them for me. also, merge if it LGTY. thanks! @qihqi

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