Exemplo n.º 1
0
import time
import logging

from contextlib import contextmanager

from theano import config
from theano.configparser import AddConfigVar, IntParam

_logger = logging.getLogger("theano.gof.compilelock")
# If the user provided a logging level, we don't want to override it.
if _logger.level == logging.NOTSET:
    # INFO will show the "Refreshing lock" messages
    _logger.setLevel(logging.INFO)

AddConfigVar('compile.wait',
             """Time to wait before retrying to aquire the compile lock.""",
             IntParam(5, lambda i: i > 0, allow_override=False),
             in_c_key=False)


def _timeout_default():
    return config.compile.wait * 24


AddConfigVar('compile.timeout',
             """In seconds, time that a process will wait before deciding to
override an existing lock. An override only happens when the existing
lock is held by the same owner *and* has not been 'refreshed' by this
owner for more than this period. Refreshes are done every half timeout
period for running processes.""",
             IntParam(_timeout_default, lambda i: i >= 0,
                      allow_override=False),
Exemplo n.º 2
0
import theano
from theano.gof.link import WrapLinker
from theano.compile.mode import (Mode, register_mode,
                                 predefined_modes, predefined_linkers,
                                 predefined_optimizers)
from theano import gof
from theano.configparser import config, AddConfigVar, IntParam, BoolParam
from theano.compile.function_module import FunctionMaker
run_cthunk = None  # Will be imported only when needed.

from profiling import ProfileStats

import_time = time.time()

AddConfigVar('ProfileMode.n_apply_to_print',
        "Number of apply instances to print by default",
        IntParam(15, lambda i: i > 0),
        in_c_key=False)

AddConfigVar('ProfileMode.n_ops_to_print',
        "Number of ops to print by default",
        IntParam(20, lambda i: i > 0),
        in_c_key=False)

AddConfigVar('ProfileMode.min_memory_size',
             """For the memory profile, do not print apply nodes if the size
 of their outputs (in bytes) is lower then this threshold""",
        IntParam(1024, lambda i: i >= 0),
        in_c_key=False)

AddConfigVar('ProfileMode.profile_memory',
             """Enable profiling of memory used by Theano functions""",
Exemplo n.º 3
0
        """
_logger = logging.getLogger("theano.tests.unittest_tools")


def good_seed_param(seed):
    if seed == "random":
        return True
    try:
        int(seed)
    except Exception:
        return False
    return True

AddConfigVar('unittests.rseed',
             "Seed to use for randomized unit tests. "
             "Special value 'random' means using a seed of None.",
             StrParam(666, is_valid=good_seed_param),
             in_c_key=False)


def fetch_seed(pseed=None):
    """
    Returns the seed to use for running the unit tests.
    If an explicit seed is given, it will be used for seeding numpy's rng.
    If not, it will use config.unittest.rseed (its default value is 666).
    If config.unittest.rseed is set to "random", it will seed the rng with
    None, which is equivalent to seeding with a random seed.

    Useful for seeding RandomState objects.
    >>> rng = numpy.random.RandomState(unittest_tools.fetch_seed())
    """
Exemplo n.º 4
0
from theano.configparser import AddConfigVar, StrParam

AddConfigVar('pthreads.inc_dir', "location of pthread.h", StrParam(""))

AddConfigVar('pthreads.lib_dir', "location of library implementing pthreads",
             StrParam(""))

AddConfigVar(
    'pthreads.lib',
    'name of the library that implements pthreads (e.g. "pthreadVC2" if using pthreadVC2.dll/.lib from pthreads-win32)',
    StrParam(""))
Exemplo n.º 5
0
import sys

import numpy

import theano.tensor as T
from theano.configparser import config, AddConfigVar, StrParam

AddConfigVar('unittests.rseed',
        "Seed to use for randomized unit tests. Special value 'random' means using a seed of None.",
        StrParam(666),
        in_c_key=False)

def fetch_seed(pseed=None):
    """
    Returns the seed to use for running the unit tests.
    If an explicit seed is given, it will be used for seeding numpy's rng.
    If not, it will use config.unittest.rseed (its default value is 666).
    If config.unittest.rseed is set to "random", it will seed the rng with None,
    which is equivalent to seeding with a random seed.

    Useful for seeding RandomState objects.
    >>> rng = numpy.random.RandomState(unittest_tools.fetch_seed())
    """

    seed = pseed or config.unittests.rseed
    if seed=='random':
        seed = None

    try:
        if seed:
            seed = int(seed)
Exemplo n.º 6
0
                # function would incorrectly identify it as (1 + exp(x)).
                if config.warn.identify_1pexp_bug:
                    warnings.warn(
                        'Although your current code is fine, please note that '
                        'Theano versions prior to 0.5 (more specifically, '
                        'prior to commit 7987b51 on 2011-12-18) may have '
                        'yielded an incorrect result. To remove this warning, '
                        'either set the `warn.identify_1pexp_bug` config '
                        'option to False, or `warn.ignore_bug_before` to at '
                        'least \'0.4.1\'.')
    return None


AddConfigVar('warn.identify_1pexp_bug',
        'Warn if Theano versions prior to 7987b51 (2011-12-18) could have '
        'yielded a wrong result due to a bug in the is_1pexp function',
        BoolParam(theano.configdefaults.warn_default('0.4.1')),
        in_c_key=False)


def is_exp(var):
    """
    Match a variable with either of the `exp(x)` or `-exp(x)` patterns.

    :param var: The Variable to analyze.

    :return: A pair (b, x) with `b` a boolean set to True if `var` is of the
    form `-exp(x)` and False if `var` is of the form `exp(x)`. If `var` cannot
    be cast into either form, then return `None`.
    """
    neg = False
Exemplo n.º 7
0
from theano.gof.cc import hash_from_file
from theano.gof.cmodule import (std_libs, std_lib_dirs,
                                std_include_dirs, dlimport,
                                get_lib_extension)
from theano.gof.python25 import any
from theano.misc.windows import call_subprocess_Popen

_logger = logging.getLogger("theano.sandbox.cuda.nvcc_compiler")
_logger.setLevel(logging.WARN)

from theano.configparser import (config, AddConfigVar, StrParam,
                                 BoolParam, ConfigParam)

AddConfigVar('nvcc.compiler_bindir',
             "If defined, nvcc compiler driver will seek g++ and gcc"
             " in this directory",
             StrParam(""),
             in_c_key=False)

AddConfigVar('cuda.nvccflags',
        "DEPRECATED, use nvcc.flags instead",
        StrParam("", allow_override=False),
        in_c_key=False)

if config.cuda.nvccflags != '':
    warnings.warn('Configuration variable cuda.nvccflags is deprecated. '
            'Please use nvcc.flags instead. You provided value: %s'
            % config.cuda.nvccflags)


def filter_nvcc_flags(s):
Exemplo n.º 8
0
import atexit, logging, os, shutil, stat, sys
from theano.compile import optdb
from theano.gof.cmodule import get_lib_extension
from theano.configparser import config, AddConfigVar, StrParam
import nvcc_compiler

_logger_name = 'theano.sandbox.cuda'
_logger = logging.getLogger(_logger_name)
_logger.setLevel(logging.WARNING)

AddConfigVar(
    'cuda.root', """directory with bin/, lib/, include/ for cuda utilities.
        This directory is included via -L and -rpath when linking
        dynamically compiled modules.  If AUTO and nvcc is in the
        path, it will use one of nvcc parent directory.  Otherwise
        /usr/local/cuda will be used.  Leave empty to prevent extra
        linker directives.  Default: environment variable "CUDA_ROOT"
        or else "AUTO".
        """, StrParam(os.getenv('CUDA_ROOT', "AUTO")))

if config.cuda.root == "AUTO":
    # set nvcc_path correctly and get the version
    nvcc_compiler.set_cuda_root()

#is_nvcc_available called here to initialize global vars in nvcc_compiler module
nvcc_compiler.is_nvcc_available()

# Compile cuda_ndarray.cu
# This need that nvcc (part of cuda) is installed. If it is not, a warning is
# printed and this module will not be working properly (we set `cuda_available`
# to False).
Exemplo n.º 9
0
        name = (kwargs and kwargs.pop('name')) or local_opt.__name__
        gpu_optimizer.register(name, local_opt, 'fast_run', 'fast_compile',
                               'gpu', *tags)
        return local_opt

    return f


_logger_name = 'theano.sandbox.cuda'
_logger = logging.getLogger(_logger_name)

AddConfigVar('pycuda.init',
             """If True, always initialize PyCUDA when Theano want to
           initilize the GPU.  Currently, we must always initialize
           PyCUDA before Theano do it.  Setting this flag to True,
           ensure that, but always import PyCUDA.  It can be done
           manually by importing theano.misc.pycuda_init before theano
           initialize the GPU device.
             """,
             BoolParam(False),
             in_c_key=False)

AddConfigVar('cublas.lib', """Name of the cuda blas library for the linker.""",
             StrParam('cublas'))

# is_nvcc_available called here to initialize global vars in
# nvcc_compiler module
nvcc_compiler.is_nvcc_available()

# Compile cuda_ndarray.cu
# This need that nvcc (part of cuda) is installed. If it is not, a warning is
# printed and this module will not be working properly (we set `cuda_available`
Exemplo n.º 10
0

def floatX_convert(s):
    if s == "32":
        return "float32"
    elif s == "64":
        return "float64"
    elif s == "16":
        return "float16"
    else:
        return s

AddConfigVar('floatX',
             "Default floating-point precision for python casts.\n"
             "\n"
             "Note: float16 support is experimental, use at your own risk.",
             EnumStr('float64', 'float32', 'float16',
                     convert=floatX_convert,),
             )

AddConfigVar('warn_float64',
             "Do an action when a tensor variable with float64 dtype is"
             " created. They can't be run on the GPU with the current(old)"
             " gpu back-end and are slow with gamer GPUs.",
             EnumStr('ignore', 'warn', 'raise', 'pdb'),
             in_c_key=False,
             )

AddConfigVar('cast_policy',
             'Rules for implicit type casting',
             EnumStr('custom', 'numpy+floatX',
Exemplo n.º 11
0
import os
import logging
import subprocess

from theano.configparser import (AddConfigVar, BoolParam, ConfigParam, EnumStr,
                                 IntParam, StrParam, TheanoConfigParser)
from theano.misc.cpucount import cpuCount
from theano.misc.windows import call_subprocess_Popen

_logger = logging.getLogger('theano.configdefaults')

config = TheanoConfigParser()

AddConfigVar(
    'floatX',
    "Default floating-point precision for python casts",
    EnumStr('float64', 'float32'),
)

AddConfigVar(
    'cast_policy',
    "Rules for implicit type casting",
    EnumStr(
        'custom',
        'numpy+floatX',
        # The 'numpy' policy was originally planned to provide a smooth
        # transition from numpy. It was meant to behave the same as
        # numpy+floatX, but keeping float64 when numpy would. However
        # the current implementation of some cast mechanisms makes it
        # a bit more complex to add than what was expected, so it is
        # currently not available.
Exemplo n.º 12
0
from theano.gof import local_bitwidth
from theano.gof.cc import hash_from_file
from theano.gof.cmodule import (std_libs, std_lib_dirs, std_include_dirs,
                                dlimport, get_lib_extension)
from theano.gof.python25 import any
from theano.misc.windows import call_subprocess_Popen

_logger = logging.getLogger("theano.sandbox.cuda.nvcc_compiler")
_logger.setLevel(logging.WARN)

from theano.configparser import (config, AddConfigVar, StrParam, BoolParam,
                                 ConfigParam)

AddConfigVar(
    'nvcc.compiler_bindir',
    "If defined, nvcc compiler driver will seek g++ and gcc"
    " in this directory", StrParam(""))

AddConfigVar('cuda.nvccflags',
             "DEPRECATED, use nvcc.flags instead",
             StrParam("", allow_override=False),
             in_c_key=False)

if config.cuda.nvccflags != '':
    warnings.warn('Configuration variable cuda.nvccflags is deprecated. '
                  'Please use nvcc.flags instead. You provided value: %s' %
                  config.cuda.nvccflags)


def filter_nvcc_flags(s):
    assert isinstance(s, str)
Exemplo n.º 13
0
import os
import logging
import subprocess

from theano.configparser import (AddConfigVar, BoolParam, ConfigParam, EnumStr,
                                 IntParam, TheanoConfigParser)

_logger = logging.getLogger('theano.configdefaults')

config = TheanoConfigParser()

AddConfigVar(
    'floatX',
    "Default floating-point precision for python casts",
    EnumStr('float64', 'float32'),
)

AddConfigVar(
    'cast_policy',
    "Rules for implicit type casting",
    EnumStr(
        'custom',
        'numpy+floatX',
        # The 'numpy' policy was originally planned to provide a smooth
        # transition from numpy. It was meant to behave the same as
        # numpy+floatX, but keeping float64 when numpy would. However
        # the current implementation of some cast mechanisms makes it
        # a bit more complex to add than what was expected, so it is
        # currently not available.
        #numpy,
    ),
Exemplo n.º 14
0
from theano.compile import optdb
from theano.gof.cmodule import get_lib_extension
from theano.gof.compilelock import get_lock, release_lock
from theano.configparser import config, AddConfigVar, StrParam, BoolParam
import nvcc_compiler

_logger_name = 'theano.sandbox.cuda'
_logger = logging.getLogger(_logger_name)
_logger.setLevel(logging.WARNING)

AddConfigVar('cuda.root',
             """directory with bin/, lib/, include/ for cuda utilities.
        This directory is included via -L and -rpath when linking
        dynamically compiled modules.  If AUTO and nvcc is in the
        path, it will use one of nvcc parent directory.  Otherwise
        /usr/local/cuda will be used.  Leave empty to prevent extra
        linker directives.  Default: environment variable "CUDA_ROOT"
        or else "AUTO".
        """,
             StrParam(os.getenv('CUDA_ROOT', "AUTO")),
             in_c_key=False)

AddConfigVar('pycuda.init',
             """If True, always initialize PyCUDA when Theano want to
           initilize the GPU.  Currently, we must always initialize
           PyCUDA before Theano do it.  Setting this flag to True,
           ensure that, but always import PyCUDA.  It can be done
           manually by importing theano.misc.pycuda_init before theano
           initialize the GPU device.
             """,
             BoolParam(False),
Exemplo n.º 15
0
import logging

import numpy

import theano
from theano import gof
import theano.gof.vm
from theano.configparser import config, AddConfigVar, StrParam
from theano.compile.ops import _output_guard
from six import string_types

_logger = logging.getLogger('theano.compile.mode')

AddConfigVar('optimizer_excluding',
             ("When using the default mode, we will remove optimizer with "
              "these tags. Separate tags with ':'."),
             StrParam("", allow_override=False),
             in_c_key=False)
AddConfigVar('optimizer_including',
             ("When using the default mode, we will add optimizer with "
              "these tags. Separate tags with ':'."),
             StrParam("", allow_override=False),
             in_c_key=False)
AddConfigVar('optimizer_requiring',
             ("When using the default mode, we will require optimizer with "
              "these tags. Separate tags with ':'."),
             StrParam("", allow_override=False),
             in_c_key=False)


def check_equal(x, y):
Exemplo n.º 16
0
        name = (kwargs and kwargs.pop('name')) or local_opt.__name__
        gpu_optimizer.register(name, local_opt, 'fast_run', 'fast_compile',
                               'gpu', *tags, **kwargs)
        return local_opt

    return f


_logger_name = 'theano.sandbox.cuda'
_logger = logging.getLogger(_logger_name)

AddConfigVar('pycuda.init',
             """If True, always initialize PyCUDA when Theano want to
           initilize the GPU.  Currently, we must always initialize
           PyCUDA before Theano do it.  Setting this flag to True,
           ensure that, but always import PyCUDA.  It can be done
           manually by importing theano.misc.pycuda_init before theano
           initialize the GPU device.
             """,
             BoolParam(False),
             in_c_key=False)

AddConfigVar('cublas.lib', """Name of the cuda blas library for the linker.""",
             StrParam('cublas'))

AddConfigVar(
    'lib.cnmem',
    """Do we enable CNMeM or not (a faster CUDA memory allocator).

             The parameter represent the start size (in MB or % of
             total GPU memory) of the memory pool.
Exemplo n.º 17
0
from __future__ import print_function
import sys

import numpy

from theano.compat import DefaultOrderedDict
from theano.misc.ordered_set import OrderedSet
from six import StringIO
from theano.gof import opt
from theano.configparser import AddConfigVar, FloatParam
from theano import config

AddConfigVar('optdb.position_cutoff',
             'Where to stop eariler during optimization. It represent the'
             ' position of the optimizer where to stop.',
             FloatParam(numpy.inf),
             in_c_key=False)
AddConfigVar('optdb.max_use_ratio',
             'A ratio that prevent infinite loop in EquilibriumOptimizer.',
             FloatParam(5),
             in_c_key=False)


class DB(object):
    def __hash__(self):
        if not hasattr(self, '_optimizer_idx'):
            self._optimizer_idx = opt._optimizer_idx[0]
            opt._optimizer_idx[0] += 1
        return self._optimizer_idx

    def __init__(self):
Exemplo n.º 18
0
import copy
import sys
import time

import numpy

import theano
from theano.configparser import AddConfigVar, BoolParam

import_time = time.time()
config = theano.config

_atexit_print_list = []
_atexit_print_file = sys.stderr

AddConfigVar('profiling.time_thunks',
             """Time individual thunks when profiling""", BoolParam(True))


def _atexit_print_fn():
    """Print ProfileStat objects in _atexit_print_list to _atexit_print_file
    """
    printed = 0
    for ps in _atexit_print_list:
        if ps.fct_callcount or ps.compile_time > 0:
            ps.summary(file=_atexit_print_file)
            printed += 1
        else:
            print 'Skipping empty Profile'
    if printed > 1:
        # Make a global profile
        cum = copy.copy(_atexit_print_list[0])
Exemplo n.º 19
0
    sp[1] = '.'.join(rest)

    # For sp[2:], we don't change anything.
    sr = '-'.join(sp)
    p = p.replace(r, sr)

    return p
compiledir_format_dict['short_platform'] = short_platform()
compiledir_format_keys = ", ".join(sorted(compiledir_format_dict.keys()))
default_compiledir_format = ("compiledir_%(short_platform)s-%(processor)s-"
                             "%(python_version)s-%(python_bitwidth)s")

AddConfigVar("compiledir_format",
             textwrap.fill(textwrap.dedent("""\
                 Format string for platform-dependent compiled
                 module subdirectory (relative to base_compiledir).
                 Available keys: %s. Defaults to %r.
             """ % (compiledir_format_keys, default_compiledir_format))),
             StrParam(default_compiledir_format, allow_override=False),
             in_c_key=False)


def default_compiledirname():
    formatted = config.compiledir_format % compiledir_format_dict
    safe = re.sub("[\(\)\s,]+", "_", formatted)
    return safe


def filter_base_compiledir(path):
    # Expand '~' in path
    return os.path.expanduser(str(path))
Exemplo n.º 20
0
import os
import sys
import time
import warnings

from theano.gof.python25 import all

from theano.configparser import (config, AddConfigVar, BoolParam, ConfigParam,
                                 _config_var_list)

import theano.gof.cmodule

logger = logging.getLogger(__name__)

AddConfigVar('profile',
             "If VM should collect profile information",
             BoolParam(False),
             in_c_key=False)
AddConfigVar('profile_optimizer',
             "If VM should collect optimizer profile information",
             BoolParam(False),
             in_c_key=False)
AddConfigVar('profile_memory',
             "If VM should collect memory profile information and print it",
             BoolParam(False),
             in_c_key=False)


def filter_vm_lazy(val):
    if val == 'False' or val is False:
        return False
    elif val == 'True' or val is True:
Exemplo n.º 21
0
import time

import numpy

import theano
from theano.configparser import AddConfigVar, BoolParam, IntParam


import_time = time.time()
config = theano.config

_atexit_print_list = []
_atexit_print_file = sys.stderr

AddConfigVar('profiling.time_thunks',
             """Time individual thunks when profiling""",
             BoolParam(True))

AddConfigVar('profiling.n_apply',
             "Number of Apply instances to print by default",
             IntParam(20, lambda i: i > 0),
             in_c_key=False)

AddConfigVar('profiling.n_ops',
             "Number of Ops to print by default",
             IntParam(20, lambda i: i > 0),
             in_c_key=False)

AddConfigVar('profiling.min_memory_size',
             """For the memory profile, do not print Apply nodes if the size
             of their outputs (in bytes) is lower than this threshold""",
Exemplo n.º 22
0
from theano.gof import local_bitwidth
from theano.gof.cc import hash_from_file
from theano.gof.cmodule import (std_libs, std_lib_dirs, std_include_dirs,
                                dlimport, get_lib_extension)
from theano.gof.python25 import any
from theano.misc.windows import call_subprocess_Popen

_logger = logging.getLogger("theano.sandbox.cuda.nvcc_compiler")
_logger.setLevel(logging.WARN)

from theano.configparser import (config, AddConfigVar, StrParam, BoolParam,
                                 ConfigParam)

AddConfigVar('nvcc.compiler_bindir',
             "If defined, nvcc compiler driver will seek g++ and gcc"
             " in this directory",
             StrParam(""),
             in_c_key=False)

user_provided_cuda_root = True


def default_cuda_root():
    global user_provided_cuda_root
    v = os.getenv('CUDA_ROOT', "")
    user_provided_cuda_root = False
    if v:
        return v
    return find_cuda_root()

Exemplo n.º 23
0
import collections
import logging

import numpy as np

import theano
from theano.configparser import config, AddConfigVar, BoolParam
import theano.tensor as T
import theano.sandbox.cuda as cuda
from theano.compile import Mode

AddConfigVar('NanGuardMode.nan_is_error',
             "Default value for nan_is_error",
             BoolParam(True),
             in_c_key=False)

AddConfigVar('NanGuardMode.inf_is_error',
             "Default value for inf_is_error",
             BoolParam(True),
             in_c_key=False)

AddConfigVar('NanGuardMode.big_is_error',
             "Default value for big_is_error",
             BoolParam(True),
             in_c_key=False)

logger = logging.getLogger("theano.compile.nanguardmode")


def flatten(l):
    """
Exemplo n.º 24
0
            home = os.getenv('USERPROFILE')
    assert home is not None
    return home


# On Windows we should avoid writing temporary files to a directory that is
# part of the roaming part of the user profile. Instead we use the local part
# of the user profile, when available.
if sys.platform == 'win32' and os.getenv('LOCALAPPDATA') is not None:
    default_base_compiledir = os.path.join(os.getenv('LOCALAPPDATA'), 'Theano')
else:
    default_base_compiledir = os.path.join(get_home_dir(), '.theano')


AddConfigVar('base_compiledir',
        "arch-independent cache directory for compiled modules",
        StrParam(default_base_compiledir, allow_override=False))


AddConfigVar('compiledir',
        "arch-dependent cache directory for compiled modules",
        ConfigParam(
            os.path.join(
                os.path.expanduser(config.base_compiledir),
                default_compiledirname()),
            filter=filter_compiledir,
            allow_override=False))


def print_compiledir_content():
Exemplo n.º 25
0
import logging
import sys
import time
import link
import traceback
from theano.gof.python25 import all

import theano
config = theano.config

from theano.configparser import config, AddConfigVar, BoolParam
from theano import config

logger = logging.getLogger(__name__)

AddConfigVar('profile', "If VM should collect profile information",
             BoolParam(False))

raise_with_op = link.raise_with_op


class VM(object):
    """
    A VM object evaluates a Theano program with its __call__ method.

    Attributes:

    call_counts - list of integers, one for each thunk. call_count[i] is the
        number of times thunks[i] was called in the course of computations
        performed by call_with_timers().

    call_times - list of floats, one for each thunk. call_times[i] is the amount