prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  pytorch2onnx fails with mmedit models
  error with master branch
  ```
  TypeError: forward_dummy() got an unexpected keyword argument 'img_metas'
  ```
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `mmdeploy/apis/pytorch2onnx.py`
  Content:
  ```
  1 # Copyright (c) OpenMMLab. All rights reserved.
  2 import os.path as osp
  3 from typing import Any, Optional, Union
  4 
  5 import mmcv
  6 import torch
  7 
  8 from mmdeploy.apis.core.pipeline_manager import no_mp
  9 from mmdeploy.utils import (get_backend, get_dynamic_axes, get_input_shape,
  10                             get_onnx_config, load_config)
  11 from .core import PIPELINE_MANAGER
  12 from .onnx import export
  13 
  14 
  15 @PIPELINE_MANAGER.register_pipeline()
  16 def torch2onnx(img: Any,
  17                work_dir: str,
  18                save_file: str,
  19                deploy_cfg: Union[str, mmcv.Config],
  20                model_cfg: Union[str, mmcv.Config],
  21                model_checkpoint: Optional[str] = None,
  22                device: str = 'cuda:0'):
  23     """Convert PyTorch model to ONNX model.
  24 
  25     Examples:
  26         >>> from mmdeploy.apis import torch2onnx
  27         >>> img = 'demo.jpg'
  28         >>> work_dir = 'work_dir'
  29         >>> save_file = 'fcos.onnx'
  30         >>> deploy_cfg = ('configs/mmdet/detection/'
  31                           'detection_onnxruntime_dynamic.py')
  32         >>> model_cfg = ('mmdetection/configs/fcos/'
  33                          'fcos_r50_caffe_fpn_gn-head_1x_coco.py')
  34         >>> model_checkpoint = ('checkpoints/'
  35                                 'fcos_r50_caffe_fpn_gn-head_1x_coco-821213aa.pth')
  36         >>> device = 'cpu'
  37         >>> torch2onnx(img, work_dir, save_file, deploy_cfg, \
  38             model_cfg, model_checkpoint, device)
  39 
  40     Args:
  41         img (str | np.ndarray | torch.Tensor): Input image used to assist
  42             converting model.
  43         work_dir (str): A working directory to save files.
  44         save_file (str): Filename to save onnx model.
  45         deploy_cfg (str | mmcv.Config): Deployment config file or
  46             Config object.
  47         model_cfg (str | mmcv.Config): Model config file or Config object.
  48         model_checkpoint (str): A checkpoint path of PyTorch model,
  49             defaults to `None`.
  50         device (str): A string specifying device type, defaults to 'cuda:0'.
  51     """
  52     # load deploy_cfg if necessary
  53     deploy_cfg, model_cfg = load_config(deploy_cfg, model_cfg)
  54     mmcv.mkdir_or_exist(osp.abspath(work_dir))
  55 
  56     input_shape = get_input_shape(deploy_cfg)
  57 
  58     # create model an inputs
  59     from mmdeploy.apis import build_task_processor
  60     task_processor = build_task_processor(model_cfg, deploy_cfg, device)
  61 
  62     torch_model = task_processor.init_pytorch_model(model_checkpoint)
  63     data, model_inputs = task_processor.create_input(img, input_shape)
  64     input_metas = dict(img_metas=data.get('img_metas', None))
  65     if not isinstance(model_inputs, torch.Tensor) and len(model_inputs) == 1:
  66         model_inputs = model_inputs[0]
  67 
  68     # export to onnx
  69     context_info = dict()
  70     context_info['deploy_cfg'] = deploy_cfg
  71     output_prefix = osp.join(work_dir,
  72                              osp.splitext(osp.basename(save_file))[0])
  73     backend = get_backend(deploy_cfg).value
  74 
  75     onnx_cfg = get_onnx_config(deploy_cfg)
  76     opset_version = onnx_cfg.get('opset_version', 11)
  77 
  78     input_names = onnx_cfg['input_names']
  79     output_names = onnx_cfg['output_names']
  80     axis_names = input_names + output_names
  81     dynamic_axes = get_dynamic_axes(deploy_cfg, axis_names)
  82     verbose = not onnx_cfg.get('strip_doc_string', True) or onnx_cfg.get(
  83         'verbose', False)
  84     keep_initializers_as_inputs = onnx_cfg.get('keep_initializers_as_inputs',
  85                                                True)
  86     optimize = onnx_cfg.get('optimize', False)
  87     with no_mp():
  88         export(
  89             torch_model,
  90             model_inputs,
  91             input_metas=input_metas,
  92             output_path_prefix=output_prefix,
  93             backend=backend,
  94             input_names=input_names,
  95             output_names=output_names,
  96             context_info=context_info,
  97             opset_version=opset_version,
  98             dynamic_axes=dynamic_axes,
  99             verbose=verbose,
  100             keep_initializers_as_inputs=keep_initializers_as_inputs,
  101             optimize=optimize)
  102
  ```
  --- 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/mmdeploy/apis/pytorch2onnx.py b/mmdeploy/apis/pytorch2onnx.py
  --- a/mmdeploy/apis/pytorch2onnx.py
  +++ b/mmdeploy/apis/pytorch2onnx.py
  @@ -61,7 +61,11 @@
   
       torch_model = task_processor.init_pytorch_model(model_checkpoint)
       data, model_inputs = task_processor.create_input(img, input_shape)
  -    input_metas = dict(img_metas=data.get('img_metas', None))
  +    if 'img_metas' in data:
  +        input_metas = dict(img_metas=data['img_metas'])
  +    else:
  +        # codebases like mmedit do not have img_metas argument
  +        input_metas = None
       if not isinstance(model_inputs, torch.Tensor) and len(model_inputs) == 1:
           model_inputs = model_inputs[0]
  
