I wrote the following reinforcement learning code in PyTorch, TorchRL. It specifies an option to obtain pixel-based environment information in the env
variable, where tensordict_data
contains the pixel data (96x96x3) of the environment. Here is the Google Colab links.
The problem here is that the RAM space is overloaded as soon as this programme is run, probably because of this pixel data which are obtained from environments. This happens even with 50 GB of RAM in Google Colab.
I think this is a more universal problem than I would think, as machine learning often deals with image data. I suspect that this could simply be rephrased as a question of how to prevent memory leaks when dealing with RGB images as training data.
However, I have not been able to find such vital information in my research.
Anyones have any ideas?
Source code:
from collections import defaultdict
import matplotlib.pyplot as plt
import torch
import torchrl.envs.utils
from tensordict.nn import TensorDictModule
from tensordict.nn.distributions import NormalParamExtractor
from torch import nn
from torchrl.collectors import SyncDataCollector
from torchrl.data.replay_buffers import ReplayBuffer
from torchrl.data.replay_buffers.samplers import SamplerWithoutReplacement
from torchrl.data.replay_buffers.storages import LazyTensorStorage
from torchrl.envs import (Compose, DoubleToFloat, ObservationNorm, StepCounter,
TransformedEnv, GrayScale, ToTensorImage, Resize, PermuteTransform)
from torchrl.envs.libs.gym import GymEnv
from torchrl.envs.utils import check_env_specs, ExplorationType, set_exploration_type
from torchrl.modules import ProbabilisticActor, TanhNormal, ValueOperator
from torchrl.objectives import ClipPPOLoss
from torchrl.objectives.value import GAE
from torchrl.record.loggers.csv import CSVLogger
from torchrl.record.recorder import VideoRecorder
from torchvision.transforms.functional import rgb_to_grayscale
import torchvision
from tqdm import tqdm
import os, sys
import random
import numpy as np
import torchvision.transforms.functional as F
import math
import gc
def main():
train(0)
def train(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
torch.use_deterministic_algorithms(True, warn_only=True)
device = "cpu" if not torch.backends.cuda.is_built() else "cuda:0"
num_cells = 256
lr = 3e-4
max_grad_norm = 1.0
frame_skip = 1
frames_per_batch = 1000 // frame_skip
total_frames = 500_000 // frame_skip
sub_batch_size = 64 # cardinality of the sub-samples gathered from the current data in the inner loop
num_epochs = 3 # optimization steps per batch of data collected
clip_epsilon = (
0.2 # clip value for PPO loss: see the equation in the intro for more context.
)
gamma = 0.99
lmbda = 0.95
entropy_eps = 1e-4
base_env = GymEnv("InvertedDoublePendulum-v4", device=device, frame_skip=frame_skip,
from_pixels=True, pixels_only=False)
env = TransformedEnv(
base_env,
Compose(
ObservationNorm(in_keys=["observation"]),
PermuteTransform((-1, -3, -2), in_keys=["pixels"]),
Resize(96, 96),
PermuteTransform((-2, -1, -3), in_keys=["pixels"]),
ToTensorImage(),
DoubleToFloat(),
StepCounter(),
),
)
_ = env.set_seed(seed)
env.transform[0].init_stats(num_iter=1000, reduce_dim=0, cat_dim=0)
print(env)
print("normalization constant shape:", env.transform[0].loc.shape)
print("observation_spec:", env.observation_spec)
print("reward_spec:", env.reward_spec)
print("input_spec:", env.input_spec)
print("action_spec (as defined by input_spec):", env.action_spec)
check_env_specs(env)
actor_net = nn.Sequential(
nn.LazyLinear(num_cells, device=device),
nn.Tanh(),
nn.LazyLinear(num_cells, device=device),
nn.Tanh(),
nn.LazyLinear(num_cells, device=device),
nn.Tanh(),
nn.LazyLinear(2 * env.action_spec.shape[-1], device=device),
NormalParamExtractor(),
)
policy_module = TensorDictModule(
actor_net, in_keys=["observation"], out_keys=["loc", "scale"]
)
policy_module = ProbabilisticActor(
module=policy_module,
spec=env.action_spec,
in_keys=["loc", "scale"],
distribution_class=TanhNormal,
distribution_kwargs={
"low": env.action_spec.space.low,
"high": env.action_spec.space.high,
},
# safe=True,
return_log_prob=True,
# we'll need the log-prob for the numerator of the importance weights
)
value_net = nn.Sequential(
nn.LazyLinear(num_cells, device=device),
nn.Tanh(),
nn.LazyLinear(num_cells, device=device),
nn.Tanh(),
nn.LazyLinear(num_cells, device=device),
nn.Tanh(),
nn.LazyLinear(1, device=device),
)
value_module = ValueOperator(
module=value_net,
in_keys=["observation"],
)
print("Running policy:", policy_module(env.reset()))
print("Running value:", value_module(env.reset()))
collector = SyncDataCollector(
env,
policy_module,
frames_per_batch=frames_per_batch,
total_frames=total_frames,
split_trajs=False,
device=device,
return_same_td=True
)
collector.set_seed(seed)
replay_buffer = ReplayBuffer(
storage=LazyTensorStorage(max_size=frames_per_batch),
sampler=SamplerWithoutReplacement(),
)
advantage_module = GAE(
gamma=gamma, lmbda=lmbda, value_network=value_module, average_gae=True
)
loss_module = ClipPPOLoss(
actor_network=policy_module,
critic_network=value_module,
clip_epsilon=clip_epsilon,
entropy_bonus=bool(entropy_eps),
entropy_coef=entropy_eps,
# these keys match by default but we set this for completeness
critic_coef=1.0,
loss_critic_type="smooth_l1",
)
optim = torch.optim.Adam(loss_module.parameters(), lr)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
optim, total_frames // frames_per_batch, 0.0
)
logs = defaultdict(list)
# pbar = tqdm(total=total_frames * frame_skip)
eval_str = ""
# We iterate over the collector until it reaches the total number of frames it was designed to collect:
for i, tensordict_data in enumerate(collector):
from torchrl.collectors.utils import split_trajectories
# We now have a batch of data to work with. Let's learn something from it.
for _ in range(num_epochs):
# We'll need an "advantage" signal to make PPO work.
# We re-compute it at each epoch as its value depends on the value network which is updated in the inner loop.
advantage_module(tensordict_data)
data_view = tensordict_data.reshape(-1)
replay_buffer.extend(data_view)
for _ in range(frames_per_batch // sub_batch_size):
subdata = replay_buffer.sample(sub_batch_size)
loss_vals = loss_module(subdata.to(device))
loss_value = (
loss_vals["loss_objective"]
+ loss_vals["loss_critic"]
+ loss_vals["loss_entropy"]
)
# Optimization: backward, grad clipping and optimization step
loss_value.backward()
# this is not strictly mandatory but it's good practice to keep your gradient norm bounded
torch.nn.utils.clip_grad_norm_(loss_module.parameters(), max_grad_norm, error_if_nonfinite=False)
del loss_value
optim.step()
optim.zero_grad()
logs["reward"].append(tensordict_data["next", "reward"].mean().item())
# pbar.update(tensordict_data.numel() * frame_skip)
cum_reward_str = (
f"average reward={logs['reward'][-1]: 4.4f} (init={logs['reward'][0]: 4.4f})"
)
logs["step_count"].append(tensordict_data["step_count"].max().item())
stepcount_str = f"step count (max): {logs['step_count'][-1]}"
print(stepcount_str)
logs["lr"].append(optim.param_groups[0]["lr"])
lr_str = f"lr policy: {logs['lr'][-1]: 4.4f}"
if i % 10 == 0:
# We evaluate the policy once every 10 batches of data.
# Evaluation is rather simple: execute the policy without exploration
# (take the expected value of the action distribution) for a given
# number of steps (1000, which is our ``env`` horizon).
# The ``rollout`` method of the ``env`` can take a policy as argument:
# it will then execute this policy at each step.
with set_exploration_type(ExplorationType.MEAN), torch.no_grad():
# execute a rollout with the trained policy
eval_rollout = env.rollout(1000, policy_module)
env.transform.dump()
logs["eval reward"].append(eval_rollout["next", "reward"].mean().item())
logs["eval reward (sum)"].append(
eval_rollout["next", "reward"].sum().item()
)
logs["eval step_count"].append(eval_rollout["step_count"].max().item())
eval_str = (
f"eval cumulative reward: {logs['eval reward (sum)'][-1]: 4.4f} "
f"(init: {logs['eval reward (sum)'][0]: 4.4f}), "
f"eval step-count: {logs['eval step_count'][-1]}"
)
del eval_rollout
if __name__ == "__main__":
main()
2
So the lowest hanging fruit here would be making use of parallel computation with GPU. Since according to the picture your RAM is overloaded and your GPU is almost unused transferring some operations might be a good place to start. To start use device='cuda'
I think all GPU from Collab should work. You can also split data into batches to make even more use of parallel computation (torch.nn.DataParallel
) to split it even further and distribute the load.
Other suggestions to try are here but they are very dependent on your data and overall goal so not all might be applicable. I’m naming even the once not applicable to your data for future readers:
- Convert image to gray-scale, don’t know how important is the color but this immediately reduces the size of your data from 96x96x3 to 96×96
- Reduce the size of the images from 98×98 to something like 64×64 or 48×48. Same as in gray-scale you lose some information and this is depending on your data.
- You could try reducing your
frames_per_batch
to something like 500 - Call garbage collection
gc.collect()
and free GPU memorytorch.cuda.empty_cache()
manually. Second part assumes you are using GPU for computation - You can try storing only necessary fields like observation, action, reward in the replay buffer to avoid keeping large pixel data unnecessarily. This is a stretch though and I would say that parallel computation and first two mentioned techniques here are your better bet.
- By a 10$ a month subscription on Collab or apply if you are a student for discount from other services like Azure (gets you 100$ to use for a year as a student). Not best solution I know but depends if you want or can spend money. This is again something I would only recommend after trying other options optimizing your code by buying better equipment is a solution just not a good one and should be done after your code is already optimized.
3