Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I get input embeddings? #224

Closed
StrikingLoo opened this issue Mar 17, 2023 · 9 comments
Closed

How do I get input embeddings? #224

StrikingLoo opened this issue Mar 17, 2023 · 9 comments
Labels
question Further information is requested

Comments

@StrikingLoo
Copy link
Contributor

I am trying to output just the sentence embedding for a given input, instead of any new generated text. I think this should be rather straightforward but figured someone more familiar with the codebase could help me.

I just want to return the sentence embedding vector and stop execution for a given input.

I am almost sure the place where I want to make the embedding is right after norm but before lm_head, and I think they will be in inpL if I run

ggml_build_forward_expand(&gf, inpL);
ggml_graph_compute       (ctx0, &gf);

However I am confused by the struct and not sure how to get the sentence embedding itself. I understand it should be some index of ggml_get_data(inpL), but don't get which index, and that is why I come to you. Would anyone lend me a hand?

@gjmulder gjmulder added the question Further information is requested label Mar 17, 2023
@j-f1
Copy link
Collaborator

j-f1 commented Mar 17, 2023

I believe you can get the embedding using llama_tokenize which only requires the gpt_vocab object and the text to tokenize.

@setzer22
Copy link

setzer22 commented Mar 17, 2023

I believe you can get the embedding using llama_tokenize which only requires the gpt_vocab object and the text to tokenize.

Those wouldn't be embeddings, those would just be tokenized values. The embeddings are obtained in the call to get_rows inside llama_eval. That's where you fetch the row from tok_embeddings corresponding to the indices (i.e. token ids) you get from tokenize.

@StrikingLoo
Copy link
Contributor Author

StrikingLoo commented Mar 17, 2023

Those are the token embeddings for the window (line 582 if it didn't change from my last pull). I wanted the sentence representation, which would be the embedding of the last word after doing one feedforward through the transformer.
Again I think they would be somewhere after line 722 before line 724 in main.cpp, I just don't understand how to access them. I would just need the minimal code that prints the values for the embedding of the last token in that output.

@MillionthOdin16
Copy link

Is this what you're thinking?

        /// (loop handling each layer) ///

        // input for next layer
        inpL = cur;
        
    }

    // norm
    {
        inpL = ggml_rms_norm(ctx0, inpL);

        // inpL = norm*inpL
        inpL = ggml_mul(ctx0,
                        ggml_repeat(ctx0, model.norm, inpL),
                        inpL);
        
        std::vector<float> embedding_representation;    
        embedding_representation.resize(n_embd);
        memcpy(embedding_representation.data(), (float *) ggml_get_data(inpL) + (n_embd * (N - 1)), sizeof(float) * n_embd);
    }
    
    /// (forward expand) ///

@StrikingLoo
Copy link
Contributor Author

StrikingLoo commented Mar 17, 2023 via email

@Ronsor
Copy link
Contributor

Ronsor commented Mar 18, 2023

I know I want it. It should probably be folded into the in-progress API implementation too at #77

@adriacabeza
Copy link

Would be awesome to have the option to get the embeddings as well as an option. Did you end up doing a PR for it? @StrikingLoo

@StrikingLoo
Copy link
Contributor Author

Hi both of you. I did! It's here.
#282
But it's not working yet. I am not sure what is failing. I added the console flag as an argument, and everything works fine outside of the embedding mode, but on embedding mode I get an execution error. Without the flag it was 'working' but I an't make this one work. If anyone can take a look at it and tell me what I'm missing that would be great

@louis030195
Copy link

@StrikingLoo by any chance did you explore evaluation of these embeddings?
I was looking at the codebase out there https://github.com/UKPLab/sentence-transformers/blob/master/sentence_transformers/evaluation/SentenceEvaluator.py
and was thinking on how it could be integrated

Repository owner locked and limited conversation to collaborators Apr 16, 2023
@prusnak prusnak converted this issue into discussion #1013 Apr 16, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

8 participants