





'''simple docstring'''



import logging
import os

import quant_trainer
import torch
from torch.utils.data import DataLoader

from transformers import Trainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput


__magic_name__   :						Dict    =			logging.getLogger(__name__)

if is_torch_tpu_available(check_device=False):
 import torch_xla.core.xla_model as xm
 import torch_xla.debug.metrics as met


class       __SCREAMING_SNAKE_CASE	(						__UpperCamelCase			):



     '''simple docstring'''






     def __init__(					self						,   *lowerCamelCase						,   lowerCamelCase=None						,   lowerCamelCase=None						,   lowerCamelCase=None						,   **lowerCamelCase	):
       super().__init__(*lowerCamelCase						,   **lowerCamelCase	)
       _snake_case     		=				eval_examples
       _snake_case     		=				post_process_function
       _snake_case     		=				quant_trainer_args
       _snake_case     		=				128  # default number of calibration samples

     def 	UpperCamelCase(					self						,   lowerCamelCase=None	):
       if calib_dataset is None and self.calib_dataset is None:
         raise ValueError("Trainer: calibration requires an calib_dataset."	)
       _snake_case     		=				calib_dataset if calib_dataset is not None else self.calib_dataset

       _snake_case     		=				self._remove_unused_columns(lowerCamelCase						,   description="Calibration"	)

       return DataLoader(
           lowerCamelCase						,   batch_size=self.args.eval_batch_size						,   collate_fn=self.data_collator						,   drop_last=self.args.dataloader_drop_last						,   num_workers=self.args.dataloader_num_workers						,   pin_memory=self.args.dataloader_pin_memory						,   shuffle=lowerCamelCase						,   )

     def 	UpperCamelCase(					self						,   lowerCamelCase=None	):
       _snake_case     		=				self.train_dataset if calib_dataset is None else calib_dataset
       _snake_case     		=				self.get_calib_dataloader(lowerCamelCase	)

       _snake_case     		=				self.model
       quant_trainer.configure_model(lowerCamelCase						,   self.quant_trainer_args						,   calib=lowerCamelCase	)
       model.eval()
       quant_trainer.enable_calibration(lowerCamelCase	)

       logger.info("***** Running calibration *****"	)
       logger.info(F'''  Num examples = {self.calib_num}'''	)
       logger.info(F'''  Batch size = {calib_dataloader.batch_size}'''	)

       for step, inputs in enumerate(lowerCamelCase	):
         # Prediction step
         _snake_case		,		_snake_case		,		_snake_case     		=				self.prediction_step(lowerCamelCase						,   lowerCamelCase						,   prediction_loss_only=lowerCamelCase	)
         if (step + 1) * calib_dataloader.batch_size >= self.calib_num:
           break

       quant_trainer.finish_calibration(lowerCamelCase						,   self.quant_trainer_args	)
       _snake_case     		=				model

     def 	UpperCamelCase(					self						,   lowerCamelCase=None						,   lowerCamelCase=None						,   lowerCamelCase=None						,   lowerCamelCase = "eval"	):
       _snake_case     		=				self.eval_dataset if eval_dataset is None else eval_dataset
       _snake_case     		=				self.get_eval_dataloader(lowerCamelCase	)
       _snake_case     		=				self.eval_examples if eval_examples is None else eval_examples

       # Temporarily disable metric computation, we will do it in the loop here.
       _snake_case     		=				self.compute_metrics
       _snake_case     		=				None
       _snake_case     		=				self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
       try:
         _snake_case     		=				eval_loop(
             lowerCamelCase						,   description="Evaluation"						,   prediction_loss_only=True if compute_metrics is None else None						,   ignore_keys=lowerCamelCase						,   )
       finally:
         _snake_case     		=				compute_metrics

       if self.post_process_function is not None and self.compute_metrics is not None:
         _snake_case     		=				self.post_process_function(lowerCamelCase						,   lowerCamelCase						,   output.predictions	)
         _snake_case     		=				self.compute_metrics(lowerCamelCase	)

         # Prefix all keys with metric_key_prefix + '_'
         for key in list(metrics.keys()	):
           if not key.startswith(F'''{metric_key_prefix}_'''	):
             _snake_case     		=				metrics.pop(lowerCamelCase	)

         self.log(lowerCamelCase	)
       else:
         _snake_case     		=				{}

       if self.args.tpu_metrics_debug or self.args.debug:
         # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
         xm.master_print(met.metrics_report()	)

       _snake_case     		=				self.callback_handler.on_evaluate(self.args						,   self.state						,   self.control						,   lowerCamelCase	)
       return metrics

     def 	UpperCamelCase(					self						,   lowerCamelCase						,   lowerCamelCase						,   lowerCamelCase=None						,   lowerCamelCase = "test"	):
       _snake_case     		=				self.get_test_dataloader(lowerCamelCase	)

       # Temporarily disable metric computation, we will do it in the loop here.
       _snake_case     		=				self.compute_metrics
       _snake_case     		=				None
       _snake_case     		=				self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
       try:
         _snake_case     		=				eval_loop(
             lowerCamelCase						,   description="Prediction"						,   prediction_loss_only=True if compute_metrics is None else None						,   ignore_keys=lowerCamelCase						,   )
       finally:
         _snake_case     		=				compute_metrics

       if self.post_process_function is None or self.compute_metrics is None:
         return output

       _snake_case     		=				self.post_process_function(lowerCamelCase						,   lowerCamelCase						,   output.predictions						,   "predict"	)
       _snake_case     		=				self.compute_metrics(lowerCamelCase	)

       # Prefix all keys with metric_key_prefix + '_'
       for key in list(metrics.keys()	):
         if not key.startswith(F'''{metric_key_prefix}_'''	):
           _snake_case     		=				metrics.pop(lowerCamelCase	)

       return PredictionOutput(predictions=predictions.predictions						,   label_ids=predictions.label_ids						,   metrics=lowerCamelCase	)






     def 	UpperCamelCase(					self						,   lowerCamelCase="./"	):
       _snake_case     		=				self.eval_dataset
       _snake_case     		=				self.get_eval_dataloader(lowerCamelCase	)

       _snake_case     		=				next(iter(lowerCamelCase	)	)

       # saving device - to make it consistent
       _snake_case     		=				torch.device("cuda" if torch.cuda.is_available() else "cpu"	)

       # convert to tuple
       _snake_case     		=				tuple(v.to(lowerCamelCase	) for k, v in batch.items()	)

       logger.info("Converting model to be onnx compatible"	)
       from pytorch_quantization.nn import TensorQuantizer

       _snake_case     		=				True

       _snake_case     		=				self.model.to(lowerCamelCase	)

       model.eval()
       model.float()

       _snake_case     		=				model.module if hasattr(lowerCamelCase						,   "module"	) else model
       quant_trainer.configure_model(lowerCamelCase						,   self.quant_trainer_args	)

       _snake_case     		=				os.path.join(lowerCamelCase						,   "model.onnx"	)
       logger.info(F'''exporting model to {output_model_file}'''	)

       _snake_case     		=				{0: "batch_size", 1: "seq_len"}

       torch.onnx.export(
           lowerCamelCase						,   lowerCamelCase						,   lowerCamelCase						,   export_params=lowerCamelCase						,   opset_version=13						,   do_constant_folding=lowerCamelCase						,   input_names=["input_ids", "attention_mask", "token_type_ids"]						,   output_names=["output_start_logits", "output_end_logits"]						,   dynamic_axes={
               "input_ids": axes,
               "attention_mask": axes,
               "token_type_ids": axes,
               "output_start_logits": axes,
               "output_end_logits": axes,
           }						,   verbose=lowerCamelCase						,   )
       logger.info("onnx export finished"	)









