stopping_criteria
mindnlp.transformers.generation.stopping_criteria
¶
Stopping criteria
mindnlp.transformers.generation.stopping_criteria.MaxLengthCriteria
¶
Bases: StoppingCriteria
This class can be used to stop generation whenever the full generated number of tokens exceeds max_length
. Keep
in mind for decoder-only type of transformers, this will include the initial prompted tokens.
PARAMETER | DESCRIPTION |
---|---|
max_length |
The maximum length that the output sequence can have in number of tokens.
TYPE:
|
Source code in mindnlp/transformers/generation/stopping_criteria.py
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 |
|
mindnlp.transformers.generation.stopping_criteria.MaxLengthCriteria.__call__(input_ids, scores, **kwargs)
¶
This method evaluates whether the length of input_ids meets the maximum length criteria.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MaxLengthCriteria class.
TYPE:
|
input_ids |
The input tensor representing the input ids.
TYPE:
|
scores |
The tensor containing scores associated with the input_ids.
TYPE:
|
**kwargs |
Additional keyword arguments.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
Returns True if the length of input_ids meets the maximum length criteria, otherwise False.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
TypeError
|
If input_ids or scores are not of type mindspore.Tensor. |
ValueError
|
If input_ids or scores are empty or have incompatible shapes. |
Source code in mindnlp/transformers/generation/stopping_criteria.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
mindnlp.transformers.generation.stopping_criteria.MaxLengthCriteria.__init__(max_length)
¶
Initializes a MaxLengthCriteria object with the specified maximum length.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MaxLengthCriteria class.
TYPE:
|
max_length |
The maximum length to be set for the criteria. It must be a positive integer.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If max_length is not a positive integer. |
Source code in mindnlp/transformers/generation/stopping_criteria.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
|
mindnlp.transformers.generation.stopping_criteria.MaxNewTokensCriteria
¶
Bases: StoppingCriteria
This class can be used to stop generation whenever the generated number of tokens exceeds max_new_tokens
. Keep in
mind for decoder-only type of transformers, this will not include the initial prompted tokens. This is very
close to MaxLengthCriteria
but ignores the number of initial tokens.
PARAMETER | DESCRIPTION |
---|---|
start_length |
The number of initial tokens.
TYPE:
|
max_new_tokens |
The maximum number of tokens to generate.
TYPE:
|
Source code in mindnlp/transformers/generation/stopping_criteria.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
mindnlp.transformers.generation.stopping_criteria.MaxNewTokensCriteria.__call__(input_ids, scores, **kwargs)
¶
This method evaluates the condition for the maximum number of new tokens based on the input ids and scores.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MaxNewTokensCriteria class.
TYPE:
|
input_ids |
A tensor containing the input ids.
TYPE:
|
scores |
A tensor containing the scores associated with the input ids.
TYPE:
|
**kwargs |
Additional keyword arguments that are not used in this method.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
Returns True if the length of the input_ids is greater than or equal to the max_length defined in the MaxNewTokensCriteria instance; otherwise, returns False.
TYPE:
|
Source code in mindnlp/transformers/generation/stopping_criteria.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
mindnlp.transformers.generation.stopping_criteria.MaxNewTokensCriteria.__init__(start_length, max_new_tokens)
¶
Initializes an instance of the MaxNewTokensCriteria class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MaxNewTokensCriteria class.
|
start_length |
The starting length value for the criteria.
TYPE:
|
max_new_tokens |
The maximum number of new tokens allowed.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
FutureWarning
|
If the MaxNewTokensCriteria class is deprecated. Suggests using MaxLengthCriteria with max_length equal to start_length plus max_new_tokens instead. |
Source code in mindnlp/transformers/generation/stopping_criteria.py
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 |
|
mindnlp.transformers.generation.stopping_criteria.MaxTimeCriteria
¶
Bases: StoppingCriteria
This class can be used to stop generation whenever the full generation exceeds some amount of time. By default, the
time will start being counted when you initialize this function. You can override this by passing an
initial_time
.
PARAMETER | DESCRIPTION |
---|---|
max_time |
The maximum allowed time in seconds for the generation.
TYPE:
|
initial_time |
The start of the generation allowed time.
TYPE:
|
Source code in mindnlp/transformers/generation/stopping_criteria.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
mindnlp.transformers.generation.stopping_criteria.MaxTimeCriteria.__call__(input_ids, scores, **kwargs)
¶
This method represents the call functionality of the MaxTimeCriteria class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MaxTimeCriteria class.
TYPE:
|
input_ids |
The tensor containing input IDs. This parameter is used to pass input IDs to the method.
TYPE:
|
scores |
The tensor containing scores. This parameter is used to pass scores to the method.
TYPE:
|
**kwargs |
Additional keyword arguments that may be passed but are not used in this method.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
A boolean value indicating whether the time elapsed since the initial timestamp exceeds the maximum allowed time defined by self.max_time.
TYPE:
|
Source code in mindnlp/transformers/generation/stopping_criteria.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
mindnlp.transformers.generation.stopping_criteria.MaxTimeCriteria.__init__(max_time, initial_timestamp=None)
¶
Initialize a MaxTimeCriteria object.
PARAMETER | DESCRIPTION |
---|---|
max_time |
The maximum time value for the criteria. Must be a non-negative float representing the maximum time in seconds.
TYPE:
|
initial_timestamp |
The initial timestamp for the criteria. If provided, it should be a float representing the initial timestamp in seconds. Defaults to None, in which case the current time will be used.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/generation/stopping_criteria.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
|
mindnlp.transformers.generation.stopping_criteria.StoppingCriteria
¶
Abstract base class for all stopping criteria that can be applied during generation.
Source code in mindnlp/transformers/generation/stopping_criteria.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
mindnlp.transformers.generation.stopping_criteria.StoppingCriteria.__call__(input_ids, scores, **kwargs)
¶
This method is the call method for the StoppingCriteria class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the StoppingCriteria class.
TYPE:
|
input_ids |
The input tensor containing the IDs.
TYPE:
|
scores |
The input tensor containing the scores.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
Returns a boolean value indicating whether the stopping criteria has been met.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
NotImplementedError
|
This exception is raised if the StoppingCriteria class is used directly instead of being subclassed. |
Source code in mindnlp/transformers/generation/stopping_criteria.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
mindnlp.transformers.generation.stopping_criteria.StoppingCriteriaList
¶
Bases: list
StoppingCriteriaList
Source code in mindnlp/transformers/generation/stopping_criteria.py
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 |
|
mindnlp.transformers.generation.stopping_criteria.StoppingCriteriaList.max_length: Optional[int]
property
¶
return max length
mindnlp.transformers.generation.stopping_criteria.StoppingCriteriaList.__call__(input_ids, scores, **kwargs)
¶
This method 'call' in the class 'StoppingCriteriaList' evaluates a list of stopping criteria against the input data.
PARAMETER | DESCRIPTION |
---|---|
self |
Represents the instance of the StoppingCriteriaList class.
|
input_ids |
Tensor containing input IDs for evaluation.
TYPE:
|
scores |
Tensor containing scores for evaluation.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
Returns a boolean value indicating whether any of the stopping criteria have been met.
TYPE:
|
Source code in mindnlp/transformers/generation/stopping_criteria.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
|
mindnlp.transformers.generation.stopping_criteria.validate_stopping_criteria(stopping_criteria, max_length)
¶
validate stopping criteria
Source code in mindnlp/transformers/generation/stopping_criteria.py
235 236 237 238 239 240 241 242 243 |
|