-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from emt0re0/stablelm-2-1.6b
feat: Stablelm 2 1.6b support
- Loading branch information
Showing
11 changed files
with
100,471 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include <iostream> | ||
#include "cmdline.h" | ||
#include "models/stablelm/modeling_stablelm.hpp" | ||
#include "models/stablelm/tokenization_stablelm.hpp" | ||
#include "processor/PostProcess.hpp" | ||
|
||
using namespace mllm; | ||
|
||
int main(int argc, char **argv) { | ||
cmdline::parser cmdParser; | ||
cmdParser.add<string>("vocab", 'v', "specify mllm tokenizer model path", false, "../vocab/stablelm_vocab.mllm"); | ||
cmdParser.add<string>("merge", 'm', "specify mllm merge path", false, "../vocab/stablelm_merges.txt"); | ||
cmdParser.add<string>("model", 'o', "specify mllm model path", false, "../models/stablelm-2-1.6b-chat-q4_k.mllm"); | ||
cmdParser.add<int>("limits", 'l', "max KV cache size", false, 400); | ||
cmdParser.add<int>("thread", 't', "num of threads", false, 4); | ||
cmdParser.parse_check(argc, argv); | ||
|
||
string vocab_path = cmdParser.get<string>("vocab"); | ||
string merge_path = cmdParser.get<string>("merge"); | ||
string model_path = cmdParser.get<string>("model"); | ||
int tokens_limit = cmdParser.get<int>("limits"); | ||
CPUBackend::cpu_threads = cmdParser.get<int>("thread"); | ||
|
||
auto tokenizer = StableLMTokenizer(vocab_path, merge_path); | ||
|
||
string system_prompt_start = "<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n"; | ||
string system_prompt_end = "<|im_end|>\n<|im_start|>assistant\n"; | ||
|
||
StableLMConfig config(tokens_limit, "1.6B", HFHUBROPE); | ||
auto model = StableLMModel(config); | ||
model.load(model_path); | ||
|
||
vector<string> in_strs = { | ||
" Hello, who are you?", | ||
" What can you do?", | ||
"Please introduce Beijing University of Posts and Telecommunications."}; | ||
|
||
for (int i = 0; i < in_strs.size(); ++i) { | ||
const auto& in_str_origin = in_strs[i]; | ||
auto in_str = system_prompt_start + in_str_origin + system_prompt_end; | ||
std::cout << "[Q] " << in_str_origin << std::endl; | ||
auto input_tensor = tokenizer.tokenize(in_str, i); | ||
std::cout << "[A] " << std::flush; | ||
for (int step = 0; step < 100; step++) { | ||
auto result = model({input_tensor}); | ||
auto outputs = tokenizer.detokenize(result[0]); | ||
auto out_string = outputs.first; | ||
auto out_token = outputs.second; | ||
if (out_token == 100278) { | ||
break; | ||
} | ||
size_t pos = 0; | ||
while ((pos = out_string.find("Ċ", pos)) != std::string::npos) { | ||
out_string.replace(pos, 2, " "); | ||
} | ||
pos = 0; | ||
while ((pos = out_string.find("Ġ", pos)) != std::string::npos) { | ||
out_string.replace(pos, 2, " "); | ||
} | ||
|
||
std::cout << out_string << std::flush; | ||
chatPostProcessing(out_token, input_tensor, {}); | ||
} | ||
printf("\n"); | ||
} | ||
|
||
return 0; | ||
} |
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,69 @@ | ||
#ifndef CONFIG_STABLELM_HPP | ||
#define CONFIG_STABLELM_HPP | ||
#include "models/transformer/configuration_transformer.hpp" | ||
|
||
using namespace mllm; | ||
|
||
class stablelmNameConfig : public TransformerNameConfig { | ||
public: | ||
std::string blk_name; | ||
std::string token_embd_name; | ||
std::string post_norm_name; | ||
std::string lm_head_name; | ||
std::string _gate_proj_name; | ||
|
||
void init(RoPEType type = HFHUBROPE) { | ||
switch (type) { | ||
case HFHUBROPE: { | ||
blk_name = "model.layers."; | ||
_attn_base_name = "self_attn."; | ||
_ffn_base_name = "mlp."; | ||
_q_proj_name = "q_proj"; | ||
_k_proj_name = "k_proj"; | ||
_v_proj_name = "v_proj"; | ||
_o_proj_name = "o_proj"; | ||
_gate_proj_name = "gate_proj"; | ||
_up_proj_name = "up_proj"; | ||
_down_proj_name = "down_proj"; | ||
_attn_norm_name = "input_layernorm"; | ||
_ffn_norm_name = "post_attention_layernorm"; | ||
token_embd_name = "model.embed_tokens"; | ||
post_norm_name = "model.norm"; | ||
lm_head_name = "lm_head"; | ||
break; | ||
} | ||
default: { | ||
throw std::runtime_error("Unsupported llama type"); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
class StableLMConfig { | ||
public: | ||
int vocab_size{}; | ||
int hidden_dim{}; | ||
int head_size{}; | ||
int ffn_hidden{}; | ||
int block_num{}; | ||
RoPEType RoPE_type; | ||
int cache_limit{}; | ||
stablelmNameConfig names_config; | ||
|
||
explicit StableLMConfig(int token_limit, string billions = "1.6B", RoPEType type = HFHUBROPE, int vocab = 100352) { | ||
names_config.init(type); | ||
vocab_size = vocab; | ||
if (billions == "1.6B" || billions == "1.6b") { | ||
hidden_dim = 2048; | ||
head_size = 32; | ||
ffn_hidden = 5632; | ||
block_num = 24; | ||
} else { | ||
throw std::runtime_error("Unsupported model size"); | ||
} | ||
RoPE_type = type; | ||
cache_limit = token_limit; | ||
} | ||
}; | ||
|
||
#endif // |
Oops, something went wrong.