Implement conversion from ORT String to WinML Tensor String (#6097)

* Implement conversion from ort string to winml string

* NIT:comment
This commit is contained in:
Ryan Lai 2020-12-10 17:47:50 -08:00 committed by GitHub
parent 8bcb5fd119
commit 1eb146f561
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 3 deletions

View file

@ -1,5 +1,6 @@
#include "testPch.h"
#include "ort_value_helper.h"
#include "StringHelpers.h"
using namespace winml;
namespace OrtValueHelpers {
@ -13,6 +14,56 @@ winml::ITensor CreateTensorFromShape(std::vector<int64_t>& shape)
return tensor;
}
static int64_t ShapeSize(const int64_t* shape, size_t count) {
// for each dim
int64_t size = 1;
for (size_t i = 0; i < count; i++) {
// find out it's total size
size *= shape[i];
// make sure there are no invalid dimensions (-1 or any invalid shape)
THROW_HR_IF(E_INVALIDARG, shape[i] <= 0);
}
return size;
}
winml::ITensor CreateStringTensor(Ort::Value& val) {
size_t dimensionCount = 0;
WINML_EXPECT_NO_THROW(dimensionCount = val.GetTensorTypeAndShapeInfo().GetDimensionsCount());
std::vector<int64_t> shape;
if (dimensionCount > 0) {
WINML_EXPECT_NO_THROW(shape = val.GetTensorTypeAndShapeInfo().GetShape());
}
auto length = ShapeSize(shape.data(), shape.size());
// make a big buffer to hold all the string data
size_t bufferLength = 0;
WINML_EXPECT_NO_THROW(bufferLength = val.GetStringTensorDataLength());
std::vector<winrt::hstring> strings;
std::unique_ptr<uint8_t[]> buffer(new uint8_t[bufferLength]);
std::vector<size_t> offsets(static_cast<size_t>(length));
WINML_EXPECT_NO_THROW(val.GetStringTensorContent(buffer.get(), bufferLength, offsets.data(), offsets.size()));
// now go build all the strings
for (auto i = 0; i < length; ++i) {
size_t strLength = 0;
// are we on the last one?
if (i == (length - 1)) {
strLength = bufferLength - offsets[i];
} else {
strLength = offsets[i+1] - offsets[i];
}
auto strView = std::string_view(reinterpret_cast<const char*>(buffer.get() + offsets[i]), strLength);
strings.push_back(_winml::Strings::HStringFromUTF8(strView.data(), strLength));
}
TensorString tensor = nullptr;
WINML_EXPECT_NO_THROW(tensor = TensorString::CreateFromShapeArrayAndDataArray(shape, strings));
return tensor;
}
// This function takes in an Ort::Value and returns a copy of winml::ITensor
// TODO: String types still need to be implemented.
winml::ITensor LoadTensorFromOrtValue(Ort::Value& val) {
@ -39,6 +90,9 @@ winml::ITensor LoadTensorFromOrtValue(Ort::Value& val) {
tensor = CreateTensorFromShape<ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_INT16>(shape);
break;
}
case (ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING): {
return CreateStringTensor(val);
}
case (ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32): {
tensor = CreateTensorFromShape<ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32>(shape);
break;

View file

@ -7,9 +7,7 @@ static const std::string disabledGpuTestDefaultReason = "Model not working on GP
// {"model test name", "reason for why it is happening and bug filed for it."}
std::unordered_map<std::string, std::string> disabledTests(
{// Onnx zoo models
{"test_bidaf_opset9", "Bug 31011100: Processing string tensors need to be implemented in WinML model tests https://microsoft.visualstudio.com/OS/_workitems/edit/31011100"},
{
// Tier 2 models
{"coreml_VGG16_ImageNet_opset8", "Bug 31011100: Processing string tensors need to be implemented in WinML model tests https://microsoft.visualstudio.com/OS/_workitems/edit/31011100"},
{"coreml_VGG16_ImageNet_opset9", "Bug 31011100: Processing string tensors need to be implemented in WinML model tests https://microsoft.visualstudio.com/OS/_workitems/edit/31011100"},