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 |
|
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:
|
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:
|
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 |
|
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:
|
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:
|
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 |
|
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:
|
event |
The name of the event to be called on each callback.
TYPE:
|
args |
The arguments to be passed to the event callback.
TYPE:
|
state |
The current state of the system.
TYPE:
|
control |
The initial control parameter for the event handling.
TYPE:
|
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 |
|
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 |
|
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:
|
args |
The training arguments for the model.
TYPE:
|
state |
The state of the trainer during training.
TYPE:
|
control |
The control object for the trainer.
TYPE:
|
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 |
|
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 |
|
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:
|
state |
An object representing the state of the trainer.
TYPE:
|
control |
An object providing control over the trainer.
TYPE:
|
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 |
|
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 |
|
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 |
|
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:
|
args |
The training arguments for the current training session.
TYPE:
|
state |
The current state of the trainer during training.
TYPE:
|
control |
The control object for the trainer.
TYPE:
|
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 |
|
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:
|
args |
An object containing training arguments. This parameter provides information such as hyperparameters and settings for the training process.
TYPE:
|
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:
|
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:
|
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 |
|
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:
|
state |
TrainerState An object containing the current state of the Trainer.
TYPE:
|
control |
TrainerControl An object providing control over the behavior of the Trainer.
TYPE:
|
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 |
|
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:
|
args |
An object that contains the training arguments. This includes various parameters such as the number of epochs, batch size, learning rate, etc.
TYPE:
|
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:
|
control |
An object that allows the CallbackHandler to control the training process. It provides methods to pause, resume, or stop the training.
TYPE:
|
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 |
|
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:
|
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:
|
control |
An object that allows control over the training process. This parameter provides methods to pause, resume, or stop the training.
TYPE:
|
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 |
|
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:
|
- |
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:
|
- |
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:
|
- |
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:
|
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 |
|
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:
|
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:
|
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:
|
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 |
|
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:
|
callback |
The callback to be removed from the list. It can be either a function or a class type.
TYPE:
|
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 |
|
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:
|
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:
|
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 |
|
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 |
|
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:
|
args |
The training arguments containing various settings for training. This parameter is mandatory and must be of type TrainingArguments.
TYPE:
|
state |
The current state of the Trainer. This parameter is mandatory and must be of type TrainerState.
TYPE:
|
control |
The control object that determines the actions to be taken during training. This parameter is mandatory and must be of type TrainerControl.
TYPE:
|
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 |
|
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:
|
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:
|
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:
|
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 |
|
mindnlp.engine.callbacks.EarlyStoppingCallback
¶
Bases: TrainerCallback
A [TrainerCallback
] that handles early stopping.
PARAMETER | DESCRIPTION |
---|---|
early_stopping_patience |
Use with
TYPE:
|
early_stopping_threshold(`float`, |
Use with TrainingArguments
TYPE:
|
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 |
|
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:
|
early_stopping_threshold |
The minimum improvement required in the monitored metric to be considered as improvement. Defaults to 0.0.
TYPE:
|
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 |
|
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 |
|
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 |
|
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:
|
- |
A dictionary containing various settings for the training process. Type: dict Purpose: Contains configuration settings for the training process. Restrictions: None
TYPE:
|
- |
Represents the current state of the training process. Type: any Purpose: Provides information about the current state of the training. Restrictions: None
TYPE:
|
- |
A control variable to manage the training process. Type: any Purpose: Helps in controlling the flow of the training process. Restrictions: None
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
|
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 |
|
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 |
|
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:
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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:
|
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 |
|
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 |
|
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:
|
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 |
|
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:
|
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 |
|
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 |
|
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 [
TYPE:
|
state |
The current state of the [
TYPE:
|
control |
The object that is returned to the [
TYPE:
|
model |
The model being trained.
TYPE:
|
tokenizer |
The tokenizer used for encoding the data.
TYPE:
|
optimizer |
The optimizer used for the training steps.
TYPE:
|
lr_scheduler |
The scheduler used for setting the learning rate.
TYPE:
|
train_dataset |
The current dataloader used for training.
TYPE:
|
eval_dataset |
The current dataloader used for training.
TYPE:
|
metrics |
The metrics computed by the last evaluation phase. Those are only accessible in the event
TYPE:
|
logs |
The values to log. Those are only accessible in the event
TYPE:
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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
TYPE:
|
should_epoch_stop |
Whether or not the current epoch should be interrupted. If
TYPE:
|
should_save |
Whether or not the model should be saved at this step. If
TYPE:
|
should_evaluate |
Whether or not the model should be evaluated at this step. If
TYPE:
|
should_log |
Whether or not the logs should be reported at this step. If
TYPE:
|
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 |
|
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:
|
global_step |
During training, represents the number of update steps completed.
TYPE:
|
max_steps |
The number of update steps to do during the current training.
TYPE:
|
logging_steps |
Log every X updates steps
TYPE:
|
eval_steps |
Run an evaluation every X steps.
TYPE:
|
save_steps |
Save checkpoint every X updates steps.
TYPE:
|
train_batch_size |
The batch size for the training dataloader. Only needed when
TYPE:
|
num_input_tokens_seen |
The number of tokens seen during training (number of input tokens, not the number of prediction tokens).
TYPE:
|
total_flos |
The total number of floating operations done by the model since the beginning of training (stored as floats to avoid overflow).
TYPE:
|
log_history |
The list of logs done since the beginning of training.
TYPE:
|
best_metric |
When tracking the best model, the value of the best metric encountered so far.
TYPE:
|
best_model_checkpoint |
When tracking the best model, the value of the name of the checkpoint for the best model encountered so far.
TYPE:
|
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:
|
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
TYPE:
|
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:
|
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 |
|
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 |
|
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 |
|
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 |
|