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
OpenDAS
opencompass
Commits
e3d4901b
Unverified
Commit
e3d4901b
authored
Oct 27, 2023
by
Fengzhe Zhou
Committed by
GitHub
Oct 27, 2023
Browse files
[Feat] Add _set_model_kwargs_torch_dtype for HF model (#507)
* add _set_model_kwargs_torch_dtype for hf models * add logger
parent
6405cd2d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
4 deletions
+25
-4
opencompass/models/huggingface.py
opencompass/models/huggingface.py
+17
-2
run.py
run.py
+8
-2
No files found.
opencompass/models/huggingface.py
View file @
e3d4901b
...
@@ -131,13 +131,28 @@ class HuggingFace(BaseModel):
...
@@ -131,13 +131,28 @@ class HuggingFace(BaseModel):
self
.
tokenizer
.
eos_token
=
'</s>'
self
.
tokenizer
.
eos_token
=
'</s>'
self
.
tokenizer
.
pad_token_id
=
0
self
.
tokenizer
.
pad_token_id
=
0
def
_set_model_kwargs_torch_dtype
(
self
,
model_kwargs
):
if
'torch_dtype'
not
in
model_kwargs
:
torch_dtype
=
torch
.
float16
else
:
torch_dtype
=
{
'torch.float16'
:
torch
.
float16
,
'torch.bfloat16'
:
torch
.
bfloat16
,
'torch.float'
:
torch
.
float
,
'auto'
:
'auto'
,
'None'
:
None
}.
get
(
model_kwargs
[
'torch_dtype'
])
self
.
logger
.
debug
(
f
'HF using torch_dtype:
{
torch_dtype
}
'
)
if
torch_dtype
is
not
None
:
model_kwargs
[
'torch_dtype'
]
=
torch_dtype
def
_load_model
(
self
,
def
_load_model
(
self
,
path
:
str
,
path
:
str
,
model_kwargs
:
dict
,
model_kwargs
:
dict
,
peft_path
:
Optional
[
str
]
=
None
):
peft_path
:
Optional
[
str
]
=
None
):
from
transformers
import
AutoModel
,
AutoModelForCausalLM
from
transformers
import
AutoModel
,
AutoModelForCausalLM
model_kwargs
.
setdefault
(
'torch_dtype'
,
torch
.
float16
)
self
.
_set_
model_kwargs
_torch_dtype
(
model_kwargs
)
try
:
try
:
self
.
model
=
AutoModelForCausalLM
.
from_pretrained
(
self
.
model
=
AutoModelForCausalLM
.
from_pretrained
(
path
,
**
model_kwargs
)
path
,
**
model_kwargs
)
...
@@ -409,7 +424,7 @@ class HuggingFaceCausalLM(HuggingFace):
...
@@ -409,7 +424,7 @@ class HuggingFaceCausalLM(HuggingFace):
peft_path
:
Optional
[
str
]
=
None
):
peft_path
:
Optional
[
str
]
=
None
):
from
transformers
import
AutoModelForCausalLM
from
transformers
import
AutoModelForCausalLM
model_kwargs
.
setdefault
(
'torch_dtype'
,
torch
.
float16
)
self
.
_set_
model_kwargs
_torch_dtype
(
model_kwargs
)
self
.
model
=
AutoModelForCausalLM
.
from_pretrained
(
path
,
**
model_kwargs
)
self
.
model
=
AutoModelForCausalLM
.
from_pretrained
(
path
,
**
model_kwargs
)
if
peft_path
is
not
None
:
if
peft_path
is
not
None
:
from
peft
import
PeftModel
from
peft
import
PeftModel
...
...
run.py
View file @
e3d4901b
...
@@ -175,8 +175,14 @@ def parse_hf_args(hf_parser):
...
@@ -175,8 +175,14 @@ def parse_hf_args(hf_parser):
hf_parser
.
add_argument
(
'--hf-path'
,
type
=
str
)
hf_parser
.
add_argument
(
'--hf-path'
,
type
=
str
)
hf_parser
.
add_argument
(
'--peft-path'
,
type
=
str
)
hf_parser
.
add_argument
(
'--peft-path'
,
type
=
str
)
hf_parser
.
add_argument
(
'--tokenizer-path'
,
type
=
str
)
hf_parser
.
add_argument
(
'--tokenizer-path'
,
type
=
str
)
hf_parser
.
add_argument
(
'--model-kwargs'
,
nargs
=
'+'
,
action
=
DictAction
)
hf_parser
.
add_argument
(
'--model-kwargs'
,
hf_parser
.
add_argument
(
'--tokenizer-kwargs'
,
nargs
=
'+'
,
action
=
DictAction
)
nargs
=
'+'
,
action
=
DictAction
,
default
=
{})
hf_parser
.
add_argument
(
'--tokenizer-kwargs'
,
nargs
=
'+'
,
action
=
DictAction
,
default
=
{})
hf_parser
.
add_argument
(
'--max-out-len'
,
type
=
int
)
hf_parser
.
add_argument
(
'--max-out-len'
,
type
=
int
)
hf_parser
.
add_argument
(
'--max-seq-len'
,
type
=
int
)
hf_parser
.
add_argument
(
'--max-seq-len'
,
type
=
int
)
hf_parser
.
add_argument
(
'--no-batch-padding'
,
hf_parser
.
add_argument
(
'--no-batch-padding'
,
...
...
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