modelcard.py 34 KB
Newer Older
thomwolf's avatar
thomwolf committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# coding=utf-8
# Copyright 2018 The HuggingFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" Configuration base class and utilities."""


import copy
import json
import os
Sylvain Gugger's avatar
Sylvain Gugger committed
21
22
23
24
import warnings
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Dict, List, Optional, Union
thomwolf's avatar
thomwolf committed
25

Sylvain Gugger's avatar
Sylvain Gugger committed
26
import requests
27
import yaml
28
from huggingface_hub import model_info
29
from huggingface_hub.utils import HFValidationError
Sylvain Gugger's avatar
Sylvain Gugger committed
30
31

from . import __version__
32
from .models.auto.modeling_auto import (
33
    MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES,
Sylvain Gugger's avatar
Sylvain Gugger committed
34
    MODEL_FOR_CAUSAL_LM_MAPPING_NAMES,
35
    MODEL_FOR_CTC_MAPPING_NAMES,
Sylvain Gugger's avatar
Sylvain Gugger committed
36
    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES,
37
    MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES,
Sylvain Gugger's avatar
Sylvain Gugger committed
38
39
40
41
42
    MODEL_FOR_MASKED_LM_MAPPING_NAMES,
    MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES,
    MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES,
    MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES,
    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES,
43
    MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES,
Sylvain Gugger's avatar
Sylvain Gugger committed
44
45
46
    MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING_NAMES,
    MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES,
)
47
from .training_args import ParallelMode
48
49
from .utils import (
    MODEL_CARD_NAME,
Sylvain Gugger's avatar
Sylvain Gugger committed
50
    cached_file,
51
52
53
54
55
56
57
    is_datasets_available,
    is_offline_mode,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
    logging,
)
Sylvain Gugger's avatar
Sylvain Gugger committed
58

thomwolf's avatar
thomwolf committed
59

Sylvain Gugger's avatar
Sylvain Gugger committed
60
61
62
TASK_MAPPING = {
    "text-generation": MODEL_FOR_CAUSAL_LM_MAPPING_NAMES,
    "image-classification": MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES,
63
    "image-segmentation": MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES,
Sylvain Gugger's avatar
Sylvain Gugger committed
64
65
66
67
68
69
70
    "fill-mask": MODEL_FOR_MASKED_LM_MAPPING_NAMES,
    "object-detection": MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES,
    "question-answering": MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES,
    "text2text-generation": MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES,
    "text-classification": MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES,
    "table-question-answering": MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING_NAMES,
    "token-classification": MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES,
71
    "audio-classification": MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES,
72
    "automatic-speech-recognition": {**MODEL_FOR_CTC_MAPPING_NAMES, **MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES},
Sylvain Gugger's avatar
Sylvain Gugger committed
73
}
thomwolf's avatar
thomwolf committed
74

Lysandre Debut's avatar
Lysandre Debut committed
75
logger = logging.get_logger(__name__)
thomwolf's avatar
thomwolf committed
76
77


78
class ModelCard:
Sylvain Gugger's avatar
Sylvain Gugger committed
79
80
    r"""
    Structured Model Card class. Store model card as well as methods for loading/downloading/saving model cards.
thomwolf's avatar
thomwolf committed
81

Sylvain Gugger's avatar
Sylvain Gugger committed
82
83
84
    Please read the following paper for details and explanation on the sections: "Model Cards for Model Reporting" by
    Margaret Mitchell, Simone Wu, Andrew Zaldivar, Parker Barnes, Lucy Vasserman, Ben Hutchinson, Elena Spitzer,
    Inioluwa Deborah Raji and Timnit Gebru for the proposal behind model cards. Link: https://arxiv.org/abs/1810.03993
thomwolf's avatar
thomwolf committed
85

Sylvain Gugger's avatar
Sylvain Gugger committed
86
    Note: A model card can be loaded and saved to disk.
thomwolf's avatar
thomwolf committed
87
    """
88

thomwolf's avatar
thomwolf committed
89
    def __init__(self, **kwargs):
Sylvain Gugger's avatar
Sylvain Gugger committed
90
91
92
        warnings.warn(
            "The class `ModelCard` is deprecated and will be removed in version 5 of Transformers", FutureWarning
        )
93
        # Recommended attributes from https://arxiv.org/abs/1810.03993 (see papers)
94
95
96
97
98
99
100
101
102
        self.model_details = kwargs.pop("model_details", {})
        self.intended_use = kwargs.pop("intended_use", {})
        self.factors = kwargs.pop("factors", {})
        self.metrics = kwargs.pop("metrics", {})
        self.evaluation_data = kwargs.pop("evaluation_data", {})
        self.training_data = kwargs.pop("training_data", {})
        self.quantitative_analyses = kwargs.pop("quantitative_analyses", {})
        self.ethical_considerations = kwargs.pop("ethical_considerations", {})
        self.caveats_and_recommendations = kwargs.pop("caveats_and_recommendations", {})
thomwolf's avatar
thomwolf committed
103
104
105
106
107
108

        # Open additional attributes
        for key, value in kwargs.items():
            try:
                setattr(self, key, value)
            except AttributeError as err:
