Add MPS support (#1264)
Browse files* add mps support
* linter stuff
* CI fixes
* install packaging for various tests
* Update setup.py
* Revert "install packaging for various tests"
This reverts commit 980e7aa44d667b9cbbfe01b9743edb00d0ac447b.
* Revert "CI fixes"
This reverts commit 4609e3b166ff0ce8e926f39d541aa7ef76592ec4.
---------
Co-authored-by: Wing Lian <[email protected]>
- examples/tiny-llama/lora-mps.yml +65 -0
- setup.py +20 -4
- src/axolotl/monkeypatch/utils.py +2 -2
- src/axolotl/utils/bench.py +10 -1
- src/axolotl/utils/models.py +5 -1
examples/tiny-llama/lora-mps.yml
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
base_model: TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T
|
| 2 |
+
model_type: LlamaForCausalLM
|
| 3 |
+
tokenizer_type: LlamaTokenizer
|
| 4 |
+
is_llama_derived_model: true
|
| 5 |
+
|
| 6 |
+
load_in_8bit: true
|
| 7 |
+
load_in_4bit: false
|
| 8 |
+
strict: false
|
| 9 |
+
|
| 10 |
+
datasets:
|
| 11 |
+
- path: mhenrichsen/alpaca_2k_test
|
| 12 |
+
type: alpaca
|
| 13 |
+
dataset_prepared_path:
|
| 14 |
+
val_set_size: 0
|
| 15 |
+
output_dir: ./lora-out
|
| 16 |
+
|
| 17 |
+
sequence_len: 4096
|
| 18 |
+
sample_packing: true
|
| 19 |
+
pad_to_sequence_len: true
|
| 20 |
+
eval_sample_packing: false
|
| 21 |
+
|
| 22 |
+
adapter: lora
|
| 23 |
+
lora_model_dir:
|
| 24 |
+
lora_r: 32
|
| 25 |
+
lora_alpha: 16
|
| 26 |
+
lora_dropout: 0.05
|
| 27 |
+
lora_target_linear: true
|
| 28 |
+
lora_fan_in_fan_out:
|
| 29 |
+
|
| 30 |
+
wandb_project:
|
| 31 |
+
wandb_entity:
|
| 32 |
+
wandb_watch:
|
| 33 |
+
wandb_name:
|
| 34 |
+
wandb_log_model:
|
| 35 |
+
|
| 36 |
+
gradient_accumulation_steps: 4
|
| 37 |
+
micro_batch_size: 2
|
| 38 |
+
num_epochs: 4
|
| 39 |
+
optimizer: adamw_torch
|
| 40 |
+
lr_scheduler: cosine
|
| 41 |
+
learning_rate: 0.0002
|
| 42 |
+
|
| 43 |
+
train_on_inputs: false
|
| 44 |
+
group_by_length: false
|
| 45 |
+
bf16: auto
|
| 46 |
+
fp16: false
|
| 47 |
+
tf32: true
|
| 48 |
+
|
| 49 |
+
gradient_checkpointing: true
|
| 50 |
+
early_stopping_patience:
|
| 51 |
+
resume_from_checkpoint:
|
| 52 |
+
local_rank:
|
| 53 |
+
logging_steps: 1
|
| 54 |
+
xformers_attention:
|
| 55 |
+
flash_attention: false
|
| 56 |
+
|
| 57 |
+
warmup_steps: 10
|
| 58 |
+
evals_per_epoch: 0
|
| 59 |
+
saves_per_epoch: 1
|
| 60 |
+
debug:
|
| 61 |
+
deepspeed:
|
| 62 |
+
weight_decay: 0.0
|
| 63 |
+
fsdp:
|
| 64 |
+
fsdp_config:
|
| 65 |
+
special_tokens:
|
setup.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
"""setup.py for axolotl"""
|
| 2 |
|
|
|
|
|
|
|
| 3 |
from importlib.metadata import PackageNotFoundError, version
|
| 4 |
|
| 5 |
from setuptools import find_packages, setup
|
|
@@ -26,11 +28,25 @@ def parse_requirements():
|
|
| 26 |
_install_requires.append(line)
|
| 27 |
|
| 28 |
try:
|
| 29 |
-
|
| 30 |
-
_install_requires.append(f"torch=={torch_version}")
|
| 31 |
-
if torch_version.startswith("2.1."):
|
| 32 |
_install_requires.pop(_install_requires.index("xformers==0.0.22"))
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
except PackageNotFoundError:
|
| 35 |
pass
|
| 36 |
|
|
|
|
| 1 |
"""setup.py for axolotl"""
|
| 2 |
|
| 3 |
+
import platform
|
| 4 |
+
import re
|
| 5 |
from importlib.metadata import PackageNotFoundError, version
|
| 6 |
|
| 7 |
from setuptools import find_packages, setup
|
|
|
|
| 28 |
_install_requires.append(line)
|
| 29 |
|
| 30 |
try:
|
| 31 |
+
if "Darwin" in platform.system():
|
|
|
|
|
|
|
| 32 |
_install_requires.pop(_install_requires.index("xformers==0.0.22"))
|
| 33 |
+
else:
|
| 34 |
+
torch_version = version("torch")
|
| 35 |
+
_install_requires.append(f"torch=={torch_version}")
|
| 36 |
+
|
| 37 |
+
version_match = re.match(r"^(\d+)\.(\d+)(?:\.(\d+))?", torch_version)
|
| 38 |
+
if version_match:
|
| 39 |
+
major, minor, patch = version_match.groups()
|
| 40 |
+
major, minor = int(major), int(minor)
|
| 41 |
+
patch = (
|
| 42 |
+
int(patch) if patch is not None else 0
|
| 43 |
+
) # Default patch to 0 if not present
|
| 44 |
+
else:
|
| 45 |
+
raise ValueError("Invalid version format")
|
| 46 |
+
|
| 47 |
+
if (major, minor) >= (2, 1):
|
| 48 |
+
_install_requires.pop(_install_requires.index("xformers==0.0.22"))
|
| 49 |
+
_install_requires.append("xformers>=0.0.23")
|
| 50 |
except PackageNotFoundError:
|
| 51 |
pass
|
| 52 |
|
src/axolotl/monkeypatch/utils.py
CHANGED
|
@@ -186,8 +186,8 @@ def mask_2d_to_4d(
|
|
| 186 |
# Create a binary mask from the original mask where zeros remain zeros and all other values are set to one
|
| 187 |
binary_mask = torch.where(
|
| 188 |
mask != 0,
|
| 189 |
-
torch.tensor(1).to(dtype),
|
| 190 |
-
torch.tensor(0).to(dtype),
|
| 191 |
)
|
| 192 |
|
| 193 |
# Create a block-diagonal mask.
|
|
|
|
| 186 |
# Create a binary mask from the original mask where zeros remain zeros and all other values are set to one
|
| 187 |
binary_mask = torch.where(
|
| 188 |
mask != 0,
|
| 189 |
+
torch.tensor(1, device=mask.device).to(dtype),
|
| 190 |
+
torch.tensor(0, device=mask.device).to(dtype),
|
| 191 |
)
|
| 192 |
|
| 193 |
# Create a block-diagonal mask.
|
src/axolotl/utils/bench.py
CHANGED
|
@@ -47,6 +47,12 @@ def gpu_memory_usage_all(device=0):
|
|
| 47 |
return usage, reserved - usage, max(0, smi - reserved)
|
| 48 |
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
@check_cuda_device(0.0)
|
| 51 |
def gpu_memory_usage_smi(device=0):
|
| 52 |
if isinstance(device, torch.device):
|
|
@@ -63,7 +69,10 @@ def gpu_memory_usage_smi(device=0):
|
|
| 63 |
|
| 64 |
|
| 65 |
def log_gpu_memory_usage(log, msg, device):
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
| 67 |
extras = []
|
| 68 |
if cache > 0:
|
| 69 |
extras.append(f"+{cache:.03f}GB cache")
|
|
|
|
| 47 |
return usage, reserved - usage, max(0, smi - reserved)
|
| 48 |
|
| 49 |
|
| 50 |
+
def mps_memory_usage_all():
|
| 51 |
+
usage = torch.mps.current_allocated_memory() / 1024.0**3
|
| 52 |
+
reserved = torch.mps.driver_allocated_memory() / 1024.0**3
|
| 53 |
+
return usage, reserved - usage, 0
|
| 54 |
+
|
| 55 |
+
|
| 56 |
@check_cuda_device(0.0)
|
| 57 |
def gpu_memory_usage_smi(device=0):
|
| 58 |
if isinstance(device, torch.device):
|
|
|
|
| 69 |
|
| 70 |
|
| 71 |
def log_gpu_memory_usage(log, msg, device):
|
| 72 |
+
if torch.backends.mps.is_available():
|
| 73 |
+
usage, cache, misc = mps_memory_usage_all()
|
| 74 |
+
else:
|
| 75 |
+
usage, cache, misc = gpu_memory_usage_all(device)
|
| 76 |
extras = []
|
| 77 |
if cache > 0:
|
| 78 |
extras.append(f"+{cache:.03f}GB cache")
|
src/axolotl/utils/models.py
CHANGED
|
@@ -409,6 +409,10 @@ def load_model(
|
|
| 409 |
|
| 410 |
model_kwargs["device_map"] = device_map
|
| 411 |
model_kwargs["torch_dtype"] = cfg.torch_dtype
|
|
|
|
|
|
|
|
|
|
|
|
|
| 412 |
# TODO can we put the reference model on it's own gpu? I think we have to move logits around to calculate loss
|
| 413 |
# if cfg.rl:
|
| 414 |
# if torch.cuda.device_count() > 1:
|
|
@@ -651,7 +655,7 @@ def load_model(
|
|
| 651 |
):
|
| 652 |
model.config.eos_token_id = tokenizer.eos_token_id
|
| 653 |
|
| 654 |
-
if hasattr(model, "device") and model.device.type
|
| 655 |
log_gpu_memory_usage(LOG, "after model load", model.device)
|
| 656 |
|
| 657 |
# make sure these are fp32 per Ramesh et al. (2021)
|
|
|
|
| 409 |
|
| 410 |
model_kwargs["device_map"] = device_map
|
| 411 |
model_kwargs["torch_dtype"] = cfg.torch_dtype
|
| 412 |
+
|
| 413 |
+
if torch.backends.mps.is_available():
|
| 414 |
+
model_kwargs["device_map"] = "mps:0"
|
| 415 |
+
|
| 416 |
# TODO can we put the reference model on it's own gpu? I think we have to move logits around to calculate loss
|
| 417 |
# if cfg.rl:
|
| 418 |
# if torch.cuda.device_count() > 1:
|
|
|
|
| 655 |
):
|
| 656 |
model.config.eos_token_id = tokenizer.eos_token_id
|
| 657 |
|
| 658 |
+
if hasattr(model, "device") and model.device.type in ("cuda", "mps"):
|
| 659 |
log_gpu_memory_usage(LOG, "after model load", model.device)
|
| 660 |
|
| 661 |
# make sure these are fp32 per Ramesh et al. (2021)
|