__init__.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # Copyright (c) 2025 PaddlePaddle Authors. 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. import contextlib
  15. import importlib
  16. from pathlib import Path
  17. from typing import Any, Dict, Optional, Type
  18. from pydantic import BaseModel
  19. from ....utils import logging
  20. from ...utils.official_models import official_models
  21. from ..utils import check_backend, model_name_to_module_name
  22. NETWORK_CLASS_GETTER_KEY = "get_network_class"
  23. PROCESSOR_CLASS_GETTER_KEY = "get_processor_class"
  24. CONFIG_GETTER_KEY = "get_config"
  25. CHAT_TEMPLATE_PATH_GETTER_KEY = "get_chat_template_path"
  26. DEFAULT_CHAT_TEMPLATE_FILENAME = "chat_template.jinja"
  27. ALL_MODEL_NAMES = {"PaddleOCR-VL-0.9B"}
  28. def _check_model_name_and_backend(model_name, backend):
  29. if model_name not in ALL_MODEL_NAMES:
  30. raise ValueError(f"Unknown model: {model_name}")
  31. check_backend(backend)
  32. def get_model_dir(model_name, backend):
  33. _check_model_name_and_backend(model_name, backend)
  34. try:
  35. model_dir = official_models[model_name]
  36. except Exception as e:
  37. raise RuntimeError(
  38. f"Could not prepare the official model for the {repr(model_name)} model with the {repr(backend)} backend."
  39. ) from e
  40. return str(model_dir)
  41. def get_model_components(model_name, backend):
  42. def _get_component(getter_key):
  43. if not hasattr(model_module, getter_key):
  44. raise RuntimeError(f"`{model_module}` does not have `{getter_key}`")
  45. getter = getattr(model_module, getter_key)
  46. comp = getter(backend)
  47. return comp
  48. _check_model_name_and_backend(model_name, backend)
  49. mod_name = model_name_to_module_name(model_name)
  50. try:
  51. model_module = importlib.import_module(f".{mod_name}", package=__package__)
  52. except ModuleNotFoundError as e:
  53. raise ValueError(f"Unknown model: {model_name}") from e
  54. network_class = _get_component(NETWORK_CLASS_GETTER_KEY)
  55. if backend == "sglang":
  56. processor_class = _get_component(PROCESSOR_CLASS_GETTER_KEY)
  57. else:
  58. processor_class = None
  59. return network_class, processor_class
  60. def get_default_config(model_name, backend):
  61. _check_model_name_and_backend(model_name, backend)
  62. mod_name = model_name_to_module_name(model_name)
  63. try:
  64. config_module = importlib.import_module(
  65. f"..configs.{mod_name}", package=__package__
  66. )
  67. except ModuleNotFoundError:
  68. logging.debug("No default configs were found for the model '%s'", model_name)
  69. default_config = {}
  70. else:
  71. if not hasattr(config_module, CONFIG_GETTER_KEY):
  72. raise RuntimeError(f"`{config_module}` does not have `{CONFIG_GETTER_KEY}`")
  73. config_getter = getattr(config_module, CONFIG_GETTER_KEY)
  74. default_config = config_getter(backend)
  75. return default_config
  76. @contextlib.contextmanager
  77. def get_chat_template_path(model_name, backend, model_dir):
  78. _check_model_name_and_backend(model_name, backend)
  79. with importlib.resources.path(
  80. "paddlex.inference.genai.chat_templates", f"{model_name}.jinja"
  81. ) as chat_template_path:
  82. if not chat_template_path.exists():
  83. default_chat_template_path = Path(model_dir, DEFAULT_CHAT_TEMPLATE_FILENAME)
  84. if (
  85. default_chat_template_path.exists()
  86. and default_chat_template_path.is_file()
  87. ):
  88. # TODO: Support symbolic links
  89. yield default_chat_template_path
  90. else:
  91. logging.debug(
  92. "No chat template was found for the model '%s' with the backend '%s'",
  93. model_name,
  94. backend,
  95. )
  96. yield None
  97. else:
  98. yield chat_template_path