Module neuralops.tests.test_inference_real
Tests for production inference engine features.
Functions
def test_real_beam_search_generate()-
Expand source code
def test_real_beam_search_generate(): model = TinyLLM() ids = torch.tensor([[1, 2, 3]]) out = beam_search_generate(model, ids, beam_size=3, max_new_tokens=4, length_penalty=1.0) assert out.shape == (1, 7) def test_real_continuous_batcher()-
Expand source code
def test_real_continuous_batcher(): model = TinyLLM() batcher = ContinuousBatcher(model, max_batch_size=4) r1 = GenerationRequest("req_1", torch.tensor([[1, 2]]), max_new_tokens=2) r2 = GenerationRequest("req_2", torch.tensor([[3, 4, 5]]), max_new_tokens=3) batcher.add_request(r1) batcher.add_request(r2) step1 = batcher.step() assert len(step1) == 2 step2 = batcher.step() assert len(step2) == 2 assert r1.finished # generated 2 tokens step3 = batcher.step() assert len(step3) == 1 # only r2 remaining assert r2.finished def test_real_inference_generate_penalties()-
Expand source code
def test_real_inference_generate_penalties(): model = TinyLLM() engine = NeuralOpsInferenceEngine(model) ids = torch.tensor([[1, 2, 3]]) # Top-k & repetition penalty test out = engine.generate(ids, max_new_tokens=8, top_k=10, repetition_penalty=1.2) assert out.shape == (1, 11) def test_real_speculative_generate()-
Expand source code
def test_real_speculative_generate(): target = TinyLLM() draft = TinyLLM() ids = torch.tensor([[1, 2, 3]]) out = speculative_generate(target, draft, ids, max_new_tokens=6, K=2) assert out.shape[1] >= 6
Classes
class TinyLLM (vocab_size: int = 64, dim: int = 32)-
Expand source code
class TinyLLM(nn.Module): def __init__(self, vocab_size: int = 64, dim: int = 32): super().__init__() self.embedding = nn.Embedding(vocab_size, dim) self.head = nn.Linear(dim, vocab_size) def forward(self, input_ids, **kwargs): 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, **kwargs) ‑> Callable[..., typing.Any]-
Expand source code
def forward(self, input_ids, **kwargs): 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:
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.