Пример #1
0
def test_hist(nbin_offset_dim_dtype_inp, backend_pair):
    """
    Compare the nervanagpu and nervanacpu hist implementation to the reference
    implementation above.

    Parameterized test case, uses pytest_generate_test to enumerate dim_dtype_inp
    tuples that drive the test.
    """

    (nbins, offset), dim, dtype, (name, inp_gen) = nbin_offset_dim_dtype_inp

    gpuflag = (check_gpu.get_compute_capability(0) >= 3.0)
    if gpuflag is False:
        raise RuntimeError("Device does not have CUDA compute capability 3.0 or greater")

    ng, nc = backend_pair
    ng.set_hist_buffers(nbins, offset)
    nc.set_hist_buffers(nbins, offset)

    np_inp = inp_gen(dim).astype(dtype)
    np_hist = ref_hist(np_inp, nbins=nbins, offset=offset)
    for be in [ng, nc]:
        be_inp = be.array(np_inp, dtype=dtype)
        be_hist = be_inp.hist(name)
        assert tensors_allclose(np_hist, be_hist)
Пример #2
0
def test_hist(nbin_offset_dim_dtype_inp, backend_pair):
    """
    Compare the nervanagpu and nervanacpu hist implementation to the reference
    implementation above.

    Parameterized test case, uses pytest_generate_test to enumerate dim_dtype_inp
    tuples that drive the test.
    """

    (nbins, offset), dim, dtype, (name, inp_gen) = nbin_offset_dim_dtype_inp

    gpuflag = (check_gpu.get_compute_capability(0) >= 3.0)
    if gpuflag is False:
        raise RuntimeError(
            "Device does not have CUDA compute capability 3.0 or greater")

    ng, nc = backend_pair
    ng.set_hist_buffers(nbins, offset)
    nc.set_hist_buffers(nbins, offset)

    np_inp = inp_gen(dim).astype(dtype)
    np_hist = ref_hist(np_inp, nbins=nbins, offset=offset)
    for be in [ng, nc]:
        be_inp = be.array(np_inp, dtype=dtype)
        be_hist = be_inp.hist(name)
        assert tensors_allclose(np_hist, be_hist)
Пример #3
0
def test_edge_cases(backend_pair):
    """
    Test several edge cases related to min/max bin, and rounding.

    Also test backend dump_hist_data functionality.
    """
    gpuflag = (check_gpu.get_compute_capability(0) >= 3.0)
    if gpuflag is False:
        raise RuntimeError("Device does not have CUDA compute capability 3.0 or greater")
    ng, nc = backend_pair

    # edge case test
    np_ref = dict()
    inputs = [
        ("edges", np.array([2 ** -48, 2 ** 15], dtype=np.float32)),
        ("rounding", np.array([2 ** 5, 63.99998856, 2 ** 6, 2 ** -3, 2 ** -4,
                               0.11262291, 92.22483826], dtype=np.float32)),
        ("fp16 rounding", np.array([45.21875], dtype=np.float16))
    ]
    for tag, inp in inputs:
        np_ref[tag] = ref_hist(inp)
        for be in [ng, nc]:
            be_inp = be.array(inp)
            be_hist = be_inp.hist(tag)
            assert tensors_allclose(np_ref[tag], be_hist), tag + str(be)

    # dump_hist_data test
    for be in [ng, nc]:
        be_hist_data, be_hist_map = be.dump_hist_data()
        for tag, inp in inputs:
            be_data = be_hist_data[be_hist_map[tag]]
            assert tensors_allclose(np_ref[tag], be_data), tag + str(be)
