Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions rsl_rl/algorithms/distillation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(
num_learning_epochs=1,
gradient_length=15,
learning_rate=1e-3,
max_grad_norm=None,
loss_type="mse",
device="cpu",
# Distributed training parameters
Expand Down Expand Up @@ -55,6 +56,7 @@ def __init__(
self.num_learning_epochs = num_learning_epochs
self.gradient_length = gradient_length
self.learning_rate = learning_rate
self.max_grad_norm = max_grad_norm

# initialize the loss function
if loss_type == "mse":
Expand Down Expand Up @@ -127,6 +129,8 @@ def update(self):
loss.backward()
if self.is_multi_gpu:
self.reduce_parameters()
if self.max_grad_norm:
nn.utils.clip_grad_norm_(self.policy.student.parameters(), self.max_grad_norm)
self.optimizer.step()
self.policy.detach_hidden_states()
loss = 0
Expand Down
29 changes: 20 additions & 9 deletions rsl_rl/runners/on_policy_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ def __init__(self, env: VecEnv, train_cfg: dict, log_dir: str | None = None, dev

# initialize algorithm
alg_class = eval(self.alg_cfg.pop("class_name"))
self.alg: PPO | Distillation = alg_class(policy, device=self.device, **self.alg_cfg, multi_gpu_cfg=self.multi_gpu_cfg)
self.alg: PPO | Distillation = alg_class(
policy, device=self.device, **self.alg_cfg, multi_gpu_cfg=self.multi_gpu_cfg
)

# store training configuration
self.num_steps_per_env = self.cfg["num_steps_per_env"]
Expand Down Expand Up @@ -387,8 +389,13 @@ def log(self, locs: dict, width: int = 80, pad: int = 35):
f"""{'Total timesteps:':>{pad}} {self.tot_timesteps}\n"""
f"""{'Iteration time:':>{pad}} {iteration_time:.2f}s\n"""
f"""{'Time elapsed:':>{pad}} {time.strftime("%H:%M:%S", time.gmtime(self.tot_time))}\n"""
f"""{'ETA:':>{pad}} {time.strftime("%H:%M:%S", time.gmtime(self.tot_time / (locs['it'] - locs['start_iter'] + 1) * (
locs['start_iter'] + locs['num_learning_iterations'] - locs['it'])))}\n"""
f"""{'ETA:':>{pad}} {time.strftime(
"%H:%M:%S",
time.gmtime(
self.tot_time / (locs['it'] - locs['start_iter'] + 1)
* (locs['start_iter'] + locs['num_learning_iterations'] - locs['it'])
)
)}\n"""
)
print(log_string)

Expand Down Expand Up @@ -513,16 +520,20 @@ def _configure_multi_gpu(self):

# check if user has device specified for local rank
if self.device != f"cuda:{self.gpu_local_rank}":
raise ValueError(f"Device '{self.device}' does not match expected device for local rank '{self.gpu_local_rank}'.")
raise ValueError(
f"Device '{self.device}' does not match expected device for local rank '{self.gpu_local_rank}'."
)
# validate multi-gpu configuration
if self.gpu_local_rank >= self.gpu_world_size:
raise ValueError(f"Local rank '{self.gpu_local_rank}' is greater than or equal to world size '{self.gpu_world_size}'.")
raise ValueError(
f"Local rank '{self.gpu_local_rank}' is greater than or equal to world size '{self.gpu_world_size}'."
)
if self.gpu_global_rank >= self.gpu_world_size:
raise ValueError(f"Global rank '{self.gpu_global_rank}' is greater than or equal to world size '{self.gpu_world_size}'.")
raise ValueError(
f"Global rank '{self.gpu_global_rank}' is greater than or equal to world size '{self.gpu_world_size}'."
)

# initialize torch distributed
torch.distributed.init_process_group(
backend="nccl", rank=self.gpu_global_rank, world_size=self.gpu_world_size
)
torch.distributed.init_process_group(backend="nccl", rank=self.gpu_global_rank, world_size=self.gpu_world_size)
# set device to the local rank
torch.cuda.set_device(self.gpu_local_rank)