

'''simple docstring'''




import argparse
from copy import deepcopy

import numpy as np
from datasets import ClassLabel, DatasetDict, load_dataset
from evaluate import load

from transformers import (
    AutoModelForSequenceClassification,
    AutoTokenizer,
    DataCollatorWithPadding,
    Trainer,
    TrainerCallback,
    TrainingArguments,
    set_seed,
)


def    lowerCamelCase__		(    ):
  UpperCAmelCase    				=						argparse.ArgumentParser()
  parser.add_argument('--model_ckpt'							, type=SCREAMING_SNAKE_CASE							, default='microsoft/unixcoder-base-nine'    )
  parser.add_argument('--num_epochs'							, type=SCREAMING_SNAKE_CASE							, default=5    )
  parser.add_argument('--batch_size'							, type=SCREAMING_SNAKE_CASE							, default=6    )
  parser.add_argument('--gradient_accumulation_steps'							, type=SCREAMING_SNAKE_CASE							, default=1    )
  parser.add_argument('--freeze'							, type=SCREAMING_SNAKE_CASE							, default=SCREAMING_SNAKE_CASE    )
  parser.add_argument('--learning_rate'							, type=SCREAMING_SNAKE_CASE							, default=5E-4    )
  parser.add_argument('--seed'							, type=SCREAMING_SNAKE_CASE							, default=0    )
  parser.add_argument('--lr_scheduler_type'							, type=SCREAMING_SNAKE_CASE							, default='cosine'    )
  parser.add_argument('--num_warmup_steps'							, type=SCREAMING_SNAKE_CASE							, default=10    )
  parser.add_argument('--weight_decay'							, type=SCREAMING_SNAKE_CASE							, default=0.01    )
  parser.add_argument('--output_dir'							, type=SCREAMING_SNAKE_CASE							, default='./results'    )
  return parser.parse_args()


_a					:			Any							=    load('accuracy')


def    lowerCamelCase__		(    SCREAMING_SNAKE_CASE   : str    ):
  UpperCAmelCase				,   UpperCAmelCase    				=						eval_pred
  UpperCAmelCase    				=						np.argmax(SCREAMING_SNAKE_CASE							, axis=1    )
  return metric.compute(predictions=SCREAMING_SNAKE_CASE							, references=SCREAMING_SNAKE_CASE    )



class 							lowercase_						(		a			):

  '''simple docstring'''





  def __init__(  self ,			a_  )				->					None:


    """simple docstring"""
    super().__init__()
    UpperCAmelCase    				=						trainer





  def     snake_case_      (  self ,			a_ ,			a_ ,			a_ ,			**a_  )				->					List[Any]:


    """simple docstring"""
    if control.should_evaluate:
      UpperCAmelCase    				=						deepcopy(a_  )
      self._trainer.evaluate(eval_dataset=self._trainer.train_dataset ,			metric_key_prefix='train'  )
      return control_copy




def    lowerCamelCase__		(    ):
  UpperCAmelCase    				=						get_args()
  set_seed(args.seed    )

  UpperCAmelCase    				=						load_dataset('codeparrot/codecomplex'							, split='train'    )
  UpperCAmelCase    				=						dataset.train_test_split(test_size=0.2    )
  UpperCAmelCase    				=						train_test['test'].train_test_split(test_size=0.5    )
  UpperCAmelCase    				=						DatasetDict(
      {
          'train': train_test['train'],
          'test': test_validation['train'],
          'valid': test_validation['test'],
      }    )

  print('Loading tokenizer and model'    )
  UpperCAmelCase    				=						AutoTokenizer.from_pretrained(args.model_ckpt    )
  UpperCAmelCase    				=						tokenizer.eos_token
  UpperCAmelCase    				=						AutoModelForSequenceClassification.from_pretrained(args.model_ckpt							, num_labels=7    )
  UpperCAmelCase    				=						model.config.eos_token_id

  if args.freeze:
    for param in model.roberta.parameters():
      UpperCAmelCase    				=						False

  UpperCAmelCase    				=						ClassLabel(num_classes=7							, names=list(set(train_test_validation['train']['complexity']    )    )    )

  def tokenize(SCREAMING_SNAKE_CASE   : str    ):
    UpperCAmelCase    				=						tokenizer(example['src']							, truncation=SCREAMING_SNAKE_CASE							, max_length=1024    )
    UpperCAmelCase    				=						labels.straint(example['complexity']    )
    return {
        "input_ids": inputs["input_ids"],
        "attention_mask": inputs["attention_mask"],
        "label": label,
    }

  UpperCAmelCase    				=						train_test_validation.map(
      SCREAMING_SNAKE_CASE							, batched=SCREAMING_SNAKE_CASE							, remove_columns=train_test_validation['train'].column_names							, )
  UpperCAmelCase    				=						DataCollatorWithPadding(tokenizer=SCREAMING_SNAKE_CASE    )

  UpperCAmelCase    				=						TrainingArguments(
      output_dir=args.output_dir							, learning_rate=args.learning_rate							, lr_scheduler_type=args.lr_scheduler_type							, evaluation_strategy='epoch'							, save_strategy='epoch'							, logging_strategy='epoch'							, per_device_train_batch_size=args.batch_size							, per_device_eval_batch_size=args.batch_size							, num_train_epochs=args.num_epochs							, gradient_accumulation_steps=args.gradient_accumulation_steps							, weight_decay=0.01							, metric_for_best_model='accuracy'							, run_name='complexity-java'							, report_to='wandb'							, )

  UpperCAmelCase    				=						Trainer(
      model=SCREAMING_SNAKE_CASE							, args=SCREAMING_SNAKE_CASE							, train_dataset=tokenized_datasets['train']							, eval_dataset=tokenized_datasets['valid']							, tokenizer=SCREAMING_SNAKE_CASE							, data_collator=SCREAMING_SNAKE_CASE							, compute_metrics=SCREAMING_SNAKE_CASE							, )

  print('Training...'    )
  trainer.add_callback(CustomCallback(SCREAMING_SNAKE_CASE    )    )
  trainer.train()


if __name__ == "__main__":
  main()





'''simple docstring'''




import qiskit


def    lowerCamelCase__		(    SCREAMING_SNAKE_CASE   : int							, SCREAMING_SNAKE_CASE   : int    ):
  UpperCAmelCase    				=						qiskit.Aer.get_backend('aer_simulator'    )

  # Create a Quantum Circuit acting on the q register
  UpperCAmelCase    				=						qiskit.QuantumCircuit(SCREAMING_SNAKE_CASE							, SCREAMING_SNAKE_CASE    )

  # Map the quantum measurement to the classical bits
  circuit.measure([0]							, [0]    )

  # Execute the circuit on the simulator
  UpperCAmelCase    				=						qiskit.execute(SCREAMING_SNAKE_CASE							, SCREAMING_SNAKE_CASE							, shots=1000    )

  # Return the histogram data of the results of the experiment.
  return job.result().get_counts(SCREAMING_SNAKE_CASE    )


if __name__ == "__main__":
  print(F'''Total count for various states are: {single_qubit_measure(1, 1)}''')


