Module neuralops.tests.test_dobi_svd_comprehensive

Functions

def test_dobi_compress_conv1d()
Expand source code
def test_dobi_compress_conv1d():
    class ModelWithConv1D(nn.Module):
        def __init__(self):
            super().__init__()
            self.c_attn = DummyConv1D(nf=192, nx=64)
            self.c_proj = DummyConv1D(nf=64, nx=64)

        def forward(self, x):
            return self.c_proj(self.c_attn(x))

    model = ModelWithConv1D()
    with torch.no_grad():
        # Low rank Conv1D weights: rank 8
        a1 = torch.randn(64, 8)
        b1 = torch.randn(8, 192)
        model.c_attn.weight.copy_(torch.matmul(a1, b1))
        a2 = torch.randn(64, 8)
        b2 = torch.randn(8, 64)
        model.c_proj.weight.copy_(torch.matmul(a2, b2))

    compressor = DobiSVDCompressor(model, rank=16, target_attention_only=True)
    compressed_model, stats = compressor.compress()
    assert isinstance(compressed_model.c_attn, DobiSVDLinearLayer)
    assert isinstance(compressed_model.c_proj, DobiSVDLinearLayer)
    x = torch.randn(2, 8, 64)
    out = compressed_model(x)
    assert out.shape == (2, 8, 64)
def test_dobi_compressor_rank_cap_128()
Expand source code
def test_dobi_compressor_rank_cap_128():
    linear = nn.Linear(512, 512)
    compressor = DobiSVDCompressor(linear, rank=300)
    # Target rank was requested as 300, but max_r must cap it at 128
    r = compressor._determine_rank(512, 512, 300)
    assert r <= 128
def test_dobi_compressor_target_attention_only()
Expand source code
def test_dobi_compressor_target_attention_only():
    model = DummyAttentionModel(d=64)
    # Initialize weights with low-rank structure so SVD reconstruction error is low
    with torch.no_grad():
        for mod in (model.query, model.key, model.value, model.out_proj):
            a = torch.randn(64, 8)
            b = torch.randn(8, 64)
            mod.weight.copy_(torch.matmul(a, b))

    compressor = DobiSVDCompressor(model, rank=16, target_attention_only=True)
    compressed_model, stats = compressor.compress()
    # Attention projections should be compressed
    assert isinstance(compressed_model.query, DobiSVDLinearLayer)
    assert isinstance(compressed_model.key, DobiSVDLinearLayer)
    assert isinstance(compressed_model.value, DobiSVDLinearLayer)
    assert isinstance(compressed_model.out_proj, DobiSVDLinearLayer)
    # FFN and classifier should NOT be compressed
    assert not isinstance(compressed_model.ffn, DobiSVDLinearLayer)
    assert not isinstance(compressed_model.classifier, DobiSVDLinearLayer)
def test_dobi_factory_and_inject()
Expand source code
def test_dobi_factory_and_inject():
    model = DummyAttentionModel(d=64)
    res = dobi_factory(model, rank=8)
    assert res is not None

    single_linear = nn.Linear(64, 64)
    injected = inject_dobi(single_linear, rank=8)
    assert injected is not None
def test_dobi_fallback_on_high_error()
Expand source code
def test_dobi_fallback_on_high_error():
    model = DummyAttentionModel(d=64)
    # With full rank random weights and max_error=0.01, it must fall back to original
    compressor = DobiSVDCompressor(model, rank=4, max_error=0.01, fallback_to_original=True)
    compressed_model, stats = compressor.compress()
    assert not isinstance(compressed_model.query, DobiSVDLinearLayer)
    assert len(stats["fallback_layers"]) > 0
def test_dobi_layer_backward_gradients()
Expand source code
def test_dobi_layer_backward_gradients():
    layer = DobiSVDLinearLayer(in_features=64, out_features=64, rank=16)
    x = torch.randn(2, 8, 64, requires_grad=True)
    out = layer(x)
    loss = out.sum()
    loss.backward()
    assert layer.U.grad is not None
    assert layer.V.grad is not None
    assert layer.gate.grad is not None
    assert layer.bias.grad is not None
def test_dobi_layer_basic_and_no_output_scale()
Expand source code
def test_dobi_layer_basic_and_no_output_scale():
    layer = DobiSVDLinearLayer(in_features=64, out_features=128, rank=16)
    assert not hasattr(layer, "output_scale")
    assert not hasattr(layer, "__iter__") or type(layer).__iter__ is nn.Module.__iter__
    x = torch.randn(2, 8, 64)
    out = layer(x)
    assert out.shape == (2, 8, 128)

Classes

class DummyAttentionModel (d=64)
Expand source code
class DummyAttentionModel(nn.Module):
    def __init__(self, d=64):
        super().__init__()
        self.query = nn.Linear(d, d)
        self.key = nn.Linear(d, d)
        self.value = nn.Linear(d, d)
        self.out_proj = nn.Linear(d, d)
        self.ffn = nn.Linear(d, 4 * d)
        self.classifier = nn.Linear(d, 2)

    def forward(self, x):
        q = self.query(x)
        k = self.key(x)
        v = self.value(x)
        attn = torch.matmul(q, k.transpose(-1, -2)) / 8.0
        out = torch.matmul(torch.softmax(attn, dim=-1), v)
        out = self.out_proj(out)
        return self.classifier(out)

Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes::

import torch.nn as nn
import torch.nn.functional as F


class Model(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will also have their parameters converted when you call :meth:to, etc.

Note

As per the example above, an __init__() call to the parent class must be made before assignment on the child.

:ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Ancestors

  • torch.nn.modules.module.Module

Methods

def forward(self, x) ‑> Callable[..., typing.Any]
Expand source code
def forward(self, x):
    q = self.query(x)
    k = self.key(x)
    v = self.value(x)
    attn = torch.matmul(q, k.transpose(-1, -2)) / 8.0
    out = torch.matmul(torch.softmax(attn, dim=-1), v)
    out = self.out_proj(out)
    return self.classifier(out)

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the :class:Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class DummyConv1D (nf, nx)
Expand source code
class DummyConv1D(nn.Module):
    def __init__(self, nf, nx):
        super().__init__()
        self.nf = nf
        self.nx = nx
        self.weight = nn.Parameter(torch.randn(nx, nf) * 0.02)
        self.bias = nn.Parameter(torch.zeros(nf))

    def forward(self, x):
        size_out = x.size()[:-1] + (self.nf,)
        x = torch.addmm(self.bias, x.view(-1, x.size(-1)), self.weight)
        return x.view(size_out)

Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes::

import torch.nn as nn
import torch.nn.functional as F


class Model(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will also have their parameters converted when you call :meth:to, etc.

Note

As per the example above, an __init__() call to the parent class must be made before assignment on the child.

:ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Ancestors

  • torch.nn.modules.module.Module

Methods

def forward(self, x) ‑> Callable[..., typing.Any]
Expand source code
def forward(self, x):
    size_out = x.size()[:-1] + (self.nf,)
    x = torch.addmm(self.bias, x.view(-1, x.size(-1)), self.weight)
    return x.view(size_out)

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the :class:Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.