Back to the room

Pass 3 of 3 · Deep dive of PHAROSPass 1 · OverviewPass 2 · How it works

Code Observatory / Source

Embed the question

ONNX inference on the CPU, with the model's pooling and L2 normalisation.

PINNED SOURCE Copied from the repository at this commit. Source, not an execution.

PHAROS at 975a38dMIT license

OnnxEmbedder.encode_rawbackend/app/codeintel/embeddings.py, lines 341–364Open source
    def embed_query(self, text: str) -> list[float]:        return self.encode_raw([self.query_prefix + text])[0]    def encode_raw(self, texts: list[str]) -> list[list[float]]:        import numpy as np        self._load()        out: list[list[float]] = []        for i in range(0, len(texts), self.batch_size):            batch = self._tokenizer.encode_batch(texts[i : i + self.batch_size])            ids = np.array([e.ids for e in batch], dtype=np.int64)            mask = np.array([e.attention_mask for e in batch], dtype=np.int64)            feeds = {"input_ids": ids, "attention_mask": mask}            if "token_type_ids" in self._input_names:                feeds["token_type_ids"] = np.zeros_like(ids)            hidden = self._session.run(None, feeds)[0]  # (batch, seq, dim)            if self.pooling == "cls":                pooled = hidden[:, 0, :].astype(np.float32)            else:                m = mask[..., None].astype(np.float32)                pooled = (hidden * m).sum(axis=1) / np.clip(m.sum(axis=1), 1e-9, None)            pooled /= np.clip(np.linalg.norm(pooled, axis=1, keepdims=True), 1e-12, None)            out.extend(_pad(v.tolist(), self.dim) for v in pooled)        return out

Where this comes from

Commit
975a38d5d17839c26ee219295a5be6ac66b6d910
File
backend/app/codeintel/embeddings.py
Lines
341–364
Git blob
df7f8202f23ca6ad2c57aca5f09b435eb6486d79
License
MIT, © 2024–2026 Pharos Contributors. Notice

Read it on GitHub at this commit

Check it yourself

From a clone of the repository, the first command prints the blob above and the second prints these lines.

git rev-parse 975a38d:backend/app/codeintel/embeddings.py
git show 975a38d:backend/app/codeintel/embeddings.py | sed -n '341,364p'