1# Copyright 2019 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"""Utilities that help manage directory path in distributed settings. 16 17In multi-worker training, the need to write a file to distributed file 18location often requires only one copy done by one worker despite many workers 19that are involved in training. The option to only perform saving by chief is 20not feasible for a couple of reasons: 1) Chief and workers may each contain 21a client that runs the same piece of code and it's preferred not to make 22any distinction between the code run by chief and other workers, and 2) 23saving of model or model's related information may require SyncOnRead 24variables to be read, which needs the cooperation of all workers to perform 25all-reduce. 26 27This set of utility is used so that only one copy is written to the needed 28directory, by supplying a temporary write directory path for workers that don't 29need to save, and removing the temporary directory once file writing is done. 30 31Example usage: 32``` 33# Before using a directory to write file to. 34self.log_write_dir = write_dirpath(self.log_dir, get_distribution_strategy()) 35# Now `self.log_write_dir` can be safely used to write file to. 36 37... 38 39# After the file is written to the directory. 40remove_temp_dirpath(self.log_dir, get_distribution_strategy()) 41 42``` 43 44Experimental. API is subject to change. 45""" 46 47from __future__ import absolute_import 48from __future__ import division 49from __future__ import print_function 50 51import os 52from tensorflow.python.distribute import distribution_strategy_context 53from tensorflow.python.lib.io import file_io 54 55 56def _get_base_dirpath(strategy): 57 task_id = strategy.extended._task_id # pylint: disable=protected-access 58 return 'workertemp_' + str(task_id) 59 60 61def _is_temp_dir(dirpath, strategy): 62 return dirpath.endswith(_get_base_dirpath(strategy)) 63 64 65def _get_temp_dir(dirpath, strategy): 66 if _is_temp_dir(dirpath, strategy): 67 temp_dir = dirpath 68 else: 69 temp_dir = os.path.join(dirpath, _get_base_dirpath(strategy)) 70 file_io.recursive_create_dir_v2(temp_dir) 71 return temp_dir 72 73 74def write_dirpath(dirpath, strategy): 75 """Returns the writing dir that should be used to save file distributedly. 76 77 `dirpath` would be created if it doesn't exist. 78 79 Args: 80 dirpath: Original dirpath that would be used without distribution. 81 strategy: The tf.distribute strategy object currently used. 82 83 Returns: 84 The writing dir path that should be used to save with distribution. 85 """ 86 if strategy is None: 87 # Infer strategy from `distribution_strategy_context` if not given. 88 strategy = distribution_strategy_context.get_strategy() 89 if strategy is None: 90 # If strategy is still not available, this is not in distributed training. 91 # Fallback to original dirpath. 92 return dirpath 93 if not strategy.extended._in_multi_worker_mode(): # pylint: disable=protected-access 94 return dirpath 95 if strategy.extended.should_checkpoint: 96 return dirpath 97 # If this worker is not chief and hence should not save file, save it to a 98 # temporary directory to be removed later. 99 return _get_temp_dir(dirpath, strategy) 100 101 102def remove_temp_dirpath(dirpath, strategy): 103 """Removes the temp path after writing is finished. 104 105 Args: 106 dirpath: Original dirpath that would be used without distribution. 107 strategy: The tf.distribute strategy object currently used. 108 """ 109 if strategy is None: 110 # Infer strategy from `distribution_strategy_context` if not given. 111 strategy = distribution_strategy_context.get_strategy() 112 if strategy is None: 113 # If strategy is still not available, this is not in distributed training. 114 # Fallback to no-op. 115 return 116 # TODO(anjalisridhar): Consider removing the check for multi worker mode since 117 # it is redundant when used with the should_checkpoint property. 118 if (strategy.extended._in_multi_worker_mode() and # pylint: disable=protected-access 119 not strategy.extended.should_checkpoint): 120 # If this worker is not chief and hence should not save file, remove 121 # the temporary directory. 122 file_io.delete_recursively(_get_temp_dir(dirpath, strategy)) 123 124 125def write_filepath(filepath, strategy): 126 """Returns the writing file path to be used to save file distributedly. 127 128 Directory to contain `filepath` would be created if it doesn't exist. 129 130 Args: 131 filepath: Original filepath that would be used without distribution. 132 strategy: The tf.distribute strategy object currently used. 133 134 Returns: 135 The writing filepath that should be used to save file with distribution. 136 """ 137 dirpath = os.path.dirname(filepath) 138 base = os.path.basename(filepath) 139 return os.path.join(write_dirpath(dirpath, strategy), base) 140 141 142def remove_temp_dir_with_filepath(filepath, strategy): 143 """Removes the temp path for file after writing is finished. 144 145 Args: 146 filepath: Original filepath that would be used without distribution. 147 strategy: The tf.distribute strategy object currently used. 148 """ 149 remove_temp_dirpath(os.path.dirname(filepath), strategy) 150