'''simple docstring'''



import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.layers import LSTM, Dense
from tensorflow.keras.models import Sequential

if __name__ == "__main__":
 __magic_name__   :						Union[str, Any]    =			pd.read_csv("""sample_data.csv""", header=None)
 __magic_name__   :						Optional[Any]    =			df.shape[:1][0]
 # If you're using some other dataset input the target column
 __magic_name__   :						str    =			df.iloc[:, 1:2]
 __magic_name__   :						Dict    =			actual_data.values.reshape(len_data, 1)
 __magic_name__   :						Tuple    =			MinMaxScaler().fit_transform(actual_data)
 __magic_name__   :						Union[str, Any]    =			10
 __magic_name__   :						Optional[int]    =			5
 __magic_name__   :						Any    =			20
 __magic_name__   :						int    =			len_data - periods * look_back
 __magic_name__   :						Union[str, Any]    =			actual_data[:division]
 __magic_name__   :						int    =			actual_data[division - look_back :]
 __magic_name__   ,    __magic_name__   :						List[str]    =			[], []
 __magic_name__   ,    __magic_name__   :						Tuple    =			[], []

 for i in range(0, len(train_data) - forward_days - look_back + 1):
  train_x.append(train_data[i : i + look_back])
  train_y.append(train_data[i + look_back : i + look_back + forward_days])
 for i in range(0, len(test_data) - forward_days - look_back + 1):
  test_x.append(test_data[i : i + look_back])
  test_y.append(test_data[i + look_back : i + look_back + forward_days])
 __magic_name__   :						List[str]    =			np.array(train_x)
 __magic_name__   :						List[str]    =			np.array(test_x)
 __magic_name__   :						int    =			np.array([list(i.ravel()) for i in train_y])
 __magic_name__   :						int    =			np.array([list(i.ravel()) for i in test_y])

 __magic_name__   :						Tuple    =			Sequential()
 model.add(LSTM(128, input_shape=(look_back, 1), return_sequences=True))
 model.add(LSTM(64, input_shape=(128, 1)))
 model.add(Dense(forward_days))
 model.compile(loss="""mean_squared_error""", optimizer="""adam""")
 __magic_name__   :						int    =			model.fit(
     x_train, y_train, epochs=150, verbose=1, shuffle=True, batch_size=4
 )
 __magic_name__   :						int    =			model.predict(x_test)


