def get_basic_sparsity_config(model_size=4, input_sample_size=None, sparsity_init=0.02, sparsity_target=0.5, sparsity_target_epoch=2, sparsity_freeze_epoch=3): if input_sample_size is None: input_sample_size = [1, 1, 4, 4] config = NNCFConfig() config.update({ 'model': 'basic_sparse_conv', 'model_size': model_size, 'input_info': { 'sample_size': input_sample_size, }, 'compression': { 'algorithm': 'rb_sparsity', 'sparsity_init': sparsity_init, 'params': { 'schedule': 'polynomial', 'sparsity_target': sparsity_target, 'sparsity_target_epoch': sparsity_target_epoch, 'sparsity_freeze_epoch': sparsity_freeze_epoch }, } }) return config
def worker(rank: int, world_size: int) -> None: torch.distributed.init_process_group(backend="nccl", init_method='tcp://127.0.0.1:8999', world_size=world_size, rank=rank) model = TestModelWithChangedTrain(freezing_stages=1) model.cuda() model.to(rank) nncf_config = NNCFConfig() nncf_config.update({ "input_info": { "sample_size": [1, 1, 30, 30] }, "compression": { "algorithm": "quantization", "initializer": { "range": { "num_init_samples": 10 }, "batchnorm_adaptation": { "num_bn_adaptation_samples": 10 } } } }) dataloader = create_random_mock_dataloader(nncf_config, num_samples=10) register_default_init_args(nncf_config, dataloader) _, compressed_model = create_compressed_model(model, nncf_config) # At this part the additional processes may be freezing _ = torch.nn.parallel.DistributedDataParallel(compressed_model, device_ids=[rank])
def test_model_device_before_create_compressed_model(device_placing, inference_type): if not torch.cuda.is_available() and not inference_type == 'cpu': pytest.skip("Skipping CUDA test cases for CPU only setups") input_size = [1, 1, 8, 8] config = NNCFConfig() config = get_kd_config(config) config.update({ "input_info": { "sample_size": input_size, }, }) if inference_type == 'DDP': ngpus_per_node = torch.cuda.device_count() config.world_size = ngpus_per_node torch.multiprocessing.spawn(run_training_for_device_testing, nprocs=ngpus_per_node, args=(config, inference_type, ngpus_per_node, device_placing), join=True) else: run_training_for_device_testing(None, config, inference_type, None, device_placing=device_placing)
def get_basic_sparsity_config(model_size=4, input_sample_size=None, sparsity_init=0.02, sparsity_target=0.5, sparsity_target_epoch=2, sparsity_freeze_epoch=3, scheduler='polinomial'): if input_sample_size is None: input_sample_size = [1, 1, 4, 4] config = NNCFConfig() config.update({ "model": "basic_sparse_conv", "model_size": model_size, "input_info": { "sample_size": input_sample_size, }, "compression": { "algorithm": "rb_sparsity", "sparsity_init": sparsity_init, "params": { "schedule": scheduler, "sparsity_target": sparsity_target, "sparsity_target_epoch": sparsity_target_epoch, "sparsity_freeze_epoch": sparsity_freeze_epoch }, } }) return config
def get_config_for_logarithm_scale(logarithm_scale: bool, quantization_type: str) -> NNCFConfig: nncf_config = NNCFConfig() nncf_config.update({ "input_info": { "sample_size": SAMPLE_SIZE }, "target_device": 'TRIAL', "compression": { "algorithm": "quantization", "initializer": { "range": { "num_init_samples": 4, "type": "percentile", "params": { "min_percentile": 0.001, "max_percentile": 99.999 } } }, "activations": { "mode": quantization_type, "logarithm_scale": logarithm_scale }, "weights": { "mode": quantization_type, "signed": True, "logarithm_scale": logarithm_scale } } }) class RandDatasetMock: def __getitem__(self, index): return torch.rand(*SAMPLE_SIZE) def __len__(self): return 4 data_loader = torch.utils.data.DataLoader(RandDatasetMock(), batch_size=1, shuffle=False, drop_last=True) class SquadInitializingDataloader( nncf.torch.initialization.PTInitializingDataLoader): def get_inputs(self, batch): return batch, {} def get_target(self, batch): return None initializing_data_loader = SquadInitializingDataloader(data_loader) init_range = nncf.config.structures.QuantizationRangeInitArgs( initializing_data_loader) nncf_config.register_extra_structs([init_range]) register_bn_adaptation_init_args(nncf_config) return nncf_config
def get_config_for_export_mode(should_be_onnx_standard: bool) -> NNCFConfig: nncf_config = NNCFConfig() nncf_config.update({ "input_info": { "sample_size": [1, 1, 4, 4] }, "compression": { "algorithm": "quantization", "export_to_onnx_standard_ops": should_be_onnx_standard } }) return nncf_config
def get_basic_quantization_config(model_size=4): config = NNCFConfig() config.update( Dict({ 'model': 'basic_quant_conv', 'input_info': { 'sample_size': [1, model_size, model_size, 1], }, 'compression': { 'algorithm': 'quantization', } })) return config
def get_quantization_config_without_range_init(model_size=4): config = NNCFConfig() config.update({ "model": "basic_quant_conv", "model_size": model_size, "input_info": { "sample_size": [1, 1, model_size, model_size], }, "compression": { "algorithm": "quantization", "initializer": { "range": { "num_init_samples": 0 } } } }) return config
def get_binarization_config() -> NNCFConfig: config = NNCFConfig() config.update({ "model": "resnet18", "input_info": { "sample_size": [1, *LeNet.INPUT_SIZE] }, "compression": [{ "algorithm": "binarization", "mode": "xnor", "params": { "activations_quant_start_epoch": 0, "weights_quant_start_epoch": 0 } }] }) return config
def get_config_for_test(batch_size=10, num_bn_adaptation_samples=100): config = NNCFConfig() config.update( Dict({ "compression": { "algorithm": "quantization", "initializer": { "batchnorm_adaptation": { "num_bn_adaptation_samples": num_bn_adaptation_samples, } } } })) dataset = get_dataset_for_test() config = register_default_init_args(config, dataset, batch_size) return config
def get_basic_pruning_config(model_size=8): config = NNCFConfig() config.update(Dict({ "model": "basic", "input_info": { "sample_size": [1, model_size, model_size, 1], }, "compression": { "algorithm": "filter_pruning", "pruning_init": 0.5, "params": { "prune_first_conv": True, } } })) return config
def get_basic_quantization_config(): config = NNCFConfig() config.update({ "model": "AlexNet", "input_info": { "sample_size": [1, 3, 32, 32], }, "compression": { "algorithm": "quantization", "quantize_inputs": True, "initializer": { "range": { "num_init_samples": 0 } } } }) return config
def get_basic_quantization_config(): config = NNCFConfig() config.update({ 'model': 'AlexNet', 'input_info': { 'sample_size': [1, 3, 32, 32], }, 'compression': { 'algorithm': 'quantization', 'quantize_inputs': True, 'initializer': { 'range': { 'num_init_samples': 0 } } } }) register_bn_adaptation_init_args(config) return config