mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-11-11 11:26:34 +00:00
add a convert_token_to_id function for photomaker to extract trigger word's token id
This commit is contained in:
1 parent
df7f64243b
commit
ad7ec45ec1
2 files changed
+35
-1
No files matched your search
@@ -166,7 +166,7 @@ public:
|
|||||||
}
|
}
|
||||||
encoder_len = i;
|
encoder_len = i;
|
||||||
|
|
||||||
auto it = encoder.find(utf8_to_utf32("img"));
|
auto it = encoder.find(utf8_to_utf32("img</w>"));
|
||||||
if (it != encoder.end()) {
|
if (it != encoder.end()) {
|
||||||
printf(" trigger word img already in vocab \n");
|
printf(" trigger word img already in vocab \n");
|
||||||
}else{
|
}else{
|
||||||
@@ -1215,6 +1215,34 @@ struct FrozenCLIPEmbedderWithCustomWords : public GGMLModule {
|
|||||||
return tokenize(text, text_model.max_position_embeddings, padding);
|
return tokenize(text, text_model.max_position_embeddings, padding);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<int> convert_token_to_id(std::string text) {
|
||||||
|
auto on_new_token_cb = [&](std::string& str, std::vector<int32_t>& bpe_tokens) -> bool {
|
||||||
|
size_t word_end = str.find(",");
|
||||||
|
std::string embd_name = word_end == std::string::npos ? str : str.substr(0, word_end);
|
||||||
|
embd_name = trim(embd_name);
|
||||||
|
std::string embd_path = get_full_path(text_model.embd_dir, embd_name + ".pt");
|
||||||
|
if (embd_path.size() == 0) {
|
||||||
|
embd_path = get_full_path(text_model.embd_dir, embd_name + ".ckpt");
|
||||||
|
}
|
||||||
|
if (embd_path.size() == 0) {
|
||||||
|
embd_path = get_full_path(text_model.embd_dir, embd_name + ".safetensors");
|
||||||
|
}
|
||||||
|
if (embd_path.size() > 0) {
|
||||||
|
if (text_model.load_embedding(embd_name, embd_path, bpe_tokens)) {
|
||||||
|
if (word_end != std::string::npos) {
|
||||||
|
str = str.substr(word_end);
|
||||||
|
} else {
|
||||||
|
str = "";
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
std::vector<int> curr_tokens = tokenizer.encode(text, on_new_token_cb);
|
||||||
|
return curr_tokens;
|
||||||
|
}
|
||||||
|
|
||||||
std::pair<std::vector<int>, std::vector<float>> tokenize(std::string text,
|
std::pair<std::vector<int>, std::vector<float>> tokenize(std::string text,
|
||||||
size_t max_length = 0,
|
size_t max_length = 0,
|
||||||
bool padding = false) {
|
bool padding = false) {
|
||||||
|
|||||||
@@ -585,6 +585,12 @@ public:
|
|||||||
int height,
|
int height,
|
||||||
bool force_zero_embeddings = false) {
|
bool force_zero_embeddings = false) {
|
||||||
cond_stage_model.set_clip_skip(clip_skip);
|
cond_stage_model.set_clip_skip(clip_skip);
|
||||||
|
auto image_tokens = cond_stage_model.convert_token_to_id(trigger_word);
|
||||||
|
printf(" length of image tokens: %lu \n", image_tokens.size());
|
||||||
|
if(image_tokens.size() == 1){
|
||||||
|
printf(" image token id is: %d \n", image_tokens[0]);
|
||||||
|
}
|
||||||
|
|
||||||
auto tokens_and_weights = cond_stage_model.tokenize(text, true);
|
auto tokens_and_weights = cond_stage_model.tokenize(text, true);
|
||||||
std::vector<int>& tokens = tokens_and_weights.first;
|
std::vector<int>& tokens = tokens_and_weights.first;
|
||||||
std::vector<float>& weights = tokens_and_weights.second;
|
std::vector<float>& weights = tokens_and_weights.second;
|
||||||
|
|||||||
Reference in new issue
Block a user