-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed adapter segments when batches contain multiple distinct adapters (
#62)
- Loading branch information
Showing
6 changed files
with
143 additions
and
74 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from typing import List, Tuple | ||
|
||
import torch | ||
|
||
|
||
def find_segments(adapter_indices: torch.Tensor) -> Tuple[List[int], List[int]]: | ||
segments = [0] | ||
segment_indices = [] | ||
|
||
# Calling .item() repeatedly on CUDA tensor is very slow, so we move it to CPU first | ||
adapter_indices = adapter_indices.cpu() | ||
|
||
start_index = 0 | ||
for i in range(1, adapter_indices.shape[0]): | ||
if adapter_indices[i] != adapter_indices[i - 1]: | ||
segments.append(i) | ||
segment_indices.append(adapter_indices[i - 1].item()) | ||
start_index = i | ||
|
||
# Handle the last segment | ||
if start_index < len(adapter_indices): | ||
segments.append(len(adapter_indices)) | ||
segment_indices.append(adapter_indices[-1].item()) | ||
|
||
return segments, segment_indices | ||
|
||
|
||
class SegmentConcatBuilder: | ||
def __init__(self): | ||
self.adapter_segment_indices = [] | ||
self.adapter_segment_tensors = [] | ||
|
||
def concat(self, adapter_segments: torch.Tensor, segment_indices: List[int]): | ||
# Update adapter segments | ||
if self.adapter_segment_tensors: | ||
# Because we have already processed at least one batch, remove the 0 start index | ||
# from this batch denoting the beginning of the segment, then offset all segment | ||
# positions by the value of the last segment in the previous batch to account for | ||
# the concatenation. | ||
adapter_segments = adapter_segments[1:] + self.adapter_segment_tensors[-1][-1] | ||
|
||
if self.adapter_segment_indices and self.adapter_segment_indices[-1] == segment_indices[0]: | ||
# If the last segment in the previous batch is the same as the first segment in this batch, | ||
# then we merge them together into a single segment. In effect, this means removing it from | ||
# the segment indices of this batch, and extending the segment span by removing the segment | ||
# end index from the previous batch. | ||
segment_indices = segment_indices[1:] | ||
self.adapter_segment_tensors[-1] = self.adapter_segment_tensors[-1][:-1] | ||
|
||
self.adapter_segment_indices.extend(segment_indices) | ||
self.adapter_segment_tensors.append(adapter_segments) | ||
|
||
def build(self) -> Tuple[torch.Tensor, List[int]]: | ||
return torch.concat(self.adapter_segment_tensors), self.adapter_segment_indices |
This file contains 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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import pytest | ||
import torch | ||
|
||
from lorax_server.utils.segments import find_segments, SegmentConcatBuilder | ||
|
||
|
||
|
||
@pytest.mark.parametrize( | ||
"adapter_indices,expected_segments,expected_segment_indices", | ||
[ | ||
( | ||
torch.tensor([0, 0, 0, 1, 1, 2, 2, 2, 2, 2, 1, 1]), | ||
[0, 3, 5, 10, 12], | ||
[0, 1, 2, 1], | ||
), | ||
(torch.tensor([]), [0], []), | ||
(torch.tensor([0]), [0, 1], [0]), | ||
(torch.tensor([1]), [0, 1], [1]), | ||
], | ||
) | ||
def test_find_segments(adapter_indices, expected_segments, expected_segment_indices): | ||
segments, segment_indices = find_segments(adapter_indices) | ||
assert segments == expected_segments | ||
assert segment_indices == expected_segment_indices | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"batches,expected_segments,expected_segment_indices", | ||
[ | ||
( | ||
[ | ||
(torch.tensor([0, 1, 4, 7, 8]), [2, 1, 2, 1]), | ||
(torch.tensor([0, 2, 5]), [1, 2]), | ||
], | ||
[0, 1, 4, 7, 10, 13], | ||
[2, 1, 2, 1, 2], | ||
), | ||
( | ||
[ | ||
(torch.tensor([0, 1, 4, 7]), [2, 1, 2]), | ||
(torch.tensor([0, 2, 5]), [1, 2]), | ||
], | ||
[0, 1, 4, 7, 9, 12], | ||
[2, 1, 2, 1, 2], | ||
), | ||
], | ||
) | ||
def test_concat_segments(batches, expected_segments, expected_segment_indices): | ||
builder = SegmentConcatBuilder() | ||
for segment, indices in batches: | ||
builder.concat(segment, indices) | ||
|
||
segments, segment_indices = builder.build() | ||
assert segments.tolist() == expected_segments | ||
assert segment_indices == expected_segment_indices |