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
fda2f623
Commit
fda2f623
authored
Mar 24, 2019
by
Catalin Voss
Browse files
Fix test failures due to old torch issue with non-contiguous view
parent
0dd796e3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
8 additions
and
8 deletions
+8
-8
pytorch_pretrained_bert/modeling_gpt2.py
pytorch_pretrained_bert/modeling_gpt2.py
+4
-4
pytorch_pretrained_bert/modeling_openai.py
pytorch_pretrained_bert/modeling_openai.py
+4
-4
No files found.
pytorch_pretrained_bert/modeling_gpt2.py
View file @
fda2f623
...
@@ -618,8 +618,8 @@ class GPT2LMHeadModel(GPT2PreTrainedModel):
...
@@ -618,8 +618,8 @@ class GPT2LMHeadModel(GPT2PreTrainedModel):
lm_logits
=
self
.
lm_head
(
hidden_states
)
lm_logits
=
self
.
lm_head
(
hidden_states
)
if
lm_labels
is
not
None
:
if
lm_labels
is
not
None
:
# Shift so that tokens < n predict n
# Shift so that tokens < n predict n
shift_logits
=
lm_logits
[:,
:
-
1
]
shift_logits
=
lm_logits
[:,
:
-
1
]
.
contiguous
()
shift_labels
=
lm_labels
[:,
1
:]
shift_labels
=
lm_labels
[:,
1
:]
.
contiguous
()
# In tensorflow, it's [batch, d_0, d_1, ..., d_{r-1}, num_classes]
# In tensorflow, it's [batch, d_0, d_1, ..., d_{r-1}, num_classes]
# in pytorch, it's [batch, num_classes, d_0, d_1, ..., d_{r-1}]
# in pytorch, it's [batch, num_classes, d_0, d_1, ..., d_{r-1}]
...
@@ -698,8 +698,8 @@ class GPT2DoubleHeadsModel(GPT2PreTrainedModel):
...
@@ -698,8 +698,8 @@ class GPT2DoubleHeadsModel(GPT2PreTrainedModel):
mc_logits
=
self
.
multiple_choice_head
(
hidden_states
,
mc_token_ids
)
mc_logits
=
self
.
multiple_choice_head
(
hidden_states
,
mc_token_ids
)
losses
=
[]
losses
=
[]
if
lm_labels
is
not
None
:
if
lm_labels
is
not
None
:
shift_logits
=
lm_logits
[:,
:
-
1
]
shift_logits
=
lm_logits
[:,
:
-
1
]
.
contiguous
()
shift_labels
=
lm_labels
[:,
1
:]
shift_labels
=
lm_labels
[:,
1
:]
.
contiguous
()
loss_fct
=
CrossEntropyLoss
(
ignore_index
=-
1
)
loss_fct
=
CrossEntropyLoss
(
ignore_index
=-
1
)
losses
.
append
(
loss_fct
(
shift_logits
.
view
(
-
1
,
losses
.
append
(
loss_fct
(
shift_logits
.
view
(
-
1
,
shift_logits
.
size
(
-
1
)),
shift_labels
.
view
(
-
1
)))
shift_logits
.
size
(
-
1
)),
shift_labels
.
view
(
-
1
)))
...
...
pytorch_pretrained_bert/modeling_openai.py
View file @
fda2f623
...
@@ -717,8 +717,8 @@ class OpenAIGPTLMHeadModel(OpenAIGPTPreTrainedModel):
...
@@ -717,8 +717,8 @@ class OpenAIGPTLMHeadModel(OpenAIGPTPreTrainedModel):
lm_logits
=
self
.
lm_head
(
hidden_states
)
lm_logits
=
self
.
lm_head
(
hidden_states
)
if
lm_labels
is
not
None
:
if
lm_labels
is
not
None
:
# Shift so that tokens < n predict n
# Shift so that tokens < n predict n
shift_logits
=
lm_logits
[:,
:
-
1
]
shift_logits
=
lm_logits
[:,
:
-
1
]
.
contiguous
()
shift_labels
=
lm_labels
[:,
1
:]
shift_labels
=
lm_labels
[:,
1
:]
.
contiguous
()
# In tensorflow, it's [batch, d_0, d_1, ..., d_{r-1}, num_classes]
# In tensorflow, it's [batch, d_0, d_1, ..., d_{r-1}, num_classes]
# in pytorch, it's [batch, num_classes, d_0, d_1, ..., d_{r-1}]
# in pytorch, it's [batch, num_classes, d_0, d_1, ..., d_{r-1}]
...
@@ -811,8 +811,8 @@ class OpenAIGPTDoubleHeadsModel(OpenAIGPTPreTrainedModel):
...
@@ -811,8 +811,8 @@ class OpenAIGPTDoubleHeadsModel(OpenAIGPTPreTrainedModel):
mc_logits
=
self
.
multiple_choice_head
(
hidden_states
,
mc_token_ids
)
mc_logits
=
self
.
multiple_choice_head
(
hidden_states
,
mc_token_ids
)
losses
=
[]
losses
=
[]
if
lm_labels
is
not
None
:
if
lm_labels
is
not
None
:
shift_logits
=
lm_logits
[:,
:
-
1
]
shift_logits
=
lm_logits
[:,
:
-
1
]
.
contiguous
()
shift_labels
=
lm_labels
[:,
1
:]
shift_labels
=
lm_labels
[:,
1
:]
.
contiguous
()
loss_fct
=
CrossEntropyLoss
(
ignore_index
=-
1
)
loss_fct
=
CrossEntropyLoss
(
ignore_index
=-
1
)
losses
.
append
(
loss_fct
(
shift_logits
.
view
(
-
1
,
losses
.
append
(
loss_fct
(
shift_logits
.
view
(
-
1
,
shift_logits
.
size
(
-
1
)),
shift_labels
.
view
(
-
1
)))
shift_logits
.
size
(
-
1
)),
shift_labels
.
view
(
-
1
)))
...
...
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