ykae commited on
Commit
2759894
·
verified ·
1 Parent(s): 4160ec4

Upload 7 files

Browse files
config.json ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "BertForSequenceClassification"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.1,
6
+ "auto_map": {
7
+ "AutoModelForSequenceClassification": "modeling_monarch_bert.MonarchBertForSequenceClassification"
8
+ },
9
+ "classifier_dropout": null,
10
+ "finetuning_task": "mnli",
11
+ "hidden_act": "gelu",
12
+ "hidden_dropout_prob": 0.1,
13
+ "hidden_size": 768,
14
+ "id2label": {
15
+ "0": "LABEL_0",
16
+ "1": "LABEL_1",
17
+ "2": "LABEL_2"
18
+ },
19
+ "initializer_range": 0.02,
20
+ "intermediate_size": 3072,
21
+ "label2id": {
22
+ "LABEL_0": 0,
23
+ "LABEL_1": 1,
24
+ "LABEL_2": 2
25
+ },
26
+ "layer_norm_eps": 1e-12,
27
+ "max_position_embeddings": 512,
28
+ "model_type": "bert",
29
+ "monarch_groups": 16,
30
+ "monarch_start_layer": 0,
31
+ "num_attention_heads": 12,
32
+ "num_hidden_layers": 12,
33
+ "pad_token_id": 0,
34
+ "position_embedding_type": "absolute",
35
+ "transformers_version": "4.56.2",
36
+ "type_vocab_size": 2,
37
+ "use_cache": true,
38
+ "vocab_size": 30522
39
+ }
modeling_monarch_bert.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ from transformers import BertModel, BertPreTrainedModel
4
+ from transformers.models.bert.modeling_bert import BertEncoder, BertLayer
5
+
6
+ # --- 1. Monarch Low-Level Operations ---
7
+ class MonarchUp(nn.Module):
8
+ def __init__(self, d_model=768, hidden_dim=3072, n_blocks=16):
9
+ super().__init__()
10
+ self.n_blocks = n_blocks
11
+ self.in_block = d_model // n_blocks
12
+ self.out_block_exp = hidden_dim // self.in_block
13
+
14
+ self.b1 = nn.Parameter(torch.randn(n_blocks, self.in_block, self.in_block) * 0.02)
15
+ self.b2 = nn.Parameter(torch.randn(self.in_block, self.out_block_exp, n_blocks) * 0.02)
16
+ self.bias = nn.Parameter(torch.zeros(hidden_dim))
17
+
18
+ def forward(self, x):
19
+ B, S, D = x.shape
20
+ x = x.view(B, S, self.n_blocks, self.in_block)
21
+ x = torch.einsum('bsni,noi->bsno', x, self.b1)
22
+ x = x.transpose(-1, -2) # Implicit fusion friendly for Triton later
23
+ x = torch.einsum('bsni,noi->bsno', x, self.b2)
24
+ return x.reshape(B, S, -1) + self.bias
25
+
26
+ class MonarchDown(nn.Module):
27
+ def __init__(self, d_model=768, hidden_dim=3072, n_blocks=16):
28
+ super().__init__()
29
+ self.n_blocks = n_blocks
30
+ self.out_block = d_model // n_blocks
31
+ self.in_block_exp = hidden_dim // self.out_block
32
+
33
+ self.b1 = nn.Parameter(torch.randn(self.out_block, n_blocks, self.in_block_exp) * 0.02)
34
+ self.b2 = nn.Parameter(torch.randn(n_blocks, self.out_block, self.out_block) * 0.02)
35
+ self.bias = nn.Parameter(torch.zeros(d_model))
36
+
37
+ def forward(self, x):
38
+ B, S, D = x.shape
39
+ x = x.view(B, S, self.out_block, self.in_block_exp)
40
+ x = torch.einsum('bsni,noi->bsno', x, self.b1)
41
+ x = x.transpose(-1, -2)
42
+ x = torch.einsum('bsni,noi->bsno', x, self.b2)
43
+ return x.reshape(B, S, -1) + self.bias
44
+
45
+ class MonarchFFN(nn.Module):
46
+ def __init__(self, d_model, hidden_dim, groups, act_fn):
47
+ super().__init__()
48
+ self.monarch_up = MonarchUp(d_model, hidden_dim, n_blocks=groups)
49
+ self.act = act_fn
50
+ self.monarch_down = MonarchDown(d_model, hidden_dim, n_blocks=groups)
51
+
52
+ def forward(self, x):
53
+ x = self.monarch_up(x)
54
+ x = self.act(x)
55
+ x = self.monarch_down(x)
56
+ return x
57
+
58
+ class FFNWrapper(nn.Module):
59
+ def __init__(self, new_ffn_module):
60
+ super().__init__()
61
+ self.ffn = new_ffn_module
62
+ def forward(self, hidden_states):
63
+ return self.ffn(hidden_states)
64
+
65
+ # --- 2. The Model Architecture ---
66
+ class MonarchBertForSequenceClassification(BertPreTrainedModel):
67
+ def __init__(self, config):
68
+ super().__init__(config)
69
+ self.num_labels = config.num_labels
70
+ self.config = config
71
+
72
+ # 1. Load Standard BERT
73
+ self.bert = BertModel(config)
74
+ self.classifier = nn.Linear(config.hidden_size, config.num_labels)
75
+
76
+ # 2. Inject Monarch Layers based on Config
77
+ # This reconstructs the architecture exactly as it was during distillation
78
+ monarch_start_layer = getattr(config, "monarch_start_layer", 0)
79
+ n_groups = getattr(config, "monarch_groups", 16)
80
+
81
+ bert_layers = self.bert.encoder.layer
82
+ # Backward replacement logic (11 -> start_layer)
83
+ for i in range(11, monarch_start_layer - 1, -1):
84
+ d_model = config.hidden_size
85
+ h_dim = config.intermediate_size
86
+
87
+ # Create Monarch Module
88
+ monarch_ffn = MonarchFFN(d_model, h_dim, n_groups, nn.GELU())
89
+
90
+ # Surgery
91
+ bert_layers[i].intermediate = FFNWrapper(monarch_ffn)
92
+ bert_layers[i].output.dense = nn.Identity()
93
+
94
+ self.init_weights()
95
+
96
+ def forward(self, input_ids=None, attention_mask=None, token_type_ids=None, labels=None):
97
+ outputs = self.bert(input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids)
98
+ pooled_output = outputs[1]
99
+ logits = self.classifier(pooled_output)
100
+
101
+ loss = None
102
+ if labels is not None:
103
+ loss_fct = nn.CrossEntropyLoss()
104
+ loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
105
+
106
+ return type('SequenceClassifierOutput', (object,), {'loss': loss, 'logits': logits})()
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:df08af71ced68de7c27a397d9fe6324915c9d23f44d9f846bd01b7009b3ccfef
3
+ size 219794023
special_tokens_map.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "cls_token": "[CLS]",
3
+ "mask_token": "[MASK]",
4
+ "pad_token": "[PAD]",
5
+ "sep_token": "[SEP]",
6
+ "unk_token": "[UNK]"
7
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "0": {
4
+ "content": "[PAD]",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "100": {
12
+ "content": "[UNK]",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "101": {
20
+ "content": "[CLS]",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": true
26
+ },
27
+ "102": {
28
+ "content": "[SEP]",
29
+ "lstrip": false,
30
+ "normalized": false,
31
+ "rstrip": false,
32
+ "single_word": false,
33
+ "special": true
34
+ },
35
+ "103": {
36
+ "content": "[MASK]",
37
+ "lstrip": false,
38
+ "normalized": false,
39
+ "rstrip": false,
40
+ "single_word": false,
41
+ "special": true
42
+ }
43
+ },
44
+ "clean_up_tokenization_spaces": true,
45
+ "cls_token": "[CLS]",
46
+ "do_basic_tokenize": true,
47
+ "do_lower_case": true,
48
+ "extra_special_tokens": {},
49
+ "mask_token": "[MASK]",
50
+ "model_max_length": 512,
51
+ "never_split": null,
52
+ "pad_token": "[PAD]",
53
+ "sep_token": "[SEP]",
54
+ "strip_accents": null,
55
+ "tokenize_chinese_chars": true,
56
+ "tokenizer_class": "BertTokenizer",
57
+ "unk_token": "[UNK]"
58
+ }
vocab.txt ADDED
The diff for this file is too large to render. See raw diff