Skip to content

Commit 3df95d2

Browse files
youkaichaoAlvant
authored andcommitted
[torch.compile] fix tensor alias (vllm-project#8982)
Signed-off-by: Alvant <alvasian@yandex.ru>
1 parent d847fc2 commit 3df95d2

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

vllm/worker/embedding_model_runner.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ def execute_model(
103103
# a placeholder (it has wide hardware support).
104104
kv_caches = [
105105
torch.tensor([], dtype=torch.float32, device=self.device)
106-
] * num_layers
106+
for _ in range(num_layers)
107+
]
107108

108109
execute_model_kwargs = {
109110
"input_ids":

vllm/worker/enc_dec_model_runner.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ def profile_run(self) -> None:
348348
# a placeholder (it has wide hardware support).
349349
kv_caches = [
350350
torch.tensor([], dtype=torch.float32, device=self.device)
351-
] * num_layers
351+
for _ in range(num_layers)
352+
]
352353
finished_requests_ids = [seq.request_id for seq in seqs]
353354
model_input = self.prepare_model_input(
354355
seqs, finished_requests_ids=finished_requests_ids)

vllm/worker/model_runner.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1244,9 +1244,13 @@ def profile_run(self) -> None:
12441244
# it by reference, rather by specializing on the value ``None``.
12451245
# the `dtype` argument does not matter, and we use `float32` as
12461246
# a placeholder (it has wide hardware support).
1247+
# it is important to create tensors inside the loop, rather than
1248+
# multiplying the list, to avoid Dynamo from treating them as
1249+
# tensor aliasing.
12471250
kv_caches = [
12481251
torch.tensor([], dtype=torch.float32, device=self.device)
1249-
] * num_layers
1252+
for _ in range(num_layers)
1253+
]
12501254
finished_requests_ids = [seq.request_id for seq in seqs]
12511255
model_input = self.prepare_model_input(
12521256
seqs, finished_requests_ids=finished_requests_ids)

0 commit comments

Comments
 (0)