Model generated embeddings is different than the one generated by the built in supabase generated embeddings.

#6
by jjl777777 - opened

For context, I want to use this model in two places and generate the same embeddings.
In my Supabase typescript edge function code I generate the embedding using these lines of code
const session = new Supabase.ai.Session("gte-small");
const output = await session.run(title, {
mean_pool: true,
normalize: true,
});

And in python I do the following

def average_pool(last_hidden_states: Tensor,
attention_mask: Tensor) -> Tensor:
last_hidden = last_hidden_states.masked_fill(~attention_mask[..., None].bool(), 0.0)
return last_hidden.sum(dim=1) / attention_mask.sum(dim=1)[..., None]

tokenizer = AutoTokenizer.from_pretrained("Supabase/gte-small")
model = AutoModel.from_pretrained("Supabase/gte-small")

batch_dict = tokenizer(titles, max_length=512, padding=True, truncation=False, return_tensors='pt')
batch_dict = {k: v.to(device) for k, v in batch_dict.items()}
#generate embeddings
outputs = model(**batch_dict)
embeddings = average_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
embeddings = F.normalize(embeddings, p=2, dim=1)
#convert embeddings to list
embeddings_list = embeddings.detach().cpu().numpy().tolist()

Based on my understanding, these two method should generate the same embeddings but when I printed out the embeddings they are slightly different
for some examples I've tested. The cosine similarity of the embedding generated by the edge function and the one by the hugging face model in python turned out to be higher than 0.999 so they are very similar.

I was wondering what might cause this? Is there something significantly wrong with my code? And is the model used by supabase the same version as the most recent model here?

I saw that in previous discussions https://huggingface.co/Supabase/gte-small/discussions/2 the model file that Supabase uses is the onnx files which is different from the model card that pytorch is using and they have different sizes. Could this be related to why the embeddings we see generated by Supabase.ai.Session("gte-small") is different from the one generated by pytorch?
This is the code I am currently looking for the supabase implementations https://github.com/supabase/edge-runtime/blob/main/crates/sb_ai/lib.rs#L45.

After looking at previous discussions, I think most of the differences are contributed to the different number of bits between the onnx and pytorch models

https://huggingface.co/Supabase/gte-small/discussions/2

jjl777777 changed discussion status to closed

Sign up or log in to comment