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

Method encode

usage/infer_llm_embedding.py:50–81  ·  view source on GitHub ↗
(self, sentences_batch, instruction)

Source from the content-addressed store, hash-verified

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