μμ±μ μν μ νΈλ¦¬ν° [[utilities-for-generation]]
μ΄ νμ΄μ§λ [~generation.GenerationMixin.generate]μμ μ¬μ©λλ λͺ¨λ μ νΈλ¦¬ν° ν¨μλ€μ λμ΄ν©λλ€.
μΆλ ₯μ μμ±νκΈ° (Generate Outputs) [[generate-outputs]]
[~generation.GenerationMixin.generate]μ μΆλ ₯μ [~utils.ModelOutput]μ νμ ν΄λμ€μ μΈμ€ν΄μ€μ
λλ€. μ΄ μΆλ ₯μ [~generation.GenerationMixin.generate]μμ λ°νλλ λͺ¨λ μ 보λ₯Ό ν¬ν¨νλ λ°μ΄ν° ꡬ쑰체μ΄λ©°, νν λλ λμ
λ리λ‘λ μ¬μ©ν μ μμ΅λλ€.
λ€μμ μμμ λλ€:
from transformers import GPT2Tokenizer, GPT2LMHeadModel
tokenizer = GPT2Tokenizer.from_pretrained("openai-community/gpt2")
model = GPT2LMHeadModel.from_pretrained("openai-community/gpt2")
inputs = tokenizer("Hello, my dog is cute and ", return_tensors="pt")
generation_output = model.generate(**inputs, return_dict_in_generate=True, output_scores=True)
generation_output κ°μ²΄λ [~generation.GenerateDecoderOnlyOutput]μ
λλ€. μλ λ¬Έμμμ νμΈν μ μλ―μ΄, μ΄ ν΄λμ€λ λ€μκ³Ό κ°μ μμ±μ κ°μ§κ³ μμ΅λλ€:
sequences: μμ±λ ν ν° μνμ€scores(μ΅μ ): κ° μμ± λ¨κ³μμ μΈμ΄ λͺ¨λΈλ§ ν€λμ μμΈ‘ μ μhidden_states(μ΅μ ): κ° μμ± λ¨κ³μμ λͺ¨λΈμ μλ μνattentions(μ΅μ ): κ° μμ± λ¨κ³μμ λͺ¨λΈμ μ΄ν μ κ°μ€μΉ
output_scores=Trueλ₯Ό μ λ¬νκΈ° λλ¬Έμ scoresλ ν¬ν¨λμ΄ μμ§λ§, output_hidden_states=True λλ output_attentions=Trueλ₯Ό μ λ¬νμ§ μμμΌλ―λ‘ hidden_statesμ attentionsλ ν¬ν¨λμ§ μμμ΅λλ€.
κ° μμ±μ μΌλ°μ μΌλ‘ μ κ·Όν μ μμΌλ©°, λͺ¨λΈμ΄ ν΄λΉ μμ±μ λ°ννμ§ μμλ€λ©΄ Noneμ΄ λ°νλ©λλ€. μλ₯Ό λ€μ΄, generation_output.scoresλ μΈμ΄ λͺ¨λΈλ§ ν€λμμ μμ±λ λͺ¨λ μμΈ‘ μ μλ₯Ό ν¬ν¨νκ³ μμΌλ©°, generation_output.attentionsλ Noneμ
λλ€.
generation_output κ°μ²΄λ₯Ό ννλ‘ μ¬μ©ν κ²½μ°, None κ°μ΄ μλ μμ±λ§ ν¬ν¨λ©λλ€. μλ₯Ό λ€μ΄, lossμ logitsλΌλ λ μμκ° ν¬ν¨λ κ²½μ°:
generation_output[:2]
μ μ½λλ (generation_output.sequences, generation_output.scores) ννμ λ°νν©λλ€.
generation_output κ°μ²΄λ₯Ό λμ
λλ¦¬λ‘ μ¬μ©ν κ²½μ°, None κ°μ΄ μλ μμ±λ§ ν¬ν¨λ©λλ€. μλ₯Ό λ€μ΄, sequencesμ scoresλΌλ λ κ°μ ν€λ₯Ό κ°μ§ μ μμ΅λλ€.
μ¬κΈ°μλ λͺ¨λ μΆλ ₯ μ νμ λ¬Έμνν©λλ€.
PyTorch [[transformers.generation.GenerateDecoderOnlyOutput]]
[[autodoc]] generation.GenerateDecoderOnlyOutput
[[autodoc]] generation.GenerateEncoderDecoderOutput
[[autodoc]] generation.GenerateBeamDecoderOnlyOutput
[[autodoc]] generation.GenerateBeamEncoderDecoderOutput
TensorFlow [[transformers.generation.TFGreedySearchEncoderDecoderOutput]]
[[autodoc]] generation.TFGreedySearchEncoderDecoderOutput
[[autodoc]] generation.TFGreedySearchDecoderOnlyOutput
[[autodoc]] generation.TFSampleEncoderDecoderOutput
[[autodoc]] generation.TFSampleDecoderOnlyOutput
[[autodoc]] generation.TFBeamSearchEncoderDecoderOutput
[[autodoc]] generation.TFBeamSearchDecoderOnlyOutput
[[autodoc]] generation.TFBeamSampleEncoderDecoderOutput
[[autodoc]] generation.TFBeamSampleDecoderOnlyOutput
[[autodoc]] generation.TFContrastiveSearchEncoderDecoderOutput
[[autodoc]] generation.TFContrastiveSearchDecoderOnlyOutput
FLAX [[transformers.generation.FlaxSampleOutput]]
[[autodoc]] generation.FlaxSampleOutput
[[autodoc]] generation.FlaxGreedySearchOutput
[[autodoc]] generation.FlaxBeamSearchOutput
LogitsProcessor [[logitsprocessor]]
[LogitsProcessor]λ μμ± μ€ μΈμ΄ λͺ¨λΈ ν€λμ μμΈ‘ μ μλ₯Ό μμ νλ λ° μ¬μ©λ©λλ€.
PyTorch [[transformers.AlternatingCodebooksLogitsProcessor]]
[[autodoc]] AlternatingCodebooksLogitsProcessor - call
[[autodoc]] ClassifierFreeGuidanceLogitsProcessor - call
[[autodoc]] EncoderNoRepeatNGramLogitsProcessor - call
[[autodoc]] EncoderRepetitionPenaltyLogitsProcessor - call
[[autodoc]] EpsilonLogitsWarper - call
[[autodoc]] EtaLogitsWarper - call
[[autodoc]] ExponentialDecayLengthPenalty - call
[[autodoc]] ForcedBOSTokenLogitsProcessor - call
[[autodoc]] ForcedEOSTokenLogitsProcessor - call
[[autodoc]] HammingDiversityLogitsProcessor - call
[[autodoc]] InfNanRemoveLogitsProcessor - call
[[autodoc]] LogitNormalization - call
[[autodoc]] LogitsProcessor - call
[[autodoc]] LogitsProcessorList - call
[[autodoc]] MinLengthLogitsProcessor - call
[[autodoc]] MinNewTokensLengthLogitsProcessor - call
[[autodoc]] MinPLogitsWarper - call
[[autodoc]] NoBadWordsLogitsProcessor - call
[[autodoc]] NoRepeatNGramLogitsProcessor - call
[[autodoc]] PrefixConstrainedLogitsProcessor - call
[[autodoc]] RepetitionPenaltyLogitsProcessor - call
[[autodoc]] SequenceBiasLogitsProcessor - call
[[autodoc]] SuppressTokensAtBeginLogitsProcessor - call
[[autodoc]] SuppressTokensLogitsProcessor - call
[[autodoc]] TemperatureLogitsWarper - call
[[autodoc]] TopKLogitsWarper - call
[[autodoc]] TopPLogitsWarper - call
[[autodoc]] TypicalLogitsWarper - call
[[autodoc]] UnbatchedClassifierFreeGuidanceLogitsProcessor - call
[[autodoc]] WhisperTimeStampLogitsProcessor - call
[[autodoc]] WatermarkLogitsProcessor - call
TensorFlow [[transformers.TFForcedBOSTokenLogitsProcessor]]
[[autodoc]] TFForcedBOSTokenLogitsProcessor - call
[[autodoc]] TFForcedEOSTokenLogitsProcessor - call
[[autodoc]] TFForceTokensLogitsProcessor - call
[[autodoc]] TFLogitsProcessor - call
[[autodoc]] TFLogitsProcessorList - call
[[autodoc]] TFLogitsWarper - call
[[autodoc]] TFMinLengthLogitsProcessor - call
[[autodoc]] TFNoBadWordsLogitsProcessor - call
[[autodoc]] TFNoRepeatNGramLogitsProcessor - call
[[autodoc]] TFRepetitionPenaltyLogitsProcessor - call
[[autodoc]] TFSuppressTokensAtBeginLogitsProcessor - call
[[autodoc]] TFSuppressTokensLogitsProcessor - call
[[autodoc]] TFTemperatureLogitsWarper - call
[[autodoc]] TFTopKLogitsWarper - call
[[autodoc]] TFTopPLogitsWarper - call
FLAX [[transformers.FlaxForcedBOSTokenLogitsProcessor]]
[[autodoc]] FlaxForcedBOSTokenLogitsProcessor - call
[[autodoc]] FlaxForcedEOSTokenLogitsProcessor - call
[[autodoc]] FlaxForceTokensLogitsProcessor - call
[[autodoc]] FlaxLogitsProcessor - call
[[autodoc]] FlaxLogitsProcessorList - call
[[autodoc]] FlaxLogitsWarper - call
[[autodoc]] FlaxMinLengthLogitsProcessor - call
[[autodoc]] FlaxSuppressTokensAtBeginLogitsProcessor - call
[[autodoc]] FlaxSuppressTokensLogitsProcessor - call
[[autodoc]] FlaxTemperatureLogitsWarper - call
[[autodoc]] FlaxTopKLogitsWarper - call
[[autodoc]] FlaxTopPLogitsWarper - call
[[autodoc]] FlaxWhisperTimeStampLogitsProcessor - call
StoppingCriteria [[transformers.StoppingCriteria]]
[StoppingCriteria]λ μμ±μ΄ μΈμ λ©μΆμ§λ₯Ό κ²°μ νλ λ° μ¬μ©λ©λλ€ (EOS ν ν° μΈ). μ΄ κΈ°λ₯μ PyTorch ꡬνμλ§ μ 곡λ©λλ€.
[[autodoc]] StoppingCriteria - call
[[autodoc]] StoppingCriteriaList - call
[[autodoc]] MaxLengthCriteria - call
[[autodoc]] MaxTimeCriteria - call
[[autodoc]] StopStringCriteria - call
[[autodoc]] EosTokenCriteria - call
Constraint [[transformers.Constraint]]
[Constraint]λ μμ± μΆλ ₯μ νΉμ ν ν°μ΄λ μνμ€λ₯Ό κ°μ λ‘ ν¬ν¨μν€λ λ° μ¬μ©λ©λλ€. μ΄ κΈ°λ₯μ PyTorch ꡬνμλ§ μ 곡λ©λλ€.
[[autodoc]] Constraint
[[autodoc]] PhrasalConstraint
[[autodoc]] DisjunctiveConstraint
[[autodoc]] ConstraintListState
λΉ κ²μ (BeamSearch) [[transformers.BeamScorer]]
[[autodoc]] BeamScorer - process - finalize
[[autodoc]] BeamSearchScorer - process - finalize
[[autodoc]] ConstrainedBeamSearchScorer - process - finalize
μ€νΈλ¦¬λ¨Έ (Streamers) [[transformers.TextStreamer]]
[[autodoc]] TextStreamer
[[autodoc]] TextIteratorStreamer
μΊμ (Caches) [[transformers.Cache]]
[[autodoc]] Cache - update
[[autodoc]] CacheConfig - update
[[autodoc]] QuantizedCacheConfig - validate
[[autodoc]] DynamicCache - update - get_seq_length - reorder_cache - to_legacy_cache - from_legacy_cache
[[autodoc]] QuantizedCache - update - get_seq_length
[[autodoc]] QuantoQuantizedCache
[[autodoc]] HQQQuantizedCache
[[autodoc]] SinkCache - update - get_seq_length - reorder_cache
[[autodoc]] OffloadedCache - update - prefetch_layer - evict_previous_layer
[[autodoc]] StaticCache - update - get_seq_length - reset
[[autodoc]] OffloadedStaticCache - update - get_seq_length - reset
[[autodoc]] HybridCache - update - get_seq_length - reset
[[autodoc]] SlidingWindowCache - update - reset
[[autodoc]] EncoderDecoderCache - get_seq_length - to_legacy_cache - from_legacy_cache - reset - reorder_cache
[[autodoc]] MambaCache - update_conv_state - update_ssm_state - reset
μν°λ§ν¬ μ νΈλ¦¬ν° (Watermark Utils) [[transformers.WatermarkDetector]]
[[autodoc]] WatermarkDetector - call