(config)
| 165 | |
| 166 | |
| 167 | def main(config): |
| 168 | # prepare data loaders |
| 169 | dataset_train, dataset_val, dataset_test, data_loader_train, \ |
| 170 | data_loader_val, data_loader_test, mixup_fn = build_loader(config) |
| 171 | |
| 172 | # build runner |
| 173 | logger.info(f'Creating model:{config.MODEL.TYPE}/{config.MODEL.NAME}') |
| 174 | model = build_model(config) |
| 175 | model.cuda() |
| 176 | logger.info(str(model)) |
| 177 | |
| 178 | # build optimizer |
| 179 | optimizer = build_optimizer(config, model) |
| 180 | |
| 181 | if config.AMP_OPT_LEVEL != 'O0': |
| 182 | config.defrost() |
| 183 | if has_native_amp: |
| 184 | config.native_amp = True |
| 185 | use_amp = 'native' |
| 186 | elif has_apex: |
| 187 | config.apex_amp = True |
| 188 | use_amp = 'apex' |
| 189 | else: |
| 190 | use_amp = None |
| 191 | logger.warning( |
| 192 | 'Neither APEX or native Torch AMP is available, using float32. ' |
| 193 | 'Install NVIDA apex or upgrade to PyTorch 1.6') |
| 194 | config.freeze() |
| 195 | |
| 196 | # setup automatic mixed-precision (AMP) loss scaling and op casting |
| 197 | amp_autocast = suppress # do nothing |
| 198 | loss_scaler = None |
| 199 | if config.AMP_OPT_LEVEL != 'O0': |
| 200 | if use_amp == 'apex': |
| 201 | model, optimizer = amp.initialize(model, |
| 202 | optimizer, |
| 203 | opt_level=config.AMP_OPT_LEVEL) |
| 204 | loss_scaler = ApexScaler() |
| 205 | if config.LOCAL_RANK == 0: |
| 206 | logger.info( |
| 207 | 'Using NVIDIA APEX AMP. Training in mixed precision.') |
| 208 | if use_amp == 'native': |
| 209 | amp_autocast = torch.cuda.amp.autocast |
| 210 | loss_scaler = NativeScaler() |
| 211 | if config.LOCAL_RANK == 0: |
| 212 | logger.info( |
| 213 | 'Using native Torch AMP. Training in mixed precision.') |
| 214 | else: |
| 215 | if config.LOCAL_RANK == 0: |
| 216 | logger.info('AMP not enabled. Training in float32.') |
| 217 | |
| 218 | # put model on gpus |
| 219 | model = torch.nn.parallel.DistributedDataParallel( |
| 220 | model, device_ids=[config.LOCAL_RANK], broadcast_buffers=False) |
| 221 | |
| 222 | # try: |
| 223 | # model.register_comm_hook(state=None, hook=fp16_compress_hook) |
| 224 | # logger.info('using fp16_compress_hook!') |
no test coverage detected