opentau.utils.monkey_patch
Monkey patches to inject behaviour into computational graph construction. This eliminates the need to trace and modify source code of 3rd party libraries, such as transformers or even PyTorch itself.
Where necessary, we can do
>>> from opentau.utils.monkey_patch import torch_cumsum_patch, torch_pow_patch
>>> torch_cumsum_patch() # Apply the patch to handle bool tensors in cumsum
>>> torch_pow_patch() # Apply the patch to handle mixed number-tensor exponentiation in pow
to apply the patches.
Running the same monkey patch twice will not have any effect, as the 2nd call will return immediately.
Note: currently, there is no way to undo a monkey patch once it has been applied, which can be a future to-do. For now, only apply the patch when its implications are understood.
Functions
Monkey patch to make import gym equivalent to import gymnasium as gym. |
|
Get a list of all patches that have been applied. |
|
Override torch.cumsum to handle bool tensors correctly. |
|
Fix torch.distributions.Beta to work in FakeTensorMode. |
|
Patch torch.isinf to work with FakeTensor. |
|
Prefer overwriting (not in-place updating) module params on .to() conversion. |
|
Enable .numpy() calls on FakeTensor to return random numpy arrays. |
|
Override torch.full to convert bool fill values to int. |
|
Override torch.load to handle weights_only argument. |
|
Override torch.pow to ensure both base and exponent are tensors. |
- opentau.utils.monkey_patch.gym_is_gymnasium_patch()[source]
Monkey patch to make import gym equivalent to import gymnasium as gym.
This patch is necessary because the original gym package is incompatible with numpy >= 2.0. It redirects gym imports to use gymnasium instead.
- opentau.utils.monkey_patch.patches_applied()[source]
Get a list of all patches that have been applied.
- Returns:
List of patch function names that have been applied.
- opentau.utils.monkey_patch.torch_cumsum_patch()[source]
Override torch.cumsum to handle bool tensors correctly.
PyTorch allows cumsum on bool tensors, but ONNX Runtime does not. This patch converts bool tensors to int64 before calling cumsum.
- opentau.utils.monkey_patch.torch_fake_tensor_beta_validate_args_patch()[source]
Fix torch.distributions.Beta to work in FakeTensorMode.
This patch sets validate_args=False by default for Beta distributions, which is required for FakeTensorMode compatibility.
- opentau.utils.monkey_patch.torch_fake_tensor_is_inf_patch()[source]
Patch torch.isinf to work with FakeTensor.
This patch provides a mock implementation of torch.isinf that returns a mock object compatible with FakeTensor operations.
- opentau.utils.monkey_patch.torch_fake_tensor_module_to_patch()[source]
Prefer overwriting (not in-place updating) module params on .to() conversion.
Historically this flag made
Module.to(device)behave sensibly in FakeTensorMode (see https://github.com/pytorch/pytorch/issues/119665). On modern torch (confirmed on the locked 2.10) it is no longer sufficient:nn.Module._applyhardcodes thetorch.utils.swap_tensorspath for anyFakeTensorparam (... or isinstance(param, FakeTensor)), andswap_tensorsrejects them becauseFakeTensorModetracks them with weakrefs — neither this flag norset_swap_module_params_on_conversioncan steer around it. So the supported pattern is to construct modules directly on the target device inside FakeTensorMode (nn.Linear(..., device=device)) rather than building on CPU and calling.to(device)afterwards.The flag is retained as a harmless, defensive default (it is a no-op for FakeTensor params); it only affects the now-unused
Module._applyconversion path.
- opentau.utils.monkey_patch.torch_fake_tensor_to_numpy_patch()[source]
Enable .numpy() calls on FakeTensor to return random numpy arrays.
This patch allows .numpy() to be called on FakeTensor instances, returning numpy arrays with random values. Note that calling .numpy() multiple times on the same FakeTensor may return different values.
- opentau.utils.monkey_patch.torch_full_patch()[source]
Override torch.full to convert bool fill values to int.
This patch ensures that True/False are converted to 1/0 before reaching the C++ level, improving compatibility with certain backends.