-
Notifications
You must be signed in to change notification settings - Fork 27
[RSDK-10284]: MLModelService: don't check for name if there is only 1 expected tensor, return error if expected tensors not present #394
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
Merged
bhaney
merged 10 commits into
viamrobotics:main
from
bhaney:make-mlmodel-server-less-restrictive
Mar 27, 2025
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c6352fe
dont do metadata check
bhaney 3c586c1
Merge branch 'main' into make-mlmodel-server-less-restrictive
bhaney ad72349
makes single tensor exception, and adds error
bhaney 117a1f1
linter
bhaney dd0e8c2
add type check in 1 input case too
bhaney c03fc15
bug fix
bhaney 23e0930
undo the bad linting
bhaney 439f8b2
header and comment
bhaney 4943664
linting
bhaney f2cae06
small tweak
bhaney 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
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
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
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
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
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
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 |
|---|---|---|
|
|
@@ -12,12 +12,11 @@ | |
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include <viam/sdk/services/private/mlmodel_server.hpp> | ||
|
|
||
| #include <viam/sdk/common/private/service_helper.hpp> | ||
| #include <viam/sdk/rpc/server.hpp> | ||
| #include <viam/sdk/services/mlmodel.hpp> | ||
| #include <viam/sdk/services/private/mlmodel.hpp> | ||
| #include <viam/sdk/services/private/mlmodel_server.hpp> | ||
|
|
||
| namespace viam { | ||
| namespace sdk { | ||
|
|
@@ -38,17 +37,13 @@ ::grpc::Status MLModelServiceServer::Infer( | |
|
|
||
| const auto md = mlms->metadata({}); | ||
| MLModelService::named_tensor_views inputs; | ||
| for (const auto& input : md.inputs) { | ||
| const auto where = request->input_tensors().tensors().find(input.name); | ||
| if (where == request->input_tensors().tensors().end()) { | ||
| // Ignore any inputs for which we don't have metadata, since | ||
| // we can't validate the type info. | ||
| // | ||
| // TODO: Should this be an error? For now we just don't decode | ||
| // those tensors. | ||
| continue; | ||
| } | ||
| auto tensor = mlmodel::make_sdk_tensor_from_api_tensor(where->second); | ||
|
|
||
| // Check if there's only one input tensor and metadata only expects one, too | ||
| if (request->input_tensors().tensors().size() == 1 && md.inputs.size() == 1) { | ||
| // Special case: just one tensor, add it without name check | ||
| const MLModelService::tensor_info input = md.inputs[0]; | ||
| const auto& tensor_pair = *request->input_tensors().tensors().begin(); | ||
| auto tensor = mlmodel::make_sdk_tensor_from_api_tensor(tensor_pair.second); | ||
| const auto tensor_type = MLModelService::tensor_info::tensor_views_to_data_type(tensor); | ||
| if (tensor_type != input.data_type) { | ||
| std::ostringstream message; | ||
|
|
@@ -58,7 +53,35 @@ ::grpc::Status MLModelServiceServer::Infer( | |
| << static_cast<ut>(tensor_type); | ||
| return helper.fail(::grpc::INVALID_ARGUMENT, message.str().c_str()); | ||
| } | ||
| inputs.emplace(std::move(input.name), std::move(tensor)); | ||
| inputs.emplace(tensor_pair.first, std::move(tensor)); | ||
| } else { | ||
| // Normal case: multiple tensors, do metadata checks | ||
| for (const auto& input : md.inputs) { | ||
| const auto where = request->input_tensors().tensors().find(input.name); | ||
| if (where == request->input_tensors().tensors().end()) { | ||
| // Ignore any inputs for which we don't have metadata, since | ||
|
||
| // we can't validate the type info. | ||
| // if the input vector of the expected name is not found, return an error | ||
| std::ostringstream message; | ||
| message << "Expected tensor input `" << input.name | ||
| << "` was not found; if you believe you have this tensor under a " | ||
| "different name, rename it to the expected tensor name"; | ||
| return helper.fail(::grpc::INVALID_ARGUMENT, message.str().c_str()); | ||
| } | ||
| auto tensor = mlmodel::make_sdk_tensor_from_api_tensor(where->second); | ||
| const auto tensor_type = | ||
| MLModelService::tensor_info::tensor_views_to_data_type(tensor); | ||
| if (tensor_type != input.data_type) { | ||
| std::ostringstream message; | ||
| using ut = std::underlying_type<MLModelService::tensor_info::data_types>::type; | ||
| message << "Tensor input `" << input.name | ||
| << "` was the wrong type; expected type " | ||
| << static_cast<ut>(input.data_type) << " but got type " | ||
| << static_cast<ut>(tensor_type); | ||
| return helper.fail(::grpc::INVALID_ARGUMENT, message.str().c_str()); | ||
| } | ||
| inputs.emplace(std::move(input.name), std::move(tensor)); | ||
| } | ||
| } | ||
|
|
||
| const auto outputs = mlms->infer(inputs, helper.getExtra()); | ||
|
|
||
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
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like maybe your editor got a little aggressive in its formatting opinions, here and in other files. Can you put back the header ordering? It actually is intentional the way it is, with the associated header coming first in its own group (helps ensure the header is standalone).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
undid the bad linting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one should go back up as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done