Пример #4
0
def test_edge_cases(backend_pair):
    """
    Test several edge cases related to min/max bin, and rounding.

    Also test backend dump_hist_data functionality.
    """
    gpuflag = (check_gpu.get_compute_capability(0) >= 3.0)
    if gpuflag is False:
        raise RuntimeError(
            "Device does not have CUDA compute capability 3.0 or greater")
    ng, nc = backend_pair

    # edge case test
    np_ref = dict()
    inputs = [
        ("edges", np.array([2**-48, 2**15], dtype=np.float32)),
        ("rounding",
         np.array(
             [2**5, 63.99998856, 2**6, 2**-3, 2**-4, 0.11262291, 92.22483826],
             dtype=np.float32)),
        ("fp16 rounding", np.array([45.21875], dtype=np.float16))
    ]
    for tag, inp in inputs:
        np_ref[tag] = ref_hist(inp)
        for be in [ng, nc]:
            be_inp = be.array(inp)
            be_hist = be_inp.hist(tag)
            assert tensors_allclose(np_ref[tag], be_hist), tag + str(be)

    # dump_hist_data test
    for be in [ng, nc]:
        be_hist_data, be_hist_map = be.dump_hist_data()
        for tag, inp in inputs:
            be_data = be_hist_data[be_hist_map[tag]]
            assert tensors_allclose(np_ref[tag], be_data), tag + str(be)
Пример #5
0
def test_gpu_edge_cases(backend_gpu):
    """
    Test several edge cases related to min/max bin, and rounding.

    Also test backend dump_hist_data functionality.
    """
    gpuflag = (check_gpu.get_compute_capability(0) >= 3.0)
    if gpuflag is False:
        raise RuntimeError(
            "Device does not have CUDA compute capability 3.0 or greater")
    edge_cases_helper(backend_gpu)
Пример #6
0
def test_gpu_hist(nbin_offset_dim_dtype_inp, backend_gpu):
    """
    Compare the nervanagpu and nervanacpu hist implementation to the reference
    implementation above.

    Parameterized test case, uses pytest_generate_test to enumerate dim_dtype_inp
    tuples that drive the test.
    """
    gpuflag = (check_gpu.get_compute_capability(0) >= 3.0)
    if gpuflag is False:
        raise RuntimeError(
            "Device does not have CUDA compute capability 3.0 or greater")

    hist_helper(nbin_offset_dim_dtype_inp, backend_gpu)
Пример #7
0
    def __enter__(self):
        """
        Construct and return a backend instance of the appropriate type based on
        the arguments given. With no parameters, a single CPU core, float32
        backend is returned.

        Arguments:
            datatype (dtype): Default tensor data type. CPU backend supports np.float64, np.float32,
                              and np.float16; GPU backend supports np.float32 and np.float16.
            batch_size (int): Set the size the data batches.
            stochastic_round (int/bool, optional): Set this to True or an integer to implent
                                                   stochastic rounding. If this is False rounding will
                                                   be to nearest. If True will perform stochastic
                                                   rounding using default bit width. If set to an
                                                   integer will round to that number of bits.
                                                   Only affects the gpu backend.
            device_id (numeric, optional): Set this to a numeric value which can be used to select
                                           device on which to run the process
            compat_mode (str, optional): if this is set to 'caffe' then the conv and pooling
                                         layer output sizes will match that of caffe as will
                                         the dropout layer implementation
            deterministic (bool, optional): if set to true, all operations will be done deterministically.
            cache_dir (str, optional): a location for the backend to cache tuning parameters.

        Returns:
            Backend: newly constructed backend instance of the specifed type.

        Notes:
            * Attempts to construct a GPU instance without a CUDA capable card or without nervanagpu
              package installed will cause the program to display an error message and exit.
        """
        # check nvcc
        self.gpuflag = (check_gpu.get_compute_capability(self.device_id) >= 3.0)
        if self.gpuflag is False:
            raise RuntimeError("Device " + str(self.device_id) + " does not have CUDA compute " +
                               "capability 3.0 or greater")
        # init gpu
        self.be = NervanaGPU(default_dtype=self.datatype,
                        stochastic_round=self.stochastic_round,
                        device_id=self.device_id,
                        compat_mode=self.compat_mode,
                        cache_dir=self.cache_dir)

        self.be.bsz = self.batch_size
        return self.be
