-
Notifications
You must be signed in to change notification settings - Fork 7
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
[REVIEW] Fix Padding Related Bugs: Crossfit
#66
Merged
VibhuJawa
merged 20 commits into
rapidsai:main
from
VibhuJawa:vjawa/fix-translation-dynamic-batch-bug
Aug 5, 2024
Merged
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
130d465
Add crossfit bits
VibhuJawa 67ed3a4
Add padding fixes
VibhuJawa b20b4e9
Fix test
VibhuJawa 91dd34d
Add docstrings
VibhuJawa 1b02e4b
fix torch import
VibhuJawa d63761e
fix torch import
VibhuJawa 2c19aa2
fix padding to only pad the last dim
VibhuJawa 3109e1f
fix padding tests
VibhuJawa 7d0fb6f
Add test for left/right
VibhuJawa 97b3aea
Skip test for cf_loader
VibhuJawa 62aed47
Fix bugs in clipping
VibhuJawa bd73727
Fix bugs in clipping
VibhuJawa bbdc4f2
Add early stopping to HF memory estimation
VibhuJawa 4a2b544
Fix copy-right year
VibhuJawa a553b11
Add copyright year
VibhuJawa a6f4ac2
Address last of Ryan's reviews
VibhuJawa de6fb38
Skip loading model if its allready fitted
VibhuJawa 23a8aeb
Use self.load_cfg instead of AutoConfig.from_pretrained
VibhuJawa 15e27bb
Use self.load_cfg instead of AutoConfig.from_pretrained
VibhuJawa 987836f
Fix memory_curve_utils and skip loading cfg/tokenizer here
VibhuJawa 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# Copyright 2024 NVIDIA CORPORATION | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import gc | ||
|
||
import joblib | ||
import numpy as np | ||
import torch | ||
from sklearn.linear_model import LinearRegression | ||
from tqdm import tqdm | ||
from transformers import AutoConfig, AutoTokenizer, PreTrainedModel | ||
|
||
from crossfit.utils.model_adapter import adapt_model_input | ||
|
||
|
||
def fit_memory_estimate_curve( | ||
model: PreTrainedModel, | ||
path_or_name: str, | ||
start_batch_size: int = 1, | ||
end_batch_size: int = 2048, | ||
batch_size_increment: int = 256, | ||
start_seq_len: int = 1, | ||
end_seq_len: int = 2048, | ||
seq_len_increment: int = 64, | ||
mem_model_path: str = None, | ||
) -> LinearRegression: | ||
print(f"Fitting memory estimate curve for model: {path_or_name}") | ||
|
||
device = next(model.parameters()).device | ||
X: list[list[int]] = [] | ||
y: list[float] = [] | ||
|
||
max_seq = min(AutoTokenizer.from_pretrained(path_or_name).model_max_length, end_seq_len) | ||
if max_seq > 1e5: | ||
max_seq = min(AutoConfig.from_pretrained(path_or_name).max_position_embeddings, end_seq_len) | ||
|
||
batch_size_pbar = tqdm( | ||
range(start_batch_size, end_batch_size + 1, batch_size_increment), desc="Batch size" | ||
) | ||
for batch_size in batch_size_pbar: | ||
seq_len_pbar = tqdm( | ||
range(start_seq_len, max_seq + 1, seq_len_increment), | ||
desc="Sequence length", | ||
leave=False, | ||
) | ||
for seq_len in seq_len_pbar: | ||
torch.cuda.reset_peak_memory_stats() | ||
|
||
batch = { | ||
"input_ids": torch.randint(1, 501, (batch_size, seq_len)).to(device=device), | ||
"attention_mask": torch.ones((batch_size, seq_len)).to(device=device), | ||
} | ||
|
||
try: | ||
_ = adapt_model_input(model, batch) | ||
memory_used = torch.cuda.max_memory_allocated() / (1024**2) # Convert to MB | ||
X.append([batch_size, seq_len, seq_len**2]) | ||
y.append(memory_used) | ||
|
||
except RuntimeError as e: | ||
if "out of memory" in str(e) or "out_of_memory" in str(e): | ||
# Early stopping for this batch size | ||
seq_len_pbar.close() | ||
break | ||
else: | ||
raise e | ||
finally: | ||
del batch | ||
if "outputs" in vars(): | ||
del outputs | ||
gc.collect() | ||
torch.cuda.empty_cache() | ||
|
||
# Check if we've hit the memory limit for all sequence lengths | ||
if seq_len == start_seq_len: | ||
batch_size_pbar.close() | ||
break | ||
|
||
mem_model = LinearRegression().fit(np.array(X), np.array(y)) | ||
joblib.dump(mem_model, mem_model_path) | ||
|
||
return mem_model |
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
Oops, something went wrong.
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.
Similar to the other issue, I think every time
AutoTokenizer
orAutoConfig
is used in this file it should be using the corresponding methods of the model. Now I'm getting this error with llama guard: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.
Resolved by: 987836f