envs.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. import os
  2. import tempfile
  3. from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional
  4. if TYPE_CHECKING:
  5. APHRODITE_HOST_IP: str = ""
  6. APHRODITE_PORT: Optional[int] = None
  7. APHRODITE_RPC_BASE_PATH: str = tempfile.gettempdir()
  8. APHRODITE_USE_MODELSCOPE: bool = False
  9. APHRODITE_RINGBUFFER_WARNING_INTERVAL: int = 60
  10. APHRODITE_INSTANCE_ID: Optional[str] = None
  11. APHRODITE_NCCL_SO_PATH: Optional[str] = None
  12. LD_LIBRARY_PATH: Optional[str] = None
  13. APHRODITE_USE_TRITON_FLASH_ATTN: bool = False
  14. LOCAL_RANK: int = 0
  15. CUDA_VISIBLE_DEVICES: Optional[str] = None
  16. APHRODITE_ENGINE_ITERATION_TIMEOUT_S: int = 60
  17. APHRODITE_API_KEY: Optional[str] = None
  18. S3_ACCESS_KEY_ID: Optional[str] = None
  19. S3_SECRET_ACCESS_KEY: Optional[str] = None
  20. S3_ENDPOINT_URL: Optional[str] = None
  21. APHRODITE_CACHE_ROOT: str = os.path.expanduser("~/.cache/aphrodite")
  22. APHRODITE_CONFIG_ROOT: str = os.path.expanduser("~/.config/aphrodite")
  23. APHRODITE_CONFIGURE_LOGGING: int = 1
  24. APHRODITE_LOGGING_LEVEL: str = "INFO"
  25. APHRODITE_LOGGING_CONFIG_PATH: Optional[str] = None
  26. APHRODITE_TRACE_FUNCTION: int = 0
  27. APHRODITE_ATTENTION_BACKEND: Optional[str] = None
  28. APHRODITE_USE_SAMPLING_KERNELS: bool = False
  29. APHRODITE_PP_LAYER_PARTITION: Optional[str] = None
  30. APHRODITE_CPU_KVCACHE_SPACE: int = 0
  31. APHRODITE_CPU_OMP_THREADS_BIND: str = ""
  32. APHRODITE_OPENVINO_KVCACHE_SPACE: int = 0
  33. APHRODITE_OPENVINO_CPU_KV_CACHE_PRECISION: Optional[str] = None
  34. APHRODITE_OPENVINO_ENABLE_QUANTIZED_WEIGHTS: bool = False
  35. APHRODITE_XLA_CACHE_PATH: str = os.path.join(APHRODITE_CACHE_ROOT, "xla_cache") # noqa: E501
  36. APHRODITE_FUSED_MOE_CHUNK_SIZE: int = 64 * 1024
  37. APHRODITE_USE_RAY_SPMD_WORKER: bool = False
  38. APHRODITE_USE_RAY_COMPILED_DAG: bool = False
  39. APHRODITE_USE_RAY_COMPILED_DAG_NCCL_CHANNEL: bool = True
  40. APHRODITE_WORKER_MULTIPROC_METHOD: str = "fork"
  41. APHRODITE_ASSETS_CACHE: str = os.path.join(APHRODITE_CACHE_ROOT, "assets")
  42. APHRODITE_IMAGE_FETCH_TIMEOUT: int = 5
  43. APHRODITE_AUDIO_FETCH_TIMEOUT: int = 5
  44. APHRODITE_TARGET_DEVICE: str = "cuda"
  45. MAX_JOBS: Optional[str] = None
  46. NVCC_THREADS: Optional[str] = None
  47. APHRODITE_USE_PRECOMPILED: bool = False
  48. APHRODITE_NO_DEPRECATION_WARNING: bool = False
  49. APHRODITE_KEEP_ALIVE_ON_ENGINE_DEATH: bool = False
  50. CMAKE_BUILD_TYPE: Optional[str] = None
  51. VERBOSE: bool = False
  52. APHRODITE_DYNAMIC_ROPE_SCALING: bool = False
  53. APHRODITE_TEST_FORCE_FP8_MARLIN: bool = False
  54. APHRODITE_ALLOW_ENGINE_USE_RAY: bool = False
  55. APHRODITE_PLUGINS: Optional[List[str]] = None
  56. def get_default_cache_root():
  57. return os.getenv(
  58. "XDG_CACHE_HOME",
  59. os.path.join(os.path.expanduser("~"), ".cache"),
  60. )
  61. def get_default_config_root():
  62. return os.getenv(
  63. "XDG_CONFIG_HOME",
  64. os.path.join(os.path.expanduser("~"), ".config"),
  65. )
  66. # The begin-* and end* here are used by the documentation generator
  67. # to extract the used env vars.
  68. # begin-env-vars-definition
  69. environment_variables: Dict[str, Callable[[], Any]] = {
  70. # ================== Installation Time Env Vars ==================
  71. # Target device of Aphrodite, supporting [cuda (by default),
  72. # rocm, neuron, cpu, openvino]
  73. "APHRODITE_TARGET_DEVICE":
  74. lambda: os.getenv("APHRODITE_TARGET_DEVICE", "cuda"),
  75. # Maximum number of compilation jobs to run in parallel.
  76. # By default this is the number of CPUs
  77. "MAX_JOBS":
  78. lambda: os.getenv("MAX_JOBS", None),
  79. # Number of threads to use for nvcc
  80. # By default this is 1.
  81. # If set, `MAX_JOBS` will be reduced to avoid oversubscribing the CPU.
  82. "NVCC_THREADS":
  83. lambda: os.getenv("NVCC_THREADS", None),
  84. # If set, Aphrodite will use precompiled binaries (*.so)
  85. "APHRODITE_USE_PRECOMPILED":
  86. lambda: bool(os.environ.get("APHRODITE_USE_PRECOMPILED")),
  87. # CMake build type
  88. # If not set, defaults to "Debug" or "RelWithDebInfo"
  89. # Available options: "Debug", "Release", "RelWithDebInfo"
  90. "CMAKE_BUILD_TYPE":
  91. lambda: os.getenv("CMAKE_BUILD_TYPE"),
  92. # If set, Aphrodite will print verbose logs during installation
  93. "VERBOSE":
  94. lambda: bool(int(os.getenv('VERBOSE', '0'))),
  95. # Root directory for APHRODITE configuration files
  96. # Defaults to `~/.config/aphrodite` unless `XDG_CONFIG_HOME` is set
  97. # Note that this not only affects how aphrodite finds its configuration
  98. # files during runtime, but also affects how aphrodite installs its
  99. # configuration files during **installation**.
  100. "APHRODITE_CONFIG_ROOT":
  101. lambda: os.path.expanduser(
  102. os.getenv(
  103. "APHRODITE_CONFIG_ROOT",
  104. os.path.join(get_default_config_root(), "aphrodite"),
  105. )),
  106. # ================== Runtime Env Vars ==================
  107. # Root directory for APHRODITE cache files
  108. # Defaults to `~/.cache/aphrodite` unless `XDG_CACHE_HOME` is set
  109. "APHRODITE_CACHE_ROOT":
  110. lambda: os.path.expanduser(
  111. os.getenv(
  112. "APHRODITE_CACHE_ROOT",
  113. os.path.join(get_default_cache_root(), "aphrodite"),
  114. )),
  115. # used in distributed environment to determine the ip address
  116. # of the current node, when the node has multiple network interfaces.
  117. # If you are using multi-node inference, you should set this differently
  118. # on each node.
  119. 'APHRODITE_HOST_IP':
  120. lambda: os.getenv('APHRODITE_HOST_IP', "") or os.getenv("HOST_IP", ""),
  121. # used in distributed environment to manually set the communication port
  122. # Note: if APHRODITE_PORT is set, and some code asks for multiple ports, the
  123. # APHRODITE_PORT will be used as the first port, and the rest will be
  124. # generated by incrementing the APHRODITE_PORT value.
  125. # '0' is used to make mypy happy
  126. 'APHRODITE_PORT':
  127. lambda: int(os.getenv('APHRODITE_PORT', '0'))
  128. if 'APHRODITE_PORT' in os.environ else None,
  129. # path used for ipc when the frontend api server is running in
  130. # multi-processing mode to communicate with the backend engine process.
  131. 'APHRODITE_RPC_BASE_PATH':
  132. lambda: os.getenv('APHRODITE_RPC_BASE_PATH', tempfile.gettempdir()),
  133. # If true, will load models from ModelScope instead of Hugging Face Hub.
  134. # note that the value is true or false, not numbers
  135. "APHRODITE_USE_MODELSCOPE":
  136. lambda: os.environ.get(
  137. "APHRODITE_USE_MODELSCOPE", "False").lower() == "true",
  138. # Instance id represents an instance of the APHRODITE. All processes in the
  139. # same instance should have the same instance id.
  140. "APHRODITE_INSTANCE_ID":
  141. lambda: os.environ.get("APHRODITE_INSTANCE_ID", None),
  142. # Interval in seconds to log a warning message when the ring buffer is full
  143. "APHRODITE_RINGBUFFER_WARNING_INTERVAL":
  144. lambda: int(os.environ.get("APHRODITE_RINGBUFFER_WARNING_INTERVAL", "60")),
  145. # path to cudatoolkit home directory, under which should be bin, include,
  146. # and lib directories.
  147. "CUDA_HOME":
  148. lambda: os.environ.get("CUDA_HOME", None),
  149. # Path to the NCCL library file. It is needed because nccl>=2.19 brought
  150. # by PyTorch contains a bug: https://github.com/NVIDIA/nccl/issues/1234
  151. "APHRODITE_NCCL_SO_PATH":
  152. lambda: os.environ.get("APHRODITE_NCCL_SO_PATH", None),
  153. # when `APHRODITE_NCCL_SO_PATH` is not set, aphrodite will try to find the
  154. # nccl library file in the locations specified by `LD_LIBRARY_PATH`
  155. "LD_LIBRARY_PATH":
  156. lambda: os.environ.get("LD_LIBRARY_PATH", None),
  157. # flag to control if aphrodite should use triton flash attention
  158. "APHRODITE_USE_TRITON_FLASH_ATTN":
  159. lambda: (os.environ.get(
  160. "APHRODITE_USE_TRITON_FLASH_ATTN", "True").lower() in ("true", "1")),
  161. # Internal flag to enable Dynamo graph capture
  162. "APHRODITE_TEST_DYNAMO_GRAPH_CAPTURE":
  163. lambda: int(os.environ.get("APHRODITE_TEST_DYNAMO_GRAPH_CAPTURE", "0")),
  164. # local rank of the process in the distributed setting, used to determine
  165. # the GPU device id
  166. "LOCAL_RANK":
  167. lambda: int(os.environ.get("LOCAL_RANK", "0")),
  168. # used to control the visible devices in the distributed setting
  169. "CUDA_VISIBLE_DEVICES":
  170. lambda: os.environ.get("CUDA_VISIBLE_DEVICES", None),
  171. # timeout for each iteration in the engine
  172. "APHRODITE_ENGINE_ITERATION_TIMEOUT_S":
  173. lambda: int(os.environ.get("APHRODITE_ENGINE_ITERATION_TIMEOUT_S", "60")),
  174. # API key for APHRODITE API server
  175. "APHRODITE_API_KEY":
  176. lambda: os.environ.get("APHRODITE_API_KEY", None),
  177. # S3 access information, used for tensorizer to load model from S3
  178. "S3_ACCESS_KEY_ID":
  179. lambda: os.environ.get("S3_ACCESS_KEY_ID", None),
  180. "S3_SECRET_ACCESS_KEY":
  181. lambda: os.environ.get("S3_SECRET_ACCESS_KEY", None),
  182. "S3_ENDPOINT_URL":
  183. lambda: os.environ.get("S3_ENDPOINT_URL", None),
  184. # Logging configuration
  185. # If set to 0, aphrodite will not configure logging
  186. # If set to 1, aphrodite will configure logging using the default
  187. # configuration or the configuration file specified by
  188. # APHRODITE_LOGGING_CONFIG_PATH
  189. "APHRODITE_CONFIGURE_LOGGING":
  190. lambda: int(os.getenv("APHRODITE_CONFIGURE_LOGGING", "1")),
  191. "APHRODITE_LOGGING_CONFIG_PATH":
  192. lambda: os.getenv("APHRODITE_LOGGING_CONFIG_PATH"),
  193. # this is used for configuring the default logging level
  194. "APHRODITE_LOGGING_LEVEL":
  195. lambda: os.getenv("APHRODITE_LOGGING_LEVEL", "INFO"),
  196. # Trace function calls
  197. # If set to 1, aphrodite will trace function calls
  198. # Useful for debugging
  199. "APHRODITE_TRACE_FUNCTION":
  200. lambda: int(os.getenv("APHRODITE_TRACE_FUNCTION", "0")),
  201. # Backend for attention computation
  202. # Available options:
  203. # - "TORCH_SDPA": use torch.nn.MultiheadAttention
  204. # - "FLASH_ATTN": use FlashAttention
  205. # - "XFORMERS": use XFormers
  206. # - "ROCM_FLASH": use ROCmFlashAttention
  207. # - "FLASHINFER": use flashinfer
  208. "APHRODITE_ATTENTION_BACKEND":
  209. lambda: os.getenv("APHRODITE_ATTENTION_BACKEND", None),
  210. # If set, aphrodite will use flashinfer sampler
  211. "APHRODITE_USE_SAMPLING_KERNELS":
  212. lambda: bool(int(os.getenv("APHRODITE_USE_SAMPLING_KERNELS", "0"))),
  213. # Pipeline stage partition strategy
  214. "APHRODITE_PP_LAYER_PARTITION":
  215. lambda: os.getenv("APHRODITE_PP_LAYER_PARTITION", None),
  216. # (CPU backend only) CPU key-value cache space.
  217. # default is 4GB
  218. "APHRODITE_CPU_KVCACHE_SPACE":
  219. lambda: int(os.getenv("APHRODITE_CPU_KVCACHE_SPACE", "0")),
  220. # (CPU backend only) CPU core ids bound by OpenMP threads, e.g., "0-31",
  221. # "0,1,2", "0-31,33". CPU cores of different ranks are separated by '|'.
  222. "APHRODITE_CPU_OMP_THREADS_BIND":
  223. lambda: os.getenv("APHRODITE_CPU_OMP_THREADS_BIND", "all"),
  224. # OpenVINO key-value cache space
  225. # default is 4GB
  226. "APHRODITE_OPENVINO_KVCACHE_SPACE":
  227. lambda: int(os.getenv("APHRODITE_OPENVINO_KVCACHE_SPACE", "0")),
  228. # OpenVINO KV cache precision
  229. # default is bf16 if natively supported by platform, otherwise f16
  230. # To enable KV cache compression, please, explicitly specify u8
  231. "APHRODITE_OPENVINO_CPU_KV_CACHE_PRECISION":
  232. lambda: os.getenv("APHRODITE_OPENVINO_CPU_KV_CACHE_PRECISION", None),
  233. # Enables weights compression during model export via HF Optimum
  234. # default is False
  235. "APHRODITE_OPENVINO_ENABLE_QUANTIZED_WEIGHTS":
  236. lambda: bool(os.getenv(
  237. "APHRODITE_OPENVINO_ENABLE_QUANTIZED_WEIGHTS", False)),
  238. # If the env var is set, then all workers will execute as separate
  239. # processes from the engine, and we use the same mechanism to trigger
  240. # execution on all workers.
  241. # Run aphrodite with APHRODITE_USE_RAY_SPMD_WORKER=1 to enable it.
  242. "APHRODITE_USE_RAY_SPMD_WORKER":
  243. lambda: bool(int(os.getenv("APHRODITE_USE_RAY_SPMD_WORKER", "0"))),
  244. # If the env var is set, it uses the Ray's compiled DAG API
  245. # which optimizes the control plane overhead.
  246. # Run aphrodite with APHRODITE_USE_RAY_COMPILED_DAG=1 to enable it.
  247. "APHRODITE_USE_RAY_COMPILED_DAG":
  248. lambda: bool(int(os.getenv("APHRODITE_USE_RAY_COMPILED_DAG", "0"))),
  249. # If the env var is set, it uses NCCL for communication in
  250. # Ray's compiled DAG. This flag is ignored if
  251. # APHRODITE_USE_RAY_COMPILED_DAG is not set.
  252. "APHRODITE_USE_RAY_COMPILED_DAG_NCCL_CHANNEL":
  253. lambda: bool(int(
  254. os.getenv("APHRODITE_USE_RAY_COMPILED_DAG_NCCL_CHANNEL", "1"))),
  255. # Use dedicated multiprocess context for workers.
  256. # Both spawn and fork work
  257. "APHRODITE_WORKER_MULTIPROC_METHOD":
  258. lambda: os.getenv("APHRODITE_WORKER_MULTIPROC_METHOD", "fork"),
  259. # Path to the cache for storing downloaded assets
  260. "APHRODITE_ASSETS_CACHE":
  261. lambda: os.path.expanduser(
  262. os.getenv(
  263. "APHRODITE_ASSETS_CACHE",
  264. os.path.join(get_default_cache_root(), "aphrodite", "assets"),
  265. )),
  266. # Timeout for fetching images when serving multimodal models
  267. # Default is 5 seconds
  268. "APHRODITE_IMAGE_FETCH_TIMEOUT":
  269. lambda: int(os.getenv("APHRODITE_IMAGE_FETCH_TIMEOUT", "5")),
  270. # Timeout for fetching audio when serving multimodal models
  271. # Default is 5 seconds
  272. "APHRODITE_AUDIO_FETCH_TIMEOUT":
  273. lambda: int(os.getenv("APHRODITE_AUDIO_FETCH_TIMEOUT", "5")),
  274. # Path to the XLA persistent cache directory.
  275. # Only used for XLA devices such as TPUs.
  276. "APHRODITE_XLA_CACHE_PATH":
  277. lambda: os.path.expanduser(
  278. os.getenv(
  279. "APHRODITE_XLA_CACHE_PATH",
  280. os.path.join(get_default_cache_root(), "aphrodite", "xla_cache"),
  281. )),
  282. "APHRODITE_FUSED_MOE_CHUNK_SIZE":
  283. lambda: int(os.getenv("APHRODITE_FUSED_MOE_CHUNK_SIZE", "65536")),
  284. # If set, aphrodite will skip the deprecation warnings.
  285. "APHRODITE_NO_DEPRECATION_WARNING":
  286. lambda: bool(int(os.getenv("APHRODITE_NO_DEPRECATION_WARNING", "0"))),
  287. # If set, the OpenAI API server will stay alive even after the underlying
  288. # AsyncLLMEngine errors and stops serving requests
  289. "APHRODITE_KEEP_ALIVE_ON_ENGINE_DEATH":
  290. lambda: bool(os.getenv("APHRODITE_KEEP_ALIVE_ON_ENGINE_DEATH", 0)),
  291. # If the env var APHRODITE_DYNAMIC_ROPE_SCALING is set, it allows
  292. # the user to specify a max sequence length greater than
  293. # the max length derived from the model's config.json.
  294. # To enable this, set APHRODITE_DYNAMIC_ROPE_SCALING=1.
  295. "APHRODITE_DYNAMIC_ROPE_SCALING":
  296. lambda:
  297. (os.environ.get(
  298. "APHRODITE_DYNAMIC_ROPE_SCALING",
  299. "0").strip().lower() in ("1", "true")),
  300. # If set, forces FP8 Marlin to be used for FP8 quantization regardless
  301. # of the hardware support for FP8 compute.
  302. "APHRODITE_TEST_FORCE_FP8_MARLIN":
  303. lambda:
  304. (os.environ.get("APHRODITE_TEST_FORCE_FP8_MARLIN", "0").strip().lower() in
  305. ("1", "true")),
  306. # If set, allow running the engine as a separate ray actor,
  307. # which is a deprecated feature soon to be removed.
  308. "APHRODITE_ALLOW_ENGINE_USE_RAY":
  309. lambda:
  310. (os.environ.get("APHRODITE_ALLOW_ENGINE_USE_RAY", "0").strip().lower() in
  311. ("1", "true")),
  312. # a list of plugin names to load, separated by commas.
  313. # if this is not set, it means all plugins will be loaded
  314. # if this is set to an empty string, no plugins will be loaded
  315. "APHRODITE_PLUGINS":
  316. lambda: None if "APHRODITE_PLUGINS" not in os.environ else os.environ[
  317. "APHRODITE_PLUGINS"].split(","),
  318. }
  319. # end-env-vars-definition
  320. def __getattr__(name: str):
  321. # lazy evaluation of environment variables
  322. if name in environment_variables:
  323. return environment_variables[name]()
  324. raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
  325. def __dir__():
  326. return list(environment_variables.keys())