Пример #8
0
def gen_backend(backend='cpu',
                rng_seed=None,
                default_dtype=np.float32,
                batch_size=0,
                stochastic_round=False,
                device_id=0):
    """
    Construct and return a backend instance of the appropriate type based on
    the arguments given. With no parameters, a single CPU core, float32
    backend is returned.

    Arguments:
        backend (string, optional): 'cpu' or 'gpu'.
        rng_seed (numeric, optional): Set this to a numeric value which can be
                                      used to seed the random number generator
                                      of the instantiated backend.  Defaults to
                                      None, which doesn't explicitly seed (so
                                      each run will be different)
        default_dtype (dtype): Default tensor data type. CPU backend supports
                               np.float64, np.float32 and np.float16; GPU
                               backend supports np.float32 and np.float16.
        batch_size (int): Set the size the data batches.
        stochastic_round (int/bool, optional): Set this to True or an integer
                                               to implent stochastic rounding.
                                               If this is False rounding will
                                               be to nearest.
                                               If True will perform stochastic
                                               rounding using default bit width.
                                               If set to an integer will round
                                               to that number of bits.
                                               Only affects the gpu backend.
        device_id (numeric, optional): Set this to a numeric value which can be
                                       used to select which device to run the
                                       process on

    Returns:
        Backend: newly constructed backend instance of the specifed type.

    Notes:
        * Attempts to construct a GPU instance without a CUDA capable card or
          without nervanagpu package installed will cause the
          program to display an error message and exit.
    """
    logger = logging.getLogger(__name__)

    if NervanaObject.be is not None:
        # backend was already generated
        # clean it up first
        cleanup_backend()
    else:
        # at exit from python force cleanup of backend
        # only register this function once, will use
        # NervanaObject.be instead of a global
        atexit.register(cleanup_backend)

    if backend == 'cpu' or backend is None:
        from neon.backends.nervanacpu import NervanaCPU
        be = NervanaCPU(rng_seed=rng_seed, default_dtype=default_dtype)
    elif backend == 'gpu':
        gpuflag = False
        # check nvcc
        from neon.backends.util import check_gpu
        gpuflag = (check_gpu.get_compute_capability(device_id) >= 5.0)
        if gpuflag is False:
            raise RuntimeError("Device " + str(device_id) +
                               " does not have CUDA compute " +
                               "capability 5.0 or greater")
        from neon.backends.nervanagpu import NervanaGPU
        # init gpu
        be = NervanaGPU(rng_seed=rng_seed,
                        default_dtype=default_dtype,
                        stochastic_round=stochastic_round,
                        device_id=device_id)
    elif backend == 'mgpu':
        raise NotImplementedError("mgpu will be ready soon")
    else:
        raise ValueError("backend must be one of " "('cpu', 'gpu', 'mgpu')")

    logger.info("Backend: {}, RNG seed: {}".format(backend, rng_seed))

    NervanaObject.be = be
    be.bsz = batch_size
    return be
Пример #9
0
    def setup_default_args(self):
        """
        Setup the default arguments used by neon
        """

        self.add_argument("--version", action="version", version=neon_version)
        self.add_argument(
            "-c",
            "--config",
            is_config_file=True,
            help="Read values for these arguments from the " "configuration file specified here first.",
        )
        self.add_argument(
            "-v",
            "--verbose",
            action="count",
            default=0,
            help="verbosity level.  Add multiple v's to " "further increase verbosity",
        )
        # we store the negation of no_progress_bar in args.progress_bar during
        # parsing
        self.add_argument(
            "--no_progress_bar",
            action="store_true",
            help="suppress running display of progress bar and " "training loss",
        )

        # runtime specifc options
        rt_grp = self.add_argument_group("runtime")
        rt_grp.add_argument(
            "-w",
            "--data_dir",
            default=os.path.join(self.work_dir, "data"),
            help="working directory in which to cache " "downloaded and preprocessed datasets",
        )
        rt_grp.add_argument(
            "-e", "--epochs", type=int, default=10, help="number of complete passes over the dataset to run"
        )
        rt_grp.add_argument("-s", "--save_path", type=str, help="file path to save model snapshots")
        rt_grp.add_argument(
            "--serialize", nargs="?", type=int, default=0, const=1, metavar="N", help="serialize model every N epochs"
        )
        rt_grp.add_argument("--model_file", help="load model from pkl file")
        rt_grp.add_argument(
            "-l", "--log", dest="logfile", nargs="?", const=os.path.join(self.work_dir, "neon_log.txt"), help="log file"
        )
        rt_grp.add_argument(
            "-o",
            "--output_file",
            default=None,
            help="hdf5 data file for metrics computed during "
            "the run, optional.  Can be used by nvis for "
            "visualization.",
        )
        rt_grp.add_argument(
            "-eval", "--eval_freq", type=int, default=None, help="frequency (in epochs) to test the eval set."
        )
        rt_grp.add_argument("-H", "--history", type=int, default=1, help="number of checkpoint files to retain")

        be_grp = self.add_argument_group("backend")
        be_grp.add_argument(
            "-b",
            "--backend",
            choices=["cpu", "gpu", "mgpu"],
            default="gpu" if get_compute_capability() >= 5.0 else "cpu",
            help="backend type. Multi-GPU support is a premium "
            "feature available exclusively through the "
            "Nervana cloud. Please contact "
            "[email protected] for details.",
        )
        be_grp.add_argument("-i", "--device_id", type=int, default=0, help="gpu device id (only used with GPU backend)")
        be_grp.add_argument(
            "-m",
            "--max_devices",
            type=int,
            default=get_device_count(),
            help="max number of GPUs (only used with mgpu backend",
        )

        be_grp.add_argument(
            "-r", "--rng_seed", type=int, default=None, metavar="SEED", help="random number generator seed"
        )
        be_grp.add_argument(
            "-u",
            "--rounding",
            const=True,
            type=int,
            nargs="?",
            metavar="BITS",
            default=False,
            help="use stochastic rounding [will round to BITS number " "of bits if specified]",
        )
        be_grp.add_argument(
            "-d",
            "--datatype",
            choices=["f16", "f32", "f64"],
            default="f32",
            metavar="default datatype",
            help="default floating point " "precision for backend [f64 for cpu only]",
        )

        be_grp.add_argument("-z", "--batch_size", type=int, default=128, help="batch size")

        return
Пример #10
0
def gen_backend(backend='cpu',
                rng_seed=None,
                datatype=np.float32,
                batch_size=0,
                stochastic_round=False,
                device_id=0,
                max_devices=get_device_count(),
                compat_mode=None,
                deterministic_update=False,
                deterministic=True):
    """
    Construct and return a backend instance of the appropriate type based on
    the arguments given. With no parameters, a single CPU core, float32
    backend is returned.

    Arguments:
        backend (string, optional): 'cpu' or 'gpu'.
        rng_seed (numeric, optional): Set this to a numeric value which can be used to seed the
                                      random number generator of the instantiated backend.
                                      Defaults to None, which doesn't explicitly seed (so each run
                                      will be different)
        dataype (dtype): Default tensor data type. CPU backend supports np.float64, np.float32 and
                         np.float16; GPU backend supports np.float32 and np.float16.
        batch_size (int): Set the size the data batches.
        stochastic_round (int/bool, optional): Set this to True or an integer to implent
                                               stochastic rounding. If this is False rounding will
                                               be to nearest. If True will perform stochastic
                                               rounding using default bit width. If set to an
                                               integer will round to that number of bits.
                                               Only affects the gpu backend.
        device_id (numeric, optional): Set this to a numeric value which can be used to select
                                       device on which to run the process
        max_devices (int, optional): For use with multi-GPU backend only.
                                      Controls the maximum number of GPUs to run
                                      on.
        compat_mode (str, optional): if this is set to 'caffe' then the conv and pooling
                                     layer output sizes will match that of caffe as will
                                     the dropout layer implementation
        deterministic (bool, optional): if set to true, all operations will be done deterministically.

    Returns:
        Backend: newly constructed backend instance of the specifed type.

    Notes:
        * Attempts to construct a GPU instance without a CUDA capable card or without nervanagpu
          package installed will cause the program to display an error message and exit.
    """
    logger = logging.getLogger(__name__)

    if NervanaObject.be is not None:
        # backend was already generated clean it up first
        cleanup_backend()
    else:
        # at exit from python force cleanup of backend only register this function once, will use
        # NervanaObject.be instead of a global
        atexit.register(cleanup_backend)

    if deterministic_update:
        deterministic = True
        logger.warning(
            "--deterministic_update is deprecated in favor of --deterministic")

    if backend == 'cpu' or backend is None:
        from neon.backends.nervanacpu import NervanaCPU
        be = NervanaCPU(rng_seed=rng_seed,
                        default_dtype=datatype,
                        compat_mode=compat_mode)
    elif backend == 'gpu' or backend == 'mgpu':
        gpuflag = False
        # check nvcc
        from neon.backends.util import check_gpu
        gpuflag = (check_gpu.get_compute_capability(device_id) >= 3.0)
        if gpuflag is False:
            raise RuntimeError("Device " + str(device_id) +
                               " does not have CUDA compute " +
                               "capability 3.0 or greater")
        if backend == 'gpu':
            from neon.backends.nervanagpu import NervanaGPU
            # init gpu
            be = NervanaGPU(rng_seed=rng_seed,
                            default_dtype=datatype,
                            stochastic_round=stochastic_round,
                            device_id=device_id,
                            compat_mode=compat_mode,
                            deterministic=deterministic)
        else:
            try:
                from mgpu.nervanamgpu import NervanaMGPU
                # init multiple GPU
                be = NervanaMGPU(rng_seed=rng_seed,
                                 default_dtype=datatype,
                                 stochastic_round=stochastic_round,
                                 num_devices=max_devices,
                                 compat_mode=compat_mode,
                                 deterministic=deterministic)
            except ImportError:
                logger.error(
                    "Multi-GPU support is a premium feature "
                    "available exclusively through the Nervana cloud."
                    " Please contact [email protected] for details.")
                raise
    elif backend == 'argon':
        from argon.neon_backend.ar_backend import ArBackend
        be = ArBackend(rng_seed=rng_seed, default_dtype=datatype)
    else:
        raise ValueError("backend must be one of ('cpu', 'gpu', 'mgpu')")

    logger.info("Backend: {}, RNG seed: {}".format(backend, rng_seed))

    NervanaObject.be = be
    be.bsz = batch_size
    return be
Пример #11
0
def gen_backend(backend='cpu', rng_seed=None, default_dtype=np.float32,
                batch_size=0, stochastic_round=False, device_id=0):
    """
    Construct and return a backend instance of the appropriate type based on
    the arguments given. With no parameters, a single CPU core, float32
    backend is returned.

    Arguments:
        backend (string, optional): 'cpu' or 'gpu'.
        rng_seed (numeric, optional): Set this to a numeric value which can be
                                      used to seed the random number generator
                                      of the instantiated backend.  Defaults to
                                      None, which doesn't explicitly seed (so
                                      each run will be different)
        default_dtype (dtype): Default tensor data type. CPU backend supports
                               np.float64, np.float32 and np.float16; GPU
                               backend supports np.float32 and np.float16.
        batch_size (int): Set the size the data batches.
        stochastic_round (int/bool, optional): Set this to True or an integer
                                               to implent stochastic rounding.
                                               If this is False rounding will
                                               be to nearest.
                                               If True will perform stochastic
                                               rounding using default bit width.
                                               If set to an integer will round
                                               to that number of bits.
                                               Only affects the gpu backend.
        device_id (numeric, optional): Set this to a numeric value which can be
                                       used to select which device to run the
                                       process on

    Returns:
        Backend: newly constructed backend instance of the specifed type.

    Notes:
        * Attempts to construct a GPU instance without a CUDA capable card or
          without nervanagpu package installed will cause the
          program to display an error message and exit.
    """
    logger = logging.getLogger(__name__)

    if NervanaObject.be is not None:
        # backend was already generated
        # clean it up first
        cleanup_backend()
    else:
        # at exit from python force cleanup of backend
        # only register this function once, will use
        # NervanaObject.be instead of a global
        atexit.register(cleanup_backend)

    if backend == 'cpu' or backend is None:
        from neon.backends.nervanacpu import NervanaCPU
        be = NervanaCPU(rng_seed=rng_seed, default_dtype=default_dtype)
    elif backend == 'gpu':
        gpuflag = False
        # check nvcc
        from neon.backends.util import check_gpu
        gpuflag = (check_gpu.get_compute_capability(device_id) >= 5.0)
        if gpuflag is False:
            raise RuntimeError("Device " + str(device_id) + " does not have CUDA compute " +
                               "capability 5.0 or greater")
        from neon.backends.nervanagpu import NervanaGPU
        # init gpu
        be = NervanaGPU(rng_seed=rng_seed, default_dtype=default_dtype,
                        stochastic_round=stochastic_round, device_id=device_id)
    elif backend == 'mgpu':
        raise NotImplementedError("mgpu will be ready soon")
    else:
        raise ValueError("backend must be one of "
                         "('cpu', 'gpu', 'mgpu')")

    logger.info("Backend: {}, RNG seed: {}".format(backend, rng_seed))

    NervanaObject.be = be
    be.bsz = batch_size
    return be
