hypothesis-torch
Hypothesis strategies for various Pytorch structures (including tensors and modules).
Hypothesis is a powerful property-based testing library for Python. It lacks built-in support for Pytorch tensors and modules, so this library provides strategies for generating them.
Installation
hypothesis-torch can be installed via pip:
pip install hypothesis-torch
Optionally, you can also install the huggingface extra to also install the transformers library:
pip install hypothesis-torch[huggingface]
Strategies for generating Hugging Face transformer models are provided in the hypothesis_torch.huggingface module. If
and only if transformers is installed when hypothesis-torch is imported, these strategies will be available from
the root hypothesis_torch module.
What you can generate
Tensors
Tensors can be generated with the tensor_strategy function. This function takes in optional arguments for the shape,
dtype, device, and other properties of the desired tensors. Each property can be specified as a fixed value or as a
strategy. For example, to generate a tensor with a shape of 3x3, a dtype of torch.float32, and values between 0 and 1,
import hypothesis_torch
from hypothesis import strategies as st
import torch
hypothesis_torch.tensor_strategy(dtype=torch.float32, shape=(3, 3), elements=st.floats(0, 1))
Note that specifying other hypothesis strategies that return the same type as an argument will sample from that strategy while generating the tensor. For example, to generate a tensor with any dtype, specify a strategy that returns a dtype:
import hypothesis_torch
from hypothesis import strategies as st
import torch
hypothesis_torch.tensor_strategy(dtype=st.sampled_from([torch.float32, torch.float64]), shape=(3, 3), elements=st.floats(0, 1))
Dtypes
Dtypes can be generated with the dtype_strategy function. If no arguments are provided, this function will default to
sampling from the set of all Pytorch dtypes.
import hypothesis_torch
hypothesis_torch.dtype_strategy()
If a set of dtypes is provided, the function will sample from that set.
import hypothesis_torch
import torch
hypothesis_torch.dtype_strategy(dtypes={torch.float32, torch.float64})
Devices
Devices can be generated with the device_strategy function. If no arguments are provided, this function will default to
sampling from the set of all available, physical devices.
import hypothesis_torch
hypothesis_torch.device_strategy()
If a set of devices is provided, the function will sample from that set.
import hypothesis_torch
import torch
hypothesis_torch.device_strategy(devices={torch.device('cuda:0'), torch.device('cpu')})
If allow_meta_device is set to True, the strategy may also return meta devices, i.e. torch.device('meta').
import hypothesis_torch
hypothesis_torch.device_strategy(allow_meta_device=True)
Modules
Various types of PyTorch modules have their own strategies.
Activation functions
Activation functions can be generated with the same_shape_activation_strategy function.
import hypothesis_torch
hypothesis_torch.same_shape_activation_strategy()
Fully-connected/Feed forward neural networks
Fully-connected neural networks can be generated with the linear_network_strategy function. This function takes in
optional arguments for the input size, output size, and number of hidden layers. Each of these arguments can be
specified as a fixed value or as a strategy. For example, to generate a fully-connected neural network with an input
size of 10, an output size of 5, and 3 hidden layers with sizes between 5 and 10:
import hypothesis_torch
from hypothesis import strategies as st
hypothesis_torch.linear_network_strategy(input_shape=(1,10), output_shape=(1,5), hidden_layer_size=st.integers(5, 10), num_hidden_layers=3)
Hugging Face Transformer Models
Hugging Face transformer models can be generated with the transformer_strategy function. This function takes in any
Hugging Face PreTrainedModel subclass (or a strategy that generates references PreTrainedModel subclasses) and
returns an instance of that model. For example, to generate an arbitrary Llama2 model:
import hypothesis_torch
import transformers
hypothesis_torch.transformer_strategy(transformers.LlamaForCausalLM)
The strategy also accepts kwargs to pass to the model constructor. These can be either fixed values or strategies to
generate those corresponding values. For example, to generate an arbitrary Llama2 model with a hidden size between 64 and
128, but a fixed vocabulary size of 1000:
import hypothesis_torch
import transformers
from hypothesis import strategies as st
hypothesis_torch.transformer_strategy(transformers.LlamaForCausalLM, hidden_size=st.integers(64, 128), vocab_size=1000)
[! Note]
Currently, the transformer_strategy only accepts kwargs that can be passed to the constructor of the model's
config class. Thus, it cannot currently replicate all the behavior of calling from_pretrained on a model class.
Metadata
Release files for hypothesis-torch 2.0.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| hypothesis_torch-2.0.7.tar.gz | 22.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| hypothesis_torch-2.0.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 47.2 kB
Release files / hypothesis_torch-2.0.7.tar.gz
| Download URL | hypothesis_torch-2.0.7.tar.gz |
|---|---|
| Size | 22.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d31cdd491824c4dcf907a54955458a73a17731e5b2aa1ac1d3034a646bd13e86
|
|
BLAKE2b-256 checksum How to use checksums |
819dc472b7cabe7537aff020fef4dd90bf139309183f74640ec240c498c601cc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 23, 2026.
Transparency logRelease files / hypothesis_torch-2.0.7-py3-none-any.whl
| Download URL | hypothesis_torch-2.0.7-py3-none-any.whl |
|---|---|
| Size | 24.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
449b01bf2b2d7c6e8ca24c349426f194a3241322d2f97ce705f6f8a701f7bd0c
|
|
BLAKE2b-256 checksum How to use checksums |
d7807213a6f3753a21ecac71c1af8742b2bef15dd6ef52526f7f65adea8e6fb0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 23, 2026.
Transparency log