utils
mindnlp.engine.utils
¶
Utils for engine
mindnlp.engine.utils.BestRun
¶
Bases: NamedTuple
The best run found by a hyperparameter search (see [~Trainer.hyperparameter_search
]).
PARAMETER | DESCRIPTION |
---|---|
run_id |
The id of the best run (if models were saved, the corresponding checkpoint will be in the folder ending with run-{run_id}).
TYPE:
|
objective |
The objective that was obtained for this run.
TYPE:
|
hyperparameters |
The hyperparameters picked to get this run.
TYPE:
|
run_summary |
A summary of tuning experiments.
TYPE:
|
Source code in mindnlp/engine/utils.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
|
mindnlp.engine.utils.EvalLoopOutput
¶
Bases: NamedTuple
Represents an output from an evaluation loop.
This class represents the output from an evaluation loop and inherits from NamedTuple.
Source code in mindnlp/engine/utils.py
298 299 300 301 302 303 304 305 306 307 308 |
|
mindnlp.engine.utils.EvalPrediction
¶
Evaluation output (always contains labels), to be used to compute metrics.
PARAMETER | DESCRIPTION |
---|---|
predictions |
Predictions of the model.
TYPE:
|
label_ids |
Targets to be matched.
TYPE:
|
inputs |
TYPE:
|
Source code in mindnlp/engine/utils.py
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 |
|
mindnlp.engine.utils.EvalPrediction.__getitem__(idx)
¶
Description
This method allows for accessing elements within the EvalPrediction object using index values.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the EvalPrediction class.
TYPE:
|
idx |
The index value used to access elements within the EvalPrediction object. Must be an integer within the range of 0 to 2, inclusive.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method does not return any value directly, it accesses and returns specific attributes based on the provided index. |
RAISES | DESCRIPTION |
---|---|
IndexError
|
Raised if the provided index is less than 0, greater than 2, or equal to 2 when self.inputs is None. |
Source code in mindnlp/engine/utils.py
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 |
|
mindnlp.engine.utils.EvalPrediction.__init__(predictions, label_ids, inputs=None)
¶
Initializes an instance of the EvalPrediction class.
PARAMETER | DESCRIPTION |
---|---|
predictions |
The predictions made by the model. It can be either a NumPy array or a tuple of NumPy arrays.
TYPE:
|
label_ids |
The label ids used for evaluation. It can be either a NumPy array or a tuple of NumPy arrays.
TYPE:
|
inputs |
The input data used for evaluation. It can be either a NumPy array or a tuple of NumPy arrays. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. This method does not return any value. |
Source code in mindnlp/engine/utils.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
|
mindnlp.engine.utils.EvalPrediction.__iter__()
¶
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the EvalPrediction class. It is used to access the attributes and methods of the EvalPrediction class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
iter
|
An iterator object that iterates over the predictions and label_ids attributes of the EvalPrediction instance. |
Source code in mindnlp/engine/utils.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
|
mindnlp.engine.utils.EvaluationStrategy
¶
Bases: ExplicitEnum
Representation of an evaluation strategy for a system.
This class defines a specific evaluation strategy that can be applied to a system. It inherits properties and methods from the ExplicitEnum class, providing additional functionality and customization
options. An evaluation strategy determines how the system processes and analyzes data to make informed decisions or assessments. Subclasses of EvaluationStrategy can implement different strategies tailored to specific use cases or requirements.
Source code in mindnlp/engine/utils.py
150 151 152 153 154 155 156 157 158 159 160 161 |
|
mindnlp.engine.utils.HubStrategy
¶
Bases: ExplicitEnum
Represents a hub strategy for managing connections and communication. This class inherits from the ExplicitEnum class and provides methods to define and handle different strategies for hub operations.
Source code in mindnlp/engine/utils.py
164 165 166 167 168 169 170 171 172 173 |
|
mindnlp.engine.utils.IntervalStrategy
¶
Bases: ExplicitEnum
Represents a strategy for handling intervals in a specific context.
This class inherits from the ExplicitEnum class, which provides an enumeration-like behavior with explicit values. The IntervalStrategy class is designed to be used in situations where intervals need to be
managed and processed according to a specific strategy.
Attributes:
- strategy_name (str): The name of the interval strategy.
- strategy_description (str): A brief description of the interval strategy.
Methods:
- process_interval(interval): Processes the given interval based on the specific strategy.
Examples:
>>> strategy = IntervalStrategy('Strategy A', 'This strategy handles intervals by merging overlapping intervals.')
>>> strategy.process_interval((1, 5))
(1, 5)
>>> strategy.process_interval((3, 7))
(1, 7)
Note:
The IntervalStrategy class should not be instantiated directly. Instead, use one of its derived classes that implement specific interval handling strategies.
Source code in mindnlp/engine/utils.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
|
mindnlp.engine.utils.LabelSmoother
dataclass
¶
Adds label-smoothing on a pre-computed output from a Transformers model.
PARAMETER | DESCRIPTION |
---|---|
epsilon |
The label smoothing factor.
TYPE:
|
ignore_index |
The index in the labels to ignore when computing the loss.
TYPE:
|
Source code in mindnlp/engine/utils.py
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 |
|
mindnlp.engine.utils.LabelSmoother.__call__(model_output, labels, shift_labels=False)
¶
This method performs label smoothing for the given model output and labels.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the LabelSmoother class.
TYPE:
|
model_output |
The output of the model, which can be a dictionary containing 'logits' key or a list.
TYPE:
|
labels |
The ground truth labels for the model output.
TYPE:
|
shift_labels |
A flag indicating whether to shift the labels for label smoothing. Defaults to False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method does not return any value. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the dimensions of labels and log_probs do not match. |
RuntimeError
|
If any runtime error occurs during the label smoothing process. |
Source code in mindnlp/engine/utils.py
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 |
|
mindnlp.engine.utils.PredictionOutput
¶
Bases: NamedTuple
Represents the output of a prediction process, containing the predicted values and associated metadata.
This class inherits from NamedTuple and provides a structured way to store and access the output of prediction tasks. It includes attributes for the predicted values and any additional metadata related to
the prediction process.
Attributes:
predicted_values (Any): The predicted values generated by the prediction process.
metadata (Dict[str, Any]): Additional metadata associated with the prediction, stored as key-value pairs.
Note:
This class is designed to provide a standardized and organized representation of prediction outputs, making it easier to work with and analyze the results of prediction tasks.
Source code in mindnlp/engine/utils.py
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
|
mindnlp.engine.utils.SchedulerType
¶
Bases: ExplicitEnum
Represents a scheduler type that inherits from the ExplicitEnum class.
The SchedulerType class provides a way to define different types of schedulers by extending the functionality of the ExplicitEnum class. A scheduler type is used to specify the behavior and characteristics
of a scheduler in a system.
Attributes:
- name (str): The name of the scheduler type.
- value (Any): The value associated with the scheduler type.
Methods:
- __init__(self, name: str, value: Any): Initializes a new instance of the SchedulerType class with the specified name and value.
- __str__(self) -> str: Returns the string representation of the SchedulerType instance.
- __repr__(self) -> str: Returns the string representation of the SchedulerType instance that can be used to recreate the instance.
Inherits From:
- ExplicitEnum: A base class for creating explicit enumeration-like objects.
Usage:
To use the SchedulerType class, create a new instance with a name and value, and optionally provide custom implementations for the __str__ and __repr__ methods.
Example:
>> type1 = SchedulerType("Type 1", 1)
>> print(type1)
Type 1
>> type2 = SchedulerType("Type 2", 2)
>> print(type2)
Type 2
>> repr(type1)
<SchedulerType: Type 1>
>> repr(type2)
<SchedulerType: Type 2>
Source code in mindnlp/engine/utils.py
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 |
|
mindnlp.engine.utils.TrainOutput
¶
Bases: NamedTuple
TrainOutput represents the output of a machine learning model training process.
TrainOutput inherits from NamedTuple, providing a convenient way to represent a named tuple with a fixed set of fields. The TrainOutput class encapsulates the results and metrics obtained during the
training of a machine learning model.
Attributes:
<attribute_name> (type): Description of the attribute.
Methods:
<method_name>(<parameters>): Description of the method.
Examples:
>>> output = TrainOutput(...)
>>> output.attribute_name
attribute_value
Note:
The TrainOutput class is designed to be immutable, meaning that its attributes cannot be modified after instantiation.
Source code in mindnlp/engine/utils.py
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 |
|
mindnlp.engine.utils.args_only_in_map_fn(map_fn_args, col_names)
¶
This function filters the elements in 'map_fn_args' that are not present in 'col_names'.
PARAMETER | DESCRIPTION |
---|---|
map_fn_args |
A list of elements to be filtered.
TYPE:
|
col_names |
A list of elements to compare against.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
list
|
A list of elements from 'map_fn_args' that are not present in 'col_names'. |
Source code in mindnlp/engine/utils.py
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 |
|
mindnlp.engine.utils.atleast_1d(tensor_or_array)
¶
Converts the input tensor or array to at least one dimension.
PARAMETER | DESCRIPTION |
---|---|
tensor_or_array |
The input tensor or array to be converted.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
The converted tensor or array, with at least one dimension. |
Source code in mindnlp/engine/utils.py
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 |
|
mindnlp.engine.utils.check_input_output_count(fn)
¶
Checks the input and output parameter count of a given function.
PARAMETER | DESCRIPTION |
---|---|
fn |
The function for which the input and output parameter count needs to be checked.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
Returns True if the number of input parameters matches the number of output parameters; otherwise, False. |
RAISES | DESCRIPTION |
---|---|
<Exception Type>
|
|
<Exception Type>
|
|
Source code in mindnlp/engine/utils.py
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 |
|
mindnlp.engine.utils.convert_tensor_to_scalar(data)
¶
Converts tensor objects within nested dictionaries and lists to scalar values.
PARAMETER | DESCRIPTION |
---|---|
data |
The input data structure containing nested dictionaries
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This function does not return any value. It modifies the input data structure in place. |
Source code in mindnlp/engine/utils.py
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 |
|
mindnlp.engine.utils.denumpify_detensorize(metrics)
¶
Recursively calls .item()
on the element of the dictionary passed
Source code in mindnlp/engine/utils.py
548 549 550 551 552 553 554 555 556 557 558 559 560 |
|
mindnlp.engine.utils.enable_full_determinism(seed, warn_only=False)
¶
Helper function for reproducible behavior during distributed training. See - https://www.tensorflow.org/api_docs/python/tf/config/experimental/enable_op_determinism for tensorflow
Source code in mindnlp/engine/utils.py
221 222 223 224 225 226 227 228 229 230 |
|
mindnlp.engine.utils.find_batch_size(tensors)
¶
Find the first dimension of a tensor in a nested list/tuple/dict of tensors.
Source code in mindnlp/engine/utils.py
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 |
|
mindnlp.engine.utils.find_executable_batch_size(function=None, starting_batch_size=128, auto_find_batch_size=False)
¶
A basic decorator that will try to execute function
. If it fails from exceptions related to out-of-memory or
CUDNN, the batch size is cut in half and passed to function
. function
must take in a batch_size
parameter as
its first argument.
function (callable
, optional)
A function to wrap
starting_batch_size (int
, optional)
The batch size to try and fit into memory
auto_find_batch_size (bool
, optional)
If False, will just execute function
Source code in mindnlp/engine/utils.py
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
|
mindnlp.engine.utils.get_function_args(fn)
¶
This function retrieves the names of the parameters of a given function.
PARAMETER | DESCRIPTION |
---|---|
fn |
The function whose parameter names need to be retrieved.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This function returns a value of type None. |
RAISES | DESCRIPTION |
---|---|
None
|
This function does not raise any exceptions. |
Source code in mindnlp/engine/utils.py
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 |
|
mindnlp.engine.utils.get_last_checkpoint(folder)
¶
This function returns the path to the most recent checkpoint folder within the specified folder.
PARAMETER | DESCRIPTION |
---|---|
folder |
The path to the folder containing the checkpoint folders.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
The path to the most recent checkpoint folder within the specified folder. |
Source code in mindnlp/engine/utils.py
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
|
mindnlp.engine.utils.get_model_param_count(model, trainable_only=False)
¶
Calculate model's total param count. If trainable_only is True then count only those requiring grads
Source code in mindnlp/engine/utils.py
473 474 475 476 477 |
|
mindnlp.engine.utils.get_parameter_names(model, forbidden_layer_types)
¶
Returns the names of the model parameters that are not inside a forbidden layer.
Source code in mindnlp/engine/utils.py
458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
|
mindnlp.engine.utils.has_length(dataset)
¶
Checks if the dataset implements len() and it doesn't raise an error
Source code in mindnlp/engine/utils.py
196 197 198 199 200 201 202 203 204 |
|
mindnlp.engine.utils.mismatch_dataset_col_names(map_fn_args, col_names)
¶
Checks if all elements of the map_fn_args parameter are present in the col_names parameter.
PARAMETER | DESCRIPTION |
---|---|
map_fn_args |
A list of strings representing the column names to be checked.
TYPE:
|
col_names |
A list of strings representing the available column names.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
Returns True if all elements of map_fn_args are present in col_names, otherwise returns False. |
RAISES | DESCRIPTION |
---|---|
None
|
This function does not raise any exceptions. |
Source code in mindnlp/engine/utils.py
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 |
|
mindnlp.engine.utils.ms_pad_and_concatenate(tensor1, tensor2, padding_index=-100)
¶
Concatenates tensor1
and tensor2
on first axis, applying padding on the second if necessary.
Source code in mindnlp/engine/utils.py
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 |
|
mindnlp.engine.utils.neftune_post_forward_hook(module, input, output)
¶
Implements the NEFTune forward pass for the model using forward hooks. Note this works only for mindspore.nn.Embedding layers. This method is slightly adapted from the original source code that can be found here: https://github.com/neelsjain/NEFTune Simply add it to your model as follows:
model = ...
model.embed_tokens.neftune_noise_alpha = 0.1
model.embed_tokens.register_forward_hook(neftune_post_forward_hook)
mindspore.nn.cell
):
The embedding module where the hook is attached. Note that you need to set module.neftune_noise_alpha
to
the desired noise alpha value.
input (mindspore.tensor
):
The input tensor to the model.
output (mindspore.tensor
):
The output tensor of the model (i.e. the embeddings).
Source code in mindnlp/engine/utils.py
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 |
|
mindnlp.engine.utils.nested_concat(tensors, new_tensors, padding_index=-100)
¶
Concat the new_tensors
to tensors
on the first dim and pad them on the second if needed. Works for tensors or
nested list/tuples/dict of tensors.
Source code in mindnlp/engine/utils.py
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 |
|
mindnlp.engine.utils.nested_numpify(tensors)
¶
Numpify tensors
(even if it's a nested list/tuple/dict of tensors).
Source code in mindnlp/engine/utils.py
666 667 668 669 670 671 672 673 674 675 676 677 678 |
|
mindnlp.engine.utils.number_of_arguments(func)
¶
Return the number of arguments of the passed function, even if it's a partial function.
Source code in mindnlp/engine/utils.py
289 290 291 292 293 294 295 296 |
|
mindnlp.engine.utils.numpy_pad_and_concatenate(array1, array2, padding_index=-100)
¶
Concatenates array1
and array2
on first axis, applying padding on the second if necessary.
Source code in mindnlp/engine/utils.py
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 |
|
mindnlp.engine.utils.set_seed(seed)
¶
Helper function for reproducible behavior to set the seed in random
, numpy
, mindspore
and/or tf
(if installed).
PARAMETER | DESCRIPTION |
---|---|
seed |
The seed to set.
TYPE:
|
Source code in mindnlp/engine/utils.py
207 208 209 210 211 212 213 214 215 216 217 218 219 |
|
mindnlp.engine.utils.speed_metrics(split, start_time, num_samples=None, num_steps=None, num_tokens=None)
¶
Measure and return speed performance metrics.
This function requires a time snapshot start_time
before the operation to be measured starts and this function
should be run immediately after the operation to be measured has completed.
Args: - split: name to prefix metric (like train, eval, test...) - start_time: operation start time - num_samples: number of samples processed - num_steps: number of steps processed - num_tokens: number of tokens processed
Source code in mindnlp/engine/utils.py
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 |
|