Skip to content

How to checkpoint ak.numpy.random.Generator ? #4709

Open

Description

@vasslitvinov

This issue seeks a solution for the currently-incomplete checkpointing of ak.numpy.random.Generator objects.

Details

The current checkpointing effort and #4710 are concerned with saving and restoring the SymTab entires ON THE SERVER. This is not sufficient / correct for preserving the complete state of an ak.numpy.random.Generator instance. That is, in the following scenario:

g = default_rng(...)
... use `g` ...
ak.save_checkpoint(someName)
... use `g` some more #1 ...
ak.load_checkpoint(someName)
... use `g` some more #2 ...

The user expects that the use of g in #2 will generate the same sequence of numbers as its use in #1.

Currently this will not be the case, here is why. While the server's state related to g will be restored properly upon ak.load_checkpoint(someName), g._state will not be restored while affecting the numbers generated by g.

In other words, these Generators rely on both server-side and client-side state, while the current checkpointing approach preserves only server-side state.

My initial recommendation is to eliminate g._state. It is used to fast-forward Chapel's RNGs, however it may not be necessary.

BTW if we keep g._state, we need to add its increment by 1 in cases like this:

if size is None:
    # delegate to numpy when return size is 1
    return self._np_generator.uniform(low=low, high=high)

Activity

  1. added
    bug
    on Jul 11, 2025
  2. vasslitvinov commented on Jul 11, 2025

    @vasslitvinov
    ContributorAuthor

    Here is another proposal that will result in simpler implementation, thanks to @mppf:

    • Store (seed, state) on the client side. Notes: "State", which is the current position in the random number sequence for the given seed, is already stored as Generator._state. "Seed" is also stored, we just need to "instantiate" it to an integer in the case that the generator is created with seed=None.

    • Whenever a Generator needs to participate in a server-side operation, send its seed and state over to the server. The server will create a randomStream on the fly, with the desired element type.

    This way we can remove Generator._name_dict in the client and GeneratorSymEntry on the server, as well as back out the additions in #4710 that are related to checkpointing of GeneratorSymEntrys.

    I am realizing that my recommendation to eliminate Generator._state is not appropriate unless accompanied with other changes. This is because, for example, if the same Generator is created with a given seed then used to fill an array of ints and then an array of floats, both arrays will be sourcing from the same portion of the random number sequence. This latter is because a Generator contains separate randomStream instances on the server, one for ints and another for floats. Today each randomStream is advanced to the generator's _state before it is asked to generate numbers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions