Skip to content

Commit

Permalink
enable bfloat16 for cpu (#1034)
Browse files Browse the repository at this point in the history
if there's no cuda. disable custom kernels

# What does this PR do?

<!--
Congratulations! You've made it this far! You're not quite done yet
though.

Once merged, your PR is going to appear in the release notes with the
title you set, so make sure it's a great title that fully reflects the
extent of your awesome contribution.

Then, please replace this with a description of the change and which
issue is fixed (if applicable). Please also include relevant motivation
and context. List any dependencies (if any) that are required for this
change.

Once you're done, someone will review your PR shortly (see the section
"Who can review?" below to tag some potential reviewers). They may
suggest changes to make the code even better. If no one reviewed your PR
after a week has passed, don't hesitate to post a new comment
@-mentioning the same persons---sometimes notifications get lost.
-->

<!-- Remove if not applicable -->

Fixes # (issue)


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the
other checks if that's the case).
- [ ] Did you read the [contributor
guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#start-contributing-pull-requests),
      Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the
[forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes?
Here are the
[documentation
guidelines](https://github.com/huggingface/transformers/tree/main/docs),
and
[here are tips on formatting
docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?


## Who can review?

Anyone in the community is free to review the PR once the tests have
passed. Feel free to tag
members/contributors who may be interested in your PR.

<!-- Your PR will be replied to more quickly if you can figure out the
right person to tag with @


@OlivierDehaene OR @Narsil

 -->

Signed-off-by: Wang, Yi A <[email protected]>
  • Loading branch information
sywangyi authored Sep 19, 2023
1 parent c8a01d7 commit eeaa22a
Show file tree
Hide file tree
Showing 15 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion server/text_generation_server/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def get_model(
)
elif model_type == "mpt":
return MPTSharded(
model_id, revision, quantize=quantize, trust_remote_code=trust_remote_code
model_id, revision, quantize=quantize, dtype=dtype, trust_remote_code=trust_remote_code
)

elif model_type == "gpt_neox":
Expand Down
2 changes: 1 addition & 1 deletion server/text_generation_server/models/bloom.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(
dtype = torch.float16 if dtype is None else dtype
else:
device = torch.device("cpu")
dtype = torch.float32
dtype = torch.float32 if dtype is None else dtype

tokenizer = AutoTokenizer.from_pretrained(
model_id,
Expand Down
2 changes: 1 addition & 1 deletion server/text_generation_server/models/causal_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ def __init__(
raise ValueError("quantization is not available on CPU")

device = torch.device("cpu")
dtype = torch.float32
dtype = torch.float32 if dtype is None else dtype

tokenizer = AutoTokenizer.from_pretrained(
model_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
)

CUSTOM_KERNELS_ENABLED = False
if not os.environ.get("DISABLE_CUSTOM_KERNELS", "False") == "True":
if torch.cuda.is_available() and not os.environ.get("DISABLE_CUSTOM_KERNELS", "False") == "True":
try:
from custom_kernels import fused_bloom_attention_cuda

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@


CUSTOM_KERNELS_ENABLED = False
if not os.environ.get("DISABLE_CUSTOM_KERNELS", "False") == "True":
if torch.cuda.is_available() and not os.environ.get("DISABLE_CUSTOM_KERNELS", "False") == "True":
try:
from custom_kernels import fused_attention_cuda

Expand Down
2 changes: 1 addition & 1 deletion server/text_generation_server/models/galactica.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def __init__(
dtype = torch.float16 if dtype is None else dtype
else:
device = torch.device("cpu")
dtype = torch.float32
dtype = torch.float32 if dtype is None else dtype

tokenizer = AutoTokenizer.from_pretrained(
model_id,
Expand Down
2 changes: 1 addition & 1 deletion server/text_generation_server/models/gpt_neox.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(
dtype = torch.float16 if dtype is None else dtype
else:
device = torch.device("cpu")
dtype = torch.float32
dtype = torch.float32 if dtype is None else dtype

tokenizer = AutoTokenizer.from_pretrained(
model_id,
Expand Down
2 changes: 1 addition & 1 deletion server/text_generation_server/models/idefics.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(
dtype = torch.bfloat16 if dtype is None else dtype
else:
device = torch.device("cpu")
dtype = torch.float32
dtype = torch.float32 if dtype is None else dtype
self.device, self.dtype = device, dtype

config = IdeficsConfig.from_pretrained(
Expand Down
2 changes: 1 addition & 1 deletion server/text_generation_server/models/idefics_causal_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ def __init__(
raise ValueError("quantization is not available on CPU")

device = torch.device("cpu")
dtype = torch.float32
dtype = torch.float32 if dtype is None else dtype

tokenizer = AutoTokenizer.from_pretrained(
model_id,
Expand Down
6 changes: 4 additions & 2 deletions server/text_generation_server/models/mpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ def __init__(
model_id: str,
revision: Optional[str] = None,
quantize: Optional[str] = None,
dtype: Optional[torch.dtype] = None,
trust_remote_code: bool = False,
):
self.process_group, rank, world_size = initialize_torch_distributed()
if torch.cuda.is_available():
device = torch.device(f"cuda:{rank}")
dtype = torch.float16
dtype = torch.float16 if dtype is None else dtype
else:
raise NotImplementedError("MPTSharded is only available on GPU")
device = torch.device("cpu")
dtype = torch.float32 if dtype is None else dtype

tokenizer = AutoTokenizer.from_pretrained(
model_id,
Expand Down
2 changes: 1 addition & 1 deletion server/text_generation_server/models/opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(
dtype = torch.float16 if dtype is None else dtype
else:
device = torch.device("cpu")
dtype = torch.float32
dtype = torch.float32 if dtype is None else dtype

tokenizer = AutoTokenizer.from_pretrained(
model_id,
Expand Down
2 changes: 1 addition & 1 deletion server/text_generation_server/models/rw.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(
raise ValueError("quantization is not available on CPU")

device = torch.device("cpu")
dtype = torch.float32
dtype = torch.float32 if dtype is None else dtype

tokenizer = AutoTokenizer.from_pretrained(
model_id,
Expand Down
2 changes: 1 addition & 1 deletion server/text_generation_server/models/santacoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(
raise ValueError("quantization is not available on CPU")

device = torch.device("cpu")
dtype = torch.float32
dtype = torch.float32 if dtype is None else dtype

tokenizer = AutoTokenizer.from_pretrained(
model_id,
Expand Down
2 changes: 1 addition & 1 deletion server/text_generation_server/models/seq2seq_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ def __init__(
raise ValueError("quantization is not available on CPU")

device = torch.device("cpu")
dtype = torch.float32
dtype = torch.float32 if dtype is None else dtype

model = AutoModelForSeq2SeqLM.from_pretrained(
model_id,
Expand Down
2 changes: 1 addition & 1 deletion server/text_generation_server/models/t5.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(
dtype = torch.float16 if dtype is None else dtype
else:
device = torch.device("cpu")
dtype = torch.float32
dtype = torch.float32 if dtype is None else dtype

config = AutoConfig.from_pretrained(
model_id,
Expand Down

0 comments on commit eeaa22a

Please sign in to comment.