109
                logger.error(f"Can't set {key} with value {value} for {self}")
thomwolf's avatar
thomwolf committed
110
111
                raise err

thomwolf's avatar
thomwolf committed
112
    def save_pretrained(self, save_directory_or_file):
Lysandre's avatar
Lysandre committed
113
        """Save a model card object to the directory or file `save_directory_or_file`."""
thomwolf's avatar
thomwolf committed
114
115
116
117
118
        if os.path.isdir(save_directory_or_file):
            # If we save using the predefined names, we can load using `from_pretrained`
            output_model_card_file = os.path.join(save_directory_or_file, MODEL_CARD_NAME)
        else:
            output_model_card_file = save_directory_or_file
thomwolf's avatar
thomwolf committed
119
120

        self.to_json_file(output_model_card_file)
121
        logger.info(f"Model card saved in {output_model_card_file}")
thomwolf's avatar
thomwolf committed
122
123
124

    @classmethod
    def from_pretrained(cls, pretrained_model_name_or_path, **kwargs):
Sylvain Gugger's avatar
Sylvain Gugger committed
125
        r"""
126
        Instantiate a [`ModelCard`] from a pre-trained model model card.
thomwolf's avatar
thomwolf committed
127
128
129
130

        Parameters:
            pretrained_model_name_or_path: either:

131
132
133
                - a string, the *model id* of a pretrained model card hosted inside a model repo on huggingface.co.
                  Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a
                  user or organization name, like `dbmdz/bert-base-german-cased`.
Sylvain Gugger's avatar
Sylvain Gugger committed
134
135
                - a path to a *directory* containing a model card file saved using the [`~ModelCard.save_pretrained`]
                  method, e.g.: `./my_model_directory/`.
136
                - a path or url to a saved model card JSON *file*, e.g.: `./my_model_directory/modelcard.json`.
thomwolf's avatar
thomwolf committed
137

138
            cache_dir: (*optional*) string:
Sylvain Gugger's avatar
Sylvain Gugger committed
139
140
                Path to a directory in which a downloaded pre-trained model card should be cached if the standard cache
                should not be used.
thomwolf's avatar
thomwolf committed
141

142
            kwargs: (*optional*) dict: key/value pairs with which to update the ModelCard object after loading.
thomwolf's avatar
thomwolf committed
143

Sylvain Gugger's avatar
Sylvain Gugger committed
144
145
146
                - The values in kwargs of any keys which are model card attributes will be used to override the loaded
                  values.
                - Behavior concerning key/value pairs whose keys are *not* model card attributes is controlled by the
147
                  *return_unused_kwargs* keyword parameter.
thomwolf's avatar
thomwolf committed
148

149
            proxies: (*optional*) dict, default None:
Sylvain Gugger's avatar
Sylvain Gugger committed
150
151
                A dictionary of proxy servers to use by protocol or endpoint, e.g.: {'http': 'foo.bar:3128',
                'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
thomwolf's avatar
thomwolf committed
152

153
            return_unused_kwargs: (*optional*) bool:
thomwolf's avatar
thomwolf committed
154
155

                - If False, then this function returns just the final model card object.
156
                - If True, then this functions returns a tuple *(model card, unused_kwargs)* where *unused_kwargs* is a
Sylvain Gugger's avatar
Sylvain Gugger committed
157
                  dictionary consisting of the key/value pairs whose keys are not model card attributes: ie the part of
158
                  kwargs which has not been used to update *ModelCard* and is otherwise ignored.
thomwolf's avatar
thomwolf committed
159

160
        Examples:
thomwolf's avatar
thomwolf committed
161

162
        ```python
Sylvain Gugger's avatar
Sylvain Gugger committed
163
164
165
166
        # Download model card from huggingface.co and cache.
        modelcard = ModelCard.from_pretrained("bert-base-uncased")
        # Model card was saved using *save_pretrained('./test/saved_model/')*
        modelcard = ModelCard.from_pretrained("./test/saved_model/")
Sylvain Gugger's avatar
Sylvain Gugger committed
167
168
        modelcard = ModelCard.from_pretrained("./test/saved_model/modelcard.json")
        modelcard = ModelCard.from_pretrained("bert-base-uncased", output_attentions=True, foo=False)
169
        ```"""
170
171
172
        cache_dir = kwargs.pop("cache_dir", None)
        proxies = kwargs.pop("proxies", None)
        return_unused_kwargs = kwargs.pop("return_unused_kwargs", False)
173
174
175
176
177
        from_pipeline = kwargs.pop("_from_pipeline", None)

        user_agent = {"file_type": "model_card"}
        if from_pipeline is not None:
            user_agent["using_pipeline"] = from_pipeline
thomwolf's avatar
thomwolf committed
178

Sylvain Gugger's avatar
Sylvain Gugger committed
179
180
181
182
        is_local = os.path.isdir(pretrained_model_name_or_path)
        if os.path.isfile(pretrained_model_name_or_path):
            resolved_model_card_file = pretrained_model_name_or_path
            is_local = True
thomwolf's avatar
thomwolf committed
183
        else:
