Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
70d0c2d4
提交
70d0c2d4
authored
4月 08, 2014
作者:
Yann N. Dauphin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
integration in module
上级
37477155
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
48 行增加
和
20 行删除
+48
-20
rng_mrg.py
theano/sandbox/rng_mrg.py
+42
-18
test_rng_mrg.py
theano/sandbox/test_rng_mrg.py
+6
-2
没有找到文件。
theano/sandbox/rng_mrg.py
浏览文件 @
70d0c2d4
...
...
@@ -10,7 +10,7 @@ import warnings
import
numpy
from
theano
import
Op
,
Apply
,
shared
,
config
,
Variable
from
theano
import
gradient
from
theano
import
gradient
,
function
from
theano
import
tensor
from
theano.tensor
import
(
raw_random
,
TensorType
,
as_tensor_variable
,
get_vector_length
,
cast
,
opt
,
scal
)
...
...
@@ -30,21 +30,45 @@ def matVecModM(A, s, m):
return
numpy
.
int32
(
numpy
.
sum
((
numpy
.
int64
(
A
)
*
s
)
%
m
,
1
)
%
m
)
dot_modulo
=
None
def
multMatVect
(
v
,
A
,
m1
,
B
,
m2
):
"""
multiply the first half of v by A with a modulo of m1
and the second half by B with a modulo of m2
Note: The parameters of dot_modulo are passed implicitly because passing
them explicitly takes more time then running the function's C-code.
"""
#multiply the first half of v by A with a modulo of m1
#and the second half by B with a modulo of m2
err_orig
=
numpy
.
seterr
(
over
=
'ignore'
)
try
:
r
=
numpy
.
zeros_like
(
v
)
r
[:
3
]
=
matVecModM
(
A
,
v
[:
3
],
m1
)
r
[
3
:]
=
matVecModM
(
B
,
v
[
3
:],
m2
)
finally
:
numpy
.
seterr
(
**
err_orig
)
global
dot_modulo
if
dot_modulo
==
None
:
A_sym
=
tensor
.
lmatrix
(
'A'
)
s_sym
=
tensor
.
ivector
(
's'
)
m_sym
=
tensor
.
iscalar
(
'm'
)
dot_modulo
=
function
([
A_sym
,
s_sym
,
m_sym
],
DotModulo
()(
A_sym
,
s_sym
,
m_sym
))
r
=
numpy
.
zeros_like
(
v
)
dot_modulo
.
input_storage
[
0
]
.
storage
[
0
]
=
A
dot_modulo
.
input_storage
[
1
]
.
storage
[
0
]
=
v
[:
3
]
dot_modulo
.
input_storage
[
2
]
.
storage
[
0
]
=
m1
r
[:
3
]
=
dot_modulo
.
fn
()[
0
]
dot_modulo
.
input_storage
[
0
]
.
storage
[
0
]
=
B
dot_modulo
.
input_storage
[
1
]
.
storage
[
0
]
=
v
[
3
:]
dot_modulo
.
input_storage
[
2
]
.
storage
[
0
]
=
m2
r
[
3
:]
=
dot_modulo
.
fn
()[
0
]
return
r
class
DotModulo
(
Op
):
"""
Efficient and numerically stable implementation of a dot product followed
by a modulo operation. This performs the same function as matVecModM.
"""
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
...
...
@@ -57,8 +81,8 @@ class DotModulo(Op):
def
perform
(
self
,
node
,
(
A
,
s
,
m
),
(
out
,
)):
out
[
0
]
=
matVecModM
(
A
,
s
,
m
)
#
def c_code_cache_version(self):
# return (2
,)
def
c_code_cache_version
(
self
):
return
(
3
,)
def
c_code
(
self
,
node
,
name
,
(
_A
,
_s
,
_m
),
(
_z
,
),
sub
):
return
"""
...
...
@@ -101,7 +125,7 @@ class DotModulo(Op):
for (npy_int32 j = 0; j < N; ++j)
{
r +=
Ds[j * Ss] * (npy_int64)(Ak[j * SA])
;
r +=
(npy_int64)(Ds[j * Ss] * (npy_int64)(Ak[j * SA]))
%%
m
;
}
Dz[i * Sz] = r
%%
m;
...
...
@@ -113,13 +137,13 @@ class DotModulo(Op):
#MRG31k3p
#generator constants :
M1
=
numpy
.
int32
(
2147483647
)
#2^31 - 1
M2
=
numpy
.
int32
(
2147462579
)
#2^31 - 21069
MASK12
=
numpy
.
int32
(
511
)
#2^9 - 1
MASK13
=
numpy
.
int32
(
16777215
)
#2^24 - 1
MASK2
=
numpy
.
int32
(
65535
)
#2^16 - 1
M1
=
numpy
.
asarray
(
numpy
.
int32
(
2147483647
)
)
#2^31 - 1
M2
=
numpy
.
asarray
(
numpy
.
int32
(
2147462579
)
)
#2^31 - 21069
MASK12
=
numpy
.
int32
(
511
)
#2^9 - 1
MASK13
=
numpy
.
int32
(
16777215
)
#2^24 - 1
MASK2
=
numpy
.
int32
(
65535
)
#2^16 - 1
MULT2
=
numpy
.
int32
(
21069
)
NORM
=
4.656612873077392578125e-10
;
#1./2^31
NORM
=
4.656612873077392578125e-10
;
#1./2^31
A1p0
=
numpy
.
asarray
([[
0
,
4194304
,
129
],
[
1
,
0
,
0
],
[
0
,
1
,
0
]])
A2p0
=
numpy
.
asarray
([[
32768
,
0
,
32769
],
[
1
,
0
,
0
],
[
0
,
1
,
0
]])
...
...
theano/sandbox/test_rng_mrg.py
浏览文件 @
70d0c2d4
...
...
@@ -783,9 +783,13 @@ def test_multMatVect():
A
=
numpy
.
random
.
randint
(
0
,
numpy
.
iinfo
(
numpy
.
int32
)
.
max
,
(
3
,
3
))
.
astype
(
'int32'
)
s
=
numpy
.
random
.
randint
(
0
,
numpy
.
iinfo
(
numpy
.
int32
)
.
max
,
3
)
.
astype
(
'int32'
)
m
=
numpy
.
random
.
randint
(
numpy
.
iinfo
(
numpy
.
int32
)
.
max
)
m
=
numpy
.
asarray
(
numpy
.
random
.
randint
(
numpy
.
iinfo
(
numpy
.
int32
)
.
max
),
dtype
=
"int32"
)
f0
.
input_storage
[
0
]
.
storage
[
0
]
=
A
f0
.
input_storage
[
1
]
.
storage
[
0
]
=
s
f0
.
input_storage
[
2
]
.
storage
[
0
]
=
m
r_a
=
rng_mrg
.
matVecModM
(
A
,
s
,
m
)
r_b
=
f0
(
A
,
s
,
m
)
r_b
=
f0
.
fn
(
)
assert
numpy
.
allclose
(
r_a
,
r_b
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论