import argparse
import logging
import pickle
from collections import Counter


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

if __name__ == "__main__":
  UpperCAmelCase_			    =							argparse.ArgumentParser(
      description='''Token Counts for smoothing the masking probabilities in MLM (cf XLM/word2vec)'''
  )
  parser.add_argument(
      '''--data_file''', type=str, default='''data/dump.bert-base-uncased.pickle''', help='''The binarized dataset.'''
  )
  parser.add_argument(
      '''--token_counts_dump''', type=str, default='''data/token_counts.bert-base-uncased.pickle''', help='''The dump file.'''
  )
  parser.add_argument('''--vocab_size''', default=3_0_5_2_2, type=int)
  UpperCAmelCase_			    =							parser.parse_args()

  logger.info(f"""Loading data from {args.data_file}""")
  with open(args.data_file, '''rb''') as fp:
    UpperCAmelCase_			    =							pickle.load(fp)

  logger.info('''Counting occurrences for MLM.''')
  UpperCAmelCase_			    =							Counter()
  for tk_ids in data:
    counter.update(tk_ids)
  UpperCAmelCase_			    =							[0] * args.vocab_size
  for k, v in counter.items():
    UpperCAmelCase_			    =							v

  logger.info(f"""Dump to {args.token_counts_dump}""")
  with open(args.token_counts_dump, '''wb''') as handle:
    pickle.dump(counts, handle, protocol=pickle.HIGHEST_PROTOCOL)
import os
import time

import numpy as np
import onnxruntime as ort


UpperCAmelCase_			    =							'''1'''
UpperCAmelCase_			    =							'''0'''
UpperCAmelCase_			    =							'''1'''

UpperCAmelCase_			    =							ort.SessionOptions()
UpperCAmelCase_			    =							ort.GraphOptimizationLevel.ORT_DISABLE_ALL
print('''Create inference session...''')
UpperCAmelCase_			    =							['''TensorrtExecutionProvider''', '''CUDAExecutionProvider''']
UpperCAmelCase_			    =							ort.InferenceSession('''model.onnx''', sess_options=sess_opt, providers=execution_provider)
UpperCAmelCase_			    =							ort.RunOptions()

UpperCAmelCase_			    =							1_2_8
UpperCAmelCase_			    =							1
UpperCAmelCase_			    =							np.ones((batch, sequence), dtype=np.intaa)
UpperCAmelCase_			    =							np.ones((batch, sequence), dtype=np.intaa)
UpperCAmelCase_			    =							np.ones((batch, sequence), dtype=np.intaa)

print('''Warm up phase...''')
sess.run(
    None,
    {
        sess.get_inputs()[0].name: input_ids,
        sess.get_inputs()[1].name: attention_mask,
        sess.get_inputs()[2].name: token_type_ids,
    },
    run_options=run_opt,
)

print('''Start inference...''')
UpperCAmelCase_			    =							time.time()
UpperCAmelCase_			    =							2_0_0_0
UpperCAmelCase_			    =							{}
for iter in range(max_iters):
  UpperCAmelCase_			    =							sess.run(
      None,
      {
          sess.get_inputs()[0].name: input_ids,
          sess.get_inputs()[1].name: attention_mask,
          sess.get_inputs()[2].name: token_type_ids,
      },
      run_options=run_opt,
  )
print('''Average Inference Time = {:.3f} ms'''.format((time.time() - start_time) * 1_0_0_0 / max_iters))