Sylvain Gugger's avatar
Sylvain Gugger committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
            try:
                # Load from URL or cache if already cached
                resolved_model_card_file = cached_file(
                    pretrained_model_name_or_path,
                    filename=MODEL_CARD_NAME,
                    cache_dir=cache_dir,
                    proxies=proxies,
                    user_agent=user_agent,
                )
                if is_local:
                    logger.info(f"loading model card file {resolved_model_card_file}")
                else:
                    logger.info(f"loading model card file {MODEL_CARD_NAME} from cache at {resolved_model_card_file}")
                # Load model card
                modelcard = cls.from_json_file(resolved_model_card_file)

            except (EnvironmentError, json.JSONDecodeError):
                # We fall back on creating an empty model card
                modelcard = cls()
thomwolf's avatar
thomwolf committed
203
204
205
206

        # Update model card with kwargs if needed
        to_remove = []
        for key, value in kwargs.items():
207
208
            if hasattr(modelcard, key):
                setattr(modelcard, key, value)
thomwolf's avatar
thomwolf committed
209
210
211
212
                to_remove.append(key)
        for key in to_remove:
            kwargs.pop(key, None)

213
        logger.info(f"Model card: {modelcard}")
thomwolf's avatar
thomwolf committed
214
        if return_unused_kwargs:
215
            return modelcard, kwargs
thomwolf's avatar
thomwolf committed
216
        else:
217
            return modelcard
thomwolf's avatar
thomwolf committed
218
219
220
221
222
223
224
225
226

    @classmethod
    def from_dict(cls, json_object):
        """Constructs a `ModelCard` from a Python dictionary of parameters."""
        return cls(**json_object)

    @classmethod
    def from_json_file(cls, json_file):
        """Constructs a `ModelCard` from a json file of parameters."""
227
        with open(json_file, "r", encoding="utf-8") as reader:
thomwolf's avatar
thomwolf committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
            text = reader.read()
        dict_obj = json.loads(text)
        return cls(**dict_obj)

    def __eq__(self, other):
        return self.__dict__ == other.__dict__

    def __repr__(self):
        return str(self.to_json_string())

    def to_dict(self):
        """Serializes this instance to a Python dictionary."""
        output = copy.deepcopy(self.__dict__)
        return output

    def to_json_string(self):
        """Serializes this instance to a JSON string."""
        return json.dumps(self.to_dict(), indent=2, sort_keys=True) + "\n"

    def to_json_file(self, json_file_path):
Patrick von Platen's avatar
Patrick von Platen committed
248
        """Save this instance to a json file."""
249
        with open(json_file_path, "w", encoding="utf-8") as writer:
thomwolf's avatar
thomwolf committed
250
            writer.write(self.to_json_string())
Sylvain Gugger's avatar
Sylvain Gugger committed
251
252


Matt's avatar
Matt committed
253
AUTOGENERATED_TRAINER_COMMENT = """
Sylvain Gugger's avatar
Sylvain Gugger committed
254
255
256
257
<!-- This model card has been generated automatically according to the information the Trainer had access to. You
should probably proofread and complete it, then remove this comment. -->
"""

Matt's avatar
Matt committed
258
259
260
261
262
AUTOGENERATED_KERAS_COMMENT = """
<!-- This model card has been generated automatically according to the information Keras had access to. You should
probably proofread and complete it, then remove this comment. -->
"""

Sylvain Gugger's avatar
Sylvain Gugger committed
263
264
265

TASK_TAG_TO_NAME_MAPPING = {
    "fill-mask": "Masked Language Modeling",
Sylvain Gugger's avatar
Sylvain Gugger committed
266
    "image-classification": "Image Classification",
267
    "image-segmentation": "Image Segmentation",
Sylvain Gugger's avatar
Sylvain Gugger committed
268
    "multiple-choice": "Multiple Choice",
Sylvain Gugger's avatar
Sylvain Gugger committed
269
    "object-detection": "Object Detection",
Sylvain Gugger's avatar
Sylvain Gugger committed
270
271
    "question-answering": "Question Answering",
    "summarization": "Summarization",
Sylvain Gugger's avatar
Sylvain Gugger committed
272
    "table-question-answering": "Table Question Answering",
Sylvain Gugger's avatar
Sylvain Gugger committed
273
274
275
276
277
278
    "text-classification": "Text Classification",
    "text-generation": "Causal Language Modeling",
    "text2text-generation": "Sequence-to-sequence Language Modeling",
    "token-classification": "Token Classification",
    "translation": "Translation",
    "zero-shot-classification": "Zero Shot Classification",
279
    "automatic-speech-recognition": "Automatic Speech Recognition",
Sylvain Gugger's avatar
Sylvain Gugger committed
280
281
282
283
284
285
286
287
288
289
290
291
292
293
}


METRIC_TAGS = [
    "accuracy",
    "bleu",
    "f1",
    "matthews_correlation",
    "pearsonr",
    "precision",
    "recall",
    "rouge",
    "sacrebleu",
    "spearmanr",
294
    "wer",
Sylvain Gugger's avatar
Sylvain Gugger committed
295
296
297
298
299
300
301
302
303
304
305
306
]


def _listify(obj):
    if obj is None:
        return []
    elif isinstance(obj, str):
        return [obj]
    else:
        return obj


