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
e58e7b3b
Commit
e58e7b3b
authored
May 24, 2021
by
Rich Ho
Browse files
add random routing in gshard gate
parent
ba2b7aa9
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
8 additions
and
2 deletions
+8
-2
fmoe/gates/gshard_gate.py
fmoe/gates/gshard_gate.py
+8
-1
fmoe/gates/switch_gate.py
fmoe/gates/switch_gate.py
+0
-1
No files found.
fmoe/gates/gshard_gate.py
View file @
e58e7b3b
...
@@ -9,9 +9,11 @@ from .utils import limit_by_capacity
...
@@ -9,9 +9,11 @@ from .utils import limit_by_capacity
class
GShardGate
(
NaiveGate
):
class
GShardGate
(
NaiveGate
):
def
__init__
(
self
,
d_model
,
num_expert
,
world_size
,
capacity
=
(
1.2
,
2.4
)):
def
__init__
(
self
,
d_model
,
num_expert
,
world_size
,
capacity
=
(
1.2
,
2.4
),
random_routing
=
True
):
super
().
__init__
(
d_model
,
num_expert
,
world_size
,
top_k
=
2
)
super
().
__init__
(
d_model
,
num_expert
,
world_size
,
top_k
=
2
)
self
.
capacity
=
capacity
self
.
capacity
=
capacity
self
.
random_routing
=
True
def
forward
(
self
,
x
):
def
forward
(
self
,
x
):
naive_outs
=
super
().
forward
(
x
,
return_all_scores
=
True
)
naive_outs
=
super
().
forward
(
x
,
return_all_scores
=
True
)
...
@@ -34,4 +36,9 @@ class GShardGate(NaiveGate):
...
@@ -34,4 +36,9 @@ class GShardGate(NaiveGate):
capacity
=
math
.
ceil
(
cap_rate
*
x
.
shape
[
0
])
capacity
=
math
.
ceil
(
cap_rate
*
x
.
shape
[
0
])
limit_by_capacity
(
topk_idx
,
self
.
num_expert
,
self
.
world_size
,
capacity
)
limit_by_capacity
(
topk_idx
,
self
.
num_expert
,
self
.
world_size
,
capacity
)
if
self
.
random_routing
:
rand_routing_prob
=
torch
.
rand
(
gate_score
.
size
(
0
),
device
=
x
.
device
)
mask
=
(
2
*
topk_val
[:,
1
]
<
rand_routing_prob
)
topk_idx
[:,
1
].
masked_fill_
(
mask
,
-
1
)
return
topk_idx
,
topk_val
return
topk_idx
,
topk_val
fmoe/gates/switch_gate.py
View file @
e58e7b3b
...
@@ -39,7 +39,6 @@ class SwitchGate(NaiveGate):
...
@@ -39,7 +39,6 @@ class SwitchGate(NaiveGate):
score
,
k
=
1
,
dim
=-
1
,
largest
=
True
score
,
k
=
1
,
dim
=-
1
,
largest
=
True
)
# [.. x top_k]
)
# [.. x top_k]
top1_score
=
top1_score
.
to
(
dtype
=
inp
.
dtype
)
top1_score
=
top1_score
.
to
(
dtype
=
inp
.
dtype
)
top1_score
=
top1_score
.
to
(
dtype
=
inp
.
dtype
)
cap_rate
=
self
.
capacity
[
0
if
self
.
training
else
1
]
cap_rate
=
self
.
capacity
[
0
if
self
.
training
else
1
]
capacity
=
math
.
ceil
(
cap_rate
*
inp
.
shape
[
0
])
capacity
=
math
.
ceil
(
cap_rate
*
inp
.
shape
[
0
])
...
...
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