Skip to content

Conversation

@codeflash-ai
Copy link

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

📄 106% (1.06x) speedup for intermediate_name in stanza/models/classifier.py

⏱️ Runtime : 2.43 milliseconds 1.18 milliseconds (best of 315 runs)

📝 Explanation and details

The optimized code achieves a 105% speedup through two key optimizations:

1. Custom file extension parsing replaces os.path.splitext()

  • Uses filename.rfind('.') to find the last dot position, then slices the string manually
  • Eliminates the overhead of os.path.splitext() which creates a tuple and performs additional validation
  • Line profiler shows this reduces the most expensive operation from 67.6% to just 43% of total time across multiple operations

2. F-string formatting replaces .format() with dictionary unpacking

  • Changes from ".format(**{"epoch": epoch, "score_type": dev_scoring.value, "acc": score * 100})" to f-string
  • Eliminates dictionary creation, unpacking overhead, and the slower .format() method
  • F-strings are compiled to more efficient bytecode for simple variable interpolation

Performance characteristics by test type:

  • Basic cases: Consistent ~2x speedup across all standard filenames and extensions
  • Edge cases: Excellent performance on files without extensions, multiple dots, and special characters
  • Large scale: Maintains speedup even with 1000+ iterations, long filenames (500+ chars), and varied parameters

The optimization is particularly effective because intermediate_name() is typically called in training loops where model checkpoints are frequently saved, making this a high-impact improvement for ML workflows.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 1387 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
import os
from collections import namedtuple

# imports
import pytest  # used for our unit tests
from stanza.models.classifier import intermediate_name

# Helper for dev_scoring mock (since we can't use pytest.mock)
ScoreType = namedtuple("ScoreType", ["value"])

# ---------------------------
# Basic Test Cases
# ---------------------------

def test_basic_typical_filename():
    # Typical usage with .pt extension
    filename = "model.pt"
    epoch = 7
    dev_scoring = ScoreType("acc")
    score = 0.8532
    expected = "model.E0007-acc85.32.pt"
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)

def test_basic_zero_epoch():
    # Epoch is zero
    filename = "checkpoint.ckpt"
    epoch = 0
    dev_scoring = ScoreType("f1")
    score = 0.91234
    expected = "checkpoint.E0000-f191.23.ckpt"
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)

def test_basic_score_rounding():
    # Score is rounded correctly to two decimals
    filename = "run.bin"
    epoch = 12
    dev_scoring = ScoreType("acc")
    score = 0.89999
    expected = "run.E0012-acc90.00.bin"
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)

def test_basic_no_extension():
    # Filename without extension
    filename = "model"
    epoch = 3
    dev_scoring = ScoreType("loss")
    score = 0.12345
    expected = "model.E0003-loss12.35"
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)

# ---------------------------
# Edge Test Cases
# ---------------------------

def test_edge_negative_epoch():
    # Negative epoch
    filename = "edge.pt"
    epoch = -1
    dev_scoring = ScoreType("acc")
    score = 0.5
    expected = "edge.E-001-acc50.00.pt"
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)

def test_edge_large_epoch():
    # Large epoch number
    filename = "big.pt"
    epoch = 1234
    dev_scoring = ScoreType("acc")
    score = 0.75
    expected = "big.E1234-acc75.00.pt"
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)

def test_edge_score_zero():
    # Score is zero
    filename = "zero.pt"
    epoch = 1
    dev_scoring = ScoreType("acc")
    score = 0.0
    expected = "zero.E0001-acc00.00.pt"
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)

def test_edge_score_one():
    # Score is one (100%)
    filename = "perfect.pt"
    epoch = 10
    dev_scoring = ScoreType("acc")
    score = 1.0
    expected = "perfect.E0010-acc100.00.pt"
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)

def test_edge_score_negative():
    # Negative score
    filename = "neg.pt"
    epoch = 2
    dev_scoring = ScoreType("acc")
    score = -0.25
    expected = "neg.E0002-acc-25.00.pt"
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)

def test_edge_score_over_one():
    # Score greater than 1
    filename = "over.pt"
    epoch = 5
    dev_scoring = ScoreType("acc")
    score = 1.234
    expected = "over.E0005-acc123.40.pt"
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)

