Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Shape Transformer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Gregor Kobsik
Shape Transformer
Commits
ad18c097
Commit
ad18c097
authored
3 years ago
by
Gregor Kobsik
Browse files
Options
Downloads
Patches
Plain Diff
refactored transformer - compute memory/logits
- added external functions to compute memory or logits for sampler
parent
f1b5f801
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
modules/architecture/transformer.py
+21
-12
21 additions, 12 deletions
modules/architecture/transformer.py
with
21 additions
and
12 deletions
modules/architecture/transformer.py
+
21
−
12
View file @
ad18c097
...
...
@@ -166,15 +166,24 @@ class Transformer(nn.Module):
# process sequence layers individually
for
idx
,
seq_layer
in
enumerate
(
sequence
):
is_final
=
idx
==
seq_len
-
1
if
idx
<
seq_len
-
1
:
# intermediate layer
memory
=
self
.
compute_memory
(
seq_layer
,
memory
,
idx
,
False
)
# [N, L, E]
else
:
# only final layer
return
self
.
compute_logits
(
seq_layer
,
memory
,
idx
)
# [N, T, V]
def
compute_memory
(
self
,
seq_layer
,
memory
,
idx
,
is_final
):
"""
"""
# embed sequence tokens
emb
=
self
.
embedding
[
idx
](
*
seq_layer
)
# [N, L, E]
seq_mask
=
self
.
embedding
[
idx
].
padding_mask
(
*
seq_layer
)
# [N, L]
# compute memory / process sequence
memory
=
self
.
process
(
emb
,
memory
,
seq_mask
,
idx
,
is_final
)
# [N, L, E]
return
self
.
process
(
emb
,
memory
,
seq_mask
,
idx
,
is_final
)
# [N, L, E]
def
compute_logits
(
self
,
seq_layer
,
memory
,
idx
):
"""
"""
# compute memory
memory
=
self
.
compute_memory
(
seq_layer
,
memory
,
idx
,
True
)
# [N, L, E]
# return logits
if
is_final
:
# compute only for final layer
return
self
.
head
[
idx
](
memory
,
*
seq_layer
)
# [N, T, V]
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment