lmdeploy_server.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import os
  2. import sys
  3. from loguru import logger
  4. from mineru.backend.vlm.utils import set_lmdeploy_backend
  5. from mineru.utils.models_download_utils import auto_download_and_get_model_root_path
  6. def main():
  7. args = sys.argv[1:]
  8. has_port_arg = False
  9. has_gpu_memory_utilization_arg = False
  10. has_log_level_arg = False
  11. has_backend_arg = False
  12. device_type = ""
  13. lm_backend = ""
  14. # 检查现有参数
  15. for i, arg in enumerate(args):
  16. if arg == "--server-port" or arg.startswith("--server-port="):
  17. has_port_arg = True
  18. if arg == "--cache-max-entry-count" or arg.startswith("--cache-max-entry-count="):
  19. has_gpu_memory_utilization_arg = True
  20. if arg == "--log-level" or arg.startswith("--log-level="):
  21. has_log_level_arg = True
  22. if arg == "--backend":
  23. has_backend_arg = True
  24. if i + 1 < len(args):
  25. lm_backend = args[i + 1]
  26. if arg.startswith("--backend="):
  27. has_backend_arg = True
  28. lm_backend = arg.split("=", 1)[1]
  29. if arg == "--device":
  30. if i + 1 < len(args):
  31. device_type = args[i + 1]
  32. if arg.startswith("--device="):
  33. device_type = arg.split("=", 1)[1]
  34. # 添加默认参数
  35. if not has_port_arg:
  36. args.extend(["--server-port", "30000"])
  37. if not has_gpu_memory_utilization_arg:
  38. args.extend(["--cache-max-entry-count", "0.5"])
  39. if not has_log_level_arg:
  40. args.extend(["--log-level", "ERROR"])
  41. if lm_backend == "":
  42. if device_type == "":
  43. device_type = "cuda"
  44. elif device_type not in ["cuda", "ascend", "maca", "camb"]:
  45. raise ValueError(f"Unsupported lmdeploy device type: {device_type}")
  46. lm_backend = set_lmdeploy_backend(device_type)
  47. elif lm_backend not in ["pytorch", "turbomind"]:
  48. raise ValueError(f"Unsupported lmdeploy backend: {lm_backend}")
  49. logger.info(f"Set lmdeploy_backend to: {lm_backend}")
  50. if lm_backend == "pytorch":
  51. os.environ["TOKENIZERS_PARALLELISM"] = "false"
  52. # args中如果有--backend参数,则不设置
  53. if not has_backend_arg:
  54. args.extend(["--backend", lm_backend])
  55. model_path = auto_download_and_get_model_root_path("/", "vlm")
  56. # 重构参数,将模型路径作为位置参数
  57. sys.argv = [sys.argv[0]] + ["serve", "api_server", model_path] + args
  58. if os.getenv('OMP_NUM_THREADS') is None:
  59. os.environ["OMP_NUM_THREADS"] = "1"
  60. # 启动 lmdeploy 服务器
  61. print(f"start lmdeploy server: {sys.argv}")
  62. # 使用os.system调用启动lmdeploy服务器
  63. os.system("lmdeploy " + " ".join(sys.argv[1:]))
  64. if __name__ == "__main__":
  65. main()