Apply HF transformers patches from sglang init (#23103)
This commit is contained in:
@@ -24,6 +24,11 @@ if _sys.platform == "darwin":
|
|||||||
pass
|
pass
|
||||||
del _sys
|
del _sys
|
||||||
|
|
||||||
|
from sglang.srt.utils.hf_transformers_patches import apply_all as _apply_hf_patches
|
||||||
|
|
||||||
|
_apply_hf_patches()
|
||||||
|
del _apply_hf_patches
|
||||||
|
|
||||||
# Frontend Language APIs
|
# Frontend Language APIs
|
||||||
from sglang.global_config import global_config
|
from sglang.global_config import global_config
|
||||||
from sglang.lang.api import (
|
from sglang.lang.api import (
|
||||||
|
|||||||
@@ -14,17 +14,16 @@
|
|||||||
"""Hugging Face Transformers utilities.
|
"""Hugging Face Transformers utilities.
|
||||||
|
|
||||||
This package provides HF Transformers helpers, split into submodules
|
This package provides HF Transformers helpers, split into submodules
|
||||||
(common, compat, config, tokenizer, processor, mistral_utils).
|
(common, config, tokenizer, processor, mistral_utils). Compatibility
|
||||||
|
monkey-patches live in the sibling ``sglang.srt.utils.hf_transformers_patches``
|
||||||
|
module and are applied at sglang import time.
|
||||||
All public symbols are re-exported here for convenience. The old import
|
All public symbols are re-exported here for convenience. The old import
|
||||||
path ``sglang.srt.utils.hf_transformers_utils`` is preserved by a
|
path ``sglang.srt.utils.hf_transformers_utils`` is preserved by a
|
||||||
separate shim module.
|
separate shim module.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from .compat import apply_all as _apply_compat
|
from ..hf_transformers_patches import normalize_rope_scaling_compat
|
||||||
|
from .common import (
|
||||||
_apply_compat()
|
|
||||||
|
|
||||||
from .common import ( # noqa: E402
|
|
||||||
CONTEXT_LENGTH_KEYS,
|
CONTEXT_LENGTH_KEYS,
|
||||||
AutoConfig,
|
AutoConfig,
|
||||||
attach_additional_stop_token_ids,
|
attach_additional_stop_token_ids,
|
||||||
@@ -37,10 +36,9 @@ from .common import ( # noqa: E402
|
|||||||
get_sparse_attention_config,
|
get_sparse_attention_config,
|
||||||
get_tokenizer_from_processor,
|
get_tokenizer_from_processor,
|
||||||
)
|
)
|
||||||
from .compat import normalize_rope_scaling_compat # noqa: E402
|
from .config import get_config
|
||||||
from .config import get_config # noqa: E402
|
from .processor import get_processor
|
||||||
from .processor import get_processor # noqa: E402
|
from .tokenizer import (
|
||||||
from .tokenizer import ( # noqa: E402
|
|
||||||
_fix_added_tokens_encoding,
|
_fix_added_tokens_encoding,
|
||||||
_fix_v5_add_bos_eos_token,
|
_fix_v5_add_bos_eos_token,
|
||||||
get_tokenizer,
|
get_tokenizer,
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ from sglang.srt.configs.deepseek_ocr import DeepseekVLV2Config
|
|||||||
from sglang.srt.configs.internvl import InternVLChatConfig
|
from sglang.srt.configs.internvl import InternVLChatConfig
|
||||||
from sglang.srt.utils import get_bool_env_var, logger, lru_cache_frozenset
|
from sglang.srt.utils import get_bool_env_var, logger, lru_cache_frozenset
|
||||||
|
|
||||||
from .compat import normalize_rope_scaling_compat
|
from ..hf_transformers_patches import normalize_rope_scaling_compat
|
||||||
|
|
||||||
if get_bool_env_var("SGLANG_USE_MODELSCOPE"):
|
if get_bool_env_var("SGLANG_USE_MODELSCOPE"):
|
||||||
from modelscope import AutoConfig, GenerationConfig
|
from modelscope import AutoConfig, GenerationConfig
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ from sglang.srt.connector import create_remote_connector
|
|||||||
from sglang.srt.utils import is_remote_url, logger, lru_cache_frozenset
|
from sglang.srt.utils import is_remote_url, logger, lru_cache_frozenset
|
||||||
from sglang.srt.utils.runai_utils import ObjectStorageModel, is_runai_obj_uri
|
from sglang.srt.utils.runai_utils import ObjectStorageModel, is_runai_obj_uri
|
||||||
|
|
||||||
|
from ..hf_transformers_patches import _ensure_gguf_version
|
||||||
from .common import (
|
from .common import (
|
||||||
_CONFIG_REGISTRY,
|
_CONFIG_REGISTRY,
|
||||||
AutoConfig,
|
AutoConfig,
|
||||||
@@ -34,7 +35,6 @@ from .common import (
|
|||||||
check_gguf_file,
|
check_gguf_file,
|
||||||
get_hf_text_config,
|
get_hf_text_config,
|
||||||
)
|
)
|
||||||
from .compat import _ensure_gguf_version
|
|
||||||
from .mistral_utils import is_mistral_model, load_mistral_config
|
from .mistral_utils import is_mistral_model, load_mistral_config
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -30,12 +30,12 @@ from sglang.srt.utils import is_remote_url, logger
|
|||||||
from sglang.srt.utils.patch_tokenizer import patch_tokenizer
|
from sglang.srt.utils.patch_tokenizer import patch_tokenizer
|
||||||
from sglang.srt.utils.runai_utils import ObjectStorageModel, is_runai_obj_uri
|
from sglang.srt.utils.runai_utils import ObjectStorageModel, is_runai_obj_uri
|
||||||
|
|
||||||
|
from ..hf_transformers_patches import _ensure_gguf_version
|
||||||
from .common import (
|
from .common import (
|
||||||
_resolve_local_or_cached_file,
|
_resolve_local_or_cached_file,
|
||||||
attach_additional_stop_token_ids,
|
attach_additional_stop_token_ids,
|
||||||
check_gguf_file,
|
check_gguf_file,
|
||||||
)
|
)
|
||||||
from .compat import _ensure_gguf_version, patch_is_base_mistral_in_ci
|
|
||||||
from .mistral_utils import (
|
from .mistral_utils import (
|
||||||
_MISTRAL_TOKENIZER_REDIRECTS,
|
_MISTRAL_TOKENIZER_REDIRECTS,
|
||||||
patch_mistral_common_tokenizer,
|
patch_mistral_common_tokenizer,
|
||||||
@@ -462,7 +462,6 @@ def get_tokenizer(
|
|||||||
kwargs["use_fast"] = True
|
kwargs["use_fast"] = True
|
||||||
|
|
||||||
tokenizer_name = _resolve_tokenizer_name(tokenizer_name, kwargs)
|
tokenizer_name = _resolve_tokenizer_name(tokenizer_name, kwargs)
|
||||||
patch_is_base_mistral_in_ci()
|
|
||||||
|
|
||||||
common_kwargs = dict(
|
common_kwargs = dict(
|
||||||
trust_remote_code=trust_remote_code,
|
trust_remote_code=trust_remote_code,
|
||||||
|
|||||||
+17
-11
@@ -11,21 +11,15 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
"""Compatibility patches for transformers v5.x.
|
"""Monkey-patches on transformers internals.
|
||||||
|
|
||||||
This module applies monkey-patches to work around breaking changes in
|
Mix of backward-compat shims (re-add symbols removed in v5), workarounds
|
||||||
transformers v5. Each patch is tagged with the upstream issue it works
|
for transformers v5 bugs, fixes for remote-model-code (trust_remote_code)
|
||||||
around so it can be removed once the upstream fix lands.
|
that hasn't been updated for v5 yet, and CI-only patches (e.g. neutralize
|
||||||
|
HF API calls to avoid rate limits).
|
||||||
|
|
||||||
Import this module early (before any ``from_pretrained`` call) to activate
|
Import this module early (before any ``from_pretrained`` call) to activate
|
||||||
all patches. It is safe to import multiple times -- patches are idempotent.
|
all patches. It is safe to import multiple times -- patches are idempotent.
|
||||||
|
|
||||||
Patches fall into two categories:
|
|
||||||
|
|
||||||
1. **Transformers bugs / regressions** -- issues in transformers itself.
|
|
||||||
2. **Remote-model-code compat** -- remote model code (trust_remote_code)
|
|
||||||
that hasn't been updated for v5 yet. These should be removed once
|
|
||||||
the model authors publish fixes.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import inspect
|
import inspect
|
||||||
@@ -44,10 +38,19 @@ def apply_all():
|
|||||||
"""Apply all transformers compatibility patches (idempotent).
|
"""Apply all transformers compatibility patches (idempotent).
|
||||||
|
|
||||||
Call this once at import time. It is safe to call multiple times.
|
Call this once at import time. It is safe to call multiple times.
|
||||||
|
|
||||||
|
No-op when the ``transformers`` package is not installed -- frontend-only
|
||||||
|
sglang users should not be forced to install transformers just to import
|
||||||
|
the top-level ``sglang`` package.
|
||||||
"""
|
"""
|
||||||
global _applied
|
global _applied
|
||||||
if _applied:
|
if _applied:
|
||||||
return
|
return
|
||||||
|
try:
|
||||||
|
import transformers # noqa: F401
|
||||||
|
except ImportError:
|
||||||
|
_applied = True
|
||||||
|
return
|
||||||
_applied = True
|
_applied = True
|
||||||
|
|
||||||
# v5.4 patches
|
# v5.4 patches
|
||||||
@@ -62,6 +65,9 @@ def apply_all():
|
|||||||
_ensure_clean_up_tokenization_compat()
|
_ensure_clean_up_tokenization_compat()
|
||||||
_ensure_is_torch_fx_available_compat()
|
_ensure_is_torch_fx_available_compat()
|
||||||
|
|
||||||
|
# CI-only: neutralize HF API calls inside tokenizer from_pretrained
|
||||||
|
patch_is_base_mistral_in_ci()
|
||||||
|
|
||||||
logger.debug("transformers compatibility patches applied")
|
logger.debug("transformers compatibility patches applied")
|
||||||
|
|
||||||
|
|
||||||
@@ -20,8 +20,8 @@ from sglang.srt.utils.hf_transformers.common import (
|
|||||||
get_hf_text_config,
|
get_hf_text_config,
|
||||||
get_rope_config,
|
get_rope_config,
|
||||||
)
|
)
|
||||||
from sglang.srt.utils.hf_transformers.compat import normalize_rope_scaling_compat
|
|
||||||
from sglang.srt.utils.hf_transformers.tokenizer import _fix_special_tokens_pattern
|
from sglang.srt.utils.hf_transformers.tokenizer import _fix_special_tokens_pattern
|
||||||
|
from sglang.srt.utils.hf_transformers_patches import normalize_rope_scaling_compat
|
||||||
from sglang.test.ci.ci_register import register_cpu_ci
|
from sglang.test.ci.ci_register import register_cpu_ci
|
||||||
|
|
||||||
register_cpu_ci(est_time=5, suite="stage-a-test-cpu")
|
register_cpu_ci(est_time=5, suite="stage-a-test-cpu")
|
||||||
|
|||||||
Reference in New Issue
Block a user