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"""Extending CheckpointReader for TensorFlow.""" 16from __future__ import absolute_import 17from __future__ import division 18from __future__ import print_function 19 20from tensorflow.python.framework import dtypes 21from tensorflow.python.framework import errors_impl 22from tensorflow.python.util import compat 23from tensorflow.python.util._pywrap_checkpoint_reader import CheckpointReader 24from tensorflow.python.util.tf_export import tf_export 25 26 27def error_translator(e): 28 """Translate the tensor_slice_reader.cc errors.""" 29 # TODO(b/143319754): Remove the RuntimeError casting logic once we resolve the 30 # issue with throwing python exceptions from C++. 31 error_message = str(e) 32 if 'not found in checkpoint' in error_message or ( 33 'Failed to find any ' 34 'matching files for') in error_message: 35 raise errors_impl.NotFoundError(None, None, error_message) 36 elif 'Sliced checkpoints are not supported' in error_message or ( 37 'Data type ' 38 'not ' 39 'supported') in error_message: 40 raise errors_impl.UnimplementedError(None, None, error_message) 41 elif 'Failed to get matching files on' in error_message: 42 raise errors_impl.InvalidArgumentError(None, None, error_message) 43 elif 'Unable to open table file' in error_message: 44 raise errors_impl.DataLossError(None, None, error_message) 45 elif 'Failed to find the saved tensor slices' in error_message: 46 raise errors_impl.InternalError(None, None, error_message) 47 else: 48 raise errors_impl.OpError(None, None, error_message, errors_impl.UNKNOWN) 49 50 51def get_variable_to_dtype_map(self): 52 return { 53 name: dtypes.DType(type_enum) 54 for name, type_enum in self._GetVariableToDataTypeMap().items() # pylint: disable=protected-access 55 } 56 57CheckpointReader.get_variable_to_dtype_map = get_variable_to_dtype_map 58 59 60def has_tensor(self, tensor_str): 61 return self._HasTensor(compat.as_bytes(tensor_str)) # pylint: disable=protected-access 62 63CheckpointReader.has_tensor = has_tensor 64 65 66def get_tensor(self, tensor_str): 67 """Get the tensor from the Checkpoint object.""" 68 try: 69 return CheckpointReader.CheckpointReader_GetTensor( 70 self, compat.as_bytes(tensor_str)) 71 # TODO(b/143319754): Remove the RuntimeError casting logic once we resolve the 72 # issue with throwing python exceptions from C++. 73 except RuntimeError as e: 74 error_translator(e) 75 76 77CheckpointReader.get_tensor = get_tensor 78 79 80# Disable invalid name to keep backwards compatibility with that function. 81# It was previously exported from py_checkpoint_reader.i which did not conform 82# to pylint checks. 83# pylint: disable=invalid-name 84@tf_export(v1=['train.NewCheckpointReader']) 85def NewCheckpointReader(filepattern): 86 """A function that returns a CheckPointReader. 87 88 Args: 89 filepattern: The filename. 90 91 Returns: 92 A CheckpointReader object. 93 """ 94 try: 95 return CheckpointReader(compat.as_bytes(filepattern)) 96 # TODO(b/143319754): Remove the RuntimeError casting logic once we resolve the 97 # issue with throwing python exceptions from C++. 98 except RuntimeError as e: 99 error_translator(e) 100