Update quality tooling for formatting (#21480)
* Result of black 23.1 * Update target to Python 3.7 * Switch flake8 to ruff * Configure isort * Configure isort * Apply isort with line limit * Put the right black version * adapt black in check copies * Fix copies
This commit is contained in:
@@ -72,7 +72,7 @@ class EncoderDecoderMixin:
|
||||
decoder_config,
|
||||
decoder_input_ids,
|
||||
decoder_attention_mask,
|
||||
**kwargs
|
||||
**kwargs,
|
||||
):
|
||||
encoder_decoder_config = EncoderDecoderConfig.from_encoder_decoder_configs(config, decoder_config)
|
||||
self.assertTrue(encoder_decoder_config.decoder.is_decoder)
|
||||
@@ -106,7 +106,7 @@ class EncoderDecoderMixin:
|
||||
decoder_config,
|
||||
decoder_input_ids,
|
||||
decoder_attention_mask,
|
||||
**kwargs
|
||||
**kwargs,
|
||||
):
|
||||
encoder_model, decoder_model = self.get_encoder_decoder_model(config, decoder_config)
|
||||
enc_dec_model = EncoderDecoderModel(encoder=encoder_model, decoder=decoder_model)
|
||||
@@ -167,7 +167,7 @@ class EncoderDecoderMixin:
|
||||
decoder_config,
|
||||
decoder_input_ids,
|
||||
decoder_attention_mask,
|
||||
**kwargs
|
||||
**kwargs,
|
||||
):
|
||||
encoder_model, decoder_model = self.get_encoder_decoder_model(config, decoder_config)
|
||||
with tempfile.TemporaryDirectory() as encoder_tmp_dirname, tempfile.TemporaryDirectory() as decoder_tmp_dirname:
|
||||
@@ -210,7 +210,7 @@ class EncoderDecoderMixin:
|
||||
decoder_input_ids,
|
||||
decoder_attention_mask,
|
||||
return_dict,
|
||||
**kwargs
|
||||
**kwargs,
|
||||
):
|
||||
encoder_model, decoder_model = self.get_encoder_decoder_model(config, decoder_config)
|
||||
kwargs = {"encoder_model": encoder_model, "decoder_model": decoder_model, "return_dict": return_dict}
|
||||
@@ -240,7 +240,7 @@ class EncoderDecoderMixin:
|
||||
decoder_config,
|
||||
decoder_input_ids,
|
||||
decoder_attention_mask,
|
||||
**kwargs
|
||||
**kwargs,
|
||||
):
|
||||
encoder_model, decoder_model = self.get_encoder_decoder_model(config, decoder_config)
|
||||
enc_dec_model = EncoderDecoderModel(encoder=encoder_model, decoder=decoder_model)
|
||||
@@ -281,7 +281,7 @@ class EncoderDecoderMixin:
|
||||
decoder_config,
|
||||
decoder_input_ids,
|
||||
decoder_attention_mask,
|
||||
**kwargs
|
||||
**kwargs,
|
||||
):
|
||||
encoder_model, decoder_model = self.get_encoder_decoder_model(config, decoder_config)
|
||||
enc_dec_model = EncoderDecoderModel(encoder=encoder_model, decoder=decoder_model)
|
||||
@@ -327,7 +327,7 @@ class EncoderDecoderMixin:
|
||||
decoder_input_ids,
|
||||
decoder_attention_mask,
|
||||
labels,
|
||||
**kwargs
|
||||
**kwargs,
|
||||
):
|
||||
encoder_model, decoder_model = self.get_encoder_decoder_model(config, decoder_config)
|
||||
enc_dec_model = EncoderDecoderModel(encoder=encoder_model, decoder=decoder_model)
|
||||
@@ -395,7 +395,7 @@ class EncoderDecoderMixin:
|
||||
decoder_input_ids,
|
||||
decoder_attention_mask,
|
||||
labels,
|
||||
**kwargs
|
||||
**kwargs,
|
||||
):
|
||||
# make the decoder inputs a different shape from the encoder inputs to harden the test
|
||||
decoder_input_ids = decoder_input_ids[:, :-1]
|
||||
@@ -424,7 +424,7 @@ class EncoderDecoderMixin:
|
||||
decoder_input_ids,
|
||||
decoder_attention_mask,
|
||||
labels,
|
||||
**kwargs
|
||||
**kwargs,
|
||||
):
|
||||
# Similar to `check_encoder_decoder_model_output_attentions`, but with `output_attentions` triggered from the
|
||||
# config file. Contrarily to most models, changing the model's config won't work -- the defaults are loaded
|
||||
@@ -491,7 +491,7 @@ class EncoderDecoderMixin:
|
||||
decoder_input_ids,
|
||||
decoder_attention_mask,
|
||||
labels,
|
||||
**kwargs
|
||||
**kwargs,
|
||||
):
|
||||
torch.manual_seed(0)
|
||||
encoder_model, decoder_model = self.get_encoder_decoder_model(config, decoder_config)
|
||||
|
||||
@@ -69,7 +69,7 @@ class FlaxEncoderDecoderMixin:
|
||||
decoder_config,
|
||||
decoder_input_ids,
|
||||
decoder_attention_mask,
|
||||
**kwargs
|
||||
**kwargs,
|
||||
):
|
||||
encoder_decoder_config = EncoderDecoderConfig.from_encoder_decoder_configs(config, decoder_config)
|
||||
self.assertTrue(encoder_decoder_config.decoder.is_decoder)
|
||||
@@ -102,7 +102,7 @@ class FlaxEncoderDecoderMixin:
|
||||
decoder_input_ids,
|
||||
decoder_attention_mask,
|
||||
return_dict,
|
||||
**kwargs
|
||||
**kwargs,
|
||||
):
|
||||
encoder_model, decoder_model = self.get_encoder_decoder_model(config, decoder_config)
|
||||
kwargs = {"encoder_model": encoder_model, "decoder_model": decoder_model, "return_dict": return_dict}
|
||||
@@ -131,7 +131,7 @@ class FlaxEncoderDecoderMixin:
|
||||
decoder_config,
|
||||
decoder_input_ids,
|
||||
decoder_attention_mask,
|
||||
**kwargs
|
||||
**kwargs,
|
||||
):
|
||||
encoder_model, decoder_model = self.get_encoder_decoder_model(config, decoder_config)
|
||||
kwargs = {"encoder_model": encoder_model, "decoder_model": decoder_model}
|
||||
@@ -170,7 +170,7 @@ class FlaxEncoderDecoderMixin:
|
||||
decoder_config,
|
||||
decoder_input_ids,
|
||||
decoder_attention_mask,
|
||||
**kwargs
|
||||
**kwargs,
|
||||
):
|
||||
encoder_model, decoder_model = self.get_encoder_decoder_model(config, decoder_config)
|
||||
# assert that model attributes match those of configs
|
||||
@@ -215,7 +215,7 @@ class FlaxEncoderDecoderMixin:
|
||||
decoder_config,
|
||||
decoder_input_ids,
|
||||
decoder_attention_mask,
|
||||
**kwargs
|
||||
**kwargs,
|
||||
):
|
||||
# make the decoder inputs a different shape from the encoder inputs to harden the test
|
||||
decoder_input_ids = decoder_input_ids[:, :-1]
|
||||
@@ -292,7 +292,6 @@ class FlaxEncoderDecoderMixin:
|
||||
self.assertEqual(generated_sequences.shape, (input_ids.shape[0],) + (decoder_config.max_length,))
|
||||
|
||||
def check_pt_flax_equivalence(self, pt_model, fx_model, inputs_dict):
|
||||
|
||||
pt_model.to(torch_device)
|
||||
pt_model.eval()
|
||||
|
||||
@@ -334,7 +333,6 @@ class FlaxEncoderDecoderMixin:
|
||||
self.assert_almost_equals(fx_output, pt_output_loaded.numpy(), 1e-5)
|
||||
|
||||
def check_equivalence_pt_to_flax(self, config, decoder_config, inputs_dict):
|
||||
|
||||
encoder_decoder_config = EncoderDecoderConfig.from_encoder_decoder_configs(config, decoder_config)
|
||||
|
||||
pt_model = EncoderDecoderModel(encoder_decoder_config)
|
||||
@@ -346,7 +344,6 @@ class FlaxEncoderDecoderMixin:
|
||||
self.check_pt_flax_equivalence(pt_model, fx_model, inputs_dict)
|
||||
|
||||
def check_equivalence_flax_to_pt(self, config, decoder_config, inputs_dict):
|
||||
|
||||
encoder_decoder_config = EncoderDecoderConfig.from_encoder_decoder_configs(config, decoder_config)
|
||||
|
||||
pt_model = EncoderDecoderModel(encoder_decoder_config)
|
||||
@@ -390,7 +387,6 @@ class FlaxEncoderDecoderMixin:
|
||||
|
||||
@is_pt_flax_cross_test
|
||||
def test_pt_flax_equivalence(self):
|
||||
|
||||
config_inputs_dict = self.prepare_config_and_inputs()
|
||||
config = config_inputs_dict.pop("config")
|
||||
decoder_config = config_inputs_dict.pop("decoder_config")
|
||||
@@ -589,7 +585,6 @@ class FlaxEncoderDecoderModelTest(unittest.TestCase):
|
||||
return FlaxEncoderDecoderModel.from_encoder_decoder_pretrained("bert-base-cased", "gpt2")
|
||||
|
||||
def _check_configuration_tie(self, model):
|
||||
|
||||
module = model.module.bind(model.params)
|
||||
|
||||
assert id(module.decoder.config) == id(model.config.decoder)
|
||||
|
||||
@@ -78,7 +78,7 @@ class TFEncoderDecoderMixin:
|
||||
decoder_config,
|
||||
decoder_input_ids,
|
||||
decoder_attention_mask,
|
||||
**kwargs
|
||||
**kwargs,
|
||||
):
|
||||
encoder_decoder_config = EncoderDecoderConfig.from_encoder_decoder_configs(config, decoder_config)
|
||||
self.assertTrue(encoder_decoder_config.decoder.is_decoder)
|
||||
@@ -111,7 +111,7 @@ class TFEncoderDecoderMixin:
|
||||
decoder_config,
|
||||
decoder_input_ids,
|
||||
decoder_attention_mask,
|
||||
**kwargs
|
||||
**kwargs,
|
||||
):
|
||||
encoder_model, decoder_model = self.get_encoder_decoder_model(config, decoder_config)
|
||||
enc_dec_model = TFEncoderDecoderModel(encoder=encoder_model, decoder=decoder_model)
|
||||
@@ -160,7 +160,7 @@ class TFEncoderDecoderMixin:
|
||||
decoder_input_ids,
|
||||
decoder_attention_mask,
|
||||
return_dict,
|
||||
**kwargs
|
||||
**kwargs,
|
||||
):
|
||||
encoder_model, decoder_model = self.get_encoder_decoder_model(config, decoder_config)
|
||||
kwargs = {"encoder_model": encoder_model, "decoder_model": decoder_model, "return_dict": return_dict}
|
||||
@@ -190,7 +190,7 @@ class TFEncoderDecoderMixin:
|
||||
decoder_config,
|
||||
decoder_input_ids,
|
||||
decoder_attention_mask,
|
||||
**kwargs
|
||||
**kwargs,
|
||||
):
|
||||
encoder_model, decoder_model = self.get_encoder_decoder_model(config, decoder_config)
|
||||
enc_dec_model = TFEncoderDecoderModel(encoder=encoder_model, decoder=decoder_model)
|
||||
@@ -231,7 +231,7 @@ class TFEncoderDecoderMixin:
|
||||
decoder_input_ids,
|
||||
decoder_attention_mask,
|
||||
labels,
|
||||
**kwargs
|
||||
**kwargs,
|
||||
):
|
||||
encoder_model, decoder_model = self.get_encoder_decoder_model(config, decoder_config)
|
||||
enc_dec_model = TFEncoderDecoderModel(encoder=encoder_model, decoder=decoder_model)
|
||||
@@ -298,7 +298,7 @@ class TFEncoderDecoderMixin:
|
||||
decoder_config,
|
||||
decoder_input_ids,
|
||||
decoder_attention_mask,
|
||||
**kwargs
|
||||
**kwargs,
|
||||
):
|
||||
# make the decoder inputs a different shape from the encoder inputs to harden the test
|
||||
decoder_input_ids = decoder_input_ids[:, :-1]
|
||||
@@ -326,7 +326,7 @@ class TFEncoderDecoderMixin:
|
||||
decoder_config,
|
||||
decoder_input_ids,
|
||||
decoder_attention_mask,
|
||||
**kwargs
|
||||
**kwargs,
|
||||
):
|
||||
# Similar to `check_encoder_decoder_model_output_attentions`, but with `output_attentions` triggered from the
|
||||
# config file. Contrarily to most models, changing the model's config won't work -- the defaults are loaded
|
||||
@@ -470,7 +470,6 @@ class TFEncoderDecoderMixin:
|
||||
)
|
||||
|
||||
def prepare_pt_inputs_from_tf_inputs(self, tf_inputs_dict):
|
||||
|
||||
pt_inputs_dict = {}
|
||||
for name, key in tf_inputs_dict.items():
|
||||
if type(key) == bool:
|
||||
@@ -490,7 +489,6 @@ class TFEncoderDecoderMixin:
|
||||
return pt_inputs_dict
|
||||
|
||||
def check_pt_tf_models(self, tf_model, pt_model, tf_inputs_dict):
|
||||
|
||||
pt_inputs_dict = self.prepare_pt_inputs_from_tf_inputs(tf_inputs_dict)
|
||||
|
||||
# send pytorch inputs to the correct device
|
||||
@@ -607,7 +605,6 @@ class TFEncoderDecoderMixin:
|
||||
|
||||
@is_pt_tf_cross_test
|
||||
def test_pt_tf_model_equivalence(self):
|
||||
|
||||
config_inputs_dict = self.prepare_config_and_inputs()
|
||||
labels = config_inputs_dict.pop("decoder_token_labels")
|
||||
|
||||
@@ -762,7 +759,6 @@ class TFBertEncoderDecoderModelTest(TFEncoderDecoderMixin, unittest.TestCase):
|
||||
@slow
|
||||
@is_pt_tf_cross_test
|
||||
def test_bert2bert_summarization(self):
|
||||
|
||||
from transformers import EncoderDecoderModel
|
||||
|
||||
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
||||
@@ -863,7 +859,6 @@ class TFGPT2EncoderDecoderModelTest(TFEncoderDecoderMixin, unittest.TestCase):
|
||||
@slow
|
||||
@is_pt_tf_cross_test
|
||||
def test_bert2gpt2_summarization(self):
|
||||
|
||||
from transformers import EncoderDecoderModel
|
||||
|
||||
tokenizer_in = AutoTokenizer.from_pretrained("bert-base-cased")
|
||||
@@ -1171,7 +1166,6 @@ class TFEncoderDecoderModelSaveLoadTests(unittest.TestCase):
|
||||
decoder_input_ids = decoder_tokenizer("Linda Davis", return_tensors="tf").input_ids
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp_dirname:
|
||||
|
||||
# Since most of HF's models don't have pretrained cross-attention layers, they are randomly
|
||||
# initialized even if we create models using `from_pretrained` method.
|
||||
# For the tests, the decoder need to be a model with pretrained cross-attention layers.
|
||||
|
||||
Reference in New Issue
Block a user