Exemplo n.º 1
0
    def test_svd_automatic_rank_selection_mem_top_percent(self):

        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

        # Allocate the generator you wish to use to provide the network with data
        generator = tf_gen.TfRecordGenerator(
            tfrecords=[os.path.join('data', 'mnist', 'validation.tfrecords')],
            parser=MnistParser(data_inputs=['reshape_input']))

        # Allocate the SVD instance and compress the network
        svd = s.Svd(graph=os.path.join('models', 'mnist_save.meta'),
                    checkpoint=os.path.join('models', 'mnist_save'),
                    output_file=os.path.join('svd', 'svd_graph'),
                    layers=[],
                    num_ranks=5,
                    layer_selection_threshold=0.90,
                    metric=s.CostMetric.mac)

        stats = svd.compress_net(generator=generator,
                                 iterations=10,
                                 error_margin=2)

        stats.pretty_print(logger=logger)

        self.assertTrue(1.0 > stats.compressed_model_accuracy)
        self.assertTrue(
            stats.baseline_model_accuracy >= stats.compressed_model_accuracy)
        self.assertEqual(
            2,
            len(stats.per_rank_index[
                stats.best_rank_index].per_selected_layer))

        shutil.rmtree(str('./svd'))
Exemplo n.º 2
0
    def test_svd_automatic_rank_selection_mac_top_percent(self):

        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

        # Allocate the generator you wish to use to provide the network with data
        generator = tf_gen.TfRecordGenerator(
            tfrecords=[os.path.join('data', 'mnist', 'validation.tfrecords')],
            parser=MnistParser(data_inputs=['reshape_input']))

        # error_margin 85 % : forces to go through all 20 rank indices
        error_margin = 85

        # Allocate the SVD instance and compress the network
        svd = s.Svd(graph=os.path.join('models', 'mnist_save.meta'),
                    checkpoint=os.path.join('models', 'mnist_save'),
                    output_file=os.path.join('svd', 'svd_graph'),
                    layers=[],
                    num_ranks=5,
                    layer_selection_threshold=0.90,
                    metric=s.CostMetric.mac)

        stats = svd.compress_net(generator=generator,
                                 iterations=10,
                                 error_margin=error_margin)
        stats.pretty_print(logger=logger)

        shutil.rmtree(str('./svd'))
Exemplo n.º 3
0
    def test_svd_manual_rank_selection(self):

        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

        # Allocate the generator you wish to use to provide the network with data
        generator = tf_gen.TfRecordGenerator(
            tfrecords=[os.path.join('data', 'mnist', 'validation.tfrecords')],
            parser=MnistParser(data_inputs=['reshape_input']))

        # Only Compress Conv2d_1 and MatMul_1 with ranks 31 and 9 respectively
        # no_evaluation should be True in Manual mode

        layers = ['conv2/Conv2D', 'dense_1/MatMul']
        layer_ranks = [('conv2/Conv2D', 31), ('dense_1/MatMul', 9)]

        svd = s.Svd(graph=os.path.join('models', 'mnist_save.meta'),
                    checkpoint=os.path.join('models', 'mnist_save'),
                    output_file=os.path.join('svd', 'svd_graph'),
                    layers=layers,
                    layer_ranks=layer_ranks,
                    num_ranks=20,
                    no_evaluation=True,
                    metric=s.CostMetric.memory)

        stats = svd.compress_net(generator=generator,
                                 iterations=10,
                                 error_margin=2)
        stats.pretty_print(logger=logger)

        self.assertTrue(1.0 >= stats.compressed_model_accuracy)
        self.assertTrue(
            stats.baseline_model_accuracy >= stats.compressed_model_accuracy)

        shutil.rmtree(str('./svd'))
Exemplo n.º 4
0
def weight_svd_manual_mode(self):

    # Allocate the generator you wish to use to provide the network with data
    generator = tf_gen.TfRecordGenerator(
        tfrecords=[os.path.join('data', 'mnist', 'validation.tfrecords')],
        parser=MnistParser())

    # Only Compress Conv2d_1 and MatMul_1 with ranks 31 and 9 respectively
    # no_evaluation should be True in Manual mode

    layers = ['Conv2D_1', 'MatMul_1']
    layer_ranks = [('Conv2D_1', 31), ('MatMul_1', 9)]

    svd = s.Svd(graph=os.path.join('models', 'mnist_save.meta'),
                checkpoint=os.path.join('models', 'mnist_save'),
                output_file=os.path.join('svd', 'svd_graph'),
                layers=layers,
                layer_ranks=layer_ranks,
                num_ranks=20,
                no_evaluation=True,
                metric=s.CostMetric.memory)

    stats = svd.compress_net(generator=generator, iterations=10)

    stats.pretty_print()  # Print the stats for Weight SVD compression
Exemplo n.º 5
0
    def test_svd_layer_selection_without_mo(self):
        tf.compat.v1.reset_default_graph()
        svd = s.Svd(None, None, s.CostMetric.memory)
        svd._svd = create_autospec(pymo.Svd, instance=True)

        x = tf.compat.v1.placeholder(tf.float32, [None, 784], 'data')
        y_hat = model(x)
        sess = tf.compat.v1.Session()
        sess.run(tf.compat.v1.global_variables_initializer())

        svd._svd.GetCompressionType.return_value = pymo.SVD_COMPRESS_TYPE.TYPE_SINGLE
        svd._store_net_stats(sess)
        self.assertTrue(svd._svd.GetCompressionType.called)
Exemplo n.º 6
0
    def test_svd_layer_selection_with_mo(self):
        tf.compat.v1.reset_default_graph()
        svd = s.Svd(None, None, s.CostMetric.memory)

        x = tf.compat.v1.placeholder(tf.float32, [None, 784], 'data')
        y_hat = model(x)
        sess = tf.compat.v1.Session()
        sess.run(tf.compat.v1.global_variables_initializer())

        svd._store_net_stats(sess)

        layers = svd._svd.GetLayerNames()
        print("Layers added: ", layers)
Exemplo n.º 7
0
def weight_svd_auto_mode(self):

    # Allocate the generator you wish to use to provide the network with data
    generator = tf_gen.TfRecordGenerator(
        tfrecords=[os.path.join('data', 'mnist', 'validation.tfrecords')],
        parser=MnistParser())

    # Allocate the SVD instance and compress the network
    svd = s.Svd(graph=os.path.join('models', 'mnist_save.meta'),
                checkpoint=os.path.join('models', 'mnist_save'),
                output_file=os.path.join('svd', 'svd_graph'),
                layers=[],
                num_ranks=20,
                layer_selection_threshold=0.95,
                metric=s.CostMetric.memory)

    stats = svd.compress_net(generator=generator, iterations=10)

    stats.pretty_print()  # Print the stats for Weight SVD compression