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
fd673510
Unverified
Commit
fd673510
authored
Jun 28, 2023
by
Yih-Dar
Committed by
GitHub
Jun 28, 2023
Browse files
Make PT/Flax tests could be run on GPU (#24557)
fix Co-authored-by:
ydshieh
<
ydshieh@users.noreply.github.com
>
parent
faae8d82
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
10 additions
and
10 deletions
+10
-10
tests/models/clip/test_modeling_clip.py
tests/models/clip/test_modeling_clip.py
+2
-2
tests/models/clipseg/test_modeling_clipseg.py
tests/models/clipseg/test_modeling_clipseg.py
+2
-2
tests/models/whisper/test_modeling_whisper.py
tests/models/whisper/test_modeling_whisper.py
+4
-4
tests/test_modeling_common.py
tests/test_modeling_common.py
+2
-2
No files found.
tests/models/clip/test_modeling_clip.py
View file @
fd673510
...
@@ -611,7 +611,7 @@ class CLIPModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
...
@@ -611,7 +611,7 @@ class CLIPModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
pt_outputs
=
pt_model
(
**
pt_inputs
).
to_tuple
()
pt_outputs
=
pt_model
(
**
pt_inputs
).
to_tuple
()
# convert inputs to Flax
# convert inputs to Flax
fx_inputs
=
{
k
:
np
.
array
(
v
)
for
k
,
v
in
pt_inputs
.
items
()
if
torch
.
is_tensor
(
v
)}
fx_inputs
=
{
k
:
np
.
array
(
v
.
to
(
"cpu"
)
)
for
k
,
v
in
pt_inputs
.
items
()
if
torch
.
is_tensor
(
v
)}
fx_outputs
=
fx_model
(
**
fx_inputs
).
to_tuple
()
fx_outputs
=
fx_model
(
**
fx_inputs
).
to_tuple
()
self
.
assertEqual
(
len
(
fx_outputs
),
len
(
pt_outputs
),
"Output lengths differ between Flax and PyTorch"
)
self
.
assertEqual
(
len
(
fx_outputs
),
len
(
pt_outputs
),
"Output lengths differ between Flax and PyTorch"
)
for
fx_output
,
pt_output
in
zip
(
fx_outputs
[:
4
],
pt_outputs
[:
4
]):
for
fx_output
,
pt_output
in
zip
(
fx_outputs
[:
4
],
pt_outputs
[:
4
]):
...
@@ -669,7 +669,7 @@ class CLIPModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
...
@@ -669,7 +669,7 @@ class CLIPModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
with
torch
.
no_grad
():
with
torch
.
no_grad
():
pt_outputs
=
pt_model
(
**
pt_inputs
).
to_tuple
()
pt_outputs
=
pt_model
(
**
pt_inputs
).
to_tuple
()
fx_inputs
=
{
k
:
np
.
array
(
v
)
for
k
,
v
in
pt_inputs
.
items
()
if
torch
.
is_tensor
(
v
)}
fx_inputs
=
{
k
:
np
.
array
(
v
.
to
(
"cpu"
)
)
for
k
,
v
in
pt_inputs
.
items
()
if
torch
.
is_tensor
(
v
)}
fx_outputs
=
fx_model
(
**
fx_inputs
).
to_tuple
()
fx_outputs
=
fx_model
(
**
fx_inputs
).
to_tuple
()
self
.
assertEqual
(
len
(
fx_outputs
),
len
(
pt_outputs
),
"Output lengths differ between Flax and PyTorch"
)
self
.
assertEqual
(
len
(
fx_outputs
),
len
(
pt_outputs
),
"Output lengths differ between Flax and PyTorch"
)
...
...
tests/models/clipseg/test_modeling_clipseg.py
View file @
fd673510
...
@@ -592,7 +592,7 @@ class CLIPSegModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase)
...
@@ -592,7 +592,7 @@ class CLIPSegModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase)
pt_outputs
=
pt_model
(
**
pt_inputs
).
to_tuple
()
pt_outputs
=
pt_model
(
**
pt_inputs
).
to_tuple
()
# convert inputs to Flax
# convert inputs to Flax
fx_inputs
=
{
k
:
np
.
array
(
v
)
for
k
,
v
in
pt_inputs
.
items
()
if
torch
.
is_tensor
(
v
)}
fx_inputs
=
{
k
:
np
.
array
(
v
.
to
(
"cpu"
)
)
for
k
,
v
in
pt_inputs
.
items
()
if
torch
.
is_tensor
(
v
)}
fx_outputs
=
fx_model
(
**
fx_inputs
).
to_tuple
()
fx_outputs
=
fx_model
(
**
fx_inputs
).
to_tuple
()
self
.
assertEqual
(
len
(
fx_outputs
),
len
(
pt_outputs
),
"Output lengths differ between Flax and PyTorch"
)
self
.
assertEqual
(
len
(
fx_outputs
),
len
(
pt_outputs
),
"Output lengths differ between Flax and PyTorch"
)
for
fx_output
,
pt_output
in
zip
(
fx_outputs
[:
4
],
pt_outputs
[:
4
]):
for
fx_output
,
pt_output
in
zip
(
fx_outputs
[:
4
],
pt_outputs
[:
4
]):
...
@@ -650,7 +650,7 @@ class CLIPSegModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase)
...
@@ -650,7 +650,7 @@ class CLIPSegModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase)
with
torch
.
no_grad
():
with
torch
.
no_grad
():
pt_outputs
=
pt_model
(
**
pt_inputs
).
to_tuple
()
pt_outputs
=
pt_model
(
**
pt_inputs
).
to_tuple
()
fx_inputs
=
{
k
:
np
.
array
(
v
)
for
k
,
v
in
pt_inputs
.
items
()
if
torch
.
is_tensor
(
v
)}
fx_inputs
=
{
k
:
np
.
array
(
v
.
to
(
"cpu"
)
)
for
k
,
v
in
pt_inputs
.
items
()
if
torch
.
is_tensor
(
v
)}
fx_outputs
=
fx_model
(
**
fx_inputs
).
to_tuple
()
fx_outputs
=
fx_model
(
**
fx_inputs
).
to_tuple
()
self
.
assertEqual
(
len
(
fx_outputs
),
len
(
pt_outputs
),
"Output lengths differ between Flax and PyTorch"
)
self
.
assertEqual
(
len
(
fx_outputs
),
len
(
pt_outputs
),
"Output lengths differ between Flax and PyTorch"
)
...
...
tests/models/whisper/test_modeling_whisper.py
View file @
fd673510
...
@@ -875,7 +875,7 @@ class WhisperModelTest(ModelTesterMixin, GenerationTesterMixin, PipelineTesterMi
...
@@ -875,7 +875,7 @@ class WhisperModelTest(ModelTesterMixin, GenerationTesterMixin, PipelineTesterMi
}
}
# convert inputs to Flax
# convert inputs to Flax
fx_inputs
=
{
k
:
np
.
array
(
v
)
for
k
,
v
in
pt_inputs
.
items
()
if
torch
.
is_tensor
(
v
)}
fx_inputs
=
{
k
:
np
.
array
(
v
.
to
(
"cpu"
)
)
for
k
,
v
in
pt_inputs
.
items
()
if
torch
.
is_tensor
(
v
)}
fx_state
=
convert_pytorch_state_dict_to_flax
(
pt_model
.
state_dict
(),
fx_model
)
fx_state
=
convert_pytorch_state_dict_to_flax
(
pt_model
.
state_dict
(),
fx_model
)
fx_model
.
params
=
fx_state
fx_model
.
params
=
fx_state
...
@@ -948,7 +948,7 @@ class WhisperModelTest(ModelTesterMixin, GenerationTesterMixin, PipelineTesterMi
...
@@ -948,7 +948,7 @@ class WhisperModelTest(ModelTesterMixin, GenerationTesterMixin, PipelineTesterMi
}
}
# convert inputs to Flax
# convert inputs to Flax
fx_inputs
=
{
k
:
np
.
array
(
v
)
for
k
,
v
in
pt_inputs
.
items
()
if
torch
.
is_tensor
(
v
)}
fx_inputs
=
{
k
:
np
.
array
(
v
.
to
(
"cpu"
)
)
for
k
,
v
in
pt_inputs
.
items
()
if
torch
.
is_tensor
(
v
)}
pt_model
=
load_flax_weights_in_pytorch_model
(
pt_model
,
fx_model
.
params
)
pt_model
=
load_flax_weights_in_pytorch_model
(
pt_model
,
fx_model
.
params
)
...
@@ -1805,7 +1805,7 @@ class WhisperEncoderModelTest(ModelTesterMixin, GenerationTesterMixin, unittest.
...
@@ -1805,7 +1805,7 @@ class WhisperEncoderModelTest(ModelTesterMixin, GenerationTesterMixin, unittest.
}
}
# convert inputs to Flax
# convert inputs to Flax
fx_inputs
=
{
k
:
np
.
array
(
v
)
for
k
,
v
in
pt_inputs
.
items
()
if
torch
.
is_tensor
(
v
)}
fx_inputs
=
{
k
:
np
.
array
(
v
.
to
(
"cpu"
)
)
for
k
,
v
in
pt_inputs
.
items
()
if
torch
.
is_tensor
(
v
)}
fx_state
=
convert_pytorch_state_dict_to_flax
(
pt_model
.
state_dict
(),
fx_model
)
fx_state
=
convert_pytorch_state_dict_to_flax
(
pt_model
.
state_dict
(),
fx_model
)
fx_model
.
params
=
fx_state
fx_model
.
params
=
fx_state
...
@@ -1878,7 +1878,7 @@ class WhisperEncoderModelTest(ModelTesterMixin, GenerationTesterMixin, unittest.
...
@@ -1878,7 +1878,7 @@ class WhisperEncoderModelTest(ModelTesterMixin, GenerationTesterMixin, unittest.
}
}
# convert inputs to Flax
# convert inputs to Flax
fx_inputs
=
{
k
:
np
.
array
(
v
)
for
k
,
v
in
pt_inputs
.
items
()
if
torch
.
is_tensor
(
v
)}
fx_inputs
=
{
k
:
np
.
array
(
v
.
to
(
"cpu"
)
)
for
k
,
v
in
pt_inputs
.
items
()
if
torch
.
is_tensor
(
v
)}
pt_model
=
load_flax_weights_in_pytorch_model
(
pt_model
,
fx_model
.
params
)
pt_model
=
load_flax_weights_in_pytorch_model
(
pt_model
,
fx_model
.
params
)
...
...
tests/test_modeling_common.py
View file @
fd673510
...
@@ -2206,7 +2206,7 @@ class ModelTesterMixin:
...
@@ -2206,7 +2206,7 @@ class ModelTesterMixin:
}
}
# convert inputs to Flax
# convert inputs to Flax
fx_inputs
=
{
k
:
np
.
array
(
v
)
for
k
,
v
in
pt_inputs
.
items
()
if
torch
.
is_tensor
(
v
)}
fx_inputs
=
{
k
:
np
.
array
(
v
.
to
(
"cpu"
)
)
for
k
,
v
in
pt_inputs
.
items
()
if
torch
.
is_tensor
(
v
)}
fx_state
=
convert_pytorch_state_dict_to_flax
(
pt_model
.
state_dict
(),
fx_model
)
fx_state
=
convert_pytorch_state_dict_to_flax
(
pt_model
.
state_dict
(),
fx_model
)
fx_model
.
params
=
fx_state
fx_model
.
params
=
fx_state
...
@@ -2278,7 +2278,7 @@ class ModelTesterMixin:
...
@@ -2278,7 +2278,7 @@ class ModelTesterMixin:
}
}
# convert inputs to Flax
# convert inputs to Flax
fx_inputs
=
{
k
:
np
.
array
(
v
)
for
k
,
v
in
pt_inputs
.
items
()
if
torch
.
is_tensor
(
v
)}
fx_inputs
=
{
k
:
np
.
array
(
v
.
to
(
"cpu"
)
)
for
k
,
v
in
pt_inputs
.
items
()
if
torch
.
is_tensor
(
v
)}
pt_model
=
load_flax_weights_in_pytorch_model
(
pt_model
,
fx_model
.
params
)
pt_model
=
load_flax_weights_in_pytorch_model
(
pt_model
,
fx_model
.
params
)
...
...
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