Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
FastMoE
Commits
51361a3f
Commit
51361a3f
authored
Dec 06, 2020
by
Jiezhong Qiu
Browse files
residual in hmoe
parent
823f9c2e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
6 additions
and
3 deletions
+6
-3
pytorch/mem_transformer.py
pytorch/mem_transformer.py
+6
-3
No files found.
pytorch/mem_transformer.py
View file @
51361a3f
...
...
@@ -84,7 +84,7 @@ class MoEPositionwiseFF(nn.Module):
# return output, relu_out.detach()
class
HierarchicalMoEPositionwiseFF
(
nn
.
Module
):
def
__init__
(
self
,
d_model
,
d_inner
,
dropout
,
pre_lnorm
=
False
,
n_block
=
512
,
top_block
=
12
8
):
def
__init__
(
self
,
d_model
,
d_inner
,
dropout
,
pre_lnorm
=
False
,
n_block
=
64
,
top_block
=
8
):
super
(
HierarchicalMoEPositionwiseFF
,
self
).
__init__
()
print
(
"HierarchicalMoEPositionwiseFF"
)
...
...
@@ -133,14 +133,17 @@ class HierarchicalMoEPositionwiseFF(nn.Module):
block
=
self
.
block_net
(
inp
)
block_val
,
block_idx
=
torch
.
topk
(
block
,
k
=
self
.
top_block
,
dim
=-
1
,
largest
=
True
,
sorted
=
False
)
# [.. x top_k]
block_val
.
mul_
(
self
.
scale
)
# block_val.mul_(self.scale)
gate
=
F
.
softmax
(
block_val
,
dim
=-
1
)
W1_block
=
self
.
W1
[
block_idx
]
# [.. x top_k x d_block x d_model]
b1_block
=
self
.
b1
[
block_idx
]
# [.. x top_k x d_block]
x
=
torch
.
einsum
(
'ibd,ibnhd->ibnh'
,
(
inp
,
W1_block
))
+
b1_block
x
=
torch
.
einsum
(
'ibd,ibnhd->ibnh'
,
(
inp
,
W1_block
))
+
b1_block
# [.. x top_k x d_block]
x
=
x
+
block_val
.
unsqueeze
(
-
1
)
# somehow like residual
x
=
x
*
gate
.
unsqueeze
(
-
1
)
relu_out
=
F
.
relu
(
x
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment