예제 #1
0
def test_invalid_default():
    # Ensure an invalid default value found in the Theano code only causes
    # a crash if it is not overridden by the user.

    def validate(val):
        if val == "invalid":
            raise ValueError("Test-triggered")

    with pytest.raises(ValueError, match="Test-triggered"):
        # This should raise a ValueError because the default value is
        # invalid.
        AddConfigVar(
            "T_config__test_invalid_default_a",
            doc="unittest",
            configparam=ConfigParam("invalid", validate=validate),
            in_c_key=False,
        )

    THEANO_FLAGS_DICT["T_config__test_invalid_default_b"] = "ok"
    # This should succeed since we defined a proper value, even
    # though the default was invalid.
    AddConfigVar(
        "T_config__test_invalid_default_b",
        doc="unittest",
        configparam=ConfigParam("invalid", validate=validate),
        in_c_key=False,
    )

    # TODO We should remove these dummy options on test exit.
    # Check that the flag has been removed
    assert "T_config__test_invalid_default_b" not in THEANO_FLAGS_DICT
예제 #2
0
    def test_invalid_default(self):
        # Ensure an invalid default value found in the Theano code only causes
        # a crash if it is not overridden by the user.

        def filter(val):
            if val == "invalid":
                raise ValueError()
            else:
                return val

        try:
            # This should raise a ValueError because the default value is
            # invalid.
            AddConfigVar(
                "T_config.test_invalid_default_a",
                doc="unittest",
                configparam=ConfigParam("invalid", filter=filter),
                in_c_key=False,
            )
            raise AssertionError()
        except ValueError:
            pass

        THEANO_FLAGS_DICT["T_config.test_invalid_default_b"] = "ok"
        # This should succeed since we defined a proper value, even
        # though the default was invalid.
        AddConfigVar(
            "T_config.test_invalid_default_b",
            doc="unittest",
            configparam=ConfigParam("invalid", filter=filter),
            in_c_key=False,
        )

        # Check that the flag has been removed
        assert "T_config.test_invalid_default_b" not in THEANO_FLAGS_DICT
예제 #3
0
def test_invalid_default():
    # Ensure an invalid default value found in the Theano code only causes
    # a crash if it is not overridden by the user.

    root = _create_test_config()

    def validate(val):
        if val == "invalid":
            raise ValueError("Test-triggered")

    with pytest.raises(ValueError, match="Test-triggered"):
        # This should raise a ValueError because the default value is
        # invalid.
        root.add(
            "test__test_invalid_default_a",
            doc="unittest",
            configparam=ConfigParam("invalid", validate=validate),
            in_c_key=False,
        )

    root._flags_dict["test__test_invalid_default_b"] = "ok"
    # This should succeed since we defined a proper value, even
    # though the default was invalid.
    root.add(
        "test__test_invalid_default_b",
        doc="unittest",
        configparam=ConfigParam("invalid", validate=validate),
        in_c_key=False,
    )

    # Check that the flag has been removed
    assert "test__test_invalid_default_b" not in root._flags_dict
예제 #4
0
    def test_invalid_default(self):
        # Ensure an invalid default value found in the Theano code only causes
        # a crash if it is not overridden by the user.

        def filter(val):
            if val == 'invalid':
                raise ValueError()
            else:
                return val

        try:
            # This should raise a ValueError because the default value is
            # invalid.
            AddConfigVar('T_config.test_invalid_default_a',
                         doc='unittest',
                         configparam=ConfigParam('invalid', filter=filter),
                         in_c_key=False)
            assert False
        except ValueError:
            pass

        THEANO_FLAGS_DICT['T_config.test_invalid_default_b'] = 'ok'
        # This should succeed since we defined a proper value, even
        # though the default was invalid.
        AddConfigVar('T_config.test_invalid_default_b',
                     doc='unittest',
                     configparam=ConfigParam('invalid', filter=filter),
                     in_c_key=False)

        # Check that the flag has been removed
        assert 'T_config.test_invalid_default_b' not in THEANO_FLAGS_DICT
예제 #5
0
 def __init__(self):
     def filter(val):
         if val == '':
             return val
         for v in val.split(';'):
             s = v.split('->')
             if len(s) != 2:
                 raise ValueError("Malformed context map: %s" % (v,))
         return val
     ConfigParam.__init__(self, '', filter, False)
예제 #6
0
    def __init__(self):
        def filter(val):
            if val == '':
                return val
            for v in val.split(';'):
                s = v.split('->')
                if len(s) != 2:
                    raise ValueError("Malformed context map: %s" % (v, ))
            return val

        ConfigParam.__init__(self, '', filter, False)
