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
32dbb2d9
"vscode:/vscode.git/clone" did not exist on "ba52dec47f870ab713317d3e61ba209bb5800783"
Unverified
Commit
32dbb2d9
authored
Apr 26, 2021
by
Patrick von Platen
Committed by
GitHub
Apr 26, 2021
Browse files
make style (#11442)
parent
04ab2ca6
Changes
105
Hide whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
32 additions
and
32 deletions
+32
-32
examples/legacy/question-answering/run_squad.py
examples/legacy/question-answering/run_squad.py
+1
-1
examples/legacy/run_openai_gpt.py
examples/legacy/run_openai_gpt.py
+2
-2
examples/legacy/run_swag.py
examples/legacy/run_swag.py
+1
-1
examples/legacy/seq2seq/minify_dataset.py
examples/legacy/seq2seq/minify_dataset.py
+1
-1
examples/research_projects/bert-loses-patience/run_glue_with_pabee.py
...earch_projects/bert-loses-patience/run_glue_with_pabee.py
+1
-1
examples/research_projects/bertabs/modeling_bertabs.py
examples/research_projects/bertabs/modeling_bertabs.py
+7
-7
examples/research_projects/bertabs/test_utils_summarization.py
...les/research_projects/bertabs/test_utils_summarization.py
+3
-3
examples/research_projects/bertabs/utils_summarization.py
examples/research_projects/bertabs/utils_summarization.py
+1
-1
examples/research_projects/bertology/run_bertology.py
examples/research_projects/bertology/run_bertology.py
+2
-2
examples/research_projects/bertology/run_prune_gpt.py
examples/research_projects/bertology/run_prune_gpt.py
+2
-2
examples/research_projects/deebert/run_glue_deebert.py
examples/research_projects/deebert/run_glue_deebert.py
+1
-1
examples/research_projects/distillation/run_squad_w_distillation.py
...esearch_projects/distillation/run_squad_w_distillation.py
+1
-1
examples/research_projects/mm-imdb/run_mmimdb.py
examples/research_projects/mm-imdb/run_mmimdb.py
+1
-1
examples/research_projects/movement-pruning/emmental/modeling_bert_masked.py
...rojects/movement-pruning/emmental/modeling_bert_masked.py
+1
-1
examples/research_projects/movement-pruning/masked_run_glue.py
...les/research_projects/movement-pruning/masked_run_glue.py
+1
-1
examples/research_projects/movement-pruning/masked_run_squad.py
...es/research_projects/movement-pruning/masked_run_squad.py
+1
-1
src/transformers/commands/lfs.py
src/transformers/commands/lfs.py
+1
-1
src/transformers/data/processors/utils.py
src/transformers/data/processors/utils.py
+1
-1
src/transformers/file_utils.py
src/transformers/file_utils.py
+2
-2
src/transformers/modelcard.py
src/transformers/modelcard.py
+1
-1
No files found.
examples/legacy/question-answering/run_squad.py
View file @
32dbb2d9
...
...
@@ -74,7 +74,7 @@ def to_list(tensor):
def
train
(
args
,
train_dataset
,
model
,
tokenizer
):
"""
Train the model
"""
"""Train the model"""
if
args
.
local_rank
in
[
-
1
,
0
]:
tb_writer
=
SummaryWriter
()
...
...
examples/legacy/run_openai_gpt.py
View file @
32dbb2d9
...
...
@@ -61,7 +61,7 @@ def accuracy(out, labels):
def
load_rocstories_dataset
(
dataset_path
):
"""
Output a list of tuples(story, 1st continuation, 2nd continuation, label)
"""
"""Output a list of tuples(story, 1st continuation, 2nd continuation, label)"""
with
open
(
dataset_path
,
encoding
=
"utf_8"
)
as
f
:
f
=
csv
.
reader
(
f
)
output
=
[]
...
...
@@ -184,7 +184,7 @@ def main():
# Load and encode the datasets
def
tokenize_and_encode
(
obj
):
"""
Tokenize and encode a nested object
"""
"""Tokenize and encode a nested object"""
if
isinstance
(
obj
,
str
):
return
tokenizer
.
convert_tokens_to_ids
(
tokenizer
.
tokenize
(
obj
))
elif
isinstance
(
obj
,
int
):
...
...
examples/legacy/run_swag.py
View file @
32dbb2d9
...
...
@@ -276,7 +276,7 @@ def load_and_cache_examples(args, tokenizer, evaluate=False, output_examples=Fal
def
train
(
args
,
train_dataset
,
model
,
tokenizer
):
"""
Train the model
"""
"""Train the model"""
if
args
.
local_rank
in
[
-
1
,
0
]:
tb_writer
=
SummaryWriter
()
...
...
examples/legacy/seq2seq/minify_dataset.py
View file @
32dbb2d9
...
...
@@ -19,7 +19,7 @@ import fire
def
minify
(
src_dir
:
str
,
dest_dir
:
str
,
n
:
int
):
"""Write first n lines of each file f in src_dir to dest_dir/f
"""
"""Write first n lines of each file f in src_dir to dest_dir/f"""
src_dir
=
Path
(
src_dir
)
dest_dir
=
Path
(
dest_dir
)
dest_dir
.
mkdir
(
exist_ok
=
True
)
...
...
examples/research_projects/bert-loses-patience/run_glue_with_pabee.py
View file @
32dbb2d9
...
...
@@ -71,7 +71,7 @@ def set_seed(args):
def
train
(
args
,
train_dataset
,
model
,
tokenizer
):
"""
Train the model
"""
"""Train the model"""
if
args
.
local_rank
in
[
-
1
,
0
]:
tb_writer
=
SummaryWriter
()
...
...
examples/research_projects/bertabs/modeling_bertabs.py
View file @
32dbb2d9
...
...
@@ -251,7 +251,7 @@ class TransformerDecoder(nn.Module):
return
output
,
state
# , state
def
init_decoder_state
(
self
,
src
,
memory_bank
,
with_cache
=
False
):
"""
Init decoder state
"""
"""Init decoder state"""
state
=
TransformerDecoderState
(
src
)
if
with_cache
:
state
.
_init_cache
(
memory_bank
,
self
.
num_layers
)
...
...
@@ -479,11 +479,11 @@ class MultiHeadedAttention(nn.Module):
head_count
=
self
.
head_count
def
shape
(
x
):
"""
projection
"""
"""projection"""
return
x
.
view
(
batch_size
,
-
1
,
head_count
,
dim_per_head
).
transpose
(
1
,
2
)
def
unshape
(
x
):
"""
compute context
"""
"""compute context"""
return
x
.
transpose
(
1
,
2
).
contiguous
().
view
(
batch_size
,
-
1
,
head_count
*
dim_per_head
)
# 1) Project key, value, and query.
...
...
@@ -571,12 +571,12 @@ class DecoderState(object):
"""
def
detach
(
self
):
"""
Need to document this
"""
"""Need to document this"""
self
.
hidden
=
tuple
([
_
.
detach
()
for
_
in
self
.
hidden
])
self
.
input_feed
=
self
.
input_feed
.
detach
()
def
beam_update
(
self
,
idx
,
positions
,
beam_size
):
"""
Need to document this
"""
"""Need to document this"""
for
e
in
self
.
_all
:
sizes
=
e
.
size
()
br
=
sizes
[
1
]
...
...
@@ -592,7 +592,7 @@ class DecoderState(object):
class
TransformerDecoderState
(
DecoderState
):
"""
Transformer Decoder state base class
"""
"""Transformer Decoder state base class"""
def
__init__
(
self
,
src
):
"""
...
...
@@ -638,7 +638,7 @@ class TransformerDecoderState(DecoderState):
self
.
cache
[
"layer_{}"
.
format
(
l
)]
=
layer_cache
def
repeat_beam_size_times
(
self
,
beam_size
):
"""
Repeat beam_size times along batch dimension.
"""
"""Repeat beam_size times along batch dimension."""
self
.
src
=
self
.
src
.
data
.
repeat
(
1
,
beam_size
,
1
)
def
map_batch_fn
(
self
,
fn
):
...
...
examples/research_projects/bertabs/test_utils_summarization.py
View file @
32dbb2d9
...
...
@@ -25,19 +25,19 @@ class SummarizationDataProcessingTest(unittest.TestCase):
self
.
block_size
=
10
def
test_fit_to_block_sequence_too_small
(
self
):
"""
Pad the sequence with 0 if the sequence is smaller than the block size."""
"""Pad the sequence with 0 if the sequence is smaller than the block size."""
sequence
=
[
1
,
2
,
3
,
4
]
expected_output
=
[
1
,
2
,
3
,
4
,
0
,
0
,
0
,
0
,
0
,
0
]
self
.
assertEqual
(
truncate_or_pad
(
sequence
,
self
.
block_size
,
0
),
expected_output
)
def
test_fit_to_block_sequence_fit_exactly
(
self
):
"""
Do nothing if the sequence is the right size.
"""
"""Do nothing if the sequence is the right size."""
sequence
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
]
expected_output
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
]
self
.
assertEqual
(
truncate_or_pad
(
sequence
,
self
.
block_size
,
0
),
expected_output
)
def
test_fit_to_block_sequence_too_big
(
self
):
"""
Truncate the sequence if it is too long.
"""
"""Truncate the sequence if it is too long."""
sequence
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
,
11
,
12
,
13
]
expected_output
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
]
self
.
assertEqual
(
truncate_or_pad
(
sequence
,
self
.
block_size
,
0
),
expected_output
)
...
...
examples/research_projects/bertabs/utils_summarization.py
View file @
32dbb2d9
...
...
@@ -47,7 +47,7 @@ class CNNDMDataset(Dataset):
self
.
documents
.
append
(
path_to_story
)
def
__len__
(
self
):
"""
Returns the number of documents.
"""
"""Returns the number of documents."""
return
len
(
self
.
documents
)
def
__getitem__
(
self
,
idx
):
...
...
examples/research_projects/bertology/run_bertology.py
View file @
32dbb2d9
...
...
@@ -49,14 +49,14 @@ logger = logging.getLogger(__name__)
def
entropy
(
p
):
"""
Compute the entropy of a probability distribution
"""
"""Compute the entropy of a probability distribution"""
plogp
=
p
*
torch
.
log
(
p
)
plogp
[
p
==
0
]
=
0
return
-
plogp
.
sum
(
dim
=-
1
)
def
print_2d_tensor
(
tensor
):
"""
Print a 2D tensor
"""
"""Print a 2D tensor"""
logger
.
info
(
"lv, h >
\t
"
+
"
\t
"
.
join
(
f
"
{
x
+
1
}
"
for
x
in
range
(
len
(
tensor
))))
for
row
in
range
(
len
(
tensor
)):
if
tensor
.
dtype
!=
torch
.
long
:
...
...
examples/research_projects/bertology/run_prune_gpt.py
View file @
32dbb2d9
...
...
@@ -36,7 +36,7 @@ def save_model(model, dirpath):
def
entropy
(
p
,
unlogit
=
False
):
"""
Compute the entropy of a probability distribution
"""
"""Compute the entropy of a probability distribution"""
exponent
=
2
if
unlogit
:
p
=
torch
.
pow
(
p
,
exponent
)
...
...
@@ -46,7 +46,7 @@ def entropy(p, unlogit=False):
def
print_2d_tensor
(
tensor
):
"""
Print a 2D tensor
"""
"""Print a 2D tensor"""
logger
.
info
(
"lv, h >
\t
"
+
"
\t
"
.
join
(
f
"
{
x
+
1
}
"
for
x
in
range
(
len
(
tensor
))))
for
row
in
range
(
len
(
tensor
)):
if
tensor
.
dtype
!=
torch
.
long
:
...
...
examples/research_projects/deebert/run_glue_deebert.py
View file @
32dbb2d9
...
...
@@ -70,7 +70,7 @@ def get_wanted_result(result):
def
train
(
args
,
train_dataset
,
model
,
tokenizer
,
train_highway
=
False
):
"""
Train the model
"""
"""Train the model"""
if
args
.
local_rank
in
[
-
1
,
0
]:
tb_writer
=
SummaryWriter
()
...
...
examples/research_projects/distillation/run_squad_w_distillation.py
View file @
32dbb2d9
...
...
@@ -92,7 +92,7 @@ def to_list(tensor):
def
train
(
args
,
train_dataset
,
model
,
tokenizer
,
teacher
=
None
):
"""
Train the model
"""
"""Train the model"""
if
args
.
local_rank
in
[
-
1
,
0
]:
tb_writer
=
SummaryWriter
()
...
...
examples/research_projects/mm-imdb/run_mmimdb.py
View file @
32dbb2d9
...
...
@@ -64,7 +64,7 @@ def set_seed(args):
def
train
(
args
,
train_dataset
,
model
,
tokenizer
,
criterion
):
"""
Train the model
"""
"""Train the model"""
if
args
.
local_rank
in
[
-
1
,
0
]:
tb_writer
=
SummaryWriter
()
...
...
examples/research_projects/movement-pruning/emmental/modeling_bert_masked.py
View file @
32dbb2d9
...
...
@@ -393,7 +393,7 @@ class MaskedBertPreTrainedModel(PreTrainedModel):
base_model_prefix
=
"bert"
def
_init_weights
(
self
,
module
):
"""
Initialize the weights
"""
"""Initialize the weights"""
if
isinstance
(
module
,
(
nn
.
Linear
,
nn
.
Embedding
)):
# Slightly different from the TF version which uses truncated_normal for initialization
# cf https://github.com/pytorch/pytorch/pull/5617
...
...
examples/research_projects/movement-pruning/masked_run_glue.py
View file @
32dbb2d9
...
...
@@ -105,7 +105,7 @@ def regularization(model: nn.Module, mode: str):
def
train
(
args
,
train_dataset
,
model
,
tokenizer
,
teacher
=
None
):
"""
Train the model
"""
"""Train the model"""
if
args
.
local_rank
in
[
-
1
,
0
]:
tb_writer
=
SummaryWriter
(
log_dir
=
args
.
output_dir
)
...
...
examples/research_projects/movement-pruning/masked_run_squad.py
View file @
32dbb2d9
...
...
@@ -113,7 +113,7 @@ def to_list(tensor):
def
train
(
args
,
train_dataset
,
model
,
tokenizer
,
teacher
=
None
):
"""
Train the model
"""
"""Train the model"""
if
args
.
local_rank
in
[
-
1
,
0
]:
tb_writer
=
SummaryWriter
(
log_dir
=
args
.
output_dir
)
...
...
src/transformers/commands/lfs.py
View file @
32dbb2d9
...
...
@@ -96,7 +96,7 @@ def write_msg(msg: Dict):
def
read_msg
()
->
Optional
[
Dict
]:
"""Read Line delimited JSON from stdin.
"""
"""Read Line delimited JSON from stdin."""
msg
=
json
.
loads
(
sys
.
stdin
.
readline
().
strip
())
if
"terminate"
in
(
msg
.
get
(
"type"
),
msg
.
get
(
"event"
)):
...
...
src/transformers/data/processors/utils.py
View file @
32dbb2d9
...
...
@@ -124,7 +124,7 @@ class DataProcessor:
class
SingleSentenceClassificationProcessor
(
DataProcessor
):
"""
Generic processor for a single sentence classification data set."""
"""Generic processor for a single sentence classification data set."""
def
__init__
(
self
,
labels
=
None
,
examples
=
None
,
mode
=
"classification"
,
verbose
=
False
):
self
.
labels
=
[]
if
labels
is
None
else
labels
...
...
src/transformers/file_utils.py
View file @
32dbb2d9
...
...
@@ -1462,7 +1462,7 @@ def tf_required(func):
def
is_tensor
(
x
):
"""
Tests if ``x`` is a :obj:`torch.Tensor`, :obj:`tf.Tensor` or :obj:`np.ndarray`.
"""
"""Tests if ``x`` is a :obj:`torch.Tensor`, :obj:`tf.Tensor` or :obj:`np.ndarray`."""
if
is_torch_available
():
import
torch
...
...
@@ -1684,7 +1684,7 @@ class _BaseLazyModule(ModuleType):
def
copy_func
(
f
):
"""
Returns a copy of a function f."""
"""Returns a copy of a function f."""
# Based on http://stackoverflow.com/a/6528148/190597 (Glenn Maynard)
g
=
types
.
FunctionType
(
f
.
__code__
,
f
.
__globals__
,
name
=
f
.
__name__
,
argdefs
=
f
.
__defaults__
,
closure
=
f
.
__closure__
)
g
=
functools
.
update_wrapper
(
g
,
f
)
...
...
src/transformers/modelcard.py
View file @
32dbb2d9
...
...
@@ -215,6 +215,6 @@ class ModelCard:
return
json
.
dumps
(
self
.
to_dict
(),
indent
=
2
,
sort_keys
=
True
)
+
"
\n
"
def
to_json_file
(
self
,
json_file_path
):
"""
Save this instance to a json file."""
"""Save this instance to a json file."""
with
open
(
json_file_path
,
"w"
,
encoding
=
"utf-8"
)
as
writer
:
writer
.
write
(
self
.
to_json_string
())
Prev
1
2
3
4
5
6
Next
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