def test_edge_filename_with_multiple_dots():
    # Filename with multiple dots
    filename = "my.model.best.pt"
    epoch = 4
    dev_scoring = ScoreType("f1")
    score = 0.999
    expected = "my.model.best.E0004-f199.90.pt"
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)

def test_edge_filename_hidden_file():
    # Hidden file (starts with dot)
    filename = ".hidden"
    epoch = 1
    dev_scoring = ScoreType("acc")
    score = 0.5
    expected = ".hidden.E0001-acc50.00"
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)

def test_edge_filename_empty_string():
    # Empty filename
    filename = ""
    epoch = 1
    dev_scoring = ScoreType("acc")
    score = 0.5
    expected = ".E0001-acc50.00"
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)

def test_edge_score_type_empty_string():
    # Empty score type
    filename = "empty.pt"
    epoch = 1
    dev_scoring = ScoreType("")
    score = 0.5
    expected = "empty.E0001-50.00.pt"
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)

def test_edge_score_type_special_chars():
    # Score type with special characters
    filename = "special.pt"
    epoch = 2
    dev_scoring = ScoreType("acc@#")
    score = 0.75
    expected = "special.E0002-acc@#75.00.pt"
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)

def test_edge_filename_with_spaces():
    # Filename with spaces
    filename = "my model.pt"
    epoch = 3
    dev_scoring = ScoreType("acc")
    score = 0.5
    expected = "my model.E0003-acc50.00.pt"
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)

def test_edge_filename_with_unicode():
    # Filename with unicode characters
    filename = "模型.pt"
    epoch = 1
    dev_scoring = ScoreType("acc")
    score = 0.99
    expected = "模型.E0001-acc99.00.pt"
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)

def test_edge_score_type_unicode():
    # Score type with unicode
    filename = "unicode.pt"
    epoch = 1
    dev_scoring = ScoreType("准确率")
    score = 0.88
    expected = "unicode.E0001-准确率88.00.pt"
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)

def test_edge_filename_with_path():
    # Filename with path
    filename = "checkpoints/model.pt"
    epoch = 6
    dev_scoring = ScoreType("acc")
    score = 0.7
    expected = "checkpoints/model.E0006-acc70.00.pt"
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)

def test_edge_filename_with_long_extension():
    # Filename with long extension
    filename = "data.model.checkpoint"
    epoch = 2
    dev_scoring = ScoreType("f1")
    score = 0.77
    expected = "data.model.E0002-f177.00.checkpoint"
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)

# ---------------------------
# Large Scale Test Cases
# ---------------------------

def test_large_many_epochs():
    # Test with many epochs (up to 999)
    filename = "large.pt"
    dev_scoring = ScoreType("acc")
    score = 0.8
    for epoch in range(0, 1000, 100):  # 0, 100, ..., 900
        expected = f"large.E{epoch:04d}-acc80.00.pt"
        codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)

def test_large_many_score_types():
    # Test with many different score types
    filename = "multi.pt"
    epoch = 5
    score = 0.6
    score_types = [f"type{i}" for i in range(100)]  # 100 types
    for st in score_types:
        expected = f"multi.E0005-{st}60.00.pt"
        codeflash_output = intermediate_name(filename, epoch, ScoreType(st), score)

def test_large_many_filenames():
    # Test with many different filenames
    epoch = 1
    dev_scoring = ScoreType("acc")
    score = 0.5
    for i in range(100):  # 100 filenames
        filename = f"file{i}.pt"
        expected = f"file{i}.E0001-acc50.00.pt"
        codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)

def test_large_long_filename():
    # Test with a very long filename (255 chars, typical filesystem limit)
    base = "a" * 250
    filename = base + ".pt"
    epoch = 1
    dev_scoring = ScoreType("acc")
    score = 0.5
    expected = base + ".E0001-acc50.00.pt"
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)

def test_large_score_precision():
    # Test with scores with high precision
    filename = "precise.pt"
    epoch = 1
    dev_scoring = ScoreType("acc")
    for score in [0.123456789, 0.987654321, 0.00001, 0.99999]:
        expected = f"precise.E0001-acc{score*100:05.2f}.pt"
        codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)

def test_large_varied_extensions():
    # Test with many different extensions
    epoch = 2
    dev_scoring = ScoreType("acc")
    score = 0.75
    extensions = ["", ".pt", ".ckpt", ".tar", ".bin", ".model", ".h5", ".weights", ".data", ".pkl"]
    for ext in extensions:
        filename = "varied" + ext
        root, _ = os.path.splitext(filename)
        expected = root + f".E0002-acc75.00" + ext
        codeflash_output = intermediate_name(filename, epoch, dev_scoring, score)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
import os
from collections import namedtuple

# imports
import pytest  # used for our unit tests
from stanza.models.classifier import intermediate_name

# Helper for dev_scoring mock
DevScoring = namedtuple('DevScoring', ['value'])

# ========================
# Basic Test Cases
# ========================

def test_basic_filename_with_txt_extension():
    # Test with a standard filename and extension
    filename = "model.txt"
    epoch = 1
    dev_scoring = DevScoring("acc")
    score = 0.95
    # Expected: model.E0001-acc95.00.txt
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score); result = codeflash_output

def test_basic_filename_with_no_extension():
    # Test with a filename without extension
    filename = "checkpoint"
    epoch = 12
    dev_scoring = DevScoring("f1")
    score = 0.81234
    # Expected: checkpoint.E0012-f181.23
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score); result = codeflash_output

def test_basic_filename_with_multiple_dots():
    # Test with filename containing multiple dots
    filename = "my.model.best.pt"
    epoch = 123
    dev_scoring = DevScoring("acc")
    score = 0.87654
    # Expected: my.model.best.E0123-acc87.65.pt
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score); result = codeflash_output

def test_basic_epoch_zero():
    # Test with epoch zero
    filename = "foo.bin"
    epoch = 0
    dev_scoring = DevScoring("f1")
    score = 1.0
    # Expected: foo.E0000-f1100.00.bin
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score); result = codeflash_output

def test_basic_score_zero():
    # Test with score zero
    filename = "bar.model"
    epoch = 5
    dev_scoring = DevScoring("acc")
    score = 0.0
    # Expected: bar.E0005-acc00.00.model
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score); result = codeflash_output

# ========================
# Edge Test Cases
# ========================

def test_edge_filename_empty_string():
    # Test with empty filename
    filename = ""
    epoch = 9
    dev_scoring = DevScoring("f1")
    score = 0.5
    # Expected: .E0009-f150.00
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score); result = codeflash_output

def test_edge_filename_dot_only():
    # Test with filename that is just a dot
    filename = "."
    epoch = 2
    dev_scoring = DevScoring("acc")
    score = 0.333
    # Expected: .E0002-acc33.30
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score); result = codeflash_output

def test_edge_filename_with_spaces():
    # Test with filename containing spaces
    filename = "my model .pth"
    epoch = 42
    dev_scoring = DevScoring("f1")
    score = 0.12345
    # Expected: 'my model .E0042-f112.35.pth'
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score); result = codeflash_output

def test_edge_dev_scoring_non_string_value():
    # Test with dev_scoring.value as a non-string (int)
    dev_scoring = DevScoring(123)
    filename = "test.bin"
    epoch = 7
    score = 0.789
    # Should convert dev_scoring.value to string in format
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score); result = codeflash_output

def test_edge_score_negative():
    # Test with negative score
    filename = "neg.pt"
    epoch = 3
    dev_scoring = DevScoring("acc")
    score = -0.12
    # Expected: neg.E0003-acc-12.00.pt
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score); result = codeflash_output

def test_edge_score_over_one():
    # Test with score > 1.0
    filename = "highscore.model"
    epoch = 9999
    dev_scoring = DevScoring("f1")
    score = 1.2345
    # Expected: highscore.E9999-f123.45.model
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score); result = codeflash_output

def test_edge_epoch_large():
    # Test with epoch larger than 9999 (overflow)
    filename = "overflow.ckpt"
    epoch = 12345
    dev_scoring = DevScoring("acc")
    score = 0.999
    # Expected: overflow.E12345-acc99.90.ckpt (no truncation)
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score); result = codeflash_output

def test_edge_score_many_decimals():
    # Test with score having many decimals
    filename = "precise.bin"
    epoch = 1
    dev_scoring = DevScoring("acc")
    score = 0.123456789
    # Expected: precise.E0001-acc12.35.bin (rounded to two decimals)
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score); result = codeflash_output

