Elffuss BraidHub

kikocis/SpatialLM public

main · 3.2 KiBRawPermalink
1# Copyright 2025 LMSYS and the LlamaFactory team.
2# Copyright 2023 Rohan Taori, Ishaan Gulrajani, Tianyi Zhang, Yann Dubois, Xuechen Li
3#
4# This code is inspired by the LMSYS's FastChat library.
5# https://github.com/lm-sys/FastChat/blob/v0.2.30/fastchat/train/train.py
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18
19import math
20from enum import Enum
21from typing import TYPE_CHECKING
22
23from ..framework import logging
24
25if TYPE_CHECKING:
26 from transformers import PretrainedConfig
27
28 from ..hparams import ModelArguments
29
30
31class RopeScaling(str, Enum):
32 LINEAR = "linear"
33 DYNAMIC = "dynamic"
34 YARN = "yarn"
35 LLAMA3 = "llama3"
36
37
38logger = logging.get_logger(__name__)
39
40
41def configure_rope(config: "PretrainedConfig", model_args: "ModelArguments") -> None:
42 if model_args.rope_scaling is None:
43 return
44
45 if not hasattr(config, "rope_scaling"):
46 logger.warning_rank0("Current model does not support RoPE scaling.")
47 return
48
49 if hasattr(config, "max_position_embeddings"):
50 old_max_length = getattr(config, "max_position_embeddings", None)
51 else:
52 logger.warning_rank0("Cannot find the max position embeddings in the config.")
53 return
54
55 if model_args.model_max_length is not None: # training
56 if model_args.model_max_length <= old_max_length:
57 logger.warning_rank0(
58 "Input length is smaller than max length. Disabling rope scaling."
59 )
60 return
61
62 if model_args.rope_scaling == RopeScaling.DYNAMIC:
63 logger.warning_rank0(
64 "Dynamic NTK scaling may not work well with fine-tuning. "
65 "See: https://github.com/huggingface/transformers/pull/24653"
66 )
67
68 rope_factor = float(math.ceil(model_args.model_max_length / old_max_length))
69 else: # inference
70 rope_factor = 2.0
71
72 rope_kwargs = {
73 "rope_type": getattr(
74 model_args.rope_scaling, "value", model_args.rope_scaling
75 ), # handle enum
76 "factor": rope_factor,
77 }
78 setattr(config, "max_position_embeddings", old_max_length * rope_factor)
79 logger.info_rank0(
80 f"Enlarge max model length from {old_max_length} to {old_max_length * rope_factor}."
81 )
82
83 if model_args.rope_scaling in [RopeScaling.DYNAMIC, RopeScaling.YARN]:
84 rope_kwargs["original_max_position_embeddings"] = old_max_length
85 elif model_args.rope_scaling == RopeScaling.LLAMA3:
86 rope_kwargs["original_max_position_embeddings"] = old_max_length
87 rope_kwargs["low_freq_factor"] = 1.0
88 rope_kwargs["high_freq_factor"] = 4.0
89
90 setattr(config, "rope_scaling", rope_kwargs)
91 logger.info_rank0(
92 f"Using {rope_kwargs['rope_type']} scaling strategy and setting scaling factor to {rope_kwargs['factor']}."
93 )