forked from pytorch/torchtune
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97b994d
commit 08fae5e
Showing
8 changed files
with
374 additions
and
21 deletions.
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
76 changes: 76 additions & 0 deletions
76
recipes/configs/llama2/7B_full_single_device_low_memory.yaml
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,76 @@ | ||
# Config for single device full finetuning in full_finetune_single_device.py | ||
# using a Llama2 7B model | ||
# | ||
# This config assumes that you've run the following command before launching | ||
# this run: | ||
# tune download --repo-id meta-llama/Llama-2-7b \ | ||
# --hf-token <HF_TOKEN> \ | ||
# --output-dir /tmp/llama2 | ||
# | ||
# To launch on a single device, run the following command from root: | ||
# tune --nnodes 1 --nproc_per_node 1 full_finetune_single_device \ | ||
# --config llama2/7B_full_single_device_low_memory \ | ||
# | ||
# You can add specific overrides through the command line. For example | ||
# to override the checkpointer directory while launching training | ||
# you can run: | ||
# tune --nnodes 1 --nproc_per_node 1 full_finetune_single_device \ | ||
# --config llama2/7B_full_single_device_low_memory \ | ||
# checkpointer.checkpoint_dir=<YOUR_CHECKPOINT_DIR> | ||
# | ||
# This config works only for training on single device. | ||
|
||
|
||
# Tokenizer | ||
tokenizer: | ||
_component_: torchtune.models.llama2.llama2_tokenizer | ||
path: /tmp/llama2/tokenizer.model | ||
|
||
# Dataset | ||
dataset: | ||
_component_: torchtune.datasets.alpaca_dataset | ||
train_on_input: True | ||
seed: null | ||
shuffle: True | ||
|
||
# Model Arguments | ||
model: | ||
_component_: torchtune.models.llama2.llama2_7b | ||
|
||
checkpointer: | ||
_component_: torchtune.utils.FullModelMetaCheckpointer | ||
checkpoint_dir: /tmp/llama2 | ||
checkpoint_files: [consolidated.00.pth] | ||
recipe_checkpoint: null | ||
output_dir: /tmp/llama2 | ||
model_type: LLAMA2 | ||
resume_from_checkpoint: False | ||
|
||
# Fine-tuning arguments | ||
batch_size: 2 | ||
epochs: 1 | ||
optimizer: | ||
_component_: bitsandbytes.optim.PagedAdamW | ||
lr: 2e-5 | ||
optimizer_in_bwd: True | ||
loss: | ||
_component_: torch.nn.CrossEntropyLoss | ||
max_steps_per_epoch: null | ||
gradient_accumulation_steps: 1 | ||
|
||
|
||
# Training environment | ||
device: cuda | ||
|
||
# Memory management | ||
enable_activation_checkpointing: True | ||
|
||
# Reduced precision | ||
dtype: bf16 | ||
|
||
# Logging | ||
metric_logger: | ||
_component_: torchtune.utils.metric_logging.DiskLogger | ||
log_dir: ${output_dir} | ||
output_dir: /tmp/alpaca-llama2-finetune | ||
log_every_n_steps: null |
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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import pytest | ||
import torch | ||
from torchtune.utils import create_optim_in_bwd_wrapper, register_optim_in_bwd_hooks | ||
|
||
|
||
def _run_dummy_step(model, wrapper): | ||
with torch.no_grad(): | ||
for p in model.parameters(): | ||
p.grad = torch.rand_like(p) | ||
for v in wrapper.optim_map.values(): | ||
v.step() | ||
v.zero_grad() | ||
|
||
|
||
def _validate_dicts(d1, d2): | ||
if len(d1) != len(d2): | ||
return False | ||
for k, v in d1.items(): | ||
if k not in d2: | ||
return False | ||
if isinstance(v, dict): | ||
return _validate_dicts(v, d2[k]) | ||
else: | ||
if isinstance(v, torch.Tensor): | ||
if not torch.allclose(v, d2[k]): | ||
return False | ||
elif v != d2[k]: | ||
return False | ||
return True | ||
|
||
|
||
@pytest.fixture | ||
def model(): | ||
return torch.nn.Linear(10, 1) | ||
|
||
|
||
@pytest.fixture | ||
def optim_dict(model): | ||
return {p: torch.optim.AdamW([p], lr=0.01) for p in model.parameters()} | ||
|
||
|
||
@pytest.fixture | ||
def wrapper(model, optim_dict): | ||
return create_optim_in_bwd_wrapper(model, optim_dict) | ||
|
||
|
||
class TestOptimInBackward: | ||
def test_state_dict_save_load(self, model, wrapper): | ||
# Run a dummy step to create optimizer states | ||
_run_dummy_step(model, wrapper) | ||
|
||
sd = wrapper.state_dict() | ||
new_optim_dict = create_optim_in_bwd_wrapper( | ||
model, {p: torch.optim.AdamW([p], lr=0.01) for p in model.parameters()} | ||
) | ||
assert not _validate_dicts(sd, new_optim_dict.state_dict()) | ||
new_optim_dict.load_state_dict(sd) | ||
assert _validate_dicts(sd, new_optim_dict.state_dict()) | ||
|
||
def test_missing_unexpected_param_load_raises(self, model, wrapper): | ||
# Run a dummy step to create optimizer states | ||
_run_dummy_step(model, wrapper) | ||
sd = wrapper.state_dict() | ||
new_optim_dict = create_optim_in_bwd_wrapper( | ||
model, {p: torch.optim.AdamW([p], lr=0.01) for p in model.parameters()} | ||
) | ||
with pytest.raises(RuntimeError, match="Expected to load optimizer state"): | ||
sd.pop(next(iter(sd.keys()))) | ||
new_optim_dict.load_state_dict(sd) | ||
|
||
sd = wrapper.state_dict() | ||
sd["new_key"] = 1234 | ||
with pytest.raises(RuntimeError, match="unexpected param"): | ||
new_optim_dict.load_state_dict(sd) | ||
|
||
|
||
class TestRegisterOptimHooks: | ||
def test_register_optim_in_bwd_hooks(self, model, optim_dict): | ||
register_optim_in_bwd_hooks(model, optim_dict) | ||
# Ensure backward() updates the parameters and sets grads to None | ||
orig_params = [p.clone().detach() for p in model.parameters()] | ||
model(torch.rand(2, 10)).sum().backward() | ||
for p, orig_p in zip(model.parameters(), orig_params): | ||
assert not p.grad | ||
assert not torch.allclose(p, orig_p) |
Oops, something went wrong.