def test_edge_filename_with_unicode():
    # Test with unicode characters in filename
    filename = "模型.测试.pt"
    epoch = 88
    dev_scoring = DevScoring("acc")
    score = 0.65432
    # Expected: 模型.测试.E0088-acc65.43.pt
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score); result = codeflash_output

def test_edge_filename_with_special_characters():
    # Test with special characters in filename
    filename = "foo@bar#baz!.ckpt"
    epoch = 77
    dev_scoring = DevScoring("f1")
    score = 0.9999
    # Expected: foo@bar#baz!.E0077-f199.99.ckpt
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score); result = codeflash_output

def test_edge_filename_with_long_extension():
    # Test with a long extension
    filename = "longfile.extensionislong"
    epoch = 5
    dev_scoring = DevScoring("acc")
    score = 0.5
    # Expected: longfile.E0005-acc50.00.extensionislong
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score); result = codeflash_output

# ========================
# Large Scale Test Cases
# ========================

def test_large_scale_many_files():
    # Test with a large number of filenames
    filenames = [f"file_{i}.bin" for i in range(1000)]
    epoch = 10
    dev_scoring = DevScoring("acc")
    score = 0.75
    # All results should have correct formatting and unique names
    for i, filename in enumerate(filenames):
        codeflash_output = intermediate_name(filename, epoch, dev_scoring, score); result = codeflash_output
        expected = f"file_{i}.E0010-acc75.00.bin"

def test_large_scale_long_filename():
    # Test with a very long filename
    filename = "a" * 500 + ".pt"
    epoch = 999
    dev_scoring = DevScoring("f1")
    score = 0.1234
    # Expected: 'a'*500 + '.E0999-f112.34.pt'
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score); result = codeflash_output
    expected = "a" * 500 + ".E0999-f112.34.pt"

def test_large_scale_varied_epochs_and_scores():
    # Test with varied epochs and scores
    filename = "varied.model"
    dev_scoring = DevScoring("acc")
    for epoch in [0, 1, 10, 100, 999, 1000]:
        for score in [0.0, 0.5, 0.99999, 1.0]:
            codeflash_output = intermediate_name(filename, epoch, dev_scoring, score); result = codeflash_output
            # Format expected manually
            expected = f"varied.E{epoch:04d}-acc{score*100:05.2f}.model"

def test_large_scale_all_score_types():
    # Test with many different score types
    filename = "scoretype.model"
    epoch = 12
    score = 0.33
    score_types = [f"type{i}" for i in range(100)]
    for stype in score_types:
        dev_scoring = DevScoring(stype)
        codeflash_output = intermediate_name(filename, epoch, dev_scoring, score); result = codeflash_output
        expected = f"scoretype.E0012-{stype}33.00.model"

def test_large_scale_high_epoch_and_score():
    # Test with very high epoch and score
    filename = "huge.model"
    epoch = 99999
    dev_scoring = DevScoring("f1")
    score = 123.456
    # Expected: huge.E99999-f112345.60.model
    codeflash_output = intermediate_name(filename, epoch, dev_scoring, score); result = codeflash_output
# 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-intermediate_name-mh9q0t0g and push.

Codeflash

The optimized code achieves a **105% speedup** through two key optimizations:

**1. Custom file extension parsing replaces `os.path.splitext()`**
- Uses `filename.rfind('.')` to find the last dot position, then slices the string manually
- Eliminates the overhead of `os.path.splitext()` which creates a tuple and performs additional validation
- Line profiler shows this reduces the most expensive operation from 67.6% to just 43% of total time across multiple operations

**2. F-string formatting replaces `.format()` with dictionary unpacking**
- Changes from `".format(**{"epoch": epoch, "score_type": dev_scoring.value, "acc": score * 100})"` to f-string
- Eliminates dictionary creation, unpacking overhead, and the slower `.format()` method
- F-strings are compiled to more efficient bytecode for simple variable interpolation

**Performance characteristics by test type:**
- **Basic cases**: Consistent ~2x speedup across all standard filenames and extensions
- **Edge cases**: Excellent performance on files without extensions, multiple dots, and special characters
- **Large scale**: Maintains speedup even with 1000+ iterations, long filenames (500+ chars), and varied parameters

The optimization is particularly effective because `intermediate_name()` is typically called in training loops where model checkpoints are frequently saved, making this a high-impact improvement for ML workflows.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 27, 2025 22:40
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash labels Oct 27, 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