Skip to content

Commit

Permalink
[shortfin][python] Skip tokenization in sd.server if request holds `i…
Browse files Browse the repository at this point in the history
…nput_ids` (#842)

`sd.server` currently performs tokenization irrespective of whether the
request has `input_ids` attached to it.

This patch adds a check to skip tokenization if the request has
tokenized inputs attached to it, and adds `input_ids` to `gen_inputs` so
that such requests can be created from
`InferenceExecRequests.from_batch()`.
  • Loading branch information
vinayakdsci authored Jan 17, 2025
1 parent 0c6d061 commit 1f50538
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions shortfin/python/shortfin_apps/sd/components/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def from_batch(gen_req: GenerateReqInput, index: int) -> "InferenceExecRequest":
"steps",
"guidance_scale",
"seed",
"input_ids",
]
rec_inputs = {}
for item in gen_inputs:
Expand Down
15 changes: 9 additions & 6 deletions shortfin/python/shortfin_apps/sd/components/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,15 @@ async def _prepare(self, device, requests):
# Tokenize prompts and negative prompts. We tokenize in bs1 for now and join later.
input_ids_list = []
neg_ids_list = []
for tokenizer in self.service.tokenizers:
input_ids = tokenizer.encode(request.prompt)
input_ids_list.append(input_ids)
neg_ids = tokenizer.encode(request.neg_prompt)
neg_ids_list.append(neg_ids)
ids_list = [*input_ids_list, *neg_ids_list]
ids_list = request.input_ids
# Tokenize the prompts if the request does not hold input_ids.
if ids_list is None:
for tokenizer in self.service.tokenizers:
input_ids = tokenizer.encode(request.prompt)
input_ids_list.append(input_ids)
neg_ids = tokenizer.encode(request.neg_prompt)
neg_ids_list.append(neg_ids)
ids_list = [*input_ids_list, *neg_ids_list]

request.input_ids = ids_list

Expand Down

0 comments on commit 1f50538

Please sign in to comment.