





import logging

import torch
from accelerate import Accelerator
from arguments import EvaluationArguments
from datasets import load_dataset
from torch.utils.data import IterableDataset
from torch.utils.data.dataloader import DataLoader

from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, set_seed

class 							__SCREAMING_SNAKE_CASE       (  _a							):






							def __init__(    self	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase=1024	,			__lowerCAmelCase=1024	,			__lowerCAmelCase=3.6	):
														UpperCamelCase__					  =      tokenizer
														UpperCamelCase__					  =      tokenizer.bos_token_id
														UpperCamelCase__					  =      dataset
														UpperCamelCase__					  =      seq_length
														UpperCamelCase__					  =      seq_length * chars_per_token * num_of_sequences







							def __iter__(    self	):
														UpperCamelCase__					  =      iter(self.dataset	)
														UpperCamelCase__					  =      True
														while more_examples:
																					UpperCamelCase__  ,  UpperCamelCase__					  =      [], 0
																					while True:
																												if buffer_len >= self.input_characters:
																																			break
																												try:
																																			buffer.append(next(__lowerCAmelCase	)["""content"""]	)
																																			buffer_len += len(buffer[-1]	)
																												except StopIteration:
																																			UpperCamelCase__					  =      False
																																			break
																					UpperCamelCase__					  =      tokenizer(__lowerCAmelCase	,			truncation=__lowerCAmelCase	)["""input_ids"""]
																					UpperCamelCase__					  =      []
																					for tokenized_input in tokenized_inputs:
																												all_token_ids.extend(tokenized_input + [self.concat_token_id]	)
																					for i in range(0	,			len(__lowerCAmelCase	)	,			self.seq_length	):
																												UpperCamelCase__					  =      all_token_ids[i : i + self.seq_length]
																												if len(__lowerCAmelCase	) == self.seq_length:
																																			yield torch.tensor(__lowerCAmelCase	)






def      _UpperCamelCase  (a__ :Tuple							):






							"""simple docstring"""





							UpperCamelCase__					  =      {"""streaming""": True}
							UpperCamelCase__					  =      load_dataset(args.dataset_name ,      split="""train""" ,      **a__							)
							UpperCamelCase__					  =      ConstantLengthDataset(a__ ,      a__ ,      seq_length=args.seq_length							)
							UpperCamelCase__					  =      DataLoader(a__ ,      batch_size=args.batch_size							)
							return eval_dataloader





def      _UpperCamelCase  (a__ :Any							):






							"""simple docstring"""





							model.eval()
							UpperCamelCase__					  =      []
							for step, batch in enumerate(a__							):
														with torch.no_grad():
																					UpperCamelCase__					  =      model(a__ ,      labels=a__							)
														UpperCamelCase__					  =      outputs.loss.repeat(args.batch_size							)
														losses.append(accelerator.gather(a__							)							)

														if args.max_eval_steps > 0 and step >= args.max_eval_steps:
																					break
							UpperCamelCase__					  =      torch.mean(torch.cat(a__							)							)
							try:
														UpperCamelCase__					  =      torch.exp(a__							)
							except OverflowError:
														UpperCamelCase__					  =      float("""inf"""							)
							return loss.item(), perplexity.item()


# Setup Accelerator
UpperCamelCase__           =			Accelerator()

# Parse configuration
UpperCamelCase__           =			HfArgumentParser(EvaluationArguments)
UpperCamelCase__           =			parser.parse_args()
set_seed(args.seed)

# Logging
UpperCamelCase__           =			logging.getLogger(__name__)
logging.basicConfig(
    format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO
)

# Load model and tokenizer
UpperCamelCase__           =			AutoModelForCausalLM.from_pretrained(args.model_ckpt)
UpperCamelCase__           =			AutoTokenizer.from_pretrained(args.model_ckpt)

# Load dataset and dataloader
UpperCamelCase__           =			create_dataloader(args)

# Prepare everything with our `accelerator`.
UpperCamelCase__       ,			UpperCamelCase__           =			accelerator.prepare(model, eval_dataloader)

# Evaluate and save the last checkpoint
logger.info("Evaluating and saving model after training")
UpperCamelCase__       ,			UpperCamelCase__           =			evaluate(args)
logger.info(f"""loss/eval: {eval_loss}, perplexity: {perplexity}""")











def      _UpperCamelCase  (a__ :int = 1000							):






							"""simple docstring"""





							UpperCamelCase__					  =      2**power
							UpperCamelCase__					  =      0
							while n:
														UpperCamelCase__  ,  UpperCamelCase__					  =      r + n % 10, n // 10
							return r


if __name__ == "__main__":
					print(solution(int(str(input()).strip())))




