transformers / docs /source /ko /internal /generation_utils.md
AbdulElahGwaith's picture
Upload folder using huggingface_hub
a9bd396 verified

생성을 μœ„ν•œ μœ ν‹Έλ¦¬ν‹° [[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

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]] 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

StoppingCriteria [[transformers.StoppingCriteria]]

[StoppingCriteria]λŠ” 생성이 μ–Έμ œ λ©ˆμΆœμ§€λ₯Ό κ²°μ •ν•˜λŠ” 데 μ‚¬μš©λ©λ‹ˆλ‹€ (EOS 토큰 μ™Έ). 이 κΈ°λŠ₯은 PyTorch κ΅¬ν˜„μ—λ§Œ μ œκ³΅λ©λ‹ˆλ‹€.

[[autodoc]] StoppingCriteria - call

[[autodoc]] StoppingCriteriaList - call

[[autodoc]] MaxLengthCriteria - call

[[autodoc]] MaxTimeCriteria - call

[[autodoc]] StopStringCriteria - call

[[autodoc]] EosTokenCriteria - call

슀트리머 (Streamers) [[transformers.TextStreamer]]

[[autodoc]] TextStreamer

[[autodoc]] TextIteratorStreamer

μΊμ‹œ (Caches) [[transformers.Cache]]

[[autodoc]] CacheLayerMixin - update - get_seq_length - get_mask_sizes - get_max_cache_shape - reset - reorder_cache

[[autodoc]] DynamicLayer - update - crop - batch_repeat_interleave - batch_select_indices

[[autodoc]] StaticLayer - update

[[autodoc]] StaticSlidingWindowLayer - update

[[autodoc]] QuantoQuantizedLayer - update

[[autodoc]] HQQQuantizedLayer - update

[[autodoc]] Cache - update - get_seq_length - get_mask_sizes - get_max_cache_shape - reset - reorder_cache - crop - batch_repeat_interleave - batch_select_indices

[[autodoc]] DynamicCache

[[autodoc]] QuantizedCache

[[autodoc]] StaticCache

[[autodoc]] EncoderDecoderCache

μ›Œν„°λ§ˆν¬ μœ ν‹Έλ¦¬ν‹° (Watermark Utils) [[transformers.WatermarkDetector]]

[[autodoc]] WatermarkDetector - call