Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
574d0203
提交
574d0203
authored
4月 13, 2017
作者:
notoraptor
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Address @nouiz 's comments.
上级
3654195c
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
35 行增加
和
13 行删除
+35
-13
params_type.py
theano/gof/params_type.py
+12
-11
test_params_type.py
theano/gof/tests/test_params_type.py
+10
-2
test_types.py
theano/gof/tests/test_types.py
+8
-0
type.py
theano/gof/type.py
+5
-0
没有找到文件。
theano/gof/params_type.py
浏览文件 @
574d0203
...
@@ -238,7 +238,7 @@ class ParamsType(Type):
...
@@ -238,7 +238,7 @@ class ParamsType(Type):
self
.
length
=
len
(
kwargs
)
self
.
length
=
len
(
kwargs
)
self
.
fields
=
tuple
(
sorted
(
kwargs
.
keys
()))
self
.
fields
=
tuple
(
sorted
(
kwargs
.
keys
()))
self
.
types
=
tuple
(
kwargs
[
field
]
for
field
in
self
.
fields
)
self
.
types
=
tuple
(
kwargs
[
field
]
for
field
in
self
.
fields
)
self
.
name
=
self
.
__
generate_struct_name
()
self
.
name
=
self
.
generate_struct_name
()
self
.
__const_to_enum
=
{}
self
.
__const_to_enum
=
{}
self
.
__alias_to_enum
=
{}
self
.
__alias_to_enum
=
{}
...
@@ -246,10 +246,15 @@ class ParamsType(Type):
...
@@ -246,10 +246,15 @@ class ParamsType(Type):
if
enum_types
:
if
enum_types
:
# We don't want same enum names in different enum types.
# We don't want same enum names in different enum types.
if
sum
(
len
(
t
)
for
t
in
enum_types
)
!=
len
(
set
(
k
for
t
in
enum_types
for
k
in
t
)):
if
sum
(
len
(
t
)
for
t
in
enum_types
)
!=
len
(
set
(
k
for
t
in
enum_types
for
k
in
t
)):
raise
AttributeError
(
'ParamsType: found different enum types with common constant names.'
)
raise
AttributeError
(
'ParamsType: found different enum types with common constant
s
names.'
)
# We don't want same aliases in different enum types.
# We don't want same aliases in different enum types.
if
sum
(
len
(
t
.
aliases
)
for
t
in
enum_types
)
!=
len
(
set
(
alias
for
t
in
enum_types
for
alias
in
t
.
aliases
)):
if
sum
(
len
(
t
.
aliases
)
for
t
in
enum_types
)
!=
len
(
set
(
alias
for
t
in
enum_types
for
alias
in
t
.
aliases
)):
raise
AttributeError
(
'ParamsType: found different enum types with common constant aliases.'
)
raise
AttributeError
(
'ParamsType: found different enum types with common constants aliases.'
)
# We don't want aliases that have same names as some constants.
all_enums
=
{
e
for
t
in
enum_types
for
e
in
t
}
all_aliases
=
{
a
for
t
in
enum_types
for
a
in
t
.
aliases
}
if
[
a
for
a
in
all_aliases
if
a
in
all_enums
]:
raise
AttributeError
(
'ParamsType: found aliases that have same names as constants.'
)
# We map each enum name to the enum type in which it is defined.
# We map each enum name to the enum type in which it is defined.
# We will then use this dict to find enum value when looking for enum name in Wrapper object directly.
# We will then use this dict to find enum value when looking for enum name in Wrapper object directly.
self
.
__const_to_enum
=
{
enum_name
:
enum_type
for
enum_type
in
enum_types
for
enum_name
in
enum_type
}
self
.
__const_to_enum
=
{
enum_name
:
enum_type
for
enum_type
in
enum_types
for
enum_name
in
enum_type
}
...
@@ -270,7 +275,7 @@ class ParamsType(Type):
...
@@ -270,7 +275,7 @@ class ParamsType(Type):
def
__hash__
(
self
):
def
__hash__
(
self
):
return
hash
((
type
(
self
),)
+
self
.
fields
+
self
.
types
)
return
hash
((
type
(
self
),)
+
self
.
fields
+
self
.
types
)
def
__
generate_struct_name
(
self
):
def
generate_struct_name
(
self
):
# This method tries to generate an unique name for the current instance.
# This method tries to generate an unique name for the current instance.
# This name is intended to be used as struct name in C code and as constant
# This name is intended to be used as struct name in C code and as constant
# definition to check if a similar ParamsType has already been created
# definition to check if a similar ParamsType has already been created
...
@@ -396,7 +401,7 @@ class ParamsType(Type):
...
@@ -396,7 +401,7 @@ class ParamsType(Type):
value_for_c = False
value_for_c = False
# Value for c can't be retrieved from o, so we add a value for that field in kwargs.
# Value for c can't be retrieved from o, so we add a value for that field in kwargs.
params
1
= params_type.get_params(o, c=value_for_c)
params = params_type.get_params(o, c=value_for_c)
# params.a contains 10
# params.a contains 10
# params.b contains [[1, 2, 3], [4, 5, 6]]
# params.b contains [[1, 2, 3], [4, 5, 6]]
# params.c contains value_for_c
# params.c contains value_for_c
...
@@ -519,7 +524,6 @@ class ParamsType(Type):
...
@@ -519,7 +524,6 @@ class ParamsType(Type):
struct_name
=
self
.
name
struct_name
=
self
.
name
struct_name_defined
=
struct_name
.
upper
()
struct_name_defined
=
struct_name
.
upper
()
c_support_code_set
=
set
()
c_support_code_set
=
set
()
c_support_code_list
=
[]
c_declare_list
=
[]
c_declare_list
=
[]
c_init_list
=
[]
c_init_list
=
[]
c_cleanup_list
=
[]
c_cleanup_list
=
[]
...
@@ -527,10 +531,7 @@ class ParamsType(Type):
...
@@ -527,10 +531,7 @@ class ParamsType(Type):
for
attribute_name
,
type_instance
in
zip
(
self
.
fields
,
self
.
types
):
for
attribute_name
,
type_instance
in
zip
(
self
.
fields
,
self
.
types
):
try
:
try
:
c_support_code_current
=
type_instance
.
c_support_code
()
c_support_code_set
.
add
(
type_instance
.
c_support_code
())
if
c_support_code_current
not
in
c_support_code_set
:
c_support_code_list
.
append
(
c_support_code_current
)
c_support_code_set
.
add
(
c_support_code_current
)
except
MethodNotDefined
:
except
MethodNotDefined
:
pass
pass
...
@@ -549,7 +550,7 @@ class ParamsType(Type):
...
@@ -549,7 +550,7 @@ class ParamsType(Type):
'extract_code'
:
type_instance
.
c_extract
(
attribute_name
,
sub
)
'extract_code'
:
type_instance
.
c_extract
(
attribute_name
,
sub
)
})
})
support_code
=
'
\n
'
.
join
(
c_support_code_list
)
support_code
=
'
\n
'
.
join
(
sorted
(
list
(
c_support_code_set
))
)
struct_declare
=
'
\n
'
.
join
(
c_declare_list
)
struct_declare
=
'
\n
'
.
join
(
c_declare_list
)
struct_init
=
'
\n
'
.
join
(
c_init_list
)
struct_init
=
'
\n
'
.
join
(
c_init_list
)
struct_cleanup
=
'
\n
'
.
join
(
c_cleanup_list
)
struct_cleanup
=
'
\n
'
.
join
(
c_cleanup_list
)
...
...
theano/gof/tests/test_params_type.py
浏览文件 @
574d0203
...
@@ -214,14 +214,22 @@ class TestParamsType(TestCase):
...
@@ -214,14 +214,22 @@ class TestParamsType(TestCase):
assert
w
.
values_eq_approx
(
o1
,
o3
)
assert
w
.
values_eq_approx
(
o1
,
o3
)
def
test_params_type_with_enums
(
self
):
def
test_params_type_with_enums
(
self
):
# Test that we fail if we create a
wrapper
with common enum names inside different enum types.
# Test that we fail if we create a
params type
with common enum names inside different enum types.
try
:
try
:
w
=
ParamsType
(
enum1
=
EnumList
(
'A'
,
'B'
,
'C'
),
enum2
=
EnumList
(
'A'
,
'B'
,
'F'
))
ParamsType
(
enum1
=
EnumList
(
'A'
,
'B'
,
'C'
),
enum2
=
EnumList
(
'A'
,
'B'
,
'F'
))
except
AttributeError
:
except
AttributeError
:
pass
pass
else
:
else
:
raise
Exception
(
'ParamsType should fail with common enum names inside different enum types.'
)
raise
Exception
(
'ParamsType should fail with common enum names inside different enum types.'
)
# Test that we fail if we create a params type with common names in both aliases and constants.
try
:
ParamsType
(
enum1
=
EnumList
((
'A'
,
'a'
),
(
'B'
,
'b'
)),
enum2
=
EnumList
((
'ONE'
,
'a'
),
(
'TWO'
,
'two'
)))
except
AttributeError
:
ParamsType
(
enum1
=
EnumList
((
'A'
,
'a'
),
(
'B'
,
'b'
)),
enum2
=
EnumList
((
'ONE'
,
'one'
),
(
'TWO'
,
'two'
)))
else
:
raise
Exception
(
'ParamsType should fail when there are aliases with same names as some constants.'
)
# Test that we can access enum values through wrapper directly.
# Test that we can access enum values through wrapper directly.
w
=
ParamsType
(
enum1
=
EnumList
(
'A'
,
(
'B'
,
'beta'
),
'C'
),
enum2
=
EnumList
((
'D'
,
'delta'
),
'E'
,
'F'
))
w
=
ParamsType
(
enum1
=
EnumList
(
'A'
,
(
'B'
,
'beta'
),
'C'
),
enum2
=
EnumList
((
'D'
,
'delta'
),
'E'
,
'F'
))
assert
w
.
A
==
0
and
w
.
B
==
1
and
w
.
C
==
2
assert
w
.
A
==
0
and
w
.
B
==
1
and
w
.
C
==
2
...
...
theano/gof/tests/test_types.py
浏览文件 @
574d0203
...
@@ -228,6 +228,14 @@ class TestEnumTypes(TestCase):
...
@@ -228,6 +228,14 @@ class TestEnumTypes(TestCase):
assert
e1
.
filter
(
'beta'
)
==
e1
.
fromalias
(
'beta'
)
==
e1
.
B
==
1
assert
e1
.
filter
(
'beta'
)
==
e1
.
fromalias
(
'beta'
)
==
e1
.
B
==
1
assert
e1
.
filter
(
'C'
)
==
e1
.
fromalias
(
'C'
)
==
e1
.
C
==
2
assert
e1
.
filter
(
'C'
)
==
e1
.
fromalias
(
'C'
)
==
e1
.
C
==
2
# Check that invalid alias (same as a constant) raises exception.
try
:
EnumList
((
'A'
,
'a'
),
(
'B'
,
'B'
))
except
TypeError
:
EnumList
((
'A'
,
'a'
),
(
'B'
,
'b'
))
else
:
raise
Exception
(
'Enum with an alias name equal to a constant name should fail.'
)
def
test_op_with_enumlist
(
self
):
def
test_op_with_enumlist
(
self
):
a
=
scalar
.
int32
()
a
=
scalar
.
int32
()
b
=
scalar
.
int32
()
b
=
scalar
.
int32
()
...
...
theano/gof/type.py
浏览文件 @
574d0203
...
@@ -925,6 +925,9 @@ class EnumType(Type, dict):
...
@@ -925,6 +925,9 @@ class EnumType(Type, dict):
if
not
isinstance
(
alias
,
str
):
if
not
isinstance
(
alias
,
str
):
raise
TypeError
(
'
%
s: constant alias should be a string, got "
%
s".'
raise
TypeError
(
'
%
s: constant alias should be a string, got "
%
s".'
%
(
type
(
self
)
.
__name__
,
alias
))
%
(
type
(
self
)
.
__name__
,
alias
))
if
alias
==
k
:
raise
TypeError
(
"
%
s: it's useless to create an alias "
"with the same name as its associated constant."
%
type
(
self
)
.
__name__
)
if
alias
in
self
.
aliases
:
if
alias
in
self
.
aliases
:
raise
TypeError
(
'
%
s: consant alias "
%
s" already used.'
%
(
type
(
self
)
.
__name__
,
alias
))
raise
TypeError
(
'
%
s: consant alias "
%
s" already used.'
%
(
type
(
self
)
.
__name__
,
alias
))
self
.
aliases
[
alias
]
=
k
self
.
aliases
[
alias
]
=
k
...
@@ -934,6 +937,8 @@ class EnumType(Type, dict):
...
@@ -934,6 +937,8 @@ class EnumType(Type, dict):
elif
not
isinstance
(
kwargs
[
k
],
(
int
,
float
)):
elif
not
isinstance
(
kwargs
[
k
],
(
int
,
float
)):
raise
TypeError
(
'
%
s: constant "
%
s": expected integer or floating value, got "
%
s".'
raise
TypeError
(
'
%
s: constant "
%
s": expected integer or floating value, got "
%
s".'
%
(
type
(
self
)
.
__name__
,
k
,
type
(
kwargs
[
k
])
.
__name__
))
%
(
type
(
self
)
.
__name__
,
k
,
type
(
kwargs
[
k
])
.
__name__
))
if
[
a
for
a
in
self
.
aliases
if
a
in
self
]:
raise
TypeError
(
"
%
s: some aliases have same names as constants."
%
type
(
self
)
.
__name__
)
super
(
EnumType
,
self
)
.
__init__
(
**
kwargs
)
super
(
EnumType
,
self
)
.
__init__
(
**
kwargs
)
def
fromalias
(
self
,
alias
):
def
fromalias
(
self
,
alias
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论