Returns: CfgNode: a new config. Same as original if ``cfg.SOLVER.REFERENCE_WORLD_SIZE==0``.
(cfg, num_workers: int)
| 267 | |
| 268 | @staticmethod |
| 269 | def auto_scale_workers(cfg, num_workers: int): |
| 270 | """ |
| 271 | Returns: |
| 272 | CfgNode: a new config. Same as original if ``cfg.SOLVER.REFERENCE_WORLD_SIZE==0``. |
| 273 | """ |
| 274 | old_world_size = cfg.SOLVER.REFERENCE_WORLD_SIZE |
| 275 | if old_world_size == 0 or old_world_size == num_workers: |
| 276 | return cfg |
| 277 | cfg = copy.deepcopy(cfg) |
| 278 | # frozen = cfg.is_frozen() |
| 279 | # cfg.defrost() |
| 280 | |
| 281 | assert ( |
| 282 | cfg.SOLVER.IMS_PER_BATCH % old_world_size == 0 |
| 283 | ), "Invalid REFERENCE_WORLD_SIZE in config!" |
| 284 | scale = num_workers / old_world_size |
| 285 | bs = cfg.SOLVER.IMS_PER_BATCH = int(round(cfg.SOLVER.IMS_PER_BATCH * scale)) |
| 286 | lr = cfg.SOLVER.BASE_LR = cfg.SOLVER.BASE_LR * scale |
| 287 | max_iter = cfg.SOLVER.MAX_ITER = int(round(cfg.SOLVER.MAX_ITER / scale)) |
| 288 | warmup_iter = cfg.SOLVER.WARMUP_ITERS = int(round(cfg.SOLVER.WARMUP_ITERS / scale)) |
| 289 | cfg.SOLVER.STEPS = tuple(int(round(s / scale)) for s in cfg.SOLVER.STEPS) |
| 290 | cfg.TEST.EVAL_PERIOD = int(round(cfg.TEST.EVAL_PERIOD / scale)) |
| 291 | cfg.SOLVER.CHECKPOINT_PERIOD = int(round(cfg.SOLVER.CHECKPOINT_PERIOD / scale)) |
| 292 | cfg.SOLVER.REFERENCE_WORLD_SIZE = num_workers # maintain invariant |
| 293 | logger = logging.getLogger(__name__) |
| 294 | logger.info( |
| 295 | f"Auto-scaling the config to batch_size={bs}, learning_rate={lr}, " |
| 296 | f"max_iter={max_iter}, warmup={warmup_iter}." |
| 297 | ) |
| 298 | return cfg |
| 299 | |
| 300 | @classmethod |
| 301 | def test(cls, cfg, model, evaluators=None): |