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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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import argparse
import logging
import math
import queue
from typing import Dict, List, Optional, Union
from tqdm.autonotebook import trange
import numpy as np
import torch
import torch.multiprocessing as mp
from transformers import AutoModel, AutoTokenizer

from mteb import MTEB


logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(name)s : %(message)s'
)

logger = logging.getLogger('eval_mteb.py')

def get_detailed_instruct(task_description: str) -> str:
    if not task_description:
        return ''

    return 'Instruct: {}\nQuery: '.format(task_description)

def get_task_def_by_task_name_and_type(task_name: str, task_type: str, default_instruct='Given a web search query, retrieve relevant passages that answer the query') -> str:
    if task_type in ['Retrieval']:
        if task_name.lower().startswith('cqadupstack'):
            return 'Given a question, retrieve detailed question descriptions from Stackexchange that are duplicates to the given question'

        task_name_to_instruct: Dict[str, str] = {
            # C-MTEB eval instructions
            'T2Retrieval': 'Given a Chinese search query, retrieve web passages that answer the question',
            'MMarcoRetrieval': 'Given a web search query, retrieve relevant passages that answer the query',
            'DuRetrieval': 'Given a Chinese search query, retrieve web passages that answer the question',
            'CovidRetrieval': 'Given a question on COVID-19, retrieve news articles that answer the question',
            'CmedqaRetrieval': 'Given a Chinese community medical question, retrieve replies that best answer the question',
            'EcomRetrieval': 'Given a user query from an e-commerce website, retrieve description sentences of relevant products',
            'MedicalRetrieval': 'Given a medical question, retrieve user replies that best answer the question',
            'VideoRetrieval': 'Given a video search query, retrieve the titles of relevant videos',
        }

        return task_name_to_instruct[task_name]
    logging.warning(f"No instruction config for task {task_name} with type {task_type}, use default instruction.")
    return default_instruct 


class Encoder(torch.nn.Module):
    def __init__(self, name_or_path:str, pooling: str):
        super().__init__()
        self.model = AutoModel.from_pretrained(name_or_path, trust_remote_code=True)
        self.model = self.model.half() 
        self.model.eval()  
        self.pooling = pooling

    def forward(self, **features) -> torch.Tensor:
        output = self.model(**features, output_hidden_states=True, return_dict=True)
        hidden_state = output.hidden_states[-1]        
        embeddings = self.pooler(hidden_state, **features)
        return embeddings

    def pooler(
        self,
        hidden_state: torch.Tensor,
        attention_mask: torch.Tensor,
        **kwargs
    ) -> torch.Tensor:
        if attention_mask.ndim == 2:
            mask_expanded = attention_mask.unsqueeze(-1).expand(hidden_state.size())
        elif attention_mask.ndim == 3:
            mask_expanded = attention_mask
        else:
            raise RuntimeError(f"Unexpected {attention_mask.ndim=}")

        hidden_state = hidden_state * mask_expanded

        if self.pooling == 'first':
            pooled_output = hidden_state[:, 0]

        elif self.pooling == 'last':
            left_padding = (attention_mask[:, -1].sum() == attention_mask.shape[0])
            if left_padding:
                return hidden_state[:, -1]
            else:
                sequence_lengths = attention_mask.sum(dim=1) - 1
                batch_size = hidden_state.shape[0]
            return hidden_state[torch.arange(batch_size, device=hidden_state.device), sequence_lengths]
        elif self.pooling == 'mean':
            lengths = mask_expanded.sum(1).clamp(min=1e-9)
            pooled_output = hidden_state.sum(dim=1) / lengths

        elif self.pooling == 'weightedmean':
            input_mask_expanded = attention_mask.unsqueeze(-1).expand(hidden_state.size()).float()
            # hidden_state shape: bs, seq, hidden_dim
            weights = (
                    torch.arange(start=1, end=hidden_state.shape[1] + 1)
                    .unsqueeze(0)
                    .unsqueeze(-1)
                    .expand(hidden_state.size())
                    .float().to(hidden_state.device)
                )
            assert weights.shape == hidden_state.shape == input_mask_expanded.shape
            input_mask_expanded = input_mask_expanded * weights

            sum_embeddings = torch.sum(hidden_state * input_mask_expanded, 1)
            sum_mask = input_mask_expanded.sum(1)
            sum_mask = torch.clamp(sum_mask, min=1e-9)
            pooled_output = sum_embeddings / sum_mask

        else:
            raise ValueError(f"Wrong pooler mode : {self.pooling}")
        return pooled_output


