def test_sparse_network(self, model_name, model_builder, input_size, algo, params): model = model_builder() from nncf.layers import NNCF_MODULES_MAP sparsifiable_modules = list(NNCF_MODULES_MAP.values()) ref_num_sparsed = len(get_all_modules_by_type(model, sparsifiable_modules)) config = get_empty_config(input_sample_size=input_size) config["compression"] = {"algorithm": algo, "params": params} compressed_model, compression_ctrl = create_compressed_model_and_algo_for_test(model, config) assert ref_num_sparsed == len(compression_ctrl.sparsified_module_info) check_model_graph(compressed_model, model_name, algo)
def check_correct_nncf_modules_replacement(model: NNCFNetwork, compressed_model: NNCFNetwork) \ -> Tuple[Dict[Scope, Module], Dict[Scope, Module]]: """ Check that all convolutions in model was replaced by NNCF convolution. :param model: original model :param compressed_model: compressed model :return: list of all convolutions in original model and list of all NNCF convolutions from compressed model """ NNCF_MODULES_REVERSED_MAP = {value: key for key, value in NNCF_MODULES_MAP.items()} original_modules = get_all_modules_by_type(model, list(NNCF_MODULES_MAP.values())) nncf_modules = get_all_modules_by_type(compressed_model.get_nncf_wrapped_model(), list(NNCF_MODULES_MAP.keys())) assert len(original_modules) == len(nncf_modules) print(original_modules, nncf_modules) for scope in original_modules.keys(): sparse_scope = deepcopy(scope) elt = sparse_scope.pop() # type: ScopeElement elt.calling_module_class_name = NNCF_MODULES_REVERSED_MAP[elt.calling_module_class_name] sparse_scope.push(elt) print(sparse_scope, nncf_modules) assert sparse_scope in nncf_modules return original_modules, nncf_modules
def test_sparse_network(self, desc: ModelDesc, algo): model = desc.model_builder() from nncf.layers import NNCF_MODULES_MAP sparsifiable_modules = list(NNCF_MODULES_MAP.values()) ref_num_sparsed = len( get_all_modules_by_type(model, sparsifiable_modules)) config = get_empty_config(input_sample_sizes=desc.input_sample_sizes) config["compression"] = {"algorithm": algo} compressed_model, compression_ctrl = \ create_compressed_model_and_algo_for_test(model, config, dummy_forward_fn=desc.dummy_forward_fn) assert ref_num_sparsed == len(compression_ctrl.sparsified_module_info) check_model_graph(compressed_model, desc.dot_filename, algo)
def test_sparse_network(self, model_name, model_builder, input_size, algo, params): model = model_builder() from nncf.layers import NNCF_MODULES_MAP sparsifiable_modules = list(NNCF_MODULES_MAP.values()) ref_num_sparsed = len(get_all_modules_by_type(model, sparsifiable_modules)) ctx = reset_context('test') config = get_empty_config(input_sample_size=input_size) config["compression"] = {"algorithm": algo, "params": params} compression_algo = create_compression_algorithm(model, config) assert ref_num_sparsed == len(compression_algo.sparsified_module_info) model = compression_algo.model with context('test') as c: _ = model(torch.zeros(input_size)) c.reset_scope_operator_call_counters() _ = model(torch.zeros(input_size)) check_graph(to_networkx(ctx), model_name, algo)
def test_sparse_network(self, model_name, model_builder, forward_fn_, algo, params): model = model_builder() from nncf.layers import NNCF_MODULES_MAP sparsifiable_modules = list(NNCF_MODULES_MAP.values()) ref_num_sparsed = len(get_all_modules_by_type(model, sparsifiable_modules)) ctx = reset_context('test') config = get_empty_config() config["compression"] = {"algorithm": algo, "params": params} compression_algo = create_compression_algorithm(model, config, dummy_forward_fn=forward_fn_) assert ref_num_sparsed == len(compression_algo.sparsified_module_info) model = compression_algo.model model.to(self.device) with context('test') as c: forward_fn_(model) c.reset_scope_operator_call_counters() forward_fn_(model) check_graph(ctx.graph, model_name, algo)
def test_sparse_quantize_network(self, model_name, model_builder, forward_fn_): model = model_builder() from nncf.layers import NNCF_MODULES_MAP sparsifiable_modules = list(NNCF_MODULES_MAP.values()) ref_num_sparsed = len(get_all_modules_by_type(model, sparsifiable_modules)) ctx = reset_context('test') config = get_empty_config() config["compression"] = [ {"algorithm": "rb_sparsity", "params": {}}, {"algorithm": "quantization", "params": {}} ] ctx = reset_context('orig') ctx = reset_context('quantized_graphs') compression_algo = create_compression_algorithm(model, config, dummy_forward_fn=forward_fn_) assert ref_num_sparsed == len(compression_algo.child_algos[0].sparsified_module_info) model = compression_algo.model model.to(self.device) forward_fn_(model) forward_fn_(model) check_graph(ctx.graph, model_name, "quantized_rb_sparsity")