"docs/vscode:/vscode.git/clone" did not exist on "09dc99517f5f38ee210cf1145a7b17fc99b37dac"
Unverified Commit acc3bd9d authored by Sylvain Gugger's avatar Sylvain Gugger Committed by GitHub
Browse files

Enforce string-formatting with f-strings (#10980)



* First third

* Styling and fix mistake

* Quality

* All the rest

* Treat %s and %d

* typo

* Missing )

* Apply suggestions from code review
Co-authored-by: default avatarLysandre Debut <lysandre@huggingface.co>
Co-authored-by: default avatarLysandre Debut <lysandre@huggingface.co>
parent d0b3797a
...@@ -798,10 +798,8 @@ class AutoModel: ...@@ -798,10 +798,8 @@ class AutoModel:
if type(config) in MODEL_MAPPING.keys(): if type(config) in MODEL_MAPPING.keys():
return MODEL_MAPPING[type(config)](config) return MODEL_MAPPING[type(config)](config)
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of AutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in MODEL_MAPPING.keys())}."
config.__class__, cls.__name__, ", ".join(c.__name__ for c in MODEL_MAPPING.keys())
)
) )
@classmethod @classmethod
...@@ -841,10 +839,8 @@ class AutoModel: ...@@ -841,10 +839,8 @@ class AutoModel:
pretrained_model_name_or_path, *model_args, config=config, **kwargs pretrained_model_name_or_path, *model_args, config=config, **kwargs
) )
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of AutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in MODEL_MAPPING.keys())}."
config.__class__, cls.__name__, ", ".join(c.__name__ for c in MODEL_MAPPING.keys())
)
) )
...@@ -893,10 +889,8 @@ class AutoModelForPreTraining: ...@@ -893,10 +889,8 @@ class AutoModelForPreTraining:
if type(config) in MODEL_FOR_PRETRAINING_MAPPING.keys(): if type(config) in MODEL_FOR_PRETRAINING_MAPPING.keys():
return MODEL_FOR_PRETRAINING_MAPPING[type(config)](config) return MODEL_FOR_PRETRAINING_MAPPING[type(config)](config)
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of AutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in MODEL_FOR_PRETRAINING_MAPPING.keys())}."
config.__class__, cls.__name__, ", ".join(c.__name__ for c in MODEL_FOR_PRETRAINING_MAPPING.keys())
)
) )
@classmethod @classmethod
...@@ -936,10 +930,8 @@ class AutoModelForPreTraining: ...@@ -936,10 +930,8 @@ class AutoModelForPreTraining:
pretrained_model_name_or_path, *model_args, config=config, **kwargs pretrained_model_name_or_path, *model_args, config=config, **kwargs
) )
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of AutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in MODEL_FOR_PRETRAINING_MAPPING.keys())}."
config.__class__, cls.__name__, ", ".join(c.__name__ for c in MODEL_FOR_PRETRAINING_MAPPING.keys())
)
) )
...@@ -999,10 +991,8 @@ class AutoModelWithLMHead: ...@@ -999,10 +991,8 @@ class AutoModelWithLMHead:
if type(config) in MODEL_WITH_LM_HEAD_MAPPING.keys(): if type(config) in MODEL_WITH_LM_HEAD_MAPPING.keys():
return MODEL_WITH_LM_HEAD_MAPPING[type(config)](config) return MODEL_WITH_LM_HEAD_MAPPING[type(config)](config)
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of AutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in MODEL_WITH_LM_HEAD_MAPPING.keys())}."
config.__class__, cls.__name__, ", ".join(c.__name__ for c in MODEL_WITH_LM_HEAD_MAPPING.keys())
)
) )
@classmethod @classmethod
...@@ -1048,10 +1038,8 @@ class AutoModelWithLMHead: ...@@ -1048,10 +1038,8 @@ class AutoModelWithLMHead:
pretrained_model_name_or_path, *model_args, config=config, **kwargs pretrained_model_name_or_path, *model_args, config=config, **kwargs
) )
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of AutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in MODEL_WITH_LM_HEAD_MAPPING.keys())}."
config.__class__, cls.__name__, ", ".join(c.__name__ for c in MODEL_WITH_LM_HEAD_MAPPING.keys())
)
) )
...@@ -1099,10 +1087,8 @@ class AutoModelForCausalLM: ...@@ -1099,10 +1087,8 @@ class AutoModelForCausalLM:
if type(config) in MODEL_FOR_CAUSAL_LM_MAPPING.keys(): if type(config) in MODEL_FOR_CAUSAL_LM_MAPPING.keys():
return MODEL_FOR_CAUSAL_LM_MAPPING[type(config)](config) return MODEL_FOR_CAUSAL_LM_MAPPING[type(config)](config)
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of AutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in MODEL_FOR_CAUSAL_LM_MAPPING.keys())}."
config.__class__, cls.__name__, ", ".join(c.__name__ for c in MODEL_FOR_CAUSAL_LM_MAPPING.keys())
)
) )
@classmethod @classmethod
...@@ -1142,10 +1128,8 @@ class AutoModelForCausalLM: ...@@ -1142,10 +1128,8 @@ class AutoModelForCausalLM:
pretrained_model_name_or_path, *model_args, config=config, **kwargs pretrained_model_name_or_path, *model_args, config=config, **kwargs
) )
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of AutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in MODEL_FOR_CAUSAL_LM_MAPPING.keys())}."
config.__class__, cls.__name__, ", ".join(c.__name__ for c in MODEL_FOR_CAUSAL_LM_MAPPING.keys())
)
) )
...@@ -1193,10 +1177,8 @@ class AutoModelForMaskedLM: ...@@ -1193,10 +1177,8 @@ class AutoModelForMaskedLM:
if type(config) in MODEL_FOR_MASKED_LM_MAPPING.keys(): if type(config) in MODEL_FOR_MASKED_LM_MAPPING.keys():
return MODEL_FOR_MASKED_LM_MAPPING[type(config)](config) return MODEL_FOR_MASKED_LM_MAPPING[type(config)](config)
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of AutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in MODEL_FOR_MASKED_LM_MAPPING.keys())}."
config.__class__, cls.__name__, ", ".join(c.__name__ for c in MODEL_FOR_MASKED_LM_MAPPING.keys())
)
) )
@classmethod @classmethod
...@@ -1236,10 +1218,8 @@ class AutoModelForMaskedLM: ...@@ -1236,10 +1218,8 @@ class AutoModelForMaskedLM:
pretrained_model_name_or_path, *model_args, config=config, **kwargs pretrained_model_name_or_path, *model_args, config=config, **kwargs
) )
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of AutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in MODEL_FOR_MASKED_LM_MAPPING.keys())}."
config.__class__, cls.__name__, ", ".join(c.__name__ for c in MODEL_FOR_MASKED_LM_MAPPING.keys())
)
) )
...@@ -1288,12 +1268,8 @@ class AutoModelForSeq2SeqLM: ...@@ -1288,12 +1268,8 @@ class AutoModelForSeq2SeqLM:
if type(config) in MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING.keys(): if type(config) in MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING.keys():
return MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING[type(config)](config) return MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING[type(config)](config)
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of AutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING.keys()),
)
) )
@classmethod @classmethod
...@@ -1333,12 +1309,8 @@ class AutoModelForSeq2SeqLM: ...@@ -1333,12 +1309,8 @@ class AutoModelForSeq2SeqLM:
pretrained_model_name_or_path, *model_args, config=config, **kwargs pretrained_model_name_or_path, *model_args, config=config, **kwargs
) )
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of AutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING.keys()),
)
) )
...@@ -1387,12 +1359,8 @@ class AutoModelForSequenceClassification: ...@@ -1387,12 +1359,8 @@ class AutoModelForSequenceClassification:
if type(config) in MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING.keys(): if type(config) in MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING.keys():
return MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING[type(config)](config) return MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING[type(config)](config)
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of AutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING.keys()),
)
) )
@classmethod @classmethod
...@@ -1432,12 +1400,8 @@ class AutoModelForSequenceClassification: ...@@ -1432,12 +1400,8 @@ class AutoModelForSequenceClassification:
pretrained_model_name_or_path, *model_args, config=config, **kwargs pretrained_model_name_or_path, *model_args, config=config, **kwargs
) )
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of AutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING.keys()),
)
) )
...@@ -1485,12 +1449,8 @@ class AutoModelForQuestionAnswering: ...@@ -1485,12 +1449,8 @@ class AutoModelForQuestionAnswering:
return MODEL_FOR_QUESTION_ANSWERING_MAPPING[type(config)](config) return MODEL_FOR_QUESTION_ANSWERING_MAPPING[type(config)](config)
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of AutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in MODEL_FOR_QUESTION_ANSWERING_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in MODEL_FOR_QUESTION_ANSWERING_MAPPING.keys()),
)
) )
@classmethod @classmethod
...@@ -1531,12 +1491,8 @@ class AutoModelForQuestionAnswering: ...@@ -1531,12 +1491,8 @@ class AutoModelForQuestionAnswering:
) )
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of AutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in MODEL_FOR_QUESTION_ANSWERING_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in MODEL_FOR_QUESTION_ANSWERING_MAPPING.keys()),
)
) )
...@@ -1586,12 +1542,8 @@ class AutoModelForTableQuestionAnswering: ...@@ -1586,12 +1542,8 @@ class AutoModelForTableQuestionAnswering:
return MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING[type(config)](config) return MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING[type(config)](config)
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of AutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING.keys()),
)
) )
@classmethod @classmethod
...@@ -1632,12 +1584,8 @@ class AutoModelForTableQuestionAnswering: ...@@ -1632,12 +1584,8 @@ class AutoModelForTableQuestionAnswering:
) )
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of AutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING.keys()),
)
) )
...@@ -1685,12 +1633,8 @@ class AutoModelForTokenClassification: ...@@ -1685,12 +1633,8 @@ class AutoModelForTokenClassification:
return MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING[type(config)](config) return MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING[type(config)](config)
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of AutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.keys()),
)
) )
@classmethod @classmethod
...@@ -1731,12 +1675,8 @@ class AutoModelForTokenClassification: ...@@ -1731,12 +1675,8 @@ class AutoModelForTokenClassification:
) )
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of AutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.keys()),
)
) )
...@@ -1786,12 +1726,8 @@ class AutoModelForMultipleChoice: ...@@ -1786,12 +1726,8 @@ class AutoModelForMultipleChoice:
return MODEL_FOR_MULTIPLE_CHOICE_MAPPING[type(config)](config) return MODEL_FOR_MULTIPLE_CHOICE_MAPPING[type(config)](config)
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of AutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in MODEL_FOR_MULTIPLE_CHOICE_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in MODEL_FOR_MULTIPLE_CHOICE_MAPPING.keys()),
)
) )
@classmethod @classmethod
...@@ -1832,12 +1768,8 @@ class AutoModelForMultipleChoice: ...@@ -1832,12 +1768,8 @@ class AutoModelForMultipleChoice:
) )
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of AutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in MODEL_FOR_MULTIPLE_CHOICE_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in MODEL_FOR_MULTIPLE_CHOICE_MAPPING.keys()),
)
) )
...@@ -1887,12 +1819,8 @@ class AutoModelForNextSentencePrediction: ...@@ -1887,12 +1819,8 @@ class AutoModelForNextSentencePrediction:
return MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING[type(config)](config) return MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING[type(config)](config)
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of AutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING.keys()),
)
) )
@classmethod @classmethod
...@@ -1933,10 +1861,6 @@ class AutoModelForNextSentencePrediction: ...@@ -1933,10 +1861,6 @@ class AutoModelForNextSentencePrediction:
) )
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of AutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING.keys()),
)
) )
...@@ -590,10 +590,8 @@ class TFAutoModel(object): ...@@ -590,10 +590,8 @@ class TFAutoModel(object):
if type(config) in TF_MODEL_MAPPING.keys(): if type(config) in TF_MODEL_MAPPING.keys():
return TF_MODEL_MAPPING[type(config)](config, **kwargs) return TF_MODEL_MAPPING[type(config)](config, **kwargs)
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of TFAutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of TFAutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in TF_MODEL_MAPPING.keys())}."
config.__class__, cls.__name__, ", ".join(c.__name__ for c in TF_MODEL_MAPPING.keys())
)
) )
@classmethod @classmethod
...@@ -633,10 +631,8 @@ class TFAutoModel(object): ...@@ -633,10 +631,8 @@ class TFAutoModel(object):
pretrained_model_name_or_path, *model_args, config=config, **kwargs pretrained_model_name_or_path, *model_args, config=config, **kwargs
) )
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of TFAutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of TFAutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in TF_MODEL_MAPPING.keys())}."
config.__class__, cls.__name__, ", ".join(c.__name__ for c in TF_MODEL_MAPPING.keys())
)
) )
...@@ -685,10 +681,8 @@ class TFAutoModelForPreTraining(object): ...@@ -685,10 +681,8 @@ class TFAutoModelForPreTraining(object):
if type(config) in TF_MODEL_FOR_PRETRAINING_MAPPING.keys(): if type(config) in TF_MODEL_FOR_PRETRAINING_MAPPING.keys():
return TF_MODEL_FOR_PRETRAINING_MAPPING[type(config)](config) return TF_MODEL_FOR_PRETRAINING_MAPPING[type(config)](config)
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of TFAutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of TFAutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in TF_MODEL_FOR_PRETRAINING_MAPPING.keys())}."
config.__class__, cls.__name__, ", ".join(c.__name__ for c in TF_MODEL_FOR_PRETRAINING_MAPPING.keys())
)
) )
@classmethod @classmethod
...@@ -728,10 +722,8 @@ class TFAutoModelForPreTraining(object): ...@@ -728,10 +722,8 @@ class TFAutoModelForPreTraining(object):
pretrained_model_name_or_path, *model_args, config=config, **kwargs pretrained_model_name_or_path, *model_args, config=config, **kwargs
) )
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of TFAutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of TFAutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in TF_MODEL_FOR_PRETRAINING_MAPPING.keys())}."
config.__class__, cls.__name__, ", ".join(c.__name__ for c in TF_MODEL_FOR_PRETRAINING_MAPPING.keys())
)
) )
...@@ -791,10 +783,8 @@ class TFAutoModelWithLMHead(object): ...@@ -791,10 +783,8 @@ class TFAutoModelWithLMHead(object):
if type(config) in TF_MODEL_WITH_LM_HEAD_MAPPING.keys(): if type(config) in TF_MODEL_WITH_LM_HEAD_MAPPING.keys():
return TF_MODEL_WITH_LM_HEAD_MAPPING[type(config)](config) return TF_MODEL_WITH_LM_HEAD_MAPPING[type(config)](config)
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of TFAutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of TFAutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in TF_MODEL_WITH_LM_HEAD_MAPPING.keys())}."
config.__class__, cls.__name__, ", ".join(c.__name__ for c in TF_MODEL_WITH_LM_HEAD_MAPPING.keys())
)
) )
@classmethod @classmethod
...@@ -840,10 +830,8 @@ class TFAutoModelWithLMHead(object): ...@@ -840,10 +830,8 @@ class TFAutoModelWithLMHead(object):
pretrained_model_name_or_path, *model_args, config=config, **kwargs pretrained_model_name_or_path, *model_args, config=config, **kwargs
) )
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of TFAutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of TFAutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in TF_MODEL_WITH_LM_HEAD_MAPPING.keys())}."
config.__class__, cls.__name__, ", ".join(c.__name__ for c in TF_MODEL_WITH_LM_HEAD_MAPPING.keys())
)
) )
...@@ -891,10 +879,8 @@ class TFAutoModelForCausalLM: ...@@ -891,10 +879,8 @@ class TFAutoModelForCausalLM:
if type(config) in TF_MODEL_FOR_CAUSAL_LM_MAPPING.keys(): if type(config) in TF_MODEL_FOR_CAUSAL_LM_MAPPING.keys():
return TF_MODEL_FOR_CAUSAL_LM_MAPPING[type(config)](config) return TF_MODEL_FOR_CAUSAL_LM_MAPPING[type(config)](config)
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of TFAutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of TFAutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in TF_MODEL_FOR_CAUSAL_LM_MAPPING.keys())}."
config.__class__, cls.__name__, ", ".join(c.__name__ for c in TF_MODEL_FOR_CAUSAL_LM_MAPPING.keys())
)
) )
@classmethod @classmethod
...@@ -934,10 +920,8 @@ class TFAutoModelForCausalLM: ...@@ -934,10 +920,8 @@ class TFAutoModelForCausalLM:
pretrained_model_name_or_path, *model_args, config=config, **kwargs pretrained_model_name_or_path, *model_args, config=config, **kwargs
) )
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of TFAutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of TFAutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in TF_MODEL_FOR_CAUSAL_LM_MAPPING.keys())}."
config.__class__, cls.__name__, ", ".join(c.__name__ for c in TF_MODEL_FOR_CAUSAL_LM_MAPPING.keys())
)
) )
...@@ -985,10 +969,8 @@ class TFAutoModelForMaskedLM: ...@@ -985,10 +969,8 @@ class TFAutoModelForMaskedLM:
if type(config) in TF_MODEL_FOR_MASKED_LM_MAPPING.keys(): if type(config) in TF_MODEL_FOR_MASKED_LM_MAPPING.keys():
return TF_MODEL_FOR_MASKED_LM_MAPPING[type(config)](config) return TF_MODEL_FOR_MASKED_LM_MAPPING[type(config)](config)
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of TFAutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of TFAutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in TF_MODEL_FOR_MASKED_LM_MAPPING.keys())}."
config.__class__, cls.__name__, ", ".join(c.__name__ for c in TF_MODEL_FOR_MASKED_LM_MAPPING.keys())
)
) )
@classmethod @classmethod
...@@ -1028,10 +1010,8 @@ class TFAutoModelForMaskedLM: ...@@ -1028,10 +1010,8 @@ class TFAutoModelForMaskedLM:
pretrained_model_name_or_path, *model_args, config=config, **kwargs pretrained_model_name_or_path, *model_args, config=config, **kwargs
) )
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of TFAutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of TFAutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in TF_MODEL_FOR_MASKED_LM_MAPPING.keys())}."
config.__class__, cls.__name__, ", ".join(c.__name__ for c in TF_MODEL_FOR_MASKED_LM_MAPPING.keys())
)
) )
...@@ -1080,12 +1060,8 @@ class TFAutoModelForSeq2SeqLM: ...@@ -1080,12 +1060,8 @@ class TFAutoModelForSeq2SeqLM:
if type(config) in TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING.keys(): if type(config) in TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING.keys():
return TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING[type(config)](config, **kwargs) return TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING[type(config)](config, **kwargs)
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of TFAutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of TFAutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING.keys()),
)
) )
@classmethod @classmethod
...@@ -1125,12 +1101,8 @@ class TFAutoModelForSeq2SeqLM: ...@@ -1125,12 +1101,8 @@ class TFAutoModelForSeq2SeqLM:
pretrained_model_name_or_path, *model_args, config=config, **kwargs pretrained_model_name_or_path, *model_args, config=config, **kwargs
) )
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of TFAutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of TFAutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING.keys()),
)
) )
...@@ -1179,12 +1151,8 @@ class TFAutoModelForSequenceClassification(object): ...@@ -1179,12 +1151,8 @@ class TFAutoModelForSequenceClassification(object):
if type(config) in TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING.keys(): if type(config) in TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING.keys():
return TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING[type(config)](config) return TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING[type(config)](config)
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of TFAutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of TFAutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING.keys()),
)
) )
@classmethod @classmethod
...@@ -1224,12 +1192,8 @@ class TFAutoModelForSequenceClassification(object): ...@@ -1224,12 +1192,8 @@ class TFAutoModelForSequenceClassification(object):
pretrained_model_name_or_path, *model_args, config=config, **kwargs pretrained_model_name_or_path, *model_args, config=config, **kwargs
) )
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of TFAutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of TFAutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING.keys()),
)
) )
...@@ -1277,12 +1241,8 @@ class TFAutoModelForQuestionAnswering(object): ...@@ -1277,12 +1241,8 @@ class TFAutoModelForQuestionAnswering(object):
if type(config) in TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING.keys(): if type(config) in TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING.keys():
return TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING[type(config)](config) return TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING[type(config)](config)
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of TFAutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of TFAutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING.keys()),
)
) )
@classmethod @classmethod
...@@ -1322,12 +1282,8 @@ class TFAutoModelForQuestionAnswering(object): ...@@ -1322,12 +1282,8 @@ class TFAutoModelForQuestionAnswering(object):
pretrained_model_name_or_path, *model_args, config=config, **kwargs pretrained_model_name_or_path, *model_args, config=config, **kwargs
) )
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of TFAutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of TFAutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING.keys()),
)
) )
...@@ -1374,12 +1330,8 @@ class TFAutoModelForTokenClassification: ...@@ -1374,12 +1330,8 @@ class TFAutoModelForTokenClassification:
if type(config) in TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.keys(): if type(config) in TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.keys():
return TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING[type(config)](config) return TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING[type(config)](config)
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of TFAutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of TFAutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.keys()),
)
) )
@classmethod @classmethod
...@@ -1419,12 +1371,8 @@ class TFAutoModelForTokenClassification: ...@@ -1419,12 +1371,8 @@ class TFAutoModelForTokenClassification:
pretrained_model_name_or_path, *model_args, config=config, **kwargs pretrained_model_name_or_path, *model_args, config=config, **kwargs
) )
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of TFAutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of TFAutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.keys()),
)
) )
...@@ -1473,12 +1421,8 @@ class TFAutoModelForMultipleChoice: ...@@ -1473,12 +1421,8 @@ class TFAutoModelForMultipleChoice:
if type(config) in TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING.keys(): if type(config) in TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING.keys():
return TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING[type(config)](config) return TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING[type(config)](config)
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of TFAutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of TFAutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING.keys()),
)
) )
@classmethod @classmethod
...@@ -1518,12 +1462,8 @@ class TFAutoModelForMultipleChoice: ...@@ -1518,12 +1462,8 @@ class TFAutoModelForMultipleChoice:
pretrained_model_name_or_path, *model_args, config=config, **kwargs pretrained_model_name_or_path, *model_args, config=config, **kwargs
) )
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of TFAutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of TFAutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING.keys()),
)
) )
...@@ -1572,12 +1512,8 @@ class TFAutoModelForNextSentencePrediction: ...@@ -1572,12 +1512,8 @@ class TFAutoModelForNextSentencePrediction:
if type(config) in TF_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING.keys(): if type(config) in TF_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING.keys():
return TF_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING[type(config)](config) return TF_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING[type(config)](config)
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of TFAutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of TFAutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in TF_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in TF_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING.keys()),
)
) )
@classmethod @classmethod
...@@ -1617,10 +1553,6 @@ class TFAutoModelForNextSentencePrediction: ...@@ -1617,10 +1553,6 @@ class TFAutoModelForNextSentencePrediction:
pretrained_model_name_or_path, *model_args, config=config, **kwargs pretrained_model_name_or_path, *model_args, config=config, **kwargs
) )
raise ValueError( raise ValueError(
"Unrecognized configuration class {} for this kind of TFAutoModel: {}.\n" f"Unrecognized configuration class {config.__class__} for this kind of TFAutoModel: {cls.__name__}.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in TF_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING.keys())}."
config.__class__,
cls.__name__,
", ".join(c.__name__ for c in TF_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING.keys()),
)
) )
...@@ -402,7 +402,7 @@ class AutoTokenizer: ...@@ -402,7 +402,7 @@ class AutoTokenizer:
if tokenizer_class is None: if tokenizer_class is None:
raise ValueError( raise ValueError(
"Tokenizer class {} does not exist or is not currently imported.".format(tokenizer_class_candidate) f"Tokenizer class {tokenizer_class_candidate} does not exist or is not currently imported."
) )
return tokenizer_class.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs) return tokenizer_class.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
...@@ -431,8 +431,6 @@ class AutoTokenizer: ...@@ -431,8 +431,6 @@ class AutoTokenizer:
) )
raise ValueError( raise ValueError(
"Unrecognized configuration class {} to build an AutoTokenizer.\n" f"Unrecognized configuration class {config.__class__} to build an AutoTokenizer.\n"
"Model type should be one of {}.".format( f"Model type should be one of {', '.join(c.__name__ for c in TOKENIZER_MAPPING.keys())}."
config.__class__, ", ".join(c.__name__ for c in TOKENIZER_MAPPING.keys())
)
) )
...@@ -256,7 +256,7 @@ class BarthezTokenizer(PreTrainedTokenizer): ...@@ -256,7 +256,7 @@ class BarthezTokenizer(PreTrainedTokenizer):
def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]:
if not os.path.isdir(save_directory): if not os.path.isdir(save_directory):
logger.error("Vocabulary path ({}) should be a directory".format(save_directory)) logger.error(f"Vocabulary path ({save_directory}) should be a directory")
return return
out_vocab_file = os.path.join( out_vocab_file = os.path.join(
save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
......
...@@ -218,7 +218,7 @@ class BarthezTokenizerFast(PreTrainedTokenizerFast): ...@@ -218,7 +218,7 @@ class BarthezTokenizerFast(PreTrainedTokenizerFast):
def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]:
if not os.path.isdir(save_directory): if not os.path.isdir(save_directory):
logger.error("Vocabulary path ({}) should be a directory".format(save_directory)) logger.error(f"Vocabulary path ({save_directory}) should be a directory")
return return
out_vocab_file = os.path.join( out_vocab_file = os.path.join(
save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
......
...@@ -38,14 +38,14 @@ logger = logging.get_logger(__name__) ...@@ -38,14 +38,14 @@ logger = logging.get_logger(__name__)
def load_tf2_weights_in_bert(model, tf_checkpoint_path, config): def load_tf2_weights_in_bert(model, tf_checkpoint_path, config):
tf_path = os.path.abspath(tf_checkpoint_path) tf_path = os.path.abspath(tf_checkpoint_path)
logger.info("Converting TensorFlow checkpoint from {}".format(tf_path)) logger.info(f"Converting TensorFlow checkpoint from {tf_path}")
# Load weights from TF model # Load weights from TF model
init_vars = tf.train.list_variables(tf_path) init_vars = tf.train.list_variables(tf_path)
names = [] names = []
arrays = [] arrays = []
layer_depth = [] layer_depth = []
for full_name, shape in init_vars: for full_name, shape in init_vars:
# logger.info("Loading TF weight {} with shape {}".format(name, shape)) # logger.info(f"Loading TF weight {name} with shape {shape}")
name = full_name.split("/") name = full_name.split("/")
if full_name == "_CHECKPOINTABLE_OBJECT_GRAPH" or name[0] in ["global_step", "save_counter"]: if full_name == "_CHECKPOINTABLE_OBJECT_GRAPH" or name[0] in ["global_step", "save_counter"]:
logger.info(f"Skipping non-model layer {full_name}") logger.info(f"Skipping non-model layer {full_name}")
......
...@@ -29,14 +29,14 @@ logging.set_verbosity_info() ...@@ -29,14 +29,14 @@ logging.set_verbosity_info()
def convert_tf_checkpoint_to_pytorch(tf_checkpoint_path, bert_config_file, pytorch_dump_path): def convert_tf_checkpoint_to_pytorch(tf_checkpoint_path, bert_config_file, pytorch_dump_path):
# Initialise PyTorch model # Initialise PyTorch model
config = BertConfig.from_json_file(bert_config_file) config = BertConfig.from_json_file(bert_config_file)
print("Building PyTorch model from configuration: {}".format(str(config))) print(f"Building PyTorch model from configuration: {config}")
model = BertForPreTraining(config) model = BertForPreTraining(config)
# Load weights from tf checkpoint # Load weights from tf checkpoint
load_tf_weights_in_bert(model, config, tf_checkpoint_path) load_tf_weights_in_bert(model, config, tf_checkpoint_path)
# Save pytorch-model # Save pytorch-model
print("Save PyTorch model to {}".format(pytorch_dump_path)) print(f"Save PyTorch model to {pytorch_dump_path}")
torch.save(model.state_dict(), pytorch_dump_path) torch.save(model.state_dict(), pytorch_dump_path)
......
...@@ -65,7 +65,7 @@ def convert_pytorch_checkpoint_to_tf(model: BertModel, ckpt_dir: str, model_name ...@@ -65,7 +65,7 @@ def convert_pytorch_checkpoint_to_tf(model: BertModel, ckpt_dir: str, model_name
def to_tf_var_name(name: str): def to_tf_var_name(name: str):
for patt, repl in iter(var_map): for patt, repl in iter(var_map):
name = name.replace(patt, repl) name = name.replace(patt, repl)
return "bert/{}".format(name) return f"bert/{name}"
def create_tf_var(tensor: np.ndarray, name: str, session: tf.Session): def create_tf_var(tensor: np.ndarray, name: str, session: tf.Session):
tf_dtype = tf.dtypes.as_dtype(tensor.dtype) tf_dtype = tf.dtypes.as_dtype(tensor.dtype)
...@@ -84,7 +84,7 @@ def convert_pytorch_checkpoint_to_tf(model: BertModel, ckpt_dir: str, model_name ...@@ -84,7 +84,7 @@ def convert_pytorch_checkpoint_to_tf(model: BertModel, ckpt_dir: str, model_name
tf_var = create_tf_var(tensor=torch_tensor, name=tf_name, session=session) tf_var = create_tf_var(tensor=torch_tensor, name=tf_name, session=session)
tf.keras.backend.set_value(tf_var, torch_tensor) tf.keras.backend.set_value(tf_var, torch_tensor)
tf_weight = session.run(tf_var) tf_weight = session.run(tf_var)
print("Successfully created {}: {}".format(tf_name, np.allclose(tf_weight, torch_tensor))) print(f"Successfully created {tf_name}: {np.allclose(tf_weight, torch_tensor)}")
saver = tf.train.Saver(tf.trainable_variables()) saver = tf.train.Saver(tf.trainable_variables())
saver.save(session, os.path.join(ckpt_dir, model_name.replace("-", "_") + ".ckpt")) saver.save(session, os.path.join(ckpt_dir, model_name.replace("-", "_") + ".ckpt"))
......
...@@ -103,13 +103,13 @@ def load_tf_weights_in_bert(model, config, tf_checkpoint_path): ...@@ -103,13 +103,13 @@ def load_tf_weights_in_bert(model, config, tf_checkpoint_path):
) )
raise raise
tf_path = os.path.abspath(tf_checkpoint_path) tf_path = os.path.abspath(tf_checkpoint_path)
logger.info("Converting TensorFlow checkpoint from {}".format(tf_path)) logger.info(f"Converting TensorFlow checkpoint from {tf_path}")
# Load weights from TF model # Load weights from TF model
init_vars = tf.train.list_variables(tf_path) init_vars = tf.train.list_variables(tf_path)
names = [] names = []
arrays = [] arrays = []
for name, shape in init_vars: for name, shape in init_vars:
logger.info("Loading TF weight {} with shape {}".format(name, shape)) logger.info(f"Loading TF weight {name} with shape {shape}")
array = tf.train.load_variable(tf_path, name) array = tf.train.load_variable(tf_path, name)
names.append(name) names.append(name)
arrays.append(array) arrays.append(array)
...@@ -122,7 +122,7 @@ def load_tf_weights_in_bert(model, config, tf_checkpoint_path): ...@@ -122,7 +122,7 @@ def load_tf_weights_in_bert(model, config, tf_checkpoint_path):
n in ["adam_v", "adam_m", "AdamWeightDecayOptimizer", "AdamWeightDecayOptimizer_1", "global_step"] n in ["adam_v", "adam_m", "AdamWeightDecayOptimizer", "AdamWeightDecayOptimizer_1", "global_step"]
for n in name for n in name
): ):
logger.info("Skipping {}".format("/".join(name))) logger.info(f"Skipping {'/'.join(name)}")
continue continue
pointer = model pointer = model
for m_name in name: for m_name in name:
...@@ -142,7 +142,7 @@ def load_tf_weights_in_bert(model, config, tf_checkpoint_path): ...@@ -142,7 +142,7 @@ def load_tf_weights_in_bert(model, config, tf_checkpoint_path):
try: try:
pointer = getattr(pointer, scope_names[0]) pointer = getattr(pointer, scope_names[0])
except AttributeError: except AttributeError:
logger.info("Skipping {}".format("/".join(name))) logger.info(f"Skipping {'/'.join(name)}")
continue continue
if len(scope_names) >= 2: if len(scope_names) >= 2:
num = int(scope_names[1]) num = int(scope_names[1])
...@@ -158,7 +158,7 @@ def load_tf_weights_in_bert(model, config, tf_checkpoint_path): ...@@ -158,7 +158,7 @@ def load_tf_weights_in_bert(model, config, tf_checkpoint_path):
except AssertionError as e: except AssertionError as e:
e.args += (pointer.shape, array.shape) e.args += (pointer.shape, array.shape)
raise raise
logger.info("Initialize PyTorch weight {}".format(name)) logger.info(f"Initialize PyTorch weight {name}")
pointer.data = torch.from_numpy(array) pointer.data = torch.from_numpy(array)
return model return model
...@@ -215,8 +215,8 @@ class BertSelfAttention(nn.Module): ...@@ -215,8 +215,8 @@ class BertSelfAttention(nn.Module):
super().__init__() super().__init__()
if config.hidden_size % config.num_attention_heads != 0 and not hasattr(config, "embedding_size"): if config.hidden_size % config.num_attention_heads != 0 and not hasattr(config, "embedding_size"):
raise ValueError( raise ValueError(
"The hidden size (%d) is not a multiple of the number of attention " f"The hidden size ({config.hidden_size}) is not a multiple of the number of attention "
"heads (%d)" % (config.hidden_size, config.num_attention_heads) f"heads ({config.num_attention_heads})"
) )
self.num_attention_heads = config.num_attention_heads self.num_attention_heads = config.num_attention_heads
......
...@@ -411,7 +411,7 @@ class TFBertEncoder(tf.keras.layers.Layer): ...@@ -411,7 +411,7 @@ class TFBertEncoder(tf.keras.layers.Layer):
def __init__(self, config: BertConfig, **kwargs): def __init__(self, config: BertConfig, **kwargs):
super().__init__(**kwargs) super().__init__(**kwargs)
self.layer = [TFBertLayer(config, name="layer_._{}".format(i)) for i in range(config.num_hidden_layers)] self.layer = [TFBertLayer(config, name=f"layer_._{i}") for i in range(config.num_hidden_layers)]
def call( def call(
self, self,
......
...@@ -192,8 +192,8 @@ class BertTokenizer(PreTrainedTokenizer): ...@@ -192,8 +192,8 @@ class BertTokenizer(PreTrainedTokenizer):
if not os.path.isfile(vocab_file): if not os.path.isfile(vocab_file):
raise ValueError( raise ValueError(
"Can't find a vocabulary file at path '{}'. To load the vocabulary from a Google pretrained " f"Can't find a vocabulary file at path '{vocab_file}'. To load the vocabulary from a Google pretrained "
"model use `tokenizer = BertTokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`".format(vocab_file) "model use `tokenizer = BertTokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`"
) )
self.vocab = load_vocab(vocab_file) self.vocab = load_vocab(vocab_file)
self.ids_to_tokens = collections.OrderedDict([(ids, tok) for tok, ids in self.vocab.items()]) self.ids_to_tokens = collections.OrderedDict([(ids, tok) for tok, ids in self.vocab.items()])
...@@ -343,8 +343,8 @@ class BertTokenizer(PreTrainedTokenizer): ...@@ -343,8 +343,8 @@ class BertTokenizer(PreTrainedTokenizer):
for token, token_index in sorted(self.vocab.items(), key=lambda kv: kv[1]): for token, token_index in sorted(self.vocab.items(), key=lambda kv: kv[1]):
if index != token_index: if index != token_index:
logger.warning( logger.warning(
"Saving vocabulary to {}: vocabulary indices are not consecutive." f"Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive."
" Please check that the vocabulary is not corrupted!".format(vocab_file) " Please check that the vocabulary is not corrupted!"
) )
index = token_index index = token_index
writer.write(token + "\n") writer.write(token + "\n")
......
...@@ -109,7 +109,7 @@ def load_tf_weights_in_bert_generation( ...@@ -109,7 +109,7 @@ def load_tf_weights_in_bert_generation(
array = np.asarray(sess.run(all_variables[key])) array = np.asarray(sess.run(all_variables[key]))
if not is_embedding: if not is_embedding:
logger.info("Transposing numpy weight of shape {} for {}".format(array.shape, key)) logger.info(f"Transposing numpy weight of shape {array.shape} for {key}")
array = np.transpose(array) array = np.transpose(array)
else: else:
model_pointer = model_pointer.weight model_pointer = model_pointer.weight
...@@ -126,7 +126,7 @@ def load_tf_weights_in_bert_generation( ...@@ -126,7 +126,7 @@ def load_tf_weights_in_bert_generation(
model_pointer.data = torch.from_numpy(array.astype(np.float32)) model_pointer.data = torch.from_numpy(array.astype(np.float32))
keep_track_variables.pop(key, None) keep_track_variables.pop(key, None)
logger.info("Weights not copied to PyTorch model: {}".format(", ".join(keep_track_variables.keys()))) logger.info(f"Weights not copied to PyTorch model: {', '.join(keep_track_variables.keys())}")
return model return model
......
...@@ -134,7 +134,7 @@ class BertGenerationTokenizer(PreTrainedTokenizer): ...@@ -134,7 +134,7 @@ class BertGenerationTokenizer(PreTrainedTokenizer):
def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]:
if not os.path.isdir(save_directory): if not os.path.isdir(save_directory):
logger.error("Vocabulary path ({}) should be a directory".format(save_directory)) logger.error(f"Vocabulary path ({save_directory}) should be a directory")
return return
out_vocab_file = os.path.join( out_vocab_file = os.path.join(
save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
......
...@@ -130,8 +130,8 @@ class BertJapaneseTokenizer(BertTokenizer): ...@@ -130,8 +130,8 @@ class BertJapaneseTokenizer(BertTokenizer):
if not os.path.isfile(vocab_file): if not os.path.isfile(vocab_file):
raise ValueError( raise ValueError(
"Can't find a vocabulary file at path '{}'. To load the vocabulary from a Google pretrained " f"Can't find a vocabulary file at path '{vocab_file}'. To load the vocabulary from a Google pretrained "
"model use `tokenizer = BertTokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`".format(vocab_file) "model use `tokenizer = BertTokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`"
) )
self.vocab = load_vocab(vocab_file) self.vocab = load_vocab(vocab_file)
self.ids_to_tokens = collections.OrderedDict([(ids, tok) for tok, ids in self.vocab.items()]) self.ids_to_tokens = collections.OrderedDict([(ids, tok) for tok, ids in self.vocab.items()])
...@@ -151,7 +151,7 @@ class BertJapaneseTokenizer(BertTokenizer): ...@@ -151,7 +151,7 @@ class BertJapaneseTokenizer(BertTokenizer):
do_lower_case=do_lower_case, never_split=never_split, **(mecab_kwargs or {}) do_lower_case=do_lower_case, never_split=never_split, **(mecab_kwargs or {})
) )
else: else:
raise ValueError("Invalid word_tokenizer_type '{}' is specified.".format(word_tokenizer_type)) raise ValueError(f"Invalid word_tokenizer_type '{word_tokenizer_type}' is specified.")
self.do_subword_tokenize = do_subword_tokenize self.do_subword_tokenize = do_subword_tokenize
self.subword_tokenizer_type = subword_tokenizer_type self.subword_tokenizer_type = subword_tokenizer_type
...@@ -161,7 +161,7 @@ class BertJapaneseTokenizer(BertTokenizer): ...@@ -161,7 +161,7 @@ class BertJapaneseTokenizer(BertTokenizer):
elif subword_tokenizer_type == "character": elif subword_tokenizer_type == "character":
self.subword_tokenizer = CharacterTokenizer(vocab=self.vocab, unk_token=self.unk_token) self.subword_tokenizer = CharacterTokenizer(vocab=self.vocab, unk_token=self.unk_token)
else: else:
raise ValueError("Invalid subword_tokenizer_type '{}' is specified.".format(subword_tokenizer_type)) raise ValueError(f"Invalid subword_tokenizer_type '{subword_tokenizer_type}' is specified.")
@property @property
def do_lower_case(self): def do_lower_case(self):
...@@ -279,7 +279,7 @@ class MecabTokenizer: ...@@ -279,7 +279,7 @@ class MecabTokenizer:
raise ValueError("Invalid mecab_dic is specified.") raise ValueError("Invalid mecab_dic is specified.")
mecabrc = os.path.join(dic_dir, "mecabrc") mecabrc = os.path.join(dic_dir, "mecabrc")
mecab_option = '-d "{}" -r "{}" '.format(dic_dir, mecabrc) + mecab_option mecab_option = f'-d "{dic_dir}" -r "{mecabrc}" ' + mecab_option
self.mecab = fugashi.GenericTagger(mecab_option) self.mecab = fugashi.GenericTagger(mecab_option)
......
...@@ -385,7 +385,7 @@ class BertweetTokenizer(PreTrainedTokenizer): ...@@ -385,7 +385,7 @@ class BertweetTokenizer(PreTrainedTokenizer):
def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]:
if not os.path.isdir(save_directory): if not os.path.isdir(save_directory):
logger.error("Vocabulary path ({}) should be a directory".format(save_directory)) logger.error(f"Vocabulary path ({save_directory}) should be a directory")
return return
out_vocab_file = os.path.join( out_vocab_file = os.path.join(
save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
...@@ -419,7 +419,7 @@ class BertweetTokenizer(PreTrainedTokenizer): ...@@ -419,7 +419,7 @@ class BertweetTokenizer(PreTrainedTokenizer):
except FileNotFoundError as fnfe: except FileNotFoundError as fnfe:
raise fnfe raise fnfe
except UnicodeError: except UnicodeError:
raise Exception("Incorrect encoding detected in {}, please " "rebuild the dataset".format(f)) raise Exception(f"Incorrect encoding detected in {f}, please rebuild the dataset")
return return
lines = f.readlines() lines = f.readlines()
......
...@@ -27,7 +27,7 @@ logging.set_verbosity_info() ...@@ -27,7 +27,7 @@ logging.set_verbosity_info()
def convert_tf_checkpoint_to_pytorch(tf_checkpoint_path, big_bird_config_file, pytorch_dump_path, is_trivia_qa): def convert_tf_checkpoint_to_pytorch(tf_checkpoint_path, big_bird_config_file, pytorch_dump_path, is_trivia_qa):
# Initialise PyTorch model # Initialise PyTorch model
config = BigBirdConfig.from_json_file(big_bird_config_file) config = BigBirdConfig.from_json_file(big_bird_config_file)
print("Building PyTorch model from configuration: {}".format(str(config))) print(f"Building PyTorch model from configuration: {config}")
if is_trivia_qa: if is_trivia_qa:
model = BigBirdForQuestionAnswering(config) model = BigBirdForQuestionAnswering(config)
......
...@@ -122,7 +122,7 @@ def load_tf_weights_in_big_bird(model, tf_checkpoint_path, is_trivia_qa=False): ...@@ -122,7 +122,7 @@ def load_tf_weights_in_big_bird(model, tf_checkpoint_path, is_trivia_qa=False):
if i >= len(init_vars) - 2: if i >= len(init_vars) - 2:
name = name.replace("intermediate", "output") name = name.replace("intermediate", "output")
logger.info("Loading TF weight {} with shape {}".format(name, var.shape)) logger.info(f"Loading TF weight {name} with shape {var.shape}")
array = var.value().numpy() array = var.value().numpy()
names.append(name) names.append(name)
tf_weights[name] = array tf_weights[name] = array
...@@ -141,7 +141,7 @@ def load_tf_weights_in_big_bird(model, tf_checkpoint_path, is_trivia_qa=False): ...@@ -141,7 +141,7 @@ def load_tf_weights_in_big_bird(model, tf_checkpoint_path, is_trivia_qa=False):
) )
raise raise
tf_path = os.path.abspath(tf_checkpoint_path) tf_path = os.path.abspath(tf_checkpoint_path)
logger.info("Converting TensorFlow checkpoint from {}".format(tf_path)) logger.info(f"Converting TensorFlow checkpoint from {tf_path}")
# Load weights from TF model # Load weights from TF model
init_vars = tf.saved_model.load(tf_path).variables if is_trivia_qa else tf.train.list_variables(tf_path) init_vars = tf.saved_model.load(tf_path).variables if is_trivia_qa else tf.train.list_variables(tf_path)
...@@ -304,8 +304,8 @@ class BigBirdSelfAttention(nn.Module): ...@@ -304,8 +304,8 @@ class BigBirdSelfAttention(nn.Module):
super().__init__() super().__init__()
if config.hidden_size % config.num_attention_heads != 0 and not hasattr(config, "embedding_size"): if config.hidden_size % config.num_attention_heads != 0 and not hasattr(config, "embedding_size"):
raise ValueError( raise ValueError(
"The hidden size (%d) is not a multiple of the number of attention " f"The hidden size ({config.hidden_size}) is not a multiple of the number of attention "
"heads (%d)" % (config.hidden_size, config.num_attention_heads) f"heads ({config.num_attention_heads})"
) )
self.num_attention_heads = config.num_attention_heads self.num_attention_heads = config.num_attention_heads
...@@ -2171,9 +2171,8 @@ class BigBirdModel(BigBirdPreTrainedModel): ...@@ -2171,9 +2171,8 @@ class BigBirdModel(BigBirdPreTrainedModel):
padding_len = (block_size - seq_len % block_size) % block_size padding_len = (block_size - seq_len % block_size) % block_size
if padding_len > 0: if padding_len > 0:
logger.info( logger.info(
"Input ids are automatically padded from {} to {} to be a multiple of `config.block_size`: {}".format( f"Input ids are automatically padded from {seq_len} to {seq_len + padding_len} to be a multiple of "
seq_len, seq_len + padding_len, block_size f"`config.block_size`: {block_size}"
)
) )
if input_ids is not None: if input_ids is not None:
input_ids = F.pad(input_ids, (0, padding_len), value=pad_token_id) input_ids = F.pad(input_ids, (0, padding_len), value=pad_token_id)
......
...@@ -164,7 +164,7 @@ class BigBirdTokenizer(PreTrainedTokenizer): ...@@ -164,7 +164,7 @@ class BigBirdTokenizer(PreTrainedTokenizer):
def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]:
if not os.path.isdir(save_directory): if not os.path.isdir(save_directory):
logger.error("Vocabulary path ({}) should be a directory".format(save_directory)) logger.error(f"Vocabulary path ({save_directory}) should be a directory")
return return
out_vocab_file = os.path.join( out_vocab_file = os.path.join(
save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
......
...@@ -208,7 +208,7 @@ class BlenderbotSmallTokenizer(PreTrainedTokenizer): ...@@ -208,7 +208,7 @@ class BlenderbotSmallTokenizer(PreTrainedTokenizer):
def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]:
if not os.path.isdir(save_directory): if not os.path.isdir(save_directory):
logger.error("Vocabulary path ({}) should be a directory".format(save_directory)) logger.error(f"Vocabulary path ({save_directory}) should be a directory")
return return
vocab_file = os.path.join( vocab_file = os.path.join(
save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
...@@ -226,8 +226,8 @@ class BlenderbotSmallTokenizer(PreTrainedTokenizer): ...@@ -226,8 +226,8 @@ class BlenderbotSmallTokenizer(PreTrainedTokenizer):
for bpe_tokens, token_index in sorted(self.bpe_ranks.items(), key=lambda kv: kv[1]): for bpe_tokens, token_index in sorted(self.bpe_ranks.items(), key=lambda kv: kv[1]):
if index != token_index: if index != token_index:
logger.warning( logger.warning(
"Saving vocabulary to {}: BPE merge indices are not consecutive." f"Saving vocabulary to {merge_file}: BPE merge indices are not consecutive."
" Please check that the tokenizer is not corrupted!".format(merge_file) " Please check that the tokenizer is not corrupted!"
) )
index = token_index index = token_index
writer.write(" ".join(bpe_tokens) + "\n") writer.write(" ".join(bpe_tokens) + "\n")
......
...@@ -256,7 +256,7 @@ class CamembertTokenizer(PreTrainedTokenizer): ...@@ -256,7 +256,7 @@ class CamembertTokenizer(PreTrainedTokenizer):
def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]:
if not os.path.isdir(save_directory): if not os.path.isdir(save_directory):
logger.error("Vocabulary path ({}) should be a directory".format(save_directory)) logger.error(f"Vocabulary path ({save_directory}) should be a directory")
return return
out_vocab_file = os.path.join( out_vocab_file = os.path.join(
save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment