Cryptographically secure pseudorandom number generators for PyTorch
Project description
PyTorch/CSPRNG
torchcsprng is a PyTorch C++/CUDA extension that provides cryptographically secure pseudorandom number generators for PyTorch.
Design
torchcsprng generates a random 128-bit key on CPU using one of its generators and runs AES128 in CTR mode either on CPU or on GPU using CUDA to generate a random 128 bit state and apply a transformation function to map it to target tensor values. This approach is based on Parallel Random Numbers: As Easy as 1, 2, 3(John K. Salmon, Mark A. Moraes, Ron O. Dror, and David E. Shaw, D. E. Shaw Research). It makes torchcsprng both crypto-secure and parallel on CUDA and CPU.
Advantages:
- The user can choose either seed-based(for testing) or random device based(fully crypto-secure) generators
- One generator instance for both CPU and CUDA tensors(because the encryption key is always generated on CPU)
- CPU random number generation is also parallel(unlike the default PyTorch CPU generator)
Features
torchcsprng exposes two methods to create crypto-secure and non-crypto-secure PRNGs:
Method to create PRNG | Is crypto-secure? | Has seed? | Underlying implementation |
---|---|---|---|
create_random_device_generator(token: string=None) | yes | no | See std::random_device and its constructor. The implementation in libstdc++ expects token to name the source of random bytes. Possible token values include "default", "rand_s", "rdseed", "rdrand", "rdrnd", "/dev/urandom", "/dev/random", "mt19937", and integer string specifying the seed of the mt19937 engine. (Token values other than "default" are only valid for certain targets.) If token=None then constructs a new std::random_device object with an implementation-defined token. |
create_mt19937_generator(seed: int=None) | no | yes | See std::mt19937 and its constructor. Constructs a mersenne_twister_engine object, and initializes its internal state sequence to pseudo-random values. If seed=None then seeds the engine with default_seed. |
The following list of methods supports all forementioned PRNGs:
Kernel | CUDA | CPU |
---|---|---|
random_() | yes | yes |
random_(to) | yes | yes |
random_(from, to) | yes | yes |
uniform_(from, to) | yes | yes |
normal_(mean, std) | yes | yes |
cauchy_(median, sigma) | yes | yes |
log_normal_(mean, std) | yes | yes |
geometric_(p) | yes | yes |
exponential_(lambda) | yes | yes |
randperm(n) | yes* | yes |
- the calculations are done on CPU and the result is copied to CUDA
Installation
CSPRNG works with Python 3.6/3.7/3.8 on the following operating systems and can be used with PyTorch tensors on the following devices:
Tensor Device Type | Linux | macOS | MS Window |
---|---|---|---|
CPU | Supported | Supported | Supported |
CUDA | Supported | Not Supported | Coming |
Binary Installation
Anaconda:
OS | CUDA | |
---|---|---|
Linux | 9.2 10.1 10.2 None |
conda install torchcsprng cudatoolkit=9.2 -c pytorch conda install torchcsprng cudatoolkit=10.1 -c pytorch conda install torchcsprng cudatoolkit=10.2 -c pytorch conda install torchcsprng cpuonly -c pytorch |
macOS | None | conda install torchcsprng cpuonly -c pytorch |
Windows | None | conda install torchcsprng cpuonly -c pytorch |
pip:
OS | CUDA | |
---|---|---|
Linux | 9.2 10.1 10.2 None |
pip install torchcsprng==0.1.2+cu92 torch==1.6.0+cu92 -f https://download.pytorch.org/whl/torch_stable.html pip install torchcsprng==0.1.2+cu101 torch==1.6.0+cu101 -f https://download.pytorch.org/whl/torch_stable.html pip install torchcsprng torch pip install torchcsprng==0.1.2+cpu torch==1.6.0+cpu -f https://download.pytorch.org/whl/torch_stable.html |
macOS | None | pip install torchcsprng torch |
Windows | None | pip install torchcsprng torch -f https://download.pytorch.org/whl/torch_stable.html |
Nightly builds:
Anaconda:
OS | CUDA | |
---|---|---|
Linux | 9.2 10.1 10.2 None |
conda install torchcsprng cudatoolkit=9.2 -c pytorch-nightly conda install torchcsprng cudatoolkit=10.1 -c pytorch-nightly conda install torchcsprng cudatoolkit=10.2 -c pytorch-nightly conda install torchcsprng cpuonly -c pytorch-nightly |
macOS | None | conda install torchcsprng cpuonly -c pytorch-nightly |
Windows | None | conda install torchcsprng cpuonly -c pytorch-nightly |
pip:
OS | CUDA | |
---|---|---|
Linux | 9.2 10.1 10.2 None |
pip install --pre torchcsprng -f https://download.pytorch.org/whl/nightly/cu92/torch_nightly.html pip install --pre torchcsprng -f https://download.pytorch.org/whl/nightly/cu101/torch_nightly.html pip install --pre torchcsprng -f https://download.pytorch.org/whl/nightly/cu102/torch_nightly.html pip install --pre torchcsprng -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html |
macOS | None | pip install --pre torchcsprng -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html |
Windows | None | pip install --pre torchcsprng -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html |
From Source
torchcsprng is a Python C++/CUDA extension that depends on PyTorch. In order to build CSPRNG from source it is required to have Python(>=3.6) with PyTorch(>=1.6.0) installed and C++ compiler(gcc/clang for Linux, XCode for macOS, Visual Studio for MS Windows). To build torchcsprng you can run the following:
python setup.py install
By default, GPU support is built if CUDA is found and torch.cuda.is_available() is True. Additionally, it is possible to force building GPU support by setting the FORCE_CUDA=1 environment variable, which is useful when building a docker image.
Getting Started
The torchcsprng API is available in torchcsprng
module:
import torch
import torchcsprng as csprng
Create crypto-secure PRNG from /dev/urandom:
urandom_gen = csprng.create_random_device_generator('/dev/urandom')
Create empty boolean tensor on CUDA and initialize it with random values from urandom_gen:
torch.empty(10, dtype=torch.bool, device='cuda').random_(generator=urandom_gen)
tensor([ True, False, False, True, False, False, False, True, False, False],
device='cuda:0')
Create empty int16 tensor on CUDA and initialize it with random values in range [0, 100) from urandom_gen:
torch.empty(10, dtype=torch.int16, device='cuda').random_(100, generator=urandom_gen)
tensor([59, 20, 68, 51, 18, 37, 7, 54, 74, 85], device='cuda:0',
dtype=torch.int16)
Create non-crypto-secure MT19937 PRNG:
mt19937_gen = csprng.create_mt19937_generator()
torch.empty(10, dtype=torch.int64, device='cuda').random_(torch.iinfo(torch.int64).min, to=None, generator=mt19937_gen)
tensor([-7584783661268263470, 2477984957619728163, -3472586837228887516,
-5174704429717287072, 4125764479102447192, -4763846282056057972,
-182922600982469112, -498242863868415842, 728545841957750221,
7740902737283645074], device='cuda:0')
Create crypto-secure PRNG from default random device:
default_device_gen = csprng.create_random_device_generator()
torch.randn(10, device='cuda', generator=default_device_gen)
tensor([ 1.2885, 0.3240, -1.1813, 0.8629, 0.5714, 2.3720, -0.5627, -0.5551,
-0.6304, 0.1090], device='cuda:0')
Create non-crypto-secure MT19937 PRNG with seed:
mt19937_gen = csprng.create_mt19937_generator(42)
torch.empty(10, device='cuda').geometric_(p=0.2, generator=mt19937_gen)
tensor([ 7., 1., 8., 1., 11., 3., 1., 1., 5., 10.], device='cuda:0')
Recreate MT19937 PRNG with the same seed:
mt19937_gen = csprng.create_mt19937_generator(42)
torch.empty(10, device='cuda').geometric_(p=0.2, generator=mt19937_gen)
tensor([ 7., 1., 8., 1., 11., 3., 1., 1., 5., 10.], device='cuda:0')
Contributing
We appreciate all contributions. If you are planning to contribute back bug-fixes, please do so without any further discussion. If you plan to contribute new features, utility functions or extensions, please first open an issue and discuss the feature with us.
License
torchcsprng is BSD 3-clause licensed. See the license file here
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for torchcsprng-0.2.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 47df75ea3b098614e623ca712a12de5a07db90887ab96c0655ed64675be960c7 |
|
MD5 | 0434a205a39b9d9d38b4bc7039b93a4c |
|
BLAKE2b-256 | 96255b7075ee2d331788d43cd802a29ded982a554005d230a6772053ccfc03cc |
Hashes for torchcsprng-0.2.0-cp39-cp39-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dcf06ee0ad5ef94a141f069a84962a9e0b246f7af478dd60c76b586643045131 |
|
MD5 | e280ef8603ca2226ddf2e3f4012bc52d |
|
BLAKE2b-256 | 19efa2c96d2d97fb24ebd206cbf4cfd1625e47efbd413252faf81fb5aacc39fd |
Hashes for torchcsprng-0.2.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3159b85e058e40f245865e4173ec0b60577e298086fbef2c2c48f61b1e2e5fee |
|
MD5 | 90d3e9e5e5ffa04d16e20ecf3ce46fd0 |
|
BLAKE2b-256 | 551a92e6f752dd12d48e30e01de3597f61fc4d3eb850541aba5548bfb65282df |
Hashes for torchcsprng-0.2.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ebf9607305bad89496cd740ae5d6c521c67a7595853b16c1a2c3ed250041d1d |
|
MD5 | 2139d0362d07415cd910bc99cc2beb34 |
|
BLAKE2b-256 | ffc7caa8a43519c1725f393e280696bbe6b2c1f2a7cda17535c88e74adb788f9 |
Hashes for torchcsprng-0.2.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 20462af73c7efbf5084f1647c5a88ba318f641bd3b7047017217fb5504180d83 |
|
MD5 | c7f6c17a010d2951f3411115df050e8c |
|
BLAKE2b-256 | ab89681ba772eecbd584dd980308c39b91789b936ae2c8dac10bc1f841a5b845 |
Hashes for torchcsprng-0.2.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ae988164c95e45d41bdae21bb1a941a2ab7cbe8df31a3592a97bd6d0fbab2e0 |
|
MD5 | 4d77c7e13aa5e0778c21386de0a1b2ce |
|
BLAKE2b-256 | 0adeed09092af964bc516e543475c485dc951ab0784213808352a2029c235c3d |
Hashes for torchcsprng-0.2.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e8a5c5f453f01d231f8db182b2af69317ed508bf53f8f6bcbf70c133c34bcdce |
|
MD5 | 7caed736bd07ef5429aaf283db96c824 |
|
BLAKE2b-256 | 1f9509b9676a9244352a7fe8f681ab3abd20003cab72c14fe9d8d2317e2ee0c9 |
Hashes for torchcsprng-0.2.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb008c2c521ae2fac6e992b0f6c8c848eaf16a96df03b0e948ba62958ef69982 |
|
MD5 | bf7e3dfa930f467228346cf6e01e7ab6 |
|
BLAKE2b-256 | d5479cbf16af509cda234c901ca542060a39902611e1ec068dffe01398ee10ab |
Hashes for torchcsprng-0.2.0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 90a2ea833f92b44f1d8a4364df622aa851712e54b8fcc500e8a47d2286c59045 |
|
MD5 | 62aa816f65650a3595d3bbfd86db0a7a |
|
BLAKE2b-256 | 6c8fead45fd4b85120cef9c656621ce3e0545f3fd5ade8e9832feed73a943e65 |
Hashes for torchcsprng-0.2.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88fff87b56ad421569f20aa8e493f505d5811c85260bf06a4e1700584bb34c5a |
|
MD5 | 677ac3dbe3b8e9c905d405a1fdfdcbf0 |
|
BLAKE2b-256 | c1a397654190874d46aab0dc4a651a4aabc1e3386cf63c14e38e3dfaa88521fb |
Hashes for torchcsprng-0.2.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3f6970cd522ea5db689db68564e6bd1c8b80b209bc51b894abb7c38f67b048e |
|
MD5 | 31d76cb32349884c5819877f60c3f1d5 |
|
BLAKE2b-256 | bb5f396a4ae548ac61a57089bfb70d229c1e0159e29b5fc5485d5b7a4c063a90 |
Hashes for torchcsprng-0.2.0-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5140a0eaefde98d60c5973ace6817b5d12acc35d4dab2c0f3dc39e67d05011cc |
|
MD5 | 13f5908117206a2fc701557ce9eecf6b |
|
BLAKE2b-256 | 77ab98ad7c6da01f668689800ed226b627b5337ff997c3fe15148bce2a753989 |