Skip to content

Closes #4787: Shuffling alternative - #4789

Merged
ajpotts merged 1 commit into
Bears-R-Us:mainfrom
1RyanK:4787-Shuffling_alternative
Aug 29, 2025
Merged

ajpotts merged 1 commit into
Bears-R-Us:mainfrom
1RyanK:4787-Shuffling_alternative

Conversation

@1RyanK

@1RyanK 1RyanK commented Aug 15, 2025

Copy link
Copy Markdown
Contributor

This uses the idea of the Feistel cipher to shuffle an array.

Perhaps it should be noted that it won't work on arrays with more than 2^64 elements, but it probably wouldn't take that much effort to extend.

It's possible this could be improved by allowing for imbalanced Feistel ciphers.

Closes #4787: Shuffling alternative

@drculhane

drculhane commented Aug 21, 2025 •

Copy link
Copy Markdown
Contributor

I like this (mandatory disclaimer: I didn't reverse all of the code, although it definitely looks Feistel-y).

I added this to the random_test, just for fun:

        pda = ak.arange(size).astype(data_type)
        if method == "Feistel" :
            for i in range(2000):
                feistel_key = np.random.randint(1,2**63)
                pda_copy = pda.copy()
                rng.shuffle(pda_copy, method=method, feistel_key=feistel_key)
                assert (pda != pda_copy).any()
                assert (pda == ak.sort(pda_copy)).all()

The shuffled version of an arange should almost never exactly match the original.

The sorted shuffled version of an arange should always match the original.

In 2000 tries, both of these checks always passed. So I'm inclined to agree that this works. As I write this, it's still in Draft, but if it weren't, I'd approve it.

@1RyanK
1RyanK force-pushed the 4787-Shuffling_alternative branch from 29c663a to bfd78ae Compare August 25, 2025 15:04
@1RyanK

1RyanK commented Aug 27, 2025

Copy link
Copy Markdown
Contributor Author

I've realized that my comm diagnostics are not great:

| locale |   get | put | execute_on | execute_on_nb |
| -----: | ----: | --: | ---------: | ------------: |
|      0 |    78 |  81 |         12 |            87 |
|      1 | 70249 |  25 |         51 |             6 |
|      2 | 70007 |  25 |         51 |             6 |
|      3 | 70443 |  25 |         51 |             6 |

I don't think the number of gets should be that high (this was an an array of size 40,000). This is probably affecting performance. For reference, here is the comm diagnostics for MergeShuffle, same array:

| locale |   get |   put | execute_on | execute_on_nb |
| -----: | ----: | ----: | ---------: | ------------: |
|      0 | 10014 | 10000 |         15 |            27 |
|      1 |  5061 |  5035 |          0 |             0 |
|      2 | 10101 | 10088 |          0 |             0 |
|      3 |    69 |    44 |          0 |             0 |

@1RyanK

1RyanK commented Aug 27, 2025

Copy link
Copy Markdown
Contributor Author

I think I fixed the problem. Now the benchmark on 8 locales with an array size of 8,000,000 comes out like this:

------------------------------------------------------------------------------------- benchmark 'AK_shuffle': 6 tests --------------------------------------------------------------------------------------
Name (time in s)                             Min                 Max                Mean             StdDev              Median                IQR            Outliers     OPS            Rounds  Iterations
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
bench_shuffle[Feistel-uint64]            16.7456 (1.0)       18.1455 (1.0)       17.4663 (1.0)       0.7009 (2.49)      17.5077 (1.0)       1.0499 (2.49)          1;0  0.0573 (1.0)           3           1
bench_shuffle[Feistel-int64]             16.8649 (1.01)      19.0673 (1.05)      17.9778 (1.03)      1.1014 (3.91)      18.0012 (1.03)      1.6519 (3.92)          1;0  0.0556 (0.97)          3           1
bench_shuffle[Feistel-float64]           18.4559 (1.10)      19.0184 (1.05)      18.7321 (1.07)      0.2814 (1.0)       18.7218 (1.07)      0.4219 (1.0)           1;0  0.0534 (0.93)          3           1
bench_shuffle[MergeShuffle-float64]     261.1331 (15.59)    274.0888 (15.11)    268.1304 (15.35)     6.5400 (23.24)    269.1691 (15.37)     9.7168 (23.03)         1;0  0.0037 (0.07)          3           1
bench_shuffle[MergeShuffle-int64]       268.7395 (16.05)    279.8357 (15.42)    274.0829 (15.69)     5.5594 (19.76)    273.6735 (15.63)     8.3221 (19.73)         1;0  0.0036 (0.06)          3           1
bench_shuffle[MergeShuffle-uint64]      286.2646 (17.09)    314.3565 (17.32)    304.8861 (17.46)    16.1275 (57.31)    314.0373 (17.94)    21.0689 (49.94)         1;0  0.0033 (0.06)          3           1
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Furthermore, the comm diagnostics for MergeShuffle all came out along the lines of this:

| locale |     get |     put | execute_on | execute_on_nb |
| -----: | ------: | ------: | ---------: | ------------: |
|      0 | 1500264 | 1500264 |         70 |            63 |
|      1 | 1000729 | 1000724 |          0 |             0 |
|      2 | 1496273 | 1496267 |          0 |             0 |
|      3 |  502281 |  502277 |          0 |             0 |
|      4 | 1500755 | 1500749 |          0 |             0 |
|      5 |  999146 |  999141 |          0 |             0 |
|      6 | 1499070 | 1499064 |          0 |             0 |
|      7 |    3237 |    3234 |          0 |             0 |

For Feistel, it was

| locale | get | put | execute_on | execute_on_nb |
| -----: | --: | --: | ---------: | ------------: |
|      0 | 182 | 245 |         28 |           203 |
|      1 | 520 |  57 |         83 |            14 |
|      2 | 520 |  57 |         83 |            14 |
|      3 | 520 |  57 |         83 |            14 |
|      4 | 520 |  57 |         83 |            14 |
|      5 | 520 |  57 |         83 |            14 |
|      6 | 520 |  57 |         83 |            14 |
|      7 | 520 |  57 |         83 |            14 |

Exciting!

@1RyanK
1RyanK force-pushed the 4787-Shuffling_alternative branch from d57814b to 9cc4672 Compare August 29, 2025 16:46
@1RyanK
1RyanK force-pushed the 4787-Shuffling_alternative branch from 9cc4672 to 3bace64 Compare August 29, 2025 17:45
@1RyanK
1RyanK marked this pull request as ready for review August 29, 2025 17:46

@drculhane drculhane left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I ran the same check as when this was still in draft, and it still passes. Nice work.

@jaketrookman jaketrookman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good

@ajpotts
ajpotts added this pull request to the merge queue Aug 29, 2025
Merged via the queue into Bears-R-Us:main with commit c804ada Aug 29, 2025
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.

Shuffling alternative

4 participants