Prevent loading saved models where constant nodes have no tensor value.

Also reorder fuzz generated test cases following f760f88b42

PiperOrigin-RevId: 308339007
Change-Id: I11d825203964cf3397846c57fd4a6f458e8536f3
This commit is contained in:
Mihai Maruseac 2020-04-24 15:32:06 -07:00 committed by TensorFlower Gardener
parent 3b336d3173
commit fcfef19563
4 changed files with 22 additions and 3 deletions

View File

@ -70,6 +70,7 @@ uint64 GetLatencyMicroseconds(const uint64 start_microseconds) {
}
// Ensure that constant tensors loaded from the saved model have valid shape.
// Also ensure that constant nodes have a value assigned to them.
// TODO(b/154763635): this is temporary and will be replaced with a better audit
static Status ValidateSavedTensors(const GraphDef& graph_def) {
for (const auto& node : graph_def.node()) {
@ -85,6 +86,10 @@ static Status ValidateSavedTensors(const GraphDef& graph_def) {
node_shape.num_elements(), " elements");
}
}
} else if (node.op() == "Const") {
return errors::FailedPrecondition(
"Saved model contains node \"", node.name(),
"\" which is a constant tensor but no value has been provided");
}
}
return Status::OK();

View File

@ -40,8 +40,10 @@ constexpr char kTestDataInitOpV2[] =
"cc/saved_model/testdata/half_plus_two_v2/00000123";
constexpr char kTestDataV2DebugInfo[] =
"cc/saved_model/testdata/x_plus_y_v2_debuginfo";
constexpr char kTestNegativeShapeFuzzGenerated[] =
"cc/saved_model/testdata/negative_shape/fuzz_generated";
constexpr char kTestFuzzGeneratedNegativeShape[] =
"cc/saved_model/testdata/fuzz_generated/negative_shape";
constexpr char kTestFuzzGeneratedConstWithNoValue[] =
"cc/saved_model/testdata/fuzz_generated/const_with_no_value";
class LoaderTest : public ::testing::Test {
protected:
@ -264,7 +266,19 @@ TEST_F(LoaderTest, NegativeShapeDimension) {
SessionOptions session_options;
const string export_dir = io::JoinPath(testing::TensorFlowSrcRoot(),
kTestNegativeShapeFuzzGenerated);
kTestFuzzGeneratedNegativeShape);
Status st = LoadSavedModel(session_options, run_options, export_dir,
{kSavedModelTagServe}, &bundle);
EXPECT_FALSE(st.ok());
}
TEST_F(LoaderTest, ConstNoValue) {
SavedModelBundle bundle;
RunOptions run_options;
SessionOptions session_options;
const string export_dir = io::JoinPath(testing::TensorFlowSrcRoot(),
kTestFuzzGeneratedConstWithNoValue);
Status st = LoadSavedModel(session_options, run_options, export_dir,
{kSavedModelTagServe}, &bundle);
EXPECT_FALSE(st.ok());