-
Notifications
You must be signed in to change notification settings - Fork 208
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
Grounding dino requirements #6263
base: 1.5_maintenance
Are you sure you want to change the base?
Grounding dino requirements #6263
Conversation
@johnhaddon Could you have a look at this PR, it would be useful to deploy grounding dino through GafferML at IE. |
This can be useful for models like Grounding Dino that requires the onnx runtime extension for the BERT tokenizer.
8c1d9db
to
260f7f3
Compare
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.
Thanks Lucien! Would be great to get your dino stuff deployed at IE, and these seem like worthwhile features in general. I've commented inline as usual...
Cheers...
John
src/GafferML/Tensor.cpp
Outdated
@@ -213,6 +216,16 @@ Tensor::Tensor( const IECore::ConstDataPtr &data, std::vector<int64_t> shape ) | |||
std::copy( typedData->readable().begin(), typedData->readable().end(), value.GetTensorMutableData<bool>() ); | |||
m_state = new State{ std::move( value ), nullptr }; | |||
} | |||
else if constexpr( std::is_same_v<DataType, StringVectorData> ) | |||
{ | |||
// Special case for the vector of std::string fiasco. |
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.
The "vector of bool fiasco" is a specific problem with vector<bool>
that doesn't apply to strings. It looks like we do need a special case for strings, but not because of any underlying problems with vector<string>
.
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.
addressed here c7db62d
src/GafferML/Tensor.cpp
Outdated
Ort::Value value = Ort::Value::CreateTensor( | ||
allocator, shape.data(), shape.size(), ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING | ||
); | ||
std::copy( typedData->readable().begin(), typedData->readable().end(), value.GetTensorMutableData<std::string>() ); |
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.
I'm really surprised this works. As far as I can see, the underlying ONNX C library represents strings as const char *
, and there's no guarantee that std::string
has the same layout (although it is possible to implement it so). Even if it did have the same layout, I think we've leaked the strings here, because the destructors for the copies will never get called (there aren't std::string
objects in the C library).
I think we really need to build a vector<const char *
> here and pass it to value.FillStringTensor()
instead. Does that make sense?
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.
Yes that makes sense but this comment is very confusing https://github.com/microsoft/onnxruntime/blob/main/include/onnxruntime/core/session/onnxruntime_c_api.h#L186
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.
addressed here c7db62d
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.
Yes that makes sense but this comment is very confusing
Oh, that is confusing! Looking at the source it looks like the C library is actually implemented in C++ behind the scenes, so it's plausible that the tensor is backed by std::string
, which would also explain how this was working before. I'd prefer not to depend on that though and go through the "proper" API as you've done now - thanks for the update.
@@ -125,6 +125,9 @@ void dispatchTensorData( const Ort::Value &value, F &&functor ) | |||
case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64 : | |||
functor( value.GetTensorData<int64_t>() ); | |||
break; | |||
case ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING : | |||
functor( value.GetTensorData<std::string>() ); |
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.
I'm surprised that this works - as I understand it, there are const char *
elements in the tensor, so casting them to std::string
will likely blow up. Would be good to have some unit tests to demonstrate this all working. We also need to update tensorGetItem()
in GafferMLModule.cpp
to handle strings, which will help with the testing.
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.
I would need a bit more information here because it looks to me that the generic functor wont work here, right?
We need something a bit more specific to reconstruct std::string from char*, right?
Or do we convert into some kind of CharVectorData
?
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.
Ugh, yeah, this is messy. The ONNX docs do say we can't do it this way though - GetTensorData calls through to the C GetTensorMutableData function where the docs say this :
- \param[in] value A tensor type (string tensors are not supported)
We don't really want to convert to another data type either though - for most of the uses of dispatchTensorData()
it would be wasteful to make a copy. Even if we are getting lucky here and getting a pointer to real std::strings
, Tensor::isEqualTo()
and Tensor::memoryUsage()
look broken since they're assuming they're looking at POD types, and won't handle the fact that the string contains a pointer. Since 50% of the clients need a special case anyway, we could argue that dispatchTensorData()
should throw for string data, forcing clients to think carefully and implement a special case.
All this just convinces me further of the need for really strong testing of the string tensors .
Thanks for the update Lucien, and for pointing out the mysterious |
Checklist