Skip to content

⚡️ Speed up method Batch.broadcast by 12% #1116

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

Conversation

misrasaurabh1
Copy link
Contributor

📄 12% (0.12x) speedup for Batch.broadcast in inference/core/workflows/execution_engine/entities/base.py

⏱️ Runtime : 32.8 microseconds 29.2 microseconds (best of 315 runs)

📝 Explanation and details

Here's an optimized version of the provided Batch class.

Explanation of the Changes.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 71 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests Details
from copy import copy
from typing import Generic, Iterator, List, Optional, Set, Tuple

# imports
import pytest  # used for our unit tests
from inference.core.workflows.execution_engine.entities.base import Batch

B = Generic
from inference.core.workflows.execution_engine.entities.base import Batch

# unit tests

# Valid Broadcast Scenarios
def test_broadcast_single_element_to_larger_size():
    batch = Batch(content=[1], indices=[(0,)])
    codeflash_output = batch.broadcast(3)

def test_broadcast_batch_to_same_size():
    batch = Batch(content=[1, 2, 3], indices=[(0,), (1,), (2,)])
    codeflash_output = batch.broadcast(3)

# Invalid Broadcast Scenarios
def test_broadcast_to_non_positive_size():
    batch = Batch(content=[1], indices=[(0,)])
    with pytest.raises(ValueError):
        batch.broadcast(0)
    with pytest.raises(ValueError):
        batch.broadcast(-1)

def test_broadcast_batch_to_different_size():
    batch = Batch(content=[1, 2], indices=[(0,), (1,)])
    with pytest.raises(ValueError):
        batch.broadcast(3)

# Edge Cases
def test_broadcast_empty_batch():
    batch = Batch(content=[], indices=[])
    with pytest.raises(ValueError):
        batch.broadcast(1)

def test_broadcast_complex_data_types():
    batch = Batch(content=[{'key': 'value'}], indices=[(0,)])
    codeflash_output = batch.broadcast(3)

# Performance and Scalability
def test_broadcast_large_scale():
    batch = Batch(content=[1], indices=[(0,)])
    codeflash_output = batch.broadcast(1000)

# Mixed Data Types
def test_broadcast_mixed_data_types():
    batch = Batch(content=[(1, 2)], indices=[(0,)])
    codeflash_output = batch.broadcast(3)

# Boundary Conditions
def test_broadcast_min_max_sizes():
    batch = Batch(content=[1], indices=[(0,)])
    codeflash_output = batch.broadcast(1)

    import sys
    codeflash_output = batch.broadcast(sys.maxsize)

# Indices with Different Structures
def test_broadcast_indices_with_varying_lengths():
    batch = Batch(content=[1], indices=[(0,)])
    codeflash_output = batch.broadcast(3)

    batch = Batch(content=[1], indices=[(0, 1)])
    codeflash_output = batch.broadcast(2)

# Consistency Checks
def test_broadcast_consistency():
    batch = Batch(content=[1], indices=[(0,)])
    codeflash_output = batch.broadcast(3)

# Exception Handling
def test_broadcast_exceptions():
    batch = Batch(content=[1], indices=[(0,)])
    with pytest.raises(ValueError):
        batch.broadcast(0)

    batch = Batch(content=[1, 2], indices=[(0,), (1,)])
    with pytest.raises(ValueError):
        batch.broadcast(3)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

from copy import copy
from typing import Generic, Iterator, List, Optional, Set, Tuple, TypeVar

# imports
import pytest  # used for our unit tests
from inference.core.workflows.execution_engine.entities.base import Batch

# Define type variable
B = TypeVar('B')
from inference.core.workflows.execution_engine.entities.base import Batch

# unit tests

# Basic Functionality
def test_broadcast_single_element():
    batch = Batch(content=[1], indices=[(0,)])
    codeflash_output = batch.broadcast(3)

def test_broadcast_same_size():
    batch = Batch(content=[1, 2, 3], indices=[(0,), (1,), (2,)])
    codeflash_output = batch.broadcast(3)

# Edge Cases
def test_broadcast_to_zero():
    batch = Batch(content=[1, 2, 3], indices=[(0,), (1,), (2,)])
    with pytest.raises(ValueError):
        batch.broadcast(0)

def test_broadcast_to_negative():
    batch = Batch(content=[1, 2, 3], indices=[(0,), (1,), (2,)])
    with pytest.raises(ValueError):
        batch.broadcast(-1)

def test_broadcast_empty_batch():
    batch = Batch(content=[], indices=[])
    with pytest.raises(ValueError):
        batch.broadcast(3)

# Invalid Operations
def test_broadcast_multi_element_to_different_size():
    batch = Batch(content=[1, 2], indices=[(0,), (1,)])
    with pytest.raises(ValueError):
        batch.broadcast(3)

# Handling Indices
def test_broadcast_with_indices():
    batch = Batch(content=[1], indices=[(0,)])
    codeflash_output = batch.broadcast(3)

# Large Scale Test Cases
def test_broadcast_large_single_element():
    batch = Batch(content=[1], indices=[(0,)])
    codeflash_output = batch.broadcast(1000)

def test_broadcast_large_multi_element():
    content = list(range(1000))
    indices = [(i,) for i in range(1000)]
    batch = Batch(content=content, indices=indices)
    codeflash_output = batch.broadcast(1000)

# Performance and Scalability
def test_broadcast_large_elements():
    large_string = 'a' * 1000
    batch = Batch(content=[large_string], indices=[(0,)])
    codeflash_output = batch.broadcast(100)

# Mixed Data Types
def test_broadcast_mixed_data_types():
    batch = Batch(content=[1, 'a', 3.14], indices=[(0,), (1,), (2,)])
    codeflash_output = batch.broadcast(3)

# Handling Optional Indices
def test_broadcast_none_indices():
    batch = Batch(content=[1], indices=None)
    codeflash_output = batch.broadcast(3)

# Complex Indices
def test_broadcast_complex_indices():
    batch = Batch(content=[1], indices=[(1, 2)])
    codeflash_output = batch.broadcast(3)

# Custom Data Types
class MyClass:
    def __init__(self, value):
        self.value = value

def test_broadcast_custom_objects():
    obj = MyClass(1)
    batch = Batch(content=[obj], indices=[(0,)])
    codeflash_output = batch.broadcast(3)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

Codeflash

codeflash-ai bot and others added 2 commits February 20, 2025 22:17
Here's an optimized version of the provided Batch class.

### Explanation of the Changes.
@PawelPeczek-Roboflow PawelPeczek-Roboflow merged commit 04e5eeb into roboflow:main Mar 27, 2025
1 check passed
@codeflash-ai codeflash-ai bot deleted the codeflash/optimize-Batch.broadcast-m7dwju7h branch March 28, 2025 23:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants