File size: 6,589 Bytes
8245f38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import argparse
import json
import os
import random
from collections import OrderedDict
from typing import List, Sequence, Tuple

import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F

try:
    from transformers import AutoTokenizer, AutoConfig, XLMRobertaModel
except ImportError:
    AutoTokenizer = None
    HFBertModel = None


class XLMRobertaLanguageBackbone(nn.Module):

    def __init__(
        self,
        ckpt_path,
        frozen_modules: Sequence[str] = (),
        dropout: float = 0.0,
        init_cfg= None,
    ) -> None:

        super().__init__()
        if 'base' in ckpt_path:
            self.head = nn.Linear(768, 768, bias=True) # XLarge
            model_name = "./xlm-roberta-base/"
        elif 'large' in ckpt_path:
            self.head = nn.Linear(1024, 768, bias=True) # XLarge
            model_name = "./xlm-roberta-large/"

        self.frozen_modules = frozen_modules
        cfg = AutoConfig.from_pretrained(model_name)
        self.tokenizer = AutoTokenizer.from_pretrained(model_name)
        self.model = XLMRobertaModel(cfg)
        self.language_dim = cfg.hidden_size
        
        
        # 加载 model 权重
        new_state_dict = OrderedDict()
        state_dict = torch.load(
            ckpt_path,
            map_location="cpu",
            weights_only=False,
        )['state_dict']
        for k, v in state_dict.items():
            if k.startswith('backbone.text_model.'):
                name = k.split("backbone.text_model.")[-1]
                new_state_dict[name] = v
        msg = self.load_state_dict(new_state_dict, strict=True)
        print(msg)

        print("TEXT-ENCODER xlm-roberta-base LOADING WEIGHTS !!!!")



    def forward(self, text: List[str], max_seq_len: int = 32):
        text = self.tokenizer(text=text, return_tensors="pt",
                              padding="max_length", max_length=max_seq_len)
        text = text.to(device=self.model.device)

        txt_feats = self.model(**text)["last_hidden_state"][:, 0]
        txt_feats = self.head(txt_feats)

        return txt_feats


if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument('--wedetect_checkpoint', type=str, default='checkpoints/wedetect_base.pth')
    parser.add_argument('--classname_file', type=str, default='data/texts/coco_zh_class_texts.json')
    parser.add_argument('--max-seq-len', type=int, default=32,
                        help='Fixed token length (must match ONNX export).')
    parser.add_argument('--num-classes-per-group', type=int, default=4,
                        help='Number of classes per group npy.')
    parser.add_argument('--num-groups', type=int, default=64,
                        help='Number of random groups to generate.')
    parser.add_argument('--calib-dir', type=str, default='calib_data',
                        help='Directory for text-encoder quantisation calibration data.')
    args = parser.parse_args()

    with open(args.classname_file) as f:
        name_chinese = json.load(f)
    name_chinese = [name[0] for name in name_chinese]

    language_encoder = XLMRobertaLanguageBackbone(args.wedetect_checkpoint).cuda()

    # Generate random groups: each group picks 4 random classes → shape (1, 4, 768)
    total_classes = len(name_chinese)
    print(f"Total classes: {total_classes}  → Generating {args.num_groups} random groups")
    # Generate calibration data for text-encoder quantisation
    # Directories: calib_dir/input_ids/  calib_dir/attention_mask/
    calib_input_ids = os.path.join(args.calib_dir, "input_ids")
    calib_attn_mask = os.path.join(args.calib_dir, "attention_mask")
    for d in (calib_input_ids, calib_attn_mask):
        os.makedirs(d, exist_ok=True)

    tokenizer = language_encoder.tokenizer

    for g in range(args.num_groups):
        idx = random.sample(range(total_classes), args.num_classes_per_group)
        group_texts = [name_chinese[i] for i in idx]
        tokens = tokenizer(group_texts, padding="max_length",
                           max_length=args.max_seq_len, return_tensors="np")

        np.save(os.path.join(calib_input_ids, f"{g:03d}.npy"),
                tokens["input_ids"].astype(np.int64))
        np.save(os.path.join(calib_attn_mask, f"{g:03d}.npy"),
                tokens["attention_mask"].astype(np.int64))
        print(f"calib [{g:03d}] input_ids: {tokens['input_ids'].shape}  "
              f"classes: {group_texts}")

    # Compress each subdirectory to .tar.gz
    import tarfile
    for sub_name in ("input_ids", "attention_mask"):
        sub_dir = os.path.join(args.calib_dir, sub_name)
        tar_path = os.path.join(args.calib_dir, f"{sub_name}.tar.gz")
        with tarfile.open(tar_path, "w:gz") as tar:
            for fname in sorted(os.listdir(sub_dir)):
                tar.add(os.path.join(sub_dir, fname), arcname=fname)
        print(f"Compressed: {tar_path}")

    print(f"Saved calibration data to {args.calib_dir}/")

    # -------------------------------------------------------------------
    # Generate 64 random 4-class text embeddings  →  class_embedding_4cls/
    # Each file: (1, 4, 768) float32, L2-normalised (same as the ONNX
    # image encoder expects via text_features input).
    # -------------------------------------------------------------------
    embed_dir = os.path.join(args.calib_dir, "class_embedding_4cls")
    os.makedirs(embed_dir, exist_ok=True)

    print(f"\nGenerating {args.num_groups} random {args.num_classes_per_group}-class "
          f"text embeddings → {embed_dir}/")
    for g in range(args.num_groups):
        idx = random.sample(range(total_classes), args.num_classes_per_group)
        group_texts = [name_chinese[i] for i in idx]
        with torch.no_grad():
            feats = language_encoder(group_texts, max_seq_len=args.max_seq_len)
            feats = F.normalize(feats, dim=-1).unsqueeze(0)  # (1, 4, 768)
        fpath = os.path.join(embed_dir, f"{g:03d}.npy")
        np.save(fpath, feats.cpu().numpy().astype(np.float32))
        if (g + 1) % 16 == 0 or g == args.num_groups - 1:
            print(f"  [{g + 1:3d}/{args.num_groups}]  shape={feats.shape}  "
                  f"classes: {group_texts}")

    # Compress
    tar_path = os.path.join(args.calib_dir, "class_embedding_4cls.tar.gz")
    with tarfile.open(tar_path, "w:gz") as tar:
        for fname in sorted(os.listdir(embed_dir)):
            tar.add(os.path.join(embed_dir, fname), arcname=fname)
    print(f"Compressed: {tar_path}")

    print(f"Saved calibration data to {args.calib_dir}/")