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

Recompute KV cache for Phi3 when switching from short to long factor #1161

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

Conversation

ajindal1
Copy link
Collaborator

Recompute KV cache for Phi3 when switching from short to long factor.

Verified that this PR fixes the issue for:

  1. Phi3.5 mini
  2. Phi3 mini 128K
  3. Phi3 small
  4. Phi3 medium

// TODO: Extend the solution to make it work for batch size > 1 and num beams > 1
// Phi3 model switches from short factor to long factor at 4097 (original_max_position_embeddings+1) token, needs Recomputation of Position IDs and KV Cache
// at this stage which is achieved by rewinding to zero and appending the current sequence
if (model_->config_->model.type == "phi3" && search_->params_->search.batch_size == 1 && search_->params_->search.num_beams == 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The model types should be phi3, phi3small, phi3v, and phimoe. For phi3v, rewinding is not currently supported.

// Phi3 model switches from short factor to long factor at 4097 (original_max_position_embeddings+1) token, needs Recomputation of Position IDs and KV Cache
// at this stage which is achieved by rewinding to zero and appending the current sequence
if (model_->config_->model.type == "phi3" && search_->params_->search.batch_size == 1 && search_->params_->search.num_beams == 1) {
if (search_->GetSequenceLength() == 4097 && first_switch) {
Copy link
Contributor

Choose a reason for hiding this comment

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

For phi3small, the switch value is at 8K and not 4K.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we record original_max_position_embeddings in the config and avoid hardcoding this value here?

src/generators.cpp Outdated Show resolved Hide resolved
if (model_->config_->model.type == "phi3" && search_->params_->search.batch_size == 1 && search_->params_->search.num_beams == 1) {
if (search_->GetSequenceLength() == 4097 && first_switch) {
first_switch = false;
auto current_seq = cpu_span<int32_t>(GetSequence(0).CpuSpan());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you intend to copy the span over to CPU? You might want to use CopyDeviceToCpu instead of CpuSpan.

Comment on lines +363 to +364
RewindToLength(0);
AppendTokens(current_seq);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you also add a comment highlighting that this solution works only for scenarios that support continuous decoding.

  • Batch size == 1
  • Num beams == 1
  • Model must be a decoder model (not a multimodal model)
  • EP in {CPU, CUDA}. DML won't work.

if (search_->GetSequenceLength() == 4097 && first_switch) {
first_switch = false;
auto current_seq = cpu_span<int32_t>(GetSequence(0).CpuSpan());
RewindToLength(0);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you think a more general purpose solution might be to have a new API that resets the generator?

Reset()

Then we don't need to limit ourselves to only supporting scenarios that support RewindToLength(0) (i.e. continuous decoding).

Maybe we can do it in another PR.

// Phi3 model switches from short factor to long factor at 4097 (original_max_position_embeddings+1) token, needs Recomputation of Position IDs and KV Cache
// at this stage which is achieved by rewinding to zero and appending the current sequence
if (model_->config_->model.type == "phi3" && search_->params_->search.batch_size == 1 && search_->params_->search.num_beams == 1) {
if (search_->GetSequenceLength() == 4097 && first_switch) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we record original_max_position_embeddings in the config and avoid hardcoding this value here?

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.

3 participants