text: Title: Export torchscript not support dynamic batch and nms
  Body: ### Search before asking
  
  - [x] I have searched the Ultralytics YOLO [issues](https://github.com/ultralytics/ultralytics/issues) and found no similar bug report.
  
  
  ### Ultralytics YOLO Component
  
  _No response_
  
  ### Bug
  
  code
  ```
  def export(model_path='yolo11x-pose.pt', imagz=832, batch=8):
      model = YOLO(model_path)
  
      # dynamic_axes = {
      #     'images': {0: 'batch', 2: 'height', 3: 'width'},  # 让 batch, height, width 为动态维度
      #     'output0': {0: 'batch', 1: 300, 2: 6}  # 假设你需要设置输出维度
      # }
  
  
      onnx_model_path = model.export(imgsz=imagz, format="torchscript",
                                     **{'batch': batch, 'half': False, 'simplify': True,
                                        'nms': True, })
      print(f"✅ 模型已导出为 torchscript 格式: {onnx_model_path}")
      # # 转换为 FP16 格式
      # output_path = onnx_model_path.replace('.onnx', '_fp16.onnx')
      # utils.convert_to_fp16(onnx_model_path, output_path)
      return onnx_model_path
  
  
  
  
  
  imagz = 640
  model_path = 'det_31c.pt'
  batch = 8
  script_path = export(model_path, imagz=imagz, batch=batch)
  
  ```
  If I use nms = false  , also get same error.
  
  
  error 
  ```
  Traceback of TorchScript, original code (most recent call last):
  /data/anaconda3/envs/wyx/lib/python3.11/site-packages/ultralytics/engine/exporter.py(1576): forward
  /data/anaconda3/envs/wyx/lib/python3.11/site-packages/torch/nn/modules/module.py(1729): _slow_forward
  /data/anaconda3/envs/wyx/lib/python3.11/site-packages/torch/nn/modules/module.py(1750): _call_impl
  /data/anaconda3/envs/wyx/lib/python3.11/site-packages/torch/nn/modules/module.py(1739): _wrapped_call_impl
  /data/anaconda3/envs/wyx/lib/python3.11/site-packages/torch/jit/_trace.py(1276): trace_module
  /data/anaconda3/envs/wyx/lib/python3.11/site-packages/torch/jit/_trace.py(696): _trace_impl
  /data/anaconda3/envs/wyx/lib/python3.11/site-packages/torch/jit/_trace.py(1000): trace
  /data/anaconda3/envs/wyx/lib/python3.11/site-packages/ultralytics/engine/exporter.py(492): export_torchscript
  /data/anaconda3/envs/wyx/lib/python3.11/site-packages/ultralytics/engine/exporter.py(175): outer_func
  /data/anaconda3/envs/wyx/lib/python3.11/site-packages/ultralytics/engine/exporter.py(407): __call__
  /data/anaconda3/envs/wyx/lib/python3.11/site-packages/ultralytics/engine/model.py(741): export
  /tmp/wyx/yolo_onnx_demo/yolo_export_script.py(32): export
  /tmp/wyx/yolo_onnx_demo/yolo_export_script.py(48): <module>
  RuntimeError: select(): index 5 out of range for tensor of size [5, 8400, 4] at dimension 0
  
  
  Thread [77] had error: PyTorch execute failure: The following operation failed in the TorchScript interpreter.
  Traceback of TorchScript, serialized code (most recent call last):
    File "code/__torch__/ultralytics/engine/exporter.py", line 277, in forward
      _169 = torch.pad(dets3, _168)
      _170 = torch.copy_(torch.select(out, 0, 4), _169)
      box4 = torch.select(boxes, 0, 5)
             ~~~~~~~~~~~~ <--- HERE
      cls9 = torch.select(classes, 0, 5)
      score9 = torch.select(scores0, 0, 5)
  
  Traceback of TorchScript, original code (most recent call last):
  /data/anaconda3/envs/wyx/lib/python3.11/site-packages/ultralytics/engine/exporter.py(1576): forward
  /data/anaconda3/envs/wyx/lib/python3.11/site-packages/torch/nn/modules/module.py(1729): _slow_forward
  /data/anaconda3/envs/wyx/lib/python3.11/site-packages/torch/nn/modules/module.py(1750): _call_impl
  /data/anaconda3/envs/wyx/lib/python3.11/site-packages/torch/nn/modules/module.py(1739): _wrapped_call_impl
  /data/anaconda3/envs/wyx/lib/python3.11/site-packages/torch/jit/_trace.py(1276): trace_module
  /data/anaconda3/envs/wyx/lib/python3.11/site-packages/torch/jit/_trace.py(696): _trace_impl
  /data/anaconda3/envs/wyx/lib/python3.11/site-packages/torch/jit/_trace.py(1000): trace
  /data/anaconda3/envs/wyx/lib/python3.11/site-packages/ultralytics/engine/exporter.py(492): export_torchscript
  /data/anaconda3/envs/wyx/lib/python3.11/site-packages/ultralytics/engine/exporter.py(175): outer_func
  /data/anaconda3/envs/wyx/lib/python3.11/site-packages/ultralytics/engine/exporter.py(407): __call__
  /data/anaconda3/envs/wyx/lib/python3.11/site-packages/ultralytics/engine/model.py(741): export
  /tmp/wyx/yolo_onnx_demo/yolo_export_script.py(32): export
  /tmp/wyx/yolo_onnx_demo/yolo_export_script.py(48): <module>
  RuntimeError: select(): index 5 out of range for tensor of size [5, 8400, 4] at dimension 0
  
  
  Thread [91] had error: PyTorch execute failure: The following operation failed in the TorchScript interpreter.
  ```
  
  
  
  
  I want to modify the `exporter.py`  to slove.
  
  If I use `torch.jit.script` to  export such as `task.py  , head.py , module.py` will get error  and I don't know how to fix them.
  
  And I use it after `torch.jit.trace` get same error when use different batch size to inference
  ```
     @try_export
      def export_torchscript(self, prefix=colorstr("TorchScript:")):
          """YOLO TorchScript model export."""
          LOGGER.info(f"\n{prefix} starting export with torch {torch.__version__}...")
          f = self.file.with_suffix(".torchscript")
  
          ts = torch.jit.trace(NMSModel(self.model, self.args) if self.args.nms else self.model, self.im, strict=False)
          ts = torch.jit.script(ts)
          extra_files = {"config.txt": json.dumps(self.metadata)}  # torch._C.ExtraFilesMap()
          if self.args.optimize:  # https://pytorch.org/tutorials/recipes/mobile_interpreter.html
              LOGGER.info(f"{prefix} optimizing for mobile...")
              from torch.utils.mobile_optimizer import optimize_for_mobile
  
              optimize_for_mobile(ts)._save_for_lite_interpreter(str(f), _extra_files=extra_files)
          else:
              ts.save(str(f), _extra_files=extra_files)
          return f, None
  
  ```
  
  
  
  I don't know how to solve my problem
  
  ### Environment
  
  Ultralytics 8.3.76 🚀 Python-3.11.11 torch-2.6.0+cu124 CPU (Intel Xeon Gold 6462C)
  Model summary (fused): 112 layers, 43,630,509 parameters, 0 gradients, 164.9 GFLOPs
  
  PyTorch: starting from 'det_31c.pt' with input shape (8, 3, 640, 640) BCHW and output shape(s) (8, 35, 8400) (83.6 MB)
  
  ### Minimal Reproducible Example
  
  ```
  ```
  
  ### Additional
  
  _No response_
  
  ### Are you willing to submit a PR?
  
  - [ ] Yes I'd like to help by submitting a PR!