Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Oct 30, 2025

📄 11% (0.11x) speedup for PetalsConfig.get_config in litellm/llms/petals/completion/transformation.py

⏱️ Runtime : 59.5 microseconds 53.4 microseconds (best of 74 runs)

📝 Explanation and details

The optimization applies two key performance improvements to the BaseConfig.get_config() method:

1. Pre-computed function types tuple: The original code recreates a tuple of function types (types.FunctionType, types.BuiltinFunctionType, classmethod, staticmethod, property) on every method call. The optimized version moves this to module level as _function_types, eliminating repeated tuple construction.

2. Enhanced filtering strategy: Instead of just using isinstance(v, _function_types), the optimized version adds not callable(v) as an additional filter. This provides a faster path for excluding callable objects before the more expensive isinstance check, since callable() is a lightweight builtin function.

3. Direct dictionary access: Replaced cls.__dict__.items() with vars(cls).items(). While vars(cls) returns the same __dict__ object, it's slightly more direct and avoids attribute lookup overhead.

The line profiler shows the optimization reduces the total time in BaseConfig.get_config() from 307.88ns to 254.52ns (17% faster), with the dictionary comprehension itself becoming more efficient.

These optimizations are particularly effective for the test cases shown because:

  • Repeated calls: Tests calling get_config() multiple times benefit from the pre-computed tuple
  • Class introspection: The filtering improvements help when classes have many attributes to examine
  • Configuration scenarios: All test cases show 8-15% speedup, indicating consistent benefit across different PetalsConfig states

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 1010 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
import types
from abc import ABC
from typing import Any, Optional

# imports
import pytest  # used for our unit tests
from litellm.llms.petals.completion.transformation import PetalsConfig

# --- Unit Tests ---

# ----------- BASIC TEST CASES -----------


def test_basic_petals_config_default():
    # PetalsConfig should return only those class variables that are not None
    codeflash_output = PetalsConfig.get_config(); result = codeflash_output # 9.26μs -> 8.06μs (14.8% faster)

def test_basic_petals_config_after_init():
    # Setting a value via __init__ should update the class variable and show in get_config
    PetalsConfig(max_length=42, temperature=0.7)
    codeflash_output = PetalsConfig.get_config(); result = codeflash_output # 7.36μs -> 6.77μs (8.68% faster)

# ----------- EDGE TEST CASES -----------











def test_large_petals_config_many_updates():
    # Test PetalsConfig with many updates via __init__
    for i in range(1000):
        PetalsConfig(max_length=i)
    codeflash_output = PetalsConfig.get_config(); result = codeflash_output # 9.23μs -> 8.16μs (13.1% faster)

# ----------- DETERMINISM TEST CASES -----------



#------------------------------------------------
import types
from abc import ABC
from typing import TYPE_CHECKING, Any, Optional

# imports
import pytest
from litellm.llms.petals.completion.transformation import PetalsConfig


# A dummy litellm module for testing
class DummyLitellm:
    max_tokens = 42

litellm = DummyLitellm()
from litellm.llms.petals.completion.transformation import PetalsConfig

# ------------------------
# UNIT TESTS FOR get_config
# ------------------------

# ----------- BASIC TEST CASES ------------



def test_petalsconfig_default_values():
    # Should return only non-None class attributes (max_new_tokens)
    # All except max_new_tokens are None by default
    expected = {"max_new_tokens": litellm.max_tokens}
    codeflash_output = PetalsConfig.get_config() # 9.39μs -> 8.61μs (9.10% faster)

def test_petalsconfig_set_some_values():
    # Set some config values via __init__, should update class attributes
    PetalsConfig(max_length=128, temperature=0.7)
    codeflash_output = PetalsConfig.get_config(); conf = codeflash_output # 7.33μs -> 6.63μs (10.5% faster)
    # None attributes should not be present
    for k in ["do_sample", "top_k", "top_p", "repetition_penalty"]:
        pass

def test_petalsconfig_set_all_values():
    # Set all config values via __init__, should update all class attributes
    PetalsConfig(
        max_length=256,
        max_new_tokens=512,
        do_sample=True,
        temperature=1.5,
        top_k=10,
        top_p=0.9,
        repetition_penalty=1.2,
    )
    codeflash_output = PetalsConfig.get_config(); conf = codeflash_output # 7.74μs -> 7.05μs (9.86% faster)
    expected = {
        "max_length": 256,
        "max_new_tokens": 512,
        "do_sample": True,
        "temperature": 1.5,
        "top_k": 10,
        "top_p": 0.9,
        "repetition_penalty": 1.2,
    }

# ----------- EDGE TEST CASES ------------














def test_petalsconfig_cleanup():
    # Restore PetalsConfig class attributes for other tests
    PetalsConfig.max_length = None
    PetalsConfig.max_new_tokens = litellm.max_tokens
    PetalsConfig.do_sample = None
    PetalsConfig.temperature = None
    PetalsConfig.top_k = None
    PetalsConfig.top_p = None
    PetalsConfig.repetition_penalty = None
    expected = {"max_new_tokens": litellm.max_tokens}
    codeflash_output = PetalsConfig.get_config() # 9.20μs -> 8.10μs (13.6% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
from litellm.llms.petals.completion.transformation import PetalsConfig
import pytest

def test_PetalsConfig_get_config():
    with pytest.raises(TypeError, match='super\\(type,\\ obj\\):\\ obj\\ must\\ be\\ an\\ instance\\ or\\ subtype\\ of\\ type'):
        PetalsConfig.get_config(PetalsConfig)

To edit these changes git checkout codeflash/optimize-PetalsConfig.get_config-mhdt495x and push.

Codeflash Static Badge

The optimization applies two key performance improvements to the `BaseConfig.get_config()` method:

**1. Pre-computed function types tuple**: The original code recreates a tuple of function types `(types.FunctionType, types.BuiltinFunctionType, classmethod, staticmethod, property)` on every method call. The optimized version moves this to module level as `_function_types`, eliminating repeated tuple construction.

**2. Enhanced filtering strategy**: Instead of just using `isinstance(v, _function_types)`, the optimized version adds `not callable(v)` as an additional filter. This provides a faster path for excluding callable objects before the more expensive `isinstance` check, since `callable()` is a lightweight builtin function.

**3. Direct dictionary access**: Replaced `cls.__dict__.items()` with `vars(cls).items()`. While `vars(cls)` returns the same `__dict__` object, it's slightly more direct and avoids attribute lookup overhead.

The line profiler shows the optimization reduces the total time in `BaseConfig.get_config()` from 307.88ns to 254.52ns (17% faster), with the dictionary comprehension itself becoming more efficient. 

These optimizations are particularly effective for the test cases shown because:
- **Repeated calls**: Tests calling `get_config()` multiple times benefit from the pre-computed tuple
- **Class introspection**: The filtering improvements help when classes have many attributes to examine
- **Configuration scenarios**: All test cases show 8-15% speedup, indicating consistent benefit across different PetalsConfig states
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 30, 2025 19:18
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash labels Oct 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant