-
Couldn't load subscription status.
- Fork 533
Remove nvidia-mathdx dependency
#2295
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /************************************************************************* | ||
| * Copyright (c) 2022-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * | ||
| * See LICENSE for license information. | ||
| ************************************************************************/ | ||
|
|
||
| #ifndef TRANSFORMER_ENGINE_COMMON_UTIL_CURANDDX_HPP_ | ||
| #define TRANSFORMER_ENGINE_COMMON_UTIL_CURANDDX_HPP_ | ||
|
|
||
| namespace transformer_engine { | ||
| namespace curanddx { | ||
| namespace detail { | ||
|
|
||
| inline constexpr unsigned int philox4x32_w32_0 = 0x9E3779B9U; | ||
| inline constexpr unsigned int philox4x32_w32_1 = 0xBB67AE85U; | ||
| inline constexpr unsigned int philox4x32_m4x32_0 = 0xD2511F53U; | ||
| inline constexpr unsigned int philox4x32_m4x32_1 = 0xCD9E8D57U; | ||
|
|
||
| __forceinline__ __device__ unsigned int mulhilo32(unsigned int a, unsigned int b, | ||
| unsigned int* hip) { | ||
| *hip = __umulhi(a, b); | ||
| return a * b; | ||
| } | ||
|
|
||
| __forceinline__ __device__ uint4 single_round(uint4 ctr, uint2 key) { | ||
| unsigned int hi0; | ||
| unsigned int hi1; | ||
| unsigned int lo0 = mulhilo32(philox4x32_m4x32_0, ctr.x, &hi0); | ||
| unsigned int lo1 = mulhilo32(philox4x32_m4x32_1, ctr.z, &hi1); | ||
|
|
||
| uint4 ret = {hi1 ^ ctr.y ^ key.x, lo1, hi0 ^ ctr.w ^ key.y, lo0}; | ||
| return ret; | ||
| } | ||
|
|
||
| template <unsigned int Rounds> | ||
| __forceinline__ __device__ uint4 multiple_rounds(uint4 c, uint2 k) { | ||
| for (unsigned int i = 0; i < Rounds - 1; i++) { | ||
| c = single_round(c, k); // 1 | ||
| k.x += philox4x32_w32_0; | ||
| k.y += philox4x32_w32_1; | ||
| } | ||
| return single_round(c, k); // Rounds | ||
| } | ||
|
|
||
| template <unsigned int Rounds> | ||
| struct philox4x32_native_state { | ||
| static constexpr unsigned int rounds = Rounds; | ||
|
|
||
| uint4 ctr; | ||
| uint2 key; | ||
|
|
||
| __forceinline__ __device__ void philox_state_incr() { | ||
| if (++ctr.x) return; | ||
| if (++ctr.y) return; | ||
| if (++ctr.z) return; | ||
| ++ctr.w; | ||
| } | ||
|
|
||
| __forceinline__ __device__ void philox_state_incr(size_t n) { | ||
| unsigned int nlo = (unsigned int)(n); | ||
| unsigned int nhi = (unsigned int)(n >> 32); | ||
|
|
||
| ctr.x += nlo; | ||
| if (ctr.x < nlo) nhi++; | ||
|
|
||
| ctr.y += nhi; | ||
| if (nhi <= ctr.y) return; | ||
| if (++ctr.z) return; | ||
| ++ctr.w; | ||
| } | ||
|
|
||
| __forceinline__ __device__ void philox_state_incr_hi(size_t n) { | ||
| unsigned int nlo = (unsigned int)(n); | ||
| unsigned int nhi = (unsigned int)(n >> 32); | ||
|
|
||
| ctr.z += nlo; | ||
| if (ctr.z < nlo) nhi++; | ||
|
|
||
| ctr.w += nhi; | ||
| } | ||
|
|
||
| // offset is the total # of 128bits generated with a single generate4() call | ||
| __forceinline__ __device__ void skip_offset(size_t n) { philox_state_incr(n); } | ||
|
|
||
| __forceinline__ __device__ void skip_subsequence(size_t n) { philox_state_incr_hi(n); } | ||
|
|
||
| __forceinline__ __device__ void init(size_t seed, size_t subsequence, size_t offset) { | ||
| ctr = make_uint4(0, 0, 0, 0); | ||
| key.x = (unsigned int)seed; | ||
| key.y = (unsigned int)(seed >> 32); | ||
|
|
||
| skip_subsequence(subsequence); | ||
| skip_offset(offset); | ||
| } | ||
|
|
||
| __forceinline__ __device__ uint4 generate4() { | ||
| auto tmp = multiple_rounds<Rounds>(ctr, key); | ||
| philox_state_incr(); | ||
| return tmp; | ||
| } | ||
| }; | ||
| } // namespace detail | ||
| } // namespace curanddx | ||
| } // namespace transformer_engine | ||
|
|
||
| #endif // TRANSFORMER_ENGINE_COMMON_UTIL_CURANDDX_HPP_ | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: overflow logic has subtle carry propagation; the increment chain
ctr.x += nlo; if (ctr.x < nlo) nhi++; ctr.y += nhi; if (nhi <= ctr.y) return;stops early ifnhi <= ctr.y, but this skips carrying intoctr.z/ctr.wwhenctr.ywraps. Ifctr.yoverflows after addingnhi, the carry is lost. should the condition on line 67 beif (nhi > ctr.y)(overflow occurred) instead ofif (nhi <= ctr.y) return(no overflow)?