Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
220e6ddb
提交
220e6ddb
authored
3月 20, 2017
作者:
notoraptor
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update doc for enum classes.
Fix c_code_cache_version and typos in enum tests.
上级
94f092ae
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
28 行增加
和
27 行删除
+28
-27
test_types.py
theano/gof/tests/test_types.py
+8
-9
type.py
theano/gof/type.py
+20
-18
没有找到文件。
theano/gof/tests/test_types.py
浏览文件 @
220e6ddb
...
@@ -117,7 +117,7 @@ class TestOpEnumList(Op):
...
@@ -117,7 +117,7 @@ class TestOpEnumList(Op):
raise
NotImplementedError
(
'Unknown op id '
+
str
(
op
))
raise
NotImplementedError
(
'Unknown op id '
+
str
(
op
))
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
None
return
(
1
,)
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
a
,
b
=
inputs
a
,
b
=
inputs
...
@@ -125,7 +125,7 @@ class TestOpEnumList(Op):
...
@@ -125,7 +125,7 @@ class TestOpEnumList(Op):
fail
=
sub
[
'fail'
]
fail
=
sub
[
'fail'
]
op
=
sub
[
'params'
]
op
=
sub
[
'params'
]
return
"""
return
"""
switch(
(int)
%(op)
s) {
switch(
%(op)
s) {
case ADD:
case ADD:
%(o)
s =
%(a)
s +
%(b)
s;
%(o)
s =
%(a)
s +
%(b)
s;
break;
break;
...
@@ -175,14 +175,14 @@ class TestOpCEnumType(Op):
...
@@ -175,14 +175,14 @@ class TestOpCEnumType(Op):
return
Apply
(
self
,
[],
[
scalar
.
uint32
()])
return
Apply
(
self
,
[],
[
scalar
.
uint32
()])
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
None
return
(
1
,)
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
def
c_code
(
self
,
node
,
name
,
inputs
,
outputs
,
sub
):
o
,
=
outputs
o
,
=
outputs
ctype_index
=
sub
[
'params'
]
# params in C code will already contains expected C constant value.
sizeof_ctype
=
sub
[
'params'
]
return
"""
return
"""
/* ctype_index already contains expected C constant value. */
%(o)
s =
%(sizeof_ctype)
s;
%(o)
s =
%(ctype_index)
s;
"""
%
locals
()
"""
%
locals
()
...
@@ -205,7 +205,7 @@ def test_enum_class():
...
@@ -205,7 +205,7 @@ def test_enum_class():
# Check that invalid enum value raises exception.
# Check that invalid enum value raises exception.
try
:
try
:
EnumType
(
INVALID_VALUE
=
'string is not allowe.'
)
EnumType
(
INVALID_VALUE
=
'string is not allowe
d
.'
)
except
ValueError
:
except
ValueError
:
pass
pass
else
:
else
:
...
@@ -220,7 +220,6 @@ def test_enum_class():
...
@@ -220,7 +220,6 @@ def test_enum_class():
def
test_op_with_enumlist
():
def
test_op_with_enumlist
():
# Test an op with EnumList.
a
=
scalar
.
int32
()
a
=
scalar
.
int32
()
b
=
scalar
.
int32
()
b
=
scalar
.
int32
()
c_add
=
TestOpEnumList
(
'+'
)(
a
,
b
)
c_add
=
TestOpEnumList
(
'+'
)(
a
,
b
)
...
@@ -241,4 +240,4 @@ def test_op_with_cenumtype():
...
@@ -241,4 +240,4 @@ def test_op_with_cenumtype():
sizeof_long_long
=
TestOpCEnumType
(
'long long'
)()
sizeof_long_long
=
TestOpCEnumType
(
'long long'
)()
f
=
theano
.
function
([],
[
sizeof_int
,
sizeof_float
,
sizeof_long_long
])
f
=
theano
.
function
([],
[
sizeof_int
,
sizeof_float
,
sizeof_long_long
])
out
=
f
()
out
=
f
()
print
(
'(sizeof(int): '
,
out
[
0
],
', sizeof(float): '
,
out
[
1
],
', sizeof(long long): '
,
out
[
2
],
') '
,
sep
=
''
,
end
=
''
)
print
(
'(sizeof(int): '
,
out
[
0
],
', sizeof(float): '
,
out
[
1
],
', sizeof(long long): '
,
out
[
2
],
') '
,
sep
=
''
)
theano/gof/type.py
浏览文件 @
220e6ddb
...
@@ -813,46 +813,44 @@ CDataType.Constant = CDataTypeConstant
...
@@ -813,46 +813,44 @@ CDataType.Constant = CDataTypeConstant
class
EnumType
(
Type
,
dict
):
class
EnumType
(
Type
,
dict
):
"""
"""
C
lass that allows to create enumerations of constant values.
Op parameter c
lass that allows to create enumerations of constant values.
- Constants are available as object attributes in Python code and as macro-defined constants in C code.
- Constants are available as object attributes in Python code and as macro-defined constants in C code.
- Constants can be floating values, integers, or booleans (automatically converted to integers).
- Constants can be floating values, integers, or booleans (automatically converted to integers).
- Constants name must start with a capital letter and contain capital letters, underscores or digits.
- Constants name must start with a capital letter and contain capital letters, underscores or digits.
This type is intended to be used as op parameter type.
Example::
Example::
enum = EnumType(CONSTANT_1=
0, CONSTANT_2=1, CONSTANT_3=2.5, CONSTANT_4=False, CONSTANT_5
=True)
enum = EnumType(CONSTANT_1=
1, CONSTANT_2=2.5, CONSTANT_3=False, CONSTANT_4
=True)
print (enum.CONSTANT_1, enum.CONSTANT_2, enum.CONSTANT_3, enum.CONSTANT_4
, enum.CONSTANT_5
)
print (enum.CONSTANT_1, enum.CONSTANT_2, enum.CONSTANT_3, enum.CONSTANT_4)
# will print
0
1 2.5 0 1
# will print 1 2.5 0 1
In C code:
In C code:
.. code-block:: c
.. code-block:: c
int constant_1 = CONSTANT_1;
int constant_1 = CONSTANT_1;
int constant_2 = CONSTANT_2;
double constant_2 = CONSTANT_2;
double constant_3 = CONSTANT_3;
int constant_3 = CONSTANT_3; // constant_3 == 0
int constant_4 = CONSTANT_4; // constant_4 == 0
int constant_4 = CONSTANT_4; // constant_4 == 1
int constant_5 = CONSTANT_5; // constant_5 == 1
You can also specify a C type
if you want to use an op param to handle these enum values
.
You can also specify a C type
for the op param if you want to pass one of these constant values at runtime
.
Default C type is ``double``.
Default C type is ``double``.
.. code-block:: python
.. code-block:: python
enum = EnumType(CONSTANT_1=0, CONSTANT_2=1, CONSTANT_3=2, ctype='size_t')
enum = EnumType(CONSTANT_1=0, CONSTANT_2=1, CONSTANT_3=2, ctype='size_t')
op_param_value = enum.CONSTANT_1
In C code:
In C code:
.. code-block:: c
.. code-block:: c
size_t
op_param = CONSTANT_1;
size_t
value = op_param_value; // contains enum.CONSTANT_1, i.e 0
.. note::
.. note::
This Type is not complete and should never be used for regular graph operations.
This Type
(and subclasses)
is not complete and should never be used for regular graph operations.
"""
"""
...
@@ -971,7 +969,7 @@ class EnumType(Type, dict):
...
@@ -971,7 +969,7 @@ class EnumType(Type, dict):
class
EnumList
(
EnumType
):
class
EnumList
(
EnumType
):
"""
"""
C
lass that allows to create enumeration of constant values.
Op parameter c
lass that allows to create enumeration of constant values.
Same as :class:`EnumType`, but automatically gives an unique integer value for each constant in a list of
Same as :class:`EnumType`, but automatically gives an unique integer value for each constant in a list of
constants names (constant at index ``i`` in the list will receive value ``i``,
constants names (constant at index ``i`` in the list will receive value ``i``,
with ``i`` from ``0`` to ``len(constants) - 1``).
with ``i`` from ``0`` to ``len(constants) - 1``).
...
@@ -982,11 +980,13 @@ class EnumList(EnumType):
...
@@ -982,11 +980,13 @@ class EnumList(EnumType):
print (enum.CONSTANT_1, enum.CONSTANT_2, enum.CONSTANT_3, enum.CONSTANT_4, enum.CONSTANT_5)
print (enum.CONSTANT_1, enum.CONSTANT_2, enum.CONSTANT_3, enum.CONSTANT_4, enum.CONSTANT_5)
# will print: 0 1 2 3 4
# will print: 0 1 2 3 4
Like :class:`EnumType`, you can also define the C type for
a variable able to handle these enum values
.
Like :class:`EnumType`, you can also define the C type for
the op param
.
Default C type is ``int``::
Default C type is ``int``::
enum = EnumList('CONSTANT_1', 'CONSTANT_2', 'CONSTANT_3', 'CONSTANT_4', ctype='unsigned int')
enum = EnumList('CONSTANT_1', 'CONSTANT_2', 'CONSTANT_3', 'CONSTANT_4', ctype='unsigned int')
See test class :class:`theano.gof.tests.test_types.TestOpEnumList` for a working example.
"""
"""
def
__init__
(
self
,
*
args
,
**
kwargs
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
...
@@ -1004,7 +1004,7 @@ class EnumList(EnumType):
...
@@ -1004,7 +1004,7 @@ class EnumList(EnumType):
class
CEnumType
(
EnumList
):
class
CEnumType
(
EnumList
):
"""
"""
C
lass that allows to create enumeration of constant values that represent C-defined constants.
Op parameter c
lass that allows to create enumeration of constant values that represent C-defined constants.
- Constant should have same names as in C.
- Constant should have same names as in C.
- In Python, constants will have arbitrary-defined values.
- In Python, constants will have arbitrary-defined values.
...
@@ -1012,17 +1012,19 @@ class CEnumType(EnumList):
...
@@ -1012,17 +1012,19 @@ class CEnumType(EnumList):
- In C code, the real values defined in C will be used.
- In C code, the real values defined in C will be used.
They could be used either for choices or for its real values.
They could be used either for choices or for its real values.
Like :class:`EnumList`, you can also define the C type for
a variable able to handle these enum values
.
Like :class:`EnumList`, you can also define the C type for
the op param
.
Default C type is ``int``.
Default C type is ``int``.
.. code-block:: python
.. code-block:: python
enum = CEnumType('CONSTANT_CNAME_1', 'CONSTANT_CNAME_2', 'CONSTANT_CNAME_3', ctype='long')
enum = CEnumType('CONSTANT_CNAME_1', 'CONSTANT_CNAME_2', 'CONSTANT_CNAME_3', ctype='long')
See test class :class:`theano.gof.tests.test_types.TestOpCEnumType` for a working example.
.. note::
.. note::
Be sure C constants are available in your C code. If they come from a C header, consider implementing
Be sure C constants are available in your C code. If they come from a C header, consider implementing
``c_headers()`` and ``c_header_dirs()`` in the Op class wh
ich you use CEnumType as op parameters
type.
``c_headers()`` and ``c_header_dirs()`` in the Op class wh
ere you use CEnumType as op parameter
type.
"""
"""
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论