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
chenpangpang
transformers
Commits
c651eb23
Unverified
Commit
c651eb23
authored
Nov 22, 2023
by
Wangyi Jiang
Committed by
GitHub
Nov 22, 2023
Browse files
Simplify the implementation of jitter noise in moe models (#27643)
parent
b54993aa
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
2 additions
and
20 deletions
+2
-20
src/transformers/models/gptsan_japanese/modeling_gptsan_japanese.py
...ormers/models/gptsan_japanese/modeling_gptsan_japanese.py
+1
-10
src/transformers/models/switch_transformers/modeling_switch_transformers.py
...odels/switch_transformers/modeling_switch_transformers.py
+1
-10
No files found.
src/transformers/models/gptsan_japanese/modeling_gptsan_japanese.py
View file @
c651eb23
...
...
@@ -188,17 +188,8 @@ class GPTSanJapaneseTop1Router(nn.Module):
hidden_states
=
hidden_states
.
to
(
self
.
dtype
)
if
self
.
jitter_noise
>
0
:
# Get the lower and upper bound of the uniform distribution
# Adapted from: https://stackoverflow.com/questions/44328530/how-to-get-a-uniform-distribution-in-a-range-r1-r2-in-pytorch
distrib_lower_bound
=
1.0
-
self
.
jitter_noise
distrib_upper_bound
=
1.0
+
self
.
jitter_noise
uniform_distrib
=
torch
.
rand
(
hidden_states
.
shape
,
device
=
hidden_states
.
device
,
dtype
=
self
.
dtype
)
uniform_distrib
=
uniform_distrib
*
(
distrib_lower_bound
-
distrib_upper_bound
)
uniform_distrib
=
uniform_distrib
+
distrib_upper_bound
# Multiply the token inputs by the uniform distribution - adding some noise
hidden_states
*=
uniform_distrib
hidden_states
*=
torch
.
empty_like
(
hidden_states
).
uniform_
(
1.0
-
self
.
jitter_noise
,
1.0
+
self
.
jitter_noise
)
# Shape: [num_groups, tokens_per_group, num_experts]
self
.
_cast_classifier
()
...
...
src/transformers/models/switch_transformers/modeling_switch_transformers.py
View file @
c651eb23
...
...
@@ -169,17 +169,8 @@ class SwitchTransformersTop1Router(nn.Module):
hidden_states
=
hidden_states
.
to
(
self
.
dtype
)
if
self
.
jitter_noise
>
0
:
# Get the lower and upper bound of the uniform distribution
# Adapted from: https://stackoverflow.com/questions/44328530/how-to-get-a-uniform-distribution-in-a-range-r1-r2-in-pytorch
distrib_lower_bound
=
1.0
-
self
.
jitter_noise
distrib_upper_bound
=
1.0
+
self
.
jitter_noise
uniform_distrib
=
torch
.
rand
(
hidden_states
.
shape
,
device
=
hidden_states
.
device
,
dtype
=
self
.
dtype
)
uniform_distrib
=
uniform_distrib
*
(
distrib_lower_bound
-
distrib_upper_bound
)
uniform_distrib
=
uniform_distrib
+
distrib_upper_bound
# Multiply the token inputs by the uniform distribution - adding some noise
hidden_states
*=
uniform_distrib
hidden_states
*=
torch
.
empty_like
(
hidden_states
).
uniform_
(
1.0
-
self
.
jitter_noise
,
1.0
+
self
.
jitter_noise
)
# Shape: [num_groups, tokens_per_group, num_experts]
self
.
_cast_classifier
()
...
...
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