Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
4a167629
提交
4a167629
authored
12月 12, 2011
作者:
Olivier Delalleau
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #289 from nouiz/fix_import_floatX
Fix an import case and made more test run in floatX
上级
6e63a578
48ad7e6d
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
74 行增加
和
16 行删除
+74
-16
ctype.txt
doc/extending/ctype.txt
+20
-0
function_module.py
theano/compile/function_module.py
+16
-13
test_tensor_op.py
theano/sandbox/cuda/tests/test_tensor_op.py
+25
-3
type.py
theano/sandbox/cuda/type.py
+13
-0
test_basic.py
theano/tensor/tests/test_basic.py
+0
-0
没有找到文件。
doc/extending/ctype.txt
浏览文件 @
4a167629
...
...
@@ -502,6 +502,26 @@ Final version
double = Double()
DeepCopyOp
==========
We have an internal Op called DeepCopyOp. It is used to make sure we
respect the user vs Theano memory region as described in the :ref:`tutorial
<aliasing>`. Theano has a Python implementation that calls the object's
``copy()`` or ``deepcopy()`` method for Theano types for which it does not
know how to generate C code.
You can implement c_code for this op. You register it like this:
.. code-block:: python
theano.compile.function_module.register_DeepCopyOp_c_code(YOUR_TYPE_CLASS, THE_C_CODE)
In your C code, you should use %(iname)s and %(oname)s to represent
the C variable names of the DeepCopyOp input and output
respectively. See an example for the type ``CudaNdarrayType`` (GPU array)
in the file `theano/sandbox/cuda/type.py`.
Output Guard
============
...
...
theano/compile/function_module.py
浏览文件 @
4a167629
...
...
@@ -129,7 +129,21 @@ class AliasedMemoryError(Exception):
### Function
###
def
register_DeepCopyOp_c_code
(
typ
,
code
):
""" Tell DeepCopyOp how to generate C code for a Theano Type
:param typ: A Theano type. It must be the Theano class itself and not an
instance of the class.
:param code: C code that deep copies the Theano type 'typ'.
Use
%(iname)
s and
%(oname)
s for the input and output C
variable names respectively.
"""
DeepCopyOp
.
c_codes
[
typ
]
=
code
class
DeepCopyOp
(
theano
.
gof
.
Op
):
c_codes
=
{}
# Theano Type, code
def
__init__
(
self
):
pass
...
...
@@ -175,19 +189,8 @@ class DeepCopyOp(theano.gof.Op):
}
"""
%
locals
()
elif
isinstance
(
node
.
inputs
[
0
]
.
type
,
theano
.
sandbox
.
cuda
.
CudaNdarrayType
):
return
"""
Py_XDECREF(
%(oname)
s);
%(oname)
s = (CudaNdarray*)CudaNdarray_Copy(
%(iname)
s);
if (!
%(oname)
s)
{
PyErr_SetString(PyExc_ValueError, "DeepCopyOp: the copy failed!");
%(fail)
s;
}
"""
%
locals
()
elif
node
.
inputs
[
0
]
.
type
.
__class__
in
self
.
c_codes
:
return
self
.
c_codes
[
node
.
inputs
[
0
]
.
type
.
__class__
]
%
locals
()
else
:
super
(
DeepCopyOp
,
self
)
.
c_code
(
node
,
name
,
inames
,
onames
,
sub
)
...
...
theano/sandbox/cuda/tests/test_tensor_op.py
浏览文件 @
4a167629
"""
This file test tensor op that should also operate on CudaNdaray.
"""
import
numpy
import
copy
from
nose.plugins.skip
import
SkipTest
from
theano
import
tensor
import
numpy
import
theano
from
theano
import
tensor
import
theano.tensor
as
T
# Skip test if cuda_ndarray is not available.
from
nose.plugins.skip
import
SkipTest
import
theano.sandbox.cuda
as
cuda
if
cuda
.
cuda_available
==
False
:
raise
SkipTest
(
'Optional package cuda disabled'
)
...
...
@@ -105,3 +106,24 @@ def test_may_share_memory_cuda():
raise
Exception
(
"An error was expected"
)
except
TypeError
:
pass
def
test_deepcopy
():
a
=
cuda
.
fmatrix
()
a_v
=
cuda
.
CudaNdarray
(
numpy
.
zeros
((
3
,
4
),
dtype
=
'float32'
))
# We force the c code to check that we generate c code
mode
=
theano
.
Mode
(
"c"
,
mode_with_gpu
.
optimizer
)
f
=
theano
.
function
([
a
],
a
,
mode
=
mode
)
theano
.
printing
.
debugprint
(
f
)
out
=
f
(
a_v
)
assert
out
is
not
a_v
assert
numpy
.
allclose
(
numpy
.
asarray
(
a_v
),
numpy
.
asarray
(
out
))
# We force the python linker as the default code should work for this op
mode
=
theano
.
Mode
(
"py"
,
mode_with_gpu
.
optimizer
)
f
=
theano
.
function
([
a
],
a
,
mode
=
mode
)
theano
.
printing
.
debugprint
(
f
)
out
=
f
(
a_v
)
assert
out
is
not
a_v
assert
numpy
.
allclose
(
numpy
.
asarray
(
a_v
),
numpy
.
asarray
(
out
))
theano/sandbox/cuda/type.py
浏览文件 @
4a167629
...
...
@@ -356,6 +356,19 @@ class CudaNdarrayType(Type):
# to have OutputGuard generate C code for this type.
theano
.
compile
.
mode
.
register_OutputGuard_c_code
(
CudaNdarrayType
)
# Register CudaNdarrayType to the DeepCopyOp list of types with c code.
theano
.
compile
.
function_module
.
register_DeepCopyOp_c_code
(
CudaNdarrayType
,
"""
Py_XDECREF(
%(oname)
s);
%(oname)
s = (CudaNdarray*)CudaNdarray_Copy(
%(iname)
s);
if (!
%(oname)
s)
{
PyErr_SetString(PyExc_ValueError, "DeepCopyOp: the copy failed!");
%(fail)
s;
}
"""
)
# THIS WORKS
# But CudaNdarray instances don't compare equal to one another, and what about __hash__ ?
...
...
theano/tensor/tests/test_basic.py
浏览文件 @
4a167629
差异被折叠。
点击展开。
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论