Skip to content

configuration_utils

mindnlp.transformers.configuration_utils.PretrainedConfig

Abstract class for Pretrained models config.

Source code in mindnlp/transformers/configuration_utils.py
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
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
671
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
@jit_class
class PretrainedConfig:
    """
    Abstract class for Pretrained models config.
    """
    is_composition = False
    # Add for handle attribute_map
    attribute_map: Dict[str, str] = {}

    def __init__(self, **kwargs):
        '''
        This method initializes an instance of the PretrainedConfig class.

        Args:
            self: The instance of the PretrainedConfig class.

        Returns:
            None

        Raises:
            ValueError: If label2id is not a dictionary.
            ValueError: If id2label is not a dictionary.
            ValueError: If num_labels is provided and is incompatible with the id to label map.
            ValueError: If problem_type is not one of the allowed types: 'regression', 'single_label_classification',
                'multi_label_classification'.
            AttributeError: If any attribute cannot be set for the instance.
        '''
        self.ms_dtype = kwargs.pop("ms_dtype", None)
        if 'torch_dtype' in kwargs:
            self.ms_dtype = kwargs.pop("torch_dtype", None)
        self.return_dict = kwargs.pop("return_dict", True)
        self.output_hidden_states = kwargs.pop("output_hidden_states", False)
        self.output_attentions = kwargs.pop("output_attentions", False)

        self.pruned_heads = kwargs.pop("pruned_heads", {})
        self.tie_word_embeddings = kwargs.pop(
            "tie_word_embeddings", True
        )  # Whether input and output word embeddings should be tied for all MLM, LM and Seq2Seq models.

        # Is decoder is used in encoder-decoder models to differentiate encoder from decoder
        self.is_encoder_decoder = kwargs.pop("is_encoder_decoder", False)
        self.is_decoder = kwargs.pop("is_decoder", False)
        self.cross_attention_hidden_size = kwargs.pop("cross_attention_hidden_size", None)
        self.add_cross_attention = kwargs.pop("add_cross_attention", False)
        self.tie_encoder_decoder = kwargs.pop("tie_encoder_decoder", False)

       # Parameters for sequence generation
        self.max_length = kwargs.pop("max_length", 20)
        self.min_length = kwargs.pop("min_length", 0)
        self.do_sample = kwargs.pop("do_sample", False)
        self.early_stopping = kwargs.pop("early_stopping", False)
        self.num_beams = kwargs.pop("num_beams", 1)
        self.num_beam_groups = kwargs.pop("num_beam_groups", 1)
        self.diversity_penalty = kwargs.pop("diversity_penalty", 0.0)
        self.temperature = kwargs.pop("temperature", 1.0)
        self.top_k = kwargs.pop("top_k", 50)
        self.top_p = kwargs.pop("top_p", 1.0)
        self.typical_p = kwargs.pop("typical_p", 1.0)
        self.repetition_penalty = kwargs.pop("repetition_penalty", 1.0)
        self.length_penalty = kwargs.pop("length_penalty", 1.0)
        self.no_repeat_ngram_size = kwargs.pop("no_repeat_ngram_size", 0)
        self.encoder_no_repeat_ngram_size = kwargs.pop("encoder_no_repeat_ngram_size", 0)
        self.bad_words_ids = kwargs.pop("bad_words_ids", None)
        self.num_return_sequences = kwargs.pop("num_return_sequences", 1)
        self.chunk_size_feed_forward = kwargs.pop("chunk_size_feed_forward", 0)
        self.output_scores = kwargs.pop("output_scores", False)
        self.return_dict_in_generate = kwargs.pop("return_dict_in_generate", False)
        self.forced_bos_token_id = kwargs.pop("forced_bos_token_id", None)
        self.forced_eos_token_id = kwargs.pop("forced_eos_token_id", None)
        self.remove_invalid_values = kwargs.pop("remove_invalid_values", False)
        self.exponential_decay_length_penalty = kwargs.pop("exponential_decay_length_penalty", None)
        self.suppress_tokens = kwargs.pop("suppress_tokens", None)
        self.begin_suppress_tokens = kwargs.pop("begin_suppress_tokens", None)

        # Fine-tuning task arguments
        self.architectures = kwargs.pop("architectures", None)
        self.finetuning_task = kwargs.pop("finetuning_task", None)
        self.id2label = kwargs.pop("id2label", None)
        self.label2id = kwargs.pop("label2id", None)
        if self.label2id is not None and not isinstance(self.label2id, dict):
            raise ValueError("Argument label2id should be a dictionary.")
        if self.id2label is not None:
            if not isinstance(self.id2label, dict):
                raise ValueError("Argument id2label should be a dictionary.")
            num_labels = kwargs.pop("num_labels", None)

            if num_labels is not None and len(self.id2label) != num_labels:
                logger.warning(
                    f"You passed along `num_labels={num_labels}` with an incompatible id to label map: "
                    f"{self.id2label}. The number of labels wil be overwritten to {self.num_labels}."
                )
            self.id2label = {int(key): value for key, value in self.id2label.items()}
            # Keys are always strings in JSON so convert ids to int here.
        else:
            self.num_labels = kwargs.pop("num_labels", 2)

        if self.ms_dtype is not None and isinstance(self.ms_dtype, str) and self.ms_dtype != 'none':
            if is_mindspore_available():
                import mindspore

                self.ms_dtype = getattr(mindspore, self.ms_dtype)

        # Tokenizer arguments TODO: eventually tokenizer and models should share the same config
        self.tokenizer_class = kwargs.pop("tokenizer_class", None)
        self.prefix = kwargs.pop("prefix", None)
        self.bos_token_id = kwargs.pop("bos_token_id", None)
        self.pad_token_id = kwargs.pop("pad_token_id", None)
        self.eos_token_id = kwargs.pop("eos_token_id", None)
        self.sep_token_id = kwargs.pop("sep_token_id", None)

        self.decoder_start_token_id = kwargs.pop("decoder_start_token_id", None)

        # task specific arguments
        self.task_specific_params = kwargs.pop("task_specific_params", None)

        # regression / multi-label classification
        self.problem_type = kwargs.pop("problem_type", None)
        allowed_problem_types = ("regression", "single_label_classification", "multi_label_classification")
        if self.problem_type is not None and self.problem_type not in allowed_problem_types:
            raise ValueError(
                f"The config parameter `problem_type` was not understood: received {self.problem_type} "
                "but only 'regression', 'single_label_classification' and 'multi_label_classification' are valid."
            )

        # Name or path to the pretrained checkpoint
        self._name_or_path = str(kwargs.pop("name_or_path", ""))

        # Additional attributes without default values
        for key, value in kwargs.items():
            try:
                setattr(self, key, value)
            except AttributeError as err:
                logger.error(f"Can't set {key} with value {value} for {self}")
                raise err

    def __setattr__(self, key, value):
        """
        Method to set attribute values in the PretrainedConfig class.

        Args:
            self (PretrainedConfig): The instance of the PretrainedConfig class.
            key (str): The key representing the attribute to be set.
            value (any): The value to be assigned to the attribute specified by the key.

        Returns:
            None.

        Raises:
            AttributeError: If the key is not found in the attribute map of the parent class.
        """
        if key in super().__getattribute__("attribute_map"):
            key = super().__getattribute__("attribute_map")[key]
        super().__setattr__(key, value)

    def __getattribute__(self, key):
        """
        This method __getattribute__ in the class PretrainedConfig dynamically retrieves the value of the
        specified attribute key.

        Args:
            self (object): The instance of the PretrainedConfig class.
            key (str): The key for which the attribute value needs to be retrieved.
                It should be a string representing the attribute name.

        Returns:
            None: This method returns the attribute value corresponding to the provided key.
                If the key is found in the attribute map, it returns the mapped value; otherwise, it returns None.

        Raises:
            AttributeError: If the key is not found in the attribute map or the attribute itself does not exist.
            TypeError: If the key provided is not a string.
        """
        if key != "attribute_map" and key in super().__getattribute__("attribute_map"):
            key = super().__getattribute__("attribute_map")[key]
        return super().__getattribute__(key)

    @property
    def name_or_path(self) -> str:
        """get name_or_path"""
        return getattr(self, "_name_or_path", None)

    @name_or_path.setter
    def name_or_path(self, value):
        """set name_or_path"""
        self._name_or_path = str(value)  # Make sure that name_or_path is a string (for JSON encoding)

    @classmethod
    def from_json(cls, file_path):
        """load config from json."""
        with open(file_path, "r", encoding="utf-8") as file:
            text = file.read()
        config_map = json.loads(text)
        config = cls()
        for key, value in config_map.items():
            setattr(config, key, value)
        return config

    @classmethod
    def from_json_file(cls, json_file):
        """Constructs a `Config` from a json file of parameters."""
        with open(json_file, "r", encoding="utf-8") as reader:
            text = reader.read()
        dict_obj = json.loads(text)
        return cls(**dict_obj)

    @classmethod
    def load(cls, pretrained_model_name_or_path):
        """load config."""
        return cls.from_pretrained(pretrained_model_name_or_path)

    @property
    def use_return_dict(self) -> bool:
        """
        `bool`: Whether or not return [`~utils.ModelOutput`] instead of tuples.
        """
        # If torchscript is set, force `return_dict=False` to avoid jit errors
        return self.return_dict

    @classmethod
    def from_dict(cls, config_dict: Dict, **kwargs) -> "PretrainedConfig":
        """
        Constructs a `Config` from a Python dictionary of parameters.

        Args:
            config_dict (:obj:`Dict[str, any]`):
                Dictionary that will be used to instantiate the configuration object. Such a dictionary can be retrieved
                from a pre-trained checkpoint by leveraging the :func:`~transformers.PretrainedConfig.get_config_dict`
                method.
            kwargs (:obj:`Dict[str, any]`):
                Additional parameters from which to initialize the configuration object.

        Returns:
            :class:`PretrainedConfig`: An instance of a configuration object
        """
        return_unused_kwargs = kwargs.pop("return_unused_kwargs", False)

        config = cls(**config_dict)

        if hasattr(config, "pruned_heads"):
            config.pruned_heads = dict((int(key), value) for key, value in config.pruned_heads.items())

        # Update config with kwargs if needed
        if "num_labels" in kwargs and "id2label" in kwargs:
            num_labels = kwargs["num_labels"]
            id2label = kwargs["id2label"] if kwargs["id2label"] is not None else []
            if len(id2label) != num_labels:
                raise ValueError(
                    f"You passed along `num_labels={num_labels }` with an incompatible id to label map: "
                    f"{kwargs['id2label']}. Since those arguments are inconsistent with each other, you should remove "
                    "one of them.")

        to_remove = []
        for key, value in kwargs.items():
            if hasattr(config, key):
                setattr(config, key, value)
                to_remove.append(key)
        for key in to_remove:
            kwargs.pop(key, None)

        logger.info("Model config %s", str(config))
        if return_unused_kwargs:
            return config, kwargs
        return config

    @classmethod
    def from_pretrained(
        cls,
        pretrained_model_name_or_path: Union[str, os.PathLike],
        cache_dir: Optional[Union[str, os.PathLike]] = None,
        force_download: bool = False,
        local_files_only: bool = False,
        mirror: str = 'huggingface',
        **kwargs,
    ) -> "PretrainedConfig":
        r"""
        Instantiate a [`PretrainedConfig`] (or a derived class) from a pretrained model configuration.

        Args:
            pretrained_model_name_or_path (`str` or `os.PathLike`):
                This can be either:

                - a string, the *model id* of a pretrained model configuration hosted inside a model repo on
                  hf-mirror.com. 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`.
                - a path to a *directory* containing a configuration file saved using the
                  [`~PretrainedConfig.save_pretrained`] method, e.g., `./my_model_directory/`.
                - a path or url to a saved configuration JSON *file*, e.g., `./my_model_directory/configuration.json`.
            cache_dir (`str` or `os.PathLike`, *optional*):
                Path to a directory in which a downloaded pretrained model configuration should be cached if the
                standard cache should not be used.
            force_download (`bool`, *optional*, defaults to `False`):
                Whether or not to force to (re-)download the configuration files and override the cached versions if
                they exist.
            resume_download (`bool`, *optional*, defaults to `False`):
                Whether or not to delete incompletely received file. Attempts to resume the download if such a file
                exists.
            proxies (`Dict[str, str]`, *optional*):
                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.
            token (`str` or `bool`, *optional*):
                The token to use as HTTP bearer authorization for remote files. If `True`, or not specified, will use
                the token generated when running `huggingface-cli login` (stored in `~/.huggingface`).
            revision (`str`, *optional*, defaults to `"main"`):
                The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a
                git-based system for storing models and other artifacts on hf-mirror.com, so `revision` can be any
                identifier allowed by git.

                <Tip>

                To test a pull request you made on the Hub, you can pass `revision="refs/pr/<pr_number>".

                </Tip>

            return_unused_kwargs (`bool`, *optional*, defaults to `False`):
                - If `False`, then this function returns just the final configuration object.
                - If `True`, then this functions returns a `Tuple(config, unused_kwargs)` where *unused_kwargs* is a
                dictionary consisting of the key/value pairs whose keys are not configuration attributes: i.e., the
                part of `kwargs` which has not been used to update `config` and is otherwise ignored.
            subfolder (`str`, *optional*, defaults to `""`):
                In case the relevant files are located inside a subfolder of the model repo on hf-mirror.com, you can
                specify the folder name here.
            kwargs (`Dict[str, Any]`, *optional*):
                The values in kwargs of any keys which are configuration attributes will be used to override the loaded
                values. Behavior concerning key/value pairs whose keys are *not* configuration attributes is controlled
                by the `return_unused_kwargs` keyword parameter.

        Returns:
            [`PretrainedConfig`]: The configuration object instantiated from this pretrained model.

        Example:
            ```python
            >>> # We can't instantiate directly the base class *PretrainedConfig* so let's show the examples on a
            >>> # derived class: BertConfig
            >>> config = BertConfig.from_pretrained(
            >>>     "bert-base-uncased"
            >>> )  # Download configuration from hf-mirror.com and cache.
            >>> config = BertConfig.from_pretrained(
            >>>     "./test/saved_model/"
            >>> )  # E.g. config (or model) was saved using *save_pretrained('./test/saved_model/')*
            >>> config = BertConfig.from_pretrained("./test/saved_model/my_configuration.json")
            >>> config = BertConfig.from_pretrained("bert-base-uncased", output_attentions=True, foo=False)
            >>> assert config.output_attentions == True
            >>> config, unused_kwargs = BertConfig.from_pretrained(
            >>>     "bert-base-uncased", output_attentions=True, foo=False, return_unused_kwargs=True
            >>> )
            >>> assert config.output_attentions == True
            >>> assert unused_kwargs == {"foo": False}
            ```
        """
        kwargs["cache_dir"] = cache_dir
        kwargs["force_download"] = force_download
        kwargs["local_files_only"] = local_files_only
        kwargs['mirror'] = mirror

        config_dict, kwargs = cls.get_config_dict(pretrained_model_name_or_path, **kwargs)
        if "model_type" in config_dict and hasattr(cls, "model_type") and config_dict["model_type"] != cls.model_type:
            logger.warning(
                f"You are using a model of type {config_dict['model_type']} to instantiate a model of type "
                f"{cls.model_type}. This is not supported for all configurations of models and can yield errors."
            )

        return cls.from_dict(config_dict, **kwargs)

    @classmethod
    def get_config_dict(
        cls, pretrained_model_name_or_path: Union[str, os.PathLike], **kwargs
    ) -> Tuple[Dict[str, Any], Dict[str, Any]]:
        """
        From a `pretrained_model_name_or_path`, resolve to a dictionary of parameters, to be used for instantiating a
        [`PretrainedConfig`] using `from_dict`.

        Parameters:
            pretrained_model_name_or_path (`str` or `os.PathLike`):
                The identifier of the pre-trained checkpoint from which we want the dictionary of parameters.

        Returns:
            `Tuple[Dict, Dict]`: The dictionary(ies) that will be used to instantiate the configuration object.

        """
        original_kwargs = copy.deepcopy(kwargs)
        # Get config dict associated with the base config file
        config_dict, kwargs = cls._get_config_dict(pretrained_model_name_or_path, **kwargs)

        # That config file may point us toward another config file to use.
        if "configuration_files" in config_dict:
            configuration_file = get_configuration_file(config_dict["configuration_files"])
            config_dict, kwargs = cls._get_config_dict(
                pretrained_model_name_or_path, _configuration_file=configuration_file, **original_kwargs
            )

        return config_dict, kwargs

    @classmethod
    def _get_config_dict(
        cls, pretrained_model_name_or_path: Union[str, os.PathLike], **kwargs
    ) -> Tuple[Dict[str, Any], Dict[str, Any]]:
        """
        From a `pretrained_model_name_or_path`, resolve to a dictionary of parameters, to be used
        for instantiating a Config using `from_dict`.

        Parameters:
            pretrained_model_name_or_path (:obj:`string`):
                The identifier of the pre-trained checkpoint from which we want the dictionary of parameters.

        Returns:
            :obj:`Tuple[Dict, Dict]`: The dictionary that will be used to instantiate the configuration object.

        """
        cache_dir = kwargs.pop("cache_dir", None)
        force_download = kwargs.pop("force_download", False)
        resume_download = kwargs.pop("resume_download", False)
        proxies = kwargs.pop("proxies", None)
        local_files_only = kwargs.pop("local_files_only", False)
        subfolder = kwargs.pop("subfolder", "")
        token = kwargs.pop('token', None)
        revision = kwargs.pop('revision', 'main')
        mirror = kwargs.pop('mirror', 'huggingface')

        pretrained_model_name_or_path = str(pretrained_model_name_or_path)

        is_local = os.path.isdir(pretrained_model_name_or_path)
        if os.path.isfile(os.path.join(subfolder, pretrained_model_name_or_path)):
            # Special case when pretrained_model_name_or_path is a local file
            resolved_config_file = pretrained_model_name_or_path
            is_local = True

        elif is_remote_url(pretrained_model_name_or_path):
            configuration_file = pretrained_model_name_or_path
            resolved_config_file = download_url(pretrained_model_name_or_path)

        else:
            configuration_file = kwargs.pop("_configuration_file", CONFIG_NAME)

            try:
                # Load from local folder or from cache or download from model Hub and cache
                resolved_config_file = cached_file(
                    pretrained_model_name_or_path,
                    configuration_file,
                    cache_dir=cache_dir,
                    force_download=force_download,
                    proxies=proxies,
                    resume_download=resume_download,
                    local_files_only=local_files_only,
                    revision=revision,
                    token=token,
                    subfolder=subfolder,
                    mirror=mirror
                )
            except EnvironmentError:
                # Raise any environment error raise by `cached_file`. It will have a helpful error message adapted to
                # the original exception.
                raise
            except Exception as exc:
                # For any other exception, we throw a generic error.
                raise EnvironmentError(
                    f"Can't load the configuration of '{pretrained_model_name_or_path}'. If you were trying to load it"
                    ", make sure you don't have a local directory with the same"
                    f" name. Otherwise, make sure '{pretrained_model_name_or_path}' is the correct path to a directory"
                    f" containing a {configuration_file} file"
                ) from exc

        try:
            # Load config dict
            config_dict = cls._dict_from_json_file(resolved_config_file)
        except (json.JSONDecodeError, UnicodeDecodeError) as exc:
            raise EnvironmentError(
                f"It looks like the config file at '{resolved_config_file}' is not a valid JSON file."
            ) from exc

        if is_local:
            logger.info(f"loading configuration file {resolved_config_file}")
        else:
            logger.info(f"loading configuration file {configuration_file} from cache at {resolved_config_file}")

        return config_dict, kwargs

    @classmethod
    def _dict_from_json_file(cls, json_file: str):
        """_dict_from_json_file"""
        with open(json_file, "r", encoding="utf-8") as reader:
            text = reader.read()
        return json.loads(text)

    def dict_ms_dtype_to_str(self, d: Dict[str, Any]) -> None:
        """
        Checks whether the passed dictionary and its nested dicts have a *torch_dtype* key and if it's not None,
        converts torch.dtype to a string of just the type. For example, `torch.float32` get converted into *"float32"*
        string, which can then be stored in the json format.
        """
        if d.get("ms_dtype", None) is not None and not isinstance(d["ms_dtype"], str):
            d["ms_dtype"] = str(d["ms_dtype"]).lower()
        for value in d.values():
            if isinstance(value, dict):
                self.dict_ms_dtype_to_str(value)

    def to_dict(self) -> Dict[str, Any]:
        """
        Serializes this instance to a Python dictionary.

        Returns:
            `Dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance.
        """
        output = copy.deepcopy(self.__dict__)
        if hasattr(self.__class__, "model_type"):
            output["model_type"] = self.__class__.model_type
        if "_auto_class" in output:
            del output["_auto_class"]
        if "_commit_hash" in output:
            del output["_commit_hash"]
        if "_attn_implementation_internal" in output:
            del output["_attn_implementation_internal"]

        for key, value in output.items():
            # Deal with nested configs like CLIP
            if isinstance(value, PretrainedConfig):
                value = value.to_dict()

            output[key] = value

        if hasattr(self, "quantization_config"):
            output["quantization_config"] = (
                self.quantization_config.to_dict()
                if not isinstance(self.quantization_config, dict)
                else self.quantization_config
            )

            # pop the `_pre_quantization_dtype` as torch.dtypes are not serializable.
            _ = output.pop("_pre_quantization_dtype", None)

        self.dict_ms_dtype_to_str(output)

        return output

    def to_diff_dict(self) -> Dict[str, Any]:
        """
        Removes all attributes from config which correspond to the default config attributes for better readability and
        serializes to a Python dictionary.

        Returns:
            `Dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance,
        """
        config_dict = self.to_dict()

        # get the default config dict
        default_config_dict = PretrainedConfig().to_dict()

        # get class specific config dict
        class_config_dict = self.__class__().to_dict() if not self.is_composition else {}

        serializable_config_dict = {}

        # only serialize values that differ from the default config
        for key, value in config_dict.items():
            if (
                isinstance(getattr(self, key, None), PretrainedConfig)
                and key in class_config_dict
                and isinstance(class_config_dict[key], dict)
            ):
                # For nested configs we need to clean the diff recursively
                diff = recursive_diff_dict(value, class_config_dict[key], config_obj=getattr(self, key, None))
                if "model_type" in value:
                    # Needs to be set even if it's not in the diff
                    diff["model_type"] = value["model_type"]
                if len(diff) > 0:
                    serializable_config_dict[key] = diff
            elif (
                key not in default_config_dict
                or value != default_config_dict[key]
                or (key in class_config_dict and value != class_config_dict[key])
            ):
                serializable_config_dict[key] = value

        return serializable_config_dict

    def to_json_string(self, use_diff: bool = True) -> str:
        """
        Serializes this instance to a JSON string.

        Args:
            use_diff (`bool`, *optional*, defaults to `True`):
                If set to `True`, only the difference between the config instance and the default `PretrainedConfig()`
                is serialized to JSON string.

        Returns:
            `str`: String containing all the attributes that make up this configuration instance in JSON format.
        """
        if use_diff is True:
            config_dict = self.to_diff_dict()
        else:
            config_dict = self.to_dict()

        return json.dumps(config_dict, indent=2, sort_keys=True) + "\n"

    def to_file(self, save_path):
        """Serializes this instance to a JSON file."""
        output_dict = self.to_dict()
        with open(os.path.join(save_path, 'config.json'), encoding='utf-8') as f:
            json.dump(output_dict, f, sort_keys=True, indent=2)

    def update(self, config_dict: Dict[str, Any]):
        """
        Updates attributes of this class with attributes from `config_dict`.

        Args:
            config_dict (`Dict[str, Any]`): Dictionary of attributes that should be updated for this class.
        """
        for key, value in config_dict.items():
            setattr(self, key, value)

    def save_pretrained(self, save_directory: Union[str, os.PathLike], **kwargs):
        """
        Save a configuration object to the directory `save_directory`, so that it can be re-loaded using the
        [`~PretrainedConfig.from_pretrained`] class method.

        Args:
            save_directory (`str` or `os.PathLike`):
                Directory where the configuration JSON file will be saved (will be created if it does not exist).
            push_to_hub (`bool`, *optional*, defaults to `False`):
                Whether or not to push your model to the Hugging Face model hub after saving it. You can specify the
                repository you want to push to with `repo_id` (will default to the name of `save_directory` in your
                namespace).
            kwargs (`Dict[str, Any]`, *optional*):
                Additional key word arguments passed along to the [`~utils.PushToHubMixin.push_to_hub`] method.
        """
        if os.path.isfile(save_directory):
            raise AssertionError(f"Provided path ({save_directory}) should be a directory, not a file")

        os.makedirs(save_directory, exist_ok=True)

        # If we save using the predefined names, we can load using `from_pretrained`
        output_config_file = os.path.join(save_directory, CONFIG_NAME)

        self.to_json_file(output_config_file, use_diff=True)
        logger.info(f"Configuration saved in {output_config_file}")

    def to_json_file(self, json_file_path: Union[str, os.PathLike], use_diff: bool = True):
        """
        Save this instance to a JSON file.

        Args:
            json_file_path (`str` or `os.PathLike`):
                Path to the JSON file in which this configuration instance's parameters will be saved.
            use_diff (`bool`, *optional*, defaults to `True`):
                If set to `True`, only the difference between the config instance and the default `PretrainedConfig()`
                is serialized to JSON file.
        """
        with open(json_file_path, "w", encoding="utf-8") as writer:
            writer.write(self.to_json_string(use_diff=use_diff))

    @property
    def num_labels(self) -> int:
        """
        `int`: The number of labels for classification models.
        """
        return len(self.id2label)

    @num_labels.setter
    def num_labels(self, num_labels: int):
        """
        Method to set the number of labels in the PretrainedConfig class.

        Args:
            self (PretrainedConfig): The instance of the PretrainedConfig class.
            num_labels (int): The number of labels to set for the configuration.
                Must be a non-negative integer representing the total number of labels to be used.

        Returns:
            None: This method does not return any value.

        Raises:
            TypeError: If the num_labels parameter is not of type int.
            ValueError: If the num_labels parameter is a negative integer.
            AttributeError: If the id2label attribute is not present in the instance or is None.
            ValueError: If the length of the id2label attribute is not equal to the specified num_labels.
        """
        if not hasattr(self, "id2label") or self.id2label is None or len(self.id2label) != num_labels:
            self.id2label = {i: f"LABEL_{i}" for i in range(num_labels)}
            self.label2id = dict(zip(self.id2label.values(), self.id2label.keys()))

    @property
    def _attn_implementation(self):
        """
        Returns the implementation type of the attention mechanism.

        Args:
            self: An instance of the PretrainedConfig class.

        Returns:
            str:
                The implementation type of the attention mechanism. Possible values are:

                - 'eager': If the '_attn_implementation_internal' attribute is set to None or not defined.
                - The value of '_attn_implementation_internal': If it is not None.

        Raises:
            None.

        """
        # This property is made private for now (as it cannot be changed and a PreTrainedModel.use_attn_implementation method needs to be implemented.)
        if hasattr(self, "_attn_implementation_internal"):
            if self._attn_implementation_internal is None:
                # `config.attn_implementation` should never be None, for backward compatibility.
                return "eager"
            else:
                return self._attn_implementation_internal
        else:
            return "eager"

    @_attn_implementation.setter
    def _attn_implementation(self, value):
        """
        This method '_attn_implementation' in the class 'PretrainedConfig' sets the value of the
        '_attn_implementation_internal' attribute.

        Args:
            self (object): The instance of the PretrainedConfig class.
            value (any): The value to be assigned to the '_attn_implementation_internal' attribute.

        Returns:
            None.

        Raises:
            None.
        """
        self._attn_implementation_internal = value

