Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
44020007
提交
44020007
authored
1月 31, 2012
作者:
Pascal Lamblin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Optimization to move tensordot to GPU
上级
120ad7d7
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
66 行增加
和
1 行删除
+66
-1
opt.py
theano/sandbox/cuda/opt.py
+31
-0
test_opt.py
theano/sandbox/cuda/tests/test_opt.py
+35
-1
没有找到文件。
theano/sandbox/cuda/opt.py
浏览文件 @
44020007
...
@@ -746,6 +746,37 @@ def local_gpu_print_op(node):
...
@@ -746,6 +746,37 @@ def local_gpu_print_op(node):
return
[
host_from_gpu
(
new_op
(
gpu_x
))]
return
[
host_from_gpu
(
new_op
(
gpu_x
))]
return
False
return
False
@register_opt
()
@local_optimizer
([
tensor
.
TensorDot
])
def
local_gpu_tensordot
(
node
):
'''
T.tensordot(host_from_gpu) -> basic_ops.tensordot(host_from_gpu)
There is no Cuda Op for tensordot, however we can build a chain of
CPU Ops implementing tensordot. These Ops all have a GPU equivalent.
Note: applying this optimization at that stage is not ideal, because
all blas-related optimizations have already been applied.
However, if we want to apply it before the blas optimizations, then
we don't know which variables may end up on the GPU or not.
'''
if
(
isinstance
(
node
.
op
,
tensor
.
TensorDot
)
and
node
.
outputs
[
0
]
.
dtype
==
'float32'
):
x
,
y
=
node
.
inputs
transfer
=
False
if
((
x
.
owner
and
x
.
owner
.
op
==
host_from_gpu
and
y
.
dtype
==
'float32'
)
or
(
y
.
owner
and
y
.
owner
.
op
==
host_from_gpu
and
x
.
dtype
==
'float32'
)):
axes
=
node
.
op
.
axes
out
=
tensordot
(
x
,
y
,
axes
=
axes
)
return
[
out
]
def
cast
(
x
,
dtype
):
def
cast
(
x
,
dtype
):
stype
=
scal
.
Scalar
(
dtype
)
stype
=
scal
.
Scalar
(
dtype
)
cast_op
=
theano
.
tensor
.
Elemwise
(
scal
.
Identity
(
scal
.
specific_out
(
stype
)))
cast_op
=
theano
.
tensor
.
Elemwise
(
scal
.
Identity
(
scal
.
specific_out
(
stype
)))
...
...
theano/sandbox/cuda/tests/test_opt.py
浏览文件 @
44020007
import
sys
,
time
import
sys
,
time
,
unittest
import
numpy
import
numpy
# Skip test if cuda_ndarray is not available.
# Skip test if cuda_ndarray is not available.
...
@@ -8,6 +8,8 @@ from theano.compile.pfunc import pfunc
...
@@ -8,6 +8,8 @@ from theano.compile.pfunc import pfunc
from
theano
import
config
,
tensor
from
theano
import
config
,
tensor
import
theano
import
theano
from
theano.tests
import
unittest_tools
as
utt
import
theano.sandbox.cuda
as
cuda
import
theano.sandbox.cuda
as
cuda
if
cuda
.
cuda_available
==
False
:
if
cuda
.
cuda_available
==
False
:
raise
SkipTest
(
'Optional package cuda disabled'
)
raise
SkipTest
(
'Optional package cuda disabled'
)
...
@@ -246,6 +248,38 @@ def test_elemwise_fusion():
...
@@ -246,6 +248,38 @@ def test_elemwise_fusion():
f
(
theano
.
_asarray
(
numpy
.
random
.
rand
(
*
shape
),
dtype
=
'float32'
),
theano
.
_asarray
(
numpy
.
random
.
rand
(
*
shape
),
dtype
=
'float32'
))
f
(
theano
.
_asarray
(
numpy
.
random
.
rand
(
*
shape
),
dtype
=
'float32'
),
theano
.
_asarray
(
numpy
.
random
.
rand
(
*
shape
),
dtype
=
'float32'
))
class
test_local_gpu_tensordot
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
def
test_transfer
(
self
):
tensor1
=
self
.
rng
.
rand
(
20
,
10
,
5
,
8
)
.
astype
(
'float32'
)
tensor2
=
self
.
rng
.
rand
(
5
,
8
,
20
)
.
astype
(
'float32'
)
tensor3
=
self
.
rng
.
rand
(
8
,
20
,
5
)
.
astype
(
'float32'
)
x
=
tensor
.
ftensor4
(
'x'
)
y
=
tensor
.
ftensor3
(
'y'
)
tdot1
=
tensor
.
tensordot
(
x
,
y
,
2
)
f1
=
theano
.
function
([
x
,
y
],
tdot1
,
mode
=
mode_with_gpu
)
topo1
=
f1
.
maker
.
env
.
toposort
()
assert
topo1
[
-
1
]
.
op
==
cuda
.
host_from_gpu
# Let DebugMode debug
f1
(
tensor1
,
tensor2
)
tdot2
=
tensor
.
tensordot
(
x
,
y
,
axes
=
[(
0
,
3
),
(
1
,
0
)])
f2
=
theano
.
function
([
x
,
y
],
tdot2
,
mode
=
mode_with_gpu
)
topo2
=
f2
.
maker
.
env
.
toposort
()
assert
topo2
[
-
1
]
.
op
==
cuda
.
host_from_gpu
f2
(
tensor1
,
tensor3
)
tdot3
=
tensor
.
tensordot
(
x
,
y
,
axes
=
[(
0
,
3
,
2
),
(
1
,
0
,
2
)])
f3
=
theano
.
function
([
x
,
y
],
tdot3
,
mode
=
mode_with_gpu
)
topo3
=
f3
.
maker
.
env
.
toposort
()
assert
topo3
[
-
1
]
.
op
==
cuda
.
host_from_gpu
f3
(
tensor1
,
tensor3
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
test_gpualloc
()
test_gpualloc
()
test_opt_gpujoin_onlyajoin
()
test_opt_gpujoin_onlyajoin
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论