307
308
309
310
311
def _insert_values_as_list(metadata, name, values):
    if values is None:
        return metadata
    if isinstance(values, str):
        values = [values]
312
    values = [v for v in values if v is not None]
313
314
315
316
    if len(values) == 0:
        return metadata
    metadata[name] = values
    return metadata
Sylvain Gugger's avatar
Sylvain Gugger committed
317
318
319
320
321
322
323
324
325
326
327
328
329
330


def infer_metric_tags_from_eval_results(eval_results):
    if eval_results is None:
        return {}
    result = {}
    for key in eval_results.keys():
        if key.lower().replace(" ", "_") in METRIC_TAGS:
            result[key.lower().replace(" ", "_")] = key
        elif key.lower() == "rouge1":
            result["rouge"] = key
    return result


331
332
333
334
335
336
337
def _insert_value(metadata, name, value):
    if value is None:
        return metadata
    metadata[name] = value
    return metadata


Sylvain Gugger's avatar
Sylvain Gugger committed
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
def is_hf_dataset(dataset):
    if not is_datasets_available():
        return False

    from datasets import Dataset

    return isinstance(dataset, Dataset)


def _get_mapping_values(mapping):
    result = []
    for v in mapping.values():
        if isinstance(v, (tuple, list)):
            result += list(v)
        else:
            result.append(v)
    return result


Sylvain Gugger's avatar
Sylvain Gugger committed
357
358
359
360
361
362
363
@dataclass
class TrainingSummary:
    model_name: str
    language: Optional[Union[str, List[str]]] = None
    license: Optional[str] = None
    tags: Optional[Union[str, List[str]]] = None
    finetuned_from: Optional[str] = None
Sylvain Gugger's avatar
Sylvain Gugger committed
364
    tasks: Optional[Union[str, List[str]]] = None
Sylvain Gugger's avatar
Sylvain Gugger committed
365
366
367
    dataset: Optional[Union[str, List[str]]] = None
    dataset_tags: Optional[Union[str, List[str]]] = None
    dataset_args: Optional[Union[str, List[str]]] = None
368
    dataset_metadata: Optional[Dict[str, Any]] = None
Sylvain Gugger's avatar
Sylvain Gugger committed
369
370
371
    eval_results: Optional[Dict[str, float]] = None
    eval_lines: Optional[List[str]] = None
    hyperparameters: Optional[Dict[str, Any]] = None
Matt's avatar
Matt committed
372
    source: Optional[str] = "trainer"
Sylvain Gugger's avatar
Sylvain Gugger committed
373
374
375

    def __post_init__(self):
        # Infer default license from the checkpoint used, if possible.
Sylvain Gugger's avatar
Sylvain Gugger committed
376
377
378
379
380
381
        if (
            self.license is None
            and not is_offline_mode()
            and self.finetuned_from is not None
            and len(self.finetuned_from) > 0
        ):
Sylvain Gugger's avatar
Sylvain Gugger committed
382
            try:
383
384
                info = model_info(self.finetuned_from)
                for tag in info.tags:
Sylvain Gugger's avatar
Sylvain Gugger committed
385
386
                    if tag.startswith("license:"):
                        self.license = tag[8:]
387
            except (requests.exceptions.HTTPError, HFValidationError):
Sylvain Gugger's avatar
Sylvain Gugger committed
388
389
390
                pass

    def create_model_index(self, metric_mapping):
391
        model_index = {"name": self.model_name}
Sylvain Gugger's avatar
Sylvain Gugger committed
392
393
394
395
396

        # Dataset mapping tag -> name
        dataset_names = _listify(self.dataset)
        dataset_tags = _listify(self.dataset_tags)
        dataset_args = _listify(self.dataset_args)
397
        dataset_metadata = _listify(self.dataset_metadata)
Sylvain Gugger's avatar
Sylvain Gugger committed
398
399
400
401
        if len(dataset_args) < len(dataset_tags):
            dataset_args = dataset_args + [None] * (len(dataset_tags) - len(dataset_args))
        dataset_mapping = {tag: name for tag, name in zip(dataset_tags, dataset_names)}
        dataset_arg_mapping = {tag: arg for tag, arg in zip(dataset_tags, dataset_args)}
402
        dataset_metadata_mapping = {tag: metadata for tag, metadata in zip(dataset_tags, dataset_metadata)}
Sylvain Gugger's avatar
Sylvain Gugger committed
403
404

        task_mapping = {
Sylvain Gugger's avatar
Sylvain Gugger committed
405
            task: TASK_TAG_TO_NAME_MAPPING[task] for task in _listify(self.tasks) if task in TASK_TAG_TO_NAME_MAPPING
Sylvain Gugger's avatar
Sylvain Gugger committed
406
407
        }

Matt's avatar
Matt committed
408
409
        model_index["results"] = []

Sylvain Gugger's avatar
Sylvain Gugger committed
410
        if len(task_mapping) == 0 and len(dataset_mapping) == 0:
Matt's avatar
Matt committed
411
            return [model_index]
Sylvain Gugger's avatar
Sylvain Gugger committed
412
413
414
415
416
        if len(task_mapping) == 0:
            task_mapping = {None: None}
        if len(dataset_mapping) == 0:
            dataset_mapping = {None: None}