mindnlp.transformers.configuration_utils.PretrainedConfig.name_or_path: str property writable

get name_or_path

mindnlp.transformers.configuration_utils.PretrainedConfig.num_labels: int property writable

int: The number of labels for classification models.

mindnlp.transformers.configuration_utils.PretrainedConfig.use_return_dict: bool property

bool: Whether or not return [~utils.ModelOutput] instead of tuples.

mindnlp.transformers.configuration_utils.PretrainedConfig.__getattribute__(key)

This method getattribute in the class PretrainedConfig dynamically retrieves the value of the specified attribute key.

PARAMETER DESCRIPTION
self

The instance of the PretrainedConfig class.

TYPE: object

key

The key for which the attribute value needs to be retrieved. It should be a string representing the attribute name.

TYPE: str

RETURNS DESCRIPTION
None

This method returns the attribute value corresponding to the provided key. If the key is found in the attribute map, it returns the mapped value; otherwise, it returns None.

RAISES DESCRIPTION
AttributeError

If the key is not found in the attribute map or the attribute itself does not exist.

TypeError

If the key provided is not a string.

Source code in mindnlp/transformers/configuration_utils.py
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
def __getattribute__(self, key):
    """
    This method __getattribute__ in the class PretrainedConfig dynamically retrieves the value of the
    specified attribute key.

    Args:
        self (object): The instance of the PretrainedConfig class.
        key (str): The key for which the attribute value needs to be retrieved.
            It should be a string representing the attribute name.

    Returns:
        None: This method returns the attribute value corresponding to the provided key.
            If the key is found in the attribute map, it returns the mapped value; otherwise, it returns None.

    Raises:
        AttributeError: If the key is not found in the attribute map or the attribute itself does not exist.
        TypeError: If the key provided is not a string.
    """
    if key != "attribute_map" and key in super().__getattribute__("attribute_map"):
        key = super().__getattribute__("attribute_map")[key]
    return super().__getattribute__(key)

mindnlp.transformers.configuration_utils.PretrainedConfig.__init__(**kwargs)

This method initializes an instance of the PretrainedConfig class.

PARAMETER DESCRIPTION
self

The instance of the PretrainedConfig class.

RETURNS DESCRIPTION

None

RAISES DESCRIPTION
ValueError

If label2id is not a dictionary.

ValueError

If id2label is not a dictionary.

ValueError

If num_labels is provided and is incompatible with the id to label map.

ValueError

If problem_type is not one of the allowed types: 'regression', 'single_label_classification', 'multi_label_classification'.

AttributeError

If any attribute cannot be set for the instance.

Source code in mindnlp/transformers/configuration_utils.py
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
def __init__(self, **kwargs):
    '''
    This method initializes an instance of the PretrainedConfig class.

    Args:
        self: The instance of the PretrainedConfig class.

    Returns:
        None

    Raises:
        ValueError: If label2id is not a dictionary.
        ValueError: If id2label is not a dictionary.
        ValueError: If num_labels is provided and is incompatible with the id to label map.
        ValueError: If problem_type is not one of the allowed types: 'regression', 'single_label_classification',
            'multi_label_classification'.
        AttributeError: If any attribute cannot be set for the instance.
    '''
    self.ms_dtype = kwargs.pop("ms_dtype", None)
    if 'torch_dtype' in kwargs:
        self.ms_dtype = kwargs.pop("torch_dtype", None)
    self.return_dict = kwargs.pop("return_dict", True)
    self.output_hidden_states = kwargs.pop("output_hidden_states", False)
    self.output_attentions = kwargs.pop("output_attentions", False)

    self.pruned_heads = kwargs.pop("pruned_heads", {})
    self.tie_word_embeddings = kwargs.pop(
        "tie_word_embeddings", True
    )  # Whether input and output word embeddings should be tied for all MLM, LM and Seq2Seq models.

    # Is decoder is used in encoder-decoder models to differentiate encoder from decoder
    self.is_encoder_decoder = kwargs.pop("is_encoder_decoder", False)
    self.is_decoder = kwargs.pop("is_decoder", False)
    self.cross_attention_hidden_size = kwargs.pop("cross_attention_hidden_size", None)
    self.add_cross_attention = kwargs.pop("add_cross_attention", False)
    self.tie_encoder_decoder = kwargs.pop("tie_encoder_decoder", False)

   # Parameters for sequence generation
    self.max_length = kwargs.pop("max_length", 20)
    self.min_length = kwargs.pop("min_length", 0)
    self.do_sample = kwargs.pop("do_sample", False)
    self.early_stopping = kwargs.pop("early_stopping", False)
    self.num_beams = kwargs.pop("num_beams", 1)
    self.num_beam_groups = kwargs.pop("num_beam_groups", 1)
    self.diversity_penalty = kwargs.pop("diversity_penalty", 0.0)
    self.temperature = kwargs.pop("temperature", 1.0)
    self.top_k = kwargs.pop("top_k", 50)
    self.top_p = kwargs.pop("top_p", 1.0)
    self.typical_p = kwargs.pop("typical_p", 1.0)
    self.repetition_penalty = kwargs.pop("repetition_penalty", 1.0)
    self.length_penalty = kwargs.pop("length_penalty", 1.0)
    self.no_repeat_ngram_size = kwargs.pop("no_repeat_ngram_size", 0)
    self.encoder_no_repeat_ngram_size = kwargs.pop("encoder_no_repeat_ngram_size", 0)
    self.bad_words_ids = kwargs.pop("bad_words_ids", None)
    self.num_return_sequences = kwargs.pop("num_return_sequences", 1)
    self.chunk_size_feed_forward = kwargs.pop("chunk_size_feed_forward", 0)
    self.output_scores = kwargs.pop("output_scores", False)
    self.return_dict_in_generate = kwargs.pop("return_dict_in_generate", False)
    self.forced_bos_token_id = kwargs.pop("forced_bos_token_id", None)
    self.forced_eos_token_id = kwargs.pop("forced_eos_token_id", None)
    self.remove_invalid_values = kwargs.pop("remove_invalid_values", False)
    self.exponential_decay_length_penalty = kwargs.pop("exponential_decay_length_penalty", None)
    self.suppress_tokens = kwargs.pop("suppress_tokens", None)
    self.begin_suppress_tokens = kwargs.pop("begin_suppress_tokens", None)

    # Fine-tuning task arguments
    self.architectures = kwargs.pop("architectures", None)
    self.finetuning_task = kwargs.pop("finetuning_task", None)
    self.id2label = kwargs.pop("id2label", None)
    self.label2id = kwargs.pop("label2id", None)
    if self.label2id is not None and not isinstance(self.label2id, dict):
        raise ValueError("Argument label2id should be a dictionary.")
    if self.id2label is not None:
        if not isinstance(self.id2label, dict):
            raise ValueError("Argument id2label should be a dictionary.")
        num_labels = kwargs.pop("num_labels", None)

        if num_labels is not None and len(self.id2label) != num_labels:
            logger.warning(
                f"You passed along `num_labels={num_labels}` with an incompatible id to label map: "
                f"{self.id2label}. The number of labels wil be overwritten to {self.num_labels}."
            )
        self.id2label = {int(key): value for key, value in self.id2label.items()}
        # Keys are always strings in JSON so convert ids to int here.
    else:
        self.num_labels = kwargs.pop("num_labels", 2)

    if self.ms_dtype is not None and isinstance(self.ms_dtype, str) and self.ms_dtype != 'none':
        if is_mindspore_available():
            import mindspore

            self.ms_dtype = getattr(mindspore, self.ms_dtype)

    # Tokenizer arguments TODO: eventually tokenizer and models should share the same config
    self.tokenizer_class = kwargs.pop("tokenizer_class", None)
    self.prefix = kwargs.pop("prefix", None)
    self.bos_token_id = kwargs.pop("bos_token_id", None)
    self.pad_token_id = kwargs.pop("pad_token_id", None)
    self.eos_token_id = kwargs.pop("eos_token_id", None)
    self.sep_token_id = kwargs.pop("sep_token_id", None)

    self.decoder_start_token_id = kwargs.pop("decoder_start_token_id", None)

    # task specific arguments
    self.task_specific_params = kwargs.pop("task_specific_params", None)

    # regression / multi-label classification
    self.problem_type = kwargs.pop("problem_type", None)
    allowed_problem_types = ("regression", "single_label_classification", "multi_label_classification")
    if self.problem_type is not None and self.problem_type not in allowed_problem_types:
        raise ValueError(
            f"The config parameter `problem_type` was not understood: received {self.problem_type} "
            "but only 'regression', 'single_label_classification' and 'multi_label_classification' are valid."
        )

    # Name or path to the pretrained checkpoint
    self._name_or_path = str(kwargs.pop("name_or_path", ""))

    # Additional attributes without default values
    for key, value in kwargs.items():
        try:
            setattr(self, key, value)
        except AttributeError as err:
            logger.error(f"Can't set {key} with value {value} for {self}")
            raise err

mindnlp.transformers.configuration_utils.PretrainedConfig.__setattr__(key, value)

Method to set attribute values in the PretrainedConfig class.

PARAMETER DESCRIPTION
self

The instance of the PretrainedConfig class.

TYPE: PretrainedConfig

key

The key representing the attribute to be set.

TYPE: str

value

The value to be assigned to the attribute specified by the key.

TYPE: any

RETURNS DESCRIPTION

None.

RAISES DESCRIPTION
AttributeError

If the key is not found in the attribute map of the parent class.

Source code in mindnlp/transformers/configuration_utils.py
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
def __setattr__(self, key, value):
    """
    Method to set attribute values in the PretrainedConfig class.

    Args:
        self (PretrainedConfig): The instance of the PretrainedConfig class.
        key (str): The key representing the attribute to be set.
        value (any): The value to be assigned to the attribute specified by the key.

    Returns:
        None.

    Raises:
        AttributeError: If the key is not found in the attribute map of the parent class.
    """
    if key in super().__getattribute__("attribute_map"):
        key = super().__getattribute__("attribute_map")[key]
    super().__setattr__(key, value)

mindnlp.transformers.configuration_utils.PretrainedConfig.dict_ms_dtype_to_str(d)

Checks whether the passed dictionary and its nested dicts have a torch_dtype key and if it's not None, converts torch.dtype to a string of just the type. For example, torch.float32 get converted into "float32" string, which can then be stored in the json format.

Source code in mindnlp/transformers/configuration_utils.py
515
516
517
518
519
520
521
522
523
524
525
def dict_ms_dtype_to_str(self, d: Dict[str, Any]) -> None:
    """
    Checks whether the passed dictionary and its nested dicts have a *torch_dtype* key and if it's not None,
    converts torch.dtype to a string of just the type. For example, `torch.float32` get converted into *"float32"*
    string, which can then be stored in the json format.
    """
    if d.get("ms_dtype", None) is not None and not isinstance(d["ms_dtype"], str):
        d["ms_dtype"] = str(d["ms_dtype"]).lower()
    for value in d.values():
        if isinstance(value, dict):
            self.dict_ms_dtype_to_str(value)

mindnlp.transformers.configuration_utils.PretrainedConfig.from_dict(config_dict, **kwargs) classmethod

Constructs a Config from a Python dictionary of parameters.

PARAMETER DESCRIPTION
config_dict

Dictionary that will be used to instantiate the configuration object. Such a dictionary can be retrieved from a pre-trained checkpoint by leveraging the :func:~transformers.PretrainedConfig.get_config_dict method.

kwargs

Additional parameters from which to initialize the configuration object.

DEFAULT: {}

RETURNS DESCRIPTION
PretrainedConfig
Source code in mindnlp/transformers/configuration_utils.py
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
@classmethod
def from_dict(cls, config_dict: Dict, **kwargs) -> "PretrainedConfig":
    """
    Constructs a `Config` from a Python dictionary of parameters.

    Args:
        config_dict (:obj:`Dict[str, any]`):
            Dictionary that will be used to instantiate the configuration object. Such a dictionary can be retrieved
            from a pre-trained checkpoint by leveraging the :func:`~transformers.PretrainedConfig.get_config_dict`
            method.
        kwargs (:obj:`Dict[str, any]`):
            Additional parameters from which to initialize the configuration object.

    Returns:
        :class:`PretrainedConfig`: An instance of a configuration object
    """
    return_unused_kwargs = kwargs.pop("return_unused_kwargs", False)

    config = cls(**config_dict)

    if hasattr(config, "pruned_heads"):
        config.pruned_heads = dict((int(key), value) for key, value in config.pruned_heads.items())

    # Update config with kwargs if needed
    if "num_labels" in kwargs and "id2label" in kwargs:
        num_labels = kwargs["num_labels"]
        id2label = kwargs["id2label"] if kwargs["id2label"] is not None else []
        if len(id2label) != num_labels:
            raise ValueError(
                f"You passed along `num_labels={num_labels }` with an incompatible id to label map: "
                f"{kwargs['id2label']}. Since those arguments are inconsistent with each other, you should remove "
                "one of them.")

    to_remove = []
    for key, value in kwargs.items():
        if hasattr(config, key):
            setattr(config, key, value)
            to_remove.append(key)
    for key in to_remove:
        kwargs.pop(key, None)

    logger.info("Model config %s", str(config))
    if return_unused_kwargs:
        return config, kwargs
    return config

