forked from AnswerDotAI/fsdp_qlora
-
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
9655e8a
commit bae3681
Showing
4 changed files
with
186 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
|
||
import argparse | ||
from transformers import AutoConfig | ||
import torch | ||
from transformers.utils import hub, SAFE_WEIGHTS_NAME, SAFE_WEIGHTS_INDEX_NAME | ||
import safetensors | ||
from safetensors.torch import save_file | ||
import os | ||
from pathlib import Path | ||
|
||
def main(): | ||
# Set up the argument parser | ||
parser = argparse.ArgumentParser(description="Receive deepen model's args") | ||
parser.add_argument("--model_name", default='meta-llama/Llama-2-7b-hf', type=str, help="original model path") | ||
parser.add_argument("--output_dir", default=None, type=str, help="deepened model ckpt save path") | ||
parser.add_argument("--expansion_rate", default=0.1, type=float, help="add new trainable % of layers") | ||
|
||
# Parse the arguments | ||
args = parser.parse_args() | ||
|
||
idx = hub.cached_file(args.model_name, SAFE_WEIGHTS_INDEX_NAME) | ||
files, _ = hub.get_checkpoint_shard_files(args.model_name, idx) | ||
|
||
cfg = AutoConfig.from_pretrained(args.model_name) | ||
num_original_layers = cfg.num_hidden_layers | ||
new_layers = num_original_layers + int(num_original_layers * args.expansion_rate) | ||
|
||
split = int(num_original_layers / (new_layers - num_original_layers)) | ||
|
||
if args.output_dir is None: | ||
output_dir = Path(os.environ['HOME'])/'models'/(args.model_name + f'_blk_exp-{num_original_layers}-{new_layers}') | ||
else: | ||
output_dir = Path(args.output_dir)/(args.model_name + f'_blk_exp-{num_original_layers}-{new_layers}') | ||
os.makedirs(output_dir, exist_ok=True) | ||
|
||
for filename in files: | ||
weights = safetensors.torch.load_file(filename) | ||
expanded_weights = {} | ||
for k,v in iter(weights.items()): | ||
if 'layers' in k: | ||
layer_no = int(k.split('layers.')[1].split('.')[0]) | ||
# shift existing layers | ||
new_layer_no = layer_no + layer_no // split | ||
new_k = k.replace(f'layers.{layer_no}', f'layers.{new_layer_no}') | ||
expanded_weights[new_k] = v | ||
# add new layers | ||
if (layer_no+1) % split == 0: | ||
new_layer_no += 1 | ||
new_k = k.replace(f'layers.{layer_no}', f'layers.{new_layer_no}') | ||
if 'down_proj' in k or 'o_proj' in k: | ||
expanded_weights[new_k] = torch.zeros_like(v) | ||
else: | ||
expanded_weights[new_k] = v.clone() | ||
else: | ||
expanded_weights[k] = v | ||
save_file(expanded_weights, output_dir/Path(filename).name) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,44 @@ | ||
import unittest, tempfile | ||
import torch | ||
import torch.nn as nn | ||
import safetensors | ||
from safetensors.torch import save_file | ||
from pathlib import Path | ||
from transformers.utils import hub, SAFE_WEIGHTS_NAME, SAFE_WEIGHTS_INDEX_NAME | ||
from glob import glob | ||
|
||
# python -m unittest tests.test_quantize.TestQuantizer.test_quantizer | ||
class TestBlockExpansion(unittest.TestCase): | ||
|
||
def setUp(self) -> None: | ||
# set seed | ||
self.llama_pro_path = Path("/mnt/vol_b/models/meta-llama/Llama-2-7b-hf_blk_exp-32-35") | ||
self.filenames = glob(str(self.llama_pro_path/"*.safetensors")) | ||
num_original_layers, num_expanded_layers = self.llama_pro_path.name.split("blk_exp-")[1].split("-") | ||
self.num_original_layers, self.num_expanded_layers = int(num_original_layers), int(num_expanded_layers) | ||
self.split = int(self.num_original_layers / (self.num_expanded_layers - self.num_original_layers)) | ||
|
||
|
||
def tearDown(self) -> None: | ||
return super().tearDown() | ||
|
||
def test_expanded_weights(self): | ||
|
||
total_new_layers = self.num_expanded_layers - self.num_original_layers | ||
new_layer_ids = [self.split + (self.split + 1)*n for n in range(total_new_layers)] | ||
|
||
verify_weights = {} | ||
for filename in self.filenames: | ||
weights = safetensors.torch.load_file(str(filename)) | ||
for k,v in iter(weights.items()): | ||
if any(((f"layers.{i}" in k) or (f"layers.{i-1}" in k) for i in new_layer_ids)): | ||
verify_weights[k] = v | ||
|
||
for k,v in verify_weights.items(): | ||
if any(((f"layers.{i}" in k) for i in new_layer_ids)): | ||
if 'down_proj' in k or 'o_proj' in k: | ||
assert torch.equal(v, torch.zeros_like(v)) | ||
else: | ||
lid = int(k.split("layers.")[1].split(".")[0]) | ||
assert torch.equal(verify_weights[k.replace(f"layers.{lid}", f"layers.{lid-1}")], v) | ||
|
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