Skip to main content

RNG seeding and context management for pytorch

Project description

Requirements

  • PyTorch (1.0+)
  • python 3

Installation

pip install pytorch-seed

You can also install in editable mode with python3 -m pip install -e . so that modifications in the repository are automatically synced with the installed library.

Usage

Similar to pytorch lightning's seed_everything, we have

import pytorch_seed
pytorch_seed.seed(123)

which will seed python's base RNG, numpy's RNG, torch's CPU RNG, and all CUDA RNGs.

Also similar to pytorch lightning's isolate_rng context manager, we have

import torch
import pytorch_seed

pytorch_seed.seed(1)
with pytorch_seed.SavedRNG():
    print(torch.rand(1)) # tensor([0.7576])
print(torch.rand(1)) # tensor([0.7576])

They can also be used to maintain independent RNG streams:

import torch
import pytorch_seed

rng_1 = pytorch_seed.SavedRNG(1) # start the RNG stream with seed 1
rng_2 = pytorch_seed.SavedRNG(2)

with rng_1:
    # does not affect, nor is affected by the global RNG and rng_2
    print(torch.rand(1)) # tensor([0.7576])

with rng_2:
    print(torch.rand(1)) # tensor([0.6147])

torch.rand(1) # modify the global RNG state

with rng_1:
    # resumes from the last context
    print(torch.rand(1)) # tensor([0.2793])

with rng_2:
    print(torch.rand(1)) # tensor([0.3810])
    
# confirm those streams are the uninterrupted ones
pytorch_seed.seed(1)
torch.rand(2) # tensor([0.7576, 0.2793])

pytorch_seed.seed(2)
torch.rand(2) # tensor([0.6147, 0.3810])

Testing

Install pytest if you don't have it, then run

py.test

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytorch_seed-0.2.0.tar.gz (5.2 kB view hashes)

Uploaded Source

Built Distribution

pytorch_seed-0.2.0-py3-none-any.whl (4.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page