Train multiple models simultaneously on a single GPU

username: Lorenzoz
cooked: <p>I want to train an ensemble of NNs on a single GPU in parallel.<br>
Currently I’m doing this:</p>
<pre><code class="lang-auto">for model in models:
  model.to('cuda')
  train_model(model, ...)
</code></pre>
<p>Each model is quite small but the GPU utilisation is tiny (3%), which makes me think that the training is happening serially. In fact, if I increase the number of members of the ensemble the training time increases proportionally. This confuses me, as CUDA calls should be asynchronous. Is there any reason why this shouldn’t run in parallel?</p>
<p>I’ve tried to go a step further by taking inspiration from <a href="https://github.com/TorchEnsemble-Community/Ensemble-Pytorch/tree/master/torchensemble" rel="noopener nofollow ugc">torchensemble</a> using joblib (see code below) but I am not seeing a speedup whatsoever.</p>
<pre><code class="lang-auto">def _parallel_fit_per_epoch(
    train_loader,
    estimator,
    optimizer,
    criterion,
    device,
):
    for batch_idx, elem in enumerate(train_loader):
        data, target = elem[0].to(device), elem[1].to(device)
        optimizer.zero_grad()
        output = estimator.forward(data)
        loss = criterion(output, target.unsqueeze(-1).float())
        loss.backward()
        optimizer.step()
    return estimator, optimizer, loss

class Regressor(nn.Module):
    # etc
class Ensemble(nn.Module):

    def __init__(self, num_models, ...):
        self.num_models = num_models
        self.models = [Regressor(...) for _ in self.num_models]
       # etc 

    def fit(self, train_dataset,valid_dataset,epochs,batch_size, learning_rate,
             weight_decay, patience, save_model=False, save_dir=None):
        optimizers = []
        train_loaders = []
        for model in self.models:
            opt = torch.optim.Adam(model.parameters(), lr=learning_rate)            
            optimizers.append(opt)
            train_loaders.append(train_dataset, batch_size=batch_size, shuffle=True))

        with Parallel(n_jobs=self.num_models) as parallel:
            # Training loop
            for epoch in range(epochs):
                rets = parallel(
                    delayed(_parallel_fit_per_epoch)(
                        dataloader,
                        estimator,
                        optimizer,
                        self.loss,
                        self.device,
                    )
                    for idx, (estimator, optimizer, dataloader) in enumerate(
                        zip(self.models, optimizers, train_loaders)
                    )
                )

                estimators, optimizers, losses = [], [], []
                for estimator, optimizer, loss in rets:
                    estimators.append(estimator)
                    optimizers.append(optimizer)
                    losses.append(loss)
</code></pre>
<p>I’m running all this on a slurm cluster, using:</p>
<pre><code class="lang-auto">#SBATCH --nodes=1
#SBATCH --mem=120G
#SBATCH --gres=gpu:1
</code></pre>
<p>Are there any other directives I should be using?</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="Lorenzoz" data-post="1" data-topic="169597">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lorenzoz/48/30501_2.png" class="avatar"> Lorenzoz:</div>
<blockquote>
<p>This confuses me, as CUDA calls should be asynchronous. Is there any reason why this shouldn’t run in parallel?</p>
</blockquote>
</aside>
<p>CUDA calls are indeed asynchronous. However, your CPU must be fast enough in scheduling the kernels so that a parallel execution would even be possible.<br>
A low GPU utilization often indicates that your use case is CPU-limited and that the GPU is waiting for new work between short periods of actual execution.<br>
You could check if via a visual profiler such as Nsight Systems which would indicate a lot of whitespaces between the kernels in the timeline.</p>
<p>Besides that your GPU would also need free compute resources to be able to run multiple kernels in parallel.</p>

username: Lorenzoz
cooked: <p>Thanks <a class="mention" href="/u/ptrblck">@ptrblck</a> for the fast reply! I have a question though: if CUDA calls work asynchronously and my first piece of code should work in parallel (with the caveats you mentioned) then could you explain to me what is the point of using <code>joblib</code> in <code>torchensemble</code>, or <code>vmap</code> in <code>functorch</code> (and <code>JAX</code>)?</p>

username: ptrblck
cooked: <p>I’m not familiar with <code>torchensemble</code>, but would guess the authors tried to use multiprocessing via <code>joblib</code> to avoid the aforementioned CPU bottlenecks.</p>
<p><code>vmap</code> tries to automatically batch workloads instead of using a loop over a specific op.<br>
The <a href="https://pytorch.org/tutorials/prototype/vmap_recipe.html"><code>torch.vmap</code> recipe</a> shows an example for <code>torch.dot</code> which does not accept batched inputs as well as other examples.</p>

username: Lorenzoz
cooked: <p>Thanks for that. Would these bottlenecks come from anything other than a custom <code>Dataset</code> or/and training loop? My custom <code>Dataset</code> only performs indexing, and my training loop is just the standard Torch loop (see below). I’ve run this on Colab and I keep getting the same performance as on the cluster so it’s not related to slurm directives. For reference, each training epoch takes about 0.1s, so about 10s for 100 epochs of training. Isn’t that enough for the CPU to schedule the kernels?</p>
<pre><code class="lang-auto">class MyDataset(Dataset):
    def __init__(self, X, y, z, indices):
        self.X = X
        self.y = y
        self.z = z
        self.indices = indices

    def __len__(self):
        return len(self.y)

    def __getitem__(self, idx):
        return self.X[idx], self.y[idx], self.z[idx], self.indices[idx]

