⚡️ Speed up function create_finetune_request by 15% - #8
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
Conversation
The optimized code achieves a **15% speedup** through two key optimizations: **1. Fast-path optimization in `log_warn_once`**: - Added an early membership check using a lightweight string key (`msg_candidate`) before expensive `logfmt` formatting - This avoids costly regex operations and dictionary formatting for duplicate warnings (the common case) - From profiler: reduces `logfmt` calls from 44 to 1, saving ~550μs per duplicate warning **2. Reduced attribute lookups in `create_finetune_request`**: - Cached `model_limits.lora_training` and `model_limits.full_training` in local variables (`lora_cfg`, `full_cfg`) - Eliminated repeated attribute access overhead when extracting batch size limits - Consolidated validation logic to use cached references instead of multiple dotted lookups **Performance characteristics by test case**: - **Basic cases** (20-38% faster): Benefit most from reduced attribute lookups during normal execution - **Error cases with DPO/SFT validation** (90-200% faster): Fast-path through validation logic with fewer attribute accesses - **Large scale cases** (20-35% faster): Compound benefits from both optimizations - **Warning-heavy cases** (minimal impact): The `log_warn_once` optimization only helps on repeated calls The optimizations maintain identical behavior while eliminating redundant work - particularly effective for the common path of successful request creation and duplicate warning suppression.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📄 15% (0.15x) speedup for
create_finetune_requestinsrc/together/resources/finetune.py⏱️ Runtime :
2.26 milliseconds→1.97 milliseconds(best of88runs)📝 Explanation and details
The optimized code achieves a 15% speedup through two key optimizations:
1. Fast-path optimization in
log_warn_once:msg_candidate) before expensivelogfmtformattinglogfmtcalls from 44 to 1, saving ~550μs per duplicate warning2. Reduced attribute lookups in
create_finetune_request:model_limits.lora_trainingandmodel_limits.full_trainingin local variables (lora_cfg,full_cfg)Performance characteristics by test case:
log_warn_onceoptimization only helps on repeated callsThe optimizations maintain identical behavior while eliminating redundant work - particularly effective for the common path of successful request creation and duplicate warning suppression.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
unit/test_finetune_resources.py::test_bad_max_grad_normunit/test_finetune_resources.py::test_bad_min_lr_ratiounit/test_finetune_resources.py::test_bad_training_methodunit/test_finetune_resources.py::test_bad_warmupunit/test_finetune_resources.py::test_bad_weight_decayunit/test_finetune_resources.py::test_batch_size_limitunit/test_finetune_resources.py::test_both_from_checkpoint_model_nameunit/test_finetune_resources.py::test_dpo_requestunit/test_finetune_resources.py::test_dpo_request_loraunit/test_finetune_resources.py::test_from_checkpoint_requestunit/test_finetune_resources.py::test_lora_requestunit/test_finetune_resources.py::test_lora_request_with_lora_dropoutunit/test_finetune_resources.py::test_no_from_checkpoint_no_model_nameunit/test_finetune_resources.py::test_no_training_fileunit/test_finetune_resources.py::test_non_full_modelunit/test_finetune_resources.py::test_non_lora_modelunit/test_finetune_resources.py::test_simple_requestunit/test_finetune_resources.py::test_train_on_inputs_for_sftunit/test_finetune_resources.py::test_train_on_inputs_not_supported_for_dpounit/test_finetune_resources.py::test_validation_file🌀 Generated Regression Tests and Runtime
⏪ Replay Tests and Runtime
To edit these changes
git checkout codeflash/optimize-create_finetune_request-mgzsj4q6and push.