-
Notifications
You must be signed in to change notification settings - Fork 10k
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
Embedding parameters #7458
Embedding parameters #7458
Conversation
--embd-normalize --embd-output-format --embd-separator description in the README.md
fix tipo
@YannFollet is this PR all good? Have a quick lookover. Had to do some merging to keep this in sync. Marking as merging soon and will revisit later to see if anyone want to still comment about it. |
@YannFollet this PR has gone out of sync. Can you synchronize this with the main branch? It appears there is no other objection and GG is happy with it. But it's out of sync so cannot easily merge. |
@mofosyne Sorry haven't seen your message, I have done the merge with master |
examples/embedding/embedding.cpp
Outdated
|
||
// print cosine similarity matrix | ||
if (n_prompts > 1) { | ||
if (params.embd_out=="") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (params.embd_out=="") { | |
if (params.embd_out.empty()) { |
common/common.h
Outdated
int32_t embd_normalize = 2; // normalisation for embendings (-1=none, 0=max absolute, 1=taxicab, 2=euclidean, >2=p-norm) | ||
std::string embd_out = ""; // empty = default, "array" = [] or [[],[]...], "json" = openai style, "json+" = same "json" + cosine similarity matrix | ||
std::string embd_sep = "\n"; // separator of embendings |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These group of parameters should be moved below in their own section, similar to what we do for other examples:
// embedding
int32_t embd_normalize = 2; // normalisation for embedings (-1=none, 0=max absolute, 1=taxicab, 2=euclidean, >2=p-norm)
std::string embd_out = ""; // empty = default, "array" = [] or [[],[]...], "json" = openai style, "json+" = same "json" + cosine similarity matrix
std::string embd_sep = "\n"; // separator of embedings
CHECK_ARG | ||
params.embd_sep = argv[i]; | ||
return true; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expand gpt_params_print_usage
for these parameters
common/common.h
Outdated
@@ -377,7 +380,7 @@ void llama_kv_cache_dump_view_seqs(const llama_kv_cache_view & view, int row_siz | |||
// Embedding utils | |||
// | |||
|
|||
void llama_embd_normalize(const float * inp, float * out, int n); | |||
void llama_embd_normalize(const float * inp, float * out, int n, int embd_norm = 2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
void llama_embd_normalize(const float * inp, float * out, int n, int embd_norm = 2); | |
void llama_embd_normalize(const float * inp, float * out, int n, int embd_norm = 2); |
examples/embedding/embedding.cpp
Outdated
if (params.embd_normalize==0) | ||
fprintf(stdout, "%6.0f ", emb[j * n_embd + i]); | ||
else | ||
fprintf(stdout, "%9.6f ", emb[j * n_embd + i]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (params.embd_normalize==0) | |
fprintf(stdout, "%6.0f ", emb[j * n_embd + i]); | |
else | |
fprintf(stdout, "%9.6f ", emb[j * n_embd + i]); | |
if (params.embd_normalize == 0) { | |
fprintf(stdout, "%6.0f ", emb[j * n_embd + i]); | |
} else { | |
fprintf(stdout, "%9.6f ", emb[j * n_embd + i]); | |
} |
examples/embedding/embedding.cpp
Outdated
float sim = llama_embd_similarity_cos(emb + i * n_embd, emb + j * n_embd, n_embd); | ||
fprintf(stdout, "%6.2f ", sim); | ||
} | ||
fprintf(stdout, "%1.10s",prompts[i].c_str()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fprintf(stdout, "%1.10s",prompts[i].c_str()); | |
fprintf(stdout, "%1.10s", prompts[i].c_str()); |
examples/embedding/embedding.cpp
Outdated
if (params.embd_out=="json" || params.embd_out=="json+" || params.embd_out=="array") { | ||
const bool notArray = params.embd_out!="array"; | ||
|
||
fprintf(stdout, notArray?"{\n \"object\": \"list\",\n \"data\": [\n":"["); | ||
for (int j = 0;;) { // at least one iteration (one prompt) | ||
if (notArray) fprintf(stdout, " {\n \"object\": \"embedding\",\n \"index\": %d,\n \"embedding\": ",j); | ||
fprintf(stdout, "["); | ||
for (int i = 0;;) { // at least one iteration (n_embd > 0) | ||
fprintf(stdout, params.embd_normalize==0?"%1.0f":"%1.7f", emb[j * n_embd + i]); | ||
i++; | ||
if (i < n_embd) fprintf(stdout, ","); else break; | ||
} | ||
fprintf(stdout, notArray?"]\n }":"]"); | ||
j++; | ||
if (j < n_prompts) fprintf(stdout, notArray?",\n":","); else break; | ||
} | ||
fprintf(stdout, notArray?"\n ]":"]\n"); | ||
|
||
if (params.embd_out=="json+" && n_prompts > 1) { | ||
fprintf(stdout, ",\n \"cosineSimilarity\": [\n"); | ||
for (int i = 0;;) { // at least two iteration (n_prompts > 1) | ||
fprintf(stdout, " ["); | ||
for (int j = 0;;) { // at least two iteration (n_prompts > 1) | ||
float sim = llama_embd_similarity_cos(emb + i * n_embd, emb + j * n_embd, n_embd); | ||
fprintf(stdout, "%6.2f", sim); | ||
j++; | ||
if (j < n_prompts) fprintf(stdout, ", "); else break; | ||
} | ||
fprintf(stdout, " ]"); | ||
i++; | ||
if (i < n_prompts) fprintf(stdout, ",\n"); else break; | ||
} | ||
fprintf(stdout, "\n ]"); | ||
} | ||
|
||
if (notArray) fprintf(stdout, "\n}\n"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (params.embd_out=="json" || params.embd_out=="json+" || params.embd_out=="array") { | |
const bool notArray = params.embd_out!="array"; | |
fprintf(stdout, notArray?"{\n \"object\": \"list\",\n \"data\": [\n":"["); | |
for (int j = 0;;) { // at least one iteration (one prompt) | |
if (notArray) fprintf(stdout, " {\n \"object\": \"embedding\",\n \"index\": %d,\n \"embedding\": ",j); | |
fprintf(stdout, "["); | |
for (int i = 0;;) { // at least one iteration (n_embd > 0) | |
fprintf(stdout, params.embd_normalize==0?"%1.0f":"%1.7f", emb[j * n_embd + i]); | |
i++; | |
if (i < n_embd) fprintf(stdout, ","); else break; | |
} | |
fprintf(stdout, notArray?"]\n }":"]"); | |
j++; | |
if (j < n_prompts) fprintf(stdout, notArray?",\n":","); else break; | |
} | |
fprintf(stdout, notArray?"\n ]":"]\n"); | |
if (params.embd_out=="json+" && n_prompts > 1) { | |
fprintf(stdout, ",\n \"cosineSimilarity\": [\n"); | |
for (int i = 0;;) { // at least two iteration (n_prompts > 1) | |
fprintf(stdout, " ["); | |
for (int j = 0;;) { // at least two iteration (n_prompts > 1) | |
float sim = llama_embd_similarity_cos(emb + i * n_embd, emb + j * n_embd, n_embd); | |
fprintf(stdout, "%6.2f", sim); | |
j++; | |
if (j < n_prompts) fprintf(stdout, ", "); else break; | |
} | |
fprintf(stdout, " ]"); | |
i++; | |
if (i < n_prompts) fprintf(stdout, ",\n"); else break; | |
} | |
fprintf(stdout, "\n ]"); | |
} | |
if (notArray) fprintf(stdout, "\n}\n"); | |
if (params.embd_out == "json" || params.embd_out == "json+" || params.embd_out == "array") { | |
const bool notArray = params.embd_out != "array"; | |
fprintf(stdout, notArray ? "{\n \"object\": \"list\",\n \"data\": [\n":"["); | |
for (int j = 0;;) { // at least one iteration (one prompt) | |
if (notArray) fprintf(stdout, " {\n \"object\": \"embedding\",\n \"index\": %d,\n \"embedding\": ",j); | |
fprintf(stdout, "["); | |
for (int i = 0;;) { // at least one iteration (n_embd > 0) | |
fprintf(stdout, params.embd_normalize == 0 ? "%1.0f" : "%1.7f", emb[j * n_embd + i]); | |
i++; | |
if (i < n_embd) fprintf(stdout, ","); else break; | |
} | |
fprintf(stdout, notArray ? "]\n }" : "]"); | |
j++; | |
if (j < n_prompts) fprintf(stdout, notArray ? ",\n" : ","); else break; | |
} | |
fprintf(stdout, notArray ? "\n ]" : "]\n"); | |
if (params.embd_out == "json+" && n_prompts > 1) { | |
fprintf(stdout, ",\n \"cosineSimilarity\": [\n"); | |
for (int i = 0;;) { // at least two iteration (n_prompts > 1) | |
fprintf(stdout, " ["); | |
for (int j = 0;;) { // at least two iteration (n_prompts > 1) | |
float sim = llama_embd_similarity_cos(emb + i * n_embd, emb + j * n_embd, n_embd); | |
fprintf(stdout, "%6.2f", sim); | |
j++; | |
if (j < n_prompts) fprintf(stdout, ", "); else break; | |
} | |
fprintf(stdout, " ]"); | |
i++; | |
if (i < n_prompts) fprintf(stdout, ",\n"); else break; | |
} | |
fprintf(stdout, "\n ]"); | |
} | |
if (notArray) fprintf(stdout, "\n}\n"); |
group of parameters // embedding print usage for embedding parameters
This reverts commit 646ef4a.
* add parameters for embeddings --embd-normalize --embd-output-format --embd-separator description in the README.md * Update README.md fix tipo * Trailing whitespace * fix json generation, use " not ' * fix merge master * fix code formating group of parameters // embedding print usage for embedding parameters --------- Co-authored-by: Brian <[email protected]>
* add parameters for embeddings --embd-normalize --embd-output-format --embd-separator description in the README.md * Update README.md fix tipo * Trailing whitespace * fix json generation, use " not ' * fix merge master * fix code formating group of parameters // embedding print usage for embedding parameters --------- Co-authored-by: Brian <[email protected]>
extra parameters
--embd-normalize$integer$
--embd-output-format$'string'$
--embd-separator$"string"$
examples
Unix-based systems (Linux, macOS, etc.):
Windows: