Fast Walsh–Hadamard Transform (FWHT) in PyTorch
Project description
hadamard-transform
A Fast Walsh–Hadamard Transform (FWHT) implementation in PyTorch.
Installation
Install this library using pip
:
pip install hadamard-transform
Usage
For the Basic normalized fast Walsh–Hadamard transform, use:
import torch
from hadamard_transform import hadamard_transform
x = torch.rand(2 ** 10, dtype=torch.float64)
y = hadamard_transform(x)
assert torch.allclose(
hadamard_transform(y),
x
)
Since the transform is not numerically-stable, it is recommended to use float64
when possible.
The input is either a vector or a batch of vectors where the first dimension is the batch dimension. Each vector's length is expected to be a power of 2!
This package also includes a pad_to_power_of_2
util, which appends zeros up to the next power of 2 if needed.
In some common cases, we use the randomized Hadamard transform, which randomly flips the axes:
import torch
from hadamard_transform import randomized_hadamard_transform, inverse_randomized_hadamard_transform
prng = torch.Generator(device='cpu')
x = torch.rand(2 ** 10, dtype=torch.float64)
seed = prng.seed()
y = randomized_hadamard_transform(x, prng),
assert torch.allclose(
inverse_randomized_hadamard_transform(y, prng.manual_seed(seed)),
x)
This package also includes hadamard_transform_
, randomized_hadamard_transform_
, and inverse_randomized_hadamard_transform_
. These are in-place implementations of the previous methods. They can be useful when approaching memory limits.
See additional usage examples in tests/test_hadamard_transform.py
.
Development
To contribute to this library, first checkout the code. Then create a new virtual environment:
cd hadamard-transform
python -m venv venv
source venv/bin/activate
Now install the dependencies and test dependencies:
pip install -e ".[test]"
To run the tests:
pytest
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file hadamard-transform-0.1.3.tar.gz
.
File metadata
- Download URL: hadamard-transform-0.1.3.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fd2d926f665e0c09ef2aabd739b92738fecb29b977ec2e7c980db567b67db86c |
|
MD5 | 7685fdd397e7137a95b9572df5c189e9 |
|
BLAKE2b-256 | 193d184c5a5ba31151809e5aa3ca6b282b59fd95aa569ee47d456adc387ae76a |
Provenance
File details
Details for the file hadamard_transform-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: hadamard_transform-0.1.3-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36be3723f0f59abd4e2943d68c8d812d2069d18882c0d40180bf5d0dda65d628 |
|
MD5 | 0d59c6822f9498a6f35134d061dc618d |
|
BLAKE2b-256 | 92bf44145d8106e61c2d00e1297c5ef1b4b015dbc3a2da14cea57e1edcac73a1 |