Пример #12
0
    def setup_default_args(self):
        """
        Setup the default arguments used by neon
        """

        self.add_argument('--version', action='version', version=neon_version)
        self.add_argument('-c', '--config', is_config_file=True,
                          help='Read values for these arguments from the '
                               'configuration file specified here first.')
        self.add_argument('-v', '--verbose', action='count', default=1,
                          help="verbosity level.  Add multiple v's to "
                               "further increase verbosity")
        # we store the negation of no_progress_bar in args.progress_bar during
        # parsing
        self.add_argument('--no_progress_bar',
                          action="store_true",
                          help="suppress running display of progress bar and "
                               "training loss")

        # runtime specifc options
        rt_grp = self.add_argument_group('runtime')
        rt_grp.add_argument('-w', '--data_dir',
                            default=os.path.join(self.work_dir, 'data'),
                            help='working directory in which to cache '
                                 'downloaded and preprocessed datasets')
        rt_grp.add_argument('-e', '--epochs', type=int, default=10,
                            help='number of complete passes over the dataset to run')
        rt_grp.add_argument('-s', '--save_path', type=str,
                            help='file path to save model snapshots')
        rt_grp.add_argument('--serialize', nargs='?', type=int,
                            default=0, const=1, metavar='N',
                            help='serialize model every N epochs')
        rt_grp.add_argument('--model_file', help='load model from pkl file')
        rt_grp.add_argument('-l', '--log', dest='logfile', nargs='?',
                            const=os.path.join(self.work_dir, 'neon_log.txt'),
                            help='log file')
        rt_grp.add_argument('-o', '--output_file', default=None,
                            help='hdf5 data file for metrics computed during '
                                 'the run, optional.  Can be used by nvis for '
                                 'visualization.')
        rt_grp.add_argument('-eval', '--eval_freq', type=int, default=None,
                            help='frequency (in epochs) to test the eval set.')
        rt_grp.add_argument('-H', '--history', type=int, default=1,
                            help='number of checkpoint files to retain')

        be_grp = self.add_argument_group('backend')
        be_grp.add_argument('-b', '--backend', choices=['cpu', 'gpu', 'mgpu'],
                            default='gpu' if get_compute_capability() >= 5.0
                                    else 'cpu',
                            help='backend type. Multi-GPU support is a premium '
                                 'feature available exclusively through the '
                                 'Nervana cloud. Please contact '
                                 '[email protected] for details.')
        be_grp.add_argument('-i', '--device_id', type=int, default=0,
                            help='gpu device id (only used with GPU backend)')
        be_grp.add_argument('-m', '--max_devices', type=int, default=get_device_count(),
                            help='max number of GPUs (only used with mgpu backend')

        be_grp.add_argument('-r', '--rng_seed', type=int,
                            default=None, metavar='SEED',
                            help='random number generator seed')
        be_grp.add_argument('-u', '--rounding',
                            const=True,
                            type=int,
                            nargs='?',
                            metavar='BITS',
                            default=False,
                            help='use stochastic rounding [will round to BITS number '
                                 'of bits if specified]')
        be_grp.add_argument('-d', '--datatype', choices=['f16', 'f32', 'f64'],
                            default='f32', metavar='default datatype',
                            help='default floating point '
                            'precision for backend [f64 for cpu only]')
        be_grp.add_argument('-z', '--batch_size', type=int, default=128,
                            help='batch size')
        be_grp.add_argument('--caffe', action='store_true',
                            help='match caffe when computing conv and pool layer output '
                                 'sizes and dropout implementation')

        return
