taegyeonglee commited on
Commit
79d49da
·
verified ·
1 Parent(s): 110e891

Add HF-standard offline package (auto_map + modeling_kbert_mtl.py)

Browse files
Files changed (2) hide show
  1. config.json +168 -26
  2. modeling_kbert_mtl.py +52 -0
config.json CHANGED
@@ -1,39 +1,181 @@
1
  {
2
- "_name_or_path": "klue/bert-base",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  "architectures": [
4
  "KbertMTL"
5
  ],
6
- "attention_probs_dropout_prob": 0.1,
7
- "classifier_dropout": null,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  "hidden_act": "gelu",
 
9
  "hidden_dropout_prob": 0.1,
10
- "hidden_size": 768,
 
 
11
  "initializer_range": 0.02,
12
- "intermediate_size": 3072,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  "langquant_info": {
14
- "base_model": "klue/bert-base",
 
 
 
 
15
  "head_structure": {
 
16
  "action": 6,
17
  "emotion": 7,
18
- "regression": 3,
19
- "sentiment": 5
20
  },
21
- "license": "cc-by-nc-4.0",
22
- "model_name": "LQ-KBERT-Base",
23
- "organization": "LangQuant",
24
- "source_ckpt": "lq-kbert-base.pt",
25
- "task": "Crypto Market Korean Sentiment & Action Signal Classification",
26
- "year": 2025
27
  },
28
- "layer_norm_eps": 1e-12,
29
- "max_position_embeddings": 512,
30
- "model_type": "bert",
31
- "num_attention_heads": 12,
32
- "num_hidden_layers": 12,
33
- "pad_token_id": 0,
34
- "position_embedding_type": "absolute",
35
- "transformers_version": "4.27.0",
36
- "type_vocab_size": 2,
37
- "use_cache": true,
38
- "vocab_size": 32000
39
- }
 
