torch_patches
torch_patches
¶
In-memory compatibility patches for torchao version-specific issues.
The patches rewrite torchao module source as it is imported, through a
meta-path finder registered by register_torchao_patches. Nothing is
written to site-packages: other processes and projects sharing the
environment see pristine torchao, read-only installs work, and
uninstalling versatil leaves no trace.
SourcePatch
dataclass
¶
One source-fragment replacement applied at module load time.
Attributes:
| Name | Type | Description |
|---|---|---|
original |
str
|
Source fragment the installed torchao version contains. |
replacement |
str
|
Fragment compiled in its place. |
required |
bool
|
Whether the module must contain either fragment. Required patches raise on unknown torchao source because their absence means silently wrong behavior; optional patches disable crashing statements, so their absence means upstream already removed them. |
PatchingSourceLoader
¶
Bases: SourceFileLoader
Source loader applying string replacements before compilation.
Initialize the loader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fullname
|
str
|
Fully qualified module name. |
required |
path
|
str
|
Filesystem path of the module source. |
required |
patches
|
list[SourcePatch]
|
Replacements to apply to the module source. |
required |
Source code in src/versatil/quantization/torch_patches.py
get_code
¶
Compile the patched source, bypassing the bytecode cache.
Reading the cache could load unpatched bytecode; writing it would leak patched bytecode to imports that bypass the hook.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fullname
|
str
|
Fully qualified module name. |
required |
Source code in src/versatil/quantization/torch_patches.py
source_to_code
¶
Apply the replacements and compile.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
Raw module source bytes. |
required |
path
|
str
|
Filesystem path of the module source. |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the source contains neither the original nor the patched fragment, meaning the installed torchao version is not the one these patches target. |
Source code in src/versatil/quantization/torch_patches.py
TorchaoPatchFinder
¶
Bases: MetaPathFinder
Meta-path finder routing patched torchao modules through the loader.
Initialize the finder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module_patches
|
dict[str, list[SourcePatch]]
|
Mapping from module name to its source patches. |
required |
Source code in src/versatil/quantization/torch_patches.py
find_spec
¶
Return a patched spec for targeted modules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fullname
|
str
|
Fully qualified module name being imported. |
required |
path
|
list[str] | None
|
Parent package search path from the import system. |
None
|
target
|
object
|
Existing module for reloads, unused. |
None
|
Source code in src/versatil/quantization/torch_patches.py
register_torchao_patches
¶
Install the import hook applying the torchao compatibility patches.
Idempotent. Must run before torchao.quantization is imported;
targeted modules that are already imported cannot be patched in place
and are reported with a warning.