mindnlp.transformers.configuration_utils.PretrainedConfig.from_json(file_path) classmethod

load config from json.

Source code in mindnlp/transformers/configuration_utils.py
218
219
220
221
222
223
224
225
226
227
@classmethod
def from_json(cls, file_path):
    """load config from json."""
    with open(file_path, "r", encoding="utf-8") as file:
        text = file.read()
    config_map = json.loads(text)
    config = cls()
    for key, value in config_map.items():
        setattr(config, key, value)
    return config

mindnlp.transformers.configuration_utils.PretrainedConfig.from_json_file(json_file) classmethod

Constructs a Config from a json file of parameters.

Source code in mindnlp/transformers/configuration_utils.py
229
230
231
232
233
234
235
@classmethod
def from_json_file(cls, json_file):
    """Constructs a `Config` from a json file of parameters."""
    with open(json_file, "r", encoding="utf-8") as reader:
        text = reader.read()
    dict_obj = json.loads(text)
    return cls(**dict_obj)

mindnlp.transformers.configuration_utils.PretrainedConfig.from_pretrained(pretrained_model_name_or_path, cache_dir=None, force_download=False, local_files_only=False, mirror='huggingface', **kwargs) classmethod

Instantiate a [PretrainedConfig] (or a derived class) from a pretrained model configuration.

PARAMETER DESCRIPTION
pretrained_model_name_or_path

This can be either:

  • a string, the model id of a pretrained model configuration hosted inside a model repo on hf-mirror.com. 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.
  • a path to a directory containing a configuration file saved using the [~PretrainedConfig.save_pretrained] method, e.g., ./my_model_directory/.
  • a path or url to a saved configuration JSON file, e.g., ./my_model_directory/configuration.json.

TYPE: `str` or `os.PathLike`

cache_dir

Path to a directory in which a downloaded pretrained model configuration should be cached if the standard cache should not be used.

TYPE: `str` or `os.PathLike`, *optional* DEFAULT: None

force_download

Whether or not to force to (re-)download the configuration files and override the cached versions if they exist.

TYPE: `bool`, *optional*, defaults to `False` DEFAULT: False

resume_download

Whether or not to delete incompletely received file. Attempts to resume the download if such a file exists.

TYPE: `bool`, *optional*, defaults to `False`

proxies

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.

TYPE: `Dict[str, str]`, *optional*

token

The token to use as HTTP bearer authorization for remote files. If True, or not specified, will use the token generated when running huggingface-cli login (stored in ~/.huggingface).

TYPE: `str` or `bool`, *optional*

revision

The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a git-based system for storing models and other artifacts on hf-mirror.com, so revision can be any identifier allowed by git.