417
418
        # One entry per dataset and per task
        all_possibilities = [(task_tag, ds_tag) for task_tag in task_mapping for ds_tag in dataset_mapping]
Sylvain Gugger's avatar
Sylvain Gugger committed
419
        for task_tag, ds_tag in all_possibilities:
420
            result = {}
Sylvain Gugger's avatar
Sylvain Gugger committed
421
            if task_tag is not None:
422
423
                result["task"] = {"name": task_mapping[task_tag], "type": task_tag}

Sylvain Gugger's avatar
Sylvain Gugger committed
424
            if ds_tag is not None:
425
426
427
428
429
430
                metadata = dataset_metadata_mapping.get(ds_tag, {})
                result["dataset"] = {
                    "name": dataset_mapping[ds_tag],
                    "type": ds_tag,
                    **metadata,
                }
Sylvain Gugger's avatar
Sylvain Gugger committed
431
                if dataset_arg_mapping[ds_tag] is not None:
432
433
                    result["dataset"]["args"] = dataset_arg_mapping[ds_tag]

Sylvain Gugger's avatar
Sylvain Gugger committed
434
            if len(metric_mapping) > 0:
435
                result["metrics"] = []
Sylvain Gugger's avatar
Sylvain Gugger committed
436
                for metric_tag, metric_name in metric_mapping.items():
437
438
439
440
441
442
443
                    result["metrics"].append(
                        {
                            "name": metric_name,
                            "type": metric_tag,
                            "value": self.eval_results[metric_name],
                        }
                    )
Sylvain Gugger's avatar
Sylvain Gugger committed
444

445
446
447
448
            # Remove partial results to avoid the model card being rejected.
            if "task" in result and "dataset" in result and "metrics" in result:
                model_index["results"].append(result)
            else:
449
                logger.info(f"Dropping the following result as it does not have all the necessary fields:\n{result}")
Sylvain Gugger's avatar
Sylvain Gugger committed
450

451
452
453
454
455
456
457
458
459
460
461
        return [model_index]

    def create_metadata(self):
        metric_mapping = infer_metric_tags_from_eval_results(self.eval_results)

        metadata = {}
        metadata = _insert_values_as_list(metadata, "language", self.language)
        metadata = _insert_value(metadata, "license", self.license)
        metadata = _insert_values_as_list(metadata, "tags", self.tags)
        metadata = _insert_values_as_list(metadata, "datasets", self.dataset_tags)
        metadata = _insert_values_as_list(metadata, "metrics", list(metric_mapping.keys()))
462
        metadata["model-index"] = self.create_model_index(metric_mapping)
463
464

        return metadata
Sylvain Gugger's avatar
Sylvain Gugger committed
465
466
467
468

    def to_model_card(self):
        model_card = ""

469
        metadata = yaml.dump(self.create_metadata(), sort_keys=False)
Sylvain Gugger's avatar
Sylvain Gugger committed
470
471
472
473
        if len(metadata) > 0:
            model_card = f"---\n{metadata}---\n"

        # Now the model card for realsies.
Matt's avatar
Matt committed
474
475
476
477
        if self.source == "trainer":
            model_card += AUTOGENERATED_TRAINER_COMMENT
        else:
            model_card += AUTOGENERATED_KERAS_COMMENT
Sylvain Gugger's avatar
Sylvain Gugger committed
478
479
480
481
482
483

        model_card += f"\n# {self.model_name}\n\n"

        if self.finetuned_from is None:
            model_card += "This model was trained from scratch on "
        else:
Sylvain Gugger's avatar
Sylvain Gugger committed
484
485
486
487
            model_card += (
                "This model is a fine-tuned version of"
                f" [{self.finetuned_from}](https://huggingface.co/{self.finetuned_from}) on "
            )
Sylvain Gugger's avatar
Sylvain Gugger committed
488
489

        if self.dataset is None:
490
            model_card += "an unknown dataset."
Sylvain Gugger's avatar
Sylvain Gugger committed
491
492
493
        else:
            if isinstance(self.dataset, str):
                model_card += f"the {self.dataset} dataset."
Sylvain Gugger's avatar
Sylvain Gugger committed
494
495
            elif isinstance(self.dataset, (tuple, list)) and len(self.dataset) == 1:
                model_card += f"the {self.dataset[0]} dataset."
Sylvain Gugger's avatar
Sylvain Gugger committed
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
            else:
                model_card += (
                    ", ".join([f"the {ds}" for ds in self.dataset[:-1]]) + f" and the {self.dataset[-1]} datasets."
                )

        if self.eval_results is not None:
            model_card += "\nIt achieves the following results on the evaluation set:\n"
            model_card += "\n".join([f"- {name}: {_maybe_round(value)}" for name, value in self.eval_results.items()])
        model_card += "\n"

        model_card += "\n## Model description\n\nMore information needed\n"
        model_card += "\n## Intended uses & limitations\n\nMore information needed\n"
        model_card += "\n## Training and evaluation data\n\nMore information needed\n"

        model_card += "\n## Training procedure\n"
        model_card += "\n### Training hyperparameters\n"
        if self.hyperparameters is not None:
            model_card += "\nThe following hyperparameters were used during training:\n"
            model_card += "\n".join([f"- {name}: {value}" for name, value in self.hyperparameters.items()])
            model_card += "\n"
        else:
            model_card += "\nMore information needed\n"

        if self.eval_lines is not None:
            model_card += "\n### Training results\n\n"
            model_card += make_markdown_table(self.eval_lines)
            model_card += "\n"

        model_card += "\n### Framework versions\n\n"
        model_card += f"- Transformers {__version__}\n"
