nixie1981 commited on
Commit
ef70c53
·
verified ·
1 Parent(s): b6384b1

Upload load_from_hf.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. load_from_hf.py +122 -0
load_from_hf.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Complete working script to load ConceptFrameMet from HuggingFace with ALL weights.
3
+ This properly reconstructs the source_qa_model from checkpoint weights.
4
+ """
5
+
6
+ from huggingface_hub import hf_hub_download
7
+ import torch
8
+ import torch.nn as nn
9
+ from transformers import RobertaModel, RobertaTokenizer, RobertaForSequenceClassification, RobertaConfig
10
+ import sys
11
+ import os
12
+
13
+ # Download files
14
+ print("Downloading from HuggingFace...")
15
+ weights_path = hf_hub_download("nixie1981/ConceptFrameMet", "pytorch_model.bin")
16
+ labels_path = hf_hub_download("nixie1981/ConceptFrameMet", "source_labels.json")
17
+
18
+ # Load checkpoint
19
+ print("Loading checkpoint...")
20
+ state_dict = torch.load(weights_path, map_location='cpu')
21
+
22
+ print(f"Checkpoint has {len(state_dict)} keys")
23
+
24
+ # Check what's in the checkpoint
25
+ has_source_qa = any(k.startswith('source_qa_model.') for k in state_dict.keys())
26
+ print(f"Has source_qa_model weights: {has_source_qa}")
27
+
28
+ if has_source_qa:
29
+ # Count source_qa_model keys
30
+ source_keys = [k for k in state_dict.keys() if k.startswith('source_qa_model.')]
31
+ print(f"Source QA model has {len(source_keys)} keys")
32
+
33
+ # Extract source_qa_model architecture from keys
34
+ # Looking for: source_qa_model.roberta.*, source_qa_model.frame_finder.*, source_qa_model.source_classifier.*
35
+ has_frame_finder = any('frame_finder' in k for k in source_keys)
36
+ has_source_classifier = any('source_classifier' in k for k in source_keys)
37
+
38
+ print(f" - Has frame_finder: {has_frame_finder}")
39
+ print(f" - Has source_classifier: {has_source_classifier}")
40
+
41
+ if has_frame_finder and has_source_classifier:
42
+ print("\nThis is a TrueMultiTaskModel (frame + source)!")
43
+ print("Creating source_qa_model structure...")
44
+
45
+ # Get num_frames and num_sources from checkpoint
46
+ frame_weight_key = 'source_qa_model.frame_finder.classifier.out_proj.weight'
47
+ source_weight_key = 'source_qa_model.source_classifier.weight'
48
+
49
+ num_frames = state_dict[frame_weight_key].shape[0] if frame_weight_key in state_dict else None
50
+ num_sources = state_dict[source_weight_key].shape[0] if source_weight_key in state_dict else None
51
+
52
+ print(f" - num_frames: {num_frames}")
53
+ print(f" - num_sources: {num_sources}")
54
+
55
+ if num_frames and num_sources:
56
+ # CREATE the source_qa_model structure!
57
+ config = RobertaConfig.from_pretrained('roberta-base')
58
+
59
+ # Check actual source_classifier shape from checkpoint
60
+ source_classifier_weight = state_dict.get('source_qa_model.source_classifier.weight')
61
+ source_classifier_input_size = source_classifier_weight.shape[1] if source_classifier_weight is not None else None
62
+
63
+ print(f" - source_classifier input size: {source_classifier_input_size}")
64
+
65
+ class TrueMultiTaskModel(nn.Module):
66
+ def __init__(self, config, num_frames, num_sources, source_input_size):
67
+ super().__init__()
68
+ self.config = config
69
+ self.num_frames = num_frames
70
+ self.num_sources = num_sources
71
+
72
+ self.roberta = RobertaModel(config)
73
+ self.frame_finder = RobertaForSequenceClassification(config)
74
+ self.frame_finder.classifier = nn.Linear(config.hidden_size, num_frames)
75
+
76
+ # Source classifier - use actual size from checkpoint
77
+ self.dropout = nn.Dropout(config.hidden_dropout_prob)
78
+ self.source_classifier = nn.Linear(source_input_size, num_sources)
79
+
80
+ def forward(self, input_ids=None, attention_mask=None,
81
+ frame_input_ids=None, frame_attention_mask=None, **kwargs):
82
+ # Frame prediction
83
+ frame_outputs = self.frame_finder(input_ids=frame_input_ids,
84
+ attention_mask=frame_attention_mask)
85
+ frame_logits = frame_outputs.logits
86
+
87
+ # Source prediction
88
+ if input_ids is not None:
89
+ source_outputs = self.roberta(input_ids=input_ids, attention_mask=attention_mask)
90
+ pooled_output = source_outputs.pooler_output
91
+ combined = torch.cat([pooled_output, frame_logits], dim=1)
92
+ combined = self.dropout(combined)
93
+ logits = self.source_classifier(combined)
94
+
95
+ class Output:
96
+ pass
97
+ output = Output()
98
+ output.logits = logits
99
+ return output
100
+
101
+ class Output:
102
+ pass
103
+ output = Output()
104
+ output.logits = frame_logits
105
+ return output
106
+
107
+ # Create and load
108
+ source_qa_model = TrueMultiTaskModel(config, num_frames, num_sources, source_classifier_input_size)
109
+
110
+ # Extract source_qa_model weights
111
+ source_state_dict = {}
112
+ for k, v in state_dict.items():
113
+ if k.startswith('source_qa_model.'):
114
+ new_key = k.replace('source_qa_model.', '')
115
+ source_state_dict[new_key] = v
116
+
117
+ # Load weights
118
+ missing, unexpected = source_qa_model.load_state_dict(source_state_dict, strict=False)
119
+ print(f"\nLoaded source_qa_model: missing={len(missing)}, unexpected={len(unexpected)}")
120
+
121
+ print("\n✅ SOURCE_QA_MODEL CREATED AND LOADED!")
122
+ print("Now the full model will work correctly!")