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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file pytorch_seed-0.2.0.tar.gz.

File metadata

  • Download URL: pytorch_seed-0.2.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for pytorch_seed-0.2.0.tar.gz
Algorithm Hash digest
SHA256 096edd3404f8a00f3df2bab41024945806baf1f64b05678c82373b780458e1a3
MD5 6aa7cb6437f0e7ef06f0ed6169b93e07
BLAKE2b-256 deb57d1b68e7eaf16411c3e0e2707e6b0e4ff053f9765deb72a70279fd54d0a5

See more details on using hashes here.

File details

Details for the file pytorch_seed-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytorch_seed-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 50a1ee2f62e55f88c20069849aa12265a007aeaea6893f3d23ad4e40173c5c89
MD5 27794e3ee1bc379c724f7168304cb794
BLAKE2b-256 5c7b6e29f8600d0df90ffce98850130e5ac993e1e29101fa655e38f6c0f60393

See more details on using hashes here.

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