-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generalizable multi gpu to run e.g. Llama 65b #238
Open
thejaminator
wants to merge
45
commits into
main
Choose a base branch
from
generalizable-multi-gpu
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
79a0be0
add llama map
thejaminator d692b7f
add typechecking if
thejaminator 91d98d2
allocate the device properly
thejaminator aa5ee9e
print to debug
thejaminator 7cbfb9a
change to device config
thejaminator cfb3200
more logs
thejaminator 8cb4dec
print the value
thejaminator 9e8e321
fix not returning configs
thejaminator 36ea2e6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a59d0a2
test the effect of not returning the past key values
thejaminator 5fc1b5f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c2ee397
add kwargs
thejaminator 6a8de4d
Merge remote-tracking branch 'origin/main' into hardcoded-llama65-map
thejaminator 6abddbf
add device map 0
thejaminator 4953544
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a2ceeb9
fix 8bit mem
thejaminator 5409b01
make pyright happy
thejaminator 0db53b9
implement multi gpu
thejaminator 50edafe
Merge remote-tracking branch 'origin/main' into generalizable-multi-gpu
thejaminator 5ee3c3a
add cli
thejaminator 8d6279c
redirect only later
thejaminator 47529b6
add logs and remove llama
thejaminator 4a49aa0
fix keyword
thejaminator 91a06b6
try out lm head
thejaminator d74c9b6
shift it to 0.8 instead
thejaminator e6eb9c1
try hardcoded map
thejaminator 06b1a11
decrease further for gpu 1
thejaminator 64919b3
fix import
thejaminator fe331bc
remove syntax
thejaminator 0ed7f31
try comparing to hardcoding
thejaminator 69bbf64
Revert "try comparing to hardcoding"
thejaminator 051e2fd
Merge remote-tracking branch 'origin/main' into generalizable-multi-gpu
thejaminator 8c6386c
add comment on future improvement
thejaminator 0182a64
print
thejaminator df1c0ff
load in 8bit correctly
thejaminator 6d9e9ea
add comment
thejaminator 02602cb
try passing float16?
thejaminator d3a8f29
prevent mem issues?
thejaminator bf827ea
add logs
thejaminator 301e6e2
try only adding load_in_8bit if we really need to
thejaminator 6b6bb6f
catch max mem
thejaminator a5b3d5f
Revert "try only adding load_in_8bit if we really need to"
thejaminator 99db2a0
try out means of memory
thejaminator 55b18ab
remove debug print
thejaminator fa52400
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from typing import Optional | ||
|
||
import torch | ||
import transformers | ||
from transformers import ( | ||
|
@@ -20,44 +22,59 @@ | |
_AUTOREGRESSIVE_SUFFIXES = ["ConditionalGeneration"] + _DECODER_ONLY_SUFFIXES | ||
|
||
|
||
def determine_dtypes( | ||
model_str: str, | ||
is_cpu: bool, | ||
load_in_8bit: bool, | ||
) -> torch.dtype | str: | ||
model_cfg = AutoConfig.from_pretrained(model_str) | ||
|
||
# When the torch_dtype is None, this generally means the model is fp32, because | ||
# the config was probably created before the `torch_dtype` field was added. | ||
fp32_weights = model_cfg.torch_dtype in (None, torch.float32) | ||
|
||
# Required by `bitsandbytes` to load in 8-bit. | ||
if load_in_8bit: | ||
# Sanity check: we probably shouldn't be loading in 8-bit if the checkpoint | ||
# is in fp32. `bitsandbytes` only supports mixed fp16/int8 inference, and | ||
# we can't guarantee that there won't be overflow if we downcast to fp16. | ||
if fp32_weights: | ||
raise ValueError("Cannot load in 8-bit if weights are fp32") | ||
|
||
torch_dtype = torch.float16 | ||
|
||
# CPUs generally don't support anything other than fp32. | ||
elif is_cpu: | ||
torch_dtype = torch.float32 | ||
|
||
# If the model is fp32 but bf16 is available, convert to bf16. | ||
# Usually models with fp32 weights were actually trained in bf16, and | ||
# converting them doesn't hurt performance. | ||
elif fp32_weights and torch.cuda.is_bf16_supported(): | ||
torch_dtype = torch.bfloat16 | ||
print("Weights seem to be fp32, but bf16 is available. Loading in bf16.") | ||
else: | ||
torch_dtype = "auto" | ||
return torch_dtype | ||
|
||
|
||
def instantiate_model( | ||
model_str: str, | ||
device: str | torch.device = "cpu", | ||
load_in_8bit: bool, | ||
is_cpu: bool, | ||
torch_dtype: Optional[torch.dtype] = None, | ||
**kwargs, | ||
) -> PreTrainedModel: | ||
"""Instantiate a model string with the appropriate `Auto` class.""" | ||
device = torch.device(device) | ||
kwargs["device_map"] = {"": device} | ||
|
||
with prevent_name_conflicts(): | ||
model_cfg = AutoConfig.from_pretrained(model_str) | ||
|
||
# When the torch_dtype is None, this generally means the model is fp32, because | ||
# the config was probably created before the `torch_dtype` field was added. | ||
fp32_weights = model_cfg.torch_dtype in (None, torch.float32) | ||
|
||
# Required by `bitsandbytes` to load in 8-bit. | ||
if kwargs.get("load_in_8bit"): | ||
# Sanity check: we probably shouldn't be loading in 8-bit if the checkpoint | ||
# is in fp32. `bitsandbytes` only supports mixed fp16/int8 inference, and | ||
# we can't guarantee that there won't be overflow if we downcast to fp16. | ||
if fp32_weights: | ||
raise ValueError("Cannot load in 8-bit if weights are fp32") | ||
|
||
kwargs["torch_dtype"] = torch.float16 | ||
|
||
# CPUs generally don't support anything other than fp32. | ||
elif device.type == "cpu": | ||
kwargs["torch_dtype"] = torch.float32 | ||
|
||
# If the model is fp32 but bf16 is available, convert to bf16. | ||
# Usually models with fp32 weights were actually trained in bf16, and | ||
# converting them doesn't hurt performance. | ||
elif fp32_weights and torch.cuda.is_bf16_supported(): | ||
kwargs["torch_dtype"] = torch.bfloat16 | ||
print("Weights seem to be fp32, but bf16 is available. Loading in bf16.") | ||
else: | ||
kwargs["torch_dtype"] = "auto" | ||
# If a torch_dtype was not specified, try to infer it. | ||
kwargs["torch_dtype"] = torch_dtype or determine_dtypes( | ||
model_str=model_str, is_cpu=is_cpu, load_in_8bit=load_in_8bit | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. made this change because it previously was setting kwargs even if it was getting passed by the caller of instantiate_model, which confused me |
||
# Add load_in_8bit to kwargs | ||
kwargs["load_in_8bit"] = load_in_8bit | ||
|
||
archs = model_cfg.architectures | ||
if not isinstance(archs, list): | ||
|
@@ -70,7 +87,6 @@ def instantiate_model( | |
if arch_str.endswith(suffix): | ||
model_cls = getattr(transformers, arch_str) | ||
return model_cls.from_pretrained(model_str, **kwargs) | ||
|
||
return AutoModel.from_pretrained(model_str, **kwargs) | ||
|
||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kwargs["device_map"] = {"": device} will be passed by the caller instead (because for e.g. when instantiating an empty model, we can't pass a device map. otherwise it'll really load the weights and won't be an empty model anymore