Metadata-Version: 2.1
Name: pytorch-seed
Version: 0.2.0
Summary: RNG seeding and context management for pytorch
Author-email: Sheng Zhong <zhsh@umich.edu>
Maintainer-email: Sheng Zhong <zhsh@umich.edu>
License: Copyright (c) 2023 University of Michigan ARM Lab
        
        Permission is hereby granted, free of charge, to any person obtaining a copy of
        this software and associated documentation files (the "Software"), to deal in
        the Software without restriction, including without limitation the rights to
        use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
        of the Software, and to permit persons to whom the Software is furnished to do
        so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
        
Project-URL: Homepage, https://github.com/UM-ARM-Lab/pytorch_seed
Project-URL: Bug Reports, https://github.com/UM-ARM-Lab/pytorch_seed/issues
Project-URL: Source, https://github.com/UM-ARM-Lab/pytorch_seed
Keywords: rng,pytorch,seeding,reproducibility
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Requires-Python: >=3.6
Description-Content-Type: text/markdown
Provides-Extra: test
License-File: LICENSE.txt

## Requirements
- PyTorch (1.0+)
- python 3

## Installation 
```shell
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](https://pytorch-lightning.readthedocs.io/en/stable/api/pytorch_lightning.utilities.seed.html),
we have
```python
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
```python
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:
```python
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
```
