How to use the sigpy.backend.Device function in sigpy

To help you get started, we’ve selected a few sigpy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mikgroup / sigpy / tests / test_linop.py View on Github external
def check_linop_adjoint(self, A,
                            device=backend.cpu_device, dtype=np.float):
        device = backend.Device(device)
        x = util.randn(A.ishape, dtype=dtype, device=device)
        y = util.randn(A.oshape, dtype=dtype, device=device)

        xp = device.xp
        with device:
            lhs = xp.vdot(A * x, y)
            rhs = xp.vdot(A.H.H * x, y)
            xp.testing.assert_allclose(lhs, rhs,
                                       atol=1e-5, rtol=1e-5)

            rhs = xp.vdot(x, A.H * y)
            xp.testing.assert_allclose(lhs, rhs,
                                       atol=1e-5, rtol=1e-5)
github mikgroup / sigpy / tests / test_pytorch.py View on Github external
import unittest
import numpy as np
import numpy.testing as npt
from sigpy import backend, config, linop, pytorch


if config.pytorch_enabled:
    import torch

    if __name__ == '__main__':
        unittest.main()

    devices = [backend.cpu_device]
    if config.cupy_enabled:
        devices.append(backend.Device(0))

    class TestPytorch(unittest.TestCase):

        def test_to_pytorch(self):
            for dtype in [np.float32, np.float64]:
                for device in devices:
                    with self.subTest(device=device, dtype=dtype):
                        xp = device.xp
                        array = xp.array([1, 2, 3], dtype=dtype)
                        tensor = pytorch.to_pytorch(array)
                        tensor[0] = 0
                        xp.testing.assert_allclose(array, [0, 2, 3])

        def test_to_pytorch_complex(self):
            for dtype in [np.complex64, np.complex128]:
                for device in devices:
github mikgroup / sigpy / tests / test_conv.py View on Github external
def test_convolve_adjoint_filter_valid(self):
        mode = 'valid'
        devices = [backend.cpu_device]
        if config.cupy_enabled:
            devices.append(backend.Device(0))

        ndim = 2
        for device in devices:
            xp = device.xp
            with device:
                for dtype in [np.float32, np.float64,
                              np.complex64, np.complex128]:
                    x = xp.ones([1, 3], dtype=dtype)
                    y = xp.ones([1, 1], dtype=dtype)
                    W = backend.to_device(
                        conv.convolve_adjoint_filter(
                            x, y, ndim, mode=mode), backend.cpu_device)
                    npt.assert_allclose(W, [[1, 1, 1]], atol=1e-5)

                    x = xp.ones([1, 3], dtype=dtype)
                    y = xp.ones([1, 2], dtype=dtype)
github mikgroup / sigpy / tests / test_linop.py View on Github external
import unittest
import pickle
import numpy as np
import numpy.testing as npt
from sigpy import backend, linop, util, config

if __name__ == '__main__':
    unittest.main()

dtypes = [np.float32, np.float64, np.complex64, np.complex128]
devices = [backend.cpu_device]
if config.cupy_enabled:
    devices.append(backend.Device(0))


class TestLinop(unittest.TestCase):

    def check_linop_unitary(self, A,
                            device=backend.cpu_device, dtype=np.float):
        device = backend.Device(device)
        x = util.randn(A.ishape, dtype=dtype, device=device)
        xp = device.xp
        with device:
            xp.testing.assert_allclose(A.H * A * x, x,
                                       atol=1e-5, rtol=1e-5)

    def check_linop_linear(self, A,
                           device=backend.cpu_device, dtype=np.float):
        device = backend.Device(device)
github mikgroup / sigpy / tests / test_conv.py View on Github external
def test_convolve_full(self):
        mode = 'full'
        devices = [backend.cpu_device]
        if config.cupy_enabled:
            devices.append(backend.Device(0))

        for device in devices:
            xp = device.xp
            with device:
                for dtype in [np.float32, np.float64,
                              np.complex64, np.complex128]:
                    x = util.dirac([1, 3], device=device, dtype=dtype)
                    W = xp.ones([1, 3], dtype=dtype)
                    y = backend.to_device(conv.convolve(
                        x, W, mode=mode), backend.cpu_device)
                    npt.assert_allclose(y, [[0, 1, 1, 1, 0]], atol=1e-5)

                    x = util.dirac([1, 3], device=device, dtype=dtype)
                    W = xp.ones([1, 2], dtype=dtype)
                    y = backend.to_device(conv.convolve(
                        x, W, mode=mode), backend.cpu_device)
github mikgroup / sigpy / tests / test_linop.py View on Github external
def check_linop_linear(self, A,
                           device=backend.cpu_device, dtype=np.float):
        device = backend.Device(device)
        a = util.randn([1], dtype=dtype, device=device)
        x = util.randn(A.ishape, dtype=dtype, device=device)
        y = util.randn(A.ishape, dtype=dtype, device=device)

        xp = device.xp
        with device:
            xp.testing.assert_allclose(A(a * x + y),
                                       a * A(x) + A(y),
                                       atol=1e-5, rtol=1e-5)
github mikgroup / sigpy / sigpy / backend.py View on Github external
def to_device(input, device=cpu_device):
    """Move input to device. Does not copy if same device.

    Args:
        input (array): Input.
        device (int or Device or cupy.Device): Output device.

    Returns:
        array: Output array placed in device.
    """
    idevice = get_device(input)
    odevice = Device(device)

    if idevice == odevice:
        return input

    if odevice == cpu_device:
        with idevice:
            return input.get()
    else:
        with odevice:
            return cp.asarray(input)
github mikgroup / sigpy / sigpy / backend.py View on Github external
return self.cpdevice.__enter__()

    def __exit__(self, *args):
        if self.id == -1:
            pass
        else:
            self.cpdevice.__exit__()

    def __repr__(self):
        if self.id == -1:
            return ''

        return self.cpdevice.__repr__()


cpu_device = Device(-1)


def get_array_module(array):
    """Gets an appropriate module from :mod:`numpy` or :mod:`cupy`.

    This is almost equivalent to :func:`cupy.get_array_module`. The differences
    are that this function can be used even if cupy is not available.

    Args:
        array: Input array.

    Returns:
        module: :mod:`cupy` or :mod:`numpy` is returned based on input.
    """
    if config.cupy_enabled:
        return cp.get_array_module(array)