⚡️ Speed up method MoeWNA16Config.is_moe_wna16_compatible by 3,041%
#331
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.
📄 3,041% (30.41x) speedup for
MoeWNA16Config.is_moe_wna16_compatibleinpython/sglang/srt/layers/quantization/moe_wna16.py⏱️ Runtime :
33.7 milliseconds→1.07 milliseconds(best of63runs)📝 Explanation and details
The optimization adds
@lru_cache(maxsize=1)to theget_device_capabilityfunction, which provides a dramatic 31x speedup by eliminating redundant system calls.Key Optimization:
@lru_cache(maxsize=1)decorator caches the result ofget_device_capability(), which involves expensive torch API calls liketorch.cuda.is_available()andtorch.cuda.get_device_capability().Why This Works:
get_device_capability()repeatedly (1680 times in profiling), each time making expensive torch API callstorch.cuda.is_available()alone), while the cached version takes only 2.6msPerformance Impact:
maxsize=1is sufficient since device capabilities rarely vary within a single process, and the defaultdevice_id=0covers the common use caseUse Cases:
The optimization particularly benefits scenarios where
MoeWNA16Config.is_moe_wna16_compatible()is called frequently, such as during model loading, quantization setup, or configuration validation loops - all critical paths in ML inference systems.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-MoeWNA16Config.is_moe_wna16_compatible-mhoyvbxsand push.