To test a pull request you made on the Hub, you can pass `revision="refs/pr/".

TYPE: `str`, *optional*, defaults to `"main"`

return_unused_kwargs
  • If False, then this function returns just the final configuration object.
  • If True, then this functions returns a Tuple(config, unused_kwargs) where unused_kwargs is a dictionary consisting of the key/value pairs whose keys are not configuration attributes: i.e., the part of kwargs which has not been used to update config and is otherwise ignored.

TYPE: `bool`, *optional*, defaults to `False`

subfolder

In case the relevant files are located inside a subfolder of the model repo on hf-mirror.com, you can specify the folder name here.

TYPE: `str`, *optional*, defaults to `""`

kwargs

The values in kwargs of any keys which are configuration attributes will be used to override the loaded values. Behavior concerning key/value pairs whose keys are not configuration attributes is controlled by the return_unused_kwargs keyword parameter.

TYPE: `Dict[str, Any]`, *optional* DEFAULT: {}

RETURNS DESCRIPTION
PretrainedConfig

[PretrainedConfig]: The configuration object instantiated from this pretrained model.

Example
>>> # We can't instantiate directly the base class *PretrainedConfig* so let's show the examples on a
>>> # derived class: BertConfig
>>> config = BertConfig.from_pretrained(
>>>     "bert-base-uncased"
>>> )  # Download configuration from hf-mirror.com and cache.
>>> config = BertConfig.from_pretrained(
>>>     "./test/saved_model/"
>>> )  # E.g. config (or model) was saved using *save_pretrained('./test/saved_model/')*
>>> config = BertConfig.from_pretrained("./test/saved_model/my_configuration.json")
>>> config = BertConfig.from_pretrained("bert-base-uncased", output_attentions=True, foo=False)
>>> assert config.output_attentions == True
>>> config, unused_kwargs = BertConfig.from_pretrained(
>>>     "bert-base-uncased", output_attentions=True, foo=False, return_unused_kwargs=True
>>> )
>>> assert config.output_attentions == True
>>> assert unused_kwargs == {"foo": False}
Source code in mindnlp/transformers/configuration_utils.py
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
@classmethod
def from_pretrained(
    cls,
    pretrained_model_name_or_path: Union[str, os.PathLike],
    cache_dir: Optional[Union[str, os.PathLike]] = None,
    force_download: bool = False,
    local_files_only: bool = False,
    mirror: str = 'huggingface',
    **kwargs,
) -> "PretrainedConfig":
    r"""
    Instantiate a [`PretrainedConfig`] (or a derived class) from a pretrained model configuration.

    Args:
        pretrained_model_name_or_path (`str` or `os.PathLike`):
            This can be either:

            - a string, the *model id* of a pretrained model configuration hosted inside a model repo on
              hf-mirror.com. 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`.
            - a path to a *directory* containing a configuration file saved using the
              [`~PretrainedConfig.save_pretrained`] method, e.g., `./my_model_directory/`.
            - a path or url to a saved configuration JSON *file*, e.g., `./my_model_directory/configuration.json`.
        cache_dir (`str` or `os.PathLike`, *optional*):
            Path to a directory in which a downloaded pretrained model configuration should be cached if the
            standard cache should not be used.
        force_download (`bool`, *optional*, defaults to `False`):
            Whether or not to force to (re-)download the configuration files and override the cached versions if
            they exist.
        resume_download (`bool`, *optional*, defaults to `False`):
            Whether or not to delete incompletely received file. Attempts to resume the download if such a file
            exists.
        proxies (`Dict[str, str]`, *optional*):
            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.
        token (`str` or `bool`, *optional*):
            The token to use as HTTP bearer authorization for remote files. If `True`, or not specified, will use
            the token generated when running `huggingface-cli login` (stored in `~/.huggingface`).
        revision (`str`, *optional*, defaults to `"main"`):
            The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a
            git-based system for storing models and other artifacts on hf-mirror.com, so `revision` can be any
            identifier allowed by git.

            <Tip>

            To test a pull request you made on the Hub, you can pass `revision="refs/pr/<pr_number>".

            </Tip>

        return_unused_kwargs (`bool`, *optional*, defaults to `False`):
            - If `False`, then this function returns just the final configuration object.
            - If `True`, then this functions returns a `Tuple(config, unused_kwargs)` where *unused_kwargs* is a
            dictionary consisting of the key/value pairs whose keys are not configuration attributes: i.e., the
            part of `kwargs` which has not been used to update `config` and is otherwise ignored.
        subfolder (`str`, *optional*, defaults to `""`):
            In case the relevant files are located inside a subfolder of the model repo on hf-mirror.com, you can
            specify the folder name here.
        kwargs (`Dict[str, Any]`, *optional*):
            The values in kwargs of any keys which are configuration attributes will be used to override the loaded
            values. Behavior concerning key/value pairs whose keys are *not* configuration attributes is controlled
            by the `return_unused_kwargs` keyword parameter.

    Returns:
        [`PretrainedConfig`]: The configuration object instantiated from this pretrained model.

    Example:
        ```python
        >>> # We can't instantiate directly the base class *PretrainedConfig* so let's show the examples on a
        >>> # derived class: BertConfig
        >>> config = BertConfig.from_pretrained(
        >>>     "bert-base-uncased"
        >>> )  # Download configuration from hf-mirror.com and cache.
        >>> config = BertConfig.from_pretrained(
        >>>     "./test/saved_model/"
        >>> )  # E.g. config (or model) was saved using *save_pretrained('./test/saved_model/')*
        >>> config = BertConfig.from_pretrained("./test/saved_model/my_configuration.json")
        >>> config = BertConfig.from_pretrained("bert-base-uncased", output_attentions=True, foo=False)
        >>> assert config.output_attentions == True
        >>> config, unused_kwargs = BertConfig.from_pretrained(
        >>>     "bert-base-uncased", output_attentions=True, foo=False, return_unused_kwargs=True
        >>> )
        >>> assert config.output_attentions == True
        >>> assert unused_kwargs == {"foo": False}
        ```
    """
    kwargs["cache_dir"] = cache_dir
    kwargs["force_download"] = force_download
    kwargs["local_files_only"] = local_files_only
    kwargs['mirror'] = mirror

    config_dict, kwargs = cls.get_config_dict(pretrained_model_name_or_path, **kwargs)
    if "model_type" in config_dict and hasattr(cls, "model_type") and config_dict["model_type"] != cls.model_type:
        logger.warning(
            f"You are using a model of type {config_dict['model_type']} to instantiate a model of type "
            f"{cls.model_type}. This is not supported for all configurations of models and can yield errors."
        )

    return cls.from_dict(config_dict, **kwargs)

mindnlp.transformers.configuration_utils.PretrainedConfig.get_config_dict(pretrained_model_name_or_path, **kwargs) classmethod

From a pretrained_model_name_or_path, resolve to a dictionary of parameters, to be used for instantiating a [PretrainedConfig] using from_dict.

PARAMETER DESCRIPTION
pretrained_model_name_or_path

The identifier of the pre-trained checkpoint from which we want the dictionary of parameters.

TYPE: `str` or `os.PathLike`

RETURNS DESCRIPTION
Tuple[Dict[str, Any], Dict[str, Any]]

Tuple[Dict, Dict]: The dictionary(ies) that will be used to instantiate the configuration object.

Source code in mindnlp/transformers/configuration_utils.py
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
@classmethod
def get_config_dict(
    cls, pretrained_model_name_or_path: Union[str, os.PathLike], **kwargs
) -> Tuple[Dict[str, Any], Dict[str, Any]]:
    """
    From a `pretrained_model_name_or_path`, resolve to a dictionary of parameters, to be used for instantiating a
    [`PretrainedConfig`] using `from_dict`.

    Parameters:
        pretrained_model_name_or_path (`str` or `os.PathLike`):
            The identifier of the pre-trained checkpoint from which we want the dictionary of parameters.

    Returns:
        `Tuple[Dict, Dict]`: The dictionary(ies) that will be used to instantiate the configuration object.

    """
    original_kwargs = copy.deepcopy(kwargs)
    # Get config dict associated with the base config file
    config_dict, kwargs = cls._get_config_dict(pretrained_model_name_or_path, **kwargs)

    # That config file may point us toward another config file to use.
    if "configuration_files" in config_dict:
        configuration_file = get_configuration_file(config_dict["configuration_files"])
        config_dict, kwargs = cls._get_config_dict(
            pretrained_model_name_or_path, _configuration_file=configuration_file, **original_kwargs
        )

    return config_dict, kwargs

mindnlp.transformers.configuration_utils.PretrainedConfig.load(pretrained_model_name_or_path) classmethod

load config.

Source code in mindnlp/transformers/configuration_utils.py
237
238
239
240
@classmethod
def load(cls, pretrained_model_name_or_path):
    """load config."""
    return cls.from_pretrained(pretrained_model_name_or_path)

mindnlp.transformers.configuration_utils.PretrainedConfig.save_pretrained(save_directory, **kwargs)

Save a configuration object to the directory save_directory, so that it can be re-loaded using the [~PretrainedConfig.from_pretrained] class method.

PARAMETER DESCRIPTION
save_directory

Directory where the configuration JSON file will be saved (will be created if it does not exist).

TYPE: `str` or `os.PathLike`

push_to_hub

Whether or not to push your model to the Hugging Face model hub after saving it. You can specify the repository you want to push to with repo_id (will default to the name of save_directory in your namespace).

TYPE: `bool`, *optional*, defaults to `False`

kwargs

Additional key word arguments passed along to the [~utils.PushToHubMixin.push_to_hub] method.

TYPE: `Dict[str, Any]`, *optional* DEFAULT: {}

Source code in mindnlp/transformers/configuration_utils.py
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
def save_pretrained(self, save_directory: Union[str, os.PathLike], **kwargs):
    """
    Save a configuration object to the directory `save_directory`, so that it can be re-loaded using the
    [`~PretrainedConfig.from_pretrained`] class method.

    Args:
        save_directory (`str` or `os.PathLike`):
            Directory where the configuration JSON file will be saved (will be created if it does not exist).
        push_to_hub (`bool`, *optional*, defaults to `False`):
            Whether or not to push your model to the Hugging Face model hub after saving it. You can specify the
            repository you want to push to with `repo_id` (will default to the name of `save_directory` in your
            namespace).
        kwargs (`Dict[str, Any]`, *optional*):
            Additional key word arguments passed along to the [`~utils.PushToHubMixin.push_to_hub`] method.
    """
    if os.path.isfile(save_directory):
        raise AssertionError(f"Provided path ({save_directory}) should be a directory, not a file")

    os.makedirs(save_directory, exist_ok=True)

    # If we save using the predefined names, we can load using `from_pretrained`
    output_config_file = os.path.join(save_directory, CONFIG_NAME)

    self.to_json_file(output_config_file, use_diff=True)
    logger.info(f"Configuration saved in {output_config_file}")

mindnlp.transformers.configuration_utils.PretrainedConfig.to_dict()

Serializes this instance to a Python dictionary.

RETURNS DESCRIPTION
Dict[str, Any]

Dict[str, Any]: Dictionary of all the attributes that make up this configuration instance.

Source code in mindnlp/transformers/configuration_utils.py
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
def to_dict(self) -> Dict[str, Any]:
    """
    Serializes this instance to a Python dictionary.

    Returns:
        `Dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance.
    """
    output = copy.deepcopy(self.__dict__)
    if hasattr(self.__class__, "model_type"):
        output["model_type"] = self.__class__.model_type
    if "_auto_class" in output:
        del output["_auto_class"]
    if "_commit_hash" in output:
        del output["_commit_hash"]
    if "_attn_implementation_internal" in output:
        del output["_attn_implementation_internal"]

    for key, value in output.items():
        # Deal with nested configs like CLIP
        if isinstance(value, PretrainedConfig):
            value = value.to_dict()

        output[key] = value

    if hasattr(self, "quantization_config"):
        output["quantization_config"] = (
            self.quantization_config.to_dict()
            if not isinstance(self.quantization_config, dict)
            else self.quantization_config
        )

        # pop the `_pre_quantization_dtype` as torch.dtypes are not serializable.
        _ = output.pop("_pre_quantization_dtype", None)

    self.dict_ms_dtype_to_str(output)

    return output

mindnlp.transformers.configuration_utils.PretrainedConfig.to_diff_dict()

Removes all attributes from config which correspond to the default config attributes for better readability and serializes to a Python dictionary.

RETURNS DESCRIPTION
Dict[str, Any]

Dict[str, Any]: Dictionary of all the attributes that make up this configuration instance,

Source code in mindnlp/transformers/configuration_utils.py
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
def to_diff_dict(self) -> Dict[str, Any]:
    """
    Removes all attributes from config which correspond to the default config attributes for better readability and
    serializes to a Python dictionary.

    Returns:
        `Dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance,
    """
    config_dict = self.to_dict()

    # get the default config dict
    default_config_dict = PretrainedConfig().to_dict()

    # get class specific config dict
    class_config_dict = self.__class__().to_dict() if not self.is_composition else {}

    serializable_config_dict = {}

    # only serialize values that differ from the default config
    for key, value in config_dict.items():
        if (
            isinstance(getattr(self, key, None), PretrainedConfig)
            and key in class_config_dict
            and isinstance(class_config_dict[key], dict)
        ):
            # For nested configs we need to clean the diff recursively
            diff = recursive_diff_dict(value, class_config_dict[key], config_obj=getattr(self, key, None))
            if "model_type" in value:
                # Needs to be set even if it's not in the diff
                diff["model_type"] = value["model_type"]
            if len(diff) > 0:
                serializable_config_dict[key] = diff
        elif (
            key not in default_config_dict
            or value != default_config_dict[key]
            or (key in class_config_dict and value != class_config_dict[key])
        ):
            serializable_config_dict[key] = value

    return serializable_config_dict

mindnlp.transformers.configuration_utils.PretrainedConfig.to_file(save_path)

Serializes this instance to a JSON file.

Source code in mindnlp/transformers/configuration_utils.py
625
626
627
628
629
def to_file(self, save_path):
    """Serializes this instance to a JSON file."""
    output_dict = self.to_dict()
    with open(os.path.join(save_path, 'config.json'), encoding='utf-8') as f:
        json.dump(output_dict, f, sort_keys=True, indent=2)

mindnlp.transformers.configuration_utils.PretrainedConfig.to_json_file(json_file_path, use_diff=True)

Save this instance to a JSON file.

PARAMETER DESCRIPTION
json_file_path

Path to the JSON file in which this configuration instance's parameters will be saved.

TYPE: `str` or `os.PathLike`

use_diff

If set to True, only the difference between the config instance and the default PretrainedConfig() is serialized to JSON file.

TYPE: `bool`, *optional*, defaults to `True` DEFAULT: True

Source code in mindnlp/transformers/configuration_utils.py
667
668
669
670
671
672
673
674
675
676
677
678
679
def to_json_file(self, json_file_path: Union[str, os.PathLike], use_diff: bool = True):
    """
    Save this instance to a JSON file.

    Args:
        json_file_path (`str` or `os.PathLike`):
            Path to the JSON file in which this configuration instance's parameters will be saved.
        use_diff (`bool`, *optional*, defaults to `True`):
            If set to `True`, only the difference between the config instance and the default `PretrainedConfig()`
            is serialized to JSON file.
    """
    with open(json_file_path, "w", encoding="utf-8") as writer:
        writer.write(self.to_json_string(use_diff=use_diff))

mindnlp.transformers.configuration_utils.PretrainedConfig.to_json_string(use_diff=True)

Serializes this instance to a JSON string.

PARAMETER DESCRIPTION
use_diff

If set to True, only the difference between the config instance and the default PretrainedConfig() is serialized to JSON string.

TYPE: `bool`, *optional*, defaults to `True` DEFAULT: True

RETURNS DESCRIPTION
str

str: String containing all the attributes that make up this configuration instance in JSON format.

Source code in mindnlp/transformers/configuration_utils.py
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
def to_json_string(self, use_diff: bool = True) -> str:
    """
    Serializes this instance to a JSON string.

    Args:
        use_diff (`bool`, *optional*, defaults to `True`):
            If set to `True`, only the difference between the config instance and the default `PretrainedConfig()`
            is serialized to JSON string.

    Returns:
        `str`: String containing all the attributes that make up this configuration instance in JSON format.
    """
    if use_diff is True:
        config_dict = self.to_diff_dict()
    else:
        config_dict = self.to_dict()

    return json.dumps(config_dict, indent=2, sort_keys=True) + "\n"

mindnlp.transformers.configuration_utils.PretrainedConfig.update(config_dict)

Updates attributes of this class with attributes from config_dict.

PARAMETER DESCRIPTION
config_dict

Dictionary of attributes that should be updated for this class.

TYPE: `Dict[str, Any]`

Source code in mindnlp/transformers/configuration_utils.py
631
632
633
634
635
636
637
638
639
def update(self, config_dict: Dict[str, Any]):
    """
    Updates attributes of this class with attributes from `config_dict`.

    Args:
        config_dict (`Dict[str, Any]`): Dictionary of attributes that should be updated for this class.
    """
    for key, value in config_dict.items():
        setattr(self, key, value)