้กน็›ฎๆ–‡ไปถๅคน

ๆ–‡ไปถ
wehub-resource-sync e06fe8e8c6
Secret Leaks / trufflehog (push) Failing after 1s
Build documentation / build (push) Failing after 1s
Build documentation / build_other_lang (push) Failing after 0s
CodeQL Security Analysis / CodeQL Analysis (push) Failing after 0s
PR CI / pr-ci (push) Failing after 1s
Slow tests on important models (on Push - A10) / Get all modified files (push) Failing after 1s
Slow tests on important models (on Push - A10) / Model CI (push) Has been skipped
Self-hosted runner (benchmark) / Benchmark (aws-g5-4xlarge-cache) (push) Has been cancelled
New model PR merged notification / Notify new model (push) Has been cancelled
Update Transformers metadata / build_and_package (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:57:37 +08:00

10 KiB

์ธ์ฝ”๋”-๋””์ฝ”๋” ๋ชจ๋ธEncoder Decoder Models

๊ฐœ์š”Overview

[EncoderDecoderModel]์€ ์‚ฌ์ „ ํ•™์Šต๋œ ์ž๋™ ์ธ์ฝ”๋”ฉ(autoencoding) ๋ชจ๋ธ์„ ์ธ์ฝ”๋”๋กœ, ์‚ฌ์ „ ํ•™์Šต๋œ ์ž๊ฐ€ ํšŒ๊ท€(autoregressive) ๋ชจ๋ธ์„ ๋””์ฝ”๋”๋กœ ํ™œ์šฉํ•˜์—ฌ ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค(sequence-to-sequence) ๋ชจ๋ธ์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๋ฐ ์ด์šฉ๋ฉ๋‹ˆ๋‹ค.

์‚ฌ์ „ ํ•™์Šต๋œ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ํ™œ์šฉํ•ด ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค ๋ชจ๋ธ์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ฒƒ์ด ์‹œํ€€์Šค ์ƒ์„ฑ(sequence generation) ์ž‘์—…์— ํšจ๊ณผ์ ์ด๋ผ๋Š” ์ ์ด Sascha Rothe, Shashi Narayan, Aliaksei Severyn์˜ ๋…ผ๋ฌธ Leveraging Pre-trained Checkpoints for Sequence Generation Tasks์—์„œ ์ž…์ฆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

[EncoderDecoderModel]์ด ํ•™์Šต/๋ฏธ์„ธ ์กฐ์ •๋œ ํ›„์—๋Š” ๋‹ค๋ฅธ ๋ชจ๋ธ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ €์žฅ/๋ถˆ๋Ÿฌ์˜ค๊ธฐ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ž์„ธํ•œ ์‚ฌ์šฉ๋ฒ•์€ ์˜ˆ์ œ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.

์ด ์•„ํ‚คํ…์ฒ˜์˜ ํ•œ ๊ฐ€์ง€ ์‘์šฉ ์‚ฌ๋ก€๋Š” ๋‘ ๊ฐœ์˜ ์‚ฌ์ „ ํ•™์Šต๋œ [BertModel]์„ ๊ฐ๊ฐ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”๋กœ ํ™œ์šฉํ•˜์—ฌ ์š”์•ฝ ๋ชจ๋ธ(summarization model)์„ ๊ตฌ์ถ•ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” Yang Liu์™€ Mirella Lapata์˜ ๋…ผ๋ฌธ Text Summarization with Pretrained Encoders์—์„œ ์ œ์‹œ๋œ ๋ฐ” ์žˆ์Šต๋‹ˆ๋‹ค.

๋ชจ๋ธ ์„ค์ •์—์„œ EncoderDecoderModel์„ ๋ฌด์ž‘์œ„ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ[[Randomly initializing EncoderDecoderModel from model configurations.]]

[EncoderDecoderModel]์€ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋” ์„ค์ •(config)์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฌด์ž‘์œ„ ์ดˆ๊ธฐํ™”๋ฅผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์‹œ๋Š” [BertModel] ์„ค์ •์„ ์ธ์ฝ”๋”๋กœ, ๊ธฐ๋ณธ [BertForCausalLM] ์„ค์ •์„ ๋””์ฝ”๋”๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

>>> from transformers import BertConfig, EncoderDecoderConfig, EncoderDecoderModel

>>> config_encoder = BertConfig()
>>> config_decoder = BertConfig()

>>> config = EncoderDecoderConfig.from_encoder_decoder_configs(config_encoder, config_decoder)
>>> model = EncoderDecoderModel(config=config)

์‚ฌ์ „ ํ•™์Šต๋œ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”๋กœ EncoderDecoderModel ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ[[Initialising EncoderDecoderModel from a pretrained encoder and a pretrained decoder.]]

[EncoderDecoderModel]์€ ์‚ฌ์ „ ํ•™์Šต๋œ ์ธ์ฝ”๋” ์ฒดํฌํฌ์ธํŠธ์™€ ์‚ฌ์ „ ํ•™์Šต๋œ ๋””์ฝ”๋” ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์‚ฌ์šฉํ•ด ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. BERT์™€ ๊ฐ™์€ ๋ชจ๋“  ์‚ฌ์ „ ํ•™์Šต๋œ ์ž๋™ ์ธ์ฝ”๋”ฉ(auto-encoding) ๋ชจ๋ธ์€ ์ธ์ฝ”๋”๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, GPT2์™€ ๊ฐ™์€ ์ž๊ฐ€ ํšŒ๊ท€(autoregressive) ๋ชจ๋ธ์ด๋‚˜ BART์˜ ๋””์ฝ”๋”์™€ ๊ฐ™์ด ์‚ฌ์ „ ํ•™์Šต๋œ ์‹œํ€€์Šค-ํˆฌ-์‹œํ€€์Šค ๋””์ฝ”๋” ๋ชจ๋ธ์„ ๋””์ฝ”๋”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋””์ฝ”๋”๋กœ ์„ ํƒํ•œ ์•„ํ‚คํ…์ฒ˜์— ๋”ฐ๋ผ ๊ต์ฐจ ์–ดํ…์…˜(cross-attention) ๋ ˆ์ด์–ด๊ฐ€ ๋ฌด์ž‘์œ„๋กœ ์ดˆ๊ธฐํ™”๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์ „ ํ•™์Šต๋œ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋” ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ด์šฉํ•ด [EncoderDecoderModel]์„ ์ดˆ๊ธฐํ™”ํ•˜๋ ค๋ฉด, ๋ชจ๋ธ์„ ๋‹ค์šด์ŠคํŠธ๋ฆผ ์ž‘์—…์— ๋Œ€ํ•ด ๋ฏธ์„ธ ์กฐ์ •(fine-tuning)ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ the Warm-starting-encoder-decoder blog post์— ์„ค๋ช…๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ž‘์—…์„ ์œ„ํ•ด EncoderDecoderModel ํด๋ž˜์Šค๋Š” [EncoderDecoderModel.from_encoder_decoder_pretrained] ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

>>> from transformers import EncoderDecoderModel, BertTokenizer

>>> tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-uncased")
>>> model = EncoderDecoderModel.from_encoder_decoder_pretrained("google-bert/bert-base-uncased", "google-bert/bert-base-uncased")

๊ธฐ์กด EncoderDecoderModel ์ฒดํฌํฌ์ธํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ๋ฐ ์ถ”๋ก ํ•˜๊ธฐ[[Loading an existing EncoderDecoderModel checkpoint and perform inference.]]

EncoderDecoderModel ํด๋ž˜์Šค์˜ ๋ฏธ์„ธ ์กฐ์ •(fine-tuned)๋œ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋ ค๋ฉด, Transformers์˜ ๋‹ค๋ฅธ ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ [EncoderDecoderModel]์—์„œ ์ œ๊ณตํ•˜๋Š” from_pretrained(...)๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ถ”๋ก ์„ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด [generate] ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ์ž๋™ ํšŒ๊ท€(autoregressive) ๋ฐฉ์‹์œผ๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š” ํƒ์š• ๋””์ฝ”๋”ฉ(greedy decoding), ๋น” ์„œ์น˜(beam search), ๋‹คํ•ญ ์ƒ˜ํ”Œ๋ง(multinomial sampling) ๋“ฑ ๋‹ค์–‘ํ•œ ๋””์ฝ”๋”ฉ ๋ฐฉ์‹์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

>>> from transformers import AutoTokenizer, EncoderDecoderModel

>>> # ๋ฏธ์„ธ ์กฐ์ •๋œ seq2seq ๋ชจ๋ธ๊ณผ ๋Œ€์‘ํ•˜๋Š” ํ† ํฌ๋‚˜์ด์ € ๊ฐ€์ ธ์˜ค๊ธฐ
>>> model = EncoderDecoderModel.from_pretrained("patrickvonplaten/bert2bert_cnn_daily_mail")
>>> tokenizer = AutoTokenizer.from_pretrained("patrickvonplaten/bert2bert_cnn_daily_mail")

>>> # let's perform inference on a long piece of text
>>> ARTICLE_TO_SUMMARIZE = (
...     "PG&E stated it scheduled the blackouts in response to forecasts for high winds "
...     "amid dry conditions. The aim is to reduce the risk of wildfires. Nearly 800 thousand customers were "
...     "scheduled to be affected by the shutoffs which were expected to last through at least midday tomorrow."
... )
>>> input_ids = tokenizer(ARTICLE_TO_SUMMARIZE, return_tensors="pt").input_ids

>>> # ์ž๊ธฐํšŒ๊ท€์ ์œผ๋กœ ์š”์•ฝ ์ƒ์„ฑ (๊ธฐ๋ณธ์ ์œผ๋กœ ๊ทธ๋ฆฌ๋”” ๋””์ฝ”๋”ฉ ์‚ฌ์šฉ)
>>> generated_ids = model.generate(input_ids)
>>> generated_text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
>>> print(generated_text)
nearly 800 thousand customers were affected by the shutoffs. the aim is to reduce the risk of wildfires. nearly 800, 000 customers were expected to be affected by high winds amid dry conditions. pg & e said it scheduled the blackouts to last through at least midday tomorrow.

TFEncoderDecoderModel์— Pytorch ์ฒดํฌํฌ์ธํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ[[Loading a PyTorch checkpoint into TFEncoderDecoderModel.]]

[TFEncoderDecoderModel.from_pretrained] ๋ฉ”์„œ๋“œ๋Š” ํ˜„์žฌ Pytorch ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์‚ฌ์šฉํ•œ ๋ชจ๋ธ ์ดˆ๊ธฐํ™”๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ์— from_pt=True๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ์˜ˆ์™ธ(exception)๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ํŠน์ • ์ธ์ฝ”๋”-๋””์ฝ”๋” ๋ชจ๋ธ์— ๋Œ€ํ•œ Pytorch ์ฒดํฌํฌ์ธํŠธ๋งŒ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

>>> # ํŒŒ์ดํ† ์น˜ ์ฒดํฌํฌ์ธํŠธ์—์„œ ๋กœ๋“œํ•˜๋Š” ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•
>>> from transformers import EncoderDecoderModel, TFEncoderDecoderModel

>>> _model = EncoderDecoderModel.from_pretrained("patrickvonplaten/bert2bert-cnn_dailymail-fp16")

>>> _model.encoder.save_pretrained("./encoder")
>>> _model.decoder.save_pretrained("./decoder")

>>> model = TFEncoderDecoderModel.from_encoder_decoder_pretrained(
...     "./encoder", "./decoder", encoder_from_pt=True, decoder_from_pt=True
... )
>>> # ์ด ๋ถ€๋ถ„์€ ํŠน์ • ๋ชจ๋ธ์˜ ๊ตฌ์ฒด์ ์ธ ์„ธ๋ถ€์‚ฌํ•ญ์„ ๋ณต์‚ฌํ•  ๋•Œ์—๋งŒ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
>>> model.config = _model.config

ํ•™์ŠตTraining

๋ชจ๋ธ์ด ์ƒ์„ฑ๋œ ํ›„์—๋Š” BART, T5 ๋˜๋Š” ๊ธฐํƒ€ ์ธ์ฝ”๋”-๋””์ฝ”๋” ๋ชจ๋ธ๊ณผ ์œ ์‚ฌํ•œ ๋ฐฉ์‹์œผ๋กœ ๋ฏธ์„ธ ์กฐ์ •(fine-tuning)ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณด์‹œ๋‹ค์‹œํ”ผ, ์†์‹ค(loss)์„ ๊ณ„์‚ฐํ•˜๋ ค๋ฉด ๋‹จ 2๊ฐœ์˜ ์ž…๋ ฅ๋งŒ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค: input_ids(์ž…๋ ฅ ์‹œํ€€์Šค๋ฅผ ์ธ์ฝ”๋”ฉํ•œ input_ids)์™€ labels(๋ชฉํ‘œ ์‹œํ€€์Šค๋ฅผ ์ธ์ฝ”๋”ฉํ•œ input_ids).

>>> from transformers import BertTokenizer, EncoderDecoderModel

>>> tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-uncased")
>>> model = EncoderDecoderModel.from_encoder_decoder_pretrained("google-bert/bert-base-uncased", "google-bert/bert-base-uncased")

>>> model.config.decoder_start_token_id = tokenizer.cls_token_id
>>> model.config.pad_token_id = tokenizer.pad_token_id

>>> input_ids = tokenizer(
...     "The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side.During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was  finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft).Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct.",
...     return_tensors="pt",
... ).input_ids

>>> labels = tokenizer(
...     "the eiffel tower surpassed the washington monument to become the tallest structure in the world. it was the first structure to reach a height of 300 metres in paris in 1930. it is now taller than the chrysler building by 5. 2 metres ( 17 ft ) and is the second tallest free - standing structure in paris.",
...     return_tensors="pt",
... ).input_ids

>>> # forward ํ•จ์ˆ˜๊ฐ€ ์ž๋™์œผ๋กœ ์ ํ•ฉํ•œ decoder_input_ids๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
>>> loss = model(input_ids=input_ids, labels=labels).loss

ํ›ˆ๋ จ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ colab ๋…ธํŠธ๋ถ์„ ์ฐธ์กฐํ•˜์„ธ์š”.

์ด ๋ชจ๋ธ์€ thomwolf๊ฐ€ ๊ธฐ์—ฌํ–ˆ์œผ๋ฉฐ, ์ด ๋ชจ๋ธ์— ๋Œ€ํ•œ TensorFlow ๋ฐ Flax ๋ฒ„์ „์€ ydshieh๊ฐ€ ๊ธฐ์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค.

EncoderDecoderConfig

autodoc EncoderDecoderConfig

EncoderDecoderModel

autodoc EncoderDecoderModel - forward - from_encoder_decoder_pretrained