Module neuralops.tests.test_replace_comprehensive
Functions
def test_build_attention()-
Expand source code
def test_build_attention(): attn = build_attention("dummy_test_replace_attn", 64, 4) assert isinstance(attn, DummyAttention) assert attn.hidden_size == 64 def test_replace_bert_attention()-
Expand source code
def test_replace_bert_attention(): model = DummyBertModel() replace_attention_in_model(model, "dummy_test_replace_attn") assert isinstance(model.encoder.layer[0].attention.self, DummyAttention) def test_replace_gpt_attention()-
Expand source code
def test_replace_gpt_attention(): model = DummyGPT2Model() replace_attention_in_model(model, "dummy_test_replace_attn") assert isinstance(model.transformer.h[0].attn, DummyAttention) def test_replace_unsupported_model_type()-
Expand source code
def test_replace_unsupported_model_type(): model = nn.Module() model.config = SimpleNamespace(model_type="unknown_xyz") with pytest.raises(ValueError, match="Unsupported model type"): replace_attention_in_model(model, "dummy_test_replace_attn")
Classes
class DummyAttention (hidden_size=64, num_heads=4, **kwargs)-
Expand source code
class DummyAttention(nn.Module): def __init__(self, hidden_size=64, num_heads=4, **kwargs): super().__init__() self.hidden_size = hidden_size self.num_heads = num_heads self.proj = nn.Linear(hidden_size, hidden_size) def forward(self, x, *args, **kwargs): return self.proj(x), NoneBase 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, *args, **kwargs) ‑> Callable[..., typing.Any]-
Expand source code
def forward(self, x, *args, **kwargs): return self.proj(x), NoneDefine 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.
class DummyBertModel-
Expand source code
class DummyBertModel(nn.Module): def __init__(self): super().__init__() self.config = SimpleNamespace(model_type="bert", hidden_size=64, num_attention_heads=4) layer = nn.Module() layer.attention = nn.Module() layer.attention.self = nn.Module() layer.attention.self.query = nn.Linear(64, 64) layer.attention.self.num_attention_heads = 4 self.encoder = nn.Module() self.encoder.layer = nn.ModuleList([layer])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
class DummyDistilBertModel-
Expand source code
class DummyDistilBertModel(nn.Module): def __init__(self): super().__init__() self.config = SimpleNamespace(model_type="distilbert", hidden_size=64, num_attention_heads=4) layer = nn.Module() layer.attention = nn.Module() self.distilbert = nn.Module() self.distilbert.transformer = nn.Module() self.distilbert.transformer.layer = nn.ModuleList([layer])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
class DummyGPT2Model-
Expand source code
class DummyGPT2Model(nn.Module): def __init__(self): super().__init__() self.config = SimpleNamespace(model_type="gpt2", hidden_size=64, num_attention_heads=4) block = nn.Module() block.attn = nn.Module() block.attn.embed_dim = 64 block.attn.num_heads = 4 self.transformer = nn.Module() self.transformer.h = nn.ModuleList([block])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