-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Implement DeepSeek V2 #2744
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
Merged
Merged
Implement DeepSeek V2 #2744
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c3a9775
Add deepseek v2
EricLBuehler cb47324
Fix
EricLBuehler a1dd5a1
Remove unused
EricLBuehler bd8b5c4
Add kv cache
EricLBuehler 66170a0
Remove from cargo.toml
EricLBuehler 5d42e6f
Fix dtype selection logic
EricLBuehler 6df4f13
Fix unnecessary u32->f32->gather->u32
EricLBuehler 1ad7e92
Remove fromstr impl
EricLBuehler b8974e9
Use local scopes for some clarity
EricLBuehler ed0953e
Typo
EricLBuehler 95864a0
Repeat k_pe
EricLBuehler 404d012
Chain calls to remove mut
EricLBuehler f0d466b
Actually, remove all muts
EricLBuehler 1a10d87
Merge branch 'dev_main' into add_deepseekv2
EricLBuehler 67940de
Update readme
EricLBuehler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,40 @@ | ||
# DeepSeek V2 | ||
|
||
DeepSeek V2 an MoE model featuring MLA (Multi-Latent Attention). There is a lite (16B) and a full (236B) model. | ||
|
||
- Context length of **32k tokens** (Lite model), **128k tokens** (full model) | ||
- 64 routed experts (Lite model), 160 routed experts (full model) | ||
|
||
## Running the example | ||
|
||
```bash | ||
$ cargo run --example deepseekv2 --release --features cuda -- --prompt Hello --sample-len 150 | ||
|
||
Generated text: | ||
Write helloworld code in Rust | ||
============================= | ||
|
||
This is a simple example of how to write "Hello, world!" program in Rust. | ||
|
||
## Compile and run | ||
|
||
``bash | ||
$ cargo build --release | ||
Compiling hello-world v0.1.0 (/home/user/rust/hello-world) | ||
Finished release [optimized] target(s) in 0.26s | ||
$ ./target/release/hello-world | ||
Hello, world! | ||
`` | ||
|
||
## Source code | ||
|
||
``rust | ||
fn main() { | ||
println!("Hello, world!"); | ||
} | ||
`` | ||
|
||
## License | ||
|
||
This example is released under the terms | ||
``` |
This file contains hidden or 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,282 @@ | ||
#[cfg(feature = "mkl")] | ||
extern crate intel_mkl_src; | ||
|
||
#[cfg(feature = "accelerate")] | ||
extern crate accelerate_src; | ||
|
||
use anyhow::{Error as E, Result}; | ||
use clap::Parser; | ||
|
||
use candle_transformers::models::deepseek2::{DeepSeekV2, DeepSeekV2Config}; | ||
|
||
use candle::{DType, Device, Tensor}; | ||
use candle_examples::token_output_stream::TokenOutputStream; | ||
use candle_nn::VarBuilder; | ||
use candle_transformers::generation::{LogitsProcessor, Sampling}; | ||
use hf_hub::{api::sync::Api, Repo, RepoType}; | ||
use tokenizers::Tokenizer; | ||
|
||
struct TextGeneration { | ||
model: DeepSeekV2, | ||
device: Device, | ||
tokenizer: TokenOutputStream, | ||
logits_processor: LogitsProcessor, | ||
repeat_penalty: f32, | ||
repeat_last_n: usize, | ||
} | ||
|
||
impl TextGeneration { | ||
#[allow(clippy::too_many_arguments)] | ||
fn new( | ||
model: DeepSeekV2, | ||
tokenizer: Tokenizer, | ||
seed: u64, | ||
temp: Option<f64>, | ||
top_p: Option<f64>, | ||
top_k: Option<usize>, | ||
repeat_penalty: f32, | ||
repeat_last_n: usize, | ||
device: &Device, | ||
) -> Self { | ||
let logits_processor = { | ||
let temperature = temp.unwrap_or(0.); | ||
let sampling = if temperature <= 0. { | ||
Sampling::ArgMax | ||
} else { | ||
match (top_k, top_p) { | ||
(None, None) => Sampling::All { temperature }, | ||
(Some(k), None) => Sampling::TopK { k, temperature }, | ||
(None, Some(p)) => Sampling::TopP { p, temperature }, | ||
(Some(k), Some(p)) => Sampling::TopKThenTopP { k, p, temperature }, | ||
} | ||
}; | ||
LogitsProcessor::from_sampling(seed, sampling) | ||
}; | ||
|
||
Self { | ||
model, | ||
tokenizer: TokenOutputStream::new(tokenizer), | ||
logits_processor, | ||
repeat_penalty, | ||
repeat_last_n, | ||
device: device.clone(), | ||
} | ||
} | ||
|
||
fn run(&mut self, prompt: &str, sample_len: usize) -> Result<()> { | ||
use std::io::Write; | ||
self.tokenizer.clear(); | ||
let mut tokens = self | ||
.tokenizer | ||
.tokenizer() | ||
.encode(prompt, true) | ||
.map_err(E::msg)? | ||
.get_ids() | ||
.to_vec(); | ||
for &t in tokens.iter() { | ||
if let Some(t) = self.tokenizer.next_token(t)? { | ||
print!("{t}") | ||
} | ||
} | ||
std::io::stdout().flush()?; | ||
|
||
let mut generated_tokens = 0usize; | ||
let eos_token = match self.tokenizer.get_token("<|end▁of▁sentence|>") { | ||
Some(token) => token, | ||
None => anyhow::bail!("cannot find the <|end▁of▁sentence|> token"), | ||
}; | ||
let start_gen = std::time::Instant::now(); | ||
for index in 0..sample_len { | ||
let context_size = if index > 0 { 1 } else { tokens.len() }; | ||
let start_pos = tokens.len().saturating_sub(context_size); | ||
let ctxt = &tokens[start_pos..]; | ||
let input = Tensor::new(ctxt, &self.device)?.unsqueeze(0)?; | ||
let logits = self.model.forward(&input, start_pos)?; | ||
let logits = logits.squeeze(0)?.squeeze(0)?.to_dtype(DType::F32)?; | ||
let logits = if self.repeat_penalty == 1. { | ||
logits | ||
} else { | ||
let start_at = tokens.len().saturating_sub(self.repeat_last_n); | ||
candle_transformers::utils::apply_repeat_penalty( | ||
&logits, | ||
self.repeat_penalty, | ||
&tokens[start_at..], | ||
)? | ||
}; | ||
|
||
let next_token = self.logits_processor.sample(&logits)?; | ||
tokens.push(next_token); | ||
generated_tokens += 1; | ||
if next_token == eos_token { | ||
break; | ||
} | ||
if let Some(t) = self.tokenizer.next_token(next_token)? { | ||
print!("{t}"); | ||
std::io::stdout().flush()?; | ||
} | ||
} | ||
let dt = start_gen.elapsed(); | ||
if let Some(rest) = self.tokenizer.decode_rest().map_err(E::msg)? { | ||
print!("{rest}"); | ||
} | ||
std::io::stdout().flush()?; | ||
println!( | ||
"\n{generated_tokens} tokens generated ({:.2} token/s)", | ||
generated_tokens as f64 / dt.as_secs_f64(), | ||
); | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, Copy, PartialEq, Eq, clap::ValueEnum)] | ||
enum Which { | ||
#[value(name = "lite")] | ||
Lite, | ||
#[value(name = "lite-chat")] | ||
LiteChat, | ||
#[value(name = "coder-lite-chat")] | ||
CoderLiteChat, | ||
#[value(name = "v2")] | ||
V2, | ||
#[value(name = "v2-chat")] | ||
V2Chat, | ||
} | ||
|
||
#[derive(Parser, Debug)] | ||
#[command(author, version, about, long_about = None)] | ||
struct Args { | ||
/// Run on CPU rather than on GPU. | ||
#[arg(long)] | ||
cpu: bool, | ||
|
||
/// Enable tracing (generates a trace-timestamp.json file). | ||
#[arg(long)] | ||
tracing: bool, | ||
|
||
#[arg(long)] | ||
use_flash_attn: bool, | ||
|
||
#[arg(long)] | ||
prompt: String, | ||
|
||
/// The temperature used to generate samples. | ||
#[arg(long)] | ||
temperature: Option<f64>, | ||
|
||
/// Nucleus sampling probability cutoff. | ||
#[arg(long)] | ||
top_p: Option<f64>, | ||
|
||
/// Only sample among the top K samples. | ||
#[arg(long)] | ||
top_k: Option<usize>, | ||
|
||
/// The seed to use when generating random samples. | ||
#[arg(long, default_value_t = 299792458)] | ||
seed: u64, | ||
|
||
/// The length of the sample to generate (in tokens). | ||
#[arg(long, short = 'n', default_value_t = 10000)] | ||
sample_len: usize, | ||
|
||
/// The model size to use. | ||
#[arg(long, default_value = "lite")] | ||
which: Which, | ||
|
||
#[arg(long)] | ||
model_id: Option<String>, | ||
|
||
#[arg(long, default_value = "main")] | ||
revision: String, | ||
|
||
/// Penalty to be applied for repeating tokens, 1. means no penalty. | ||
#[arg(long, default_value_t = 1.1)] | ||
repeat_penalty: f32, | ||
|
||
/// The context size to consider for the repeat penalty. | ||
#[arg(long, default_value_t = 64)] | ||
repeat_last_n: usize, | ||
} | ||
|
||
fn main() -> Result<()> { | ||
use tracing_chrome::ChromeLayerBuilder; | ||
use tracing_subscriber::prelude::*; | ||
|
||
let args = Args::parse(); | ||
|
||
let _guard = if args.tracing { | ||
let (chrome_layer, guard) = ChromeLayerBuilder::new().build(); | ||
tracing_subscriber::registry().with(chrome_layer).init(); | ||
Some(guard) | ||
} else { | ||
None | ||
}; | ||
println!( | ||
"avx: {}, neon: {}, simd128: {}, f16c: {}", | ||
candle::utils::with_avx(), | ||
candle::utils::with_neon(), | ||
candle::utils::with_simd128(), | ||
candle::utils::with_f16c() | ||
); | ||
println!( | ||
"temp: {:.2} repeat-penalty: {:.2} repeat-last-n: {}", | ||
args.temperature.unwrap_or(0.), | ||
args.repeat_penalty, | ||
args.repeat_last_n | ||
); | ||
|
||
let start = std::time::Instant::now(); | ||
let api = Api::new()?; | ||
let model_id = match args.model_id { | ||
Some(model_id) => model_id, | ||
None => match args.which { | ||
Which::CoderLiteChat => "deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct".to_string(), | ||
Which::LiteChat => "deepseek-ai/DeepSeek-V2-Lite-Chat".to_string(), | ||
Which::Lite => "deepseek-ai/DeepSeek-V2-Lite".to_string(), | ||
Which::V2 => "deepseek-ai/DeepSeek-V2".to_string(), | ||
Which::V2Chat => "deepseek-ai/DeepSeek-V2-Chat".to_string(), | ||
}, | ||
}; | ||
let repo = api.repo(Repo::with_revision( | ||
model_id, | ||
RepoType::Model, | ||
args.revision, | ||
)); | ||
let tokenizer_filename = repo.get("tokenizer.json")?; | ||
let filenames = candle_examples::hub_load_safetensors(&repo, "model.safetensors.index.json")?; | ||
println!("retrieved the files in {:?}", start.elapsed()); | ||
let tokenizer = Tokenizer::from_file(tokenizer_filename).map_err(E::msg)?; | ||
|
||
let start = std::time::Instant::now(); | ||
let config: DeepSeekV2Config = { | ||
let config_file = repo.get("config.json")?; | ||
serde_json::from_slice(&std::fs::read(config_file)?)? | ||
}; | ||
let device = candle_examples::device(args.cpu)?; | ||
let (model, device) = { | ||
let dtype = if device.is_cuda() { | ||
EricLBuehler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
DType::BF16 | ||
} else { | ||
DType::F16 | ||
}; | ||
let vb = unsafe { VarBuilder::from_mmaped_safetensors(&filenames, dtype, &device)? }; | ||
let model = DeepSeekV2::new(&config, vb)?; | ||
(model, device) | ||
}; | ||
|
||
println!("loaded the model in {:?}", start.elapsed()); | ||
|
||
let mut pipeline = TextGeneration::new( | ||
model, | ||
tokenizer, | ||
args.seed, | ||
args.temperature, | ||
args.top_p, | ||
args.top_k, | ||
args.repeat_penalty, | ||
args.repeat_last_n, | ||
&device, | ||
); | ||
pipeline.run(&args.prompt, args.sample_len)?; | ||
Ok(()) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.