Module neuralops.tests.test_inference_engine

Tests for Phase 6 Inference engine features.

Functions

def test_beam_search_generate()
Expand source code
def test_beam_search_generate():
    model = DummyLLM()
    ids = torch.tensor([[1, 2, 3]])
    out = beam_search_generate(model, ids, beam_width=2, max_new_tokens=3)
    assert out.shape == (1, 6)
def test_continuous_batcher()
Expand source code
def test_continuous_batcher():
    model = DummyLLM()
    batcher = ContinuousBatcher(max_batch_size=4)
    batcher.add_request("req1", torch.tensor([[1, 2]]))
    batcher.add_request("req2", torch.tensor([[3, 4]]))

    res = batcher.step(model)
    assert len(res) == 2
    assert res[0]["id"] == "req1"
    assert res[1]["id"] == "req2"
def test_inference_engine_generate()
Expand source code
def test_inference_engine_generate():
    model = DummyLLM()
    engine = NeuralOpsInferenceEngine(model)
    ids = torch.tensor([[1, 2, 3]])
    out = engine.generate(ids, max_new_tokens=4)
    assert out.shape == (1, 7)
def test_speculative_generate()
Expand source code
def test_speculative_generate():
    target = DummyLLM()
    draft = DummyLLM()
    ids = torch.tensor([[1, 2, 3]])
    out = speculative_generate(target, draft, ids, gamma=2, max_new_tokens=4)
    assert out.shape[1] >= 4
def test_stream_generate()
Expand source code
def test_stream_generate():
    model = DummyLLM()
    ids = torch.tensor([[1, 2, 3]])
    tokens = list(stream_generate(model, ids, max_new_tokens=4))
    assert len(tokens) == 4
    assert all(isinstance(t, int) for t in tokens)

Classes

class DummyLLM (vocab_size: int = 32)
Expand source code
class DummyLLM(nn.Module):
    def __init__(self, vocab_size: int = 32):
        super().__init__()
        self.embedding = nn.Embedding(vocab_size, 16)
        self.head = nn.Linear(16, vocab_size)

    def forward(self, input_ids):
        h = self.embedding(input_ids)
        return self.head(h)

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) ‑> Callable[..., typing.Any]
Expand source code
def forward(self, input_ids):
    h = self.embedding(input_ids)
    return self.head(h)

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.