Matt's avatar
Matt committed
526
527

        if self.source == "trainer" and is_torch_available():
Sylvain Gugger's avatar
Sylvain Gugger committed
528
529
530
            import torch

            model_card += f"- Pytorch {torch.__version__}\n"
Matt's avatar
Matt committed
531
532
533
534
        elif self.source == "keras" and is_tf_available():
            import tensorflow as tf

            model_card += f"- TensorFlow {tf.__version__}\n"
Sylvain Gugger's avatar
Sylvain Gugger committed
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
        if is_datasets_available():
            import datasets

            model_card += f"- Datasets {datasets.__version__}\n"
        if is_tokenizers_available():
            import tokenizers

            model_card += f"- Tokenizers {tokenizers.__version__}\n"

        return model_card

    @classmethod
    def from_trainer(
        cls,
        trainer,
        language=None,
        license=None,
        tags=None,
        model_name=None,
        finetuned_from=None,
Sylvain Gugger's avatar
Sylvain Gugger committed
555
        tasks=None,
Sylvain Gugger's avatar
Sylvain Gugger committed
556
        dataset_tags=None,
557
        dataset_metadata=None,
Sylvain Gugger's avatar
Sylvain Gugger committed
558
559
560
        dataset=None,
        dataset_args=None,
    ):
Sylvain Gugger's avatar
Sylvain Gugger committed
561
562
563
564
565
566
        # Infer default from dataset
        one_dataset = trainer.train_dataset if trainer.train_dataset is not None else trainer.eval_dataset
        if is_hf_dataset(one_dataset) and (dataset_tags is None or dataset_args is None):
            default_tag = one_dataset.builder_name
            # Those are not real datasets from the Hub so we exclude them.
            if default_tag not in ["csv", "json", "pandas", "parquet", "text"]:
567
568
                if dataset_metadata is None:
                    dataset_metadata = [{"config": one_dataset.config_name, "split": str(one_dataset.split)}]
Sylvain Gugger's avatar
Sylvain Gugger committed
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
                if dataset_tags is None:
                    dataset_tags = [default_tag]
                if dataset_args is None:
                    dataset_args = [one_dataset.config_name]

        if dataset is None and dataset_tags is not None:
            dataset = dataset_tags

        # Infer default finetuned_from
        if (
            finetuned_from is None
            and hasattr(trainer.model.config, "_name_or_path")
            and not os.path.isdir(trainer.model.config._name_or_path)
        ):
            finetuned_from = trainer.model.config._name_or_path

        # Infer default task tag:
        if tasks is None:
            model_class_name = trainer.model.__class__.__name__
            for task, mapping in TASK_MAPPING.items():
                if model_class_name in _get_mapping_values(mapping):
                    tasks = task

Sylvain Gugger's avatar
Sylvain Gugger committed
592
593
594
        if model_name is None:
            model_name = Path(trainer.args.output_dir).name

595
596
597
598
599
600
601
602
        # Add `generated_from_trainer` to the tags
        if tags is None:
            tags = ["generated_from_trainer"]
        elif isinstance(tags, str) and tags != "generated_from_trainer":
            tags = [tags, "generated_from_trainer"]
        elif "generated_from_trainer" not in tags:
            tags.append("generated_from_trainer")

Sylvain Gugger's avatar
Sylvain Gugger committed
603
604
605
606
607
608
609
610
611
        _, eval_lines, eval_results = parse_log_history(trainer.state.log_history)
        hyperparameters = extract_hyperparameters_from_trainer(trainer)

        return cls(
            language=language,
            license=license,
            tags=tags,
            model_name=model_name,
            finetuned_from=finetuned_from,
Sylvain Gugger's avatar
Sylvain Gugger committed
612
            tasks=tasks,
Sylvain Gugger's avatar
Sylvain Gugger committed
613
            dataset=dataset,
614
            dataset_tags=dataset_tags,
Sylvain Gugger's avatar
Sylvain Gugger committed
615
            dataset_args=dataset_args,
616
            dataset_metadata=dataset_metadata,
Sylvain Gugger's avatar
Sylvain Gugger committed
617
618
619
620
621
            eval_results=eval_results,
            eval_lines=eval_lines,
            hyperparameters=hyperparameters,
        )

