Skip to content

Commit

Permalink
make style (huggingface#11442)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickvonplaten authored Apr 26, 2021
1 parent 04ab2ca commit 32dbb2d
Show file tree
Hide file tree
Showing 105 changed files with 202 additions and 202 deletions.
2 changes: 1 addition & 1 deletion examples/legacy/question-answering/run_squad.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions examples/legacy/run_openai_gpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion examples/legacy/run_swag.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion examples/legacy/seq2seq/minify_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
14 changes: 7 additions & 7 deletions examples/research_projects/bertabs/modeling_bertabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def forward(
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)
Expand Down Expand Up @@ -479,11 +479,11 @@ def forward(
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.
Expand Down Expand Up @@ -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]
Expand All @@ -592,7 +592,7 @@ def map_batch_fn(self, fn):


class TransformerDecoderState(DecoderState):
""" Transformer Decoder state base class """
"""Transformer Decoder state base class"""

def __init__(self, src):
"""
Expand Down Expand Up @@ -638,7 +638,7 @@ def _init_cache(self, memory_bank, num_layers):
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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ def setUp(self):
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)
Expand Down
2 changes: 1 addition & 1 deletion examples/research_projects/bertabs/utils_summarization.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(self, path="", prefix="train"):
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):
Expand Down
4 changes: 2 additions & 2 deletions examples/research_projects/bertology/run_bertology.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@


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:
Expand Down
4 changes: 2 additions & 2 deletions examples/research_projects/bertology/run_prune_gpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion examples/research_projects/deebert/run_glue_deebert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion examples/research_projects/mm-imdb/run_mmimdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/transformers/commands/lfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")):
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/data/processors/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def _read_tsv(cls, input_file, quotechar=None):


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
Expand Down
4 changes: 2 additions & 2 deletions src/transformers/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@ def wrapper(*args, **kwargs):


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

Expand Down Expand Up @@ -1684,7 +1684,7 @@ def _get_module(self, module_name: str) -> 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)
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/modelcard.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,6 @@ def to_json_string(self):
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())
2 changes: 1 addition & 1 deletion src/transformers/models/albert/modeling_albert.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@


def load_tf_weights_in_albert(model, config, tf_checkpoint_path):
""" Load tf checkpoints in a pytorch model."""
"""Load tf checkpoints in a pytorch model."""
try:
import re

Expand Down
2 changes: 1 addition & 1 deletion src/transformers/models/albert/modeling_tf_albert.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def call(


class TFAlbertAttention(tf.keras.layers.Layer):
""" Contains the complete attention sublayer, including both dropouts and layer norm. """
"""Contains the complete attention sublayer, including both dropouts and layer norm."""

def __init__(self, config: AlbertConfig, **kwargs):
super().__init__(**kwargs)
Expand Down
4 changes: 2 additions & 2 deletions src/transformers/models/albert/tokenization_albert.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def preprocess_text(self, inputs):
return outputs

def _tokenize(self, text, sample=False):
""" Tokenize a string. """
"""Tokenize a string."""
text = self.preprocess_text(text)

if not sample:
Expand All @@ -211,7 +211,7 @@ def _tokenize(self, text, sample=False):
return new_pieces

def _convert_token_to_id(self, token):
""" Converts a token (str) in an id using the vocab. """
"""Converts a token (str) in an id using the vocab."""
return self.sp_model.PieceToId(token)

def _convert_id_to_token(self, index):
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/models/barthez/tokenization_barthez.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def _tokenize(self, text):
return self.sp_model.EncodeAsPieces(text)

def _convert_token_to_id(self, token):
""" Converts a token (str) in an id using the vocab. """
"""Converts a token (str) in an id using the vocab."""
if token in self.fairseq_tokens_to_ids:
return self.fairseq_tokens_to_ids[token]
spm_id = self.sp_model.PieceToId(token)
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/models/bert/modeling_bert.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ class BertPreTrainedModel(PreTrainedModel):
_keys_to_ignore_on_load_missing = [r"position_ids"]

def _init_weights(self, module):
""" Initialize the weights """
"""Initialize the weights"""
if isinstance(module, nn.Linear):
# Slightly different from the TF version which uses truncated_normal for initialization
# cf https://github.com/pytorch/pytorch/pull/5617
Expand Down
4 changes: 2 additions & 2 deletions src/transformers/models/bert/tokenization_bert.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,15 @@ def _tokenize(self, text):
return split_tokens

def _convert_token_to_id(self, token):
""" Converts a token (str) in an id using the vocab. """
"""Converts a token (str) in an id using the vocab."""
return self.vocab.get(token, self.vocab.get(self.unk_token))

def _convert_id_to_token(self, index):
"""Converts an index (integer) in a token (str) using the vocab."""
return self.ids_to_tokens.get(index, self.unk_token)

def convert_tokens_to_string(self, tokens):
""" Converts a sequence of tokens (string) in a single string. """
"""Converts a sequence of tokens (string) in a single string."""
out_string = " ".join(tokens).replace(" ##", "").strip()
return out_string

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class BertGenerationPreTrainedModel(PreTrainedModel):
_keys_to_ignore_on_load_missing = [r"position_ids"]

def _init_weights(self, module):
""" Initialize the weights """
"""Initialize the weights"""
if isinstance(module, nn.Linear):
# Slightly different from the TF version which uses truncated_normal for initialization
# cf https://github.com/pytorch/pytorch/pull/5617
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def _tokenize(self, text, sample=False):
return pieces

def _convert_token_to_id(self, token):
""" Converts a token (str) in an id using the vocab. """
"""Converts a token (str) in an id using the vocab."""
return self.sp_model.piece_to_id(token)

def _convert_id_to_token(self, index):
Expand All @@ -128,7 +128,7 @@ def _convert_id_to_token(self, index):
return token

def convert_tokens_to_string(self, tokens):
""" Converts a sequence of tokens (string) in a single string. """
"""Converts a sequence of tokens (string) in a single string."""
out_string = self.sp_model.decode_pieces(tokens)
return out_string

Expand Down
4 changes: 2 additions & 2 deletions src/transformers/models/bertweet/tokenization_bertweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,15 +368,15 @@ def normalizeToken(self, token):
return token

def _convert_token_to_id(self, token):
""" Converts a token (str) in an id using the vocab. """
"""Converts a token (str) in an id using the vocab."""
return self.encoder.get(token, self.encoder.get(self.unk_token))

def _convert_id_to_token(self, index):
"""Converts an index (integer) in a token (str) using the vocab."""
return self.decoder.get(index, self.unk_token)

def convert_tokens_to_string(self, tokens):
""" Converts a sequence of tokens (string) in a single string. """
"""Converts a sequence of tokens (string) in a single string."""
out_string = " ".join(tokens).replace("@@ ", "").strip()
return out_string

Expand Down
Loading

0 comments on commit 32dbb2d

Please sign in to comment.