Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
text-generation-inference
Commits
fea62e92
Unverified
Commit
fea62e92
authored
Nov 18, 2024
by
drbh
Committed by
GitHub
Nov 18, 2024
Browse files
fix: improve find_segments via numpy diff (#2686)
parent
52e48739
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
16 deletions
+11
-16
server/text_generation_server/utils/segments.py
server/text_generation_server/utils/segments.py
+11
-16
No files found.
server/text_generation_server/utils/segments.py
View file @
fea62e92
...
@@ -5,30 +5,25 @@
...
@@ -5,30 +5,25 @@
from
typing
import
List
,
Tuple
,
Union
from
typing
import
List
,
Tuple
,
Union
import
torch
import
torch
import
numpy
as
np
# FIXME: this should be optimized
def
find_segments
(
def
find_segments
(
adapter_indices
:
Union
[
torch
.
Tensor
,
List
[
int
]]
adapter_indices
:
Union
[
torch
.
Tensor
,
List
[
int
]]
)
->
Tuple
[
List
[
int
],
List
[
int
]]:
)
->
Tuple
[
List
[
int
],
List
[
int
]]:
segments
=
[
0
]
segment_indices
=
[]
if
isinstance
(
adapter_indices
,
torch
.
Tensor
):
if
isinstance
(
adapter_indices
,
torch
.
Tensor
):
# Calling .item() repeatedly on CUDA tensor is very slow, so we move it to CPU first
adapter_indices
=
adapter_indices
.
cpu
().
numpy
()
adapter_indices
=
adapter_indices
.
cpu
().
tolist
()
elif
isinstance
(
adapter_indices
,
list
):
adapter_indices
=
np
.
array
(
adapter_indices
)
start_index
=
0
change_mask
=
np
.
diff
(
adapter_indices
,
prepend
=
adapter_indices
[
0
]
-
1
)
for
i
in
range
(
1
,
len
(
adapter_indices
)):
change_indices
=
np
.
nonzero
(
change_mask
)[
0
]
if
adapter_indices
[
i
]
!=
adapter_indices
[
i
-
1
]:
segments
.
append
(
i
)
segments
=
[
0
]
segment
_indices
.
append
(
adapter_indices
[
i
-
1
]
)
segment
s
.
extend
(
change_indices
[
1
:].
tolist
()
)
start_index
=
i
segments
.
append
(
len
(
adapter_indices
))
# Handle the last segment
segment_indices
=
adapter_indices
[
change_indices
].
tolist
()
if
start_index
<
len
(
adapter_indices
):
segments
.
append
(
len
(
adapter_indices
))
segment_indices
.
append
(
adapter_indices
[
-
1
])
return
segments
,
segment_indices
return
segments
,
segment_indices
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment