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
2f4cdd97
Unverified
Commit
2f4cdd97
authored
Mar 10, 2023
by
Dean Wyatte
Committed by
GitHub
Mar 10, 2023
Browse files
handle numpy inputs in whole word mask data collator (#22032)
parent
a70da86b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
7 deletions
+24
-7
src/transformers/data/data_collator.py
src/transformers/data/data_collator.py
+3
-1
tests/trainer/test_data_collator.py
tests/trainer/test_data_collator.py
+21
-6
No files found.
src/transformers/data/data_collator.py
View file @
2f4cdd97
...
...
@@ -883,6 +883,8 @@ class DataCollatorForWholeWordMask(DataCollatorForLanguageModeling):
return
{
"input_ids"
:
inputs
,
"labels"
:
labels
}
def
tf_call
(
self
,
examples
:
List
[
Union
[
List
[
int
],
Any
,
Dict
[
str
,
Any
]]])
->
Dict
[
str
,
Any
]:
import
tensorflow
as
tf
if
isinstance
(
examples
[
0
],
Mapping
):
input_ids
=
[
e
[
"input_ids"
]
for
e
in
examples
]
else
:
...
...
@@ -907,7 +909,7 @@ class DataCollatorForWholeWordMask(DataCollatorForLanguageModeling):
ref_tokens
[
i
]
=
"##"
+
ref_tokens
[
i
]
mask_labels
.
append
(
self
.
_whole_word_mask
(
ref_tokens
))
batch_mask
=
_tf_collate_batch
(
mask_labels
,
self
.
tokenizer
,
pad_to_multiple_of
=
self
.
pad_to_multiple_of
)
inputs
,
labels
=
self
.
tf_mask_tokens
(
batch_input
,
batch_mask
)
inputs
,
labels
=
self
.
tf_mask_tokens
(
tf
.
cast
(
batch_input
,
tf
.
int64
),
batch_mask
)
return
{
"input_ids"
:
inputs
,
"labels"
:
labels
}
def
numpy_call
(
self
,
examples
:
List
[
Union
[
List
[
int
],
Any
,
Dict
[
str
,
Any
]]])
->
Dict
[
str
,
Any
]:
...
...
tests/trainer/test_data_collator.py
View file @
2f4cdd97
...
...
@@ -271,12 +271,17 @@ class DataCollatorIntegrationTest(unittest.TestCase):
self
.
_test_no_pad_and_pad
(
no_pad_features
,
pad_features
)
def
test_data_collator_for_whole_word_mask
(
self
):
features
=
[{
"input_ids"
:
list
(
range
(
10
))},
{
"input_ids"
:
list
(
range
(
10
))}]
tokenizer
=
BertTokenizer
(
self
.
vocab_file
)
data_collator
=
DataCollatorForWholeWordMask
(
tokenizer
,
return_tensors
=
"pt"
)
features
=
[{
"input_ids"
:
list
(
range
(
10
))},
{
"input_ids"
:
list
(
range
(
10
))}]
batch
=
data_collator
(
features
)
self
.
assertEqual
(
batch
[
"input_ids"
].
shape
,
torch
.
Size
((
2
,
10
)))
self
.
assertEqual
(
batch
[
"labels"
].
shape
,
torch
.
Size
((
2
,
10
)))
# Features can already be tensors
features
=
[{
"input_ids"
:
np
.
arange
(
10
)},
{
"input_ids"
:
np
.
arange
(
10
)}]
batch
=
data_collator
(
features
)
self
.
assertEqual
(
batch
[
"input_ids"
].
shape
,
torch
.
Size
((
2
,
10
)))
self
.
assertEqual
(
batch
[
"labels"
].
shape
,
torch
.
Size
((
2
,
10
)))
...
...
@@ -553,12 +558,17 @@ class TFDataCollatorIntegrationTest(unittest.TestCase):
self
.
_test_no_pad_and_pad
(
no_pad_features
,
pad_features
)
def
test_data_collator_for_whole_word_mask
(
self
):
features
=
[{
"input_ids"
:
list
(
range
(
10
))},
{
"input_ids"
:
list
(
range
(
10
))}]
tokenizer
=
BertTokenizer
(
self
.
vocab_file
)
data_collator
=
DataCollatorForWholeWordMask
(
tokenizer
,
return_tensors
=
"tf"
)
features
=
[{
"input_ids"
:
list
(
range
(
10
))},
{
"input_ids"
:
list
(
range
(
10
))}]
batch
=
data_collator
(
features
)
self
.
assertEqual
(
batch
[
"input_ids"
].
shape
.
as_list
(),
[
2
,
10
])
self
.
assertEqual
(
batch
[
"labels"
].
shape
.
as_list
(),
[
2
,
10
])
# Features can already be tensors
features
=
[{
"input_ids"
:
np
.
arange
(
10
)},
{
"input_ids"
:
np
.
arange
(
10
)}]
batch
=
data_collator
(
features
)
self
.
assertEqual
(
batch
[
"input_ids"
].
shape
.
as_list
(),
[
2
,
10
])
self
.
assertEqual
(
batch
[
"labels"
].
shape
.
as_list
(),
[
2
,
10
])
...
...
@@ -825,12 +835,17 @@ class NumpyDataCollatorIntegrationTest(unittest.TestCase):
self
.
_test_no_pad_and_pad
(
no_pad_features
,
pad_features
)
def
test_data_collator_for_whole_word_mask
(
self
):
features
=
[{
"input_ids"
:
list
(
range
(
10
))},
{
"input_ids"
:
list
(
range
(
10
))}]
tokenizer
=
BertTokenizer
(
self
.
vocab_file
)
data_collator
=
DataCollatorForWholeWordMask
(
tokenizer
,
return_tensors
=
"np"
)
features
=
[{
"input_ids"
:
list
(
range
(
10
))},
{
"input_ids"
:
list
(
range
(
10
))}]
batch
=
data_collator
(
features
)
self
.
assertEqual
(
batch
[
"input_ids"
].
shape
,
(
2
,
10
))
self
.
assertEqual
(
batch
[
"labels"
].
shape
,
(
2
,
10
))
# Features can already be tensors
features
=
[{
"input_ids"
:
np
.
arange
(
10
)},
{
"input_ids"
:
np
.
arange
(
10
)}]
batch
=
data_collator
(
features
)
self
.
assertEqual
(
batch
[
"input_ids"
].
shape
,
(
2
,
10
))
self
.
assertEqual
(
batch
[
"labels"
].
shape
,
(
2
,
10
))
...
...
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