Matt's avatar
Matt committed
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
    @classmethod
    def from_keras(
        cls,
        model,
        model_name,
        keras_history=None,
        language=None,
        license=None,
        tags=None,
        finetuned_from=None,
        tasks=None,
        dataset_tags=None,
        dataset=None,
        dataset_args=None,
    ):
        # Infer default from dataset
        if dataset is not None:
            if is_hf_dataset(dataset) and (dataset_tags is None or dataset_args is None):
                default_tag = dataset.builder_name
                # Those are not real datasets from the Hub so we exclude them.
                if default_tag not in ["csv", "json", "pandas", "parquet", "text"]:
                    if dataset_tags is None:
                        dataset_tags = [default_tag]
                    if dataset_args is None:
                        dataset_args = [dataset.config_name]

        if dataset is None and dataset_tags is not None:
            dataset = dataset_tags

        # Infer default finetuned_from
        if (
            finetuned_from is None
            and hasattr(model.config, "_name_or_path")
            and not os.path.isdir(model.config._name_or_path)
        ):
            finetuned_from = model.config._name_or_path

        # Infer default task tag:
        if tasks is None:
            model_class_name = model.__class__.__name__
            for task, mapping in TASK_MAPPING.items():
                if model_class_name in _get_mapping_values(mapping):
                    tasks = task

        # Add `generated_from_keras_callback` to the tags
        if tags is None:
            tags = ["generated_from_keras_callback"]
        elif isinstance(tags, str) and tags != "generated_from_keras_callback":
            tags = [tags, "generated_from_keras_callback"]
671
        elif "generated_from_keras_callback" not in tags:
Matt's avatar
Matt committed
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
            tags.append("generated_from_keras_callback")

        if keras_history is not None:
            _, eval_lines, eval_results = parse_keras_history(keras_history)
        else:
            eval_lines = []
            eval_results = dict()
        hyperparameters = extract_hyperparameters_from_keras(model)

        return cls(
            language=language,
            license=license,
            tags=tags,
            model_name=model_name,
            finetuned_from=finetuned_from,
            tasks=tasks,
            dataset_tags=dataset_tags,
            dataset=dataset,
            dataset_args=dataset_args,
            eval_results=eval_results,
            eval_lines=eval_lines,
            hyperparameters=hyperparameters,
            source="keras",
        )


def parse_keras_history(logs):
    """
    Parse the `logs` of either a `tf.keras.History` object returned by `model.fit()` or an accumulated logs `dict`
    passed to the `PushToHubCallback`. Returns lines and logs compatible with those returned by `parse_log_history`.
    """
    if hasattr(logs, "history"):
        # This looks like a `History` object
705
706
707
        if not hasattr(logs, "epoch"):
            # This history looks empty, return empty results
            return None, [], dict()
Matt's avatar
Matt committed
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
        logs.history["epoch"] = logs.epoch
        logs = logs.history
    else:
        # Training logs is a list of dicts, let's invert it to a dict of lists to match a History object
        logs = {log_key: [single_dict[log_key] for single_dict in logs] for log_key in logs[0]}

    lines = []
    for i in range(len(logs["epoch"])):
        epoch_dict = {log_key: log_value_list[i] for log_key, log_value_list in logs.items()}
        values = dict()
        for k, v in epoch_dict.items():
            if k.startswith("val_"):
                k = "validation_" + k[4:]
            elif k != "epoch":
                k = "train_" + k
            splits = k.split("_")
            name = " ".join([part.capitalize() for part in splits])
            values[name] = v
        lines.append(values)

    eval_results = lines[-1]

    return logs, lines, eval_results

Sylvain Gugger's avatar
Sylvain Gugger committed
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746

def parse_log_history(log_history):
    """
    Parse the `log_history` of a Trainer to get the intermediate and final evaluation results.
    """
    idx = 0
    while idx < len(log_history) and "train_runtime" not in log_history[idx]:
        idx += 1

    # If there are no training logs
    if idx == len(log_history):
        idx -= 1
        while idx >= 0 and "eval_loss" not in log_history[idx]:
            idx -= 1

747
        if idx >= 0:
Sylvain Gugger's avatar
Sylvain Gugger committed
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
            return None, None, log_history[idx]
        else:
            return None, None, None

    # From now one we can assume we have training logs:
    train_log = log_history[idx]
    lines = []
    training_loss = "No log"
    for i in range(idx):
        if "loss" in log_history[i]:
            training_loss = log_history[i]["loss"]
        if "eval_loss" in log_history[i]:
            metrics = log_history[i].copy()
            _ = metrics.pop("total_flos", None)
            epoch = metrics.pop("epoch", None)
            step = metrics.pop("step", None)
            _ = metrics.pop("eval_runtime", None)
            _ = metrics.pop("eval_samples_per_second", None)
766
            _ = metrics.pop("eval_steps_per_second", None)
Sylvain Gugger's avatar
Sylvain Gugger committed
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
            values = {"Training Loss": training_loss, "Epoch": epoch, "Step": step}
            for k, v in metrics.items():
                if k == "eval_loss":
                    values["Validation Loss"] = v
                else:
                    splits = k.split("_")
                    name = " ".join([part.capitalize() for part in splits[1:]])
                    values[name] = v
            lines.append(values)

    idx = len(log_history) - 1
    while idx >= 0 and "eval_loss" not in log_history[idx]:
        idx -= 1

    if idx > 0:
        eval_results = {}
        for key, value in log_history[idx].items():
            if key.startswith("eval_"):
                key = key[5:]
786
            if key not in ["runtime", "samples_per_second", "steps_per_second", "epoch", "step"]:
Sylvain Gugger's avatar
Sylvain Gugger committed
787
788
789
790
791
792
793
                camel_cased_key = " ".join([part.capitalize() for part in key.split("_")])
                eval_results[camel_cased_key] = value
        return train_log, lines, eval_results
    else:
        return train_log, lines, None


Matt's avatar
Matt committed
794
795
796
797
798
799
800
801
802
803
804
805
806
def extract_hyperparameters_from_keras(model):
    import tensorflow as tf

    hyperparameters = dict()
    if hasattr(model, "optimizer") and model.optimizer is not None:
        hyperparameters["optimizer"] = model.optimizer.get_config()
    else:
        hyperparameters["optimizer"] = None
    hyperparameters["training_precision"] = tf.keras.mixed_precision.global_policy().name

    return hyperparameters


Sylvain Gugger's avatar
Sylvain Gugger committed
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
def _maybe_round(v, decimals=4):
    if isinstance(v, float) and len(str(v).split(".")) > 1 and len(str(v).split(".")[1]) > decimals:
        return f"{v:.{decimals}f}"
    return str(v)


def _regular_table_line(values, col_widths):
    values_with_space = [f"| {v}" + " " * (w - len(v) + 1) for v, w in zip(values, col_widths)]
    return "".join(values_with_space) + "|\n"


def _second_table_line(col_widths):
    values = ["|:" + "-" * w + ":" for w in col_widths]
    return "".join(values) + "|\n"


def make_markdown_table(lines):
    """
    Create a nice Markdown table from the results in `lines`.
    """
    if lines is None or len(lines) == 0:
        return ""
    col_widths = {key: len(str(key)) for key in lines[0].keys()}
    for line in lines:
        for key, value in line.items():
            if col_widths[key] < len(_maybe_round(value)):
                col_widths[key] = len(_maybe_round(value))

    table = _regular_table_line(list(lines[0].keys()), list(col_widths.values()))
    table += _second_table_line(list(col_widths.values()))
    for line in lines:
        table += _regular_table_line([_maybe_round(v) for v in line.values()], list(col_widths.values()))
    return table


_TRAINING_ARGS_KEYS = [
    "learning_rate",
    "train_batch_size",
    "eval_batch_size",
    "seed",
]


def extract_hyperparameters_from_trainer(trainer):
    hyperparameters = {k: getattr(trainer.args, k) for k in _TRAINING_ARGS_KEYS}

    if trainer.args.parallel_mode not in [ParallelMode.NOT_PARALLEL, ParallelMode.NOT_DISTRIBUTED]:
        hyperparameters["distributed_type"] = (
            "multi-GPU" if trainer.args.parallel_mode == ParallelMode.DISTRIBUTED else trainer.args.parallel_mode.value
        )
    if trainer.args.world_size > 1:
        hyperparameters["num_devices"] = trainer.args.world_size
    if trainer.args.gradient_accumulation_steps > 1:
        hyperparameters["gradient_accumulation_steps"] = trainer.args.gradient_accumulation_steps

    total_train_batch_size = (
        trainer.args.train_batch_size * trainer.args.world_size * trainer.args.gradient_accumulation_steps
    )
    if total_train_batch_size != hyperparameters["train_batch_size"]:
        hyperparameters["total_train_batch_size"] = total_train_batch_size
    total_eval_batch_size = trainer.args.eval_batch_size * trainer.args.world_size
    if total_eval_batch_size != hyperparameters["eval_batch_size"]:
        hyperparameters["total_eval_batch_size"] = total_eval_batch_size

    if trainer.args.adafactor:
        hyperparameters["optimizer"] = "Adafactor"
    else:
Sylvain Gugger's avatar
Sylvain Gugger committed
874
875
876
877
        hyperparameters["optimizer"] = (
            f"Adam with betas=({trainer.args.adam_beta1},{trainer.args.adam_beta2}) and"
            f" epsilon={trainer.args.adam_epsilon}"
        )
Sylvain Gugger's avatar
Sylvain Gugger committed
878
879
880
881
882
883
884
885
886
887
888
889

    hyperparameters["lr_scheduler_type"] = trainer.args.lr_scheduler_type.value
    if trainer.args.warmup_ratio != 0.0:
        hyperparameters["lr_scheduler_warmup_ratio"] = trainer.args.warmup_ratio
    if trainer.args.warmup_steps != 0.0:
        hyperparameters["lr_scheduler_warmup_steps"] = trainer.args.warmup_steps
    if trainer.args.max_steps != -1:
        hyperparameters["training_steps"] = trainer.args.max_steps
    else:
        hyperparameters["num_epochs"] = trainer.args.num_train_epochs

    if trainer.args.fp16:
890
        if trainer.use_cuda_amp:
Sylvain Gugger's avatar
Sylvain Gugger committed
891
            hyperparameters["mixed_precision_training"] = "Native AMP"
Stas Bekman's avatar
Stas Bekman committed
892
        elif trainer.use_apex:
Sylvain Gugger's avatar
Sylvain Gugger committed
893
894
895
896
897
898
            hyperparameters["mixed_precision_training"] = f"Apex, opt level {trainer.args.fp16_opt_level}"

    if trainer.args.label_smoothing_factor != 0.0:
        hyperparameters["label_smoothing_factor"] = trainer.args.label_smoothing_factor

    return hyperparameters