-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from YuxinZou/dist
add default sampler
- Loading branch information
Showing
15 changed files
with
85 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
from .builder import build_sampler | ||
from .distributed import DefaultSampler | ||
from .non_distributed import DefaultSampler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
from ...utils import build_from_cfg | ||
from .registry import SAMPLERS | ||
from .registry import NON_DISTRIBUTED_SAMPLERS, DISTRIBUTED_SAMPLERS | ||
|
||
|
||
def build_sampler(cfg, default_args=None): | ||
sampler = build_from_cfg(cfg, SAMPLERS, default_args) | ||
def build_sampler(distributed, cfg, default_args=None): | ||
if distributed: | ||
sampler = build_from_cfg(cfg, DISTRIBUTED_SAMPLERS, default_args) | ||
else: | ||
sampler = build_from_cfg(cfg, NON_DISTRIBUTED_SAMPLERS, default_args) | ||
|
||
return sampler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from torch.utils.data import DistributedSampler | ||
|
||
from ...utils import get_dist_info | ||
from .registry import DISTRIBUTED_SAMPLERS | ||
|
||
|
||
@DISTRIBUTED_SAMPLERS.register_module | ||
class DefaultSampler(DistributedSampler): | ||
"""Default distributed sampler.""" | ||
|
||
def __init__(self, dataset, shuffle=True): | ||
rank, num_replicas = get_dist_info() | ||
super().__init__(dataset, num_replicas, rank, shuffle) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import torch | ||
from torch.utils.data import Sampler | ||
|
||
from .registry import NON_DISTRIBUTED_SAMPLERS | ||
|
||
|
||
@NON_DISTRIBUTED_SAMPLERS.register_module | ||
class DefaultSampler(Sampler): | ||
"""Default non-distributed sampler.""" | ||
|
||
def __init__(self, dataset, shuffle=True): | ||
self.dataset = dataset | ||
self.shuffle = shuffle | ||
|
||
def __iter__(self): | ||
if self.shuffle: | ||
return iter(torch.randperm(len(self.dataset)).tolist()) | ||
else: | ||
return iter(range(len(self.dataset))) | ||
|
||
def __len__(self): | ||
return len(self.dataset) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
from torch.utils.data import DistributedSampler | ||
|
||
from ...utils import Registry | ||
|
||
SAMPLERS = Registry('sampler') | ||
|
||
SAMPLERS.register_module(DistributedSampler) | ||
DISTRIBUTED_SAMPLERS = Registry('distributed_sampler') | ||
NON_DISTRIBUTED_SAMPLERS = Registry('non_distributed_sampler') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters