'''simple docstring'''





import unittest
from queue import Empty
from threading import Thread

from transformers import AutoTokenizer, TextIteratorStreamer, TextStreamer, is_torch_available
from transformers.testing_utils import CaptureStdout, require_torch, torch_device

from ..test_modeling_common import ids_tensor


if is_torch_available():
    import torch

    from transformers import AutoModelForCausalLM




@require_torch
class        __A      ( unittest.TestCase	):



    def 					_lowercase						(self    :    Dict ):
         UpperCAmelCase_           =   AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2" )
         UpperCAmelCase_           =   AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2" ).to(__a )
         UpperCAmelCase_           =   -1

         UpperCAmelCase_           =   ids_tensor((1, 5)   ,			vocab_size=model.config.vocab_size ).to(__a )
         UpperCAmelCase_           =   model.generate(__a   ,			max_new_tokens=10   ,			do_sample=__a )
         UpperCAmelCase_           =   tokenizer.decode(greedy_ids[0] )

         with CaptureStdout() as cs:
              UpperCAmelCase_           =   TextStreamer(__a )
              model.generate(__a   ,			max_new_tokens=10   ,			do_sample=__a   ,			streamer=__a )
         # The greedy text should be printed to stdout, except for the final "\n" in the streamer
         UpperCAmelCase_           =   cs.out[:-1]

         self.assertEqual(__a   ,			__a )



    def 					_lowercase						(self    :    Union[str, Any] ):
         UpperCAmelCase_           =   AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2" )
         UpperCAmelCase_           =   AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2" ).to(__a )
         UpperCAmelCase_           =   -1

         UpperCAmelCase_           =   ids_tensor((1, 5)   ,			vocab_size=model.config.vocab_size ).to(__a )
         UpperCAmelCase_           =   model.generate(__a   ,			max_new_tokens=10   ,			do_sample=__a )
         UpperCAmelCase_           =   tokenizer.decode(greedy_ids[0] )

         UpperCAmelCase_           =   TextIteratorStreamer(__a )
         UpperCAmelCase_           =   {"input_ids": input_ids, "max_new_tokens": 10, "do_sample": False, "streamer": streamer}
         UpperCAmelCase_           =   Thread(target=model.generate   ,			kwargs=__a )
         thread.start()
         UpperCAmelCase_           =   ""
         for new_text in streamer:
              streamer_text += new_text

         self.assertEqual(__a   ,			__a )



    def 					_lowercase						(self    :    Optional[int] ):
         UpperCAmelCase_           =   AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2" )
         UpperCAmelCase_           =   AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2" ).to(__a )
         UpperCAmelCase_           =   -1

         UpperCAmelCase_           =   ids_tensor((1, 5)   ,			vocab_size=model.config.vocab_size ).to(__a )
         UpperCAmelCase_           =   model.generate(__a   ,			max_new_tokens=10   ,			do_sample=__a )
         UpperCAmelCase_           =   greedy_ids[:, input_ids.shape[1] :]
         UpperCAmelCase_           =   tokenizer.decode(new_greedy_ids[0] )

         with CaptureStdout() as cs:
              UpperCAmelCase_           =   TextStreamer(__a   ,			skip_prompt=__a )
              model.generate(__a   ,			max_new_tokens=10   ,			do_sample=__a   ,			streamer=__a )
         # The greedy text should be printed to stdout, except for the final "\n" in the streamer
         UpperCAmelCase_           =   cs.out[:-1]

         self.assertEqual(__a   ,			__a )



    def 					_lowercase						(self    :    Any ):
         # Tests that we can pass `decode_kwargs` to the streamer to control how the tokens are decoded. Must be tested
         # with actual models -- the dummy models' tokenizers are not aligned with their models, and
         # `skip_special_tokens=True` has no effect on them
         UpperCAmelCase_           =   AutoTokenizer.from_pretrained("distilgpt2" )
         UpperCAmelCase_           =   AutoModelForCausalLM.from_pretrained("distilgpt2" ).to(__a )
         UpperCAmelCase_           =   -1

         UpperCAmelCase_           =   torch.ones((1, 5)   ,			device=__a ).long() * model.config.bos_token_id
         with CaptureStdout() as cs:
              UpperCAmelCase_           =   TextStreamer(__a   ,			skip_special_tokens=__a )
              model.generate(__a   ,			max_new_tokens=1   ,			do_sample=__a   ,			streamer=__a )

         # The prompt contains a special token, so the streamer should not print it. As such, the output text, when
         # re-tokenized, must only contain one token
         UpperCAmelCase_           =   cs.out[:-1]  # Remove the final "\n"
         UpperCAmelCase_           =   tokenizer(__a   ,			return_tensors="pt" )
         self.assertEqual(streamer_text_tokenized.input_ids.shape   ,			(1, 1) )






    def 					_lowercase						(self    :    Dict ):
         UpperCAmelCase_           =   AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2" )
         UpperCAmelCase_           =   AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2" ).to(__a )
         UpperCAmelCase_           =   -1

         UpperCAmelCase_           =   ids_tensor((1, 5)   ,			vocab_size=model.config.vocab_size ).to(__a )
         UpperCAmelCase_           =   TextIteratorStreamer(__a   ,			timeout=0.0_01 )
         UpperCAmelCase_           =   {"input_ids": input_ids, "max_new_tokens": 10, "do_sample": False, "streamer": streamer}
         UpperCAmelCase_           =   Thread(target=model.generate   ,			kwargs=__a )
         thread.start()

         # The streamer will timeout after 0.001 seconds, so an exception will be raised
         with self.assertRaises(__a ):
              UpperCAmelCase_           =   ""
              for new_text in streamer:
                   streamer_text += new_text





'''simple docstring'''





import json
import logging
import os
import sys
from pathlib import Path

import finetune_rag

from transformers.file_utils import is_apex_available
from transformers.testing_utils import (
    TestCasePlus,
    execute_subprocess_async,
    require_ray,
    require_torch_gpu,
    require_torch_multi_gpu,
)


logging.basicConfig(level=logging.DEBUG)
SCREAMING_SNAKE_CASE_:     Optional[int]  =logging.getLogger()

SCREAMING_SNAKE_CASE_:     Union[str, Any]  =logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)




class        __A      ( UpperCamelCase__	):



    def 					_lowercase						(self    :    Optional[Any]   ,			__a    :    str ):
         os.makedirs(__a   ,			exist_ok=__a )
         UpperCAmelCase_           =   {"source": "What is love ?", "target": "life"}
         UpperCAmelCase_           =   {"train": 12, "val": 2, "test": 2}
         for split in ["train", "test", "val"]:
              for field in ["source", "target"]:
                   UpperCAmelCase_           =   "\n".join([contents[field]] * n_lines[split] )
                   with open(os.path.join(__a   ,			f"""{split}.{field}""" )   ,			"w" ) as f:
                        f.write(__a )



    def 					_lowercase						(self    :    Optional[int]   ,			__a    :    int   ,			__a    :    str = "pytorch" ):
         UpperCAmelCase_           =   self.get_auto_remove_tmp_dir()
         UpperCAmelCase_           =   os.path.join(__a   ,			"output" )
         UpperCAmelCase_           =   os.path.join(__a   ,			"data" )
         self._create_dummy_data(data_dir=__a )

         UpperCAmelCase_           =   f"""
                --data_dir {data_dir} \
                --output_dir {output_dir} \
                --model_name_or_path facebook/rag-sequence-base \
                --model_type rag_sequence \
                --do_train \
                --do_predict \
                --n_val -1 \
                --val_check_interval 1.0 \
                --train_batch_size 2 \
                --eval_batch_size 1 \
                --max_source_length 25 \
                --max_target_length 25 \
                --val_max_target_length 25 \
                --test_max_target_length 25 \
                --label_smoothing 0.1 \
                --dropout 0.1 \
                --attention_dropout 0.1 \
                --weight_decay 0.001 \
                --adam_epsilon 1e-08 \
                --max_grad_norm 0.1 \
                --lr_scheduler polynomial \
                --learning_rate 3e-04 \
                --num_train_epochs 1 \
                --warmup_steps 4 \
                --gradient_accumulation_steps 1 \
                --distributed-port 8787 \
                --use_dummy_dataset 1 \
                --distributed_retriever {distributed_retriever} \
            """.split()

         if gpus > 0:
              testargs.append(f"""--gpus={gpus}""" )
              if is_apex_available():
                   testargs.append("--fp16" )
         else:
              testargs.append("--gpus=0" )
              testargs.append("--distributed_backend=ddp_cpu" )
              testargs.append("--num_processes=2" )

         UpperCAmelCase_           =   [sys.executable, str(Path(finetune_rag.__file__ ).resolve() )] + testargs
         execute_subprocess_async(__a   ,			env=self.get_env() )

         UpperCAmelCase_           =   os.path.join(__a   ,			"metrics.json" )
         with open(__a ) as f:
              UpperCAmelCase_           =   json.load(__a )
         return result



    @require_torch_gpu
    def 					_lowercase						(self    :    Optional[int] ):
         UpperCAmelCase_           =   self._run_finetune(gpus=1 )
         self.assertGreaterEqual(result["test"][0]["test_avg_em"]   ,			0.2 )



    @require_torch_multi_gpu
    def 					_lowercase						(self    :    Dict ):
         UpperCAmelCase_           =   self._run_finetune(gpus=2 )
         self.assertGreaterEqual(result["test"][0]["test_avg_em"]   ,			0.2 )



    @require_torch_gpu
    @require_ray
    def 					_lowercase						(self    :    Optional[int] ):
         UpperCAmelCase_           =   self._run_finetune(gpus=1   ,			distributed_retriever="ray" )
         self.assertGreaterEqual(result["test"][0]["test_avg_em"]   ,			0.2 )






    @require_torch_multi_gpu
    @require_ray
    def 					_lowercase						(self    :    Any ):
         UpperCAmelCase_           =   self._run_finetune(gpus=1   ,			distributed_retriever="ray" )
         self.assertGreaterEqual(result["test"][0]["test_avg_em"]   ,			0.2 )




