|
a |
|
b/src/model/modeling_dummy.py |
|
|
1 |
from dataclasses import dataclass |
|
|
2 |
from typing import Tuple |
|
|
3 |
|
|
|
4 |
import torch |
|
|
5 |
from transformers import AutoModel, AutoConfig |
|
|
6 |
from transformers import PreTrainedModel, PretrainedConfig |
|
|
7 |
from transformers.utils import ModelOutput |
|
|
8 |
|
|
|
9 |
|
|
|
10 |
@dataclass |
|
|
11 |
class DummyModelOutput(ModelOutput): |
|
|
12 |
hidden_states: Tuple[torch.Tensor] |
|
|
13 |
|
|
|
14 |
|
|
|
15 |
class DummyModelConfig(PretrainedConfig): |
|
|
16 |
model_type = "dummy_model" |
|
|
17 |
|
|
|
18 |
def __init__(self, hidden_size: int = 768, **kwargs) -> None: |
|
|
19 |
self.hidden_size = hidden_size |
|
|
20 |
super().__init__(**kwargs) |
|
|
21 |
return |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
class DummyModel(PreTrainedModel): |
|
|
25 |
config_class = DummyModelConfig |
|
|
26 |
|
|
|
27 |
def __init__(self, config: DummyModelConfig) -> None: |
|
|
28 |
super().__init__(config) |
|
|
29 |
self.param = torch.nn.Parameter(torch.zeros(1), requires_grad=False) |
|
|
30 |
return |
|
|
31 |
|
|
|
32 |
def forward(self, x: torch.Tensor, **kwargs) -> DummyModelOutput: |
|
|
33 |
assert x.shape[-1] == self.config.hidden_size |
|
|
34 |
dtype = self.param.dtype |
|
|
35 |
x = x.to(dtype) |
|
|
36 |
return DummyModelOutput(hidden_states=(x,)) |
|
|
37 |
|
|
|
38 |
|
|
|
39 |
AutoConfig.register("dummy_model", DummyModelConfig) |
|
|
40 |
AutoModel.register(DummyModelConfig, DummyModel) |
|
|
41 |
|
|
|
42 |
if __name__ == "__main__": |
|
|
43 |
config = DummyModelConfig(hidden_size=3) |
|
|
44 |
print(config) |
|
|
45 |
model = AutoModel.from_config(config) |
|
|
46 |
print(model) |
|
|
47 |
x = torch.randn(1, 2, 3) |
|
|
48 |
output = model(x) |
|
|
49 |
print(output) |