Skip to content

callbacks

mindnlp.engine.callbacks

Callbacks to use with the Trainer class and customize the training loop.

mindnlp.engine.callbacks.CallbackHandler

Bases: TrainerCallback

Internal class that just calls the list of callbacks in order.

Source code in mindnlp/engine/callbacks.py
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
class CallbackHandler(TrainerCallback):
    """Internal class that just calls the list of callbacks in order."""
    def __init__(self, callbacks, model, tokenizer, optimizer, lr_scheduler):
        r"""
        Initializes a new instance of the CallbackHandler class.

        Args:
            self: The object instance.
            callbacks (list): A list of callback objects.
            model: The model object.
            tokenizer: The tokenizer object.
            optimizer: The optimizer object.
            lr_scheduler: The learning rate scheduler object.

        Returns:
            None

        Raises:
            None
        """
        self.callbacks = []
        for cb in callbacks:
            self.add_callback(cb)
        self.model = model
        self.tokenizer = tokenizer
        self.optimizer = optimizer
        self.lr_scheduler = lr_scheduler
        self.train_dataset = None
        self.eval_dataset = None

        if not any(isinstance(cb, DefaultFlowCallback) for cb in self.callbacks):
            logger.warning(
                "The Trainer will not work properly if you don't have a `DefaultFlowCallback` in its callbacks. You\n"
                + "should add one before training with `trainer.add_callback(DefaultFlowCallback). The current list of"
                + "callbacks is\n:"
                + self.callback_list
            )

    def add_callback(self, callback):
        r"""
        Adds a callback to the list of callbacks in the CallbackHandler.

        Args:
            self (CallbackHandler): The instance of the CallbackHandler class.
            callback (callable or class): The callback to be added. It can be either a callable object or a class. If it is a class, an instance of it will be created.

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

        Raises:
            None: This method does not raise any exceptions.
        """
        cb = callback() if isinstance(callback, type) else callback
        cb_class = callback if isinstance(callback, type) else callback.__class__
        if cb_class in [c.__class__ for c in self.callbacks]:
            logger.warning(
                f"You are adding a {cb_class} to the callbacks of this Trainer, but there is already one. The current"
                + "list of callbacks is\n:"
                + self.callback_list
            )
        self.callbacks.append(cb)

    def pop_callback(self, callback):
        r"""
        pop_callback method in the CallbackHandler class removes a specified callback from the list of callbacks.

        Args:
            self (object): The instance of the CallbackHandler class.
            callback (object): The callback to be removed from the list. It can be either a function or a class type.

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

        Raises:
            TypeError: If the 'callback' parameter is not a valid type (function or class type).
        """
        if isinstance(callback, type):
            for cb in self.callbacks:
                if isinstance(cb, callback):
                    self.callbacks.remove(cb)
                    return cb
        else:
            for cb in self.callbacks:
                if cb == callback:
                    self.callbacks.remove(cb)
                    return cb

    def remove_callback(self, callback):
        r"""
        Method to remove a callback from the list of callbacks in the CallbackHandler class.

        Args:
            self (CallbackHandler): The instance of the CallbackHandler class.
                It holds the list of callbacks from which the specified callback needs to be removed.
            callback (function or class): The callback to be removed from the list of callbacks.
                If the callback is a class type, then all callbacks of that specific class will be removed.
                If the callback is a function, that specific callback will be removed from the list.

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

        Raises:
            TypeError: If the callback is not a function or a class type.
            ValueError: If the specified callback is not found in the list of callbacks.
        """
        if isinstance(callback, type):
            for cb in self.callbacks:
                if isinstance(cb, callback):
                    self.callbacks.remove(cb)
                    return
        else:
            self.callbacks.remove(callback)

    @property
    def callback_list(self):
        r"""
        This method, callback_list, returns a string containing the names of the callback objects in the CallbackHandler.

        Args:
            self (CallbackHandler): The instance of the CallbackHandler class.

        Returns:
            str: A string containing the names of the callback objects separated by newline characters.

        Raises:
            None
        """
        return "\n".join(cb.__class__.__name__ for cb in self.callbacks)

    def on_init_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
        r"""
        This method is called at the end of the initialization process.

        Args:
            self: The instance of the CallbackHandler class.
            args (TrainingArguments): An object containing training arguments.
            state (TrainerState): An object representing the state of the trainer.
            control (TrainerControl): An object providing control over the trainer.

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

        Raises:
            This method does not explicitly raise any exceptions.
        """
        return self.call_event("on_init_end", args, state, control)

    def on_train_begin(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
        r"""
        The 'on_train_begin' method is a callback function in the 'CallbackHandler' class. It is called at the beginning of the training process.

        Args:
            - self: The instance of the class. It is automatically passed and represents the current object.
            - args (TrainingArguments): An object containing various training arguments.
                - This parameter holds the training arguments that were passed to the Trainer.
                - It provides information such as the number of epochs, learning rate, batch size, etc.
            - state (TrainerState): An object representing the current state of the Trainer.
                - It encapsulates the training state, including information about the current training step, loss, and more.
            - control (TrainerControl): An object providing control over the training process.
                - It contains properties that can be modified to control the training behavior, such as stopping the training process.

        Returns:
            None

        Raises:
            This method does not raise any exceptions.
        """
        control.should_training_stop = False
        return self.call_event("on_train_begin", args, state, control)

    def on_train_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
        r"""
        This method is called at the end of the training process.

        Args:
            self: A reference to the current instance of the CallbackHandler class.
            args (TrainingArguments): An object containing training arguments.
                Purpose: Provides information about the training process configuration.
                Restrictions: Must be of type TrainingArguments.
            state (TrainerState): An object representing the state of the trainer during training.
                Purpose: Provides information about the current state of the trainer.
                Restrictions: Must be of type TrainerState.
            control (TrainerControl): An object providing control over the trainer during training.
                Purpose: Allows the callback to control the behavior of the trainer.
                Restrictions: Must be of type TrainerControl.

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

        Raises:
            None
        """
        return self.call_event("on_train_end", args, state, control)

    def on_epoch_begin(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
        r"""
        CallbackHandler.on_epoch_begin method is called at the beginning of each epoch during training.

        Args:
        - self: The instance of the CallbackHandler class.
        - args (TrainingArguments): The training arguments containing configuration settings for the training process.
        - state (TrainerState): The current state of the trainer during training.
        - control (TrainerControl): An object that allows control over the training process.

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

        Raises:
        This method does not raise any exceptions.
        """
        control.should_epoch_stop = False
        return self.call_event("on_epoch_begin", args, state, control)

    def on_epoch_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
        r"""
        on_epoch_end method in CallbackHandler class.

        Args:
            self (CallbackHandler): The instance of the CallbackHandler class.
            args (TrainingArguments): The training arguments for the model.
            state (TrainerState): The state of the trainer during training.
            control (TrainerControl): The control object for the trainer.

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

        Raises:
            This method does not explicitly raise any exceptions.
        """
        return self.call_event("on_epoch_end", args, state, control)

    def on_step_begin(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
        r"""
        This method is called at the beginning of each training step.

        Args:
            self: CallbackHandler
                The instance of the CallbackHandler class invoking the method.

            args: TrainingArguments
                An object containing the training arguments for the current training session.

            state: TrainerState
                An object containing the current state of the Trainer.

            control: TrainerControl
                An object providing control over the behavior of the Trainer.

        Returns:
            None
            This method does not return any value.

        Raises:
            None
            This method does not raise any exceptions.
        """
        control.should_log = False
        control.should_evaluate = False
        control.should_save = False
        return self.call_event("on_step_begin", args, state, control)

    def on_substep_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
        r"""
        CallbackHandler.on_substep_end(self, args, state, control)

        This method is called at the end of each substep of the training process.

        Args:
            self: An instance of the CallbackHandler class.
            args (TrainingArguments): An object that contains the training arguments.
                This parameter provides access to the training arguments such as 
                the number of epochs, learning rate, and batch size.
            state (TrainerState): An object that represents the current state of the trainer.
                This parameter provides information about the training progress, including
                the current step, epoch, and metrics.
            control (TrainerControl): An object that allows control over the training process.
                This parameter provides methods to pause, resume, or stop the training.

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

        Raises:
            No specific exceptions are raised by this method.
        """
        return self.call_event("on_substep_end", args, state, control)

    def on_step_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
        r"""
        on_step_end method in the CallbackHandler class.

        This method is called at the end of each training step in the Trainer class.

        Args:
            self (CallbackHandler): An instance of the CallbackHandler class.
            args (TrainingArguments): An object that contains the training arguments.
                This includes various parameters such as the number of epochs, batch size, learning rate, etc.
            state (TrainerState): An object that represents the current state of the Trainer.
                This includes information about the training progress, such as the current step and the loss.
            control (TrainerControl): An object that allows the CallbackHandler to control the training process.
                It provides methods to pause, resume, or stop the training.

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

        Raises:
            None.

        """
        return self.call_event("on_step_end", args, state, control)

    def on_evaluate(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, metrics):
        r"""
        This method 'on_evaluate' is a callback function in the 'CallbackHandler' class that is triggered during the evaluation phase of the training process.

        Args:
        - self: Represents the current instance of the class.
        - args (TrainingArguments): An object containing training arguments such as batch size, learning rate, etc.
        - state (TrainerState): Represents the current state of the Trainer during training.
        - control (TrainerControl): Provides control options for the Trainer, such as whether to continue evaluation.
        - metrics (dict): A dictionary containing evaluation metrics to be used during the evaluation process.

        Returns:
        - None: This method does not return any value explicitly. It sets 'control.should_evaluate' to False and triggers the 'on_evaluate' event using 'self.call_event'.

        Raises:
        - No specific exceptions are documented to be raised by this method. However, exceptions could potentially be raised within the 'call_event' method if any issues arise during the event triggering
process.
        """
        control.should_evaluate = False
        return self.call_event("on_evaluate", args, state, control, metrics=metrics)

    def on_predict(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, metrics):
        r"""
        Method: on_predict

        Description:
        This method is called during the prediction phase of the training process. It is part of the CallbackHandler class and is responsible for handling the 'on_predict' event.

        Args:
        - self: The instance of the CallbackHandler class.
        - args (TrainingArguments): The arguments for training, containing various configuration settings and hyperparameters.
        - state (TrainerState): The current state of the Trainer, including the model, optimizer, and other training-related information.
        - control (TrainerControl): The control object that allows interaction with the Trainer during the training process.
        - metrics (dict): A dictionary containing the evaluation metrics calculated during the prediction phase.

        Returns:
        None.

        Raises:
        This method does not raise any exceptions.
        """
        return self.call_event("on_predict", args, state, control, metrics=metrics)

    def on_save(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
        r"""
        This method 'on_save' in the 'CallbackHandler' class is triggered when the model is saved during training.

        Args:
            self (CallbackHandler): The instance of the CallbackHandler class.
            args (TrainingArguments): An object containing training arguments.
                This parameter provides information such as hyperparameters and settings for the training process.
            state (TrainerState): An object representing the current state of the Trainer during training.
                It contains information about the model, optimizer, scheduler, and other training state variables.
            control (TrainerControl): An object that controls the behavior of the Trainer during training.
                It allows modifying the default behavior by setting flags like 'should_save' to control saving.

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

        Raises:
            This method does not raise any exceptions.
        """
        control.should_save = False
        return self.call_event("on_save", args, state, control)

    def on_log(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, logs):
        r"""
        Method 'on_log' in the class 'CallbackHandler'.

        Args:
        - self: An instance of the class CallbackHandler.
        - args (TrainingArguments): Object containing training arguments.
        - state (TrainerState): Object representing the current state of the trainer.
        - control (TrainerControl): Object providing control options for the trainer.
        - logs: Additional logs or information related to the training process.

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

        Raises:
        - No specific exceptions are raised within this method.
        """
        control.should_log = False
        return self.call_event("on_log", args, state, control, logs=logs)

    def on_prediction_step(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
        """
        Callback method called on each prediction step during training.

        Args:
            self (CallbackHandler): The instance of the CallbackHandler class.
            args (TrainingArguments): The training arguments for the current training session.
            state (TrainerState): The current state of the trainer during training.
            control (TrainerControl): The control object for the trainer.

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

        Raises:
            No specific exceptions are raised within this method.
        """
        return self.call_event("on_prediction_step", args, state, control)

    def call_event(self, event, args, state, control, **kwargs):
        r"""
        call_event method in CallbackHandler class.

        This method is responsible for calling the specified event on each callback and handling the control flow based on the results.

        Args:
            self (object): The instance of the CallbackHandler class.
            event (str): The name of the event to be called on each callback.
            args (object): The arguments to be passed to the event callback.
            state (object): The current state of the system.
            control (object): The initial control parameter for the event handling.

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

        Raises:
            No specific exceptions are raised by this method.
            However, the callbacks' event methods may raise exceptions depending on their implementation.
        """
        for callback in self.callbacks:
            result = getattr(callback, event)(
                args,
                state,
                control,
                model=self.model,
                tokenizer=self.tokenizer,
                optimizer=self.optimizer,
                lr_scheduler=self.lr_scheduler,
                train_dataset=self.train_dataset,
                eval_dataset=self.eval_dataset,
                **kwargs,
            )
            # A Callback can skip the return of `control` if it doesn't change it.
            if result is not None:
                control = result
        return control

mindnlp.engine.callbacks.CallbackHandler.callback_list property

This method, callback_list, returns a string containing the names of the callback objects in the CallbackHandler.

PARAMETER DESCRIPTION
self

The instance of the CallbackHandler class.

TYPE: CallbackHandler

RETURNS DESCRIPTION
str

A string containing the names of the callback objects separated by newline characters.

mindnlp.engine.callbacks.CallbackHandler.__init__(callbacks, model, tokenizer, optimizer, lr_scheduler)

Initializes a new instance of the CallbackHandler class.

PARAMETER DESCRIPTION
self

The object instance.

callbacks

A list of callback objects.

TYPE: list

model

The model object.

tokenizer

The tokenizer object.

optimizer

The optimizer object.

lr_scheduler

The learning rate scheduler object.

RETURNS DESCRIPTION

None

Source code in mindnlp/engine/callbacks.py
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
def __init__(self, callbacks, model, tokenizer, optimizer, lr_scheduler):
    r"""
    Initializes a new instance of the CallbackHandler class.

    Args:
        self: The object instance.
        callbacks (list): A list of callback objects.
        model: The model object.
        tokenizer: The tokenizer object.
        optimizer: The optimizer object.
        lr_scheduler: The learning rate scheduler object.

    Returns:
        None

    Raises:
        None
    """
    self.callbacks = []
    for cb in callbacks:
        self.add_callback(cb)
    self.model = model
    self.tokenizer = tokenizer
    self.optimizer = optimizer
    self.lr_scheduler = lr_scheduler
    self.train_dataset = None
    self.eval_dataset = None

    if not any(isinstance(cb, DefaultFlowCallback) for cb in self.callbacks):
        logger.warning(
            "The Trainer will not work properly if you don't have a `DefaultFlowCallback` in its callbacks. You\n"
            + "should add one before training with `trainer.add_callback(DefaultFlowCallback). The current list of"
            + "callbacks is\n:"
            + self.callback_list
        )

mindnlp.engine.callbacks.CallbackHandler.add_callback(callback)

Adds a callback to the list of callbacks in the CallbackHandler.

PARAMETER DESCRIPTION
self

The instance of the CallbackHandler class.

TYPE: CallbackHandler

callback

The callback to be added. It can be either a callable object or a class. If it is a class, an instance of it will be created.

TYPE: callable or class

RETURNS DESCRIPTION
None

This method does not return any value.

RAISES DESCRIPTION
None

This method does not raise any exceptions.

Source code in mindnlp/engine/callbacks.py
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
def add_callback(self, callback):
    r"""
    Adds a callback to the list of callbacks in the CallbackHandler.

    Args:
        self (CallbackHandler): The instance of the CallbackHandler class.
        callback (callable or class): The callback to be added. It can be either a callable object or a class. If it is a class, an instance of it will be created.

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

    Raises:
        None: This method does not raise any exceptions.
    """
    cb = callback() if isinstance(callback, type) else callback
    cb_class = callback if isinstance(callback, type) else callback.__class__
    if cb_class in [c.__class__ for c in self.callbacks]:
        logger.warning(
            f"You are adding a {cb_class} to the callbacks of this Trainer, but there is already one. The current"
            + "list of callbacks is\n:"
            + self.callback_list
        )
    self.callbacks.append(cb)

mindnlp.engine.callbacks.CallbackHandler.call_event(event, args, state, control, **kwargs)

call_event method in CallbackHandler class.

This method is responsible for calling the specified event on each callback and handling the control flow based on the results.

PARAMETER DESCRIPTION
self

The instance of the CallbackHandler class.

TYPE: object

event

The name of the event to be called on each callback.

TYPE: str

args

The arguments to be passed to the event callback.

TYPE: object

state

The current state of the system.

TYPE: object

control

The initial control parameter for the event handling.

TYPE: object

RETURNS DESCRIPTION

None. The method does not return any value.

Source code in mindnlp/engine/callbacks.py
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
def call_event(self, event, args, state, control, **kwargs):
    r"""
    call_event method in CallbackHandler class.

    This method is responsible for calling the specified event on each callback and handling the control flow based on the results.

    Args:
        self (object): The instance of the CallbackHandler class.
        event (str): The name of the event to be called on each callback.
        args (object): The arguments to be passed to the event callback.
        state (object): The current state of the system.
        control (object): The initial control parameter for the event handling.

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

    Raises:
        No specific exceptions are raised by this method.
        However, the callbacks' event methods may raise exceptions depending on their implementation.
    """
    for callback in self.callbacks:
        result = getattr(callback, event)(
            args,
            state,
            control,
            model=self.model,
            tokenizer=self.tokenizer,
            optimizer=self.optimizer,
            lr_scheduler=self.lr_scheduler,
            train_dataset=self.train_dataset,
            eval_dataset=self.eval_dataset,
            **kwargs,
        )
        # A Callback can skip the return of `control` if it doesn't change it.
        if result is not None:
            control = result
    return control

mindnlp.engine.callbacks.CallbackHandler.on_epoch_begin(args, state, control)

CallbackHandler.on_epoch_begin method is called at the beginning of each epoch during training.

Args: - self: The instance of the CallbackHandler class. - args (TrainingArguments): The training arguments containing configuration settings for the training process. - state (TrainerState): The current state of the trainer during training. - control (TrainerControl): An object that allows control over the training process.

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

Raises: This method does not raise any exceptions.

Source code in mindnlp/engine/callbacks.py
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
def on_epoch_begin(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
    r"""
    CallbackHandler.on_epoch_begin method is called at the beginning of each epoch during training.

    Args:
    - self: The instance of the CallbackHandler class.
    - args (TrainingArguments): The training arguments containing configuration settings for the training process.
    - state (TrainerState): The current state of the trainer during training.
    - control (TrainerControl): An object that allows control over the training process.

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

    Raises:
    This method does not raise any exceptions.
    """
    control.should_epoch_stop = False
    return self.call_event("on_epoch_begin", args, state, control)

mindnlp.engine.callbacks.CallbackHandler.on_epoch_end(args, state, control)

on_epoch_end method in CallbackHandler class.

PARAMETER DESCRIPTION
self

The instance of the CallbackHandler class.

TYPE: CallbackHandler

args

The training arguments for the model.

TYPE: TrainingArguments

state

The state of the trainer during training.

TYPE: TrainerState

control

The control object for the trainer.

TYPE: TrainerControl

RETURNS DESCRIPTION

None. This method does not return any value.

Source code in mindnlp/engine/callbacks.py
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
def on_epoch_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
    r"""
    on_epoch_end method in CallbackHandler class.

    Args:
        self (CallbackHandler): The instance of the CallbackHandler class.
        args (TrainingArguments): The training arguments for the model.
        state (TrainerState): The state of the trainer during training.
        control (TrainerControl): The control object for the trainer.

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

    Raises:
        This method does not explicitly raise any exceptions.
    """
    return self.call_event("on_epoch_end", args, state, control)

mindnlp.engine.callbacks.CallbackHandler.on_evaluate(args, state, control, metrics)

    This method 'on_evaluate' is a callback function in the 'CallbackHandler' class that is triggered during the evaluation phase of the training process.

    Args:
    - self: Represents the current instance of the class.
    - args (TrainingArguments): An object containing training arguments such as batch size, learning rate, etc.
    - state (TrainerState): Represents the current state of the Trainer during training.
    - control (TrainerControl): Provides control options for the Trainer, such as whether to continue evaluation.
    - metrics (dict): A dictionary containing evaluation metrics to be used during the evaluation process.

    Returns:
    - None: This method does not return any value explicitly. It sets 'control.should_evaluate' to False and triggers the 'on_evaluate' event using 'self.call_event'.

    Raises:
    - No specific exceptions are documented to be raised by this method. However, exceptions could potentially be raised within the 'call_event' method if any issues arise during the event triggering

process.

Source code in mindnlp/engine/callbacks.py
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
    def on_evaluate(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, metrics):
        r"""
        This method 'on_evaluate' is a callback function in the 'CallbackHandler' class that is triggered during the evaluation phase of the training process.

        Args:
        - self: Represents the current instance of the class.
        - args (TrainingArguments): An object containing training arguments such as batch size, learning rate, etc.
        - state (TrainerState): Represents the current state of the Trainer during training.
        - control (TrainerControl): Provides control options for the Trainer, such as whether to continue evaluation.
        - metrics (dict): A dictionary containing evaluation metrics to be used during the evaluation process.

        Returns:
        - None: This method does not return any value explicitly. It sets 'control.should_evaluate' to False and triggers the 'on_evaluate' event using 'self.call_event'.

        Raises:
        - No specific exceptions are documented to be raised by this method. However, exceptions could potentially be raised within the 'call_event' method if any issues arise during the event triggering
process.
        """
        control.should_evaluate = False
        return self.call_event("on_evaluate", args, state, control, metrics=metrics)

mindnlp.engine.callbacks.CallbackHandler.on_init_end(args, state, control)

This method is called at the end of the initialization process.

PARAMETER DESCRIPTION
self

The instance of the CallbackHandler class.

args

An object containing training arguments.

TYPE: TrainingArguments

state

An object representing the state of the trainer.

TYPE: TrainerState

control

An object providing control over the trainer.

TYPE: TrainerControl

RETURNS DESCRIPTION

None. This method does not return any value.

Source code in mindnlp/engine/callbacks.py
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
def on_init_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
    r"""
    This method is called at the end of the initialization process.

    Args:
        self: The instance of the CallbackHandler class.
        args (TrainingArguments): An object containing training arguments.
        state (TrainerState): An object representing the state of the trainer.
        control (TrainerControl): An object providing control over the trainer.

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

    Raises:
        This method does not explicitly raise any exceptions.
    """
    return self.call_event("on_init_end", args, state, control)

mindnlp.engine.callbacks.CallbackHandler.on_log(args, state, control, logs)

Method 'on_log' in the class 'CallbackHandler'.

Args: - self: An instance of the class CallbackHandler. - args (TrainingArguments): Object containing training arguments. - state (TrainerState): Object representing the current state of the trainer. - control (TrainerControl): Object providing control options for the trainer. - logs: Additional logs or information related to the training process.

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

Raises: - No specific exceptions are raised within this method.

Source code in mindnlp/engine/callbacks.py
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
def on_log(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, logs):
    r"""
    Method 'on_log' in the class 'CallbackHandler'.

    Args:
    - self: An instance of the class CallbackHandler.
    - args (TrainingArguments): Object containing training arguments.
    - state (TrainerState): Object representing the current state of the trainer.
    - control (TrainerControl): Object providing control options for the trainer.
    - logs: Additional logs or information related to the training process.

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

    Raises:
    - No specific exceptions are raised within this method.
    """
    control.should_log = False
    return self.call_event("on_log", args, state, control, logs=logs)

mindnlp.engine.callbacks.CallbackHandler.on_predict(args, state, control, metrics)

Description: This method is called during the prediction phase of the training process. It is part of the CallbackHandler class and is responsible for handling the 'on_predict' event.

Args: - self: The instance of the CallbackHandler class. - args (TrainingArguments): The arguments for training, containing various configuration settings and hyperparameters. - state (TrainerState): The current state of the Trainer, including the model, optimizer, and other training-related information. - control (TrainerControl): The control object that allows interaction with the Trainer during the training process. - metrics (dict): A dictionary containing the evaluation metrics calculated during the prediction phase.

Returns: None.

Raises: This method does not raise any exceptions.

Source code in mindnlp/engine/callbacks.py
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
def on_predict(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, metrics):
    r"""
    Method: on_predict

    Description:
    This method is called during the prediction phase of the training process. It is part of the CallbackHandler class and is responsible for handling the 'on_predict' event.

    Args:
    - self: The instance of the CallbackHandler class.
    - args (TrainingArguments): The arguments for training, containing various configuration settings and hyperparameters.
    - state (TrainerState): The current state of the Trainer, including the model, optimizer, and other training-related information.
    - control (TrainerControl): The control object that allows interaction with the Trainer during the training process.
    - metrics (dict): A dictionary containing the evaluation metrics calculated during the prediction phase.

    Returns:
    None.

    Raises:
    This method does not raise any exceptions.
    """
    return self.call_event("on_predict", args, state, control, metrics=metrics)

mindnlp.engine.callbacks.CallbackHandler.on_prediction_step(args, state, control)

Callback method called on each prediction step during training.

PARAMETER DESCRIPTION
self

The instance of the CallbackHandler class.

TYPE: CallbackHandler

args

The training arguments for the current training session.

TYPE: TrainingArguments

state

The current state of the trainer during training.

TYPE: TrainerState

control

The control object for the trainer.

TYPE: TrainerControl

RETURNS DESCRIPTION

None. This method does not return any value explicitly.

Source code in mindnlp/engine/callbacks.py
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
def on_prediction_step(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
    """
    Callback method called on each prediction step during training.

    Args:
        self (CallbackHandler): The instance of the CallbackHandler class.
        args (TrainingArguments): The training arguments for the current training session.
        state (TrainerState): The current state of the trainer during training.
        control (TrainerControl): The control object for the trainer.

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

    Raises:
        No specific exceptions are raised within this method.
    """
    return self.call_event("on_prediction_step", args, state, control)

mindnlp.engine.callbacks.CallbackHandler.on_save(args, state, control)

This method 'on_save' in the 'CallbackHandler' class is triggered when the model is saved during training.

PARAMETER DESCRIPTION
self

The instance of the CallbackHandler class.

TYPE: CallbackHandler

args

An object containing training arguments. This parameter provides information such as hyperparameters and settings for the training process.

TYPE: TrainingArguments

state

An object representing the current state of the Trainer during training. It contains information about the model, optimizer, scheduler, and other training state variables.

TYPE: TrainerState

control

An object that controls the behavior of the Trainer during training. It allows modifying the default behavior by setting flags like 'should_save' to control saving.

TYPE: TrainerControl

RETURNS DESCRIPTION
None

This method does not return any value explicitly.

Source code in mindnlp/engine/callbacks.py
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
def on_save(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
    r"""
    This method 'on_save' in the 'CallbackHandler' class is triggered when the model is saved during training.

    Args:
        self (CallbackHandler): The instance of the CallbackHandler class.
        args (TrainingArguments): An object containing training arguments.
            This parameter provides information such as hyperparameters and settings for the training process.
        state (TrainerState): An object representing the current state of the Trainer during training.
            It contains information about the model, optimizer, scheduler, and other training state variables.
        control (TrainerControl): An object that controls the behavior of the Trainer during training.
            It allows modifying the default behavior by setting flags like 'should_save' to control saving.

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

    Raises:
        This method does not raise any exceptions.
    """
    control.should_save = False
    return self.call_event("on_save", args, state, control)

mindnlp.engine.callbacks.CallbackHandler.on_step_begin(args, state, control)

This method is called at the beginning of each training step.

PARAMETER DESCRIPTION
self

CallbackHandler The instance of the CallbackHandler class invoking the method.

args

TrainingArguments An object containing the training arguments for the current training session.

TYPE: TrainingArguments

state

TrainerState An object containing the current state of the Trainer.

TYPE: TrainerState

control

TrainerControl An object providing control over the behavior of the Trainer.

TYPE: TrainerControl

RETURNS DESCRIPTION

None

This method does not return any value.

Source code in mindnlp/engine/callbacks.py
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
def on_step_begin(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
    r"""
    This method is called at the beginning of each training step.

    Args:
        self: CallbackHandler
            The instance of the CallbackHandler class invoking the method.

        args: TrainingArguments
            An object containing the training arguments for the current training session.

        state: TrainerState
            An object containing the current state of the Trainer.

        control: TrainerControl
            An object providing control over the behavior of the Trainer.

    Returns:
        None
        This method does not return any value.

    Raises:
        None
        This method does not raise any exceptions.
    """
    control.should_log = False
    control.should_evaluate = False
    control.should_save = False
    return self.call_event("on_step_begin", args, state, control)

mindnlp.engine.callbacks.CallbackHandler.on_step_end(args, state, control)

on_step_end method in the CallbackHandler class.

This method is called at the end of each training step in the Trainer class.

PARAMETER DESCRIPTION
self

An instance of the CallbackHandler class.

TYPE: CallbackHandler

args

An object that contains the training arguments. This includes various parameters such as the number of epochs, batch size, learning rate, etc.

TYPE: TrainingArguments

state

An object that represents the current state of the Trainer. This includes information about the training progress, such as the current step and the loss.

TYPE: TrainerState

control

An object that allows the CallbackHandler to control the training process. It provides methods to pause, resume, or stop the training.

TYPE: TrainerControl

RETURNS DESCRIPTION

None. This method does not return any value.

Source code in mindnlp/engine/callbacks.py
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
def on_step_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
    r"""
    on_step_end method in the CallbackHandler class.

    This method is called at the end of each training step in the Trainer class.

    Args:
        self (CallbackHandler): An instance of the CallbackHandler class.
        args (TrainingArguments): An object that contains the training arguments.
            This includes various parameters such as the number of epochs, batch size, learning rate, etc.
        state (TrainerState): An object that represents the current state of the Trainer.
            This includes information about the training progress, such as the current step and the loss.
        control (TrainerControl): An object that allows the CallbackHandler to control the training process.
            It provides methods to pause, resume, or stop the training.

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

    Raises:
        None.

    """
    return self.call_event("on_step_end", args, state, control)

mindnlp.engine.callbacks.CallbackHandler.on_substep_end(args, state, control)

CallbackHandler.on_substep_end(self, args, state, control)

This method is called at the end of each substep of the training process.

PARAMETER DESCRIPTION
self

An instance of the CallbackHandler class.

args

An object that contains the training arguments. This parameter provides access to the training arguments such as the number of epochs, learning rate, and batch size.

TYPE: TrainingArguments

state

An object that represents the current state of the trainer. This parameter provides information about the training progress, including the current step, epoch, and metrics.

TYPE: TrainerState

control

An object that allows control over the training process. This parameter provides methods to pause, resume, or stop the training.

TYPE: TrainerControl

RETURNS DESCRIPTION

None. This method does not return any value.

Source code in mindnlp/engine/callbacks.py
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
def on_substep_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
    r"""
    CallbackHandler.on_substep_end(self, args, state, control)

    This method is called at the end of each substep of the training process.

    Args:
        self: An instance of the CallbackHandler class.
        args (TrainingArguments): An object that contains the training arguments.
            This parameter provides access to the training arguments such as 
            the number of epochs, learning rate, and batch size.
        state (TrainerState): An object that represents the current state of the trainer.
            This parameter provides information about the training progress, including
            the current step, epoch, and metrics.
        control (TrainerControl): An object that allows control over the training process.
            This parameter provides methods to pause, resume, or stop the training.

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

    Raises:
        No specific exceptions are raised by this method.
    """
    return self.call_event("on_substep_end", args, state, control)

mindnlp.engine.callbacks.CallbackHandler.on_train_begin(args, state, control)

The 'on_train_begin' method is a callback function in the 'CallbackHandler' class. It is called at the beginning of the training process.

PARAMETER DESCRIPTION
-

The instance of the class. It is automatically passed and represents the current object.

TYPE: self

-

An object containing various training arguments. - This parameter holds the training arguments that were passed to the Trainer. - It provides information such as the number of epochs, learning rate, batch size, etc.

TYPE: args (TrainingArguments

-

An object representing the current state of the Trainer. - It encapsulates the training state, including information about the current training step, loss, and more.

TYPE: state (TrainerState

-

An object providing control over the training process. - It contains properties that can be modified to control the training behavior, such as stopping the training process.

TYPE: control (TrainerControl

RETURNS DESCRIPTION

None

Source code in mindnlp/engine/callbacks.py
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
def on_train_begin(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
    r"""
    The 'on_train_begin' method is a callback function in the 'CallbackHandler' class. It is called at the beginning of the training process.

    Args:
        - self: The instance of the class. It is automatically passed and represents the current object.
        - args (TrainingArguments): An object containing various training arguments.
            - This parameter holds the training arguments that were passed to the Trainer.
            - It provides information such as the number of epochs, learning rate, batch size, etc.
        - state (TrainerState): An object representing the current state of the Trainer.
            - It encapsulates the training state, including information about the current training step, loss, and more.
        - control (TrainerControl): An object providing control over the training process.
            - It contains properties that can be modified to control the training behavior, such as stopping the training process.

    Returns:
        None

    Raises:
        This method does not raise any exceptions.
    """
    control.should_training_stop = False
    return self.call_event("on_train_begin", args, state, control)

mindnlp.engine.callbacks.CallbackHandler.on_train_end(args, state, control)

This method is called at the end of the training process.

PARAMETER DESCRIPTION
self

A reference to the current instance of the CallbackHandler class.

args

An object containing training arguments. Purpose: Provides information about the training process configuration. Restrictions: Must be of type TrainingArguments.

TYPE: TrainingArguments

state

An object representing the state of the trainer during training. Purpose: Provides information about the current state of the trainer. Restrictions: Must be of type TrainerState.

TYPE: TrainerState

control

An object providing control over the trainer during training. Purpose: Allows the callback to control the behavior of the trainer. Restrictions: Must be of type TrainerControl.

TYPE: TrainerControl

RETURNS DESCRIPTION
None

This method does not return any value.

Source code in mindnlp/engine/callbacks.py
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
def on_train_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
    r"""
    This method is called at the end of the training process.

    Args:
        self: A reference to the current instance of the CallbackHandler class.
        args (TrainingArguments): An object containing training arguments.
            Purpose: Provides information about the training process configuration.
            Restrictions: Must be of type TrainingArguments.
        state (TrainerState): An object representing the state of the trainer during training.
            Purpose: Provides information about the current state of the trainer.
            Restrictions: Must be of type TrainerState.
        control (TrainerControl): An object providing control over the trainer during training.
            Purpose: Allows the callback to control the behavior of the trainer.
            Restrictions: Must be of type TrainerControl.

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

    Raises:
        None
    """
    return self.call_event("on_train_end", args, state, control)

mindnlp.engine.callbacks.CallbackHandler.pop_callback(callback)

pop_callback method in the CallbackHandler class removes a specified callback from the list of callbacks.

PARAMETER DESCRIPTION
self

The instance of the CallbackHandler class.

TYPE: object

callback

The callback to be removed from the list. It can be either a function or a class type.

TYPE: object

RETURNS DESCRIPTION
None

This method does not return any value.

RAISES DESCRIPTION
TypeError

If the 'callback' parameter is not a valid type (function or class type).

Source code in mindnlp/engine/callbacks.py
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
def pop_callback(self, callback):
    r"""
    pop_callback method in the CallbackHandler class removes a specified callback from the list of callbacks.

    Args:
        self (object): The instance of the CallbackHandler class.
        callback (object): The callback to be removed from the list. It can be either a function or a class type.

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

    Raises:
        TypeError: If the 'callback' parameter is not a valid type (function or class type).
    """
    if isinstance(callback, type):
        for cb in self.callbacks:
            if isinstance(cb, callback):
                self.callbacks.remove(cb)
                return cb
    else:
        for cb in self.callbacks:
            if cb == callback:
                self.callbacks.remove(cb)
                return cb

mindnlp.engine.callbacks.CallbackHandler.remove_callback(callback)

Method to remove a callback from the list of callbacks in the CallbackHandler class.

PARAMETER DESCRIPTION
self

The instance of the CallbackHandler class. It holds the list of callbacks from which the specified callback needs to be removed.

TYPE: CallbackHandler

callback

The callback to be removed from the list of callbacks. If the callback is a class type, then all callbacks of that specific class will be removed. If the callback is a function, that specific callback will be removed from the list.

TYPE: function or class

RETURNS DESCRIPTION

None. This method does not return any value.

RAISES DESCRIPTION
TypeError

If the callback is not a function or a class type.

ValueError

If the specified callback is not found in the list of callbacks.

Source code in mindnlp/engine/callbacks.py
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
def remove_callback(self, callback):
    r"""
    Method to remove a callback from the list of callbacks in the CallbackHandler class.

    Args:
        self (CallbackHandler): The instance of the CallbackHandler class.
            It holds the list of callbacks from which the specified callback needs to be removed.
        callback (function or class): The callback to be removed from the list of callbacks.
            If the callback is a class type, then all callbacks of that specific class will be removed.
            If the callback is a function, that specific callback will be removed from the list.

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

    Raises:
        TypeError: If the callback is not a function or a class type.
        ValueError: If the specified callback is not found in the list of callbacks.
    """
    if isinstance(callback, type):
        for cb in self.callbacks:
            if isinstance(cb, callback):
                self.callbacks.remove(cb)
                return
    else:
        self.callbacks.remove(callback)

mindnlp.engine.callbacks.DefaultFlowCallback

Bases: TrainerCallback

A [TrainerCallback] that handles the default flow of the training loop for logs, evaluation and checkpoints.

Source code in mindnlp/engine/callbacks.py
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
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
class DefaultFlowCallback(TrainerCallback):
    """
    A [`TrainerCallback`] that handles the default flow of the training loop for logs, evaluation and checkpoints.
    """
    def on_step_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
        r"""
        This method is called at the end of each training step in the 'DefaultFlowCallback' class.

        Args:
            self: An instance of the 'DefaultFlowCallback' class.
            args (TrainingArguments): An object containing training arguments.
                - args.logging_first_step (bool): Specifies whether to log the first step.
                - args.logging_strategy (IntervalStrategy): Specifies the logging strategy.
                - args.evaluation_strategy (IntervalStrategy): Specifies the evaluation strategy.
                - args.eval_delay (int): Specifies the delay in evaluation steps.
                - args.save_strategy (IntervalStrategy): Specifies the saving strategy.
            state (TrainerState): An object containing the current state of the trainer.
                - state.global_step (int): The current global step of the training.
                - state.logging_steps (int): The number of steps between each logging.
                - state.eval_steps (int): The number of steps between each evaluation.
                - state.save_steps (int): The number of steps between each saving.
                - state.max_steps (int): The maximum number of steps for the training.
            control (TrainerControl): An object controlling the behavior of the trainer.
                - control.should_log (bool): Specifies whether to log at the current step.
                - control.should_evaluate (bool): Specifies whether to evaluate at the current step.
                - control.should_save (bool): Specifies whether to save at the current step.
                - control.should_training_stop (bool): Specifies whether to stop training.

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

        Raises:
            None. This method does not raise any exceptions.
        """
        # Log
        if state.global_step == 1 and args.logging_first_step:
            control.should_log = True
        if args.logging_strategy == IntervalStrategy.STEPS and state.global_step % state.logging_steps == 0:
            control.should_log = True

        # Evaluate
        if (
            args.evaluation_strategy == IntervalStrategy.STEPS
            and state.global_step % state.eval_steps == 0
            and args.eval_delay <= state.global_step
        ):
            control.should_evaluate = True

        # Save
        if (
            args.save_strategy == IntervalStrategy.STEPS
            and state.save_steps > 0
            and state.global_step % state.save_steps == 0
        ):
            control.should_save = True

        # End training
        if state.global_step >= state.max_steps:
            control.should_training_stop = True

        return control

    def on_epoch_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
        r"""
        This method is called at the end of each epoch during the training process.

        Args:
            self (DefaultFlowCallback): The instance of the DefaultFlowCallback class.
            args (TrainingArguments): The training arguments containing various settings for training.
                This parameter is mandatory and must be of type TrainingArguments.
            state (TrainerState): The current state of the Trainer.
                This parameter is mandatory and must be of type TrainerState.
            control (TrainerControl): The control object that determines the actions to be taken during training.
                This parameter is mandatory and must be of type TrainerControl.

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

        Raises:
            None: This method does not raise any exceptions.
        """
        # Log
        if args.logging_strategy == IntervalStrategy.EPOCH:
            control.should_log = True

        # Evaluate
        if args.evaluation_strategy == IntervalStrategy.EPOCH and args.eval_delay <= state.epoch:
            control.should_evaluate = True

        # Save
        if args.save_strategy == IntervalStrategy.EPOCH:
            control.should_save = True

        return control

mindnlp.engine.callbacks.DefaultFlowCallback.on_epoch_end(args, state, control, **kwargs)

This method is called at the end of each epoch during the training process.

PARAMETER DESCRIPTION
self

The instance of the DefaultFlowCallback class.

TYPE: DefaultFlowCallback

args

The training arguments containing various settings for training. This parameter is mandatory and must be of type TrainingArguments.

TYPE: TrainingArguments

state

The current state of the Trainer. This parameter is mandatory and must be of type TrainerState.

TYPE: TrainerState

control

The control object that determines the actions to be taken during training. This parameter is mandatory and must be of type TrainerControl.

TYPE: TrainerControl

RETURNS DESCRIPTION
None

This method does not return any value.

RAISES DESCRIPTION
None

This method does not raise any exceptions.

Source code in mindnlp/engine/callbacks.py
803
804
805
806
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
def on_epoch_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
    r"""
    This method is called at the end of each epoch during the training process.

    Args:
        self (DefaultFlowCallback): The instance of the DefaultFlowCallback class.
        args (TrainingArguments): The training arguments containing various settings for training.
            This parameter is mandatory and must be of type TrainingArguments.
        state (TrainerState): The current state of the Trainer.
            This parameter is mandatory and must be of type TrainerState.
        control (TrainerControl): The control object that determines the actions to be taken during training.
            This parameter is mandatory and must be of type TrainerControl.

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

    Raises:
        None: This method does not raise any exceptions.
    """
    # Log
    if args.logging_strategy == IntervalStrategy.EPOCH:
        control.should_log = True

    # Evaluate
    if args.evaluation_strategy == IntervalStrategy.EPOCH and args.eval_delay <= state.epoch:
        control.should_evaluate = True

    # Save
    if args.save_strategy == IntervalStrategy.EPOCH:
        control.should_save = True

    return control

mindnlp.engine.callbacks.DefaultFlowCallback.on_step_end(args, state, control, **kwargs)

This method is called at the end of each training step in the 'DefaultFlowCallback' class.

PARAMETER DESCRIPTION
self

An instance of the 'DefaultFlowCallback' class.

args

An object containing training arguments. - args.logging_first_step (bool): Specifies whether to log the first step. - args.logging_strategy (IntervalStrategy): Specifies the logging strategy. - args.evaluation_strategy (IntervalStrategy): Specifies the evaluation strategy. - args.eval_delay (int): Specifies the delay in evaluation steps. - args.save_strategy (IntervalStrategy): Specifies the saving strategy.

TYPE: TrainingArguments

state

An object containing the current state of the trainer. - state.global_step (int): The current global step of the training. - state.logging_steps (int): The number of steps between each logging. - state.eval_steps (int): The number of steps between each evaluation. - state.save_steps (int): The number of steps between each saving. - state.max_steps (int): The maximum number of steps for the training.

TYPE: TrainerState

control

An object controlling the behavior of the trainer. - control.should_log (bool): Specifies whether to log at the current step. - control.should_evaluate (bool): Specifies whether to evaluate at the current step. - control.should_save (bool): Specifies whether to save at the current step. - control.should_training_stop (bool): Specifies whether to stop training.

TYPE: TrainerControl

RETURNS DESCRIPTION

None. This method does not return any value.

Source code in mindnlp/engine/callbacks.py
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
def on_step_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
    r"""
    This method is called at the end of each training step in the 'DefaultFlowCallback' class.

    Args:
        self: An instance of the 'DefaultFlowCallback' class.
        args (TrainingArguments): An object containing training arguments.
            - args.logging_first_step (bool): Specifies whether to log the first step.
            - args.logging_strategy (IntervalStrategy): Specifies the logging strategy.
            - args.evaluation_strategy (IntervalStrategy): Specifies the evaluation strategy.
            - args.eval_delay (int): Specifies the delay in evaluation steps.
            - args.save_strategy (IntervalStrategy): Specifies the saving strategy.
        state (TrainerState): An object containing the current state of the trainer.
            - state.global_step (int): The current global step of the training.
            - state.logging_steps (int): The number of steps between each logging.
            - state.eval_steps (int): The number of steps between each evaluation.
            - state.save_steps (int): The number of steps between each saving.
            - state.max_steps (int): The maximum number of steps for the training.
        control (TrainerControl): An object controlling the behavior of the trainer.
            - control.should_log (bool): Specifies whether to log at the current step.
            - control.should_evaluate (bool): Specifies whether to evaluate at the current step.
            - control.should_save (bool): Specifies whether to save at the current step.
            - control.should_training_stop (bool): Specifies whether to stop training.

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

    Raises:
        None. This method does not raise any exceptions.
    """
    # Log
    if state.global_step == 1 and args.logging_first_step:
        control.should_log = True
    if args.logging_strategy == IntervalStrategy.STEPS and state.global_step % state.logging_steps == 0:
        control.should_log = True

    # Evaluate
    if (
        args.evaluation_strategy == IntervalStrategy.STEPS
        and state.global_step % state.eval_steps == 0
        and args.eval_delay <= state.global_step
    ):
        control.should_evaluate = True

    # Save
    if (
        args.save_strategy == IntervalStrategy.STEPS
        and state.save_steps > 0
        and state.global_step % state.save_steps == 0
    ):
        control.should_save = True

    # End training
    if state.global_step >= state.max_steps:
        control.should_training_stop = True

    return control

mindnlp.engine.callbacks.EarlyStoppingCallback

Bases: TrainerCallback

A [TrainerCallback] that handles early stopping.

PARAMETER DESCRIPTION
early_stopping_patience

Use with metric_for_best_model to stop training when the specified metric worsens for early_stopping_patience evaluation calls.

TYPE: `int` DEFAULT: 1

early_stopping_threshold(`float`,

Use with TrainingArguments metric_for_best_model and early_stopping_patience to denote how much the specified metric must improve to satisfy early stopping conditions. `

TYPE: *optional*

This callback depends on [TrainingArguments] argument load_best_model_at_end functionality to set best_metric in [TrainerState]. Note that if the [TrainingArguments] argument save_steps differs from eval_steps, the early stopping will not occur until the next save step.

Source code in mindnlp/engine/callbacks.py
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
class EarlyStoppingCallback(TrainerCallback):
    """
    A [`TrainerCallback`] that handles early stopping.

    Args:
        early_stopping_patience (`int`):
            Use with `metric_for_best_model` to stop training when the specified metric worsens for
            `early_stopping_patience` evaluation calls.
        early_stopping_threshold(`float`, *optional*):
            Use with TrainingArguments `metric_for_best_model` and `early_stopping_patience` to denote how much the
            specified metric must improve to satisfy early stopping conditions. `

    This callback depends on [`TrainingArguments`] argument *load_best_model_at_end* functionality to set best_metric
    in [`TrainerState`]. Note that if the [`TrainingArguments`] argument *save_steps* differs from *eval_steps*, the
    early stopping will not occur until the next save step.
    """
    def __init__(self, early_stopping_patience: int = 1, early_stopping_threshold: Optional[float] = 0.0):
        r"""
        Initializes an instance of the EarlyStoppingCallback class.

        Args:
            self: The instance of the EarlyStoppingCallback class.
            early_stopping_patience (int, optional): The number of epochs to wait for improvement in the monitored metric before stopping the training process. Defaults to 1.
            early_stopping_threshold (float, optional): The minimum improvement required in the monitored metric to be considered as improvement. Defaults to 0.0.

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

        Raises:
            None.
        """
        self.early_stopping_patience = early_stopping_patience
        self.early_stopping_threshold = early_stopping_threshold
        # early_stopping_patience_counter denotes the number of times validation metrics failed to improve.
        self.early_stopping_patience_counter = 0

    def check_metric_value(self, args, state, control, metric_value):
        r"""
        This method 'check_metric_value' is part of the 'EarlyStoppingCallback' class and is used to evaluate a metric value and determine if early stopping criteria are met.

        Args:
        - self: Represents the instance of the class.
        - args: A set of arguments that influence the evaluation process.
            Type: Any.
            Purpose: Contains settings that affect the metric evaluation logic.
        - state: Represents the current state of the evaluation process.
            Type: Any.
            Purpose: Stores information about the best metric value encountered so far.
        - control: A parameter that controls the behavior of the evaluation process.
            Type: Any.
            Purpose: Allows for external control over the evaluation process.
        - metric_value: The value of the metric to be evaluated.
            Type: Any numerical type.
            Purpose: Represents the metric value to be checked against the current best metric.

        Returns:
        - None: This method does not return any value explicitly.
            Type: None.
            Purpose: The method updates internal state variables but does not provide any return value.

        Raises:
        - None: This method does not explicitly raise any exceptions. However, it may indirectly raise exceptions related to the operations performed within the method.
        """
        # best_metric is set by code for load_best_model
        operator = np.greater if args.greater_is_better else np.less
        if state.best_metric is None or (
            operator(metric_value, state.best_metric)
            and abs(metric_value - state.best_metric) > self.early_stopping_threshold
        ):
            self.early_stopping_patience_counter = 0
        else:
            self.early_stopping_patience_counter += 1

    def on_train_begin(self, args, state, control, **kwargs):
        r"""
        This method is called at the beginning of the training process within the 'EarlyStoppingCallback' class.

        Args:
            - self: The instance of the class.
            - args: A dictionary containing various settings for the training process.
                Type: dict
                Purpose: Contains configuration settings for the training process.
                Restrictions: None
            - state: Represents the current state of the training process.
                Type: any
                Purpose: Provides information about the current state of the training.
                Restrictions: None
            - control: A control variable to manage the training process.
                Type: any
                Purpose: Helps in controlling the flow of the training process.
                Restrictions: None

        Returns:
            - None: This method does not return any value.
                Type: None
                Purpose: N/A

        Raises:
            - AssertionError: Raised if 'load_best_model_at_end' is not set to True in the 'args' dictionary.
            - AssertionError: Raised if 'metric_for_best_model' is not defined in the 'args' dictionary.
            - AssertionError: Raised if 'evaluation_strategy' is set to IntervalStrategy.NO, as it should be steps or epoch.
        """
        assert args.load_best_model_at_end, "EarlyStoppingCallback requires load_best_model_at_end = True"
        assert (
            args.metric_for_best_model is not None
        ), "EarlyStoppingCallback requires metric_for_best_model is defined"
        assert (
            args.evaluation_strategy != IntervalStrategy.NO
        ), "EarlyStoppingCallback requires IntervalStrategy of steps or epoch"

    def on_evaluate(self, args, state, control, metrics, **kwargs):
        r"""
        This method 'on_evaluate' is a part of the 'EarlyStoppingCallback' class and is responsible for evaluating the metrics and performing early stopping if the specified metric value does not meet the
criteria.

        Args:
        - self: (object) The instance of the class.
        - args: (object) A collection of arguments containing the metric_for_best_model attribute, used to specify the metric for evaluating the model.
        - state: (object) The current state of the training process.
        - control: (object) A control object that manages the training process, including early stopping.
        - metrics: (object) A container for storing and retrieving the evaluated metrics during the training process.

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

        Raises:
        - Warning: If the specified metric_for_best_model is not found in the evaluated metrics, early stopping is disabled and a warning is logged.
        - Exception: If the early_stopping_patience_counter exceeds the early_stopping_patience, the training process is stopped by setting the should_training_stop flag in the control object to True.
        """
        metric_to_check = args.metric_for_best_model
        if not metric_to_check.startswith("eval_"):
            metric_to_check = f"eval_{metric_to_check}"
        metric_value = metrics.get(metric_to_check)

        if metric_value is None:
            logger.warning(
                f"early stopping required metric_for_best_model, but did not find {metric_to_check} so early stopping"
                " is disabled"
            )
            return

        self.check_metric_value(args, state, control, metric_value)
        if self.early_stopping_patience_counter >= self.early_stopping_patience:
            control.should_training_stop = True

mindnlp.engine.callbacks.EarlyStoppingCallback.__init__(early_stopping_patience=1, early_stopping_threshold=0.0)

Initializes an instance of the EarlyStoppingCallback class.

PARAMETER DESCRIPTION
self

The instance of the EarlyStoppingCallback class.

early_stopping_patience

The number of epochs to wait for improvement in the monitored metric before stopping the training process. Defaults to 1.

TYPE: int DEFAULT: 1

early_stopping_threshold

The minimum improvement required in the monitored metric to be considered as improvement. Defaults to 0.0.

TYPE: float DEFAULT: 0.0

RETURNS DESCRIPTION

None. This method does not return any value.

Source code in mindnlp/engine/callbacks.py
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
def __init__(self, early_stopping_patience: int = 1, early_stopping_threshold: Optional[float] = 0.0):
    r"""
    Initializes an instance of the EarlyStoppingCallback class.

    Args:
        self: The instance of the EarlyStoppingCallback class.
        early_stopping_patience (int, optional): The number of epochs to wait for improvement in the monitored metric before stopping the training process. Defaults to 1.
        early_stopping_threshold (float, optional): The minimum improvement required in the monitored metric to be considered as improvement. Defaults to 0.0.

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

    Raises:
        None.
    """
    self.early_stopping_patience = early_stopping_patience
    self.early_stopping_threshold = early_stopping_threshold
    # early_stopping_patience_counter denotes the number of times validation metrics failed to improve.
    self.early_stopping_patience_counter = 0

mindnlp.engine.callbacks.EarlyStoppingCallback.check_metric_value(args, state, control, metric_value)

This method 'check_metric_value' is part of the 'EarlyStoppingCallback' class and is used to evaluate a metric value and determine if early stopping criteria are met.

Args: - self: Represents the instance of the class. - args: A set of arguments that influence the evaluation process. Type: Any. Purpose: Contains settings that affect the metric evaluation logic. - state: Represents the current state of the evaluation process. Type: Any. Purpose: Stores information about the best metric value encountered so far. - control: A parameter that controls the behavior of the evaluation process. Type: Any. Purpose: Allows for external control over the evaluation process. - metric_value: The value of the metric to be evaluated. Type: Any numerical type. Purpose: Represents the metric value to be checked against the current best metric.

  • None: This method does not return any value explicitly. Type: None. Purpose: The method updates internal state variables but does not provide any return value.

Raises: - None: This method does not explicitly raise any exceptions. However, it may indirectly raise exceptions related to the operations performed within the method.

Source code in mindnlp/engine/callbacks.py
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
def check_metric_value(self, args, state, control, metric_value):
    r"""
    This method 'check_metric_value' is part of the 'EarlyStoppingCallback' class and is used to evaluate a metric value and determine if early stopping criteria are met.

    Args:
    - self: Represents the instance of the class.
    - args: A set of arguments that influence the evaluation process.
        Type: Any.
        Purpose: Contains settings that affect the metric evaluation logic.
    - state: Represents the current state of the evaluation process.
        Type: Any.
        Purpose: Stores information about the best metric value encountered so far.
    - control: A parameter that controls the behavior of the evaluation process.
        Type: Any.
        Purpose: Allows for external control over the evaluation process.
    - metric_value: The value of the metric to be evaluated.
        Type: Any numerical type.
        Purpose: Represents the metric value to be checked against the current best metric.

    Returns:
    - None: This method does not return any value explicitly.
        Type: None.
        Purpose: The method updates internal state variables but does not provide any return value.

    Raises:
    - None: This method does not explicitly raise any exceptions. However, it may indirectly raise exceptions related to the operations performed within the method.
    """
    # best_metric is set by code for load_best_model
    operator = np.greater if args.greater_is_better else np.less
    if state.best_metric is None or (
        operator(metric_value, state.best_metric)
        and abs(metric_value - state.best_metric) > self.early_stopping_threshold
    ):
        self.early_stopping_patience_counter = 0
    else:
        self.early_stopping_patience_counter += 1

mindnlp.engine.callbacks.EarlyStoppingCallback.on_evaluate(args, state, control, metrics, **kwargs)

    This method 'on_evaluate' is a part of the 'EarlyStoppingCallback' class and is responsible for evaluating the metrics and performing early stopping if the specified metric value does not meet the

criteria.

    Args:
    - self: (object) The instance of the class.
    - args: (object) A collection of arguments containing the metric_for_best_model attribute, used to specify the metric for evaluating the model.
    - state: (object) The current state of the training process.
    - control: (object) A control object that manages the training process, including early stopping.
    - metrics: (object) A container for storing and retrieving the evaluated metrics during the training process.

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

    Raises:
    - Warning: If the specified metric_for_best_model is not found in the evaluated metrics, early stopping is disabled and a warning is logged.
    - Exception: If the early_stopping_patience_counter exceeds the early_stopping_patience, the training process is stopped by setting the should_training_stop flag in the control object to True.
Source code in mindnlp/engine/callbacks.py
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
    def on_evaluate(self, args, state, control, metrics, **kwargs):
        r"""
        This method 'on_evaluate' is a part of the 'EarlyStoppingCallback' class and is responsible for evaluating the metrics and performing early stopping if the specified metric value does not meet the
criteria.

        Args:
        - self: (object) The instance of the class.
        - args: (object) A collection of arguments containing the metric_for_best_model attribute, used to specify the metric for evaluating the model.
        - state: (object) The current state of the training process.
        - control: (object) A control object that manages the training process, including early stopping.
        - metrics: (object) A container for storing and retrieving the evaluated metrics during the training process.

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

        Raises:
        - Warning: If the specified metric_for_best_model is not found in the evaluated metrics, early stopping is disabled and a warning is logged.
        - Exception: If the early_stopping_patience_counter exceeds the early_stopping_patience, the training process is stopped by setting the should_training_stop flag in the control object to True.
        """
        metric_to_check = args.metric_for_best_model
        if not metric_to_check.startswith("eval_"):
            metric_to_check = f"eval_{metric_to_check}"
        metric_value = metrics.get(metric_to_check)

        if metric_value is None:
            logger.warning(
                f"early stopping required metric_for_best_model, but did not find {metric_to_check} so early stopping"
                " is disabled"
            )
            return

        self.check_metric_value(args, state, control, metric_value)
        if self.early_stopping_patience_counter >= self.early_stopping_patience:
            control.should_training_stop = True

mindnlp.engine.callbacks.EarlyStoppingCallback.on_train_begin(args, state, control, **kwargs)

This method is called at the beginning of the training process within the 'EarlyStoppingCallback' class.

PARAMETER DESCRIPTION
-

The instance of the class.

TYPE: self

-

A dictionary containing various settings for the training process. Type: dict Purpose: Contains configuration settings for the training process. Restrictions: None

TYPE: args

-

Represents the current state of the training process. Type: any Purpose: Provides information about the current state of the training. Restrictions: None

TYPE: state

-

A control variable to manage the training process. Type: any Purpose: Helps in controlling the flow of the training process. Restrictions: None

TYPE: control

RETURNS DESCRIPTION
  • None: This method does not return any value. Type: None Purpose: N/A
RAISES DESCRIPTION
-AssertionError

Raised if 'load_best_model_at_end' is not set to True in the 'args' dictionary.

-AssertionError

Raised if 'metric_for_best_model' is not defined in the 'args' dictionary.

-AssertionError

Raised if 'evaluation_strategy' is set to IntervalStrategy.NO, as it should be steps or epoch.

Source code in mindnlp/engine/callbacks.py
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
def on_train_begin(self, args, state, control, **kwargs):
    r"""
    This method is called at the beginning of the training process within the 'EarlyStoppingCallback' class.

    Args:
        - self: The instance of the class.
        - args: A dictionary containing various settings for the training process.
            Type: dict
            Purpose: Contains configuration settings for the training process.
            Restrictions: None
        - state: Represents the current state of the training process.
            Type: any
            Purpose: Provides information about the current state of the training.
            Restrictions: None
        - control: A control variable to manage the training process.
            Type: any
            Purpose: Helps in controlling the flow of the training process.
            Restrictions: None

    Returns:
        - None: This method does not return any value.
            Type: None
            Purpose: N/A

    Raises:
        - AssertionError: Raised if 'load_best_model_at_end' is not set to True in the 'args' dictionary.
        - AssertionError: Raised if 'metric_for_best_model' is not defined in the 'args' dictionary.
        - AssertionError: Raised if 'evaluation_strategy' is set to IntervalStrategy.NO, as it should be steps or epoch.
    """
    assert args.load_best_model_at_end, "EarlyStoppingCallback requires load_best_model_at_end = True"
    assert (
        args.metric_for_best_model is not None
    ), "EarlyStoppingCallback requires metric_for_best_model is defined"
    assert (
        args.evaluation_strategy != IntervalStrategy.NO
    ), "EarlyStoppingCallback requires IntervalStrategy of steps or epoch"

mindnlp.engine.callbacks.PrinterCallback

Bases: TrainerCallback

A bare [TrainerCallback] that just prints the logs.

Source code in mindnlp/engine/callbacks.py
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
class PrinterCallback(TrainerCallback):
    """
    A bare [`TrainerCallback`] that just prints the logs.
    """
    def on_log(self, args, state, control, logs=None, **kwargs):
        r"""
        This method 'on_log' is defined within the class 'PrinterCallback' and is used to handle logging events.

        Args:
            self: Represents the instance of the class.
            args: A parameter that holds additional arguments.
            state: Represents the current state of the system.
            control: Indicates the control status.
            logs: A dictionary containing log information. Default is None.

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

        Raises:
            No specific exceptions are raised within this method.
        """
        _ = logs.pop("total_flos", None)
        if state.is_local_process_zero:
            print(logs)

mindnlp.engine.callbacks.PrinterCallback.on_log(args, state, control, logs=None, **kwargs)

This method 'on_log' is defined within the class 'PrinterCallback' and is used to handle logging events.

PARAMETER DESCRIPTION
self

Represents the instance of the class.

args

A parameter that holds additional arguments.

state

Represents the current state of the system.

control

Indicates the control status.

logs

A dictionary containing log information. Default is None.

DEFAULT: None

RETURNS DESCRIPTION
None

This method does not return any value.

Source code in mindnlp/engine/callbacks.py
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
def on_log(self, args, state, control, logs=None, **kwargs):
    r"""
    This method 'on_log' is defined within the class 'PrinterCallback' and is used to handle logging events.

    Args:
        self: Represents the instance of the class.
        args: A parameter that holds additional arguments.
        state: Represents the current state of the system.
        control: Indicates the control status.
        logs: A dictionary containing log information. Default is None.

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

    Raises:
        No specific exceptions are raised within this method.
    """
    _ = logs.pop("total_flos", None)
    if state.is_local_process_zero:
        print(logs)

mindnlp.engine.callbacks.ProgressCallback

Bases: TrainerCallback

A [TrainerCallback] that displays the progress of training or evaluation.

Source code in mindnlp/engine/callbacks.py
 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
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
class ProgressCallback(TrainerCallback):
    """
    A [`TrainerCallback`] that displays the progress of training or evaluation.
    """
    def __init__(self):
        r"""
        Initializes a ProgressCallback object.

        Args:
            self: The ProgressCallback object itself.

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

        Raises:
            No exceptions are raised by this method.
        """
        self.training_bar = None
        self.prediction_bar = None

    def on_train_begin(self, args, state, control, **kwargs):
        r"""
        This method is called at the beginning of the training process.

        Args:
            self (ProgressCallback): The instance of the ProgressCallback class invoking the method.
            args: Additional arguments passed to the method.
            state: The state of the training process.
            control: The control parameters for the training process.

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

        Raises:
            No specific exceptions are raised by this method.
        """
        if state.is_world_process_zero:
            self.training_bar = tqdm(total=state.max_steps, dynamic_ncols=True)
        self.current_step = 0

    def on_step_end(self, args, state, control, **kwargs):
        r"""
        Executes actions at the end of each step during training progress.

        Args:
            self (ProgressCallback): An instance of the ProgressCallback class.
            args: Additional arguments passed to the method.
            state: The state of the training process.
            control: Control parameters for the callback.

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

        Raises:
            None. This method does not raise any exceptions.
        """
        if state.is_world_process_zero:
            self.training_bar.update(state.global_step - self.current_step)
            self.current_step = state.global_step

    def on_prediction_step(self, args, state, control, eval_dataset=None, **kwargs):
        r"""
        Method: on_prediction_step

        Description:
        This method is a callback function that is executed on each prediction step during model training. It updates the progress bar for prediction progress.

        Args:
        - self: The instance of the ProgressCallback class.
        - args: Additional arguments that may be passed to the method.
        - state: The current state of the training process.
          - Type: Any
          - Purpose: Provides information about the current state of the training process.
          - Restrictions: None
        - control: The control parameters for the training process.
          - Type: Any
          - Purpose: Provides control parameters for the training process.
          - Restrictions: None
        - eval_dataset: The evaluation dataset used for predictions.
          - Type: Any
          - Purpose: Stores the evaluation dataset used for predictions.
          - Restrictions: None

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

        Raises:
        - None: This method does not raise any exceptions.
        """
        if state.is_world_process_zero and has_length(eval_dataset):
            if self.prediction_bar is None:
                self.prediction_bar = tqdm(
                    total=len(eval_dataset), leave=self.training_bar is None, dynamic_ncols=True
                )
            self.prediction_bar.update(1)

    def on_evaluate(self, args, state, control, **kwargs):
        r"""
        This method 'on_evaluate' is defined in the 'ProgressCallback' class and is used to evaluate a given state based on certain conditions.

        Args:
        - self: Represents the instance of the class.
        - args: Additional arguments passed to the method.
        - state: Represents the current state being evaluated.
        - control: Additional control parameter.

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

        Raises:
        - None.
        """
        if state.is_world_process_zero:
            if self.prediction_bar is not None:
                self.prediction_bar.close()
            self.prediction_bar = None

    def on_predict(self, args, state, control, **kwargs):
        r"""
        Performs the prediction process and updates the progress bar accordingly.

        Args:
            self (ProgressCallback): An instance of the ProgressCallback class.
            args: Additional arguments passed to the method.
            state: The current state of the prediction process.
            control: The control object used to manage the prediction process.

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

        Raises:
            None. This method does not raise any exceptions.
        """
        if state.is_world_process_zero:
            if self.prediction_bar is not None:
                self.prediction_bar.close()
            self.prediction_bar = None

    def on_log(self, args, state, control, logs=None, **kwargs):
        r"""
        Method 'on_log' in the class 'ProgressCallback' handles logging during training progress.

        Args:
        - self: The instance of the class.
        - args: Additional arguments passed to the method.
        - state: Represents the current state of the training process.
        - control: Control parameters for the logging behavior.
        - logs: A dictionary containing various log values. Default value is None.

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

        Raises:
        - None explicitly raised in this method.
        """
        if state.is_world_process_zero and self.training_bar is not None:
            _ = logs.pop("total_flos", None)
            self.training_bar.write(str(logs))

    def on_train_end(self, args, state, control, **kwargs):
        r"""
        This method 'on_train_end' is defined within the class 'ProgressCallback' and is called when the training process ends.

        Args:
        - self: Represents the instance of the class.
        - args: Additional arguments provided to the method.
        - state: Represents the current state of the training process.
        - control: Provides control options for the method.

        Returns:
        This method does not return any value, as it performs operations within the method itself and does not produce an output explicitly.

        Raises:
        This method does not explicitly raise any exceptions. However, it may raise exceptions indirectly depending on the operations performed within the method.
        """
        if state.is_world_process_zero:
            self.training_bar.close()
            self.training_bar = None

mindnlp.engine.callbacks.ProgressCallback.__init__()

Initializes a ProgressCallback object.

PARAMETER DESCRIPTION
self

The ProgressCallback object itself.

RETURNS DESCRIPTION

None. This method does not return any value.

Source code in mindnlp/engine/callbacks.py
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
def __init__(self):
    r"""
    Initializes a ProgressCallback object.

    Args:
        self: The ProgressCallback object itself.

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

    Raises:
        No exceptions are raised by this method.
    """
    self.training_bar = None
    self.prediction_bar = None

mindnlp.engine.callbacks.ProgressCallback.on_evaluate(args, state, control, **kwargs)

This method 'on_evaluate' is defined in the 'ProgressCallback' class and is used to evaluate a given state based on certain conditions.

Args: - self: Represents the instance of the class. - args: Additional arguments passed to the method. - state: Represents the current state being evaluated. - control: Additional control parameter.

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

Raises: - None.

Source code in mindnlp/engine/callbacks.py
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
def on_evaluate(self, args, state, control, **kwargs):
    r"""
    This method 'on_evaluate' is defined in the 'ProgressCallback' class and is used to evaluate a given state based on certain conditions.

    Args:
    - self: Represents the instance of the class.
    - args: Additional arguments passed to the method.
    - state: Represents the current state being evaluated.
    - control: Additional control parameter.

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

    Raises:
    - None.
    """
    if state.is_world_process_zero:
        if self.prediction_bar is not None:
            self.prediction_bar.close()
        self.prediction_bar = None

mindnlp.engine.callbacks.ProgressCallback.on_log(args, state, control, logs=None, **kwargs)

Method 'on_log' in the class 'ProgressCallback' handles logging during training progress.

Args: - self: The instance of the class. - args: Additional arguments passed to the method. - state: Represents the current state of the training process. - control: Control parameters for the logging behavior. - logs: A dictionary containing various log values. Default value is None.

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

Raises: - None explicitly raised in this method.

Source code in mindnlp/engine/callbacks.py
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
def on_log(self, args, state, control, logs=None, **kwargs):
    r"""
    Method 'on_log' in the class 'ProgressCallback' handles logging during training progress.

    Args:
    - self: The instance of the class.
    - args: Additional arguments passed to the method.
    - state: Represents the current state of the training process.
    - control: Control parameters for the logging behavior.
    - logs: A dictionary containing various log values. Default value is None.

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

    Raises:
    - None explicitly raised in this method.
    """
    if state.is_world_process_zero and self.training_bar is not None:
        _ = logs.pop("total_flos", None)
        self.training_bar.write(str(logs))

mindnlp.engine.callbacks.ProgressCallback.on_predict(args, state, control, **kwargs)

Performs the prediction process and updates the progress bar accordingly.

PARAMETER DESCRIPTION
self

An instance of the ProgressCallback class.

TYPE: ProgressCallback

args

Additional arguments passed to the method.

state

The current state of the prediction process.

control

The control object used to manage the prediction process.

RETURNS DESCRIPTION

None. This method does not return any value.

Source code in mindnlp/engine/callbacks.py
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
def on_predict(self, args, state, control, **kwargs):
    r"""
    Performs the prediction process and updates the progress bar accordingly.

    Args:
        self (ProgressCallback): An instance of the ProgressCallback class.
        args: Additional arguments passed to the method.
        state: The current state of the prediction process.
        control: The control object used to manage the prediction process.

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

    Raises:
        None. This method does not raise any exceptions.
    """
    if state.is_world_process_zero:
        if self.prediction_bar is not None:
            self.prediction_bar.close()
        self.prediction_bar = None

mindnlp.engine.callbacks.ProgressCallback.on_prediction_step(args, state, control, eval_dataset=None, **kwargs)

Description: This method is a callback function that is executed on each prediction step during model training. It updates the progress bar for prediction progress.

Args: - self: The instance of the ProgressCallback class. - args: Additional arguments that may be passed to the method. - state: The current state of the training process. - Type: Any - Purpose: Provides information about the current state of the training process. - Restrictions: None - control: The control parameters for the training process. - Type: Any - Purpose: Provides control parameters for the training process. - Restrictions: None - eval_dataset: The evaluation dataset used for predictions. - Type: Any - Purpose: Stores the evaluation dataset used for predictions. - Restrictions: None

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

Raises: - None: This method does not raise any exceptions.

Source code in mindnlp/engine/callbacks.py
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
def on_prediction_step(self, args, state, control, eval_dataset=None, **kwargs):
    r"""
    Method: on_prediction_step

    Description:
    This method is a callback function that is executed on each prediction step during model training. It updates the progress bar for prediction progress.

    Args:
    - self: The instance of the ProgressCallback class.
    - args: Additional arguments that may be passed to the method.
    - state: The current state of the training process.
      - Type: Any
      - Purpose: Provides information about the current state of the training process.
      - Restrictions: None
    - control: The control parameters for the training process.
      - Type: Any
      - Purpose: Provides control parameters for the training process.
      - Restrictions: None
    - eval_dataset: The evaluation dataset used for predictions.
      - Type: Any
      - Purpose: Stores the evaluation dataset used for predictions.
      - Restrictions: None

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

    Raises:
    - None: This method does not raise any exceptions.
    """
    if state.is_world_process_zero and has_length(eval_dataset):
        if self.prediction_bar is None:
            self.prediction_bar = tqdm(
                total=len(eval_dataset), leave=self.training_bar is None, dynamic_ncols=True
            )
        self.prediction_bar.update(1)

mindnlp.engine.callbacks.ProgressCallback.on_step_end(args, state, control, **kwargs)

Executes actions at the end of each step during training progress.

PARAMETER DESCRIPTION
self

An instance of the ProgressCallback class.

TYPE: ProgressCallback

args

Additional arguments passed to the method.

state

The state of the training process.

control

Control parameters for the callback.

RETURNS DESCRIPTION

None. This method does not return any value.

Source code in mindnlp/engine/callbacks.py
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
def on_step_end(self, args, state, control, **kwargs):
    r"""
    Executes actions at the end of each step during training progress.

    Args:
        self (ProgressCallback): An instance of the ProgressCallback class.
        args: Additional arguments passed to the method.
        state: The state of the training process.
        control: Control parameters for the callback.

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

    Raises:
        None. This method does not raise any exceptions.
    """
    if state.is_world_process_zero:
        self.training_bar.update(state.global_step - self.current_step)
        self.current_step = state.global_step

mindnlp.engine.callbacks.ProgressCallback.on_train_begin(args, state, control, **kwargs)

This method is called at the beginning of the training process.

PARAMETER DESCRIPTION
self

The instance of the ProgressCallback class invoking the method.

TYPE: ProgressCallback

args

Additional arguments passed to the method.

state

The state of the training process.

control

The control parameters for the training process.

RETURNS DESCRIPTION
None

This method does not return any value.

Source code in mindnlp/engine/callbacks.py
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
def on_train_begin(self, args, state, control, **kwargs):
    r"""
    This method is called at the beginning of the training process.

    Args:
        self (ProgressCallback): The instance of the ProgressCallback class invoking the method.
        args: Additional arguments passed to the method.
        state: The state of the training process.
        control: The control parameters for the training process.

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

    Raises:
        No specific exceptions are raised by this method.
    """
    if state.is_world_process_zero:
        self.training_bar = tqdm(total=state.max_steps, dynamic_ncols=True)
    self.current_step = 0

mindnlp.engine.callbacks.ProgressCallback.on_train_end(args, state, control, **kwargs)

This method 'on_train_end' is defined within the class 'ProgressCallback' and is called when the training process ends.

Args: - self: Represents the instance of the class. - args: Additional arguments provided to the method. - state: Represents the current state of the training process. - control: Provides control options for the method.

Returns: This method does not return any value, as it performs operations within the method itself and does not produce an output explicitly.

Raises: This method does not explicitly raise any exceptions. However, it may raise exceptions indirectly depending on the operations performed within the method.

Source code in mindnlp/engine/callbacks.py
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
def on_train_end(self, args, state, control, **kwargs):
    r"""
    This method 'on_train_end' is defined within the class 'ProgressCallback' and is called when the training process ends.

    Args:
    - self: Represents the instance of the class.
    - args: Additional arguments provided to the method.
    - state: Represents the current state of the training process.
    - control: Provides control options for the method.

    Returns:
    This method does not return any value, as it performs operations within the method itself and does not produce an output explicitly.

    Raises:
    This method does not explicitly raise any exceptions. However, it may raise exceptions indirectly depending on the operations performed within the method.
    """
    if state.is_world_process_zero:
        self.training_bar.close()
        self.training_bar = None

mindnlp.engine.callbacks.TrainerCallback

A class for objects that will inspect the state of the training loop at some events and take some decisions. At each of those events the following arguments are available:

PARAMETER DESCRIPTION
args

The training arguments used to instantiate the [Trainer].

TYPE: [`TrainingArguments`]

state

The current state of the [Trainer].

TYPE: [`TrainerState`]

control

The object that is returned to the [Trainer] and can be used to make some decisions.

TYPE: [`TrainerControl`]

model

The model being trained.

TYPE: [`PreTrainedModel`] or `mindspore.nn.cell`

tokenizer

The tokenizer used for encoding the data.

TYPE: [`PreTrainedTokenizer`]

optimizer

The optimizer used for the training steps.

TYPE: `mindspore.nn.Optimizer`

lr_scheduler

The scheduler used for setting the learning rate.

TYPE: `mindspore.experimental.optim.lr_scheduler.LambdaLR`

train_dataset

The current dataloader used for training.

TYPE: `mindspore.dataset.GeneratorDataset`, *optional*

eval_dataset

The current dataloader used for training.

TYPE: `mindspore.dataset.GeneratorDataset`, *optional*

metrics

The metrics computed by the last evaluation phase.

Those are only accessible in the event on_evaluate.

TYPE: `Dict[str, float]`

logs

The values to log.

Those are only accessible in the event on_log.

TYPE: (`Dict[str, float]`

The control object is the only one that can be changed by the callback, in which case the event that changes it should return the modified version.

The argument args, state and control are positionals for all events, all the others are grouped in kwargs. You can unpack the ones you need in the signature of the event using them. As an example, see the code of the simple [~transformers.PrinterCallback].

Example:

class PrinterCallback(TrainerCallback):
    def on_log(self, args, state, control, logs=None, **kwargs):
        _ = logs.pop("total_flos", None)
        if state.is_local_process_zero:
            print(logs)
Source code in mindnlp/engine/callbacks.py
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
class TrainerCallback:
    # no-format
    """
    A class for objects that will inspect the state of the training loop at some events and take some decisions. At
    each of those events the following arguments are available:

    Args:
        args ([`TrainingArguments`]):
            The training arguments used to instantiate the [`Trainer`].
        state ([`TrainerState`]):
            The current state of the [`Trainer`].
        control ([`TrainerControl`]):
            The object that is returned to the [`Trainer`] and can be used to make some decisions.
        model ([`PreTrainedModel`] or `mindspore.nn.cell`):
            The model being trained.
        tokenizer ([`PreTrainedTokenizer`]):
            The tokenizer used for encoding the data.
        optimizer (`mindspore.nn.Optimizer`):
            The optimizer used for the training steps.
        lr_scheduler (`mindspore.experimental.optim.lr_scheduler.LambdaLR`):
            The scheduler used for setting the learning rate.
        train_dataset (`mindspore.dataset.GeneratorDataset`, *optional*):
            The current dataloader used for training.
        eval_dataset (`mindspore.dataset.GeneratorDataset`, *optional*):
            The current dataloader used for training.
        metrics (`Dict[str, float]`):
            The metrics computed by the last evaluation phase.

            Those are only accessible in the event `on_evaluate`.
        logs  (`Dict[str, float]`):
            The values to log.

            Those are only accessible in the event `on_log`.

    The `control` object is the only one that can be changed by the callback, in which case the event that changes it
    should return the modified version.

    The argument `args`, `state` and `control` are positionals for all events, all the others are grouped in `kwargs`.
    You can unpack the ones you need in the signature of the event using them. As an example, see the code of the
    simple [`~transformers.PrinterCallback`].

    Example:

    ```python
    class PrinterCallback(TrainerCallback):
        def on_log(self, args, state, control, logs=None, **kwargs):
            _ = logs.pop("total_flos", None)
            if state.is_local_process_zero:
                print(logs)
    ```"""
    def on_init_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
        """
        Event called at the end of the initialization of the [`Trainer`].
        """
    def on_train_begin(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
        """
        Event called at the beginning of training.
        """
    def on_train_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
        """
        Event called at the end of training.
        """
    def on_epoch_begin(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
        """
        Event called at the beginning of an epoch.
        """
    def on_epoch_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
        """
        Event called at the end of an epoch.
        """
    def on_step_begin(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
        """
        Event called at the beginning of a training step. If using gradient accumulation, one training step might take
        several inputs.
        """
    def on_substep_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
        """
        Event called at the end of an substep during gradient accumulation.
        """
    def on_step_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
        """
        Event called at the end of a training step. If using gradient accumulation, one training step might take
        several inputs.
        """
    def on_evaluate(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
        """
        Event called after an evaluation phase.
        """
    def on_predict(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, metrics, **kwargs):
        """
        Event called after a successful prediction.
        """
    def on_save(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
        """
        Event called after a checkpoint save.
        """
    def on_log(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
        """
        Event called after logging the last logs.
        """
    def on_prediction_step(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
        """
        Event called after a prediction step.
        """

mindnlp.engine.callbacks.TrainerCallback.on_epoch_begin(args, state, control, **kwargs)

Event called at the beginning of an epoch.

Source code in mindnlp/engine/callbacks.py
247
248
249
250
def on_epoch_begin(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
    """
    Event called at the beginning of an epoch.
    """

mindnlp.engine.callbacks.TrainerCallback.on_epoch_end(args, state, control, **kwargs)

Event called at the end of an epoch.

Source code in mindnlp/engine/callbacks.py
251
252
253
254
def on_epoch_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
    """
    Event called at the end of an epoch.
    """

mindnlp.engine.callbacks.TrainerCallback.on_evaluate(args, state, control, **kwargs)

Event called after an evaluation phase.

Source code in mindnlp/engine/callbacks.py
269
270
271
272
def on_evaluate(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
    """
    Event called after an evaluation phase.
    """

mindnlp.engine.callbacks.TrainerCallback.on_init_end(args, state, control, **kwargs)

Event called at the end of the initialization of the [Trainer].

Source code in mindnlp/engine/callbacks.py
235
236
237
238
def on_init_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
    """
    Event called at the end of the initialization of the [`Trainer`].
    """

mindnlp.engine.callbacks.TrainerCallback.on_log(args, state, control, **kwargs)

Event called after logging the last logs.

Source code in mindnlp/engine/callbacks.py
281
282
283
284
def on_log(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
    """
    Event called after logging the last logs.
    """

mindnlp.engine.callbacks.TrainerCallback.on_predict(args, state, control, metrics, **kwargs)

Event called after a successful prediction.

Source code in mindnlp/engine/callbacks.py
273
274
275
276
def on_predict(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, metrics, **kwargs):
    """
    Event called after a successful prediction.
    """

mindnlp.engine.callbacks.TrainerCallback.on_prediction_step(args, state, control, **kwargs)

Event called after a prediction step.

Source code in mindnlp/engine/callbacks.py
285
286
287
288
def on_prediction_step(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
    """
    Event called after a prediction step.
    """

mindnlp.engine.callbacks.TrainerCallback.on_save(args, state, control, **kwargs)

Event called after a checkpoint save.

Source code in mindnlp/engine/callbacks.py
277
278
279
280
def on_save(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
    """
    Event called after a checkpoint save.
    """

mindnlp.engine.callbacks.TrainerCallback.on_step_begin(args, state, control, **kwargs)

Event called at the beginning of a training step. If using gradient accumulation, one training step might take several inputs.

Source code in mindnlp/engine/callbacks.py
255
256
257
258
259
def on_step_begin(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
    """
    Event called at the beginning of a training step. If using gradient accumulation, one training step might take
    several inputs.
    """

mindnlp.engine.callbacks.TrainerCallback.on_step_end(args, state, control, **kwargs)

Event called at the end of a training step. If using gradient accumulation, one training step might take several inputs.

Source code in mindnlp/engine/callbacks.py
264
265
266
267
268
def on_step_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
    """
    Event called at the end of a training step. If using gradient accumulation, one training step might take
    several inputs.
    """

mindnlp.engine.callbacks.TrainerCallback.on_substep_end(args, state, control, **kwargs)

Event called at the end of an substep during gradient accumulation.

Source code in mindnlp/engine/callbacks.py
260
261
262
263
def on_substep_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
    """
    Event called at the end of an substep during gradient accumulation.
    """

mindnlp.engine.callbacks.TrainerCallback.on_train_begin(args, state, control, **kwargs)

Event called at the beginning of training.

Source code in mindnlp/engine/callbacks.py
239
240
241
242
def on_train_begin(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
    """
    Event called at the beginning of training.
    """

mindnlp.engine.callbacks.TrainerCallback.on_train_end(args, state, control, **kwargs)

Event called at the end of training.

Source code in mindnlp/engine/callbacks.py
243
244
245
246
def on_train_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
    """
    Event called at the end of training.
    """

mindnlp.engine.callbacks.TrainerControl dataclass

A class that handles the [Trainer] control flow. This class is used by the [TrainerCallback] to activate some switches in the training loop.

PARAMETER DESCRIPTION
should_training_stop

Whether or not the training should be interrupted.

If True, this variable will not be set back to False. The training will just stop.

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

should_epoch_stop

Whether or not the current epoch should be interrupted.

If True, this variable will be set back to False at the beginning of the next epoch.

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

should_save

Whether or not the model should be saved at this step.

If True, this variable will be set back to False at the beginning of the next step.

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

should_evaluate

Whether or not the model should be evaluated at this step.

If True, this variable will be set back to False at the beginning of the next step.

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

should_log

Whether or not the logs should be reported at this step.

If True, this variable will be set back to False at the beginning of the next step.

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

Source code in mindnlp/engine/callbacks.py
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
@dataclass
class TrainerControl:
    """
    A class that handles the [`Trainer`] control flow. This class is used by the [`TrainerCallback`] to activate some
    switches in the training loop.

    Args:
        should_training_stop (`bool`, *optional*, defaults to `False`):
            Whether or not the training should be interrupted.

            If `True`, this variable will not be set back to `False`. The training will just stop.
        should_epoch_stop (`bool`, *optional*, defaults to `False`):
            Whether or not the current epoch should be interrupted.

            If `True`, this variable will be set back to `False` at the beginning of the next epoch.
        should_save (`bool`, *optional*, defaults to `False`):
            Whether or not the model should be saved at this step.

            If `True`, this variable will be set back to `False` at the beginning of the next step.
        should_evaluate (`bool`, *optional*, defaults to `False`):
            Whether or not the model should be evaluated at this step.

            If `True`, this variable will be set back to `False` at the beginning of the next step.
        should_log (`bool`, *optional*, defaults to `False`):
            Whether or not the logs should be reported at this step.

            If `True`, this variable will be set back to `False` at the beginning of the next step.
    """
    should_training_stop: bool = False
    should_epoch_stop: bool = False
    should_save: bool = False
    should_evaluate: bool = False
    should_log: bool = False

    def _new_training(self):
        """Internal method that resets the variable for a new training."""
        self.should_training_stop = False

    def _new_epoch(self):
        """Internal method that resets the variable for a new epoch."""
        self.should_epoch_stop = False

    def _new_step(self):
        """Internal method that resets the variable for a new step."""
        self.should_save = False
        self.should_evaluate = False
        self.should_log = False

mindnlp.engine.callbacks.TrainerState dataclass

A class containing the [Trainer] inner state that will be saved along the model and optimizer when checkpointing and passed to the [TrainerCallback].

In all this class, one step is to be understood as one update step. When using gradient accumulation, one update step may require several forward and backward passes: if you use gradient_accumulation_steps=n, then one update step requires going through n batches.

PARAMETER DESCRIPTION
epoch

Only set during training, will represent the epoch the training is at (the decimal part being the percentage of the current epoch completed).

TYPE: `float`, *optional* DEFAULT: None

global_step

During training, represents the number of update steps completed.

TYPE: `int`, *optional*, defaults to 0 DEFAULT: 0

max_steps

The number of update steps to do during the current training.

TYPE: `int`, *optional*, defaults to 0 DEFAULT: 0

logging_steps

Log every X updates steps

TYPE: `int`, *optional*, defaults to 500 DEFAULT: 500

eval_steps

Run an evaluation every X steps.

TYPE: `int`, *optional* DEFAULT: 500

save_steps

Save checkpoint every X updates steps.

TYPE: `int`, *optional*, defaults to 500 DEFAULT: 500

train_batch_size

The batch size for the training dataloader. Only needed when auto_find_batch_size has been used.

TYPE: `int`, *optional* DEFAULT: None

num_input_tokens_seen

The number of tokens seen during training (number of input tokens, not the number of prediction tokens).

TYPE: `int`, *optional*, defaults to 0 DEFAULT: 0

total_flos

The total number of floating operations done by the model since the beginning of training (stored as floats to avoid overflow).

TYPE: `float`, *optional*, defaults to 0 DEFAULT: 0

log_history

The list of logs done since the beginning of training.

TYPE: `List[Dict[str, float]]`, *optional* DEFAULT: None

best_metric

When tracking the best model, the value of the best metric encountered so far.

TYPE: `float`, *optional* DEFAULT: None

best_model_checkpoint

When tracking the best model, the value of the name of the checkpoint for the best model encountered so far.

TYPE: `str`, *optional* DEFAULT: None

is_local_process_zero

Whether or not this process is the local (e.g., on one machine if training in a distributed fashion on several machines) main process.

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

is_world_process_zero

Whether or not this process is the global main process (when training in a distributed fashion on several machines, this is only going to be True for one process).

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

is_hyper_param_search

Whether we are in the process of a hyper parameter search using Trainer.hyperparameter_search. This will impact the way data will be logged in TensorBoard.

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

Source code in mindnlp/engine/callbacks.py
 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
@dataclass
class TrainerState:
    """
    A class containing the [`Trainer`] inner state that will be saved along the model and optimizer when checkpointing
    and passed to the [`TrainerCallback`].

    <Tip>

    In all this class, one step is to be understood as one update step. When using gradient accumulation, one update
    step may require several forward and backward passes: if you use `gradient_accumulation_steps=n`, then one update
    step requires going through *n* batches.

    </Tip>

    Args:
        epoch (`float`, *optional*):
            Only set during training, will represent the epoch the training is at (the decimal part being the
            percentage of the current epoch completed).
        global_step (`int`, *optional*, defaults to 0):
            During training, represents the number of update steps completed.
        max_steps (`int`, *optional*, defaults to 0):
            The number of update steps to do during the current training.
        logging_steps (`int`, *optional*, defaults to 500):
            Log every X updates steps
        eval_steps (`int`, *optional*):
            Run an evaluation every X steps.
        save_steps (`int`, *optional*, defaults to 500):
            Save checkpoint every X updates steps.
        train_batch_size (`int`, *optional*):
            The batch size for the training dataloader. Only needed when
            `auto_find_batch_size` has been used.
        num_input_tokens_seen (`int`, *optional*, defaults to 0):
            The number of tokens seen during training (number of input tokens, not the number of prediction tokens).
        total_flos (`float`, *optional*, defaults to 0):
            The total number of floating operations done by the model since the beginning of training (stored as floats
            to avoid overflow).
        log_history (`List[Dict[str, float]]`, *optional*):
            The list of logs done since the beginning of training.
        best_metric (`float`, *optional*):
            When tracking the best model, the value of the best metric encountered so far.
        best_model_checkpoint (`str`, *optional*):
            When tracking the best model, the value of the name of the checkpoint for the best model encountered so
            far.
        is_local_process_zero (`bool`, *optional*, defaults to `True`):
            Whether or not this process is the local (e.g., on one machine if training in a distributed fashion on
            several machines) main process.
        is_world_process_zero (`bool`, *optional*, defaults to `True`):
            Whether or not this process is the global main process (when training in a distributed fashion on several
            machines, this is only going to be `True` for one process).
        is_hyper_param_search (`bool`, *optional*, defaults to `False`):
            Whether we are in the process of a hyper parameter search using Trainer.hyperparameter_search. This will
            impact the way data will be logged in TensorBoard.
    """
    epoch: Optional[float] = None
    global_step: int = 0
    max_steps: int = 0
    logging_steps: int = 500
    eval_steps: int = 500
    save_steps: int = 500
    train_batch_size: int = None
    num_train_epochs: int = 0
    num_input_tokens_seen: int = 0
    total_flos: float = 0
    log_history: List[Dict[str, float]] = None
    best_metric: Optional[float] = None
    best_model_checkpoint: Optional[str] = None
    is_local_process_zero: bool = True
    is_world_process_zero: bool = True
    is_hyper_param_search: bool = False
    trial_name: str = None
    trial_params: Dict[str, Union[str, float, int, bool]] = None

    def __post_init__(self):
        r"""
        Method __post_init__ in the class TrainerState initializes the log_history attribute if it is None.

        Args:
            self: TrainerState object - The instance of the TrainerState class on which this method is called.

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

        Raises:
            None
        """
        if self.log_history is None:
            self.log_history = []

    def save_to_json(self, json_path: str):
        """Save the content of this instance in JSON format inside `json_path`."""
        json_string = json.dumps(dataclasses.asdict(self), indent=2, sort_keys=True) + "\n"
        with open(json_path, "w", encoding="utf-8") as f:
            f.write(json_string)

    @classmethod
    def load_from_json(cls, json_path: str):
        """Create an instance from the content of `json_path`."""
        with open(json_path, "r", encoding="utf-8") as f:
            text = f.read()
        return cls(**json.loads(text))

mindnlp.engine.callbacks.TrainerState.__post_init__()

Method post_init in the class TrainerState initializes the log_history attribute if it is None.

PARAMETER DESCRIPTION
self

TrainerState object - The instance of the TrainerState class on which this method is called.

RETURNS DESCRIPTION
None

This method does not return any value.

Source code in mindnlp/engine/callbacks.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
def __post_init__(self):
    r"""
    Method __post_init__ in the class TrainerState initializes the log_history attribute if it is None.

    Args:
        self: TrainerState object - The instance of the TrainerState class on which this method is called.

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

    Raises:
        None
    """
    if self.log_history is None:
        self.log_history = []

mindnlp.engine.callbacks.TrainerState.load_from_json(json_path) classmethod

Create an instance from the content of json_path.

Source code in mindnlp/engine/callbacks.py
128
129
130
131
132
133
@classmethod
def load_from_json(cls, json_path: str):
    """Create an instance from the content of `json_path`."""
    with open(json_path, "r", encoding="utf-8") as f:
        text = f.read()
    return cls(**json.loads(text))

mindnlp.engine.callbacks.TrainerState.save_to_json(json_path)

Save the content of this instance in JSON format inside json_path.

Source code in mindnlp/engine/callbacks.py
122
123
124
125
126
def save_to_json(self, json_path: str):
    """Save the content of this instance in JSON format inside `json_path`."""
    json_string = json.dumps(dataclasses.asdict(self), indent=2, sort_keys=True) + "\n"
    with open(json_path, "w", encoding="utf-8") as f:
        f.write(json_string)