提交 f881f23c authored 作者: Frédéric Bastien's avatar Frédéric Bastien 提交者: GitHub

Merge pull request #6410 from notoraptor/fix-cenumtype-test

Fix unit tests for CEnumType.
#ifndef THEANO_TEST_CENUM
#define THEANO_TEST_CENUM
#define MILLION 1000000
#define BILLION 1000000000
#define TWO_BILLIONS 2000000000
#endif
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import os
import numpy as np import numpy as np
import theano import theano
...@@ -145,43 +146,39 @@ class MyOpEnumList(Op): ...@@ -145,43 +146,39 @@ class MyOpEnumList(Op):
class MyOpCEnumType(Op): class MyOpCEnumType(Op):
__props__ = ('ctype_index',) __props__ = ('python_value',)
params_type = CEnumType('SIZE_INT', 'SIZE_FLOAT', 'SIZE_LONG_LONG', ctype='size_t') params_type = CEnumType(('MILLION', 'million'), ('BILLION', 'billion'), ('TWO_BILLIONS', 'two_billions'),
ctype='size_t')
# Just for testing, we define our own macros. def c_header_dirs(self):
def c_support_code(self): return [os.path.join(os.path.dirname(__file__), 'c_code')]
return """
#define SIZE_INT sizeof(int) def c_headers(self):
#define SIZE_FLOAT sizeof(float) return ['test_cenum.h']
#define SIZE_LONG_LONG sizeof(long long)
""" def __init__(self, value_name):
# As we see, Python values of constants are not related to real C values.
def __init__(self, ctype): assert self.params_type.MILLION == 0
# As we see, Python values of constants are not related to real C values assert self.params_type.BILLION == 1
# (sizeof(int) will never be 0). assert self.params_type.TWO_BILLIONS == 2
assert self.params_type.SIZE_INT == 0 assert self.params_type.has_alias(value_name)
assert self.params_type.SIZE_FLOAT == 1 self.python_value = self.params_type.fromalias(value_name)
assert self.params_type.SIZE_LONG_LONG == 2
d = {'int': self.params_type.SIZE_INT,
'float': self.params_type.SIZE_FLOAT,
'long long': self.params_type.SIZE_LONG_LONG}
self.ctype_index = d[ctype]
def get_params(self, node): def get_params(self, node):
return self.ctype_index return self.python_value
def make_node(self): def make_node(self):
return Apply(self, [], [scalar.uint32()]) return Apply(self, [], [scalar.uint32()])
def c_code_cache_version(self): def c_code_cache_version(self):
return (1,) return (3,)
def c_code(self, node, name, inputs, outputs, sub): def c_code(self, node, name, inputs, outputs, sub):
return """ return """
%(o)s = %(sizeof_ctype)s; %(o)s = %(val)s;
""" % dict(o=outputs[0], """ % dict(o=outputs[0],
# params in C code will already contains expected C constant value. # params in C code will already contains expected C constant value.
sizeof_ctype=sub['params']) val=sub['params'])
class TestEnumTypes(TestCase): class TestEnumTypes(TestCase):
...@@ -254,9 +251,15 @@ class TestEnumTypes(TestCase): ...@@ -254,9 +251,15 @@ class TestEnumTypes(TestCase):
def test_op_with_cenumtype(self): def test_op_with_cenumtype(self):
if theano.config.cxx == '': if theano.config.cxx == '':
raise SkipTest('need c++') raise SkipTest('need c++')
sizeof_int = MyOpCEnumType('int')() million = MyOpCEnumType('million')()
sizeof_float = MyOpCEnumType('float')() billion = MyOpCEnumType('billion')()
sizeof_long_long = MyOpCEnumType('long long')() two_billions = MyOpCEnumType('two_billions')()
f = theano.function([], [sizeof_int, sizeof_float, sizeof_long_long]) f = theano.function([], [million, billion, two_billions])
out = f() val_million, val_billion, val_two_billions = f()
print('(sizeof(int): ', out[0], ', sizeof(float): ', out[1], ', sizeof(long long): ', out[2], ') ', sep='') assert val_million == 1000000
assert val_billion == val_million * 1000
assert val_two_billions == val_billion * 2
@theano.change_flags(**{'cmodule.debug': True})
def test_op_with_cenumtype_debug(self):
self.test_op_with_cenumtype()
...@@ -1211,7 +1211,7 @@ class CEnumType(EnumList): ...@@ -1211,7 +1211,7 @@ class CEnumType(EnumList):
Like :class:`EnumList`, you can also add an alias to a constant, with same syntax as in :class:`EnumList`. Like :class:`EnumList`, you can also add an alias to a constant, with same syntax as in :class:`EnumList`.
See test class :class:`theano.gof.tests.test_types.TestOpCEnumType` for a working example. See test :func:`theano.gof.tests.test_types.TestEnumTypes.test_op_with_cenumtype` for a working example.
.. note:: .. note::
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论