To address the problem of optimizing the request assignment between a fast instance and a slow instance, we need to carefully select which requests to move between the two instances. The goal is to maximize the sum of the minimum slacks in both instances while ensuring that no request times out (i.e., all decode_slack values are positive).
Here's a step-by-step guide and the implementation of the algorithm:
Requests in fast_instance_seqs and slow_instance_seqs must not timeout after reassignment.
You can only move requests from:
Fast to slow (from smallest to larger slack values).
Slow to fast (from largest to smaller slack values).
Algorithm Overview:
We'll use a greedy approach that iteratively moves requests to improve the minimum slack sums.
At each iteration, consider moving the next candidate request from fast to slow or from slow to fast.
Choose the move that results in the greatest increase in the minimum slack sum and does not cause any request to timeout.
Continue this process until no further improvements can be made.
Implementation Steps:
python
# Assume the following variables and methods are already defined:
# - fast_instance_seqs: List of requests on the fast instance, sorted by slack ascending.
# - slow_instance_seqs: List of requests on the slow instance, sorted by slack ascending.
# - self.pd_profiler.get_decode_seq_prediction(seq_groups): Method to predict processing time.
# - self.pd_profiler.get_decode_slack_on_pipeline(seq_group, now, tpot_predict): Method to compute decode_slack.
# We'll define helper functions to compute tpot_predict and decode_slacks.
python
def compute_tpot_predict(seq_groups):
seq_len = 0
for seq_group in seq_groups:
seq_len += (
seq_group.get_seqs()[0].get_prompt_len()
+ seq_group.get_seqs()[0].get_output_len()
)
return 4.5e-7 * seq_len + 0.014
def compute_decode_slacks(seq_groups, tpot_predict, now):
decode_slacks = []
for seq_group in seq_groups:
decode_slack = self.pd_profiler.get_decode_slack_on_pipeline(seq_group, now, tpot_predict)
if decode_slack <= 0:
return None # Indicates a timeout occurs
decode_slacks.append(decode_slack)
return decode_slacks
Implement the Greedy Algorithm:
python
Usage Example:
Suppose you have the fast_instance_seqs and slow_instance_seqs already populated and sorted:
python
# Example usage
optimized_fast_seqs, optimized_slow_seqs = optimize_request_assignment(fast_instance_seqs, slow_instance_seqs)
Notes:
The compute_decode_slacks function returns None if any request results in a non-positive decode_slack, indicating a timeout.
The algorithm continues to loop, considering moving one request at a time, until no further improvements can be made.
The requests are only moved if they result in an increase in the sum of minimum slacks and do not cause any request to timeout.
This approach ensures that we're always moving towards improving our objective while respecting the constraints. The algorithm efficiently reassesses the impact of each potential move and makes decisions that contribute to maximizing the minimum slack sum.
Not yet included in a published release.
from time import time
def optimize_request_assignment(fast_instance_seqs, slow_instance_seqs):
# Initialize current sequences
fast_seqs = fast_instance_seqs.copy()
slow_seqs = slow_instance_seqs.copy()
now = time()
# Compute initial tpot_predict and decode_slacks
tpot_predict_fast = compute_tpot_predict(fast_seqs)
tpot_predict_slow = compute_tpot_predict(slow_seqs)
decode_slacks_fast = compute_decode_slacks(fast_seqs, tpot_predict_fast, now)
decode_slacks_slow = compute_decode_slacks(slow_seqs, tpot_predict_slow, now)
if decode_slacks_fast is None or decode_slacks_slow is None:
raise Exception("Initial sequences contain requests that already timeout.")
min_slack_fast = min(decode_slacks_fast)
min_slack_slow = min(decode_slacks_slow)
max_sum = min_slack_fast + min_slack_slow
improved = True
while improved:
improved = False
options = []
# Option A: Move one request from fast to slow
if fast_seqs:
candidate_seq = fast_seqs[0]
new_fast_seqs = fast_seqs[1:]
new_slow_seqs = slow_seqs + [candidate_seq]
tpot_predict_fast_new = compute_tpot_predict(new_fast_seqs)
tpot_predict_slow_new = compute_tpot_predict(new_slow_seqs)
decode_slacks_fast_new = compute_decode_slacks(new_fast_seqs, tpot_predict_fast_new, now)
decode_slacks_slow_new = compute_decode_slacks(new_slow_seqs, tpot_predict_slow_new, now)
if decode_slacks_fast_new and decode_slacks_slow_new:
min_slack_fast_new = min(decode_slacks_fast_new)
min_slack_slow_new = min(decode_slacks_slow_new)
sum_slack_new = min_slack_fast_new + min_slack_slow_new
options.append(('A', sum_slack_new, new_fast_seqs, new_slow_seqs, tpot_predict_fast_new, tpot_predict_slow_new))
# Option B: Move one request from slow to fast
if slow_seqs:
candidate_seq = slow_seqs[-1]
new_slow_seqs = slow_seqs[:-1]
new_fast_seqs = fast_seqs + [candidate_seq]
tpot_predict_fast_new = compute_tpot_predict(new_fast_seqs)
tpot_predict_slow_new = compute_tpot_predict(new_slow_seqs)
decode_slacks_fast_new = compute_decode_slacks(new_fast_seqs, tpot_predict_fast_new, now)
decode_slacks_slow_new = compute_decode_slacks(new_slow_seqs, tpot_predict_slow_new, now)
if decode_slacks_fast_new and decode_slacks_slow_new:
min_slack_fast_new = min(decode_slacks_fast_new)
min_slack_slow_new = min(decode_slacks_slow_new)
sum_slack_new = min_slack_fast_new + min_slack_slow_new
options.append(('B', sum_slack_new, new_fast_seqs, new_slow_seqs, tpot_predict_fast_new, tpot_predict_slow_new))
if not options:
break # No valid moves left
# Select the best option
best_option = max(options, key=lambda x: x[1])
if best_option[1] > max_sum:
max_sum = best_option[1]
improved = True
_, _, fast_seqs, slow_seqs, tpot_predict_fast, tpot_predict_slow = best_option
decode_slacks_fast = compute_decode_slacks(fast_seqs, tpot_predict_fast, now)
decode_slacks_slow = compute_decode_slacks(slow_seqs, tpot_predict_slow, now)
min_slack_fast = min(decode_slacks_fast)
min_slack_slow = min(decode_slacks_slow)
else:
break # No improvement
# At this point, fast_seqs and slow_seqs are optimized
return fast_seqs, slow_seqs