code: def main():
      # We now keep distinct sets of args, for a cleaner separation of concerns.
      parser = HfArgumentParser(
          (
              ModelArguments,
              DataTrainingArguments,
              TrainingArguments,
              OptimizationArguments,
              OnnxExportArguments,
          )
      )
      if len(sys.argv) == 2 and sys.argv[1].endswith(".json"):
          # If we pass only one argument to the script and it's the path to a json file,
          # let's parse it to get our arguments.
          (
              model_args,
              data_args,
              training_args,
              optim_args,
              onnx_export_args,
          ) = parser.parse_json_file(
              json_file=os.path.abspath(sys.argv[1])
          )
      else:
          (
              model_args,
              data_args,
              training_args,
              optim_args,
              onnx_export_args,
          ) = parser.parse_args_into_dataclasses()
  
      # Setup logging
      logging.basicConfig(
          format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
          datefmt="%m/%d/%Y %H:%M:%S",
          handlers=[logging.StreamHandler(sys.stdout)],
      )
  
      log_level = training_args.get_process_log_level()
      logger.setLevel(log_level)
      datasets.utils.logging.set_verbosity(log_level)
      transformers.utils.logging.set_verbosity(log_level)
      transformers.utils.logging.enable_default_handler()
      transformers.utils.logging.enable_explicit_format()
  
      logger.info(
          f"Optimization with the following parameters {optim_args}"
      )
  
      if (
          os.path.isdir(training_args.output_dir)
          and not training_args.overwrite_output_dir
      ):
          raise ValueError(
              f"Output directory ({training_args.output_dir}) already exists and is not empty. "
              "Use --overwrite_output_dir to overcome."
          )
  
      os.makedirs(training_args.output_dir, exist_ok=True)
  
      # Get the datasets: you can either provide your own CSV/JSON/TXT training and evaluation files (see below)
      # or just provide the name of one of the public datasets available on the hub at https://huggingface.co/datasets/
      # (the dataset will be downloaded automatically from the datasets Hub).
      #
      # For CSV/JSON files, this script will use the column called 'text' or the first column if no column called
      # 'text' is found. You can easily tweak this behavior (see below).
      if data_args.dataset_name is not None:
          # Downloading and loading a dataset from the hub.
          raw_datasets = load_dataset(
              data_args.dataset_name,
              data_args.dataset_config_name,
              cache_dir=model_args.cache_dir,
          )
      else:
          data_files = {}
          if data_args.train_file is not None:
              data_files["train"] = data_args.train_file
          if data_args.validation_file is not None:
              data_files["validation"] = data_args.validation_file
          if data_args.test_file is not None:
              data_files["test"] = data_args.test_file
          extension = data_args.train_file.split(".")[-1]
          raw_datasets = load_dataset(
              extension,
              data_files=data_files,
              cache_dir=model_args.cache_dir,
          )
      # See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at
      # https://huggingface.co/docs/datasets/loading_datasets.html.
  
      if training_args.do_eval:
          if "validation" not in raw_datasets:
              raise ValueError(
                  "--do_eval requires a validation dataset"
              )
          column_names = raw_datasets["validation"].column_names
      else:
          column_names = raw_datasets["train"].column_names
  
      if data_args.text_column_name is not None:
          text_column_name = data_args.text_column_name
      elif "tokens" in column_names:
          text_column_name = "tokens"
      else:
          text_column_name = column_names[0]
  
      if data_args.label_column_name is not None:
          label_column_name = data_args.label_column_name
      elif f"{data_args.task_name}_tags" in column_names:
          label_column_name = f"{data_args.task_name}_tags"
      else:
          label_column_name = column_names[1]
  
      if training_args.do_eval:
          # Preprocess the evaluation dataset
          eval_dataset = raw_datasets["validation"]
          if data_args.max_eval_samples is not None:
              eval_dataset = eval_dataset.select(
                  range(data_args.max_eval_samples)
              )
  
          label2id = PretrainedConfig.from_pretrained(
              model_args.model_name_or_path
          ).label2id
          try:
              eval_dataset = eval_dataset.align_labels_with_mapping(
                  label2id=label2id,
                  label_column=label_column_name,
              )
          except Exception:
              logger.warning(
                  f"\nModel label mapping: {label2id}"
                  f"\nDataset label features: {eval_dataset.features[label_column_name]}"
                  f"\nCould not guarantee the model label mapping and the dataset labels match."
                  f" Evaluation results may suffer from a wrong matching."
              )
          features = eval_dataset.features
      else:
          features = raw_datasets["train"].features
  
      # In the event the labels are not a `Sequence[ClassLabel]`, we will need to go through the dataset to get the
      # unique labels.
      def get_label_list(labels):
          unique_labels = set()
          for label in labels:
              unique_labels = unique_labels | set(label)
          label_list = list(unique_labels)
          label_list.sort()
          return label_list
  
      # If the labels are of type ClassLabel, they are already integers and we have the map stored somewhere.
      # Otherwise, we have to get the list of labels manually.
      labels_are_int = isinstance(
          features[label_column_name].feature, ClassLabel
      )
      if labels_are_int:
          label_list = features[label_column_name].feature.names
          label_to_id = {i: i for i in range(len(label_list))}
      else:
          label_list = get_label_list(
              raw_datasets["train"][label_column_name]
          )
          label_to_id = {l: i for i, l in enumerate(label_list)}
  
      # Map that sends B-Xxx label to its I-Xxx counterpart
      b_to_i_label = []
      for idx, label in enumerate(label_list):
          if (
              label.startswith("B-")
              and label.replace("B-", "I-") in label_list
          ):
              b_to_i_label.append(
                  label_list.index(label.replace("B-", "I-"))
              )
          else:
              b_to_i_label.append(idx)
  
      # Tokenize all texts and align the labels with them.
      def tokenize_and_align_labels(
          examples, tokenizer: PreTrainedTokenizer
      ):
          tokenized_inputs = tokenizer(
              examples[text_column_name],
              padding="max_length",
              truncation=True,
              max_length=data_args.max_seq_length,
              # We use this argument because the texts in our dataset are lists of words (with a label for each word).
              is_split_into_words=True,
          )
          labels = []
          for i, label in enumerate(examples[label_column_name]):
              word_ids = tokenized_inputs.word_ids(batch_index=i)
              previous_word_idx = None
              label_ids = []
              for word_idx in word_ids:
                  # Special tokens have a word id that is None. We set the label to -100 so they are automatically
                  # ignored in the loss function.
                  if word_idx is None:
                      label_ids.append(-100)
                  # We set the label for the first token of each word.
                  elif word_idx != previous_word_idx:
                      label_ids.append(label_to_id[label[word_idx]])
                  # For the other tokens in a word, we set the label to either the current label or -100, depending on
                  # the label_all_tokens flag.
                  else:
                      if data_args.label_all_tokens:
                          label_ids.append(
                              b_to_i_label[label_to_id[label[word_idx]]]
                          )
                      else:
                          label_ids.append(-100)
                  previous_word_idx = word_idx
  
              labels.append(label_ids)
          tokenized_inputs["labels"] = labels
          return tokenized_inputs
  
      # Metrics
      metric = load("seqeval")
  
      def compute_metrics(p):
          predictions, labels = p
          predictions = np.argmax(predictions, axis=2)
  
          # Remove ignored index (special tokens)
          true_predictions = [
              [
                  label_list[p]
                  for (p, l) in zip(prediction, label)
                  if l != -100
              ]
              for prediction, label in zip(predictions, labels)
          ]
          true_labels = [
              [
                  label_list[l]
                  for (p, l) in zip(prediction, label)
                  if l != -100
              ]
              for prediction, label in zip(predictions, labels)
          ]
  
          results = metric.compute(
              predictions=true_predictions, references=true_labels
          )
          if data_args.return_entity_level_metrics:
              # Unpack nested dictionaries
              final_results = {}
              for key, value in results.items():
                  if isinstance(value, dict):
                      for n, v in value.items():
                          final_results[f"{key}_{n}"] = v
                  else:
                      final_results[key] = value
              return final_results
          else:
              return {
                  "precision": results["overall_precision"],
                  "recall": results["overall_recall"],
                  "f1": results["overall_f1"],
                  "accuracy": results["overall_accuracy"],
              }
  
      apply_static_quantization = (
          optim_args.quantization_approach == "static"
      )
  
      # Create the quantization configuration containing all the quantization parameters
      qconfig = QuantizationConfig(
          is_static=apply_static_quantization,
          format=QuantFormat.QDQ
          if apply_static_quantization
          else QuantFormat.QOperator,
          mode=QuantizationMode.QLinearOps
          if apply_static_quantization
          else QuantizationMode.IntegerOps,
          activations_dtype=QuantType.QInt8
          if apply_static_quantization
          else QuantType.QUInt8,
          weights_dtype=QuantType.QInt8,
          per_channel=optim_args.per_channel,
          reduce_range=optim_args.reduce_range,
          operators_to_quantize=["MatMul", "Add"],
      )
      tokenizer = AutoTokenizer.from_pretrained(
          model_args.model_name_or_path
      )
  
      # Export the model
      model = ORTModelForTokenClassification.from_pretrained(
          model_args.model_name_or_path, export=True
      )
  
      # Create the quantizer
      quantizer = ORTQuantizer.from_pretrained(model)
  
      ranges = None
      # Create a quantization preprocessor to determine the nodes to exclude
      quantization_preprocessor = QuantizationPreprocessor()
      if apply_static_quantization:
          # Preprocess the calibration dataset
          if "train" not in raw_datasets:
              raise ValueError(
                  "Static quantization requires a train dataset for calibration"
              )
          calibration_dataset = raw_datasets["train"]
          if optim_args.num_calibration_samples is not None:
              calibration_dataset = calibration_dataset.select(
                  range(optim_args.num_calibration_samples)
              )
          calibration_dataset = calibration_dataset.map(
              partial(tokenize_and_align_labels, tokenizer=tokenizer),
              batched=True,
              num_proc=data_args.preprocessing_num_workers,
              load_from_cache_file=not data_args.overwrite_cache,
              desc="Running tokenizer on the calibration dataset",
          )
  
          # Remove the unnecessary columns of the calibration dataset before the calibration step
          calibration_dataset = quantizer.clean_calibration_dataset(
              calibration_dataset
          )
  
          # Create the calibration configuration given the selected calibration method
          if optim_args.calibration_method == "entropy":
              calibration_config = AutoCalibrationConfig.entropy(
                  calibration_dataset
              )
          elif optim_args.calibration_method == "percentile":
              calibration_config = AutoCalibrationConfig.percentiles(
                  calibration_dataset,
                  percentile=optim_args.calibration_histogram_percentile,
              )
          else:
              calibration_config = AutoCalibrationConfig.minmax(
                  calibration_dataset,
                  optim_args.calibration_moving_average,
                  optim_args.calibration_moving_average_constant,
              )
  
          if (
              not 1
              <= optim_args.num_calibration_shards
              <= len(calibration_dataset)
          ):
              raise ValueError(
                  f"Invalid value of number of shards {optim_args.num_calibration_shards} chosen to split the calibration"
                  f" dataset, should be higher than 0 and lower or equal to the number of samples "
                  f"{len(calibration_dataset)}."
              )
  
          for i in range(optim_args.num_calibration_shards):
              shard = calibration_dataset.shard(
                  optim_args.num_calibration_shards, i
              )
              quantizer.partial_fit(
                  dataset=shard,
                  calibration_config=calibration_config,
                  operators_to_quantize=qconfig.operators_to_quantize,
                  batch_size=optim_args.calibration_batch_size,
                  use_external_data_format=onnx_export_args.use_external_data_format,
              )
          ranges = quantizer.compute_ranges()
  
          # Exclude the nodes constituting LayerNorm
          quantization_preprocessor.register_pass(
              ExcludeLayerNormNodes()
          )
          # Exclude the nodes constituting GELU
          quantization_preprocessor.register_pass(ExcludeGeLUNodes())
          # Exclude the residual connection Add nodes
          quantization_preprocessor.register_pass(
              ExcludeNodeAfter("Add", "Add")
          )
          # Exclude the Add nodes following the Gather operator
          quantization_preprocessor.register_pass(
              ExcludeNodeAfter("Gather", "Add")
          )
          # Exclude the Add nodes followed by the Softmax operator
          quantization_preprocessor.register_pass(
              ExcludeNodeFollowedBy("Add", "Softmax")
          )
  
      # Apply quantization on the model
      quantizer.quantize(
          save_dir=training_args.output_dir,
          calibration_tensors_range=ranges,
          quantization_config=qconfig,
          preprocessor=quantization_preprocessor,
          use_external_data_format=onnx_export_args.use_external_data_format,
      )
  
      # Evaluation
      if training_args.do_eval:
          logger.info("*** Evaluate ***")
  
          with training_args.main_process_first(
              desc="validation dataset map pre-processing"
          ):
              eval_dataset = eval_dataset.map(
                  partial(
                      tokenize_and_align_labels, tokenizer=tokenizer
                  ),
                  batched=True,
                  num_proc=data_args.preprocessing_num_workers,
                  load_from_cache_file=not data_args.overwrite_cache,
                  desc="Running tokenizer on the validation dataset",
              )
  
          ort_model = ORTModel(
              Path(training_args.output_dir) / "model_quantized.onnx",
              execution_provider=optim_args.execution_provider,
              compute_metrics=compute_metrics,
          )
          outputs = ort_model.evaluation_loop(eval_dataset)
  
          # Save evaluation metrics
          with open(
              os.path.join(
                  training_args.output_dir, "eval_results.json"
              ),
              "w",
          ) as f:
              json.dump(outputs.metrics, f, indent=4, sort_keys=True)
  
      # Prediction
      if training_args.do_predict:
          logger.info("*** Predict ***")
  
          # Preprocess the test dataset
          if "test" not in raw_datasets:
              raise ValueError("--do_predict requires a test dataset")
          predict_dataset = raw_datasets["test"]
          if data_args.max_predict_samples is not None:
              predict_dataset = predict_dataset.select(
                  range(data_args.max_predict_samples)
              )
          with training_args.main_process_first(
              desc="prediction dataset map pre-processing"
          ):
              predict_dataset = predict_dataset.map(
                  partial(
                      tokenize_and_align_labels, tokenizer=tokenizer
                  ),
                  batched=True,
                  num_proc=data_args.preprocessing_num_workers,
                  load_from_cache_file=not data_args.overwrite_cache,
                  desc="Running tokenizer on the prediction dataset",
              )
  
          ort_model = ORTModel(
              Path(training_args.output_dir) / "model_quantized.onnx",
              execution_provider=optim_args.execution_provider,
              compute_metrics=compute_metrics,
          )
          outputs = ort_model.evaluation_loop(predict_dataset)
          predictions = np.argmax(outputs.predictions, axis=2)
  
          # Remove ignored index (special tokens)
          true_predictions = [
              [
                  label_list[p]
                  for (p, l) in zip(prediction, label)
                  if l != -100
              ]
              for prediction, label in zip(
                  predictions, outputs.label_ids
              )
          ]
  
          # Save test metrics
          with open(
              os.path.join(
                  training_args.output_dir, "predict_results.json"
              ),
              "w",
          ) as f:
              json.dump(outputs.metrics, f, indent=4, sort_keys=True)
  
          # Save predictions
          output_predictions_file = os.path.join(
              training_args.output_dir, "predictions.txt"
          )
          with open(output_predictions_file, "w") as writer:
              for prediction in true_predictions:
                  writer.write(" ".join(prediction) + "\n")
  
imports: import json
  import logging
  import os
  import sys
  from dataclasses import dataclass, field
  from functools import partial
  from pathlib import Path
  from typing import Optional
  import datasets
  import numpy
  import transformers
  from datasets import ClassLabel, load_dataset
  from evaluate import load
  from onnxruntime.quantization import QuantFormat, QuantizationMode, QuantType
  from transformers import AutoTokenizer, HfArgumentParser, PretrainedConfig, PreTrainedTokenizer, TrainingArguments
  from transformers.utils import check_min_version
  from transformers.utils.versions import require_version
  from optimum.onnxruntime import ORTQuantizer
  from optimum.onnxruntime.configuration import AutoCalibrationConfig, QuantizationConfig
  from optimum.onnxruntime.model import ORTModel
  from optimum.onnxruntime.modeling_ort import ORTModelForTokenClassification
  from optimum.onnxruntime.preprocessors import QuantizationPreprocessor
  from optimum.onnxruntime.preprocessors.passes import ExcludeGeLUNodes, ExcludeLayerNormNodes, ExcludeNodeAfter, ExcludeNodeFollowedBy
label_desc: Use image node_id 1 for calling a global function with example usage: main() and returns: label_list, tokenized_inputs, final_results, dict