提交 37566d42 authored 作者: notoraptor's avatar notoraptor

Update CEnumType testing.

上级 23d7f12f
#ifndef THEANO_TEST_CENUM #ifndef THEANO_TEST_CENUM
#define THEANO_TEST_CENUM #define THEANO_TEST_CENUM
#define SIZE_INT 0 #define MILLION 1000000
#define SIZE_FLOAT 1 #define BILLION 1000000000
#define SIZE_LONG_LONG 2 #define TWO_BILLIONS 2000000000
/* NB:
For this specific test, we can not directly use macros with corresponding values (e.g. `SIZEOF_INT == sizeof(int)`)
because different types may have same size (e.g. sizeof(int) and sizeof(float) will be 4 on most machines),
leading to compilation errors in debug mode (inside a switch loop, SIZE_INT and SIZE_FLOAT would have the same
value, so that it is impossible to distinguish them, for e.g. to print macro name).
*/
static size_t type_sizes[] = {sizeof(int), sizeof(float), sizeof(long long)};
#endif #endif
...@@ -146,8 +146,9 @@ class MyOpEnumList(Op): ...@@ -146,8 +146,9 @@ 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')
def c_header_dirs(self): def c_header_dirs(self):
return [os.path.join(os.path.dirname(__file__), 'c_code')] return [os.path.join(os.path.dirname(__file__), 'c_code')]
...@@ -155,32 +156,29 @@ class MyOpCEnumType(Op): ...@@ -155,32 +156,29 @@ class MyOpCEnumType(Op):
def c_headers(self): def c_headers(self):
return ['test_cenum.h'] return ['test_cenum.h']
def __init__(self, ctype): def __init__(self, value_name):
# As we see, Python values of constants are not related to real C values # As we see, Python values of constants are not related to real C values.
# (sizeof(int) will never be 0). assert self.params_type.MILLION == 0
assert self.params_type.SIZE_INT == 0 assert self.params_type.BILLION == 1
assert self.params_type.SIZE_FLOAT == 1 assert self.params_type.TWO_BILLIONS == 2
assert self.params_type.SIZE_LONG_LONG == 2 assert self.params_type.has_alias(value_name)
d = {'int': self.params_type.SIZE_INT, self.python_value = self.params_type.fromalias(value_name)
'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 (2,) return (3,)
def c_code(self, node, name, inputs, outputs, sub): def c_code(self, node, name, inputs, outputs, sub):
return """ return """
%(o)s = type_sizes[%(typecode)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.
typecode=sub['params']) val=sub['params'])
class TestEnumTypes(TestCase): class TestEnumTypes(TestCase):
...@@ -253,12 +251,14 @@ class TestEnumTypes(TestCase): ...@@ -253,12 +251,14 @@ 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}) @theano.change_flags(**{'cmodule.debug': True})
def test_op_with_cenumtype_debug(self): def test_op_with_cenumtype_debug(self):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论