-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Judd
committed
Jun 23, 2024
1 parent
e97d1bc
commit e2d5342
Showing
6 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
typedef llama::v3::Config Config; | ||
|
||
class ChatHistoryEncoder : public BaseHistoryEncoder | ||
{ | ||
public: | ||
void append_sys_prompt(std::vector<int> &ids) const override; | ||
void append_pair(int round_idx, const std::string &user, const std::string &ai, std::vector<int> &ids) const override; | ||
void do_append_user(int round_idx, const std::string &user, std::vector<int> &ids) const override; | ||
}; | ||
|
||
static ChatHistoryEncoder _chat_encoder; | ||
|
||
class Tokenizer : public llama::v2::Tokenizer | ||
{ | ||
public: | ||
Tokenizer(const Config &config) | ||
: llama::v2::Tokenizer(config, &_chat_encoder) | ||
{ | ||
sys_prompt = ""; | ||
resevered_0_token_id = 3; | ||
resevered_1_token_id = 4; | ||
} | ||
public: | ||
int resevered_0_token_id; | ||
int resevered_1_token_id; | ||
}; | ||
|
||
class ConditionalGeneration : public llama::v3::ConditionalGeneration | ||
{ | ||
public: | ||
ConditionalGeneration() = default; | ||
ConditionalGeneration(const Config &config) | ||
: llama::v3::ConditionalGeneration(config, ModelType::MODEL_TYPE_INDEX) | ||
{} | ||
}; | ||
|
||
void ChatHistoryEncoder::append_sys_prompt(std::vector<int> &ids) const | ||
{ | ||
if (tokenizer->get_system_prompt().size() > 0) | ||
{ | ||
ids.push_back(tokenizer->pad_token_id); | ||
tokenizer->encode(tokenizer->get_system_prompt(), ids); | ||
} | ||
|
||
} | ||
|
||
void ChatHistoryEncoder::append_pair(int round_idx, const std::string &user, const std::string &ai, std::vector<int> &ids) const | ||
{ | ||
Tokenizer *tok = dynamic_cast<Tokenizer *>(tokenizer); | ||
do_append_user(round_idx, user, ids); | ||
tok->encode(ai, ids); | ||
} | ||
|
||
void ChatHistoryEncoder::do_append_user(int round_idx, const std::string &user, std::vector<int> &ids) const | ||
{ | ||
Tokenizer *tok = dynamic_cast<Tokenizer *>(tokenizer); | ||
ids.push_back(tok->resevered_0_token_id); | ||
tok->encode(user, ids); | ||
ids.push_back(tok->resevered_1_token_id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters