Esempio n. 1
0
    def test_performance(self):
        c = random.rand(2000, 2000)
        x = Variable([2000, 2000])
        K = np.abs(random.rand(9, 9))
        G = CompGraph(
            vstack([subsample((conv_nofft(K, x) - c) * 5, [2, 4]), x * 10]))
        xtest1 = random.rand(2000 * 2000).astype(np.float32)
        ytest1 = np.zeros(G.output_size, dtype=np.float32)
        t1_cpu = time.time()
        for i in range(10):
            ytest1 = G.forward(xtest1, ytest1)
        t2_cpu = time.time()

        xtest = gpuarray.to_gpu(xtest1.astype(np.float32))
        ytest = gpuarray.to_gpu(ytest1.astype(np.float32))
        t1_gpu = time.time()
        for i in range(10):
            ytest = G.forward_cuda(xtest, ytest)
        t2_gpu = time.time()

        t_cpu = t2_cpu - t1_cpu
        t_gpu = t2_gpu - t1_gpu
        logging.info("Forward timing: cpu=%.2f ms gpu=%.2f ms factor=%.3f" %
                     (t_cpu, t_gpu, t_gpu / t_cpu))
        self.assertTrue(t_gpu < t_cpu)

        t1_cpu = time.time()
        for i in range(10):
            xtest1 = G.adjoint(ytest1, xtest1)
        t2_cpu = time.time()

        t1_gpu = time.time()
        for i in range(10):
            xtest = G.adjoint_cuda(ytest, xtest)
        t2_gpu = time.time()

        t_cpu = t2_cpu - t1_cpu
        t_gpu = t2_gpu - t1_gpu
        logging.info("Adjoint timing: cpu=%.2f ms gpu=%.2f ms factor=%.3f" %
                     (t_cpu, t_gpu, t_gpu / t_cpu))
        self.assertTrue(t_gpu < t_cpu)
    def test_performance(self):
        c = random.rand(2000,2000)
        x = Variable([2000,2000])
        K = np.abs(random.rand(9,9))
        G = CompGraph(vstack([ subsample((conv_nofft(K, x) -c)*5, [2,4]), x*10 ]))
        xtest1 = random.rand(2000*2000).astype(np.float32)
        ytest1 = np.zeros(G.output_size, dtype=np.float32)
        t1_cpu = time.time()
        for i in range(10):
            ytest1 = G.forward(xtest1, ytest1)
        t2_cpu = time.time()

        xtest = gpuarray.to_gpu(xtest1.astype(np.float32))
        ytest = gpuarray.to_gpu(ytest1.astype(np.float32))
        t1_gpu = time.time()
        for i in range(10):
            ytest = G.forward_cuda(xtest, ytest)
        t2_gpu = time.time()

        t_cpu = t2_cpu - t1_cpu
        t_gpu = t2_gpu - t1_gpu
        logging.info("Forward timing: cpu=%.2f ms gpu=%.2f ms factor=%.3f" % (t_cpu, t_gpu, t_gpu/t_cpu))
        self.assertTrue(t_gpu < t_cpu)

        t1_cpu = time.time()
        for i in range(10):
            xtest1 = G.adjoint(ytest1, xtest1)
        t2_cpu = time.time()

        t1_gpu = time.time()
        for i in range(10):
            xtest = G.adjoint_cuda(ytest, xtest)
        t2_gpu = time.time()

        t_cpu = t2_cpu - t1_cpu
        t_gpu = t2_gpu - t1_gpu
        logging.info("Adjoint timing: cpu=%.2f ms gpu=%.2f ms factor=%.3f" % (t_cpu, t_gpu, t_gpu/t_cpu))
        self.assertTrue(t_gpu < t_cpu)
    def _generic_check_adjoint(self, f, inshape, outshape, s,
                               ntests = 50, eps=1e-5, verbose=False,
                               in_out_sample = None):
        """
        Generic tests used for all comp graph tests on a parametrizable function f
        """
        x = Variable(inshape)
        func = f(x)
        if not type(func) is tuple:
            func = (func,)
        G = CompGraph(vstack(func))

        nin = functools.reduce(lambda x,y: x*y, inshape, 1)
        nout = functools.reduce(lambda x,y: x*y, outshape, 1)

        if not in_out_sample is None:
            # check against the given in/out samples
            x1 = in_out_sample[0] # forward in
            y1s = in_out_sample[1] # forward out
            y2 = in_out_sample[2] # adjoint in
            x2s = in_out_sample[3] # adjoint out

            y1a = G.forward_cuda(gpuarray.to_gpu(x1.astype(np.float32)),gpuarray.to_gpu(y1s.astype(np.float32))).get()
            #print(y1s)
            #print(y1a)
            self.assertTrue(np.amax(np.abs(y1a-y1s)) < eps)

            x2a = G.adjoint_cuda(gpuarray.to_gpu(y2.astype(np.float32)),gpuarray.to_gpu(x2s.astype(np.float32))).get()
            self.assertTrue(np.amax(np.abs(x2a-x2s)) < eps)

        # test with random data that the forward/adjoint operators are consistent
        maxerr = 0.0
        random.seed(0) # make tests reproducable
        for tidx in range(ntests):
            x1 = random.rand(nin).astype(np.float32)
            y2 = random.rand(nout).astype(np.float32)
            y1 = np.zeros(nout, dtype=np.float32)
            x2 = np.zeros(nin, dtype=np.float32)

            if verbose: print("forward: ", end="")
            y1 = G.forward_cuda(gpuarray.to_gpu(x1),gpuarray.to_gpu(y1),printt=verbose).get()
            if verbose: print("adjoint: ", end="")
            x2 = G.adjoint_cuda(gpuarray.to_gpu(y2),gpuarray.to_gpu(x2),printt=verbose).get()

            self.assertTrue(not np.all(y1 == 0) and not np.all(x2 == 0))

            y1o = G.forward(x1,y1.copy())
            x2o = G.adjoint(y2,x2.copy())
            erro = abs(np.dot(x1.flatten().astype(np.float64), x2o.flatten().astype(np.float64)) -
                       np.dot(y1o.flatten().astype(np.float64), y2.flatten().astype(np.float64)))

            err = abs(np.dot(x1.flatten().astype(np.float64),x2.flatten().astype(np.float64)) -
                      np.dot(y1.flatten().astype(np.float64),y2.flatten().astype(np.float64)))
            if err > maxerr:
                maxerr = err
            if verbose and err > eps:
                print("forward CUDA code:")
                print(G.cuda_forward_subgraphs.cuda_code)
                print("backward CUDA code:")
                print(G.cuda_adjoint_subgraphs.cuda_code)
                print("x1\n",np.reshape(x1, inshape))
                print("y1\n",np.reshape(y1, outshape))
                print("y1o\n",np.reshape(y1o, outshape))
                print("y2\n",np.reshape(y2, outshape))
                print("x2\n",np.reshape(x2, inshape))
                print("x2o\n",np.reshape(x2o, inshape))
                print("(%d) Adjoint test (%s): gpu: %f, nogpu: %f" %(tidx,s,err,erro))
                print("max(abs(y1-y1o)): %f" % (np.amax(np.abs(y1-y1o))))
                print("max(abs(x2-x2o)): %f" % (np.amax(np.abs(x2-x2o))))
            self.assertTrue(err <= eps)
        if verbose: print("%s passed %d tests. Max adjoint test error: %f" % (s, ntests, maxerr))
Esempio n. 4
0
    def _test_pock_chambolle(self, impl):
        """Test pock chambolle algorithm.
        """
        #print()
        #print("----------------------",impl,"-------------------------")
        if impl == 'pycuda':
            kw = {'adapter': PyCudaAdapter()}
        else:
            kw = {}
        X = px.Variable((10, 5))
        B = np.reshape(np.arange(50), (10, 5))
        prox_fns = [px.sum_squares(X, b=B)]
        sltn = pc.solve(prox_fns, [],
                        1.0,
                        1.0,
                        1.0,
                        eps_rel=1e-5,
                        eps_abs=1e-5,
                        **kw)
        self.assertItemsAlmostEqual(X.value, B, eps=2e-2)
        self.assertAlmostEqual(sltn, 0)

        prox_fns = [px.norm1(X, b=B, beta=2)]
        sltn = pc.solve(prox_fns, [],
                        1.0,
                        1.0,
                        1.0,
                        eps_rel=1e-5,
                        eps_abs=1e-5,
                        **kw)
        self.assertItemsAlmostEqual(X.value, B / 2., eps=2e-2)
        self.assertAlmostEqual(sltn, 0, eps=2e-2)

        prox_fns = [px.norm1(X), px.sum_squares(X, b=B)]
        #print("----------------------------------------------------")
        #print("----------------------------------------------------")
        sltn = pc.solve(prox_fns, [],
                        0.5,
                        1.0,
                        1.0,
                        eps_rel=1e-5,
                        eps_abs=1e-5,
                        conv_check=1,
                        **kw)

        cvx_X = cvx.Variable((10, 5))
        cost = cvx.sum_squares(cvx_X - B) + cvx.norm(cvx_X, 1)
        prob = cvx.Problem(cvx.Minimize(cost))
        prob.solve()
        self.assertItemsAlmostEqual(X.value, cvx_X.value, eps=2e-2)
        self.assertAlmostEqual(sltn, prob.value)

        psi_fns, omega_fns = pc.partition(prox_fns)
        sltn = pc.solve(psi_fns,
                        omega_fns,
                        0.5,
                        1.0,
                        1.0,
                        eps_abs=1e-5,
                        eps_rel=1e-5,
                        **kw)
        self.assertItemsAlmostEqual(X.value, cvx_X.value, eps=2e-2)
        self.assertAlmostEqual(sltn, prob.value)

        # With linear operators.
        kernel = np.array([1, 2, 3])
        kernel_mat = np.matrix("2 1 3; 3 2 1; 1 3 2")
        x = px.Variable(3)
        b = np.array([-41, 413, 2])
        prox_fns = [px.nonneg(x), px.sum_squares(px.conv(kernel, x), b=b)]
        sltn = pc.solve(prox_fns, [],
                        0.1,
                        0.1,
                        1.0,
                        max_iters=3000,
                        eps_abs=1e-5,
                        eps_rel=1e-5,
                        **kw)
        cvx_X = cvx.Variable(3)
        cost = cvx.norm(kernel_mat * cvx_X - b)
        prob = cvx.Problem(cvx.Minimize(cost), [cvx_X >= 0])
        prob.solve()
        self.assertItemsAlmostEqual(x.value, cvx_X.value, eps=2e-2)

        psi_fns, omega_fns = pc.partition(prox_fns)
        sltn = pc.solve(psi_fns,
                        omega_fns,
                        0.1,
                        0.1,
                        1.0,
                        max_iters=3000,
                        eps_abs=1e-5,
                        eps_rel=1e-5,
                        **kw)
        self.assertItemsAlmostEqual(x.value, cvx_X.value, eps=2e-2)

        # TODO
        # Multiple variables.
        x = px.Variable(1)
        y = px.Variable(1)
        prox_fns = [
            px.nonneg(x),
            px.sum_squares(vstack([x, y]), b=np.arange(2))
        ]
        sltn = pc.solve(prox_fns, [prox_fns[-1]],
                        0.1,
                        0.1,
                        1.0,
                        max_iters=3000,
                        eps_abs=1e-5,
                        eps_rel=1e-5,
                        try_diagonalize=False)
        self.assertItemsAlmostEqual(x.value, [0])
        self.assertItemsAlmostEqual(y.value, [1])

        sltn = pc.solve(prox_fns, [prox_fns[-1]],
                        0.1,
                        0.1,
                        1.0,
                        max_iters=3000,
                        eps_abs=1e-5,
                        eps_rel=1e-5,
                        try_diagonalize=True)
        self.assertItemsAlmostEqual(x.value, [0])
        self.assertItemsAlmostEqual(y.value, [1])
