Skip to content

[V1] TPU - Remove self.kv_caches #14309

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

[V1] TPU - Remove self.kv_caches #14309

wants to merge 1 commit into from

Conversation

alexm-redhat
Copy link
Collaborator

@alexm-redhat alexm-redhat commented Mar 5, 2025

This PR removes self.kv_caches from the tpu_model_runner.py in V1, so that @heheda12345 #14098 can cleanly land.

@mgoin @NickLucche feel free to make a pass.

Copy link

github-actions bot commented Mar 5, 2025

👋 Hi! Thank you for contributing to the vLLM project.

💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels.

Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of those by going to your fastcheck build on Buildkite UI (linked in the PR checks section) and unblock them. If you do not have permission to unblock, ping simon-mo or khluu to add you in our Buildkite org.

Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.

To run CI, PR reviewers can either: Add ready label to the PR or enable auto-merge.

🚀

@alexm-redhat alexm-redhat requested a review from mgoin March 5, 2025 20:44
@mergify mergify bot added documentation Improvements or additions to documentation v1 labels Mar 5, 2025
Copy link
Member

Choose a reason for hiding this comment

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

Please revert this - we should make a examples/offline_inference/tpu/ folder to keep this

Copy link
Collaborator

@heheda12345 heheda12345 left a comment

Choose a reason for hiding this comment

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

If we add is_profile_run to forward_context, we need to change other backends to pass the is_profile_run arg. Can we achieve it by passing the attributes to ModelWrapperV1 like the pseudo code?

.static_forward_context
for layer_name, kv_cache in kv_caches.items():
# NOTE: Use list because of v0 PP virtual engine.
forward_context[layer_name].kv_cache = [kv_cache]


class ModelWrapperV1(nn.Module):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it possible to implement ModelWrapperV1 like this?

class ModelWrapperV1(nn.Module):

     def __init__(self, model: nn.Module, num_kv_heads, num_blocks, block_size):
         super().__init__()
         self.model = model
         self.num_kv_heads = num_kv_heads
         ...

     def forward(
         self,
         input_ids: torch.Tensor,
         positions: torch.Tensor,
         inputs_embeds: Optional[torch.Tensor] = None,
         is_profile_run: bool,
     ) -> torch.Tensor:
         if not is_profile_run:
              num_kv_heads = self.num_kv_heads
              ...

class TPUModelRunner:
    def _dummy_run(
           self,
           num_tokens: int,
           is_profile_run: bool,
     ) -> None:
         self.model.forward(..., is_profile_run=is_profile_run)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@heheda12345 this is not possible because num_blocks is not known until determine_num_available_blocks is done and initialize_kv_cache is executed.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we pass a fake value first and update it after determine_num_available_blocks?

@alexm-redhat
Copy link
Collaborator Author

@heheda12345 the is_profile_run arg is set to False by default, so it should not be necessary to pass this parameter explicitly to set_forward_context(..) function in other backends. Is there any specific code example where you would need to specify it explicitly?

@heheda12345
Copy link
Collaborator

If we add is_profile_run argument to ForwardContext, model vendors will start to use this arguments to do arbitrary things. We should make sure it has the correct value for all backends, instead of with value False for backends except TPU. I agree that we should have an argument like this, but we should support it in all backends when we add this argument.

@alexm-redhat
Copy link
Collaborator Author

@heheda12345 I see makes sense. There is a new PR from Google that removes the problematic non-profile reshuffling #14310 which will affect this PR. I would wait for this PR to land first and then rebase.

Copy link
Contributor

@NickLucche NickLucche left a comment

Choose a reason for hiding this comment

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

Looks good to me thanks! Only had minor comments on my side. Let's wait for the rebase to deal with is_profile_run.

self._dummy_run(num_tokens=num_tokens, is_profile_run=True)

# This is used after KV cache init
def dummy_run(
Copy link
Contributor

Choose a reason for hiding this comment

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

_dummy_run and dummy_run look confusing, do we really need this overload?



class ModelWrapperV1(nn.Module):

def __init__(self, model: nn.Module):
super().__init__()
self.model = model
self.kv_cache_shape = None

def set_kv_cache_shape(self, kv_cache_shape):
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: we can probably get away without setters as long as we keep the class and the logic lean

if kv_cache_shape_prev is None:
kv_cache_shape_prev = kv_cache_shape
else:
assert kv_cache_shape == kv_cache_shape_prev
Copy link
Contributor

Choose a reason for hiding this comment

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

qq: is this for ruling out some model architecture?

Comment on lines +811 to +815
forward_context = self.vllm_config.compilation_config \
.static_forward_context
for layer_name, kv_cache in kv_caches.items():
# NOTE: Use list because of v0 PP virtual engine.
forward_context[layer_name].kv_cache = [kv_cache]
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: do you see any use in having this bit factored as a util, similarly to bind_kv_cache? We could re-use at least in tpu_worker

Copy link

mergify bot commented Mar 27, 2025

This pull request has merge conflicts that must be resolved before it can be
merged. Please rebase the PR, @alexm-redhat.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify bot added the needs-rebase label Mar 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation needs-rebase tpu Related to Google TPUs v1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants