-
Notifications
You must be signed in to change notification settings - Fork 648
fix(ops): validate tabulation tensor shapes #5932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
njzjz-bot
wants to merge
2
commits into
deepmodeling:master
Choose a base branch
from
njzjz:fix/5895-tabulate-shape-validation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| // SPDX-License-Identifier: LGPL-3.0-or-later | ||
| #pragma once | ||
|
|
||
| #include <cmath> | ||
| #include <cstdint> | ||
| #include <limits> | ||
| #include <string> | ||
|
|
||
| namespace deepmd { | ||
|
|
||
| // Multiply non-negative tensor dimensions without invoking signed overflow. | ||
| inline bool tabulate_checked_product(const int64_t lhs, | ||
| const int64_t rhs, | ||
| int64_t& product) { | ||
| if (lhs < 0 || rhs < 0 || | ||
| (rhs != 0 && lhs > std::numeric_limits<int64_t>::max() / rhs)) { | ||
| return false; | ||
| } | ||
| product = lhs * rhs; | ||
| return true; | ||
| } | ||
|
|
||
| // Validate the five table metadata values consumed by the native tabulation | ||
| // kernels and reproduce the spline-row count used by the table generator. | ||
| // Keeping this calculation shared prevents the TensorFlow and PyTorch wrappers | ||
| // from accepting different raw-buffer contracts. | ||
| template <typename FPTYPE> | ||
| bool tabulate_required_table_rows(const FPTYPE* table_info, | ||
| const bool symmetric_range, | ||
| int64_t& required_rows, | ||
| std::string& error) { | ||
| const double lower = static_cast<double>(table_info[0]); | ||
| const double upper = static_cast<double>(table_info[1]); | ||
| const double max = static_cast<double>(table_info[2]); | ||
| const double stride0 = static_cast<double>(table_info[3]); | ||
| const double stride1 = static_cast<double>(table_info[4]); | ||
| if (!std::isfinite(lower) || !std::isfinite(upper) || !std::isfinite(max) || | ||
| !std::isfinite(stride0) || !std::isfinite(stride1)) { | ||
| error = "table_info values must be finite"; | ||
| return false; | ||
| } | ||
| if (stride0 <= 0.0 || stride1 <= 0.0) { | ||
| error = "table_info strides must be positive"; | ||
| return false; | ||
| } | ||
|
|
||
| const double min = symmetric_range ? -max : lower; | ||
| if (min > lower || lower > upper || upper > max) { | ||
| error = symmetric_range | ||
| ? "table_info must satisfy -max <= lower <= upper <= max" | ||
| : "table_info must satisfy lower <= upper <= max"; | ||
| return false; | ||
| } | ||
|
|
||
| const double lower_tail = symmetric_range ? (lower - min) / stride1 : 0.0; | ||
| const double middle = (upper - lower) / stride0; | ||
| const double upper_tail = (max - upper) / stride1; | ||
| const double total_intervals = lower_tail + middle + upper_tail; | ||
| const double max_segment = | ||
| static_cast<double>(std::numeric_limits<int>::max()); | ||
| if (!std::isfinite(lower_tail) || !std::isfinite(middle) || | ||
| !std::isfinite(upper_tail) || !std::isfinite(total_intervals) || | ||
| total_intervals > max_segment) { | ||
| error = "table_info describes too many spline intervals"; | ||
| return false; | ||
| } | ||
|
|
||
| // The Python table builder converts the sum to an integer once, which is | ||
| // observably different from truncating each range separately for SE-T. | ||
| required_rows = static_cast<int64_t>(total_intervals); | ||
| if (required_rows <= 0) { | ||
| error = "table_info must describe at least one spline interval"; | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| // Convert the validated row count into the flattened coefficient count while | ||
| // guarding the multiplication used by both framework wrappers. | ||
| inline bool tabulate_required_table_elements(const int64_t required_rows, | ||
| const int64_t last_layer_size, | ||
| int64_t& required_elements, | ||
| std::string& error) { | ||
| constexpr int64_t coefficients_per_feature = 6; | ||
| if (required_rows <= 0 || last_layer_size <= 0) { | ||
| error = "table dimensions must be positive"; | ||
| return false; | ||
| } | ||
| int64_t feature_elements = 0; | ||
| if (!tabulate_checked_product(last_layer_size, coefficients_per_feature, | ||
| feature_elements) || | ||
| !tabulate_checked_product(required_rows, feature_elements, | ||
| required_elements)) { | ||
| error = "required table size exceeds the supported integer range"; | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| } // namespace deepmd | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.