Esempio n. 5
0
    def _generic_check_adjoint(self,
                               f,
                               inshape,
                               outshape,
                               s,
                               ntests=50,
                               eps=1e-5,
                               verbose=False,
                               in_out_sample=None):
        """
        Generic tests used for all comp graph tests on a parametrizable function f
        """
        x = Variable(inshape)
        func = f(x)
        if not type(func) is tuple:
            func = (func, )
        G = CompGraph(vstack(func))

        nin = functools.reduce(lambda x, y: x * y, inshape, 1)
        nout = functools.reduce(lambda x, y: x * y, outshape, 1)

        if not in_out_sample is None:
            # check against the given in/out samples
            x1 = in_out_sample[0]  # forward in
            y1s = in_out_sample[1]  # forward out
            y2 = in_out_sample[2]  # adjoint in
            x2s = in_out_sample[3]  # adjoint out

            y1a = G.forward_cuda(gpuarray.to_gpu(x1.astype(np.float32)),
                                 gpuarray.to_gpu(y1s.astype(
                                     np.float32))).get()
            #print(y1s)
            #print(y1a)
            self.assertTrue(np.amax(np.abs(y1a - y1s)) < eps)

            x2a = G.adjoint_cuda(gpuarray.to_gpu(y2.astype(np.float32)),
                                 gpuarray.to_gpu(x2s.astype(
                                     np.float32))).get()
            self.assertTrue(np.amax(np.abs(x2a - x2s)) < eps)

        # test with random data that the forward/adjoint operators are consistent
        maxerr = 0.0
        random.seed(0)  # make tests reproducable
        for tidx in range(ntests):
            x1 = random.rand(nin).astype(np.float32)
            y2 = random.rand(nout).astype(np.float32)
            y1 = np.zeros(nout, dtype=np.float32)
            x2 = np.zeros(nin, dtype=np.float32)

            if verbose:
                print("forward: ", end="")
            y1 = G.forward_cuda(gpuarray.to_gpu(x1),
                                gpuarray.to_gpu(y1),
                                printt=verbose).get()
            if verbose:
                print("adjoint: ", end="")
            x2 = G.adjoint_cuda(gpuarray.to_gpu(y2),
                                gpuarray.to_gpu(x2),
                                printt=verbose).get()

            self.assertTrue(not np.all(y1 == 0) and not np.all(x2 == 0))

            y1o = G.forward(x1, y1.copy())
            x2o = G.adjoint(y2, x2.copy())
            erro = abs(
                np.dot(x1.flatten().astype(np.float64),
                       x2o.flatten().astype(np.float64)) -
                np.dot(y1o.flatten().astype(np.float64),
                       y2.flatten().astype(np.float64)))

            err = abs(
                np.dot(x1.flatten().astype(np.float64),
                       x2.flatten().astype(np.float64)) -
                np.dot(y1.flatten().astype(np.float64),
                       y2.flatten().astype(np.float64)))
            if err > maxerr:
                maxerr = err
            if verbose and err > eps:
                print("forward CUDA code:")
                print(G.cuda_forward_subgraphs.cuda_code)
                print("backward CUDA code:")
                print(G.cuda_adjoint_subgraphs.cuda_code)
                print("x1\n", np.reshape(x1, inshape))
                print("y1\n", np.reshape(y1, outshape))
                print("y1o\n", np.reshape(y1o, outshape))
                print("y2\n", np.reshape(y2, outshape))
                print("x2\n", np.reshape(x2, inshape))
                print("x2o\n", np.reshape(x2o, inshape))
                print("(%d) Adjoint test (%s): gpu: %f, nogpu: %f" %
                      (tidx, s, err, erro))
                print("max(abs(y1-y1o)): %f" % (np.amax(np.abs(y1 - y1o))))
                print("max(abs(x2-x2o)): %f" % (np.amax(np.abs(x2 - x2o))))
            self.assertTrue(err <= eps)
        if verbose:
            print("%s passed %d tests. Max adjoint test error: %f" %
                  (s, ntests, maxerr))