Пример #13
0
def gen_backend(backend='cpu', rng_seed=None, datatype=np.float32,
                batch_size=0, stochastic_round=False, device_id=0,
                max_devices=get_device_count(), compat_mode=None):
    """
    Construct and return a backend instance of the appropriate type based on
    the arguments given. With no parameters, a single CPU core, float32
    backend is returned.

    Arguments:
        backend (string, optional): 'cpu' or 'gpu'.
        rng_seed (numeric, optional): Set this to a numeric value which can be used to seed the
                                      random number generator of the instantiated backend.
                                      Defaults to None, which doesn't explicitly seed (so each run
                                      will be different)
        dataype (dtype): Default tensor data type. CPU backend supports np.float64, np.float32 and
                         np.float16; GPU backend supports np.float32 and np.float16.
        batch_size (int): Set the size the data batches.
        stochastic_round (int/bool, optional): Set this to True or an integer to implent
                                               stochastic rounding. If this is False rounding will
                                               be to nearest. If True will perform stochastic
                                               rounding using default bit width. If set to an
                                               integer will round to that number of bits.
                                               Only affects the gpu backend.
        device_id (numeric, optional): Set this to a numeric value which can be used to select
                                       device on which to run the process
        max_devices (int, optional): For use with multi-GPU backend only.
                                      Controls the maximum number of GPUs to run
                                      on.
        compat_mode (str, optional): if this is set to 'caffe' then the conv and pooling
                                     layer output sizes will match that of caffe as will
                                     the dropout layer implementation

    Returns:
        Backend: newly constructed backend instance of the specifed type.

    Notes:
        * Attempts to construct a GPU instance without a CUDA capable card or without nervanagpu
          package installed will cause the program to display an error message and exit.
    """
    logger = logging.getLogger(__name__)

    if NervanaObject.be is not None:
        # backend was already generated clean it up first
        cleanup_backend()
    else:
        # at exit from python force cleanup of backend only register this function once, will use
        # NervanaObject.be instead of a global
        atexit.register(cleanup_backend)

    if backend == 'cpu' or backend is None:
        from neon.backends.nervanacpu import NervanaCPU
        be = NervanaCPU(rng_seed=rng_seed, default_dtype=datatype, compat_mode=compat_mode)
    elif backend == 'gpu' or backend == 'mgpu':
        gpuflag = False
        # check nvcc
        from neon.backends.util import check_gpu
        gpuflag = (check_gpu.get_compute_capability(device_id) >= 5.0)
        if gpuflag is False:
            raise RuntimeError("Device " + str(device_id) + " does not have CUDA compute " +
                               "capability 5.0 or greater")
        if backend == 'gpu':
            from neon.backends.nervanagpu import NervanaGPU
            # init gpu
            be = NervanaGPU(rng_seed=rng_seed, default_dtype=datatype,
                            stochastic_round=stochastic_round,
                            device_id=device_id,
                            compat_mode=compat_mode)
        else:
            try:
                from mgpu.nervanamgpu import NervanaMGPU
                # init multiple GPU
                be = NervanaMGPU(rng_seed=rng_seed,
                                 default_dtype=datatype,
                                 stochastic_round=stochastic_round,
                                 num_devices=max_devices)
            except ImportError:
                logger.error("Multi-GPU support is a premium feature "
                             "available exclusively through the Nervana cloud."
                             " Please contact [email protected] for details.")
                raise
    else:
        raise ValueError("backend must be one of ('cpu', 'gpu', 'mgpu')")

    logger.info("Backend: {}, RNG seed: {}".format(backend, rng_seed))

    NervanaObject.be = be
    be.bsz = batch_size
    return be