1# Copyright 2015 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================= 15"""Tests for tensorflow.python.training.saver.py.""" 16 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import print_function 20 21import os 22 23from tensorflow.core.protobuf import saver_pb2 24from tensorflow.python.client import session 25from tensorflow.python.framework import constant_op 26from tensorflow.python.framework import dtypes 27from tensorflow.python.framework import errors_impl 28from tensorflow.python.framework import ops 29from tensorflow.python.ops import variables 30from tensorflow.python.platform import test 31from tensorflow.python.training import saver 32 33 34class SaverLargeVariableTest(test.TestCase): 35 36 # NOTE: This is in a separate file from saver_test.py because the 37 # large allocations do not play well with TSAN, and cause flaky 38 # failures. 39 def testLargeVariable(self): 40 save_path = os.path.join(self.get_temp_dir(), "large_variable") 41 with session.Session("", graph=ops.Graph()) as sess: 42 # Declare a variable that is exactly 2GB. This should fail, 43 # because a serialized checkpoint includes other header 44 # metadata. 45 with ops.device("/cpu:0"): 46 var = variables.Variable( 47 constant_op.constant( 48 False, shape=[2, 1024, 1024, 1024], dtype=dtypes.bool)) 49 save = saver.Saver( 50 { 51 var.op.name: var 52 }, write_version=saver_pb2.SaverDef.V1) 53 var.initializer.run() 54 with self.assertRaisesRegexp(errors_impl.InvalidArgumentError, 55 "Tensor slice is too large to serialize"): 56 save.save(sess, save_path) 57 58 59if __name__ == "__main__": 60 test.main() 61