class Wrapper:
    def __init__(
        self,
        tokenizer,
        encoder: Encoder,
        batch_size: int,
        max_seq_len: int = 512,
        normalize_embeddings: bool = False,
        default_query: bool = False,
        force_default: bool = False,
        sep: str = " ",
        mp_tensor_to_cuda: bool = False,
        instruction: str = None,
        attn_type: str = None
    ):
        self.tokenizer = tokenizer
        self.model = encoder
        self.batch_size = batch_size
        self.max_seq_len = max_seq_len
        self.pool: dict = None
        self.normalize_embeddings = normalize_embeddings
        self.mp_tensor_to_cuda = mp_tensor_to_cuda
        self._target_device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        self.eod_id = self.tokenizer.convert_tokens_to_ids("<|endoftext|>")
        self.instruction = instruction
        self.force_default = force_default
        self.start()
 
        if self.tokenizer.padding_side != 'right':
            logger.warning(f"Change tokenizer.padding_side from {self.tokenizer.padding_side} to right")
            self.tokenizer.padding_side = 'right'
        if self.tokenizer.pad_token is None:
            logger.warning(f"Set tokenizer.pad_token as eos_token {self.tokenizer.eos_token}")
            self.tokenizer.pad_token='<|endoftext|>'

    def start(self, target_devices: Optional[List[str]] = None):
        """
        Starts multi process to process the encoding with several, independent processes.
        This method is recommended if you want to encode on multiple GPUs. It is advised
        to start only one process per GPU. This method works together with encode_multi_process

        :param target_devices: PyTorch target devices, e.g. cuda:0, cuda:1... If None, all available CUDA devices will be used
        :return: Returns a dict with the target processes, an input queue and and output queue.
        """
        if target_devices is None:
            if torch.cuda.is_available():
                target_devices = ['cuda:{}'.format(i) for i in range(torch.cuda.device_count())]
            else:
                logger.info("CUDA is not available. Start 4 CPU worker")
                target_devices = ['cpu']*4

        logger.info("Start multi-process pool on devices: {}".format(', '.join(map(str, target_devices))))
        print('multi instruction', self.instruction)
        ctx = mp.get_context('spawn')
        input_queue = ctx.Queue()
        output_queue = ctx.Queue()
        processes = []

        for cuda_id in target_devices:
            p = ctx.Process(
                target=self._encode_multi_process_worker,
                args=(cuda_id, self, input_queue, output_queue),
                daemon=True
            )
            p.start()
            processes.append(p)

        self.pool = {'input': input_queue, 'output': output_queue, 'processes': processes}

    def stop(self):
        """
        Stops all processes started with start_multi_process_pool
        """
        for p in self.pool['processes']:
            p.terminate()

        for p in self.pool['processes']:
            p.join()
            p.close()

        self.pool['input'].close()
        self.pool['output'].close()

    @staticmethod
    def _encode_multi_process_worker(target_device: str, model, input_queue, results_queue):
        """
        Internal working process to encode sentences in multi-process setup
        """
        while True:
            try:
                id, sentences, kwargs = input_queue.get()
                kwargs.update(device=target_device, show_progress_bar=True, convert_to_numpy=True)
                embeddings = model._encode(sentences, **kwargs)
                results_queue.put([id, embeddings])
            except queue.Empty:
                break

    def encode_multi_process(
        self,
        sentences: List[str],
        **kwargs
    ):
        """
        This method allows to run encode() on multiple GPUs. The sentences are chunked into smaller packages
        and sent to individual processes, which encode these on the different GPUs. This method is only suitable
        for encoding large sets of sentences

        :param sentences: List of sentences
        :param pool: A pool of workers started with SentenceTransformer.start_multi_process_pool
        :param chunk_size: Sentences are chunked and sent to the individual processes. If none, it determine a sensible size.
        :param kwargs: other keyword arguments for model.encode() such as batch_size
        :return: Numpy matrix with all embeddings
        """
        part_size = math.ceil(len(sentences) / len(self.pool["processes"]))
        chunk_size = part_size if part_size < 3200 else 3200  # for retrieval chunk 50000

        logger.debug(f"Chunk data into {math.ceil(len(sentences) / chunk_size)} packages of size {chunk_size}")

        input_queue = self.pool['input']
        last_chunk_id = 0
        chunk = []

        for sentence in sentences:
            chunk.append(sentence)
            if len(chunk) >= chunk_size:
                input_queue.put([last_chunk_id, chunk, kwargs])
                last_chunk_id += 1
                chunk = []

        if len(chunk) > 0:
            input_queue.put([last_chunk_id, chunk, kwargs])
            last_chunk_id += 1

        output_queue = self.pool['output']
        results_list = sorted([output_queue.get() for _ in range(last_chunk_id)], key=lambda x: x[0])
        embeddings = np.concatenate([result[1] for result in results_list])
        return embeddings

    @staticmethod
    def batch_to_device(batch, target_device):
        """
        send a pytorch batch to a device (CPU/GPU)
        """
        for key in batch:
            if isinstance(batch[key], torch.Tensor):
                batch[key] = batch[key].to(target_device)
        return batch

    def _text_length(self, text: Union[List[int], List[List[int]]]):
        """
        Help function to get the length for the input text. Text can be either
        a list of ints (which means a single text as input), or a tuple of list of ints
        (representing several text inputs to the model).
        """

        if isinstance(text, dict):              #{key: value} case
            return len(next(iter(text.values())))
        elif not hasattr(text, '__len__'):      #Object has no len() method
            return 1
        elif len(text) == 0 or isinstance(text[0], int):    #Empty string or list of ints
            return len(text)
        else:
            return sum([len(t) for t in text])      #Sum of length of individual strings

    def _tokenize(self, sentences: List[str], is_query: bool):
        
        batch_dict = self.tokenizer(sentences, max_length=self.max_seq_len - 1, return_attention_mask=False, padding=False, truncation=True)
        batch_dict = self.tokenizer.pad(batch_dict, padding=True, return_attention_mask=True, return_tensors='pt')
        batch_dict['is_causal'] = False
        return batch_dict


    def _encode(
        self,
        sentences: List[str],
        is_query: bool,
        convert_to_numpy: bool = True,
        convert_to_tensor: bool = False,
        device: str = None,
        show_progress_bar: bool = True,
        **kwargs
    ):
        """
        Computes sentence embeddings

        :param sentences: the sentences to embed
        :param batch_size: the batch size used for the computation
        :param show_progress_bar: Output a progress bar when encode sentences
        :param output_value:  Default sentence_embedding, to get sentence embeddings. Can be set to token_embeddings to get wordpiece token embeddings. Set to None, to get all output values
        :param convert_to_numpy: If true, the output is a list of numpy vectors. Else, it is a list of pytorch tensors.
        :param convert_to_tensor: If true, you get one large tensor as return. Overwrites any setting from convert_to_numpy
        :param device: Which torch.device to use for the computation
        :param normalize_embeddings: If set to true, returned vectors will have length 1. In that case, the faster dot-product (util.dot_score) instead of cosine similarity can be used.

        :return:
           By default, a list of tensors is returned. If convert_to_tensor, a stacked tensor is returned. If convert_to_numpy, a numpy matrix is returned.
        """
        self.model.eval()

        if convert_to_tensor:
            convert_to_numpy = False

        input_was_string = False
        if isinstance(sentences, str) or not hasattr(sentences, '__len__'): #Cast an individual sentence to a list with length 1
            sentences = [sentences]
            input_was_string = True

        if device is None:
            device = self._target_device

        self.model.to(device)

        all_embeddings = []
        length_sorted_idx = np.argsort([-self._text_length(s) for s in sentences])
        sentences_sorted = [sentences[idx] for idx in length_sorted_idx]

        for start_index in trange(0, len(sentences), self.batch_size, desc="Batches", disable=not show_progress_bar):
            sentences_batch = sentences_sorted[start_index:start_index + self.batch_size]
            features = self._tokenize(sentences_batch, is_query)
            features = self.batch_to_device(features, device)

            with torch.no_grad():
                embeddings = self.model(**features)

                if self.normalize_embeddings:
                    embeddings = torch.nn.functional.normalize(embeddings, p=2, dim=1)

                # fixes for #522 and #487 to avoid oom problems on gpu with large datasets
                if convert_to_numpy:
                    embeddings = embeddings.cpu()

                all_embeddings.extend(embeddings)

        all_embeddings = [all_embeddings[idx] for idx in np.argsort(length_sorted_idx)]

        if convert_to_tensor:
            all_embeddings = torch.stack(all_embeddings)
        elif convert_to_numpy:
            #all_embeddings = np.asarray([emb.numpy() for emb in all_embeddings])
            all_embeddings = np.asarray([emb.to(torch.float).numpy() for emb in all_embeddings])
        if input_was_string:
            all_embeddings = all_embeddings[0]

        return all_embeddings

    def encode(
        self,
        sentences: List[str],
        is_query: Optional[bool] = None,
        convert_to_tensor: bool = False,
        **kwargs
    ):
        is_query = self.default_query if is_query is None else is_query
        if is_query and self.instruction:
           sentences = [self.instruction + sent for sent in sentences]
        kwargs.update(is_query=is_query)
        if self.pool is not None:
            kwargs.update(show_progress_bar=False)
            embeddings = self.encode_multi_process(sentences, **kwargs)
            if convert_to_tensor:
                embeddings = torch.from_numpy(embeddings)
                if self.mp_tensor_to_cuda and torch.cuda.is_available():
                    embeddings = embeddings.to(torch.device('cuda'))  # default 0-th gpu
            return embeddings

        return self._encode(sentences, convert_to_tensor=convert_to_tensor, **kwargs)

    def encode_queries(self, queries: List[str], **kwargs):
        is_query = self.default_query if self.force_default else True
        return self.encode(queries, is_query=is_query, **kwargs)

    def encode_corpus(self, corpus: List[Dict[str, str]], **kwargs):
        # borrowed from mteb.abstasks.AbsTaskRetrieval.DRESModel
        if type(corpus) is dict:
            sentences = [
                (corpus["title"][i] + self.sep + corpus["text"][i]).strip()
                if "title" in corpus
                else corpus["text"][i].strip()
                for i in range(len(corpus["text"]))
            ]
        elif isinstance(corpus[0], dict):
            sentences = [
                (doc["title"] + self.sep + doc["text"]).strip() if "title" in doc else doc["text"].strip()
                for doc in corpus
            ]
        else:
            sentences = corpus
        is_query = self.default_query if self.force_default else False
        return self.encode(sentences, is_query=is_query, **kwargs)

def main(args):
    tokenizer = AutoTokenizer.from_pretrained(args.model, trust_remote_code=True)
    encoder = Encoder(args.model, args.pooling)
    model = Wrapper(
        tokenizer, encoder,
        batch_size=args.batch_size,
        max_seq_len=args.max_seq_len,
        normalize_embeddings=args.norm
    )

    if args.task == 'cmteb':
        task_names = args.tasknames
        lang = ['zh','zh-CN']
    else:
        task_names = [args.task]
        lang = ['en','zh','zh-CN']
    for task in task_names:
        evaluation = MTEB(tasks=[task], task_langs=lang)
        task_cls = evaluation.tasks[0]
        task_name: str = task_cls.description['name']
        task_type: str = task_cls.description['type']
        instruction = get_task_def_by_task_name_and_type(task_name, task_type)
        print("instruction:", instruction)
        model.instruction = get_detailed_instruct(instruction)
        print("get_detailed_instruct:", get_detailed_instruct(instruction))
        if task == 'MSMARCO':
            eval_splits = ["dev"]
        elif task in args.tasknames:
            eval_splits = task_cls.description['eval_splits']
        else:
            eval_splits = ["test"]

        evaluation.run(model, output_folder=args.output_dir, eval_splits=eval_splits)
        print('\n')


if __name__ == "__main__":
    _PARSER = argparse.ArgumentParser()
    _PARSER.add_argument("-m", "--model", type=str, default=None)
    _PARSER.add_argument("--pooling", type=str, default='last')
    _PARSER.add_argument("--output_dir", type=str, default=None)
    _PARSER.add_argument("--default_type", type=str, default='query')
    _PARSER.add_argument("--max_seq_len", type=int, default=512)
    _PARSER.add_argument("-b", "--batch_size", type=int, default=96)
    _PARSER.add_argument("-t", "--task", type=str, default="cmteb")  # None for running default tasks
    _PARSER.add_argument("-tn", "--tasknames", nargs='+', default=['CmedqaRetrieval', 'CovidRetrieval', 'EcomRetrieval', 'DuRetrieval', 'MedicalRetrieval', 'MMarcoRetrieval', 'T2Retrieval', 'VideoRetrieval'] )
    _PARSER.add_argument("--norm", action="store_true")
    _ARGS = _PARSER.parse_args()
    main(_ARGS)