Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
zhougaofeng
internlm2-math-7B
Commits
c6fc146c
"vscode:/vscode.git/clone" did not exist on "66f6123d76f73aa889c8f3fd996c711afd46ea5b"
Commit
c6fc146c
authored
Jun 11, 2024
by
zhougaofeng
Browse files
Upload New File
parent
a4efc0fc
Pipeline
#1171
canceled with stages
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
78 additions
and
0 deletions
+78
-0
src/llmfactory/model/utils/misc.py
src/llmfactory/model/utils/misc.py
+78
-0
No files found.
src/llmfactory/model/utils/misc.py
0 → 100644
View file @
c6fc146c
from
typing
import
TYPE_CHECKING
,
List
import
torch
from
...extras.logging
import
get_logger
from
.quantization
import
QuantizationMethod
if
TYPE_CHECKING
:
from
transformers
import
PretrainedConfig
,
PreTrainedModel
,
PreTrainedTokenizer
logger
=
get_logger
(
__name__
)
def
find_all_linear_modules
(
model
:
"PreTrainedModel"
)
->
List
[
str
]:
r
"""
Finds all available modules to apply lora or galore.
"""
quantization_method
=
getattr
(
model
,
"quantization_method"
,
None
)
if
quantization_method
is
None
:
linear_cls
=
torch
.
nn
.
Linear
elif
quantization_method
==
QuantizationMethod
.
BITS_AND_BYTES
:
import
bitsandbytes
as
bnb
linear_cls
=
bnb
.
nn
.
Linear4bit
if
getattr
(
model
,
"is_loaded_in_4bit"
,
False
)
else
bnb
.
nn
.
Linear8bitLt
else
:
raise
ValueError
(
"Finding linear modules for {} models is not supported."
.
format
(
quantization_method
))
output_layer_names
=
[
"lm_head"
]
if
model
.
config
.
model_type
==
"chatglm"
:
output_layer_names
.
append
(
"output_layer"
)
elif
model
.
config
.
model_type
==
"internlm2"
:
output_layer_names
.
append
(
"output"
)
module_names
=
set
()
for
name
,
module
in
model
.
named_modules
():
if
isinstance
(
module
,
linear_cls
)
and
not
any
(
output_layer
in
name
for
output_layer
in
output_layer_names
):
module_names
.
add
(
name
.
split
(
"."
)[
-
1
])
logger
.
info
(
"Found linear modules: {}"
.
format
(
","
.
join
(
module_names
)))
return
list
(
module_names
)
def
find_expanded_modules
(
model
:
"PreTrainedModel"
,
target_modules
:
List
[
str
],
num_layer_trainable
:
int
)
->
List
[
str
]:
r
"""
Finds the modules in the expanded blocks to apply lora.
"""
num_layers
=
getattr
(
model
.
config
,
"num_hidden_layers"
,
None
)
if
not
num_layers
:
raise
ValueError
(
"Model was not supported."
)
if
num_layers
%
num_layer_trainable
!=
0
:
raise
ValueError
(
"`num_layers` {} should be divisible by `num_layer_trainable` {}."
.
format
(
num_layers
,
num_layer_trainable
)
)
stride
=
num_layers
//
num_layer_trainable
trainable_layer_ids
=
range
(
stride
-
1
,
num_layers
+
stride
-
1
,
stride
)
trainable_layers
=
[
".{:d}."
.
format
(
idx
)
for
idx
in
trainable_layer_ids
]
module_names
=
[]
for
name
,
_
in
model
.
named_modules
():
if
any
(
target_module
in
name
for
target_module
in
target_modules
)
and
any
(
trainable_layer
in
name
for
trainable_layer
in
trainable_layers
):
module_names
.
append
(
name
)
logger
.
info
(
"Apply lora to layers: {}"
.
format
(
","
.
join
(
map
(
str
,
trainable_layer_ids
))))
return
module_names
def
register_autoclass
(
config
:
"PretrainedConfig"
,
model
:
"PreTrainedModel"
,
tokenizer
:
"PreTrainedTokenizer"
):
if
"AutoConfig"
in
getattr
(
config
,
"auto_map"
,
{}):
config
.
__class__
.
register_for_auto_class
()
if
"AutoModelForCausalLM"
in
getattr
(
config
,
"auto_map"
,
{}):
model
.
__class__
.
register_for_auto_class
()
if
"AutoTokenizer"
in
tokenizer
.
init_kwargs
.
get
(
"auto_map"
,
{}):
tokenizer
.
__class__
.
register_for_auto_class
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment