-
Notifications
You must be signed in to change notification settings - Fork 43
adding Context Length Specialization (CCL) #388
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
quic-vjanfaza
wants to merge
2
commits into
quic:main
Choose a base branch
from
quic-vjanfaza:Context-Length-Specialization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -316,6 +316,7 @@ def cloud_ai_100_exec_kv( | |
prompts_txt_file_path: Optional[str] = None, | ||
device_id: Optional[List[int]] = None, | ||
generation_len: Optional[int] = None, | ||
comp_ctx_lengths: Optional[List[int]] = None, | ||
enable_debug_logs: bool = False, | ||
stream: bool = True, | ||
write_io_dir: Optional[str] = None, | ||
|
@@ -368,6 +369,7 @@ def cloud_ai_100_exec_kv( | |
qpc_path=qpc_path, | ||
device_id=device_id, | ||
ctx_len=ctx_len, | ||
comp_ctx_lengths=comp_ctx_lengths, | ||
enable_debug_logs=enable_debug_logs, | ||
write_io_dir=write_io_dir, | ||
full_batch_size=full_batch_size, | ||
|
@@ -407,12 +409,14 @@ def __init__( | |
qpc_path: str, | ||
full_batch_size: Optional[int] = None, | ||
ctx_len: Optional[int] = None, | ||
comp_ctx_lengths: Optional[List[int]] = None, | ||
device_id: Optional[List[int]] = None, | ||
enable_debug_logs: bool = False, | ||
write_io_dir: Optional[str] = None, | ||
is_tlm: Optional[int] = None, | ||
) -> None: | ||
self._ctx_len = ctx_len | ||
self.comp_ctx_lengths = comp_ctx_lengths | ||
self._write_io_dir = write_io_dir | ||
self.is_tlm = is_tlm | ||
|
||
|
@@ -724,6 +728,12 @@ def run_prefill(self, prompt, generation_len, prefill_logit_bs=1, decode_batch_i | |
batch_lora_ids = [self._prompt_to_lora_id_mapping_prefill.popleft() for i in range(self.batch_size)] | ||
inputs["lora_ids"] = np.array(batch_lora_ids, dtype=np.int64).reshape(self.batch_size, 1) | ||
|
||
inputs["comp_ctx_lengths"] = np.random.rand( | ||
self.comp_ctx_lengths[0] if self.comp_ctx_lengths is not None else self._ctx_len | ||
) | ||
buffers = {"comp_ctx_len_out": np.zeros(1)} | ||
self._session.set_buffers(buffers) | ||
|
||
for i in range(num_chunks): | ||
chunk_inputs = inputs.copy() | ||
chunk_inputs["input_ids"] = inputs["input_ids"][ | ||
|
@@ -741,6 +751,19 @@ def run_prefill(self, prompt, generation_len, prefill_logit_bs=1, decode_batch_i | |
generation_len, | ||
) | ||
|
||
def initialize_ccl(self, decode_inputs): | ||
max_ccl_id = len(self.comp_ctx_lengths) - 1 | ||
max_position_id = np.max(decode_inputs["position_ids"]) | ||
ccl_id = 1 | ||
for i in range(1, len(self.comp_ctx_lengths)): | ||
if max_position_id < self.comp_ctx_lengths[i]: | ||
ccl_id = i | ||
break | ||
buffers = {"comp_ctx_len_out": np.zeros(1)} | ||
print(f"CCL: {self.comp_ctx_lengths[ccl_id]}") | ||
|
||
return buffers, ccl_id, max_ccl_id | ||
|
||
def run_continuous_batching_decode(self, prompt_queue, generation_len): | ||
""" | ||
Runs continuous batching decode for the given prompt queue and generation length. | ||
|
@@ -771,6 +794,16 @@ def run_continuous_batching_decode(self, prompt_queue, generation_len): | |
# Prepare decode inputs inputs. | ||
decode_inputs = self.prepare_decode_inputs() | ||
|
||
if self.comp_ctx_lengths is not None: | ||
list_of_comp_ctx_lengths = [np.zeros(length) for length in self.comp_ctx_lengths] | ||
buffers, ccl_id, max_ccl_id = self.initialize_ccl(decode_inputs) | ||
decode_inputs["comp_ctx_lengths"] = list_of_comp_ctx_lengths[ccl_id] | ||
self._session.set_buffers(buffers) | ||
else: | ||
decode_inputs["comp_ctx_lengths"] = np.zeros(self._ctx_len) | ||
buffers = {"comp_ctx_len_out": np.zeros(1)} | ||
self._session.set_buffers(buffers) | ||
|
||
while prompt_queue or current_decode_ongoing.any(): | ||
outputs = self._session.run(decode_inputs) | ||
|
||
|
@@ -808,6 +841,19 @@ def run_continuous_batching_decode(self, prompt_queue, generation_len): | |
batch_id_map[decode_batch_id] | ||
] | ||
|
||
if self.comp_ctx_lengths is not None: | ||
###Recalculate ccl_id based on position ids### | ||
# Determine the maximum value of position_ids across all batch elements | ||
max_position_id = np.max(decode_inputs["position_ids"]) | ||
|
||
# Update ccl_id and comp_ctx_lengths based on the maximum position id | ||
ccl_id = 1 | ||
for i in range(1, len(self.comp_ctx_lengths)): | ||
if max_position_id < self.comp_ctx_lengths[i]: | ||
ccl_id = i | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
break | ||
decode_inputs["comp_ctx_lengths"] = list_of_comp_ctx_lengths[ccl_id] | ||
|
||
else: | ||
current_decode_ongoing[decode_batch_id] = False | ||
else: | ||
|
@@ -818,6 +864,12 @@ def run_continuous_batching_decode(self, prompt_queue, generation_len): | |
next_token_id[decode_batch_id, -1] | ||
) | ||
|
||
if self.comp_ctx_lengths is not None: | ||
# Update ccl_id and comp_ctx_lengths based on the maximum position id | ||
if decode_inputs["position_ids"][decode_batch_id, -1] >= self.comp_ctx_lengths[ccl_id] - 1: | ||
ccl_id = min(ccl_id + 1, max_ccl_id) | ||
decode_inputs["comp_ctx_lengths"] = list_of_comp_ctx_lengths[ccl_id] | ||
|
||
generated_id_current_index[decode_batch_id] += 1 | ||
|
||
return decode_pause_time | ||
|
@@ -842,7 +894,25 @@ def run_decode(self, decode_inputs, generation_len, streamer: Optional[transform | |
self._session.set_buffers({"logits": logits_out_placeholder}) | ||
finished_sequences = decode_inputs["input_ids"] == self.tokenizer.eos_token_id | ||
num_token = 0 | ||
|
||
if self.comp_ctx_lengths is not None: | ||
list_of_comp_ctx_lengths = [np.zeros(length) for length in self.comp_ctx_lengths] | ||
buffers, ccl_id, max_ccl_id = self.initialize_ccl(decode_inputs) | ||
decode_inputs["comp_ctx_lengths"] = list_of_comp_ctx_lengths[ccl_id] | ||
self._session.set_buffers(buffers) | ||
else: | ||
decode_inputs["comp_ctx_lengths"] = np.zeros(self._ctx_len) | ||
buffers = {"comp_ctx_len_out": np.zeros(1)} | ||
self._session.set_buffers(buffers) | ||
|
||
cache_index = np.max(decode_inputs["position_ids"]) | ||
for num_token in range(1, generation_len): | ||
if self.comp_ctx_lengths is not None: | ||
if cache_index >= self.comp_ctx_lengths[ccl_id] - 1: | ||
# if cache_index >= self.comp_ctx_lengths[ccl_id] - 1: | ||
ccl_id = min(ccl_id + 1, max_ccl_id) | ||
decode_inputs["comp_ctx_lengths"] = list_of_comp_ctx_lengths[ccl_id] | ||
|
||
if streamer: | ||
streamer.put(decode_inputs["input_ids"][0]) | ||
outputs = self._session.run(decode_inputs) | ||
|
@@ -854,6 +924,7 @@ def run_decode(self, decode_inputs, generation_len, streamer: Optional[transform | |
# Prepare inputs for next iteration | ||
decode_inputs["input_ids"] = outputs["logits"].argmax(2) | ||
decode_inputs["position_ids"][:, -1] += 1 | ||
cache_index += 1 | ||
self.generated_ids[:, num_token] = decode_inputs["input_ids"][:, -1] | ||
finished_sequences |= decode_inputs["input_ids"] == self.tokenizer.eos_token_id | ||
|
||
|
@@ -901,17 +972,27 @@ def __init__( | |
qpc_path: str, | ||
full_batch_size: Optional[int] = None, | ||
ctx_len: Optional[int] = None, | ||
comp_ctx_lengths: Optional[List[int]] = None, | ||
device_id: Optional[List[int]] = None, | ||
enable_debug_logs: bool = False, | ||
write_io_dir: Optional[str] = None, | ||
is_tlm: bool = False, | ||
) -> None: | ||
self._qaic_model = QEffTextGenerationBase( | ||
tokenizer, qpc_path, full_batch_size, ctx_len, device_id, enable_debug_logs, write_io_dir, is_tlm | ||
tokenizer, | ||
qpc_path, | ||
full_batch_size, | ||
ctx_len, | ||
comp_ctx_lengths, | ||
device_id, | ||
enable_debug_logs, | ||
write_io_dir, | ||
is_tlm, | ||
) | ||
self._full_batch_size = self._qaic_model.full_batch_size | ||
self._tokenizer = self._qaic_model.tokenizer | ||
self._ctx_len = ctx_len | ||
self.comp_ctx_lengths = comp_ctx_lengths | ||
self._perf_metrics = None | ||
self._prompt_queue = None | ||
self._text_streamer = None | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we have a reverse list and pop out the last value if max_position_id < self.comp_ctx_lengths[-1]? this way we can avoid the loop
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why should we check with the last element? Each request can be finished in different position_id and we need to check to find the most suitable CCL window to get the best performance. This for loop only happens at the end of a request and it's an order of length(CCL) that can't be more than a few values because of compiler limitation in the number of specializations.