verification_info: {"golden_diff": "diff --git a/mmdeploy/apis/pytorch2onnx.py b/mmdeploy/apis/pytorch2onnx.py\n--- a/mmdeploy/apis/pytorch2onnx.py\n+++ b/mmdeploy/apis/pytorch2onnx.py\n@@ -61,7 +61,11 @@\n \n     torch_model = task_processor.init_pytorch_model(model_checkpoint)\n     data, model_inputs = task_processor.create_input(img, input_shape)\n-    input_metas = dict(img_metas=data.get('img_metas', None))\n+    if 'img_metas' in data:\n+        input_metas = dict(img_metas=data['img_metas'])\n+    else:\n+        # codebases like mmedit do not have img_metas argument\n+        input_metas = None\n     if not isinstance(model_inputs, torch.Tensor) and len(model_inputs) == 1:\n         model_inputs = model_inputs[0]\n", "issue": "pytorch2onnx fails with mmedit models\nerror with master branch\r\n```\r\nTypeError: forward_dummy() got an unexpected keyword argument 'img_metas'\r\n```\n", "before_files": [{"content": "# Copyright (c) OpenMMLab. All rights reserved.\nimport os.path as osp\nfrom typing import Any, Optional, Union\n\nimport mmcv\nimport torch\n\nfrom mmdeploy.apis.core.pipeline_manager import no_mp\nfrom mmdeploy.utils import (get_backend, get_dynamic_axes, get_input_shape,\n                            get_onnx_config, load_config)\nfrom .core import PIPELINE_MANAGER\nfrom .onnx import export\n\n\n@PIPELINE_MANAGER.register_pipeline()\ndef torch2onnx(img: Any,\n               work_dir: str,\n               save_file: str,\n               deploy_cfg: Union[str, mmcv.Config],\n               model_cfg: Union[str, mmcv.Config],\n               model_checkpoint: Optional[str] = None,\n               device: str = 'cuda:0'):\n    \"\"\"Convert PyTorch model to ONNX model.\n\n    Examples:\n        >>> from mmdeploy.apis import torch2onnx\n        >>> img = 'demo.jpg'\n        >>> work_dir = 'work_dir'\n        >>> save_file = 'fcos.onnx'\n        >>> deploy_cfg = ('configs/mmdet/detection/'\n                          'detection_onnxruntime_dynamic.py')\n        >>> model_cfg = ('mmdetection/configs/fcos/'\n                         'fcos_r50_caffe_fpn_gn-head_1x_coco.py')\n        >>> model_checkpoint = ('checkpoints/'\n                                'fcos_r50_caffe_fpn_gn-head_1x_coco-821213aa.pth')\n        >>> device = 'cpu'\n        >>> torch2onnx(img, work_dir, save_file, deploy_cfg, \\\n            model_cfg, model_checkpoint, device)\n\n    Args:\n        img (str | np.ndarray | torch.Tensor): Input image used to assist\n            converting model.\n        work_dir (str): A working directory to save files.\n        save_file (str): Filename to save onnx model.\n        deploy_cfg (str | mmcv.Config): Deployment config file or\n            Config object.\n        model_cfg (str | mmcv.Config): Model config file or Config object.\n        model_checkpoint (str): A checkpoint path of PyTorch model,\n            defaults to `None`.\n        device (str): A string specifying device type, defaults to 'cuda:0'.\n    \"\"\"\n    # load deploy_cfg if necessary\n    deploy_cfg, model_cfg = load_config(deploy_cfg, model_cfg)\n    mmcv.mkdir_or_exist(osp.abspath(work_dir))\n\n    input_shape = get_input_shape(deploy_cfg)\n\n    # create model an inputs\n    from mmdeploy.apis import build_task_processor\n    task_processor = build_task_processor(model_cfg, deploy_cfg, device)\n\n    torch_model = task_processor.init_pytorch_model(model_checkpoint)\n    data, model_inputs = task_processor.create_input(img, input_shape)\n    input_metas = dict(img_metas=data.get('img_metas', None))\n    if not isinstance(model_inputs, torch.Tensor) and len(model_inputs) == 1:\n        model_inputs = model_inputs[0]\n\n    # export to onnx\n    context_info = dict()\n    context_info['deploy_cfg'] = deploy_cfg\n    output_prefix = osp.join(work_dir,\n                             osp.splitext(osp.basename(save_file))[0])\n    backend = get_backend(deploy_cfg).value\n\n    onnx_cfg = get_onnx_config(deploy_cfg)\n    opset_version = onnx_cfg.get('opset_version', 11)\n\n    input_names = onnx_cfg['input_names']\n    output_names = onnx_cfg['output_names']\n    axis_names = input_names + output_names\n    dynamic_axes = get_dynamic_axes(deploy_cfg, axis_names)\n    verbose = not onnx_cfg.get('strip_doc_string', True) or onnx_cfg.get(\n        'verbose', False)\n    keep_initializers_as_inputs = onnx_cfg.get('keep_initializers_as_inputs',\n                                               True)\n    optimize = onnx_cfg.get('optimize', False)\n    with no_mp():\n        export(\n            torch_model,\n            model_inputs,\n            input_metas=input_metas,\n            output_path_prefix=output_prefix,\n            backend=backend,\n            input_names=input_names,\n            output_names=output_names,\n            context_info=context_info,\n            opset_version=opset_version,\n            dynamic_axes=dynamic_axes,\n            verbose=verbose,\n            keep_initializers_as_inputs=keep_initializers_as_inputs,\n            optimize=optimize)\n", "path": "mmdeploy/apis/pytorch2onnx.py"}], "after_files": [{"content": "# Copyright (c) OpenMMLab. All rights reserved.\nimport os.path as osp\nfrom typing import Any, Optional, Union\n\nimport mmcv\nimport torch\n\nfrom mmdeploy.apis.core.pipeline_manager import no_mp\nfrom mmdeploy.utils import (get_backend, get_dynamic_axes, get_input_shape,\n                            get_onnx_config, load_config)\nfrom .core import PIPELINE_MANAGER\nfrom .onnx import export\n\n\n@PIPELINE_MANAGER.register_pipeline()\ndef torch2onnx(img: Any,\n               work_dir: str,\n               save_file: str,\n               deploy_cfg: Union[str, mmcv.Config],\n               model_cfg: Union[str, mmcv.Config],\n               model_checkpoint: Optional[str] = None,\n               device: str = 'cuda:0'):\n    \"\"\"Convert PyTorch model to ONNX model.\n\n    Examples:\n        >>> from mmdeploy.apis import torch2onnx\n        >>> img = 'demo.jpg'\n        >>> work_dir = 'work_dir'\n        >>> save_file = 'fcos.onnx'\n        >>> deploy_cfg = ('configs/mmdet/detection/'\n                          'detection_onnxruntime_dynamic.py')\n        >>> model_cfg = ('mmdetection/configs/fcos/'\n                         'fcos_r50_caffe_fpn_gn-head_1x_coco.py')\n        >>> model_checkpoint = ('checkpoints/'\n                                'fcos_r50_caffe_fpn_gn-head_1x_coco-821213aa.pth')\n        >>> device = 'cpu'\n        >>> torch2onnx(img, work_dir, save_file, deploy_cfg, \\\n            model_cfg, model_checkpoint, device)\n\n    Args:\n        img (str | np.ndarray | torch.Tensor): Input image used to assist\n            converting model.\n        work_dir (str): A working directory to save files.\n        save_file (str): Filename to save onnx model.\n        deploy_cfg (str | mmcv.Config): Deployment config file or\n            Config object.\n        model_cfg (str | mmcv.Config): Model config file or Config object.\n        model_checkpoint (str): A checkpoint path of PyTorch model,\n            defaults to `None`.\n        device (str): A string specifying device type, defaults to 'cuda:0'.\n    \"\"\"\n    # load deploy_cfg if necessary\n    deploy_cfg, model_cfg = load_config(deploy_cfg, model_cfg)\n    mmcv.mkdir_or_exist(osp.abspath(work_dir))\n\n    input_shape = get_input_shape(deploy_cfg)\n\n    # create model an inputs\n    from mmdeploy.apis import build_task_processor\n    task_processor = build_task_processor(model_cfg, deploy_cfg, device)\n\n    torch_model = task_processor.init_pytorch_model(model_checkpoint)\n    data, model_inputs = task_processor.create_input(img, input_shape)\n    if 'img_metas' in data:\n        input_metas = dict(img_metas=data['img_metas'])\n    else:\n        # codebases like mmedit do not have img_metas argument\n        input_metas = None\n    if not isinstance(model_inputs, torch.Tensor) and len(model_inputs) == 1:\n        model_inputs = model_inputs[0]\n\n    # export to onnx\n    context_info = dict()\n    context_info['deploy_cfg'] = deploy_cfg\n    output_prefix = osp.join(work_dir,\n                             osp.splitext(osp.basename(save_file))[0])\n    backend = get_backend(deploy_cfg).value\n\n    onnx_cfg = get_onnx_config(deploy_cfg)\n    opset_version = onnx_cfg.get('opset_version', 11)\n\n    input_names = onnx_cfg['input_names']\n    output_names = onnx_cfg['output_names']\n    axis_names = input_names + output_names\n    dynamic_axes = get_dynamic_axes(deploy_cfg, axis_names)\n    verbose = not onnx_cfg.get('strip_doc_string', True) or onnx_cfg.get(\n        'verbose', False)\n    keep_initializers_as_inputs = onnx_cfg.get('keep_initializers_as_inputs',\n                                               True)\n    optimize = onnx_cfg.get('optimize', False)\n    with no_mp():\n        export(\n            torch_model,\n            model_inputs,\n            input_metas=input_metas,\n            output_path_prefix=output_prefix,\n            backend=backend,\n            input_names=input_names,\n            output_names=output_names,\n            context_info=context_info,\n            opset_version=opset_version,\n            dynamic_axes=dynamic_axes,\n            verbose=verbose,\n            keep_initializers_as_inputs=keep_initializers_as_inputs,\n            optimize=optimize)\n", "path": "mmdeploy/apis/pytorch2onnx.py"}]}