Skip to content

Commit 15c74ff

Browse files
committed
support fractional num_updates
1 parent 5f13499 commit 15c74ff

4 files changed

Lines changed: 33 additions & 10 deletions

File tree

alf/algorithms/algorithm.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,8 +1517,8 @@ def _replay():
15171517
"No mini_batch_length is specified for off-policy training"
15181518
)
15191519
experience, batch_info = self._replay_buffer.get_batch(
1520-
batch_size=(mini_batch_size *
1521-
config.num_updates_per_train_iter),
1520+
batch_size=int(mini_batch_size *
1521+
config.num_updates_per_train_iter),
15221522
batch_length=config.mini_batch_length)
15231523
num_updates = 1
15241524
return experience, batch_info, num_updates, mini_batch_size
@@ -1562,8 +1562,8 @@ def _replay():
15621562

15631563
with record_time("time/offline_replay"):
15641564
offline_experience, offline_batch_info = self._offline_replay_buffer.get_batch(
1565-
batch_size=(mini_batch_size *
1566-
config.num_updates_per_train_iter),
1565+
batch_size=int(mini_batch_size *
1566+
config.num_updates_per_train_iter),
15671567
batch_length=config.mini_batch_length)
15681568
# train hybrid
15691569
with record_time("time/offline_train"):
@@ -1641,14 +1641,26 @@ def _train_experience(self,
16411641
torch.cuda.empty_cache()
16421642

16431643
indices = None
1644+
# In the case where ``num_updates<1``, we will skip some mini-batches (only
1645+
# applicable if ``mini_batch_size<batch_size``).
1646+
training_fraction = min(1, num_updates)
1647+
training_every_n_batches = int(1. / training_fraction)
1648+
num_updates = int(max(1, np.ceil(num_updates)))
1649+
batches = 0
16441650
for u in range(num_updates):
16451651
if mini_batch_size < batch_size:
16461652
indices = torch.randperm(batch_size,
16471653
device=experience.step_type.device)
16481654
for b in range(0, batch_size, mini_batch_size):
1655+
if (b % (training_every_n_batches * mini_batch_size)) != 0:
1656+
continue
16491657

1650-
is_last_mini_batch = (u == num_updates - 1
1651-
and b + mini_batch_size >= batch_size)
1658+
batches += 1
1659+
1660+
is_last_mini_batch = (
1661+
u == num_updates - 1
1662+
and b + mini_batch_size * training_every_n_batches
1663+
>= batch_size)
16521664
do_summary = alf.summary.should_record_summaries() and (
16531665
is_last_mini_batch or update_counter_every_mini_batch)
16541666

@@ -1674,7 +1686,7 @@ def _train_experience(self,
16741686
# These are no longer used, release them to reduce memory usage.
16751687
del exp, train_info, loss_info, params
16761688

1677-
train_steps = batch_size * mini_batch_length * num_updates
1689+
train_steps = mini_batch_length * batches * mini_batch_size
16781690
return train_steps
16791691

16801692
def _prepare_experience_data(self,

alf/algorithms/config.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,15 @@ def __init__(self,
280280
initial_collect_steps (int): if positive, number of steps each single
281281
environment steps before perform first update. Only used
282282
by ``OffPolicyAlgorithm``.
283-
num_updates_per_train_iter (int): number of optimization steps for
284-
one iteration. Only used by ``OffPolicyAlgorithm``.
283+
num_updates_per_train_iter (float): number of optimization steps for
284+
one iteration. Only used by ``OffPolicyAlgorithm``. If
285+
``config.whole_replay_buffer_training`` is False or doing offline training,
286+
this is essentially a multiplier applied to ``config.mini_batch_size``.
287+
Otherwise if this value is greater than 1, ``np.ceil(num_updates_per_train_iter)``
288+
updates is performed; if this value is less than 1, it represents
289+
the fraction of mini-batches from the entire buffer to train on. The latter
290+
case is helpful if we want to shorten the training time for each
291+
iteration.
285292
mini_batch_size (int): number of sequences for each minibatch. If None,
286293
it's set to the replayer's ``batch_size``. Only used by
287294
``OffPolicyAlgorithm``.

alf/algorithms/distributed_off_policy_algorithm.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from typing import Callable
1717
import time
1818
import io
19+
import numpy as np
1920
import queue
2021
import random
2122
import threading
@@ -553,7 +554,8 @@ def _train_iter_off_policy(self):
553554
time.sleep(0.01)
554555

555556
steps = super()._train_iter_off_policy()
556-
self._total_updates += self._config.num_updates_per_train_iter
557+
self._total_updates += int(
558+
np.ceil(self._config.num_updates_per_train_iter))
557559

558560
with record_time("time/trainer_send_params_to_unroller"):
559561
if (self._total_updates %

alf/utils/summary_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ def summarize_variables(name_and_params, with_histogram=True):
119119
with_histogram (bool): If True, generate histogram.
120120
"""
121121
for var_name, var in name_and_params:
122+
if var.grad is None:
123+
continue
122124
var_values = var
123125
if with_histogram and torch.all(torch.isfinite(var_values)):
124126
# Need to make sure all values are finite to avoid the histogram range

0 commit comments

Comments
 (0)