DistributedSampler for validation set in ImageNet example

username: churchillmic
cooked: <p>The <a href="https://github.com/pytorch/examples/blob/master/imagenet/main.py" rel="nofollow noopener">ImageNet example</a> has a DistributedSampler for the training loader, but not the validation loader. This would appear to have every rank processing the entire data for the validation set. Is this necessary, or could a DistributedSampler be used for the validation loader also, to apply the multiple nodes to processing the validation set?</p>

username: anil_batra
cooked: <p>Hi <a class="mention" href="/u/churchillmic">@churchillmic</a>,<br>
I have the same query. Did you able to find the answer for this?</p>
<p>Thanks<br>
Anil</p>

username: churchillmic
cooked: <p>I found a couple examples where a DistributedSampler was used for the validation or test set. I’m still not sure why the official Imagenet example doesn’t use it, it still seems wasteful to me. Here are a few of the examples:<br>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/huggingface/pytorch-pretrained-BERT/blob/c9fd3505678d581388fb44ba1d79ac41e8fb28a4/examples/extract_features.py" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/huggingface/pytorch-pretrained-BERT/blob/c9fd3505678d581388fb44ba1d79ac41e8fb28a4/examples/extract_features.py" target="_blank" rel="nofollow noopener">huggingface/pytorch-pretrained-BERT/blob/c9fd3505678d581388fb44ba1d79ac41e8fb28a4/examples/extract_features.py</a></h4>
<pre><code class="lang-py"># coding=utf-8
# Copyright 2018 The Google AI Language Team Authors and The HugginFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Extract pre-computed feature vectors from a PyTorch BERT model."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

</code></pre>

  This file has been truncated. <a href="https://github.com/huggingface/pytorch-pretrained-BERT/blob/c9fd3505678d581388fb44ba1d79ac41e8fb28a4/examples/extract_features.py" target="_blank" rel="nofollow noopener">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/Jongchan/Pytorch-Horovod-Examples/blob/master/examples/cifar100/main_horovod.py" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/Jongchan/Pytorch-Horovod-Examples/blob/master/examples/cifar100/main_horovod.py" target="_blank" rel="nofollow noopener">Jongchan/Pytorch-Horovod-Examples/blob/master/examples/cifar100/main_horovod.py</a></h4>
<pre><code class="lang-py">from __future__ import print_function

import torch
import torch.nn as nn
import torch.nn.init as init
import torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
import config as cf

import torchvision
import torchvision.transforms as transforms
import torchvision.datasets as datasets

import os
import sys
import time
import argparse
import datetime

</code></pre>

  This file has been truncated. <a href="https://github.com/Jongchan/Pytorch-Horovod-Examples/blob/master/examples/cifar100/main_horovod.py" target="_blank" rel="nofollow noopener">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


username: pietern
cooked: <p>It is not necessary to have every rank process the entire validation set. You can use a distributed sampler and average the errors afterwards to achieve the same result.</p>

username: Hzzone
cooked: <p>Actually, you cannot use ddp sampler to achieve validation. You can see <a href="https://pytorch.org/docs/stable/_modules/torch/utils/data/distributed.html#DistributedSampler" rel="nofollow noopener">DistributedSampler</a>; note that the dataset has added extra samples to make it evenly divisible. Therefore, if your dataset is very small, the final result may be different. The official implementation is right.</p>