lmdeploy_server.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 = "cuda"
  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 ":" in device_type:
  42. device_type = device_type.split(":")[0]
  43. if lm_backend == "":
  44. lm_backend = set_lmdeploy_backend(device_type)
  45. logger.info(f"Set lmdeploy_backend to: {lm_backend}")
  46. # args中如果有--backend参数,则不设置
  47. if not has_backend_arg:
  48. args.extend(["--backend", lm_backend])
  49. model_path = auto_download_and_get_model_root_path("/", "vlm")
  50. # 重构参数,将模型路径作为位置参数
  51. sys.argv = [sys.argv[0]] + ["serve", "api_server", model_path] + args
  52. if os.getenv('OMP_NUM_THREADS') is None:
  53. os.environ["OMP_NUM_THREADS"] = "1"
  54. # 启动 lmdeploy 服务器
  55. print(f"start lmdeploy server: {sys.argv}")
  56. # 使用os.system调用启动lmdeploy服务器
  57. os.system("lmdeploy " + " ".join(sys.argv[1:]))
  58. if __name__ == "__main__":
  59. main()