本文整理匯總了Python中horovod.tensorflow.local_rank方法的典型用法代碼示例。如果您正苦於以下問題:Python tensorflow.local_rank方法的具體用法?Python tensorflow.local_rank怎麽用?Python tensorflow.local_rank使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類horovod.tensorflow
的用法示例。
在下文中一共展示了tensorflow.local_rank方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _get_runconfig
# 需要導入模塊: from horovod import tensorflow [as 別名]
# 或者: from horovod.tensorflow import local_rank [as 別名]
def _get_runconfig(is_distributed=DISTRIBUTED, save_checkpoints_steps=None):
if is_distributed:
# Horovod: pin GPU to be used to process local rank (one GPU per process)
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.gpu_options.visible_device_list = str(hvd.local_rank())
return tf.estimator.RunConfig(
save_checkpoints_steps=save_checkpoints_steps,
save_checkpoints_secs=None,
session_config=config,
log_step_count_steps=100,
)
else:
return tf.estimator.RunConfig(
save_checkpoints_steps=save_checkpoints_steps,
save_checkpoints_secs=None,
log_step_count_steps=100,
)
示例2: _get_runconfig
# 需要導入模塊: from horovod import tensorflow [as 別名]
# 或者: from horovod.tensorflow import local_rank [as 別名]
def _get_runconfig(is_distributed=defaults.DISTRIBUTED, save_checkpoints_steps=None):
if is_distributed:
# Horovod: pin GPU to be used to process local rank (one GPU per process)
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.gpu_options.visible_device_list = str(hvd.local_rank())
return tf.estimator.RunConfig(
save_checkpoints_steps=save_checkpoints_steps,
save_checkpoints_secs=None,
session_config=config,
log_step_count_steps=100,
)
else:
return tf.estimator.RunConfig(
save_checkpoints_steps=save_checkpoints_steps,
save_checkpoints_secs=None,
log_step_count_steps=100,
)
示例3: __init__
# 需要導入模塊: from horovod import tensorflow [as 別名]
# 或者: from horovod.tensorflow import local_rank [as 別名]
def __init__(self, average=True):
"""
Args:
average (bool): whether to average or sum the gradients across processes.
"""
import byteps.tensorflow as bps
self.hvd = bps # BytePS has the same interface as Horovod
self.hvd.allreduce = bps.push_pull # https://github.com/bytedance/byteps/issues/8
assert os.environ.get("DMLC_ROLE", None) == "worker"
assert "DMLC_WORKER_ID" in os.environ and "DMLC_NUM_WORKER" in os.environ
bps.init()
self.is_chief = bps.rank() == 0
self._local_rank = bps.local_rank()
self._rank = bps.rank()
self._average = average
self._compression = None
self._has_compression = False
logger.info("[BytePSTrainer] local rank={}".format(self._local_rank))
SingleCostTrainer.__init__(self)
示例4: train
# 需要導入模塊: from horovod import tensorflow [as 別名]
# 或者: from horovod.tensorflow import local_rank [as 別名]
def train(self, dataset, total_batches=-1):
""" Update the model in 1 epoch """
train_step = self.train_step
if self.hparams.enable_tf_function:
logging.info("please be patient, enable tf.function, it takes time ...")
train_step = tf.function(train_step, input_signature=self.sample_signature)
for batch, samples in enumerate(dataset.take(total_batches)):
# train 1 step
samples = self.model.prepare_samples(samples)
loss, metrics = train_step(samples)
# Horovod: broadcast initial variable states from rank 0 to all other processes.
# This is necessary to ensure consistent initialization of all workers when
# training is started with random weights or restored from a checkpoint.
#
# Note: broadcast should be done after the first gradient step to ensure optimizer
# initialization.
if batch == 0:
hvd.broadcast_variables(self.model.trainable_variables, root_rank=0)
hvd.broadcast_variables(self.optimizer.variables(), root_rank=0)
if batch % self.hparams.log_interval == 0 and hvd.local_rank() == 0:
logging.info(self.metric_checker(loss, metrics))
self.model.reset_metrics()
示例5: evaluate
# 需要導入模塊: from horovod import tensorflow [as 別名]
# 或者: from horovod.tensorflow import local_rank [as 別名]
def evaluate(self, dataset, epoch=0):
""" evaluate the model """
loss_metric = tf.keras.metrics.Mean(name="AverageLoss")
loss, metrics = None, None
evaluate_step = self.evaluate_step
if self.hparams.enable_tf_function:
logging.info("please be patient, enable tf.function, it takes time ...")
evaluate_step = tf.function(evaluate_step, input_signature=self.sample_signature)
self.model.reset_metrics()
for batch, samples in enumerate(dataset):
samples = self.model.prepare_samples(samples)
loss, metrics = evaluate_step(samples)
if batch % self.hparams.log_interval == 0 and hvd.local_rank() == 0:
logging.info(self.metric_checker(loss, metrics, -2))
loss_metric.update_state(loss)
if hvd.local_rank() == 0:
logging.info(self.metric_checker(loss_metric.result(), metrics, evaluate_epoch=epoch))
self.model.reset_metrics()
return loss_metric.result()
示例6: _train
# 需要導入模塊: from horovod import tensorflow [as 別名]
# 或者: from horovod.tensorflow import local_rank [as 別名]
def _train(model: Model,
data: TrainingData,
checkpoint: Union[str, None],
parameter_checkpoint: Union[str, None],
save_start: bool,
train_params: trainer.TrainParams,
evaluators: List[Evaluator],
out: ModelDir,
notes=None,
dry_run=False,
start_eval=False):
print('Horovod size: ', hvd.size())
print('Horovod rank: ', hvd.rank())
print('Horovod local rank: ', hvd.local_rank())
if train_params.async_encoding:
_train_async(model, data, checkpoint, parameter_checkpoint, save_start, train_params,
evaluators, out, notes, dry_run, start_eval)
return
else:
raise NotImplementedError('Syncronous training with Horovod not supported yet')
示例7: _setup_graph
# 需要導入模塊: from horovod import tensorflow [as 別名]
# 或者: from horovod.tensorflow import local_rank [as 別名]
def _setup_graph(self):
num_gpu = cfg.TRAIN.NUM_GPUS
if cfg.TRAINER == 'replicated':
# TF bug in version 1.11, 1.12: https://github.com/tensorflow/tensorflow/issues/22750
buggy_tf = get_tf_version_tuple() in [(1, 11), (1, 12)]
# Use two predictor threads per GPU to get better throughput
self.num_predictor = num_gpu if buggy_tf else num_gpu * 2
self.predictors = [self._build_predictor(k % num_gpu) for k in range(self.num_predictor)]
self.dataflows = [get_eval_dataflow(self._eval_dataset,
shard=k, num_shards=self.num_predictor)
for k in range(self.num_predictor)]
else:
# Only eval on the first machine,
# Because evaluation assumes that all horovod workers share the filesystem.
# Alternatively, can eval on all ranks and use allgather, but allgather sometimes hangs
self._horovod_run_eval = hvd.rank() == hvd.local_rank()
if self._horovod_run_eval:
self.predictor = self._build_predictor(0)
self.dataflow = get_eval_dataflow(self._eval_dataset,
shard=hvd.local_rank(), num_shards=hvd.local_size())
self.barrier = hvd.allreduce(tf.random_normal(shape=[1]))
示例8: __init__
# 需要導入模塊: from horovod import tensorflow [as 別名]
# 或者: from horovod.tensorflow import local_rank [as 別名]
def __init__(self, average=True, compression=None):
"""
Args:
average (bool): whether to average or sum the gradients across processes.
compression: `hvd.Compression.fp16` or `hvd.Compression.none`
"""
if 'pyarrow' in sys.modules:
logger.warn("Horovod and pyarrow may conflict due to pyarrow bugs.")
# lazy import
import horovod.tensorflow as hvd
import horovod
hvd_version = tuple(map(int, horovod.__version__.split('.')[:3]))
self.hvd = hvd
hvd.init()
self.is_chief = hvd.rank() == 0
self._local_rank = hvd.local_rank()
self._rank = hvd.rank()
self._average = average
self._compression = compression
self._has_compression = hvd_version >= (0, 15, 0)
logger.info("[HorovodTrainer] local rank={}".format(self._local_rank))
super(HorovodTrainer, self).__init__()
self.BROADCAST_EVERY_EPOCH = True
示例9: setup_horovod_execution
# 需要導入模塊: from horovod import tensorflow [as 別名]
# 或者: from horovod.tensorflow import local_rank [as 別名]
def setup_horovod_execution(self):
"""
Sets up Horovod.
"""
# Check again to avoid import if unset which will crash if horovod is not installed.
if get_distributed_backend() == "horovod":
import horovod.tensorflow as hvd
self.logger.info("Setting up Horovod execution.")
hvd.init()
config = tf.ConfigProto()
config.gpu_options.visible_device_list = str(hvd.local_rank())
示例10: tensorflow_session
# 需要導入模塊: from horovod import tensorflow [as 別名]
# 或者: from horovod.tensorflow import local_rank [as 別名]
def tensorflow_session():
# Init session and params
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
# Pin GPU to local rank (one GPU per process)
config.gpu_options.visible_device_list = str(hvd.local_rank())
sess = tf.Session(config=config)
return sess
示例11: local_rank
# 需要導入模塊: from horovod import tensorflow [as 別名]
# 或者: from horovod.tensorflow import local_rank [as 別名]
def local_rank(cls, *args):
"""Get the rank of current worker at the current node."""
try:
return mgw.local_rank(*args)
except NameError:
raise NameError('module <mgw> not imported')
示例12: initialize_devices
# 需要導入模塊: from horovod import tensorflow [as 別名]
# 或者: from horovod.tensorflow import local_rank [as 別名]
def initialize_devices(visible_gpu_idx=None):
""" initialize hvd devices, should be called firstly """
if visible_gpu_idx is not None:
warnings.warn("we can not set the visible gpu idx like this")
hvd.init()
gpus = tf.config.experimental.list_physical_devices("GPU")
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
if gpus:
tf.config.experimental.set_visible_devices(gpus[hvd.local_rank()], "GPU")
示例13: test_horovod_allreduce_gpu
# 需要導入模塊: from horovod import tensorflow [as 別名]
# 或者: from horovod.tensorflow import local_rank [as 別名]
def test_horovod_allreduce_gpu(self):
"""Test that the allreduce works on GPUs.
This test will crash badly if used with an MPI implementation that does
not support GPU memory transfers directly, as it will call MPI_Send on
a GPU data pointer."""
# Only do this test if there are GPUs available.
if not tf.test.is_gpu_available(cuda_only=True):
return
hvd.init()
local_rank = hvd.local_rank()
size = hvd.size()
with self.test_session(config=self.config) as session:
dtypes = [tf.int32, tf.int64, tf.float16, tf.float32, tf.float64]
dims = [1, 2, 3]
for dtype, dim in itertools.product(dtypes, dims):
with tf.device("/gpu:%d" % local_rank):
tf.set_random_seed(1234)
tensor = tf.random_uniform(
[17] * dim, -100, 100, dtype=dtype)
summed = hvd.allreduce(tensor, average=False)
multiplied = tensor * size
max_difference = tf.reduce_max(tf.abs(summed - multiplied))
# Threshold for floating point equality depends on number of
# ranks, since we're comparing against precise multiplication.
if size <= 3 or dtype in [tf.int32, tf.int64]:
threshold = 0
elif size < 10:
threshold = 1e-4
elif size < 15:
threshold = 5e-4
else:
return
diff = session.run(max_difference)
self.assertTrue(diff <= threshold,
"hvd.allreduce on GPU produces incorrect results")
示例14: _get_hooks
# 需要導入模塊: from horovod import tensorflow [as 別名]
# 或者: from horovod.tensorflow import local_rank [as 別名]
def _get_hooks(is_distributed=DISTRIBUTED):
logger = logging.getLogger(__name__)
if is_distributed:
bcast_hook = hvd.BroadcastGlobalVariablesHook(0)
logger.info("Rank: {} Cluster Size {}".format(hvd.local_rank(), hvd.size()))
return [bcast_hook]
else:
return []
示例15: as_operator
# 需要導入模塊: from horovod import tensorflow [as 別名]
# 或者: from horovod.tensorflow import local_rank [as 別名]
def as_operator(self):
try:
import horovod.tensorflow as hvd
except ImportError:
raise ImportError('Cannot import Horovod')
self.network.session_config.gpu_options.visible_device_list = str(hvd.local_rank())
hooks = [hvd.BroadcastGlobalVariablesHook(0)]
self.network.add_hooks(hooks)
return self.op.minimize(self.network.fetch_internal_tensor(self.loss))