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

[NPU] Support streaming in Python (cpp backend) #12488

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions python/llm/src/ipex_llm/transformers/npu_models/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,14 @@ def generate(
new_generate_kwargs[var] = value

if isinstance(inputs[0], torch.Tensor):
if streamer is not None:
# input ids
streamer.put(inputs[0])
input_list = inputs[0].flatten().tolist()
else:
if streamer is not None:
# input ids
streamer.put(torch.Tensor(inputs[0]))
input_list = inputs[0]
input_length = len(input_list)

Expand All @@ -335,6 +341,9 @@ def generate(
from .npu_llm_cpp import run_decode, run_prefill, reset

token = run_prefill(self.model_ptr, input_list, self.vocab_size)
if streamer is not None:
# 1st tokens
streamer.put(torch.tensor([token]))
idx = 1
time_t2 = time.perf_counter()
output_tokens.append(torch.tensor([token]))
Expand All @@ -344,10 +353,16 @@ def generate(
token = run_decode(self.model_ptr, token, self.vocab_size)
idx += 1
output_tokens.append(torch.tensor([token]))
if streamer is not None:
# rest tokens
streamer.put(torch.tensor([token]))
output = torch.stack(output_tokens, dim=1)
output = torch.cat((inputs, output), dim=1)
time_t3 = time.perf_counter()

if streamer is not None:
streamer.end()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I put streamer.end() out of time record to fit with our BenchmarkWrapper


reset(self.model_ptr)
self.first_cost = time_t2 - time_t1 # seconds
self.rest_cost_mean = (time_t3 - time_t2) / (idx - 1) # seconds
Expand Down
Loading