Esempio n. 1
0
class TestRunnerMultiGPU(CleverHansTest):
    def setUp(self):
        super(TestRunnerMultiGPU, self).setUp()
        self.sess = tf.Session()

        inputs = []
        outputs = []
        self.niter = 10
        niter = self.niter
        # A Simple graph with `niter` sub-graphs.
        with tf.variable_scope(None, 'runner'):
            for i in range(niter):
                v = tf.get_variable('v%d' % i, shape=(100, 10))
                w = tf.get_variable('w%d' % i, shape=(100, 1))

                inputs += [{'v': v, 'w': w}]
                outputs += [{'v': v, 'w': w}]

        self.runner = RunnerMultiGPU(inputs, outputs, sess=self.sess)

    def help_test_runner(self, ninputs, niter):
        """
    Tests the MultiGPU runner by feeding in random Tensors for `ninputs`
    steps. Then validating the output after `niter-1` steps.
    """
        v_val = []
        w_val = []
        for i in range(ninputs):
            v_val += [np.random.rand(100, 10)]
            w_val += [np.random.rand(100, 1)]
            fvals = self.runner.run({'v': v_val[i], 'w': w_val[i]})
            self.assertTrue(len(fvals) == 0)
            self.assertFalse(self.runner.is_finished())

        for i in range(niter - ninputs - 1):
            self.assertFalse(self.runner.is_finished())
            fvals = self.runner.run()
            self.assertTrue(len(fvals) == 0)
            self.assertFalse(self.runner.is_finished())

        for i in range(ninputs):
            self.assertFalse(self.runner.is_finished())
            fvals = self.runner.run()
            self.assertTrue('v' in fvals and 'w' in fvals)
            self.assertTrue(np.allclose(fvals['v'], v_val[i]))
            self.assertTrue(np.allclose(fvals['w'], w_val[i]))

        self.assertTrue(self.runner.is_finished())

    def test_queue_full(self):
        self.help_test_runner(self.niter - 1, self.niter)

    def test_queue_half(self):
        self.help_test_runner(self.niter // 2, self.niter)
Esempio n. 2
0
class TestRunnerMultiGPU(CleverHansTest):
    def setUp(self):
        super(TestRunnerMultiGPU, self).setUp()
        self.sess = tf.Session()

        inputs = []
        outputs = []
        self.niter = 10
        niter = self.niter
        # A Simple graph with `niter` sub-graphs.
        with tf.variable_scope(None, 'runner'):
            for i in range(niter):
                v = tf.get_variable('v%d' % i, shape=(100, 10))
                w = tf.get_variable('w%d' % i, shape=(100, 1))

                inputs += [{'v': v, 'w': w}]
                outputs += [{'v': v, 'w': w}]

        self.runner = RunnerMultiGPU(inputs, outputs, sess=self.sess)

    def help_test_runner(self, ninputs, niter):
        """
        Tests the MultiGPU runner by feeding in random Tensors for `ninputs`
        steps. Then validating the output after `niter-1` steps.
        """
        v_val = []
        w_val = []
        for i in range(ninputs):
            v_val += [np.random.rand(100, 10)]
            w_val += [np.random.rand(100, 1)]
            fvals = self.runner.run({'v': v_val[i], 'w': w_val[i]})
            self.assertTrue(len(fvals) == 0)
            self.assertFalse(self.runner.is_finished())

        for i in range(niter-ninputs-1):
            self.assertFalse(self.runner.is_finished())
            fvals = self.runner.run()
            self.assertTrue(len(fvals) == 0)
            self.assertFalse(self.runner.is_finished())

        for i in range(ninputs):
            self.assertFalse(self.runner.is_finished())
            fvals = self.runner.run()
            self.assertTrue('v' in fvals and 'w' in fvals)
            self.assertTrue(np.allclose(fvals['v'], v_val[i]))
            self.assertTrue(np.allclose(fvals['w'], w_val[i]))

        self.assertTrue(self.runner.is_finished())

    def test_queue_full(self):
        self.help_test_runner(self.niter-1, self.niter)

    def test_queue_half(self):
        self.help_test_runner(self.niter//2, self.niter)
Esempio n. 3
0
  def generate_np(self, x_val, **kwargs):
    """
    Facilitates testing this attack.
    """
    fixed, feedable, hash_key = self.construct_variables(kwargs)

    if hash_key not in self.graphs:
      with tf.variable_scope(None, 'attack_%d' % len(self.graphs)):
        # x is a special placeholder we always want to have
        with tf.device('/gpu:0'):
          x = tf.placeholder(tf.float32, shape=x_val.shape, name='x')

        inputs, outputs = self.generate(x, **kwargs)

        from runner import RunnerMultiGPU
        runner = RunnerMultiGPU(inputs, outputs, sess=self.sess)
        self.graphs[hash_key] = runner

    runner = self.graphs[hash_key]
    feed_dict = {'x': x_val}
    for name in feedable:
      feed_dict[name] = feedable[name]
    fvals = runner.run(feed_dict)
    while not runner.is_finished():
      fvals = runner.run()

    return fvals['adv_x']
Esempio n. 4
0
    def generate_np(self, x_val, **kwargs):
        """
        Facilitates testing this attack.
        """
        fixed, feedable, hash_key = self.construct_variables(kwargs)

        if hash_key not in self.graphs:
            with tf.variable_scope(None, 'attack_%d' % len(self.graphs)):
                # x is a special placeholder we always want to have
                with tf.device('/gpu:0'):
                    x = tf.placeholder(tf.float32, shape=x_val.shape, name='x')

                inputs, outputs = self.generate(x, **kwargs)

                from runner import RunnerMultiGPU
                runner = RunnerMultiGPU(inputs, outputs, sess=self.sess)
                self.graphs[hash_key] = runner

        runner = self.graphs[hash_key]
        feed_dict = {'x': x_val}
        for name in feedable:
            feed_dict[name] = feedable[name]
        fvals = runner.run(feed_dict)
        while not runner.is_finished():
            fvals = runner.run()

        return fvals['adv_x']