Populates TensorProto with tensor from protobuf file (#4535)

* Expose load tensor proto from protobuf file function

* Add comment

* Remove use of fstream and use parsefromzerocopystream

* Close file descriptor after finish parsing it

* Close input stream too

* Set Close on delete only, no need to close file descriptor

* Revert "Set Close on delete only, no need to close file descriptor"

This reverts commit 5ba6e3c31b.

* Revert "Close input stream too"

This reverts commit 4564776733.

* Revert "Close file descriptor after finish parsing it"

This reverts commit 846e550c4f.

* Revert "Remove use of fstream and use parsefromzerocopystream"

This reverts commit 25a3117183.
This commit is contained in:
Ryan Lai 2020-07-21 17:50:15 -07:00 committed by GitHub
parent 603f2d1138
commit 0b4659c3fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 10 deletions

View file

@ -11,11 +11,6 @@
#include "core/common/logging/sinks/clog_sink.h"
#include "protobufHelpers.h"
#pragma warning(push)
#pragma warning(disable : 4100)
#include "onnx/onnx-ml.pb.h"
#pragma warning(pop)
#include <fstream>
using namespace wss;
@ -44,8 +39,8 @@ void FdClose(int fd) {
}
}
// Copy and pasted from LOTUS as is. temporary code to load tensors from protobufs
bool LoadTensorFromPb(onnx::TensorProto& tensor, std::wstring filePath) {
// Load Onnx TensorProto from Protobuf File
bool ProtobufHelpers::LoadOnnxTensorFromProtobufFile(onnx::TensorProto& tensor, std::wstring filePath) {
// setup a string converter
using convert_type = std::codecvt_utf8<wchar_t>;
std::wstring_convert<convert_type, wchar_t> converter;
@ -124,7 +119,7 @@ ITensor ProtobufHelpers::LoadTensorFromProtobufFile(
bool isFp16) {
// load from the file path into the onnx format
onnx::TensorProto tensorProto;
if (LoadTensorFromPb(tensorProto, filePath)) {
if (LoadOnnxTensorFromProtobufFile(tensorProto, filePath)) {
std::vector<int64_t> tensorShape = std::vector<int64_t>(tensorProto.dims().begin(), tensorProto.dims().end());
int64_t initialValue = 1;
int64_t elementCount = std::accumulate(tensorShape.begin(), tensorShape.end(), initialValue, std::multiplies<int64_t>());
@ -156,7 +151,7 @@ TensorFloat16Bit ProtobufHelpers::LoadTensorFloat16FromProtobufFile(
const std::wstring& filePath) {
// load from the file path into the onnx format
onnx::TensorProto tensorProto;
if (LoadTensorFromPb(tensorProto, filePath)) {
if (LoadOnnxTensorFromProtobufFile(tensorProto, filePath)) {
if (tensorProto.has_data_type()) {
if(onnx::TensorProto::DataType::TensorProto_DataType_FLOAT16 != tensorProto.data_type()) {
throw winrt::hresult_invalid_argument(L"TensorProto datatype isn't of type Float16.");

View file

@ -5,7 +5,10 @@
#include "std.h"
#include "winrt_headers.h"
#pragma warning(push)
#pragma warning(disable : 4100)
#include "onnx/onnx-ml.pb.h"
#pragma warning(pop)
namespace ProtobufHelpers
{
// LoadTensorFromProtobufFile take a path to a FP32 data file and loads it into a 32bit array or
@ -18,4 +21,7 @@ namespace ProtobufHelpers
winml::TensorKind kind,
const std::vector<int64_t>& shape,
uint32_t num_elements = 1);
// Populates TensorProto with tensor from protobuf file
bool LoadOnnxTensorFromProtobufFile(onnx::TensorProto& tensor, std::wstring filePath);
}