code: def load_and_prepare_ner(
      model_name,
      data_metric_config,
      max_seq_length,
      padding="max_length",
      **kwargs,
  ):
      # Load dataset and metric
      dataset = load_dataset(*data_metric_config["dataset"])
      metric = load(*data_metric_config["metric"])
      label_all_tokens = True
      task = "ner"
      label_list = (
          dataset["train"].features[f"{task}_tags"].feature.names
      )
  
      # Prepare model
      model = AutoModelForTokenClassification.from_pretrained(
          model_name, num_labels=len(label_list)
      )
      if model_name.split("-")[0] in {"gpt2", "roberta"}:
          tokenizer = AutoTokenizer.from_pretrained(
              model_name, use_fast=True, add_prefix_space=True
          )
      else:
          tokenizer = AutoTokenizer.from_pretrained(
              model_name, use_fast=True
          )
  
      # Prepare dataset
      max_seq_length = min(max_seq_length, tokenizer.model_max_length)
  
      if tokenizer.pad_token is None:
          tokenizer.pad_token = tokenizer.eos_token
          model.config.pad_token_id = model.config.eos_token_id
  
      data_collator = _get_data_collator(data_metric_config, tokenizer)
  
      def tokenize_and_align_labels(examples):
          tokenized_inputs = tokenizer(
              examples["tokens"],
              truncation=True,
              is_split_into_words=True,
          )
  
          labels = []
          for i, label in enumerate(examples[f"{task}_tags"]):
              word_ids = tokenized_inputs.word_ids(batch_index=i)
              previous_word_idx = None
              label_ids = []
              for word_idx in word_ids:
                  if word_idx is None:
                      label_ids.append(-100)
                  elif word_idx != previous_word_idx:
                      label_ids.append(label[word_idx])
                  else:
                      label_ids.append(
                          label[word_idx] if label_all_tokens else -100
                      )
                  previous_word_idx = word_idx
  
              labels.append(label_ids)
  
          tokenized_inputs["labels"] = labels
          return tokenized_inputs
  
      tokenized_datasets = dataset.map(
          tokenize_and_align_labels, batched=True
      )
      train_dataset = tokenized_datasets["train"]
      valid_dataset = tokenized_datasets["validation"]
      test_dataset = tokenized_datasets["test"].remove_columns(
          ["labels"]
      )
  
      max_train_samples = kwargs.get("max_train_samples", None)
      max_valid_samples = kwargs.get("max_valid_samples", None)
      max_test_samples = kwargs.get("max_test_samples", None)
  
      if max_train_samples:
          train_dataset = train_dataset.select(range(max_train_samples))
      if max_valid_samples:
          valid_dataset = valid_dataset.select(range(max_valid_samples))
      if max_test_samples:
          test_dataset = test_dataset.select(range(max_test_samples))
  
      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
          )
          return {
              "precision": results["overall_precision"],
              "recall": results["overall_recall"],
              "f1": results["overall_f1"],
              "accuracy": results["overall_accuracy"],
          }
  
      return {
          "model": model,
          "tokenizer": tokenizer,
          "data_collator": data_collator,
          "train_dataset": train_dataset,
          "eval_dataset": valid_dataset,
          "test_dataset": test_dataset,
          "compute_metrics": compute_metrics,
      }
  
imports: import gc
  import os
  import random
  import tempfile
  import unittest
  from itertools import chain
  from typing import List, Optional
  from unittest.mock import Mock, patch
  import nltk
  import numpy
  from datasets import load_dataset
  from evaluate import load
  from transformers import AutoModelForCausalLM, AutoModelForSeq2SeqLM, AutoModelForSequenceClassification, AutoModelForTokenClassification, AutoTokenizer, DataCollatorForSeq2Seq, DataCollatorWithPadding, default_data_collator, is_torch_available
  from transformers.testing_utils import mockenv_context, require_deepspeed, require_torch, slow
  from transformers.training_args import OptimizerNames
  import onnxruntime
  from parameterized import parameterized
  from optimum.onnxruntime import ORTSeq2SeqTrainer, ORTSeq2SeqTrainingArguments, ORTTrainer, ORTTrainingArguments
  from optimum.onnxruntime.training_args import ORTOptimizerNames
label_desc: Use image node_id 8 for calling a global function with example usage: load_and_prepare_ner(model_name, data_metric_config, max_seq_length, padding) and returns: dict, tokenized_inputs, dict