def build(cfg, registry, default_args=None): """Build a module. Args: cfg (dict, list[dict]): The config of modules, is is either a dict or a list of configs. registry (:obj:`Registry`): A registry the module belongs to. default_args (dict, optional): Default arguments to build the module. Defaults to None. Returns: nn.Module: A built nn module. """ if isinstance(cfg, list): modules = [ build_from_cfg(cfg_, registry, default_args) for cfg_ in cfg ] return nn.Sequential(*modules) else: return build_from_cfg(cfg, registry, default_args)
def build_sampler(cfg, default_args: Optional[Dict] = None): world_size = dist_info.get_world_size() if world_size > 1: rank = dist_info.get_local_rank() default_args.update({'world_size': world_size, 'rank': rank}) sampler_type = getattr(cfg, 'type', None) if sampler_type != 'DistributedSampler': logger = logging.getLogger(__name__) logger.warning( f'because world_size equal {world_size},expected DistributedSampler type,but got {sampler_type}' ) cfg.type = 'DistributedSampler' elif world_size == 1: sampler_type = getattr(cfg, 'type', None) if sampler_type == 'DistributedSampler': logger = logging.getLogger(__name__) logger.warning( f'because world_size equal {world_size},and the preset sampling is {sampler_type}, ' f'here it will set sampler is None') return None SamplerAdaptor.adaptor(cfg=cfg, default_args=default_args) return build_from_cfg(cfg, SAMPLER, default_args)
def build_dataset(dataset_cfg, default_args=None): return build_from_cfg(dataset_cfg, DATASETS, default_args)
def build_processor(cfg, defualt_args=None): return build_from_cfg(cfg, PROCESSOR, defualt_args)
def build_preprocessor(cfg): """Build preprocessor.""" return build_from_cfg(cfg, PREPROCESSOR)
def build_vocab(cfg): """Build vocab.""" return build_from_cfg(cfg, VOCAB)
def build_feature_reader(cfg, default_args=None): return build_from_cfg(cfg=cfg, registry=FEATURE_READERS, default_args=default_args)
def build_hook(cfg, default_args: Optional[Dict] = None): return build_from_cfg(cfg=cfg, registry=HOOKS, default_args=default_args)
def build_batch_sampler(cfg, default_args: Optional[Dict] = None): SamplerAdaptor.adaptor(cfg=cfg, default_args=default_args) return build_from_cfg(cfg, BATCH_SAMPLER, default_args)