MCPcopy Create free account
hub / github.com/TencentCloudADP/youtu-embedding / encode

Method encode

test_transformers_local.py:47–80  ·  view source on GitHub ↗
(self, sentences_batch, instruction)

Source from the content-addressed store, hash-verified

45
46 @torch.no_grad()
47 def encode(self, sentences_batch, instruction):
48 inputs = self.tokenizer(
49 sentences_batch,
50 padding=True,
51 truncation=True,
52 return_tensors="pt",
53 max_length=self.max_length,
54 add_special_tokens=True,
55 )
56 # Move inputs to device
57 inputs = {k: v.to(self.device) for k, v in inputs.items()}
58
59 with torch.no_grad():
60 outputs = self.model(**inputs)
61 last_hidden_state = outputs[0]
62
63 instruction_tokens = self.tokenizer(
64 instruction,
65 padding=False,
66 truncation=True,
67 max_length=self.max_length,
68 add_special_tokens=True,
69 )["input_ids"]
70 if len(np.shape(np.array(instruction_tokens))) == 1:
71 inputs["attention_mask"][:, :len(instruction_tokens)] = 0
72 else:
73 instruction_length = [len(item) for item in instruction_tokens]
74 assert len(instruction) == len(sentences_batch)
75 for idx in range(len(instruction_length)):
76 inputs["attention_mask"][idx, :instruction_length[idx]] = 0
77
78 embeddings = self.mean_pooling(last_hidden_state, inputs["attention_mask"])
79 embeddings = torch.nn.functional.normalize(embeddings, dim=-1)
80 return embeddings
81
82 def encode_queries(self, queries):
83 queries = queries if isinstance(queries, list) else [queries]

Callers 2

encode_queriesMethod · 0.95
encode_passagesMethod · 0.95

Calls 1

mean_poolingMethod · 0.95

Tested by

no test coverage detected