fix(quantization): add missing constexpr and static_assert in INT8Quantizer::ComputeImpl#1719
Hidden character warning
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical issue in the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
The pull request effectively resolves undefined behavior in the INT8Quantizer::ComputeImpl method by ensuring all code paths return a value. The addition of a static_assert provides compile-time validation for supported metric types, and the consistent use of if constexpr aids in compile-time branch elimination, improving code robustness and efficiency.
src/quantization/int8_quantizer.cpp
Outdated
| similarity /= mold1[0] * mold2[0]; | ||
| return 1.0F - std::max(-1.0F, std::min(1.0F, similarity)); | ||
| } else if (metric == MetricType::METRIC_TYPE_L2SQR) { | ||
| } else if constexpr (metric == MetricType::METRIC_TYPE_L2SQR) { |
There was a problem hiding this comment.
Given the static_assert at the beginning of the ComputeImpl function (lines 109-112) that guarantees metric will be one of METRIC_TYPE_IP, METRIC_TYPE_COSINE, or METRIC_TYPE_L2SQR, this else if constexpr can be simplified to a plain else statement. This would logically indicate that this branch handles the only remaining valid metric type after the previous if constexpr conditions have been evaluated.
| } else if constexpr (metric == MetricType::METRIC_TYPE_L2SQR) { | |
| } else { |
There was a problem hiding this comment.
Fixed in commit c7fe425: simplified else if constexpr to else as suggested. Thanks!
There was a problem hiding this comment.
Pull request overview
This PR hardens INT8Quantizer::ComputeImpl against unsupported template instantiations and ensures compile-time branch selection for all supported metrics, eliminating the possibility of reaching the end of a non-void function without returning.
Changes:
- Added a
static_assertto restrictINT8Quantizer<metric>to supported metrics (IP, COSINE, L2SQR). - Converted the L2SQR branch to
else if constexprso all metric branches are compile-time selected. - Ensured
ComputeImplalways returns for the explicitly instantiated metric specializations.
…ntizer::ComputeImpl - Add static_assert to ensure only supported metric types are used - Change else if to else for the final L2SQR branch - This ensures compile-time checking and removes unreachable code Signed-off-by: LHT129 <tianlan.lht@antgroup.com>
c7fe425 to
dd74a5b
Compare
There was a problem hiding this comment.
Pull request overview
Fixes a potential undefined-behavior path in INT8Quantizer::ComputeImpl by enforcing compile-time metric constraints and ensuring all control-flow paths return a value.
Changes:
- Added a
static_assertto restrictINT8Quantizerinstantiations to supported metrics (IP/COSINE/L2SQR). - Adjusted branch structure so the L2SQR path is handled by the final
elsebranch, ensuring a return on all paths.
| static_assert(metric == MetricType::METRIC_TYPE_IP || | ||
| metric == MetricType::METRIC_TYPE_COSINE || | ||
| metric == MetricType::METRIC_TYPE_L2SQR, | ||
| "Unsupported metric type for INT8Quantizer"); | ||
| if constexpr (metric == MetricType::METRIC_TYPE_IP) { |
There was a problem hiding this comment.
The PR description says the code was changed to else if constexpr (metric == MetricType::METRIC_TYPE_L2SQR), but the diff shows the L2SQR path is now handled via a plain else. Either update the PR description to match the implementation, or change the code to use else if constexpr as described.
| similarity /= mold1[0] * mold2[0]; | ||
| return 1.0F - std::max(-1.0F, std::min(1.0F, similarity)); | ||
| } else if (metric == MetricType::METRIC_TYPE_L2SQR) { | ||
| } else { |
There was a problem hiding this comment.
Using a bare else makes it less explicit that this branch is specifically for METRIC_TYPE_L2SQR (the reader has to infer it from the static_assert plus earlier branches). Consider making this branch an explicit else if constexpr (metric == MetricType::METRIC_TYPE_L2SQR) for self-documentation, while keeping the static_assert for compile-time enforcement.
| } else { | |
| } else if constexpr (metric == MetricType::METRIC_TYPE_L2SQR) { |
Summary
This PR fixes undefined behavior in
INT8Quantizer::ComputeImplmethod caused by a missingreturnstatement in the L2SQR branch.Changes
static_assertto ensure only supported metric types (IP, COSINE, L2SQR) are usedelse if (metric == MetricType::METRIC_TYPE_L2SQR)toelse if constexpr (metric == MetricType::METRIC_TYPE_L2SQR)if constexprfor compile-time branch eliminationTechnical Details
Problem:
The
ComputeImplmethod had a code path that could lead to undefined behavior:if constexpr(compile-time branch elimination)if(no constexpr), so compiler couldn't eliminate fall-throughreturnstatement at end of function → undefined behavior for non-void functionSolution:
static_assertto validate metric type at compile timeconstexprto L2SQR branch for consistency with other branchesTesting
Files Changed
src/quantization/int8_quantizer.cpp: Added static_assert and constexpr to ComputeImpl methodRelated Issues
Checklist