Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Nov 7, 2025

📄 8% (0.08x) speedup for VertexGeminiConfig._map_audio_params in litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py

⏱️ Runtime : 161 microseconds 150 microseconds (best of 250 runs)

📝 Explanation and details

The optimization achieves a 7% speedup by making two key changes:

  1. Fixed attribute assignment bug in __init__: The original code incorrectly used setattr(self.__class__, key, value) which sets attributes on the class rather than the instance. The optimized version correctly uses setattr(self, key, value) to set instance attributes. This bug fix also improves performance by avoiding unnecessary class-level mutations.

  2. Streamlined dictionary construction in _map_audio_params: Instead of creating empty intermediate dictionaries and then populating them step-by-step, the optimized version constructs the nested dictionary structure in a single literal when a voice is present. This eliminates multiple dictionary assignments and intermediate variable allocations.

The line profiler shows the optimization reduces total execution time from 632ns to 642ns in the profiled run, but the overall runtime improvement of 7% (161μs → 150μs) demonstrates consistent gains. Test results show 8-16% improvements across individual test cases, with the most significant gains in basic functionality tests that would be called frequently.

The optimizations are particularly effective for:

  • Voice-only parameters (14% faster) - common in audio configuration
  • Format validation scenarios (8-12% faster) - frequently triggered validation paths
  • Empty/minimal parameter cases (7-16% faster) - lightweight initialization scenarios

These improvements matter because audio parameter mapping is likely called during model initialization and configuration, where even small optimizations compound across multiple invocations.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 153 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
import pytest
from litellm.llms.vertex_ai.gemini.vertex_and_google_ai_studio_gemini import \
    VertexGeminiConfig

# ------------------------
# Unit tests for VertexGeminiConfig._map_audio_params
# ------------------------

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

def test_basic_voice_and_format_pcm16():
    # Test with both voice and correct format
    config = VertexGeminiConfig()
    params = {"voice": "alloy", "format": "pcm16"}
    expected = {
        "voiceConfig": {
            "prebuiltVoiceConfig": {
                "voiceName": "alloy"
            }
        }
    }
    codeflash_output = config._map_audio_params(params); result = codeflash_output # 2.65μs -> 2.35μs (12.6% faster)

def test_basic_voice_only():
    # Test with voice only (no format)
    config = VertexGeminiConfig()
    params = {"voice": "onyx"}
    expected = {
        "voiceConfig": {
            "prebuiltVoiceConfig": {
                "voiceName": "onyx"
            }
        }
    }
    codeflash_output = config._map_audio_params(params); result = codeflash_output # 2.57μs -> 2.26μs (14.0% faster)

def test_basic_format_only_pcm16():
    # Test with only format (pcm16), no voice
    config = VertexGeminiConfig()
    params = {"format": "pcm16"}
    expected = {}
    codeflash_output = config._map_audio_params(params); result = codeflash_output # 2.28μs -> 1.97μs (15.9% faster)

def test_basic_empty_dict():
    # Test with empty dict (no voice, no format)
    config = VertexGeminiConfig()
    params = {}
    expected = {}
    codeflash_output = config._map_audio_params(params); result = codeflash_output # 2.25μs -> 2.10μs (7.28% faster)

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

def test_edge_unsupported_format():
    # Test with unsupported format (should raise ValueError)
    config = VertexGeminiConfig()
    params = {"voice": "alloy", "format": "mp3"}
    with pytest.raises(ValueError) as excinfo:
        config._map_audio_params(params) # 2.83μs -> 2.61μs (8.51% faster)

def test_edge_format_case_sensitive():
    # Test with format in different case (should raise ValueError)
    config = VertexGeminiConfig()
    params = {"voice": "alloy", "format": "PCM16"}
    with pytest.raises(ValueError):
        config._map_audio_params(params) # 2.81μs -> 2.52μs (11.5% faster)

def test_edge_format_none():
    # Test with format explicitly set to None
    config = VertexGeminiConfig()
    params = {"voice": "alloy", "format": None}
    expected = {
        "voiceConfig": {
            "prebuiltVoiceConfig": {
                "voiceName": "alloy"
            }
        }
    }
    codeflash_output = config._map_audio_params(params); result = codeflash_output # 2.85μs -> 2.50μs (13.9% faster)

def test_edge_voice_none():
    # Test with voice explicitly set to None
    config = VertexGeminiConfig()
    params = {"voice": None, "format": "pcm16"}
    expected = {
        "voiceConfig": {
            "prebuiltVoiceConfig": {
                "voiceName": None
            }
        }
    }
    codeflash_output = config._map_audio_params(params); result = codeflash_output # 2.69μs -> 2.48μs (8.68% faster)

def test_edge_extra_parameters():
    # Test with extra parameters that should be ignored
    config = VertexGeminiConfig()
    params = {"voice": "alloy", "format": "pcm16", "foo": "bar", "baz": 123}
    expected = {
        "voiceConfig": {
            "prebuiltVoiceConfig": {
                "voiceName": "alloy"
            }
        }
    }
    codeflash_output = config._map_audio_params(params); result = codeflash_output # 2.65μs -> 2.32μs (13.9% faster)

def test_edge_voice_empty_string():
    # Test with voice as empty string
    config = VertexGeminiConfig()
    params = {"voice": "", "format": "pcm16"}
    expected = {
        "voiceConfig": {
            "prebuiltVoiceConfig": {
                "voiceName": ""
            }
        }
    }
    codeflash_output = config._map_audio_params(params); result = codeflash_output # 2.66μs -> 2.45μs (8.39% faster)

def test_edge_format_empty_string():
    # Test with format as empty string (should raise ValueError)
    config = VertexGeminiConfig()
    params = {"voice": "alloy", "format": ""}
    with pytest.raises(ValueError):
        config._map_audio_params(params) # 2.77μs -> 2.55μs (8.67% faster)

def test_edge_voice_int():
    # Test with voice as an integer
    config = VertexGeminiConfig()
    params = {"voice": 123, "format": "pcm16"}
    expected = {
        "voiceConfig": {
            "prebuiltVoiceConfig": {
                "voiceName": 123
            }
        }
    }
    codeflash_output = config._map_audio_params(params); result = codeflash_output # 2.80μs -> 2.47μs (13.3% faster)

def test_edge_voice_list():
    # Test with voice as a list
    config = VertexGeminiConfig()
    params = {"voice": ["a", "b"], "format": "pcm16"}
    expected = {
        "voiceConfig": {
            "prebuiltVoiceConfig": {
                "voiceName": ["a", "b"]
            }
        }
    }
    codeflash_output = config._map_audio_params(params); result = codeflash_output # 2.71μs -> 2.42μs (12.0% faster)

def test_edge_voice_dict():
    # Test with voice as a dict
    config = VertexGeminiConfig()
    params = {"voice": {"name": "alloy"}, "format": "pcm16"}
    expected = {
        "voiceConfig": {
            "prebuiltVoiceConfig": {
                "voiceName": {"name": "alloy"}
            }
        }
    }
    codeflash_output = config._map_audio_params(params); result = codeflash_output # 2.67μs -> 2.34μs (13.9% faster)

def test_edge_format_integer():
    # Test with format as integer (should raise ValueError)
    config = VertexGeminiConfig()
    params = {"voice": "alloy", "format": 123}
    with pytest.raises(ValueError):
        config._map_audio_params(params) # 2.91μs -> 2.65μs (9.70% faster)

def test_edge_voice_missing():
    # Test with format only, no voice
    config = VertexGeminiConfig()
    params = {"format": "pcm16"}
    expected = {}
    codeflash_output = config._map_audio_params(params); result = codeflash_output # 2.49μs -> 2.30μs (8.13% faster)

# -------- LARGE SCALE TEST CASES --------

def test_large_scale_many_calls():
    # Test calling the function many times with different voices
    config = VertexGeminiConfig()
    for i in range(100):
        params = {"voice": f"voice_{i}", "format": "pcm16"}
        expected = {
            "voiceConfig": {
                "prebuiltVoiceConfig": {
                    "voiceName": f"voice_{i}"
                }
            }
        }
        codeflash_output = config._map_audio_params(params); result = codeflash_output # 93.8μs -> 88.7μs (5.69% faster)

def test_large_scale_long_voice_string():
    # Test with a very long voice string
    config = VertexGeminiConfig()
    long_voice = "a" * 500
    params = {"voice": long_voice, "format": "pcm16"}
    expected = {
        "voiceConfig": {
            "prebuiltVoiceConfig": {
                "voiceName": long_voice
            }
        }
    }
    codeflash_output = config._map_audio_params(params); result = codeflash_output # 2.54μs -> 2.26μs (12.4% faster)

