-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathenvs.py
More file actions
83 lines (69 loc) · 3.43 KB
/
envs.py
File metadata and controls
83 lines (69 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Copyright (c) 2025 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Note: the design of this module is inspired by vLLM's envs.py
# For detailed usage and configuration guide, see: docs/environments.md
import os
from typing import TYPE_CHECKING, Any, Callable, Optional
if TYPE_CHECKING:
AR_LOG_LEVEL: str = "INFO"
AR_USE_MODELSCOPE: bool = "False"
environment_variables: dict[str, Callable[[], Any]] = {
# this is used for configuring the default logging level
"AR_LOG_LEVEL": lambda: os.getenv("AR_LOG_LEVEL", "INFO").upper(),
"AR_ENABLE_COMPILE_PACKING": lambda: os.getenv("AR_ENABLE_COMPILE_PACKING", "0").lower() in ("1", "true", "yes"),
"AR_USE_MODELSCOPE": lambda: os.getenv("AR_USE_MODELSCOPE", "False").lower() in ["1", "true"],
"AR_WORK_SPACE": lambda: os.getenv("AR_WORK_SPACE", "ar_work_space").lower(),
"AR_ENABLE_UNIFY_MOE_INPUT_SCALE": lambda: os.getenv("AR_ENABLE_UNIFY_MOE_INPUT_SCALE", "False").lower()
in ["1", "true"],
"AR_OMP_NUM_THREADS": lambda: os.getenv("AR_OMP_NUM_THREADS", None),
"AR_DISABLE_OFFLOAD": lambda: os.getenv("AR_DISABLE_OFFLOAD", "0").lower() in ("1", "true", "yes"),
"AR_DISABLE_DATASET_SUBPROCESS": lambda: os.getenv("AR_DISABLE_DATASET_SUBPROCESS", "0").lower() in ("1", "true"),
"AR_DISABLE_COPY_MTP_WEIGHTS": lambda: os.getenv("AR_DISABLE_COPY_MTP_WEIGHTS", "0").lower()
in ("1", "true", "yes"),
"AR_CALIB_FORCE_CUDA": lambda: os.getenv("AR_CALIB_FORCE_CUDA", "0").lower() in ("1", "true", "yes"),
}
def __getattr__(name: str):
# lazy evaluation of environment variables
if name in environment_variables:
return environment_variables[name]()
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
def __dir__():
return list(environment_variables.keys())
def is_set(name: str):
"""Check if an environment variable is explicitly set."""
if name in environment_variables:
return name in os.environ
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
def set_config(**kwargs):
"""
Set configuration values for environment variables.
Args:
**kwargs: Keyword arguments where keys are environment variable names
and values are the desired values to set.
Example:
set_config(AR_LOG_LEVEL="DEBUG", AR_USE_MODELSCOPE=True)
"""
for key, value in kwargs.items():
if key in environment_variables:
# Convert value to appropriate string format
if key == "AR_USE_MODELSCOPE":
# Handle boolean values for AR_USE_MODELSCOPE
str_value = "true" if value in [True, "True", "true", "1", 1] else "false"
else:
# For other variables, convert to string
str_value = str(value)
# Set the environment variable
os.environ[key] = str_value
else:
raise AttributeError(f"module {__name__!r} has no attribute {key!r}")