Skip to content

Commit 04e5eeb

Browse files
Merge pull request #1116 from misrasaurabh1/codeflash/optimize-Batch.broadcast-m7dwju7h
⚡️ Speed up method `Batch.broadcast` by 12%
2 parents 4df7a32 + 040672d commit 04e5eeb

File tree

1 file changed

+10
-7
lines changed
  • inference/core/workflows/execution_engine/entities

1 file changed

+10
-7
lines changed

inference/core/workflows/execution_engine/entities/base.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,18 @@ def iter_with_indices(self) -> Iterator[Tuple[Tuple[int, ...], B]]:
170170
def broadcast(self, n: int) -> "Batch":
171171
if n <= 0:
172172
raise ValueError(
173-
f"Broadcast to size {n} requested which is invalid operation."
173+
f"Broadcast to size {n} requested which is an invalid operation."
174174
)
175-
if len(self._content) == n:
175+
176+
num_content = len(self._content)
177+
178+
if num_content == n:
176179
return self
177-
if len(self._content) == 1:
178-
return Batch(content=[self._content[0]] * n, indices=[self._indices[0]] * n)
179-
raise ValueError(
180-
f"Could not broadcast batch of size {len(self._content)} to size {n}"
181-
)
180+
181+
if num_content == 1:
182+
return Batch(content=self._content * n, indices=self._indices * n)
183+
184+
raise ValueError(f"Could not broadcast batch of size {num_content} to size {n}")
182185

183186

184187
class VideoMetadata(BaseModel):

0 commit comments

Comments
 (0)