Module neuralops.tests.test_integration_end_to_end

End-to-end integration tests for neuralops modules and pipelines.

Functions

def test_attention_registry_instantiation_e2e()
Expand source code
@pytest.mark.integration
def test_attention_registry_instantiation_e2e():
    """Verify that multiple representative attention modules can be instantiated and forward passed."""
    keys = ["svd_linear_attention", "cosformer"]
    B, T, D = 2, 16, 64
    x = torch.randn(B, T, D)

    for k in keys:
        cls = ATTENTION_REGISTRY.get(k)
        assert cls is not None
        layer = cls(hidden_size=D, num_heads=4)
        out = layer(x)
        assert out is not None
        if isinstance(out, tuple):
            out = out[0]
        assert out.shape[:2] == (B, T)

    # Test full_attention with config object
    cfg = types.SimpleNamespace(hidden_size=D, num_attention_heads=4, num_heads=4, attention_dropout=0.0)
    full_cls = ATTENTION_REGISTRY.get("full_attention")
    full_layer = full_cls(cfg)
    out_full = full_layer(x)
    assert out_full is not None

Verify that multiple representative attention modules can be instantiated and forward passed.

def test_compressor_registry_instantiation_e2e()
Expand source code
@pytest.mark.integration
def test_compressor_registry_instantiation_e2e():
    """Verify that compressor classes can be retrieved from registry without error."""
    keys = ["direct_svd", "slicegpt", "optimal_subspace_projection"]
    model = DummyTransformerModel()

    for k in keys:
        cls = COMPRESSOR_REGISTRY.get(k)
        assert cls is not None

Verify that compressor classes can be retrieved from registry without error.

def test_replace_attention_distilbert_e2e()
Expand source code
@pytest.mark.integration
def test_replace_attention_distilbert_e2e():
    """Verify replace_attention surgery on DistilBERT."""
    from transformers import AutoModelForSequenceClassification

    model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased")
    model = replace_attention(model, "svd_linear_attention", svd_rank=64)
    x = torch.randint(0, 100, (1, 32))
    out = model(x)
    assert out.logits.shape == (1, 2)

Verify replace_attention surgery on DistilBERT.

Classes

class DummyTransformerModel (hidden_dim: int = 64)
Expand source code
class DummyTransformerModel(nn.Module):
    def __init__(self, hidden_dim: int = 64):
        super().__init__()
        self.embedding = nn.Embedding(100, hidden_dim)
        self.linear_in = nn.Linear(hidden_dim, hidden_dim)
        self.linear_out = nn.Linear(hidden_dim, 2)

    def forward(self, input_ids: torch.Tensor) -> torch.Tensor:
        x = self.embedding(input_ids)
        x = self.linear_in(x)
        return self.linear_out(x[:, 0, :])

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, input_ids: torch.Tensor) ‑> torch.Tensor
Expand source code
def forward(self, input_ids: torch.Tensor) -> torch.Tensor:
    x = self.embedding(input_ids)
    x = self.linear_in(x)
    return self.linear_out(x[:, 0, :])

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.