1
  {
2
+ "return_dict": true,
3
+ "output_hidden_states": false,
4
+ "output_attentions": false,
5
+ "torchscript": false,
6
+ "torch_dtype": null,
7
+ "use_bfloat16": false,
8
+ "tf_legacy_loss": false,
9
+ "pruned_heads": {},
10
+ "tie_word_embeddings": true,
11
+ "is_encoder_decoder": false,
12
+ "is_decoder": false,
13
+ "cross_attention_hidden_size": null,
14
+ "add_cross_attention": false,
15
+ "tie_encoder_decoder": false,
16
+ "max_length": 20,
17
+ "min_length": 0,
18
+ "do_sample": false,
19
+ "early_stopping": false,
20
+ "num_beams": 1,
21
+ "num_beam_groups": 1,
22
+ "diversity_penalty": 0.0,
23
+ "temperature": 1.0,
24
+ "top_k": 50,
25
+ "top_p": 1.0,
26
+ "typical_p": 1.0,
27
+ "repetition_penalty": 1.0,
28
+ "length_penalty": 1.0,
29
+ "no_repeat_ngram_size": 0,
30
+ "encoder_no_repeat_ngram_size": 0,
31
+ "bad_words_ids": null,
32
+ "num_return_sequences": 1,
33
+ "chunk_size_feed_forward": 0,
34
+ "output_scores": false,
35
+ "return_dict_in_generate": false,
36
+ "forced_bos_token_id": null,
37
+ "forced_eos_token_id": null,
38
+ "remove_invalid_values": false,
39
+ "exponential_decay_length_penalty": null,
40
+ "suppress_tokens": null,
41
+ "begin_suppress_tokens": null,
42
  "architectures": [
43
  "KbertMTL"
44
  ],
45
+ "finetuning_task": null,
46
+ "id2label": {
47
+ "0": "LABEL_0",
48
+ "1": "LABEL_1"
49
+ },
50
+ "label2id": {
51
+ "LABEL_0": 0,
52
+ "LABEL_1": 1
53
+ },
54
+ "tokenizer_class": null,
55
+ "prefix": null,
56
+ "bos_token_id": null,
57
+ "pad_token_id": 0,
58
+ "eos_token_id": null,
59
+ "sep_token_id": null,
60
+ "decoder_start_token_id": null,
61
+ "task_specific_params": null,
62
+ "problem_type": null,
63
+ "_name_or_path": "klue/bert-base",
64
+ "transformers_version": "4.27.0",
65
+ "model_type": "kbert_mtl",
66
+ "vocab_size": 32000,
67
+ "hidden_size": 768,
68
+ "num_hidden_layers": 12,
69
+ "num_attention_heads": 12,
70
  "hidden_act": "gelu",
71
+ "intermediate_size": 3072,
72
  "hidden_dropout_prob": 0.1,
73
+ "attention_probs_dropout_prob": 0.1,
74
+ "max_position_embeddings": 512,
75
+ "type_vocab_size": 2,
76
  "initializer_range": 0.02,
77
+ "layer_norm_eps": 1e-12,
78
+ "position_embedding_type": "absolute",
79
+ "use_cache": true,
80
+ "classifier_dropout": null,
81
+ "base_model_name_or_path": "klue/bert-base",
82
+ "base_model_config": {
83
+ "return_dict": true,
84
+ "output_hidden_states": false,
85
+ "output_attentions": false,
86
+ "torchscript": false,
87
+ "torch_dtype": null,
88
+ "use_bfloat16": false,
89
+ "tf_legacy_loss": false,
90
+ "pruned_heads": {},
91
+ "tie_word_embeddings": true,
92
+ "is_encoder_decoder": false,
93
+ "is_decoder": false,
94
+ "cross_attention_hidden_size": null,
95
+ "add_cross_attention": false,
96
+ "tie_encoder_decoder": false,
97
+ "max_length": 20,
98
+ "min_length": 0,
99
+ "do_sample": false,
100
+ "early_stopping": false,
101
+ "num_beams": 1,
102
+ "num_beam_groups": 1,
103
+ "diversity_penalty": 0.0,
104
+ "temperature": 1.0,
105
+ "top_k": 50,
106
+ "top_p": 1.0,
107
+ "typical_p": 1.0,
108
+ "repetition_penalty": 1.0,
109
+ "length_penalty": 1.0,
110
+ "no_repeat_ngram_size": 0,
111
+ "encoder_no_repeat_ngram_size": 0,
112
+ "bad_words_ids": null,
113
+ "num_return_sequences": 1,
114
+ "chunk_size_feed_forward": 0,
115
+ "output_scores": false,
116
+ "return_dict_in_generate": false,
117
+ "forced_bos_token_id": null,
118
+ "forced_eos_token_id": null,
119
+ "remove_invalid_values": false,
120
+ "exponential_decay_length_penalty": null,
121
+ "suppress_tokens": null,
122
+ "begin_suppress_tokens": null,
123
+ "architectures": [
124
+ "BertForMaskedLM"
125
+ ],
126
+ "finetuning_task": null,
127
+ "id2label": {
128
+ "0": "LABEL_0",
129
+ "1": "LABEL_1"
130
+ },
131
+ "label2id": {
132
+ "LABEL_0": 0,
133
+ "LABEL_1": 1
134
+ },
135
+ "tokenizer_class": null,
136
+ "prefix": null,
137
+ "bos_token_id": null,
138
+ "pad_token_id": 0,
139
+ "eos_token_id": null,
140
+ "sep_token_id": null,
141
+ "decoder_start_token_id": null,
142
+ "task_specific_params": null,
143
+ "problem_type": null,
144
+ "_name_or_path": "klue/bert-base",
145
+ "transformers_version": "4.27.0",
146
+ "model_type": "bert",
147
+ "vocab_size": 32000,
148
+ "hidden_size": 768,
149
+ "num_hidden_layers": 12,
150
+ "num_attention_heads": 12,
151
+ "hidden_act": "gelu",
152
+ "intermediate_size": 3072,
153
+ "hidden_dropout_prob": 0.1,
154
+ "attention_probs_dropout_prob": 0.1,
155
+ "max_position_embeddings": 512,
156
+ "type_vocab_size": 2,
157
+ "initializer_range": 0.02,
158
+ "layer_norm_eps": 1e-12,
159
+ "position_embedding_type": "absolute",
160
+ "use_cache": true,
161
+ "classifier_dropout": null
162
+ },
163
+ "emo_threshold": 0.35,
164
  "langquant_info": {
165
+ "model_name": "LQ-KBERT-Base",
166
+ "organization": "LangQuant",
167
+ "year": 2025,
168
+ "license": "cc-by-nc-4.0",
169
+ "task": "Korean sentiment/action multi-task",
170
  "head_structure": {
171
+ "sentiment": 5,
172
  "action": 6,
173
  "emotion": 7,
174
+ "regression": 3
 
175
  },
176
+ "source_ckpt": "lq-kbert-base.pt"
 
 
 
 
 
177
  },
178
+ "auto_map": {
179
+ "AutoModel": "modeling_kbert_mtl.KbertMTL"
180
+ }
181
+ }
 
 
 
 
 
 
 
 
modeling_kbert_mtl.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # modeling_kbert_mtl.py
2
+ import torch
3
+ import torch.nn as nn
4
+ from transformers import PreTrainedModel, AutoModel, AutoConfig
5
+
6
+ class KbertMTL(PreTrainedModel):
7
+ """
8
+ LangQuant KBERT Multi-Task Head (HF-standard, offline-friendly)
9
+
10
+ Outputs (dict):
11
+ - logits_senti: (B,5)
12
+ - logits_act: (B,6)
13
+ - logits_emo: (B,7)
14
+ - pred_reg: (B,3) # [certainty, relevance, toxicity] in 0~1 (권장)
15
+ - last_hidden_state: (B, L, H) from base encoder
16
+ """
17
+ def __init__(self, config):
18
+ super().__init__(config)
19
+
20
+ if not hasattr(config, "base_model_config") or config.base_model_config is None:
21
+ raise ValueError(
22
+ "config.base_model_config is required for offline load. "
23
+ "Make sure your config.json contains a serialized base model config."
24
+ )
25
+
26
+ base_cfg = AutoConfig.from_dict(config.base_model_config)
27
+ self.bert = AutoModel.from_config(base_cfg)
28
+
29
+ hidden = self.bert.config.hidden_size
30
+ self.head_senti = nn.Linear(hidden, 5)
31
+ self.head_act = nn.Linear(hidden, 6)
32
+ self.head_emo = nn.Linear(hidden, 7)
33
+ self.head_reg = nn.Linear(hidden, 3)
34
+
35
+ self.has_token_type = getattr(self.bert.embeddings, "token_type_embeddings", None) is not None
36
+ self.post_init()
37
+
38
+ def forward(self, input_ids=None, attention_mask=None, token_type_ids=None, **kwargs):
39
+ kw = dict(input_ids=input_ids, attention_mask=attention_mask)
40
+ if self.has_token_type and token_type_ids is not None:
41
+ kw["token_type_ids"] = token_type_ids
42
+
43
+ out = self.bert(**kw)
44
+ h = out.last_hidden_state[:, 0] # [CLS]
45
+
46
+ return {
47
+ "logits_senti": self.head_senti(h),
48
+ "logits_act": self.head_act(h),
49
+ "logits_emo": self.head_emo(h),
50
+ "pred_reg": self.head_reg(h),
51
+ "last_hidden_state": out.last_hidden_state
52
+ }