Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
4b634d24
提交
4b634d24
authored
7月 13, 2017
作者:
abergeron
提交者:
GitHub
7月 13, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #6097 from lamblin/mrg_uniform_f16
Make sure MRG uniform in float16 do not return 0
上级
1ac12452
84b61a6c
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
45 行增加
和
10 行删除
+45
-10
rng_mrg.py
theano/gpuarray/rng_mrg.py
+8
-5
test_rng_mrg.py
theano/gpuarray/tests/test_rng_mrg.py
+5
-0
rng_mrg.py
theano/sandbox/rng_mrg.py
+22
-5
test_rng_mrg.py
theano/sandbox/tests/test_rng_mrg.py
+10
-0
没有找到文件。
theano/gpuarray/rng_mrg.py
浏览文件 @
4b634d24
...
@@ -61,18 +61,21 @@ class GPUA_mrg_uniform(GpuKernelBase, mrg_uniform_base):
...
@@ -61,18 +61,21 @@ class GPUA_mrg_uniform(GpuKernelBase, mrg_uniform_base):
otype
=
'ga_half'
otype
=
'ga_half'
# limit the values of the state that we use.
# limit the values of the state that we use.
mask
=
'& 0x7fff'
mask
=
'& 0x7fff'
NORM
=
'3.0518e-05f'
# numpy.float16(1.0/(2**15+8))
offset
=
'+ 1'
NORM
=
'3.0458e-05f'
# numpy.float16(1.0/(2**15+33))
# this was determined by finding the biggest number such that
# this was determined by finding the biggest number such that
# numpy.float16(number * (
M1 & 0x7fff
)) < 1.0
# numpy.float16(number * (
(M1 & 0x7fff) + 1
)) < 1.0
elif
self
.
output_type
.
dtype
==
'float32'
:
elif
self
.
output_type
.
dtype
==
'float32'
:
otype
=
'float'
otype
=
'float'
mask
=
''
mask
=
''
offset
=
''
NORM
=
'4.6566126e-10f'
# numpy.float32(1.0/(2**31+65))
NORM
=
'4.6566126e-10f'
# numpy.float32(1.0/(2**31+65))
# this was determined by finding the biggest number such that
# this was determined by finding the biggest number such that
# numpy.float32(number * M1) < 1.0
# numpy.float32(number * M1) < 1.0
elif
self
.
output_type
.
dtype
==
'float64'
:
elif
self
.
output_type
.
dtype
==
'float64'
:
otype
=
'double'
otype
=
'double'
mask
=
''
mask
=
''
offset
=
''
NORM
=
'4.656612873077392578125e-10'
NORM
=
'4.656612873077392578125e-10'
else
:
else
:
raise
ValueError
(
'Unsupported data type for output'
,
raise
ValueError
(
'Unsupported data type for output'
,
...
@@ -143,11 +146,11 @@ class GPUA_mrg_uniform(GpuKernelBase, mrg_uniform_base):
...
@@ -143,11 +146,11 @@ class GPUA_mrg_uniform(GpuKernelBase, mrg_uniform_base):
x21 = y2;
x21 = y2;
if (x11 <= x21) {
if (x11 <= x21) {
sample_data[i] =
%(write)
s(((
x11 - x21 + M1)
%(mask
)
s) *
%(NORM)
s);
sample_data[i] =
%(write)
s(((
(x11 - x21 + M1)
%(mask)
s)
%(offset
)
s) *
%(NORM)
s);
}
}
else
else
{
{
sample_data[i] =
%(write)
s(((
x11 - x21)
%(mask
)
s) *
%(NORM)
s);
sample_data[i] =
%(write)
s(((
(x11 - x21)
%(mask)
s)
%(offset
)
s) *
%(NORM)
s);
}
}
}
}
...
@@ -299,7 +302,7 @@ class GPUA_mrg_uniform(GpuKernelBase, mrg_uniform_base):
...
@@ -299,7 +302,7 @@ class GPUA_mrg_uniform(GpuKernelBase, mrg_uniform_base):
"""
%
dict
(
fail
=
sub
[
'fail'
]))
"""
%
dict
(
fail
=
sub
[
'fail'
]))
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
(
1
5
,)
return
(
1
6
,)
@register_opt2
([
mrg_uniform
],
'fast_compile'
)
@register_opt2
([
mrg_uniform
],
'fast_compile'
)
...
...
theano/gpuarray/tests/test_rng_mrg.py
浏览文件 @
4b634d24
...
@@ -9,6 +9,7 @@ from theano.configparser import change_flags
...
@@ -9,6 +9,7 @@ from theano.configparser import change_flags
from
theano.sandbox
import
rng_mrg
from
theano.sandbox
import
rng_mrg
from
theano.sandbox.rng_mrg
import
MRG_RandomStreams
from
theano.sandbox.rng_mrg
import
MRG_RandomStreams
from
theano.sandbox.tests.test_rng_mrg
import
java_samples
,
rng_mrg_overflow
from
theano.sandbox.tests.test_rng_mrg
import
java_samples
,
rng_mrg_overflow
from
theano.sandbox.tests.test_rng_mrg
import
test_f16_nonzero
as
cpu_f16_nonzero
from
theano.tests
import
unittest_tools
as
utt
from
theano.tests
import
unittest_tools
as
utt
from
.config
import
mode_with_gpu
as
mode
from
.config
import
mode_with_gpu
as
mode
...
@@ -162,3 +163,7 @@ def test_validate_input_types_gpuarray_backend():
...
@@ -162,3 +163,7 @@ def test_validate_input_types_gpuarray_backend():
rstate
=
np
.
zeros
((
7
,
6
),
dtype
=
"int32"
)
rstate
=
np
.
zeros
((
7
,
6
),
dtype
=
"int32"
)
rstate
=
gpuarray_shared_constructor
(
rstate
)
rstate
=
gpuarray_shared_constructor
(
rstate
)
rng_mrg
.
mrg_uniform
.
new
(
rstate
,
ndim
=
None
,
dtype
=
"float32"
,
size
=
(
3
,))
rng_mrg
.
mrg_uniform
.
new
(
rstate
,
ndim
=
None
,
dtype
=
"float32"
,
size
=
(
3
,))
def
test_f16_nonzero
():
cpu_f16_nonzero
(
mode
=
mode
,
op_to_check
=
GPUA_mrg_uniform
)
theano/sandbox/rng_mrg.py
浏览文件 @
4b634d24
...
@@ -234,7 +234,7 @@ def ff_2p72(rstate):
...
@@ -234,7 +234,7 @@ def ff_2p72(rstate):
return
multMatVect
(
rstate
,
A1p72
,
M1
,
A2p72
,
M2
)
return
multMatVect
(
rstate
,
A1p72
,
M1
,
A2p72
,
M2
)
def
mrg_next_value
(
rstate
,
new_rstate
):
def
mrg_next_value
(
rstate
,
new_rstate
,
NORM
,
mask
,
offset
):
# TODO : need description for method, parameter and return
# TODO : need description for method, parameter and return
x11
,
x12
,
x13
,
x21
,
x22
,
x23
=
rstate
x11
,
x12
,
x13
,
x21
,
x22
,
x23
=
rstate
assert
type
(
x11
)
==
np
.
int32
assert
type
(
x11
)
==
np
.
int32
...
@@ -279,9 +279,9 @@ def mrg_next_value(rstate, new_rstate):
...
@@ -279,9 +279,9 @@ def mrg_next_value(rstate, new_rstate):
new_rstate
[
...
]
=
[
x11
,
x12
,
x13
,
x21
,
x22
,
x23
]
new_rstate
[
...
]
=
[
x11
,
x12
,
x13
,
x21
,
x22
,
x23
]
assert
new_rstate
.
dtype
==
np
.
int32
assert
new_rstate
.
dtype
==
np
.
int32
if
(
x11
<=
x21
):
if
(
x11
<=
x21
):
return
(
x11
-
x21
+
M1
)
*
NORM
return
(
((
x11
-
x21
+
M1
)
&
mask
)
+
offset
)
*
NORM
else
:
else
:
return
(
x11
-
x21
)
*
NORM
return
(
((
x11
-
x21
)
&
mask
)
+
offset
)
*
NORM
class
mrg_uniform_base
(
Op
):
class
mrg_uniform_base
(
Op
):
...
@@ -330,6 +330,7 @@ class mrg_uniform_base(Op):
...
@@ -330,6 +330,7 @@ class mrg_uniform_base(Op):
class
mrg_uniform
(
mrg_uniform_base
):
class
mrg_uniform
(
mrg_uniform_base
):
# CPU VERSION
# CPU VERSION
_f16_ok
=
True
def
make_node
(
self
,
rstate
,
size
):
def
make_node
(
self
,
rstate
,
size
):
# error checking slightly redundant here, since
# error checking slightly redundant here, since
...
@@ -374,12 +375,25 @@ class mrg_uniform(mrg_uniform_base):
...
@@ -374,12 +375,25 @@ class mrg_uniform(mrg_uniform_base):
n_streams
,
_
=
rstate
.
shape
n_streams
,
_
=
rstate
.
shape
rval
=
np
.
zeros
(
n_elements
,
dtype
=
self
.
output_type
.
dtype
)
rval
=
np
.
zeros
(
n_elements
,
dtype
=
self
.
output_type
.
dtype
)
if
rval
.
dtype
==
'float16'
:
mask
=
0x7fff
offset
=
1
NORM
=
np
.
float16
(
3.0458e-05
)
elif
rval
.
dtype
==
'float32'
:
mask
=
0xffffffff
offset
=
0
NORM
=
np
.
float32
(
4.6566126e-10
)
elif
rval
.
dtype
==
'float64'
:
mask
=
0xffffffff
offset
=
0
NORM
=
4.656612873077392578125e-10
# 1./2^31
err_orig
=
np
.
seterr
(
over
=
'ignore'
)
err_orig
=
np
.
seterr
(
over
=
'ignore'
)
try
:
try
:
for
i
in
xrange
(
n_elements
):
for
i
in
xrange
(
n_elements
):
sample
=
mrg_next_value
(
rstate
[
i
%
n_streams
],
sample
=
mrg_next_value
(
rstate
[
i
%
n_streams
],
rstate
[
i
%
n_streams
])
rstate
[
i
%
n_streams
],
NORM
=
NORM
,
mask
=
mask
,
offset
=
offset
)
rval
[
i
]
=
sample
rval
[
i
]
=
sample
finally
:
finally
:
np
.
seterr
(
**
err_orig
)
np
.
seterr
(
**
err_orig
)
...
@@ -476,6 +490,9 @@ class mrg_uniform(mrg_uniform_base):
...
@@ -476,6 +490,9 @@ class mrg_uniform(mrg_uniform_base):
# TensorType, something is wrong (likely one of the GPU ops
# TensorType, something is wrong (likely one of the GPU ops
# not defining C code correctly).
# not defining C code correctly).
assert
isinstance
(
node
.
inputs
[
0
]
.
type
,
TensorType
)
assert
isinstance
(
node
.
inputs
[
0
]
.
type
,
TensorType
)
if
self
.
output_type
.
dtype
==
'float16'
:
# C code is not tested, fall back to Python
super
(
mrg_uniform
,
self
)
.
c_code
(
node
,
name
,
inp
,
out
,
sub
)
return
"""
return
"""
//////// <code generated by mrg_uniform>
//////// <code generated by mrg_uniform>
npy_int64 odims_i;
npy_int64 odims_i;
...
@@ -592,7 +609,7 @@ class mrg_uniform(mrg_uniform_base):
...
@@ -592,7 +609,7 @@ class mrg_uniform(mrg_uniform_base):
"""
%
dict
(
fail
=
sub
[
'fail'
]))
"""
%
dict
(
fail
=
sub
[
'fail'
]))
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
(
9
,)
return
(
10
,)
def
guess_n_streams
(
size
,
warn
=
False
):
def
guess_n_streams
(
size
,
warn
=
False
):
...
...
theano/sandbox/tests/test_rng_mrg.py
浏览文件 @
4b634d24
...
@@ -742,6 +742,16 @@ def test_undefined_grad():
...
@@ -742,6 +742,16 @@ def test_undefined_grad():
(
avg
,
std
))
(
avg
,
std
))
def
test_f16_nonzero
(
mode
=
None
,
op_to_check
=
rng_mrg
.
mrg_uniform
):
srng
=
MRG_RandomStreams
(
seed
=
utt
.
fetch_seed
())
m
=
srng
.
uniform
(
size
=
(
1000
,
1000
),
dtype
=
'float16'
)
assert
m
.
dtype
==
'float16'
,
m
.
type
f
=
theano
.
function
([],
m
,
mode
=
mode
)
assert
any
(
isinstance
(
n
.
op
,
op_to_check
)
for
n
in
f
.
maker
.
fgraph
.
apply_nodes
)
m_val
=
f
()
assert
np
.
all
((
0
<
m_val
)
&
(
m_val
<
1
))
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
rng
=
MRG_RandomStreams
(
np
.
random
.
randint
(
2147462579
))
rng
=
MRG_RandomStreams
(
np
.
random
.
randint
(
2147462579
))
print
(
theano
.
__file__
)
print
(
theano
.
__file__
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论