Skip to main content

Orivane

Orivane is a small Python library in alpha for making experimental changes to PyTorch models. It wraps methods with input, output, or replacement callbacks and registers patch modules with the model so they participate in device movement, training mode, checkpoints, and optimizers.

Installation

uv add orivane

Add a trainable adapter

Patch classes inherit from PatchBase and call super().__init__() so PyTorch can register modules assigned to the patch. By default, applying a patch freezes the model's existing parameters while leaving the patch's trainable parameters trainable.

import torch
import torch.nn as nn

from orivane import PatchBase, PatchType, inject, patch

model = nn.Linear(10, 5)

@patch(model=model)
class AdapterPatch(PatchBase):
    def __init__(self):
        super().__init__()
        self.adapter = nn.Sequential(
            nn.Linear(5, 16),
            nn.GELU(),
            nn.Linear(16, 5),
        )

    @inject(method="forward", at=PatchType.AFTER)
    def add_adapter(self, output):
        return output + self.adapter(output)

optimizer = torch.optim.AdamW(
    (parameter for parameter in model.parameters() if parameter.requires_grad),
    lr=1e-3,
)

x = torch.randn(32, 10)
target = torch.randn(32, 5)
loss = nn.MSELoss()(model(x), target)
optimizer.zero_grad()
loss.backward()
optimizer.step()

# Restore the original method and parameter trainability when finished.
AdapterPatch.unpatch()

The decorated class name refers to the applied patch instance. Its added modules are registered under the model, so they are included in model.parameters() and model.state_dict(), and follow model.to(...) and model.train() calls. Patch modules are moved to the device and dtype of the model's first parameter when the patch is applied.

Use train_only_added=False to leave the model's existing trainability settings unchanged:

@patch(model=model, train_only_added=False)
class FineTunePatch(PatchBase):
    def __init__(self):
        super().__init__()

Unpatching is safe to call more than once. It restores the wrapped method and the original requires_grad state of model parameters, and removes the patch module from the model.

Target a nested module

inject(method=...) accepts a method name on the model or a dotted path to a nested module method. For example, "layers.0.forward" targets the first module in a ModuleList named layers:

class BlockPatch(PatchBase):
    def __init__(self):
        super().__init__()
        self.scale = nn.Parameter(torch.tensor(0.1))

    @inject(method="layers.0.forward", at=PatchType.AFTER)
    def scale_output(self, output):
        return output * (1 + self.scale)

Apply it with @patch(model=model) as in the adapter example. BEFORE callbacks receive the method's positional and keyword arguments and may return (new_args, new_kwargs); AFTER callbacks receive and return the method result; OVERWRITE callbacks replace the method implementation.

Patch types

Patch type Behavior
PatchType.BEFORE Optionally changes the arguments before the original method runs.
PatchType.AFTER Changes the original method's result.
PatchType.OVERWRITE Replaces the original method; only one overwrite is allowed per method.

Patches operate on PyTorch modules and methods. They do not provide model-specific integrations, automatic layer discovery, distributed-training support, or quantization support.

License

See the LICENSE file for license information.

Metadata

Release files for orivane 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for orivane 0.1.0
File Size Uploaded Provenance Actions
orivane-0.1.0.tar.gz 4.7 kB Without provenance Details for orivane-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for orivane 0.1.0
File Interpreter ABI Platform Actions
orivane-0.1.0-py3-none-any.whl Python 3 none any Details for orivane-0.1.0-py3-none-any.whl

Total release size: 11.8 kB

Release files / orivane-0.1.0.tar.gz

Download URL orivane-0.1.0.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
9fc552f15f4a29cc5f3080a905c19263babe9359bb6e49320e0f94361a840758
BLAKE2b-256 checksum
How to use checksums
7e9c605ae5c796b9c9d3a1525efeaee0e23f7f6a16adee892a3599900c7f59d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.0 {"installer":{"name":"uv","version":"0.12.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / orivane-0.1.0-py3-none-any.whl

Download URL orivane-0.1.0-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c9bfaa41395814ef0b54fb4caf7958dc742f3fffd03e65e63843b0b9147bc699
BLAKE2b-256 checksum
How to use checksums
bc12d1c7232556a11bb11d7afd35b696bdbb3314dd9a100a7d6b154d529851d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.0 {"installer":{"name":"uv","version":"0.12.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for orivane 0.1.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page