예제 #7
0
 def __init__(self):
     def filter(val):
         if val == '':
             return val
         for v in val.split(';'):
             s = v.split('->')
             if len(s) != 2:
                 raise ValueError("Malformed context map: %s" % (v,))
             if (s[0] == 'cpu' or s[0].startswith('cuda') or
                     s[0].startswith('opencl')):
                 raise ValueError("Cannot use %s as context name" % (s[0],))
         return val
     ConfigParam.__init__(self, '', filter, False)
예제 #8
0
def test_no_more_dotting():
    with pytest.raises(ValueError, match="Dot-based"):
        AddConfigVar(
            "T_config.something",
            doc="unittest",
            configparam=ConfigParam("invalid"),
            in_c_key=False,
        )
예제 #9
0
def test_no_more_dotting():
    root = configdefaults.config
    with pytest.raises(ValueError, match="Dot-based"):
        root.add(
            "test.something",
            doc="unittest",
            configparam=ConfigParam("invalid"),
            in_c_key=False,
        )
예제 #10
0
def test_config_param_apply_and_validation():
    cp = ConfigParam(
        "TheDeFauLt",
        apply=lambda v: v.lower(),
        validate=lambda v: v in "thedefault,thesetting",
        mutable=True,
    )
    assert cp.default == "TheDeFauLt"
    assert not hasattr(cp, "val")

    # can't assign invalid value
    with pytest.raises(ValueError, match="Invalid value"):
        cp.__set__("cls", "invalid")

    assert not hasattr(cp, "val")

    # effectivity of apply function
    cp.__set__("cls", "THESETTING")
    assert cp.val == "thesetting"

    # respect the mutability
    cp._mutable = False
    with pytest.raises(Exception, match="Can't change"):
        cp.__set__("cls", "THEDEFAULT")
예제 #11
0

def filter_nvcc_flags(s):
    assert isinstance(s, str)
    flags = [flag for flag in s.split(' ') if flag]
    if any([f for f in flags if not f.startswith("-")]):
        raise ValueError(
            "Theano nvcc.flags support only parameter/value pairs without"
            " space between them. e.g.: '--machine 64' is not supported,"
            " but '--machine=64' is supported. Please add the '=' symbol."
            " nvcc.flags value is '%s'" % s)
    return ' '.join(flags)

AddConfigVar('nvcc.flags',
             "Extra compiler flags for nvcc",
             ConfigParam("", filter_nvcc_flags),
             # Not needed in c key as it is already added.
             # We remove it as we don't make the md5 of config to change
             # if theano.sandbox.cuda is loaded or not.
             in_c_key=False)


AddConfigVar('nvcc.fastmath',
             "",
             BoolParam(False),
             # Not needed in c key as it is already added.
             # We remove it as we don't make the md5 of config to change
             # if theano.sandbox.cuda is loaded or not.
             in_c_key=False)

nvcc_path = 'nvcc'
예제 #12
0
    switch to `config.basecompiledir` instead, by raising an error when
    `config.home` is used.
    """
    if home:
        raise RuntimeError(
            'The `config.home` option has been removed and should not be '
            'used anymore. Please set the `config.base_compiledir` option '
            'instead (for instance to: %s)' %
            os.path.join(home, '.theano'))
    return True


AddConfigVar(
    'home',
    "This config option was removed in 0.5: do not use it!",
    ConfigParam('', allow_override=False, filter=safe_no_home),
    in_c_key=False)


AddConfigVar(
    'nocleanup',
    "Suppress the deletion of code files that did not compile cleanly",
    BoolParam(False),
    in_c_key=False)

AddConfigVar('on_unused_input',
             "What to do if a variable in the 'inputs' list of "
             " theano.function() is not used in the graph.",
             EnumStr('raise', 'warn', 'ignore'),
             in_c_key=False)
예제 #13
0
def filter_nvcc_flags(s):
    assert isinstance(s, str)
    flags = [flag for flag in s.split(' ') if flag]
    if any([f for f in flags if not f.startswith("-")]):
        raise ValueError(
            "Theano nvcc.flags support only parameter/value pairs without"
            " space between them. e.g.: '--machine 64' is not supported,"
            " but '--machine=64' is supported. Please add the '=' symbol."
            " nvcc.flags value is '%s'" % s)
    return ' '.join(flags)


AddConfigVar(
    'nvcc.flags',
    "Extra compiler flags for nvcc",
    ConfigParam(config.cuda.nvccflags, filter_nvcc_flags),
    # Not needed in c key as it is already added.
    # We remove it as we don't make the md5 of config to change
    # if theano.sandbox.cuda is loaded or not.
    in_c_key=False)

AddConfigVar(
    'nvcc.fastmath',
    "",
    BoolParam(False),
    # Not needed in c key as it is already added.
    # We remove it as we don't make the md5 of config to change
    # if theano.sandbox.cuda is loaded or not.
    in_c_key=False)

nvcc_path = 'nvcc'
예제 #14
0
    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',
             "platform-independent root directory for compiled modules",
             ConfigParam(default_base_compiledir,
                         filter=filter_base_compiledir,
                         allow_override=False),
             in_c_key=False)

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


def cleanup():
    """
    Delete keys in old format from the compiledir.