class Regressor(nn.Module):
    # various defs..
    def train_step(self, dataloader):
        for _, elem in enumerate(train_loader):
            data, target = elem[0].to('cuda'), elem[1].to('cuda')
            optimizer.zero_grad()
            output = estimator.forward(data)
            loss = criterion(output, target)
            loss.backward()
            optimizer.step()    
class Ensemble(nn.Module):
    def __init__(self, num_models, ...):
        self.num_models = num_models
        self.models = [Regressor(...).to('cuda') for _ in self.num_models]

X,Y,Z, ID = get_data()
train_dataset = MyDataset(X,Y,Z,ID)
train_loader = DataLoader(train_dataset, shuffle=True, pin_memory=True, batch_size=256)
ensemble = Ensemble()
for model in ensemble.models:
  model.train()
  for epoch in range(epochs):
      train_loss_ = model.train_step(train_loader, opt, epoch=epoch)
</code></pre>

username: suraj.pt
cooked: <p>Just to make sure, in code snippet 1 are you moving <code>estimator</code> to the GPU?</p>
<p>In code snippet 2, I believe you’re registering the optimizer in <code>Regressor</code> first, and then moving to cuda. You must register params to the optimizer after you move to cuda (ref: <a href="https://github.com/pytorch/pytorch/issues/7844#issuecomment-503713840" class="inline-onebox">"Parameters of a model after .cuda() will be different objects with those before the call." is wrong. · Issue #7844 · pytorch/pytorch · GitHub</a>)</p>
<p>By the way, it’s recommended to use <code>ModuleList</code> and a single optimizer (unless you want different optimizer algos on subsets of the ensemble…). This makes saving to disk easy; you only need to save <code>ensemble</code> and <code>optimizer</code> instead of saving all the different objects in your lists. See this quick example for reference:</p>
<pre><code class="lang-auto">class Ensemble(nn.Module):
   def __init__(self, num_models, ...):
        self.num_models = num_models
        self.model_list = nn.ModuleList([Regressor(...) for _ in self.num_models])
       # etc 

   def forward (data, target):
        loss = 0
        for mod in self.model_list:
            loss += mod(data, target, self.criterion). # calculate loss in the regressor's forward
        return loss
  
def fit(ensemble, ...):
    ensemble.to(device)
   # register a single optimizer (with different param_groups for each regressors if necessary) instead of constructing multiple optimizers 
    optimizer = Adam(ensemble.parameters(), lr=lr)
    for data, target in loader:
        loss = ensemble(data, target)
        optimizer.zero_grad()
        loss.backward(); optimizer.step()</code></pre>

username: Lorenzoz
cooked: <p>Thanks <a class="mention" href="/u/suraj.pt">@suraj.pt</a>!  Yes I was moving <code>estimator</code> to GPU, sorry!</p>
<aside class="quote no-group" data-username="suraj.pt" data-post="6" data-topic="169597">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/suraj.pt/48/53186_2.png" class="avatar"> suraj.pt:</div>
<blockquote>
<p>You must register params to the optimizer after you move to cuda</p>
</blockquote>
</aside>
<p>I didn’t know about this! However the behaviour does not seem to change if I register the optimiser before doing <code>.cuda()</code>. Is this expected?</p>
<p>Thanks also for the example code, I didn’t consider that option. But isn’t this going to behave very similarly to just do the naive parallelisation that I was doing to start with (see first post)?</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="Lorenzoz" data-post="5" data-topic="169597">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lorenzoz/48/30501_2.png" class="avatar"> Lorenzoz:</div>
<blockquote>
<p>Would these bottlenecks come from anything other than a custom <code>Dataset</code> or/and training loop?</p>
</blockquote>
</aside>
<p>Data loading and processing could certainly be responsible for a large CPU overhead, but note that it would depend on the actual CPU vs. GPU workload.<br>
Even just kernel launches could show a visible CPU overhead if the actual GPU workload is tiny.<br>
Think about a theoretical workload where each kernel launch takes 1ms while the GPU workload takes 1us.<br>
Even if the GPU could run these kernels in parallel your CPU will never be fast enough to schedule these kernels fast enough, which is why I recommended to profile your workload using a visual profiler.</p>

username: Lorenzoz
cooked: <p>I see, that would explain also why my GPU memory usage is so tiny. I’ll give a go with the profiler then.</p>
<p>If CPU overheads turn out to be an issue, what options do I have? It still takes only about 2 minutes to train my ensemble (some ~25s per NN), but I have to do it for 10 of these ensembles, each regressing to different variables. So that’s 20 minutes on a single GPU. I have to do Active Learning with this, and need to retrain about 300 times. This is very expensive without using a separate GPU for each ensemble. And even then, if the GPU isn’t doing much, it’s a lot of wasted GPU time…</p>

username: ptrblck
cooked: <p>If the kernel launches are indeed visible, you could use CUDA Graphs as described <a href="https://pytorch.org/blog/accelerating-pytorch-with-cuda-graphs/">here</a> and <a href="https://pytorch.org/docs/stable/notes/cuda.html#cuda-graphs">here</a>. Note that this workflow has a few limitations, such as static input shapes, but once the graph is recorded the replay would be cheap as you would only launch it once instead of each tiny kernel separately.</p>