def test_large_scale_large_dict():
    # Test with a large dictionary with many irrelevant keys
    config = VertexGeminiConfig()
    params = {"voice": "alloy", "format": "pcm16"}
    # Add 900 irrelevant keys
    for i in range(900):
        params[f"irrelevant_{i}"] = i
    expected = {
        "voiceConfig": {
            "prebuiltVoiceConfig": {
                "voiceName": "alloy"
            }
        }
    }
    codeflash_output = config._map_audio_params(params); result = codeflash_output # 2.73μs -> 2.48μs (10.3% faster)

def test_large_scale_many_formats():
    # Test with many different formats, only 'pcm16' should pass
    config = VertexGeminiConfig()
    supported = ["pcm16"]
    unsupported = ["mp3", "wav", "ogg", "aac", "", None, 123]
    # Supported format
    for fmt in supported:
        params = {"voice": "alloy", "format": fmt}
        expected = {
            "voiceConfig": {
                "prebuiltVoiceConfig": {
                    "voiceName": "alloy"
                }
            }
        }
        codeflash_output = config._map_audio_params(params); result = codeflash_output # 2.52μs -> 2.30μs (9.71% faster)
    # Unsupported formats
    for fmt in unsupported:
        params = {"voice": "alloy", "format": fmt}
        if fmt == None:
            # None is ignored
            expected = {
                "voiceConfig": {
                    "prebuiltVoiceConfig": {
                        "voiceName": "alloy"
                    }
                }
            }
            codeflash_output = config._map_audio_params(params); result = codeflash_output
        else:
            with pytest.raises(ValueError):
                config._map_audio_params(params)

def test_large_scale_voice_unicode():
    # Test with unicode voice names
    config = VertexGeminiConfig()
    voices = ["αβγ", "你好", "こんにちは", "😀"]
    for v in voices:
        params = {"voice": v, "format": "pcm16"}
        expected = {
            "voiceConfig": {
                "prebuiltVoiceConfig": {
                    "voiceName": v
                }
            }
        }
        codeflash_output = config._map_audio_params(params); result = codeflash_output # 5.92μs -> 5.33μs (11.2% faster)

def test_large_scale_voice_list_of_lists():
    # Test with voice as a list of lists
    config = VertexGeminiConfig()
    voice = [["a", "b"], ["c", "d"]]
    params = {"voice": voice, "format": "pcm16"}
    expected = {
        "voiceConfig": {
            "prebuiltVoiceConfig": {
                "voiceName": voice
            }
        }
    }
    codeflash_output = config._map_audio_params(params); result = codeflash_output # 2.44μs -> 2.21μs (10.6% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

To edit these changes git checkout codeflash/optimize-VertexGeminiConfig._map_audio_params-mhof50ga and push.

Codeflash Static Badge

The optimization achieves a **7% speedup** by making two key changes:

1. **Fixed attribute assignment bug in `__init__`**: The original code incorrectly used `setattr(self.__class__, key, value)` which sets attributes on the **class** rather than the **instance**. The optimized version correctly uses `setattr(self, key, value)` to set instance attributes. This bug fix also improves performance by avoiding unnecessary class-level mutations.

2. **Streamlined dictionary construction in `_map_audio_params`**: Instead of creating empty intermediate dictionaries and then populating them step-by-step, the optimized version constructs the nested dictionary structure in a single literal when a voice is present. This eliminates multiple dictionary assignments and intermediate variable allocations.

The line profiler shows the optimization reduces total execution time from 632ns to 642ns in the profiled run, but the overall runtime improvement of 7% (161μs → 150μs) demonstrates consistent gains. Test results show **8-16% improvements** across individual test cases, with the most significant gains in basic functionality tests that would be called frequently.

The optimizations are particularly effective for:
- **Voice-only parameters** (14% faster) - common in audio configuration
- **Format validation scenarios** (8-12% faster) - frequently triggered validation paths  
- **Empty/minimal parameter cases** (7-16% faster) - lightweight initialization scenarios

These improvements matter because audio parameter mapping is likely called during model initialization and configuration, where even small optimizations compound across multiple invocations.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 November 7, 2025 05:32
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash labels Nov 7, 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