def init_query_encoder(encoder, tokenizer_name, topics_name, encoded_queries, device, prefix): encoded_queries_map = { 'msmarco-passage-dev-subset': 'tct_colbert-msmarco-passage-dev-subset', 'dpr-nq-dev': 'dpr_multi-nq-dev', 'dpr-nq-test': 'dpr_multi-nq-test', 'dpr-trivia-dev': 'dpr_multi-trivia-dev', 'dpr-trivia-test': 'dpr_multi-trivia-test', 'dpr-wq-test': 'dpr_multi-wq-test', 'dpr-squad-test': 'dpr_multi-squad-test', 'dpr-curated-test': 'dpr_multi-curated-test' } if encoder: if 'dkrr' in encoder: return DkrrDprQueryEncoder(encoder_dir=encoder, device=device, prefix=prefix) elif 'dpr' in encoder: return DprQueryEncoder(encoder_dir=encoder, tokenizer_name=tokenizer_name, device=device) elif 'bpr' in encoder: return BprQueryEncoder(encoder_dir=encoder, tokenizer_name=tokenizer_name, device=device) elif 'tct_colbert' in encoder: return TctColBertQueryEncoder(encoder_dir=encoder, tokenizer_name=tokenizer_name, device=device) elif 'ance' in encoder: return AnceQueryEncoder(encoder_dir=encoder, tokenizer_name=tokenizer_name, device=device) elif 'sentence' in encoder: return AutoQueryEncoder(encoder_dir=encoder, tokenizer_name=tokenizer_name, device=device, pooling='mean', l2_norm=True) else: return AutoQueryEncoder(encoder_dir=encoder, tokenizer_name=tokenizer_name, device=device) if encoded_queries: if os.path.exists(encoded_queries): if 'bpr' in encoded_queries: return BprQueryEncoder(encoded_query_dir=encoded_queries) else: return QueryEncoder(encoded_queries) else: if 'bpr' in encoded_queries: return BprQueryEncoder.load_encoded_queries(encoded_queries) else: return QueryEncoder.load_encoded_queries(encoded_queries) if topics_name in encoded_queries_map: return QueryEncoder.load_encoded_queries( encoded_queries_map[topics_name]) raise ValueError(f'No encoded queries for topic {topics_name}')
def test_msmarco_passage_ance_encoded_queries(self): encoder = QueryEncoder.load_encoded_queries( 'ance-msmarco-passage-dev-subset') topics = get_topics('msmarco-passage-dev-subset') for t in topics: self.assertTrue(topics[t]['title'] in encoder.embedding)
def test_trivia_test_ance_encoded_queries(self): encoder = QueryEncoder.load_encoded_queries('dpr_multi-trivia-test') topics = get_topics('dpr-trivia-test') for t in topics: self.assertTrue(topics[t]['title'] in encoder.embedding)
def test_msmarco_doc_ance_bf_encoded_queries(self): encoder = QueryEncoder.load_encoded_queries( 'ance_maxp-msmarco-doc-dev') topics = get_topics('msmarco-doc-dev') for t in topics: self.assertTrue(topics[t]['title'] in encoder.embedding)
def test_dpr_squad_test_encoded_queries(self): encoder = QueryEncoder.load_encoded_queries('dpr_multi-squad-test') topics = get_topics('dpr-squad-test') for t in topics: self.assertTrue(topics[t]['title'] in encoder.embedding)
def test_msmarco_passage_tct_colbert_v2_hnp_encoded_queries(self): encoder = QueryEncoder.load_encoded_queries( 'tct_colbert-v2-hnp-msmarco-passage-dev-subset') topics = get_topics('msmarco-passage-dev-subset') for t in topics: self.assertTrue(topics[t]['title'] in encoder.embedding)