Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
c40eadf8
提交
c40eadf8
authored
1月 23, 2008
作者:
olivier@olivier-desktop
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
bla
上级
2aa7089e
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
37 行增加
和
143 行删除
+37
-143
core.py
core.py
+37
-140
type_spec.py
type_spec.py
+0
-3
没有找到文件。
core.py
浏览文件 @
c40eadf8
...
@@ -90,13 +90,6 @@ def _hashable(x):
...
@@ -90,13 +90,6 @@ def _hashable(x):
return
False
return
False
def
_literal_hashable
(
x
):
def
_literal_hashable
(
x
):
# try:
# present = x in literals_db
# hashable = True
# except TypeError: # x is unhashable
# present = False
# hashable = False
if
x
in
literals_db
:
if
x
in
literals_db
:
return
literals_db
[
x
]
return
literals_db
[
x
]
else
:
else
:
...
@@ -105,20 +98,6 @@ def _literal_hashable(x):
...
@@ -105,20 +98,6 @@ def _literal_hashable(x):
literals_db
[
x
]
=
r
literals_db
[
x
]
=
r
return
r
return
r
# elif isinstance(x, numpy.ndarray):
# ret = NumpyR(x, constant = True)
# elif isinstance(x, (int, float)):
# ret = NumpyR(numpy.array(x), constant = True)
# elif isinstance(x, gof.Result):
# raise TypeError("%s is already a result." % x)
# else:
# return PythonR(x, constant = True)
# if hashable:
# literals_db[x] = ret
# return ret
def
_literal_unhashable
(
x
):
def
_literal_unhashable
(
x
):
idx
=
id
(
x
)
idx
=
id
(
x
)
if
idx
in
literals_id_db
:
if
idx
in
literals_id_db
:
...
@@ -129,7 +108,6 @@ def _literal_unhashable(x):
...
@@ -129,7 +108,6 @@ def _literal_unhashable(x):
literals_id_db
[
idx
]
=
r
literals_id_db
[
idx
]
=
r
return
r
return
r
def
literal
(
x
):
def
literal
(
x
):
if
_hashable
(
x
):
if
_hashable
(
x
):
return
_literal_hashable
(
x
)
return
_literal_hashable
(
x
)
...
@@ -141,28 +119,25 @@ inplace = gof.Destroyer
...
@@ -141,28 +119,25 @@ inplace = gof.Destroyer
view
=
gof
.
Viewer
view
=
gof
.
Viewer
def
cgetspecs
(
names
,
vals
,
converters
):
d
=
{}
for
name
,
value
in
zip
(
names
,
vals
):
d
[
name
]
=
value
.
data
specs
=
weave
.
ext_tools
.
assign_variable_types
(
names
,
d
,
type_converters
=
converters
)
#, auto_downcast = 0)
return
d
,
specs
def
cgen
(
name
,
behavior
,
inames
,
ivals
,
onames
,
ovals
,
converters
=
None
):
def
cgen
(
name
,
behavior
,
inames
,
ivals
,
onames
,
ovals
,
converters
=
None
):
if
not
converters
:
if
not
converters
:
converters
=
type_spec
.
default
converters
=
type_spec
.
default
for
converter
in
converters
:
for
converter
in
converters
:
assert
isinstance
(
converter
,
type_spec
.
omega_type_converter_extension
)
assert
isinstance
(
converter
,
type_spec
.
omega_type_converter_extension
)
d
=
{}
d
,
specs
=
cgetspecs
(
inames
+
onames
,
ivals
+
ovals
,
converters
)
for
name
,
value
in
zip
(
inames
+
onames
,
ivals
+
ovals
):
d
[
name
]
=
value
.
data
# print inames + onames
# print d
# print [x.__class__ for x in converters]
specs
=
weave
.
ext_tools
.
assign_variable_types
(
inames
+
onames
,
d
,
type_converters
=
converters
)
#, auto_downcast = 0)
template
=
{}
template
=
{}
template
[
'name'
]
=
name
template
[
'name'
]
=
name
template
[
'code'
]
=
behavior
template
[
'code'
]
=
behavior
template
[
'members'
]
=
"
\n
"
.
join
([
spec
.
struct_members_code
()
for
spec
in
specs
])
template
[
'members'
]
=
"
\n
"
.
join
([
spec
.
struct_members_code
()
for
spec
in
specs
])
template
[
'decl'
]
=
"
\n
"
.
join
([
spec
.
struct_declaration_code
()
for
spec
in
specs
])
# types = [spec.struct_template_types() for spec in specs]
# template['types'] = ", ".join([", ".join([c_type for c_type, name, init in spec.provides()]) for spec in specs])
# template['typenames'] = ", ".join([spec.struct_template_code() for spec in specs])
template
[
'support'
]
=
"
\n
"
.
join
([
spec
.
struct_support_code
()
for
spec
in
specs
])
template
[
'support'
]
=
"
\n
"
.
join
([
spec
.
struct_support_code
()
for
spec
in
specs
])
template
[
'typedefs'
]
=
"
\n
"
.
join
([
spec
.
struct_typedefs
()
for
spec
in
specs
])
template
[
'typedefs'
]
=
"
\n
"
.
join
([
spec
.
struct_typedefs
()
for
spec
in
specs
])
...
@@ -174,7 +149,6 @@ def cgen(name, behavior, inames, ivals, onames, ovals, converters = None):
...
@@ -174,7 +149,6 @@ def cgen(name, behavior, inames, ivals, onames, ovals, converters = None):
%(support)
s
%(support)
s
void execute(void) {
void execute(void) {
%(decl)
s
%(code)
s
%(code)
s
}
}
"""
%
template
"""
%
template
...
@@ -184,25 +158,20 @@ def cgen(name, behavior, inames, ivals, onames, ovals, converters = None):
...
@@ -184,25 +158,20 @@ def cgen(name, behavior, inames, ivals, onames, ovals, converters = None):
struct
=
"struct
%(struct_name)
s {
%(struct_contents)
s
\n
};"
%
template
struct
=
"struct
%(struct_name)
s {
%(struct_contents)
s
\n
};"
%
template
# code = "_omega_%(name)s<%(types)s>* __STRUCT_P = new _omega_%(name)s();\n" % template
# code = "_omega_%(name)s<%(types)s>* __STRUCT_P = &_omega_%(name)s<%(types)s>();\n" % template
code
=
"
%(struct_name)
s* __STRUCT_P = &
%(struct_name)
s();
\n
"
%
template
code
=
"
%(struct_name)
s* __STRUCT_P = &
%(struct_name)
s();
\n
"
%
template
code
+=
"
\n
"
.
join
([
spec
.
struct_import_code
()
for
spec
in
specs
])
code
+=
"
\n
"
.
join
([
spec
.
struct_import_code
()
for
spec
in
specs
])
code
+=
"
\n
__STRUCT_P->execute();
\n
"
code
+=
"
\n
__STRUCT_P->execute();
\n
"
code
+=
"return_val = 10;"
code
+=
"return_val = 10;"
code
+=
"
\n
//
%(md5)
s"
%
template
code
+=
"
\n
//
%(md5)
s"
%
template
print
struct
return
d
,
code
,
struct
,
converters
print
code
for
spec
in
specs
:
print
spec
.
declaration_code
()
print
d
def
make_static
(
cls
,
fname
):
f
=
getattr
(
cls
,
fname
)
res
=
weave
.
inline
(
code
,
inames
+
onames
,
local_dict
=
d
,
global_dict
=
{},
support_code
=
struct
,
type_converters
=
converters
)
if
hasattr
(
f
,
'im_func'
):
f
=
f
.
im_func
return
res
,
None
setattr
(
cls
,
fname
,
staticmethod
(
f
))
...
@@ -212,27 +181,8 @@ class omega_op(gof.PythonOp):
...
@@ -212,27 +181,8 @@ class omega_op(gof.PythonOp):
@staticmethod
@staticmethod
def
__clsinit__
(
cls
,
name
,
bases
,
dct
):
def
__clsinit__
(
cls
,
name
,
bases
,
dct
):
# make grad a static method
for
fname
in
[
'grad'
,
'c_impl'
,
'c_alloc'
]:
grad
=
cls
.
grad
make_static
(
cls
,
fname
)
if
hasattr
(
grad
,
'im_func'
):
grad
=
grad
.
im_func
cls
.
grad
=
staticmethod
(
grad
)
# make c_impl a static method
c_impl
=
cls
.
c_impl
if
hasattr
(
c_impl
,
'im_func'
):
c_impl
=
c_impl
.
im_func
cls
.
c_impl
=
staticmethod
(
c_impl
)
# make c_alloc a static method
c_alloc
=
cls
.
c_alloc
if
hasattr
(
c_alloc
,
'im_func'
):
c_alloc
=
c_alloc
.
im_func
cls
.
c_alloc
=
staticmethod
(
c_alloc
)
# # adjust impl
# if cls.forbid_broadcast:
# cls.impl = assert_same_shapes(cls.impl)
# make impl a static method
# make impl a static method
gof
.
PythonOp
.
__clsinit__
(
cls
,
name
,
bases
,
dct
)
gof
.
PythonOp
.
__clsinit__
(
cls
,
name
,
bases
,
dct
)
...
@@ -254,51 +204,11 @@ class omega_op(gof.PythonOp):
...
@@ -254,51 +204,11 @@ class omega_op(gof.PythonOp):
def
grad
(
*
args
):
def
grad
(
*
args
):
return
UNDEFINED
return
UNDEFINED
def
c
reate_c
_code
(
self
,
converters
=
None
):
def
c_code
(
self
,
converters
=
None
):
behavior
=
self
.
c_impl
(
self
.
inputs
,
self
.
outputs
)
behavior
=
self
.
c_impl
(
self
.
inputs
,
self
.
outputs
)
(
inames
,
onames
),
_1
,
_2
,
_3
=
inspect
.
getargspec
(
self
.
c_impl
)
(
inames
,
onames
),
_1
,
_2
,
_3
=
inspect
.
getargspec
(
self
.
c_impl
)
return
cgen
(
self
.
__class__
.
__name__
,
behavior
,
inames
,
self
.
inputs
,
onames
,
self
.
outputs
,
converters
)
return
cgen
(
self
.
__class__
.
__name__
,
behavior
,
inames
,
self
.
inputs
,
onames
,
self
.
outputs
,
converters
)
## return code, struct
# behavior = self.c_impl(self.inputs, self.outputs)
# (inames, onames), _1, _2, _3 = inspect.getargspec(self.c_impl)
# d = {}
# for name, value in zip(inames + onames, self.inputs + self.outputs):
# d[name] = value.data
# converters = [omega_array_converter()] + weave.converters.default
# specs = assign_variable_types(inames + onames, d, type_converters = converters) #, auto_downcast = 0)
# # itypes = [isinstance(input.data, numpy.ndarray) and num_to_c_types[input.data.dtype.char] for input in self.inputs]
# # otypes = [num_to_c_types[output.data.dtype.char] for output in self.outputs]
# tvars_list = [spec.template_vars() for spec in specs]
# template = {}
# template['name'] = self.__class__.__name__
# template['code'] = behavior
# template['members'] = ";\n".join([" %(name)s"])
# template['decl'] = ""
# template['typespecs'] = ", ".join(["%(name)s_type" % spec.name for spec in specs] +
# ["%(name)s_num_type" % tvars['name'] for tvars in tvars_list if 'num_type' in tvars])
# struct = """
# template<%(typespecs)s>
# struct _omega_%(name)s {
# %(members)s
# _omega_%(name)s() {}
# void execute(void) {
# %(decl)s
# %(code)s
# }
# };
# """ % template
def
_c_alloc
(
self
):
def
_c_alloc
(
self
):
self
.
c_alloc
(
self
.
inputs
,
self
.
outputs
)
self
.
c_alloc
(
self
.
inputs
,
self
.
outputs
)
...
@@ -310,16 +220,16 @@ class omega_op(gof.PythonOp):
...
@@ -310,16 +220,16 @@ class omega_op(gof.PythonOp):
def
c_thunk
(
self
):
def
c_thunk
(
self
):
self
.
_c_alloc
()
self
.
_c_alloc
()
if
self
.
c_module
:
d
,
code
,
struct
,
converters
=
self
.
c_code
()
a
def
thunk
():
else
:
weave
.
inline
(
code
,
d
.
keys
(),
local_dict
=
d
,
global_dict
=
{},
support_code
=
struct
,
type_converters
=
converters
)
aaaaaa
return
thunk
def
c_perform
(
self
):
def
c_perform
(
self
):
self
.
c_thunk
()()
self
.
c_thunk
()()
def
elemwise_wrap
(
beforeloop
,
inloop
,
afterloop
,
inames
,
oname
s
):
def
elemwise_wrap
(
beforeloop
,
inloop
,
afterloop
,
loop_vars
,
writable_loop_var
s
):
return
"""
return
"""
%(beforeloop)
s
%(beforeloop)
s
for (int i = 0; i < N_
%(v1)
s[0]; i++) {
for (int i = 0; i < N_
%(v1)
s[0]; i++) {
...
@@ -330,9 +240,11 @@ def elemwise_wrap(beforeloop, inloop, afterloop, inames, onames):
...
@@ -330,9 +240,11 @@ def elemwise_wrap(beforeloop, inloop, afterloop, inames, onames):
}
}
}
}
%(afterloop)
s
%(afterloop)
s
"""
%
dict
(
v1
=
(
inames
+
onames
)[
0
],
"""
%
dict
(
v1
=
(
loop_vars
+
writable_loop_vars
)[
0
],
idefs
=
"
\n
"
.
join
([
"_
%
s_dtype
%
s = _
%
s2(i, j);"
%
(
iname
,
iname
,
iname
.
upper
())
for
iname
in
inames
if
not
iname
.
startswith
(
"_"
)]),
idefs
=
"
\n
"
.
join
([
"_
%
s_dtype
%
s = _
%
s2(i, j);"
%
(
loop_var
,
loop_var
,
loop_var
.
upper
())
odefs
=
"
\n
"
.
join
([
"_
%
s_dtype&
%
s = _
%
s2(i, j);"
%
(
oname
,
oname
,
oname
.
upper
())
for
oname
in
onames
if
not
oname
.
startswith
(
"_"
)]),
for
loop_var
in
loop_vars
]),
odefs
=
"
\n
"
.
join
([
"_
%
s_dtype&
%
s = _
%
s2(i, j);"
%
(
writable_loop_var
,
writable_loop_var
,
writable_loop_var
.
upper
())
for
writable_loop_var
in
writable_loop_vars
]),
beforeloop
=
beforeloop
,
beforeloop
=
beforeloop
,
inloop
=
inloop
,
inloop
=
inloop
,
afterloop
=
afterloop
)
afterloop
=
afterloop
)
...
@@ -342,29 +254,10 @@ class elemwise(omega_op):
...
@@ -342,29 +254,10 @@ class elemwise(omega_op):
@staticmethod
@staticmethod
def
__clsinit__
(
cls
,
name
,
bases
,
dct
):
def
__clsinit__
(
cls
,
name
,
bases
,
dct
):
# make c_init a static method
for
fname
in
[
'c_init'
,
'c_foreach'
,
'c_finalize'
]:
c_init
=
cls
.
c_init
make_static
(
cls
,
fname
)
if
hasattr
(
c_init
,
'im_func'
):
c_init
=
c_init
.
im_func
cls
.
c_init
=
staticmethod
(
c_init
)
# make c_foreach a static method
c_foreach
=
cls
.
c_foreach
if
hasattr
(
c_foreach
,
'im_func'
):
c_foreach
=
c_foreach
.
im_func
cls
.
c_foreach
=
staticmethod
(
c_foreach
)
# make c_finalize a static method
c_finalize
=
cls
.
c_finalize
if
hasattr
(
c_finalize
,
'im_func'
):
c_finalize
=
c_finalize
.
im_func
cls
.
c_finalize
=
staticmethod
(
c_finalize
)
# # adjust impl
# if cls.forbid_broadcast:
# cls.impl = assert_same_shapes(cls.impl)
# make impl
a static method
# make impl
, grad, etc. static methods
omega_op
.
__clsinit__
(
cls
,
name
,
bases
,
dct
)
omega_op
.
__clsinit__
(
cls
,
name
,
bases
,
dct
)
def
_c_alloc
(
self
):
def
_c_alloc
(
self
):
...
@@ -405,10 +298,10 @@ class elemwise(omega_op):
...
@@ -405,10 +298,10 @@ class elemwise(omega_op):
def
c_finalize
(
inputs
,
outputs
):
def
c_finalize
(
inputs
,
outputs
):
return
""
return
""
def
c
reate_c_code
(
self
,
converters
=
None
):
def
c
_code
(
self
,
converters
=
None
,
elemwise_wrap
=
elemwise_wrap
):
def
mangle
(
name
):
def
mangle
(
name
):
if
name
.
startswith
(
"_"
):
if
name
.
startswith
(
"_"
):
return
name
#[1:]
return
name
else
:
else
:
return
"_"
+
name
return
"_"
+
name
...
@@ -422,10 +315,12 @@ class elemwise(omega_op):
...
@@ -422,10 +315,12 @@ class elemwise(omega_op):
during
=
self
.
c_foreach
(
self
.
inputs
,
self
.
outputs
)
during
=
self
.
c_foreach
(
self
.
inputs
,
self
.
outputs
)
after
=
self
.
c_finalize
(
self
.
inputs
,
self
.
outputs
)
after
=
self
.
c_finalize
(
self
.
inputs
,
self
.
outputs
)
# Get c_init, etc.'s argument names so we can declare them properly in the C code
spec_b
=
inspect
.
getargspec
(
self
.
c_init
)
spec_b
=
inspect
.
getargspec
(
self
.
c_init
)
spec_d
=
inspect
.
getargspec
(
self
.
c_foreach
)
spec_d
=
inspect
.
getargspec
(
self
.
c_foreach
)
spec_a
=
inspect
.
getargspec
(
self
.
c_finalize
)
spec_a
=
inspect
.
getargspec
(
self
.
c_finalize
)
# Sanity check - apart from loop vars, variables are shared in the before/during/after parts
if
before
and
spec_b
!=
spec_d
:
if
before
and
spec_b
!=
spec_d
:
raise
Exception
(
"The input signature of c_init differs from the input signature of c_foreach."
)
raise
Exception
(
"The input signature of c_init differs from the input signature of c_foreach."
)
if
after
and
spec_a
!=
spec_d
:
if
after
and
spec_a
!=
spec_d
:
...
@@ -433,7 +328,9 @@ class elemwise(omega_op):
...
@@ -433,7 +328,9 @@ class elemwise(omega_op):
(
inames
,
onames
),
_1
,
_2
,
_3
=
spec_d
(
inames
,
onames
),
_1
,
_2
,
_3
=
spec_d
behavior
=
elemwise_wrap
(
before
,
during
,
after
,
inames
,
onames
)
behavior
=
elemwise_wrap
(
before
,
during
,
after
,
[
iname
for
iname
in
inames
if
not
iname
.
startswith
(
"_"
)],
[
oname
for
oname
in
onames
if
not
oname
.
startswith
(
"_"
)])
inames
=
[
mangle
(
name
)
for
name
in
inames
]
inames
=
[
mangle
(
name
)
for
name
in
inames
]
onames
=
[
mangle
(
name
)
for
name
in
onames
]
onames
=
[
mangle
(
name
)
for
name
in
onames
]
...
...
type_spec.py
浏览文件 @
c40eadf8
...
@@ -30,9 +30,6 @@ class omega_type_converter_extension:
...
@@ -30,9 +30,6 @@ class omega_type_converter_extension:
def
struct_support_code
(
self
):
def
struct_support_code
(
self
):
return
""
return
""
def
struct_declaration_code
(
self
):
return
""
def
struct_typedefs
(
self
):
def
struct_typedefs
(
self
):
return
"
\n
"
.
join
([
"typedef
%
s
%
s_type;"
%
(
c_type
,
name
)
for
c_type
,
name
,
init
in
self
.
provides
()])
return
"
\n
"
.
join
([
"typedef
%
s
%
s_type;"
%
(
c_type
,
name
)
for
c_type
,
name
,
init
in
self
.
provides
()])
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论