⚡️ Speed up function is_fa3_default_architecture by 56%
#464
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.
📄 56% (0.56x) speedup for
is_fa3_default_architectureinpython/sglang/srt/utils/common.py⏱️ Runtime :
48.4 microseconds→31.0 microseconds(best of96runs)📝 Explanation and details
The optimization moves the immutable
default_archsset definition outside the function and applies two key performance improvements:What was optimized:
Eliminated repeated set construction: The original code recreated the 10-element set on every function call (line profiler shows 27.4% of time spent on set creation). The optimized version moves this to module level as
_default_archs, creating it only once during import.Streamlined conditional logic: Changed
if not isinstance(architectures, list) or not architectures:toif not (isinstance(architectures, list) and architectures):, using short-circuiting more efficiently.Why it's faster:
isinstance()returns False, avoiding the secondnot architecturescheck.Performance impact based on function references:
The function is called in a critical path within
model_specific_adjustment()during model initialization, specifically when auto-selecting attention backends for different GPU architectures (Hopper, etc.). This optimization is particularly valuable because:Test case analysis:
The optimization shows consistent 50-90% speedups across all test scenarios, with particularly strong gains for:
The 55% overall speedup makes model initialization more responsive while maintaining identical functionality.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-is_fa3_default_architecture-mijqage4and push.