def test_prune_model_2_layers(self): """ Punning two layers with 0.5 comp-ratio in MNIST""" # create tf.compat.v1.Session and initialize the weights and biases with zeros config = tf.compat.v1.ConfigProto() config.gpu_options.allow_growth = True # create session with graph sess = tf.compat.v1.Session(graph=tf.Graph(), config=config) with sess.graph.as_default(): # by default, model will be constructed in default graph _ = mnist_tf_model.create_model(data_format='channels_last') sess.run(tf.compat.v1.global_variables_initializer()) # Create a layer database orig_layer_db = LayerDatabase(model=sess, input_shape=(1, 28, 28, 1), working_dir=None) conv1 = orig_layer_db.find_layer_by_name('conv2d/Conv2D') conv2 = orig_layer_db.find_layer_by_name('conv2d_1/Conv2D') layer_comp_ratio_list = [ LayerCompRatioPair(conv1, Decimal(0.5)), LayerCompRatioPair(conv2, Decimal(0.5)) ] spatial_svd_pruner = SpatialSvdPruner() comp_layer_db = spatial_svd_pruner.prune_model(orig_layer_db, layer_comp_ratio_list, CostMetric.mac, trainer=None) conv1_a = comp_layer_db.find_layer_by_name('conv2d_a/Conv2D') conv1_b = comp_layer_db.find_layer_by_name('conv2d_b/Conv2D') # Weights shape [kh, kw, Nic, Noc] self.assertEqual([5, 1, 1, 2], conv1_a.module.inputs[1].get_shape().as_list()) self.assertEqual([1, 5, 2, 32], conv1_b.module.inputs[1].get_shape().as_list()) conv2_a = comp_layer_db.find_layer_by_name('conv2d_1_a/Conv2D') conv2_b = comp_layer_db.find_layer_by_name('conv2d_1_b/Conv2D') self.assertEqual([5, 1, 32, 53], conv2_a.module.inputs[1].get_shape().as_list()) self.assertEqual([1, 5, 53, 64], conv2_b.module.inputs[1].get_shape().as_list()) for layer in comp_layer_db: print("Layer: " + layer.name) print(" Module: " + str(layer.module.name)) tf.compat.v1.reset_default_graph() sess.close() # delete temp directory shutil.rmtree(str('./temp_meta/'))
def test_prune_model_tf_slim(self): """ Punning a model with tf slim api """ # create tf.compat.v1.Session and initialize the weights and biases with zeros config = tf.compat.v1.ConfigProto() config.gpu_options.allow_growth = True # create session with graph sess = tf.compat.v1.Session(graph=tf.Graph(), config=config) with sess.graph.as_default(): # by default, model will be constructed in default graph x = tf.compat.v1.placeholder(tf.float32, [1, 32, 32, 3]) _ = tf_slim_basic_model(x) sess.run(tf.compat.v1.global_variables_initializer()) conn_graph_orig = ConnectedGraph(sess.graph, ['Placeholder'], ['tf_slim_model/Softmax']) num_ops_orig = len(conn_graph_orig.get_all_ops()) # Create a layer database orig_layer_db = LayerDatabase(model=sess, input_shape=(1, 32, 32, 3), working_dir=None) conv1 = orig_layer_db.find_layer_by_name('Conv_1/Conv2D') conv1_bias = BiasUtils.get_bias_as_numpy_data(orig_layer_db.model, conv1.module) layer_comp_ratio_list = [LayerCompRatioPair(conv1, Decimal(0.5))] spatial_svd_pruner = SpatialSvdPruner() comp_layer_db = spatial_svd_pruner.prune_model(orig_layer_db, layer_comp_ratio_list, CostMetric.mac, trainer=None) # Check that svd added these ops _ = comp_layer_db.model.graph.get_operation_by_name('Conv_1_a/Conv2D') _ = comp_layer_db.model.graph.get_operation_by_name('Conv_1_b/Conv2D') conn_graph_new = ConnectedGraph(comp_layer_db.model.graph, ['Placeholder'], ['tf_slim_model/Softmax']) num_ops_new = len(conn_graph_new.get_all_ops()) self.assertEqual(num_ops_orig + 1, num_ops_new) bias_add_op = comp_layer_db.model.graph.get_operation_by_name( 'Conv_1_b/BiasAdd') conv_1_b_op = comp_layer_db.model.graph.get_operation_by_name( 'Conv_1_b/Conv2D') self.assertEqual( conn_graph_new._module_identifier.get_op_info(bias_add_op), conn_graph_new._module_identifier.get_op_info(conv_1_b_op)) self.assertTrue( np.array_equal( conv1_bias, BiasUtils.get_bias_as_numpy_data(comp_layer_db.model, conv_1_b_op)))
def test_prune_conv_no_bias(self): """ Test spatial svd on a conv layer with no bias """ # create tf.compat.v1.Session and initialize the weights and biases with zeros config = tf.compat.v1.ConfigProto() config.gpu_options.allow_growth = True # create session with graph sess = tf.compat.v1.Session(graph=tf.Graph(), config=config) with sess.graph.as_default(): # by default, model will be constructed in default graph inputs = tf.keras.Input(shape=( 32, 32, 3, )) x = tf.keras.layers.Conv2D(32, (3, 3), use_bias=False)(inputs) _ = tf.keras.layers.Flatten()(x) sess.run(tf.compat.v1.global_variables_initializer()) # Create a layer database orig_layer_db = LayerDatabase(model=sess, input_shape=(1, 32, 32, 3), working_dir=None) conv_op = orig_layer_db.find_layer_by_name('conv2d/Conv2D') layer_comp_ratio_list = [LayerCompRatioPair(conv_op, Decimal(0.5))] spatial_svd_pruner = SpatialSvdPruner() comp_layer_db = spatial_svd_pruner.prune_model(orig_layer_db, layer_comp_ratio_list, CostMetric.mac, trainer=None) # Check that svd added these ops _ = comp_layer_db.model.graph.get_operation_by_name('conv2d_a/Conv2D') conv2d_b_op = comp_layer_db.model.graph.get_operation_by_name( 'conv2d_b/Conv2D') reshape_op = comp_layer_db.model.graph.get_operation_by_name( 'flatten/Reshape') self.assertEqual(conv2d_b_op, reshape_op.inputs[0].op)
def test_select_per_layer_comp_ratios_with_spatial_svd_pruner(self): pruner = SpatialSvdPruner() eval_func = unittest.mock.MagicMock() rounding_algo = unittest.mock.MagicMock() eval_func.side_effect = [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 11, 21, 31, 35, 40, 45, 50, 55, 60 ] rounding_algo.round.side_effect = [ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9 ] # create tf.compat.v1.Session and initialize the weights and biases with zeros config = tf.compat.v1.ConfigProto() config.gpu_options.allow_growth = True # create session with graph sess = tf.compat.v1.Session(graph=tf.Graph(), config=config) with sess.graph.as_default(): # by default, model will be constructed in default graph _ = mnist_tf_model.create_model(data_format='channels_last') sess.run(tf.compat.v1.global_variables_initializer()) # Create a layer database layer_db = LayerDatabase(model=sess, input_shape=(1, 28, 28, 1), working_dir=None) selected_layers = [ layer for layer in layer_db if layer.module.type == 'Conv2D' ] layer_db.mark_picked_layers(selected_layers) url, process = start_bokeh_server_session(8006) bokeh_session = BokehServerSession(url=url, session_id="compression") # Instantiate child greedy_algo = comp_ratio_select.GreedyCompRatioSelectAlgo( layer_db=layer_db, pruner=pruner, cost_calculator=SpatialSvdCostCalculator(), eval_func=eval_func, eval_iterations=20, cost_metric=CostMetric.mac, target_comp_ratio=Decimal(0.4), num_candidates=10, use_monotonic_fit=True, saved_eval_scores_dict=None, comp_ratio_rounding_algo=rounding_algo, use_cuda=False, bokeh_session=bokeh_session) layer_comp_ratio_list, stats = greedy_algo.select_per_layer_comp_ratios( ) original_cost = SpatialSvdCostCalculator.compute_model_cost(layer_db) for layer in layer_db: if layer not in selected_layers: layer_comp_ratio_list.append(LayerCompRatioPair(layer, None)) compressed_cost = SpatialSvdCostCalculator.calculate_compressed_cost( layer_db, layer_comp_ratio_list, CostMetric.mac) actual_compression_ratio = compressed_cost.mac / original_cost.mac self.assertTrue( math.isclose(Decimal(0.3), actual_compression_ratio, abs_tol=0.8)) print('\n') for pair in layer_comp_ratio_list: print(pair) tf.compat.v1.reset_default_graph() sess.close() bokeh_session.server_session.close("test complete") os.killpg(os.getpgid(process.pid), signal.SIGTERM)
def test_eval_scores_with_spatial_svd_pruner(self): pruner = SpatialSvdPruner() eval_func = unittest.mock.MagicMock() eval_func.side_effect = [ 90, 80, 70, 60, 50, 40, 30, 20, 10, 91, 81, 71, 61, 51, 41, 31, 21, 11 ] # create tf.compat.v1.Session and initialize the weights and biases with zeros config = tf.compat.v1.ConfigProto() config.gpu_options.allow_growth = True # create session with graph sess = tf.compat.v1.Session(graph=tf.Graph(), config=config) with sess.graph.as_default(): # by default, model will be constructed in default graph _ = mnist_tf_model.create_model(data_format='channels_last') sess.run(tf.compat.v1.global_variables_initializer()) # Create a layer database layer_db = LayerDatabase(model=sess, input_shape=(1, 28, 28, 1), working_dir=None) layer1 = layer_db.find_layer_by_name('conv2d/Conv2D') layer2 = layer_db.find_layer_by_name('conv2d_1/Conv2D') layer_db.mark_picked_layers([layer1, layer2]) url, process = start_bokeh_server_session(8006) bokeh_session = BokehServerSession(url=url, session_id="compression") # Instantiate child greedy_algo = comp_ratio_select.GreedyCompRatioSelectAlgo( layer_db=layer_db, pruner=pruner, cost_calculator=SpatialSvdCostCalculator(), eval_func=eval_func, eval_iterations=20, cost_metric=CostMetric.mac, target_comp_ratio=0.5, num_candidates=10, use_monotonic_fit=True, saved_eval_scores_dict=None, comp_ratio_rounding_algo=None, use_cuda=False, bokeh_session=bokeh_session) dict = greedy_algo._compute_eval_scores_for_all_comp_ratio_candidates() print() print(dict) self.assertEqual(90, dict['conv2d/Conv2D'][Decimal('0.1')]) self.assertEqual(51, dict['conv2d_1/Conv2D'][Decimal('0.5')]) self.assertEqual(21, dict['conv2d_1/Conv2D'][Decimal('0.8')]) tf.compat.v1.reset_default_graph() sess.close() bokeh_session.server_session.close("test complete") os.killpg(os.getpgid(process.pid), signal.SIGTERM)
def test_prune_layer(self): """ Pruning single layer with 0.5 comp-ratio in MNIST""" # create tf.compat.v1.Session and initialize the weights and biases with zeros config = tf.compat.v1.ConfigProto() config.gpu_options.allow_growth = True # create session with graph sess = tf.compat.v1.Session(graph=tf.Graph(), config=config) with sess.graph.as_default(): # by default, model will be constructed in default graph _ = mnist_tf_model.create_model(data_format='channels_last') sess.run(tf.compat.v1.global_variables_initializer()) # Create a layer database orig_layer_db = LayerDatabase(model=sess, input_shape=(1, 28, 28, 1), working_dir=None) # Copy the db comp_layer_db = copy.deepcopy(orig_layer_db) conv1 = comp_layer_db.find_layer_by_name('conv2d/Conv2D') # before the splitting bias_op = get_succeeding_bias_op(conv1.module) for consumer in bias_op.outputs[0].consumers(): self.assertEqual(consumer.name, "conv2d/Relu") spatial_svd_pruner = SpatialSvdPruner() spatial_svd_pruner._prune_layer(orig_layer_db, comp_layer_db, conv1, 0.5, CostMetric.mac) conv2d_a_op = comp_layer_db.model.graph.get_operation_by_name( 'conv2d_a/Conv2D') conv2d_b_op = comp_layer_db.model.graph.get_operation_by_name( 'conv2d_b/Conv2D') conv2d_a_weight = WeightTensorUtils.get_tensor_as_numpy_data( comp_layer_db.model, conv2d_a_op) conv2d_b_weight = WeightTensorUtils.get_tensor_as_numpy_data( comp_layer_db.model, conv2d_b_op) conv1_a = comp_layer_db.find_layer_by_name('conv2d_a/Conv2D') conv1_b = comp_layer_db.find_layer_by_name('conv2d_b/Conv2D') # [Noc, Nic, kh, kw] self.assertEqual([2, 1, 5, 1], conv1_a.weight_shape) self.assertEqual([32, 2, 1, 5], conv1_b.weight_shape) # after the splitting bias_op = get_succeeding_bias_op(conv1_b.module) for consumer in bias_op.outputs[0].consumers(): self.assertEqual(consumer.name, "conv2d/Relu") # original layer should be not there in the database self.assertRaises( KeyError, lambda: comp_layer_db.find_layer_by_name('conv2d/Conv2D')) # check if the layer replacement is done correctly orig_conv_op = comp_layer_db.model.graph.get_operation_by_name( 'conv2d/Conv2D') bias_op = get_succeeding_bias_op(orig_conv_op) # consumers list should be empty consumers = [consumer for consumer in bias_op.outputs[0].consumers()] self.assertEqual(len(consumers), 0) # Check that weights loaded during svd pruning will stick after save and load new_sess = save_and_load_graph('./temp_meta/', comp_layer_db.model) conv2d_a_op = comp_layer_db.model.graph.get_operation_by_name( 'conv2d_a/Conv2D') conv2d_b_op = comp_layer_db.model.graph.get_operation_by_name( 'conv2d_b/Conv2D') conv2d_a_weight_after_save_load = WeightTensorUtils.get_tensor_as_numpy_data( comp_layer_db.model, conv2d_a_op) conv2d_b_weight_after_save_load = WeightTensorUtils.get_tensor_as_numpy_data( comp_layer_db.model, conv2d_b_op) self.assertTrue( np.array_equal(conv2d_a_weight, conv2d_a_weight_after_save_load)) self.assertTrue( np.array_equal(conv2d_b_weight, conv2d_b_weight_after_save_load)) tf.compat.v1.reset_default_graph() sess.close() new_sess.close() # delete temp directory shutil.rmtree(str('./temp_meta/'))
def create_spatial_svd_algo(cls, sess: tf.compat.v1.Session, working_dir: str, eval_callback: EvalFunction, eval_iterations, input_shape: Union[Tuple, List[Tuple]], cost_metric: CostMetric, params: SpatialSvdParameters, bokeh_session=None) -> CompressionAlgo: """ Factory method to construct SpatialSvdCompressionAlgo :param sess: Model, represented by a tf.compat.v1.Session, to compress :param working_dir: path to store temp meta and checkpoint files :param eval_callback: Evaluation callback for the model :param eval_iterations: Evaluation iterations :param input_shape: tuple or list of tuples of input shape to the model :param cost_metric: Cost metric (mac or memory) :param params: Spatial SVD compression parameters :param bokeh_session: The Bokeh Session to display plots :return: An instance of SpatialSvdCompressionAlgo """ # pylint: disable=too-many-arguments # pylint: disable=too-many-locals # Rationale: Factory functions unfortunately need to deal with a lot of parameters # Create a layer database layer_db = LayerDatabase(sess, input_shape, working_dir, starting_ops=params.input_op_names, ending_ops=params.output_op_names) use_cuda = False # Create a pruner pruner = SpatialSvdPruner() cost_calculator = SpatialSvdCostCalculator() comp_ratio_rounding_algo = RankRounder(params.multiplicity, cost_calculator) # Create a comp-ratio selection algorithm if params.mode == SpatialSvdParameters.Mode.auto: greedy_params = params.mode_params.greedy_params comp_ratio_select_algo = GreedyCompRatioSelectAlgo( layer_db, pruner, cost_calculator, eval_callback, eval_iterations, cost_metric, greedy_params.target_comp_ratio, greedy_params.num_comp_ratio_candidates, greedy_params.use_monotonic_fit, greedy_params.saved_eval_scores_dict, comp_ratio_rounding_algo, use_cuda, bokeh_session=bokeh_session) layer_selector = ConvNoDepthwiseLayerSelector() modules_to_ignore = params.mode_params.modules_to_ignore else: # Convert (module,comp-ratio) pairs to (layer,comp-ratio) pairs layer_comp_ratio_pairs = cls._get_layer_pairs( layer_db, params.mode_params.list_of_module_comp_ratio_pairs) comp_ratio_select_algo = ManualCompRatioSelectAlgo( layer_db, layer_comp_ratio_pairs, comp_ratio_rounding_algo, cost_metric=cost_metric) layer_selector = ManualLayerSelector(layer_comp_ratio_pairs) modules_to_ignore = [] # Create the overall Spatial SVD compression algorithm spatial_svd_algo = CompressionAlgo(layer_db, comp_ratio_select_algo, pruner, eval_callback, layer_selector, modules_to_ignore, cost_calculator, use_cuda) return spatial_svd_algo