-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Increase protobuf size limit when parsing pb files (onnx#190)
* Increase protobuf size limit when parsing pb files * clang-format * Separate out the python part from proto_utils
- Loading branch information
Showing
3 changed files
with
43 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
|
||
#include <google/protobuf/io/coded_stream.h> | ||
#include <google/protobuf/io/zero_copy_stream_impl_lite.h> | ||
|
||
namespace onnx { | ||
|
||
template <typename Proto> | ||
bool ParseProtoFromBytes(Proto* proto, const char* buffer, size_t length) { | ||
// Total bytes hard limit / warning limit are set to 1GB and 512MB | ||
// respectively. | ||
::google::protobuf::io::CodedInputStream coded_stream( | ||
new google::protobuf::io::ArrayInputStream(buffer, length)); | ||
coded_stream.SetTotalBytesLimit(1024LL << 20, 512LL << 20); | ||
return proto->ParseFromCodedStream(&coded_stream); | ||
} | ||
} // namespace onnx |
This file contains 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
|
||
#include <pybind11/pybind11.h> | ||
#include "onnx/proto_utils.h" | ||
|
||
namespace onnx { | ||
namespace py = pybind11; | ||
|
||
template <typename Proto> | ||
bool ParseProtoFromPyBytes(Proto* proto, const py::bytes& bytes) { | ||
// Get the buffer from Python bytes object | ||
char* buffer = nullptr; | ||
Py_ssize_t length; | ||
PyBytes_AsStringAndSize(bytes.ptr(), &buffer, &length); | ||
|
||
ParseProtoFromBytes(proto, buffer, length); | ||
} | ||
} // namespace onnx |