prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Add Support for DeBERTaV2
  I would like to use DeBERTaV2 for sequence classification as a quantized model. Please let me know what needs to be done to open a PR to add this support!
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `optimum/onnxruntime/utils.py`
  Content:
  ```
  1 #  Copyright 2021 The HuggingFace Team. All rights reserved.
  2 #
  3 #  Licensed under the Apache License, Version 2.0 (the "License");
  4 #  you may not use this file except in compliance with the License.
  5 #  You may obtain a copy of the License at
  6 #
  7 #      http://www.apache.org/licenses/LICENSE-2.0
  8 #
  9 #  Unless required by applicable law or agreed to in writing, software
  10 #  distributed under the License is distributed on an "AS IS" BASIS,
  11 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12 #  See the License for the specific language governing permissions and
  13 #  limitations under the License.
  14 from pathlib import Path
  15 from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union
  16 
  17 import torch
  18 from transformers.onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeq2SeqConfigWithPast
  19 from transformers.utils import logging
  20 
  21 import onnx
  22 import onnxruntime as ort
  23 
  24 from ..onnx import OnnxConfigWithLoss, OnnxConfigWithPastAndLoss, OnnxSeq2SeqConfigWithPastAndLoss
  25 
  26 
  27 logger = logging.get_logger(__name__)
  28 
  29 ONNX_WEIGHTS_NAME = "model.onnx"
  30 OPTIMIZED_ONNX_WEIGHTS_NAME = "optimized_model.onnx"
  31 QUANTIZED_ONNX_WEIGHTS_NAME = "q8_model.onnx"
  32 
  33 
  34 def _is_gpu_available():
  35     """
  36     checks if a gpu is available.
  37     """
  38     available_providers = ort.get_available_providers()
  39     if "CUDAExecutionProvider" in available_providers and torch.cuda.is_available():
  40         return True
  41     else:
  42         return False
  43 
  44 
  45 class ORTConfigManager:
  46     """
  47     A class that contains all the information needed by ONNX Runtime optimization for a given model type.
  48 
  49     Attributes:
  50         _conf (`Dict[str, tuple]`):
  51             A dictionary mapping each supported model type to a tuple containing the number of attention heads
  52             and the hidden size model config attribute names as well as the corresponding ONNX Runtime model type.
  53     """
  54 
  55     _conf = {
  56         "bert": ("num_attention_heads", "hidden_size", "bert"),
  57         "albert": ("num_attention_heads", "hidden_size", "bert"),
  58         "camembert": ("num_attention_heads", "hidden_size", "bert"),
  59         "distilbert": ("n_heads", "dim", "bert"),
  60         "electra": ("num_attention_heads", "hidden_size", "bert"),
  61         "roberta": ("num_attention_heads", "hidden_size", "bert"),
  62         "bart": ("encoder_attention_heads", "d_model", "bart"),
  63         "gpt2": ("n_head", "n_embd", "gpt2"),
  64         "gpt_neo": ("num_heads", "hidden_size", "gpt2"),
  65     }
  66 
  67     @classmethod
  68     def get_num_heads_name(cls, model_type: str) -> str:
  69         num_heads = "num_attention_heads"
  70         try:
  71             num_heads = cls._conf[model_type][0]
  72         except KeyError:
  73             logger.warning(
  74                 f"{model_type} is not supported yet. Only {list(cls._conf.keys())} are supported. The default value to "
  75                 f"access the number of heads defined in the config is set to `{num_heads}`."
  76             )
  77         return num_heads
  78 
  79     @classmethod
  80     def get_hidden_size_name(cls, model_type: str) -> str:
  81         hidden_size = "hidden_size"
  82         try:
  83             hidden_size = cls._conf[model_type][1]
  84         except KeyError:
  85             logger.warning(
  86                 f"{model_type} is not supported yet. Only {list(cls._conf.keys())} are supported. The default value to "
  87                 f"access the hidden size defined in the config is set to `{hidden_size}`."
  88             )
  89         return hidden_size
  90 
  91     @classmethod
  92     def get_model_ort_type(cls, model_type: str) -> str:
  93         try:
  94             model_type = cls._conf[model_type][2]
  95         except KeyError:
  96             logger.warning(f"{model_type} is not supported yet. Only {list(cls._conf.keys())} are supported.")
  97         return model_type
  98 
  99     @classmethod
  100     def check_supported_model_or_raise(cls, model_type: str) -> bool:
  101         if model_type not in cls._conf:
  102             raise KeyError(
  103                 f"{model_type} model type is not supported yet. Only {list(cls._conf.keys())} are supported. "
  104                 f"If you want to support {model_type} please propose a PR or open up an issue."
  105             )
  106 
  107 
  108 def generate_identified_filename(filename, identifier):
  109     return filename.parent.joinpath(filename.stem + identifier).with_suffix(filename.suffix)
  110 
  111 
  112 def fix_atenops_to_gather(model_path):
  113     # Fix broken ATenOp nodes back to Gather nodes.
  114     model = onnx.load(model_path)
  115     onnx.checker.check_model(model)
  116 
  117     nodes = model.graph.node
  118 
  119     for node in nodes:
  120         if node.op_type in ["ATenOp", "ATen"]:
  121             logger.info(f"----Start fixing node: {node.name}----")
  122             op_num = node.name.split("_")[-1]
  123             new_node = onnx.helper.make_node(
  124                 "Gather",
  125                 name="Gather_" + op_num,
  126                 inputs=[node.input[0], node.input[1]],
  127                 outputs=node.output,
  128             )
  129 
  130             model.graph.node.remove(node)
  131             model.graph.node.insert(int(op_num), new_node)
  132 
  133     onnx.checker.check_model(model)
  134     onnx.save(model, model_path)
  135 
  136 
  137 def wrap_onnx_config_for_loss(onnx_config: OnnxConfig) -> OnnxConfig:
  138     if isinstance(onnx_config, OnnxSeq2SeqConfigWithPast):
  139         return OnnxSeq2SeqConfigWithPastAndLoss(onnx_config)
  140     elif isinstance(onnx_config, OnnxConfigWithPast):
  141         return OnnxConfigWithPastAndLoss(onnx_config)
  142     else:
  143         return OnnxConfigWithLoss(onnx_config)
  144
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/optimum/onnxruntime/utils.py b/optimum/onnxruntime/utils.py
  --- a/optimum/onnxruntime/utils.py
  +++ b/optimum/onnxruntime/utils.py
  @@ -57,6 +57,8 @@
           "albert": ("num_attention_heads", "hidden_size", "bert"),
           "camembert": ("num_attention_heads", "hidden_size", "bert"),
           "distilbert": ("n_heads", "dim", "bert"),
  +        "deberta": ("num_attention_heads", "hidden_size", "bert"),
  +        "deberta-v2": ("num_attention_heads", "hidden_size", "bert"),
           "electra": ("num_attention_heads", "hidden_size", "bert"),
           "roberta": ("num_attention_heads", "hidden_size", "bert"),
           "bart": ("encoder_attention_heads", "d_model", "bart"),
  
verification_info: {"golden_diff": "diff --git a/optimum/onnxruntime/utils.py b/optimum/onnxruntime/utils.py\n--- a/optimum/onnxruntime/utils.py\n+++ b/optimum/onnxruntime/utils.py\n@@ -57,6 +57,8 @@\n         \"albert\": (\"num_attention_heads\", \"hidden_size\", \"bert\"),\n         \"camembert\": (\"num_attention_heads\", \"hidden_size\", \"bert\"),\n         \"distilbert\": (\"n_heads\", \"dim\", \"bert\"),\n+        \"deberta\": (\"num_attention_heads\", \"hidden_size\", \"bert\"),\n+        \"deberta-v2\": (\"num_attention_heads\", \"hidden_size\", \"bert\"),\n         \"electra\": (\"num_attention_heads\", \"hidden_size\", \"bert\"),\n         \"roberta\": (\"num_attention_heads\", \"hidden_size\", \"bert\"),\n         \"bart\": (\"encoder_attention_heads\", \"d_model\", \"bart\"),\n", "issue": "Add Support for DeBERTaV2\nI would like to use DeBERTaV2 for sequence classification as a quantized model. Please let me know what needs to be done to open a PR to add this support!\n", "before_files": [{"content": "#  Copyright 2021 The HuggingFace Team. All rights reserved.\n#\n#  Licensed under the Apache License, Version 2.0 (the \"License\");\n#  you may not use this file except in compliance with the License.\n#  You may obtain a copy of the License at\n#\n#      http://www.apache.org/licenses/LICENSE-2.0\n#\n#  Unless required by applicable law or agreed to in writing, software\n#  distributed under the License is distributed on an \"AS IS\" BASIS,\n#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n#  See the License for the specific language governing permissions and\n#  limitations under the License.\nfrom pathlib import Path\nfrom typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union\n\nimport torch\nfrom transformers.onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeq2SeqConfigWithPast\nfrom transformers.utils import logging\n\nimport onnx\nimport onnxruntime as ort\n\nfrom ..onnx import OnnxConfigWithLoss, OnnxConfigWithPastAndLoss, OnnxSeq2SeqConfigWithPastAndLoss\n\n\nlogger = logging.get_logger(__name__)\n\nONNX_WEIGHTS_NAME = \"model.onnx\"\nOPTIMIZED_ONNX_WEIGHTS_NAME = \"optimized_model.onnx\"\nQUANTIZED_ONNX_WEIGHTS_NAME = \"q8_model.onnx\"\n\n\ndef _is_gpu_available():\n    \"\"\"\n    checks if a gpu is available.\n    \"\"\"\n    available_providers = ort.get_available_providers()\n    if \"CUDAExecutionProvider\" in available_providers and torch.cuda.is_available():\n        return True\n    else:\n        return False\n\n\nclass ORTConfigManager:\n    \"\"\"\n    A class that contains all the information needed by ONNX Runtime optimization for a given model type.\n\n    Attributes:\n        _conf (`Dict[str, tuple]`):\n            A dictionary mapping each supported model type to a tuple containing the number of attention heads\n            and the hidden size model config attribute names as well as the corresponding ONNX Runtime model type.\n    \"\"\"\n\n    _conf = {\n        \"bert\": (\"num_attention_heads\", \"hidden_size\", \"bert\"),\n        \"albert\": (\"num_attention_heads\", \"hidden_size\", \"bert\"),\n        \"camembert\": (\"num_attention_heads\", \"hidden_size\", \"bert\"),\n        \"distilbert\": (\"n_heads\", \"dim\", \"bert\"),\n        \"electra\": (\"num_attention_heads\", \"hidden_size\", \"bert\"),\n        \"roberta\": (\"num_attention_heads\", \"hidden_size\", \"bert\"),\n        \"bart\": (\"encoder_attention_heads\", \"d_model\", \"bart\"),\n        \"gpt2\": (\"n_head\", \"n_embd\", \"gpt2\"),\n        \"gpt_neo\": (\"num_heads\", \"hidden_size\", \"gpt2\"),\n    }\n\n    @classmethod\n    def get_num_heads_name(cls, model_type: str) -> str:\n        num_heads = \"num_attention_heads\"\n        try:\n            num_heads = cls._conf[model_type][0]\n        except KeyError:\n            logger.warning(\n                f\"{model_type} is not supported yet. Only {list(cls._conf.keys())} are supported. The default value to \"\n                f\"access the number of heads defined in the config is set to `{num_heads}`.\"\n            )\n        return num_heads\n\n    @classmethod\n    def get_hidden_size_name(cls, model_type: str) -> str:\n        hidden_size = \"hidden_size\"\n        try:\n            hidden_size = cls._conf[model_type][1]\n        except KeyError:\n            logger.warning(\n                f\"{model_type} is not supported yet. Only {list(cls._conf.keys())} are supported. The default value to \"\n                f\"access the hidden size defined in the config is set to `{hidden_size}`.\"\n            )\n        return hidden_size\n\n    @classmethod\n    def get_model_ort_type(cls, model_type: str) -> str:\n        try:\n            model_type = cls._conf[model_type][2]\n        except KeyError:\n            logger.warning(f\"{model_type} is not supported yet. Only {list(cls._conf.keys())} are supported.\")\n        return model_type\n\n    @classmethod\n    def check_supported_model_or_raise(cls, model_type: str) -> bool:\n        if model_type not in cls._conf:\n            raise KeyError(\n                f\"{model_type} model type is not supported yet. Only {list(cls._conf.keys())} are supported. \"\n                f\"If you want to support {model_type} please propose a PR or open up an issue.\"\n            )\n\n\ndef generate_identified_filename(filename, identifier):\n    return filename.parent.joinpath(filename.stem + identifier).with_suffix(filename.suffix)\n\n\ndef fix_atenops_to_gather(model_path):\n    # Fix broken ATenOp nodes back to Gather nodes.\n    model = onnx.load(model_path)\n    onnx.checker.check_model(model)\n\n    nodes = model.graph.node\n\n    for node in nodes:\n        if node.op_type in [\"ATenOp\", \"ATen\"]:\n            logger.info(f\"----Start fixing node: {node.name}----\")\n            op_num = node.name.split(\"_\")[-1]\n            new_node = onnx.helper.make_node(\n                \"Gather\",\n                name=\"Gather_\" + op_num,\n                inputs=[node.input[0], node.input[1]],\n                outputs=node.output,\n            )\n\n            model.graph.node.remove(node)\n            model.graph.node.insert(int(op_num), new_node)\n\n    onnx.checker.check_model(model)\n    onnx.save(model, model_path)\n\n\ndef wrap_onnx_config_for_loss(onnx_config: OnnxConfig) -> OnnxConfig:\n    if isinstance(onnx_config, OnnxSeq2SeqConfigWithPast):\n        return OnnxSeq2SeqConfigWithPastAndLoss(onnx_config)\n    elif isinstance(onnx_config, OnnxConfigWithPast):\n        return OnnxConfigWithPastAndLoss(onnx_config)\n    else:\n        return OnnxConfigWithLoss(onnx_config)\n", "path": "optimum/onnxruntime/utils.py"}], "after_files": [{"content": "#  Copyright 2021 The HuggingFace Team. All rights reserved.\n#\n#  Licensed under the Apache License, Version 2.0 (the \"License\");\n#  you may not use this file except in compliance with the License.\n#  You may obtain a copy of the License at\n#\n#      http://www.apache.org/licenses/LICENSE-2.0\n#\n#  Unless required by applicable law or agreed to in writing, software\n#  distributed under the License is distributed on an \"AS IS\" BASIS,\n#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n#  See the License for the specific language governing permissions and\n#  limitations under the License.\nfrom pathlib import Path\nfrom typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union\n\nimport torch\nfrom transformers.onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeq2SeqConfigWithPast\nfrom transformers.utils import logging\n\nimport onnx\nimport onnxruntime as ort\n\nfrom ..onnx import OnnxConfigWithLoss, OnnxConfigWithPastAndLoss, OnnxSeq2SeqConfigWithPastAndLoss\n\n\nlogger = logging.get_logger(__name__)\n\nONNX_WEIGHTS_NAME = \"model.onnx\"\nOPTIMIZED_ONNX_WEIGHTS_NAME = \"optimized_model.onnx\"\nQUANTIZED_ONNX_WEIGHTS_NAME = \"q8_model.onnx\"\n\n\ndef _is_gpu_available():\n    \"\"\"\n    checks if a gpu is available.\n    \"\"\"\n    available_providers = ort.get_available_providers()\n    if \"CUDAExecutionProvider\" in available_providers and torch.cuda.is_available():\n        return True\n    else:\n        return False\n\n\nclass ORTConfigManager:\n    \"\"\"\n    A class that contains all the information needed by ONNX Runtime optimization for a given model type.\n\n    Attributes:\n        _conf (`Dict[str, tuple]`):\n            A dictionary mapping each supported model type to a tuple containing the number of attention heads\n            and the hidden size model config attribute names as well as the corresponding ONNX Runtime model type.\n    \"\"\"\n\n    _conf = {\n        \"bert\": (\"num_attention_heads\", \"hidden_size\", \"bert\"),\n        \"albert\": (\"num_attention_heads\", \"hidden_size\", \"bert\"),\n        \"camembert\": (\"num_attention_heads\", \"hidden_size\", \"bert\"),\n        \"distilbert\": (\"n_heads\", \"dim\", \"bert\"),\n        \"deberta\": (\"num_attention_heads\", \"hidden_size\", \"bert\"),\n        \"deberta-v2\": (\"num_attention_heads\", \"hidden_size\", \"bert\"),\n        \"electra\": (\"num_attention_heads\", \"hidden_size\", \"bert\"),\n        \"roberta\": (\"num_attention_heads\", \"hidden_size\", \"bert\"),\n        \"bart\": (\"encoder_attention_heads\", \"d_model\", \"bart\"),\n        \"gpt2\": (\"n_head\", \"n_embd\", \"gpt2\"),\n        \"gpt_neo\": (\"num_heads\", \"hidden_size\", \"gpt2\"),\n    }\n\n    @classmethod\n    def get_num_heads_name(cls, model_type: str) -> str:\n        num_heads = \"num_attention_heads\"\n        try:\n            num_heads = cls._conf[model_type][0]\n        except KeyError:\n            logger.warning(\n                f\"{model_type} is not supported yet. Only {list(cls._conf.keys())} are supported. The default value to \"\n                f\"access the number of heads defined in the config is set to `{num_heads}`.\"\n            )\n        return num_heads\n\n    @classmethod\n    def get_hidden_size_name(cls, model_type: str) -> str:\n        hidden_size = \"hidden_size\"\n        try:\n            hidden_size = cls._conf[model_type][1]\n        except KeyError:\n            logger.warning(\n                f\"{model_type} is not supported yet. Only {list(cls._conf.keys())} are supported. The default value to \"\n                f\"access the hidden size defined in the config is set to `{hidden_size}`.\"\n            )\n        return hidden_size\n\n    @classmethod\n    def get_model_ort_type(cls, model_type: str) -> str:\n        try:\n            model_type = cls._conf[model_type][2]\n        except KeyError:\n            logger.warning(f\"{model_type} is not supported yet. Only {list(cls._conf.keys())} are supported.\")\n        return model_type\n\n    @classmethod\n    def check_supported_model_or_raise(cls, model_type: str) -> bool:\n        if model_type not in cls._conf:\n            raise KeyError(\n                f\"{model_type} model type is not supported yet. Only {list(cls._conf.keys())} are supported. \"\n                f\"If you want to support {model_type} please propose a PR or open up an issue.\"\n            )\n\n\ndef generate_identified_filename(filename, identifier):\n    return filename.parent.joinpath(filename.stem + identifier).with_suffix(filename.suffix)\n\n\ndef fix_atenops_to_gather(model_path):\n    # Fix broken ATenOp nodes back to Gather nodes.\n    model = onnx.load(model_path)\n    onnx.checker.check_model(model)\n\n    nodes = model.graph.node\n\n    for node in nodes:\n        if node.op_type in [\"ATenOp\", \"ATen\"]:\n            logger.info(f\"----Start fixing node: {node.name}----\")\n            op_num = node.name.split(\"_\")[-1]\n            new_node = onnx.helper.make_node(\n                \"Gather\",\n                name=\"Gather_\" + op_num,\n                inputs=[node.input[0], node.input[1]],\n                outputs=node.output,\n            )\n\n            model.graph.node.remove(node)\n            model.graph.node.insert(int(op_num), new_node)\n\n    onnx.checker.check_model(model)\n    onnx.save(model, model_path)\n\n\ndef wrap_onnx_config_for_loss(onnx_config: OnnxConfig) -> OnnxConfig:\n    if isinstance(onnx_config, OnnxSeq2SeqConfigWithPast):\n        return OnnxSeq2SeqConfigWithPastAndLoss(onnx_config)\n    elif isinstance(onnx_config, OnnxConfigWithPast):\n        return OnnxConfigWithPastAndLoss(onnx_config)\n    else:\n        return OnnxConfigWithLoss(onnx_config)\n", "path": "optimum/onnxruntime/utils.py"}]}