-
Notifications
You must be signed in to change notification settings - Fork 22
refactor: Refactor string input checks #104
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
Merged
Changes from all commits
Commits
Show all changes
4 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// Copyright 2020-2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
// Copyright 2020-2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
// | ||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions | ||
|
@@ -567,7 +567,6 @@ SetStringInputTensor( | |
cudaStream_t stream, const char* host_policy_name) | ||
{ | ||
bool cuda_copy = false; | ||
size_t element_idx = 0; | ||
|
||
// For string data type, we always need to have the data on CPU so | ||
// that we can read string length and construct the string | ||
|
@@ -582,8 +581,7 @@ SetStringInputTensor( | |
&contiguous_buffer, stream, &cuda_copy); | ||
if (err != nullptr) { | ||
RESPOND_AND_SET_NULL_IF_ERROR(response, err); | ||
FillStringTensor( | ||
tensor, tensor_offset + element_idx, request_element_cnt - element_idx); | ||
FillStringTensor(tensor, tensor_offset, request_element_cnt); | ||
free(contiguous_buffer); | ||
return cuda_copy; | ||
} | ||
|
@@ -595,68 +593,21 @@ SetStringInputTensor( | |
} | ||
#endif // TRITON_ENABLE_GPU | ||
|
||
// Parse content and assign to 'tensor'. Each string in 'content' | ||
// is a 4-byte length followed by the string itself with no | ||
// null-terminator. | ||
while (content_byte_size >= sizeof(uint32_t)) { | ||
if (element_idx >= request_element_cnt) { | ||
RESPOND_AND_SET_NULL_IF_ERROR( | ||
response, | ||
TRITONSERVER_ErrorNew( | ||
TRITONSERVER_ERROR_INVALID_ARG, | ||
std::string( | ||
"unexpected number of string elements " + | ||
std::to_string(element_idx + 1) + " for inference input '" + | ||
name + "', expecting " + std::to_string(request_element_cnt)) | ||
.c_str())); | ||
FillStringTensor( | ||
tensor, tensor_offset + element_idx, | ||
request_element_cnt - element_idx); | ||
free(contiguous_buffer); | ||
return cuda_copy; | ||
} | ||
|
||
const uint32_t len = *(reinterpret_cast<const uint32_t*>(content)); | ||
content += sizeof(uint32_t); | ||
content_byte_size -= sizeof(uint32_t); | ||
|
||
if (content_byte_size < len) { | ||
RESPOND_AND_SET_NULL_IF_ERROR( | ||
response, | ||
TRITONSERVER_ErrorNew( | ||
TRITONSERVER_ERROR_INVALID_ARG, | ||
std::string( | ||
"incomplete string data for inference input '" + | ||
std::string(name) + "', expecting string of length " + | ||
std::to_string(len) + " but only " + | ||
std::to_string(content_byte_size) + " bytes available") | ||
.c_str())); | ||
FillStringTensor( | ||
tensor, tensor_offset + element_idx, | ||
request_element_cnt - element_idx); | ||
free(contiguous_buffer); | ||
return cuda_copy; | ||
} | ||
std::vector<std::pair<const char*, const uint32_t>> str_list; | ||
err = ValidateStringBuffer( | ||
content, content_byte_size, request_element_cnt, name, &str_list); | ||
// Set string values. | ||
for (size_t element_idx = 0; element_idx < str_list.size(); ++element_idx) { | ||
const auto& [addr, len] = str_list[element_idx]; | ||
TRITONTF_TensorSetString(tensor, tensor_offset + element_idx, addr, len); | ||
} | ||
|
||
TRITONTF_TensorSetString(tensor, tensor_offset + element_idx, content, len); | ||
content += len; | ||
content_byte_size -= len; | ||
element_idx++; | ||
} | ||
|
||
if ((*response != nullptr) && (element_idx != request_element_cnt)) { | ||
RESPOND_AND_SET_NULL_IF_ERROR( | ||
tanmayv25 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
response, TRITONSERVER_ErrorNew( | ||
TRITONSERVER_ERROR_INTERNAL, | ||
std::string( | ||
"expected " + std::to_string(request_element_cnt) + | ||
" strings for inference input '" + name + "', got " + | ||
std::to_string(element_idx)) | ||
.c_str())); | ||
size_t element_cnt = str_list.size(); | ||
if (err != nullptr) { | ||
RESPOND_AND_SET_NULL_IF_ERROR(response, err); | ||
FillStringTensor( | ||
tensor, tensor_offset + element_idx, request_element_cnt - element_idx); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same question as above. |
||
tensor, tensor_offset + element_cnt, request_element_cnt - element_cnt); | ||
} | ||
|
||
free(contiguous_buffer); | ||
return cuda_copy; | ||
} | ||
|
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.