Skip to content
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

Implement DeepSeek V2 #2744

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

EricLBuehler
Copy link
Member

This PR implements the DeepSeek V2 architecture.

@EricLBuehler EricLBuehler marked this pull request as ready for review January 27, 2025 17:42
candle-examples/Cargo.toml Outdated Show resolved Hide resolved
candle-examples/examples/deepseekv2/main.rs Outdated Show resolved Hide resolved
candle-transformers/src/models/deepseek2.rs Outdated Show resolved Hide resolved
candle-transformers/src/models/deepseek2.rs Outdated Show resolved Hide resolved
candle-transformers/src/models/deepseek2.rs Outdated Show resolved Hide resolved
candle-transformers/src/models/deepseek2.rs Outdated Show resolved Hide resolved
}

pub trait SplitOp {
fn split<D: Dim>(&self, splits: &[usize], dim: D) -> Result<Vec<Tensor>>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like split is only used to split two tensors at a time, having a specialized op split2 instead that returns a pair of tensors would seem more convenient.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, my idea was to maybe eventually migrate all the ...Op traits in this file to candle_nn as they are both used in DeepSeek 3 (that PR duplicates these ops) and might be very useful.

What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather keep candle_nn on the simpler side, here it seems that the ops can be just specialized to what is exactly required by the model, I would even suggest defining helper functions rather than going through traits, and the deepseek-v3 implementation can use some functions from the deepseek-v2 module.

candle-transformers/src/models/deepseek2.rs Outdated Show resolved Hide resolved
candle-transformers/src/models/deepseek2.rs Outdated Show resolved Hide resolved
}

pub trait SplitOp {
fn split<D: Dim>(&self, splits: &[usize], dim: D) -> Result<Vec<Tensor>>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather keep candle_nn on the simpler side, here it seems that the ops can be just specialized to what is exactly required by the model, I would even suggest defining helper functions rather than going through traits, and the deepseek-v3 implementation can use some functions from the deepseek-v2 module.

// (n, topk_group)
let group_idx = scores.topk_unsorted(self.cfg.topk_group)?.indices;
// (n, n_group)
let mut group_mask = group_scores.zeros_like()?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot you just avoid this mut by chaining calls or using a local scope, seems fairly easy to do. Please also review the other remaining muts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants