Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
c9221f08
提交
c9221f08
authored
3月 10, 2008
作者:
Olivier Breuleux
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
in the process of making tensor ops work - problems with refcounts
上级
4b29e343
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
199 行增加
和
57 行删除
+199
-57
_test_tensor_ops.py
_test_tensor_ops.py
+57
-0
tensor.py
tensor.py
+37
-57
tensor_ops.py
tensor_ops.py
+105
-0
没有找到文件。
_test_tensor_ops.py
0 → 100644
浏览文件 @
c9221f08
import
unittest
from
gof
import
ResultBase
,
Op
,
Env
,
modes
import
gof
from
tensor
import
*
from
tensor_ops
import
*
import
numpy
import
sys
def
inputs
():
x
=
modes
.
build
(
tensor
([[
1.0
,
2.0
],
[
3.0
,
4.0
]],
'x'
))
y
=
None
z
=
None
return
x
,
y
,
z
def
env
(
inputs
,
outputs
,
validate
=
True
,
features
=
[]):
return
Env
(
inputs
,
outputs
,
features
=
features
,
consistency_check
=
validate
)
class
_test_TensorOps
(
unittest
.
TestCase
):
def
test_0
(
self
):
x
,
y
,
z
=
inputs
()
e
=
transpose
(
x
)
g
=
env
([
x
],
[
e
])
fn
,
(
i
,
),
(
o
,
)
=
gof
.
cc
.
CLinker
(
g
)
.
make_thunk
()
i
.
data
=
[[
1.0
,
2.0
],
[
3.0
,
4.0
]]
# print sys.getrefcount(i.data)
fn
()
# print sys.getrefcount(i.data)
# print sys.getrefcount(o.data)
print
o
.
data
# assert res == numpy.asarray(arr)
# def test_1(self):
# x, y, z = inputs()
# e = mul(add(x, y), div(x, y))
# g = env([x, y], [e])
# fn = gof.cc.CLinker(g).make_function()
# assert fn(1.0, 2.0) == 1.5
# assert e.data == 1.5
if
__name__
==
'__main__'
:
unittest
.
main
()
tensor.py
浏览文件 @
c9221f08
import
numpy
import
numpy
from
copy
import
copy
from
copy
import
copy
import
inspect
from
gof
import
ResultBase
,
Op
,
utils
from
gof
import
ResultBase
from
gof
import
Op
def
tensor
(
data
,
name
=
None
):
def
tensor
(
data
,
name
=
None
):
data
=
numpy
.
asarray
(
data
)
return
Tensor
(
data
.
dtype
,
[
0
]
*
len
(
data
.
shape
),
data
,
name
)
return
Tensor
(
data
.
dtype
,
[
0
]
*
len
(
data
.
shape
),
data
,
name
)
def
_broadcastable_pattern
(
pattern
):
def
_broadcastable_pattern
(
pattern
):
def
factory
(
data
=
None
,
name
=
None
):
def
factory
(
data
=
None
,
name
=
None
):
if
data
:
assert
len
(
data
.
shape
)
==
len
(
pattern
)
if
data
:
assert
len
(
data
.
shape
)
==
len
(
pattern
)
return
Tensor
(
data
.
dtype
,
pattern
,
data
,
name
)
return
Tensor
(
data
.
dtype
,
pattern
,
data
,
name
)
return
factory
matrix
=
_broadcastable_pattern
([
0
,
0
])
matrix
=
_broadcastable_pattern
([
0
,
0
])
row
=
_broadcastable_pattern
([
1
,
0
])
row
=
_broadcastable_pattern
([
1
,
0
])
...
@@ -23,7 +25,7 @@ class Tensor(ResultBase):
...
@@ -23,7 +25,7 @@ class Tensor(ResultBase):
def
__init__
(
self
,
dtype
,
broadcastable
,
data
=
None
,
name
=
None
):
def
__init__
(
self
,
dtype
,
broadcastable
,
data
=
None
,
name
=
None
):
self
.
broadcastable
=
broadcastable
self
.
broadcastable
=
broadcastable
self
.
dtype
=
dtype
self
.
dtype
=
str
(
dtype
)
ResultBase
.
__init__
(
self
,
role
=
None
,
data
=
None
,
name
=
name
)
ResultBase
.
__init__
(
self
,
role
=
None
,
data
=
None
,
name
=
name
)
def
filter
(
self
,
data
):
def
filter
(
self
,
data
):
...
@@ -32,31 +34,54 @@ class Tensor(ResultBase):
...
@@ -32,31 +34,54 @@ class Tensor(ResultBase):
assert
not
b
or
s
==
1
assert
not
b
or
s
==
1
return
arr
return
arr
def
dtype_specs
(
self
):
return
{
'float64'
:
(
float
,
'double'
)}[
self
.
dtype
]
def
c_declare
(
self
):
def
c_declare
(
self
):
return
"""
return
"""
PyArrayObject*
%%(name)
s;
PyArrayObject*
%%(name)
s;
typedef
%(dtype)
s
%%(name)
s_dtype;
typedef
%(dtype)
s
%%(name)
s_dtype;
"""
%
dict
(
dtype
=
self
.
to_c_type
(
self
.
dtype
))
"""
%
dict
(
dtype
=
self
.
dtype_specs
()[
1
])
def
c_init
(
self
):
return
"""
%(name)
s = NULL;
"""
def
c_
data_
extract
(
self
):
def
c_extract
(
self
):
return
"""
return
"""
if (py_
%(name)
s == Py_None)
if (py_
%(name)
s == Py_None)
{
%(name)
s = NULL;
%(name)
s = NULL;
else
}
else if (!PyArray_Check(py_
%(name)
s)) {
PyErr_SetString(PyExc_ValueError, "expected an ndarray");
%(fail)
s
}
else {
%(name)
s = (PyArrayObject*)(py_
%(name)
s);
%(name)
s = (PyArrayObject*)(py_
%(name)
s);
Py_XINCREF(
%(name)
s);
}
"""
"""
def
c_data_cleanup
(
self
):
def
c_cleanup
(
self
):
return
""
return
"""
if (
%(name)
s) {
Py_XDECREF(
%(name)
s);
for (int i = 0; i < PyArray_REFCOUNT(
%(name)
s); i++) {
printf("X");
}
printf("Y
\\
n");
}
"""
def
c_
data_
sync
(
self
):
def
c_sync
(
self
):
return
"""
return
"""
if (!
%(name)
s) {
if (!
%(name)
s) {
Py_XDECREF(py_
%(name)
);
Py_XDECREF(py_
%(name)
s
);
py_
%(name)
s = Py_None;
py_
%(name)
s = Py_None;
}
}
else if ((void*)py_
%(name)
s != (void*)
%(name)
s) {
else if ((void*)py_
%(name)
s != (void*)
%(name)
s) {
Py_XDECREF(py_
%(name)
);
Py_XDECREF(py_
%(name)
s
);
py_
%(name)
s = (PyObject*)
%(name)
s;
py_
%(name)
s = (PyObject*)
%(name)
s;
}
}
"""
"""
...
@@ -76,48 +101,3 @@ class Tensor(ResultBase):
...
@@ -76,48 +101,3 @@ class Tensor(ResultBase):
return
cpy
return
cpy
def
TensorOp
(
Op
):
nin
=
-
1
nout
=
1
def
__init__
(
self
,
*
inputs
):
def
wrap_as_tensor
(
x
):
if
isinstance
(
x
,
Tensor
):
return
x
else
:
return
Tensor
(
x
)
inputs
=
map
(
wrap_as_tensor
,
inputs
)
if
self
.
nin
>=
0
:
if
len
(
inputs
)
!=
self
.
nin
:
raise
TypeError
(
"Wrong number of inputs for
%
s (got
%
i, expected
%
i)"
)
\
%
(
self
,
len
(
inputs
),
self
.
nin
)
i_broadcastables
=
[
getattr
(
input
,
'broadcastable'
,
None
)
for
input
in
inputs
]
i_dtypes
=
[
getattr
(
input
,
'dtype'
,
None
)
for
input
in
inputs
]
o_broadcastables
=
utils
.
from_return_values
(
self
.
propagate_broadcastable
(
*
i_broadcastables
))
o_dtypes
=
utils
.
from_return_values
(
self
.
propagate_dtype
(
*
i_dtypes
))
self
.
inputs
=
inputs
self
.
outputs
=
[
Tensor
(
dtype
,
broadcastable
)
for
broadcastable
,
dtype
in
zip
(
o_broadcastables
,
o_dtypes
)]
def
propagate_broadcastable
(
self
,
*
inputs
):
raise
AbstractFunctionError
()
def
propagate_dtype
(
self
,
*
inputs
):
raise
AbstractFunctionError
()
def
impl
(
self
,
*
inputs
):
raise
AbstractFunctionError
()
def
perform
(
self
):
self
.
outputs
[
0
]
.
data
=
self
.
impl
(
*
[
input
.
data
for
input
in
self
.
inputs
])
tensor_ops.py
0 → 100644
浏览文件 @
c9221f08
from
tensor
import
*
from
gof
import
Op
,
utils
def
upcast
(
dtype
,
*
dtypes
):
z
=
numpy
.
zeros
((),
dtype
=
dtype
)
for
dtype
in
dtypes
:
z
=
z
+
numpy
.
zeros
((),
dtype
=
dtype
)
return
str
(
z
.
dtype
)
class
TensorOp
(
Op
):
nin
=
-
1
nout
=
1
cast_method
=
lambda
self
,
*
args
:
upcast
(
*
args
)
def
__init__
(
self
,
*
inputs
):
def
wrap_as_tensor
(
x
):
if
isinstance
(
x
,
Tensor
):
return
x
else
:
return
Tensor
(
x
)
inputs
=
map
(
wrap_as_tensor
,
inputs
)
if
self
.
nin
>=
0
:
if
len
(
inputs
)
!=
self
.
nin
:
raise
TypeError
(
"Wrong number of inputs for
%
s (got
%
i, expected
%
i)"
)
\
%
(
self
,
len
(
inputs
),
self
.
nin
)
i_broadcastables
=
[
getattr
(
input
,
'broadcastable'
,
None
)
for
input
in
inputs
]
i_dtypes
=
[
getattr
(
input
,
'dtype'
,
None
)
for
input
in
inputs
]
o_broadcastables
=
utils
.
from_return_values
(
self
.
propagate_broadcastable
(
*
i_broadcastables
))
o_dtypes
=
utils
.
from_return_values
(
self
.
propagate_dtype
(
*
i_dtypes
))
self
.
inputs
=
inputs
self
.
outputs
=
[
Tensor
(
dtype
,
broadcastable
)
for
broadcastable
,
dtype
in
zip
(
o_broadcastables
,
o_dtypes
)]
def
propagate_broadcastable
(
self
,
*
inputs
):
raise
AbstractFunctionError
()
def
propagate_dtype
(
self
,
*
i_dtypes
):
for
dtype
in
i_dtypes
:
if
dtype
is
None
:
raise
TypeError
(
"Expected a Tensor."
)
return
self
.
cast_method
(
*
i_dtypes
)
def
impl
(
self
,
*
inputs
):
raise
AbstractFunctionError
()
def
perform
(
self
):
self
.
outputs
[
0
]
.
data
=
self
.
impl
(
*
[
input
.
data
for
input
in
self
.
inputs
])
def
c_var_names
(
self
):
(
self
,
inames
,
onames
),
_1
,
_2
,
_3
=
inspect
.
getargspec
(
self
.
c_impl
)
inames
=
utils
.
from_return_values
(
inames
)
onames
=
utils
.
from_return_values
(
onames
)
return
[
inames
,
onames
]
def
c_code
(
self
):
return
self
.
c_impl
(
self
.
inputs
,
self
.
outputs
)
def
c_impl
(
self
,
inputs
,
outputs
):
raise
AbstractFunctionError
()
class
UnaryTensorOp
(
TensorOp
):
nin
=
1
class
BinaryTensorOp
(
TensorOp
):
nin
=
2
class
Transpose
(
UnaryTensorOp
):
def
propagate_broadcastable
(
self
,
x
):
x2
=
copy
(
x
)
x2
.
reverse
()
return
x2
def
impl
(
self
,
x
):
return
x
.
T
def
c_impl
(
self
,
x
,
z
):
return
"""
PyArrayObject* transposed = (PyArrayObject*)PyArray_Transpose(
%(x)
s, NULL);
if (PyArray_REFCOUNT(transposed) == 1) {
printf("lala
\\
n");
}
if (
%(z)
s) {
Py_XDECREF(
%(z)
s);
}
%(z)
s = transposed;
"""
from
gof
import
modes
modes
.
make_constructors
(
globals
())
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论