def gen_backend(backend='mkl' if get_mkl_lib() else '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=None, deterministic=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', 'mkl' 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) 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 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 is not None or deterministic is not None: logger.warning( 'deterministic_update and deterministic args are deprecated in favor of ' 'specifying random seed') deterministic = None from neon.backends.backend import Backend be = Backend.allocate_backend(backend, rng_seed=rng_seed, default_dtype=datatype, stochastic_round=stochastic_round, device_id=device_id, num_devices=max_devices, compat_mode=compat_mode, deterministic=deterministic) logger.info("Backend: {}, RNG seed: {}".format(backend, rng_seed)) NervanaObject.be = be be.bsz = batch_size return be
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=self.defaults.get('verbose', 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") bm_grp = self.add_argument_group('benchmark') bm_grp.add_argument('--profile', action='store_true') bm_grp.add_argument('--profiling_method', type=str, default='time') bm_grp.add_argument('--profile_inference', action='store_true') bm_grp.add_argument('--profile_iterations', type=int, default=50) bm_grp.add_argument('--profile_iter_skip', type=int, default=5) # 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=self.defaults.get('epochs', 10), help='number of complete passes over the dataset to run') rt_grp.add_argument('-s', '--save_path', type=str, default=self.defaults.get('save_path'), help='file path to save model snapshots') rt_grp.add_argument('--serialize', nargs='?', type=int, default=self.defaults.get('serialize', 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=self.defaults.get('output_file', 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=self.defaults.get('eval_freq', None), help='frequency (in epochs) to test the eval set.') rt_grp.add_argument('-H', '--history', type=int, default=self.defaults.get('history', 1), help='number of checkpoint files to retain') rt_grp.add_argument('--log_token', type=str, default='', help='access token for data logging in real time') rt_grp.add_argument('--manifest', action='append', help="manifest files") rt_grp.add_argument( '--manifest_root', type=str, default=None, help='Common root path for relative path items in the ' 'supplied manifest files') be_grp = self.add_argument_group('backend') be_grp.add_argument( '-b', '--backend', choices=Backend.backend_choices(), default='gpu' if get_compute_capability() >= 3.0 else 'mkl' if get_mkl_lib() 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=self.defaults.get('device_id', 0), help='gpu device id (only used with GPU backend)') be_grp.add_argument( '-m', '--max_devices', type=int, default=self.defaults.get('max_devices', get_device_count()), help='max number of GPUs (only used with mgpu backend') be_grp.add_argument('-r', '--rng_seed', type=int, default=self.defaults.get('rng_seed', None), metavar='SEED', help='random number generator seed') be_grp.add_argument( '-u', '--rounding', const=True, type=int, nargs='?', metavar='BITS', default=self.defaults.get('rounding', 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=self.defaults.get('datatype', '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=self.defaults.get('batch_size', 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') be_grp.add_argument('--deterministic', action='store_true', help='Use deterministic kernels where applicable') return
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=None, deterministic=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) 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 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 is not None or deterministic is not None: logger.warning('deterministic_update and deterministic args are deprecated in favor of ' 'specifying random seed') deterministic = None from neon.backends.backend import Backend be = Backend.allocate_backend(backend, rng_seed=rng_seed, default_dtype=datatype, stochastic_round=stochastic_round, device_id=device_id, num_devices=max_devices, compat_mode=compat_mode, deterministic=deterministic) logger.info("Backend: {}, RNG seed: {}".format(backend, rng_seed)) NervanaObject.be = be be.bsz = batch_size return be
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=self.defaults.get('verbose', 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=self.defaults.get('epochs', 10), help='number of complete passes over the dataset to run') rt_grp.add_argument('-s', '--save_path', type=str, default=self.defaults.get('save_path'), help='file path to save model snapshots') rt_grp.add_argument('--serialize', nargs='?', type=int, default=self.defaults.get('serialize', 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=self.defaults.get('output_file', 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=self.defaults.get('eval_freq', None), help='frequency (in epochs) to test the eval set.') rt_grp.add_argument('-H', '--history', type=int, default=self.defaults.get('history', 1), help='number of checkpoint files to retain') rt_grp.add_argument('--log_token', type=str, default='', help='access token for data logging in real time') be_grp = self.add_argument_group('backend') be_grp.add_argument('-b', '--backend', choices=Backend.backend_choices(), default='gpu' if get_compute_capability() >= 3.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=self.defaults.get('device_id', 0), help='gpu device id (only used with GPU backend)') be_grp.add_argument('-m', '--max_devices', type=int, default=self.defaults.get('max_devices', get_device_count()), help='max number of GPUs (only used with mgpu backend') be_grp.add_argument('-r', '--rng_seed', type=int, default=self.defaults.get('rng_seed', None), metavar='SEED', help='random number generator seed') be_grp.add_argument('-u', '--rounding', const=True, type=int, nargs='?', metavar='BITS', default=self.defaults.get('rounding', 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=self.defaults.get('datatype', '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=self.defaults.get('batch_size', 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') be_grp.add_argument('--deterministic', action='store_true', help='Use deterministic kernels where applicable') return