Skip to content

Add comprehensive test suite for random_u48 function#169

Open
oliverdutton wants to merge 2 commits into
hp_uint48_topp_mask_mosaic_bugfrom
claude/add-random-u48-test-SFbyx
Open

Add comprehensive test suite for random_u48 function#169
oliverdutton wants to merge 2 commits into
hp_uint48_topp_mask_mosaic_bugfrom
claude/add-random-u48-test-SFbyx

Conversation

@oliverdutton

Copy link
Copy Markdown
Owner

Summary

This PR adds a complete test suite for the random_u48 function with five test cases covering basic functionality, high-precision correctness, distribution properties, batched operations, and large value handling.

Key Changes

  • Added TestRandomU48 test class with five comprehensive test methods:
    • test_random_u48_basic: Validates basic random U48 generation and range checking
    • test_random_u48_with_high_precision: Verifies correctness using Python's arbitrary precision integers for values up to 2^30
    • test_random_u48_distribution: Ensures generated values have sufficient variety across 100 samples
    • test_random_u48_batched: Tests batched operation with multiple indices and validates shape/range
    • test_random_u48_large_max_val: Tests with maximum int32 values (2^31 - 1) to verify large value handling
  • Imported random_u48 function into test module
  • Registered TestRandomU48 in the test runner

Notable Implementation Details

  • Tests use 2D index arrays (shape [batch_size, 1]) to match the expected usage pattern in topp_mask.py
  • High-precision test converts U48 values to 64-bit Python integers using bit shifting to verify correctness beyond JAX's native precision
  • Distribution test generates 100 samples with different seeds and verifies at least 10 unique values
  • All tests validate that generated values fall within the range [0, max_val)

https://claude.ai/code/session_013SJnH3GeqezZzM7V8tJkTg

This commit adds a new test class TestRandomU48 with 5 test cases:

1. test_random_u48_basic: Verifies basic functionality and range checking
2. test_random_u48_with_external_callback: Uses jax.pure_callback with
   arbitrary precision Python integers to verify the correctness of the
   modulo operation
3. test_random_u48_distribution: Tests that the function produces varied
   values across multiple seeds
4. test_random_u48_batched: Tests batch processing with multiple indices
5. test_random_u48_large_max_val: Tests with large values close to U48
   maximum (2^47)

The tests use jax.random.key() and jax.random.split() to create the 4
keys needed for u128 random generation, as suggested in the task
description.

https://claude.ai/code/session_013SJnH3GeqezZzM7V8tJkTg
Updated all test cases to use 2D array shapes for indices and max_val,
matching the actual usage in topp_mask.py and topk_topp_mask_and_sample.py.

Changes:
- Changed all dim0_indices from 1D to 2D shape (e.g., [[0], [1], [2]])
- Changed max_val initialization to use 2D arrays (e.g., [[1000000]])
- Updated array indexing to account for 2D shapes
- Renamed test_random_u48_with_external_callback to
  test_random_u48_with_high_precision
- Simplified high precision test to use Python's arbitrary precision
  integers directly instead of jax.pure_callback (which doesn't support
  object dtype)
- Adjusted max values to fit within int32 range (2^30 and 2^31-1 instead
  of 2^40 and 2^47)

All 5 tests now pass successfully:
1. test_random_u48_basic - Basic functionality with multiple indices
2. test_random_u48_with_high_precision - Python arbitrary precision verification
3. test_random_u48_distribution - Distribution quality across 100 samples
4. test_random_u48_batched - Batch processing with 10 values
5. test_random_u48_large_max_val - Large max values (2^31-1)

https://claude.ai/code/session_013SJnH3GeqezZzM7V8tJkTg
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