Upload orthrus_hf.py
Browse filesUpdated source code for Mamba-2 compatibility.
- orthrus_hf.py +24 -7
orthrus_hf.py
CHANGED
|
@@ -5,9 +5,16 @@ import torch
|
|
| 5 |
import torch.nn as nn
|
| 6 |
|
| 7 |
from functools import partial
|
| 8 |
-
from mamba_ssm.modules.mamba_simple import Block, Mamba
|
| 9 |
from transformers import PretrainedConfig, PreTrainedModel
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
class OrthrusConfig(PretrainedConfig):
|
| 13 |
"""HuggingFace config for pre-trained Orthrus model."""
|
|
@@ -108,11 +115,21 @@ class OrthrusPretrainedModel(PreTrainedModel):
|
|
| 108 |
"""
|
| 109 |
mix_cls = partial(Mamba, layer_idx=layer_idx)
|
| 110 |
norm_cls = nn.LayerNorm
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
block.layer_idx = layer_idx
|
| 117 |
return block
|
| 118 |
|
|
@@ -166,7 +183,7 @@ class OrthrusPretrainedModel(PreTrainedModel):
|
|
| 166 |
mean_tensor = self.mean_unpadded(out, lengths)
|
| 167 |
return mean_tensor
|
| 168 |
|
| 169 |
-
def seq_to_oh(self, seq: str) -> torch.Tensor:
|
| 170 |
"""Convert nucleotide string into one-hot-encoding.
|
| 171 |
|
| 172 |
The encoding uses ordering ["A", "C", "G", "T"].
|
|
|
|
| 5 |
import torch.nn as nn
|
| 6 |
|
| 7 |
from functools import partial
|
|
|
|
| 8 |
from transformers import PretrainedConfig, PreTrainedModel
|
| 9 |
|
| 10 |
+
try:
|
| 11 |
+
from mamba_ssm.modules.mamba_simple import Mamba
|
| 12 |
+
from mamba_ssm.modules.block import Block
|
| 13 |
+
except ImportError:
|
| 14 |
+
from mamba_ssm.modules.mamba_simple import Block, Mamba
|
| 15 |
+
|
| 16 |
+
HAS_MLP = "mlp_cls" in Block.__init__.__code__.co_varnames
|
| 17 |
+
|
| 18 |
|
| 19 |
class OrthrusConfig(PretrainedConfig):
|
| 20 |
"""HuggingFace config for pre-trained Orthrus model."""
|
|
|
|
| 115 |
"""
|
| 116 |
mix_cls = partial(Mamba, layer_idx=layer_idx)
|
| 117 |
norm_cls = nn.LayerNorm
|
| 118 |
+
|
| 119 |
+
if HAS_MLP:
|
| 120 |
+
block = Block(
|
| 121 |
+
d_model,
|
| 122 |
+
mix_cls,
|
| 123 |
+
norm_cls=norm_cls,
|
| 124 |
+
mlp_cls=nn.Identity
|
| 125 |
+
)
|
| 126 |
+
else:
|
| 127 |
+
block = Block(
|
| 128 |
+
d_model,
|
| 129 |
+
mix_cls,
|
| 130 |
+
norm_cls=norm_cls
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
block.layer_idx = layer_idx
|
| 134 |
return block
|
| 135 |
|
|
|
|
| 183 |
mean_tensor = self.mean_unpadded(out, lengths)
|
| 184 |
return mean_tensor
|
| 185 |
|
| 186 |
+
def seq_to_oh(self, seq: list[str]) -> torch.Tensor:
|
| 187 |
"""Convert nucleotide string into one-hot-encoding.
|
| 188 |
|
| 189 |
The encoding uses ordering ["A", "C", "G", "T"].
|