Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
ebf84f07
Unverified
Commit
ebf84f07
authored
Feb 27, 2023
by
fxmarty
Committed by
GitHub
Feb 27, 2023
Browse files
Fix PyTorch Perceiver `PerceiverFourierPositionEncoding` with fp16 (#21787)
* fix perceiver fp16 * hopefully fix tests
parent
831f3144
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
6 deletions
+13
-6
src/transformers/models/perceiver/modeling_perceiver.py
src/transformers/models/perceiver/modeling_perceiver.py
+13
-6
No files found.
src/transformers/models/perceiver/modeling_perceiver.py
View file @
ebf84f07
...
...
@@ -2201,7 +2201,7 @@ class PerceiverBasicDecoder(PerceiverAbstractDecoder):
pos_emb
=
self
.
output_position_encodings
(
batch_size
)
elif
self
.
position_encoding_type
==
"fourier"
:
pos_emb
=
self
.
output_position_encodings
(
self
.
output_index_dims
,
batch_size
=
batch_size
,
device
=
inputs
.
device
,
pos
=
pos
self
.
output_index_dims
,
batch_size
=
batch_size
,
device
=
inputs
.
device
,
dtype
=
inputs
.
dtype
,
pos
=
pos
)
# Optionally project them to a target dimension.
...
...
@@ -2215,7 +2215,9 @@ class PerceiverBasicDecoder(PerceiverAbstractDecoder):
if
self
.
position_encoding_type
==
"trainable"
:
pos_emb
=
self
.
output_position_encodings
(
batch_size
)
elif
self
.
position_encoding_type
==
"fourier"
:
pos_emb
=
self
.
output_position_encodings
(
index_dims
,
batch_size
,
device
=
inputs
.
device
)
pos_emb
=
self
.
output_position_encodings
(
index_dims
,
batch_size
,
device
=
inputs
.
device
,
dtype
=
inputs
.
dtype
)
# Optionally project them to a target dimension.
pos_emb
=
self
.
positions_projection
(
pos_emb
)
...
...
@@ -2816,7 +2818,12 @@ class PerceiverFourierPositionEncoding(PerceiverAbstractPositionEncoding):
return
encoding_size
def
forward
(
self
,
index_dims
:
List
[
int
],
batch_size
:
int
,
device
,
pos
:
torch
.
FloatTensor
=
None
self
,
index_dims
:
List
[
int
],
batch_size
:
int
,
device
:
torch
.
device
,
dtype
:
torch
.
dtype
,
pos
:
torch
.
FloatTensor
=
None
,
)
->
torch
.
FloatTensor
:
pos
=
_check_or_build_spatial_positions
(
pos
,
index_dims
,
batch_size
)
fourier_pos_enc
=
generate_fourier_features
(
...
...
@@ -2825,7 +2832,7 @@ class PerceiverFourierPositionEncoding(PerceiverAbstractPositionEncoding):
max_resolution
=
self
.
max_resolution
,
concat_pos
=
self
.
concat_pos
,
sine_only
=
self
.
sine_only
,
).
to
(
device
)
).
to
(
device
=
device
,
dtype
=
dtype
)
return
fourier_pos_enc
...
...
@@ -3156,7 +3163,7 @@ class PerceiverImagePreprocessor(AbstractPreprocessor):
if
self
.
position_encoding_type
==
"trainable"
:
pos_enc
=
self
.
position_embeddings
(
batch_size
)
elif
self
.
position_encoding_type
==
"fourier"
:
pos_enc
=
self
.
position_embeddings
(
index_dims
,
batch_size
,
device
=
inputs
.
device
)
pos_enc
=
self
.
position_embeddings
(
index_dims
,
batch_size
,
device
=
inputs
.
device
,
dtype
=
inputs
.
dtype
)
# Optionally project them to a target dimension.
pos_enc
=
self
.
positions_projection
(
pos_enc
)
...
...
@@ -3324,7 +3331,7 @@ class PerceiverAudioPreprocessor(AbstractPreprocessor):
if
self
.
position_encoding_type
==
"trainable"
:
pos_enc
=
self
.
position_embeddings
(
batch_size
)
elif
self
.
position_encoding_type
==
"fourier"
:
pos_enc
=
self
.
position_embeddings
(
index_dims
,
batch_size
,
device
=
inputs
.
device
)
pos_enc
=
self
.
position_embeddings
(
index_dims
,
batch_size
,
device
=
inputs
.
device
,
dtype
=
inputs
.
dtype
)
# Optionally project them to a target dimension.
pos_enc
=
self
.
positions_projection
(
pos_enc
)
...
...
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