Is it possible to change the batch_size of a dataloader after it was created?

username: Shisho_Sama
cooked: <p>Is this possible?<br>
I’m asking this becasue for example today I had couple of models and for each of them I’d like to use a different batch_size, I initially created a dataloader with lets say batch_size of 32, and now I want to increase its size to lets say 128. but I dont want to create a new dataloader.<br>
Is this possible?<br>
I would appreciate any kind of help in this regard.</p>

username: ptrblck
cooked: <p>This shouldn’t be allowed in the current version anymore and you’ll get a <code>ValueError</code>:</p>
<pre><code class="lang-python">ValueError: batch_size attribute should not be set after DataLoader is initialized
</code></pre>
<p>Creating a new <code>DataLoader</code> should be cheap, so I would recommend to initialize a new <code>DataLoader</code>.</p>

username: Shisho_Sama
cooked: <p>Thanks, but why not?</p>

username: Gianmarco_Aversano
cooked: <p>Why isn’t this possible? How do I know what kind of DataLoader was passed?</p>
<p>Say I have the following:</p>
<pre><code class="lang-python">def return_dl(
    dataloader: DataLoader,
    batch_size: int = None,
) -&gt; DataLoader:

    if batch_size is not None:
        dataloader.batch_size = batch_size

    return dataloader
</code></pre>
<p>Here, <code>dataloader</code> could be any <code>DataLoader</code> subclass, e.g. 'NeighborLoader` from PyTorch Geometric. Why should this remain not allowed?</p>

username: ptrblck
cooked: <p>The <code>ValueError</code> was added <a href="https://github.com/pytorch/pytorch/blob/57e05e822d0f53db04d2ee2216906f6fc01b4a4f/torch/utils/data/dataloader.py#L418-L422">here</a> when <code>persistent_workers</code> were introduced since changing internal attributes won’t have an effect anymore. I’m sure the code owners would like to hear your concerns and PRs should be welcome in case you want to allow changing these attributes for valid use cases.</p>

username: Gianmarco_Aversano
cooked: <p>Thanks for the reply. But then how would one handle my use case? You receive an already instantiated <code>DataLoader</code>, and one has to change its batch size while keeping all the rest. What would be the best way to do so? How can one initialize a new <code>DataLoader</code> that is exactly like the old but with a different batch size?</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="Gianmarco_Aversano" data-post="6" data-topic="55912">
<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/gianmarco_aversano/48/54198_2.png" class="avatar"> Gianmarco_Aversano:</div>
<blockquote>
<p>How can one initialize a new <code>DataLoader</code> that is exactly like the old but with a different batch size?</p>
</blockquote>
</aside>
<p>Exactly as you’ve described: you could re-initialize a new <code>DataLoader</code>. Usually the creation of the <code>DataLoader</code> is cheap compared to the actual data loading and processing (assuming you are using lazy data loading).<br>
Another complication (besides persistent workers) is that each worker pre-fetches batches in advance. If your code now manipulates internal states (such as the batch size) how and when should the change be visible?<br>
Maybe there could be a clear API design, so your ideas are welcome.</p>

username: Gianmarco_Aversano
cooked: <p>Thanks for being very active here!</p>
<p>Yep, I see. But re-initializing a new <code>DataLoader</code> is not easy as I do not know how this <code>DataLoader</code> was initialized! In my design, the user passes a dataloader and I have to change its batch size. Why? Well, because that dataloader (more the data behind it) is used to train two different models. The second model benefits from huge batch sizes. To solve the problem, I see no other solutions but to force the user to create two dataloaders, unless there is an easy way to create a new <code>DataLoader</code> from an existing one.</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="Gianmarco_Aversano" data-post="8" data-topic="55912">
<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/gianmarco_aversano/48/54198_2.png" class="avatar"> Gianmarco_Aversano:</div>
<blockquote>
<p>unless there is an easy way to create a new <code>DataLoader</code> from an existing one.</p>
</blockquote>
</aside>
<p>You should be able to still read the attributes of the passed <code>DataLoader</code> and could create the new one based on these attributes. Note that you might also need to check the internal <code>sampler</code> and its attributes in order to restore it. In case the user creates a custom <code>sampler</code> and you wouldn’t have the source code of it (I don’t know how it would work but it seems you don’t even know how the <code>Dataset</code> is defined), you might not be able to restore it.</p>