예제 #15
0
# 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():
    def flatten(a):
        if isinstance(a, (tuple, list, set)):
            l = []
            for item in a:
                l.extend(flatten(item))
            return l
        else:
            return [a]

    compiledir = theano.config.compiledir
    print "List compiled ops in this theano cache:", compiledir
예제 #16
0
파일: vm.py 프로젝트: xinfanmeng/Theano
    if val == 'False' or val is False:
        return False
    elif val == 'True' or val is True:
        return True
    elif val == 'None':
        return None
    else:
        raise ValueError('Valid values for an vm.lazy parameter '
                         'should be None, False or True, not `%s`.' % val)


AddConfigVar(
    'vm.lazy', "Useful only for the vm linkers. When lazy is None,"
    " auto detect if lazy evaluation is needed and use the apropriate"
    " version. If lazy is True/False, force the version used between"
    " Loop/LoopGC and Stack.", ConfigParam('None', filter_vm_lazy))

raise_with_op = link.raise_with_op


class VM(object):
    """
    A VM object's __call__ method evaluates a Theano program.

    The Stack should be considered the reference VM/Linker implementation.
    It can correctly evaluate all graphs and is the easiest to read. The CVM
    is a port of Stack, and should have the same behavior, but run faster.
    The CVM's code is harder to read though.

    The other python VMs are maybe not necessary anymore, and don't take
    advantage of lazy computation, though they still produce the correct
예제 #17
0
    if val == 'False' or val is False:
        return False
    elif val == 'True' or val is True:
        return True
    elif val == 'None' or val is None:
        return None
    else:
        raise ValueError('Valid values for an vm.lazy parameter '
                         'should be None, False or True, not `%s`.' % val)


AddConfigVar('vm.lazy', "Useful only for the vm linkers. When lazy is None,"
             " auto detect if lazy evaluation is needed and use the apropriate"
             " version. If lazy is True/False, force the version used between"
             " Loop/LoopGC and Stack.",
             ConfigParam('None', filter_vm_lazy),
             in_c_key=False)


def calculate_reallocate_info(order, fgraph, storage_map, compute_map_re,
                              dependencies):
    reallocated_info = {}
    viewed_by = {}
    for var in fgraph.variables:
        viewed_by[var] = []
    view_of = {}
    pre_allocated = set([])
    allocated = set([])

    for idx in range(len(order)):
        node = order[idx]
예제 #18
0

def filter_nvcc_flags(s):
    assert isinstance(s, str)
    flags = [flag for flag in s.split(' ') if flag]
    if any([f for f in flags if not f.startswith("-")]):
        raise ValueError(
            "Theano nvcc.flags support only parameter/value pairs without"
            " space between them. e.g.: '--machine 64' is not supported,"
            " but '--machine=64' is supported. Please add the '=' symbol."
            " nvcc.flags value is '%s'" % s)
    return ' '.join(flags)

AddConfigVar('nvcc.flags',
             "Extra compiler flags for nvcc",
             ConfigParam("", filter_nvcc_flags),
             # Not needed in c key as it is already added.
             # We remove it as we don't make the md5 of config to change
             # if theano.sandbox.cuda is loaded or not.
             in_c_key=False)

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

AddConfigVar('nvcc.fastmath',
             "",
             BoolParam(False),
             # Not needed in c key as it is already added.
예제 #19
0
def filter_nvcc_flags(s):
    assert isinstance(s, str)
    flags = [flag for flag in s.split(' ') if flag]
    if any([f for f in flags if not f.startswith("-")]):
        raise ValueError(
            "Theano nvcc.flags support only parameter/value pairs without"
            " space between them. e.g.: '--machine 64' is not supported,"
            " but '--machine=64' is supported. Please add the '=' symbol."
            " nvcc.flags value is '%s'" % s)
    return ' '.join(flags)


AddConfigVar(
    'nvcc.flags',
    "Extra compiler flags for nvcc",
    ConfigParam("", filter_nvcc_flags),
    # Not needed in c key as it is already added.
    # We remove it as we don't make the md5 of config to change
    # if theano.sandbox.cuda is loaded or not.
    in_c_key=False)

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

AddConfigVar(
    'nvcc.fastmath',
    "",
    BoolParam(False),