Spaces:
Running
on
A100
Running
on
A100
| # Copyright (c) 2025 NVIDIA CORPORATION. | |
| # Licensed under the MIT license. | |
| # Adapted from https://github.com/NVlabs/VILA/tree/main under the Apache 2.0 license. | |
| # LICENSE is in incl_licenses directory. | |
| # Copyright 2024 NVIDIA CORPORATION & AFFILIATES | |
| # | |
| # 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. | |
| # | |
| # SPDX-License-Identifier: Apache-2.0 | |
| # This file is modified from https://github.com/haotian-liu/LLaVA/ | |
| """ | |
| Usage: | |
| python3 -m llava.model.make_delta --base ~/model_weights/llama-7b --target ~/model_weights/llava-7b --delta ~/model_weights/llava-7b-delta --hub-repo-id liuhaotian/llava-7b-delta | |
| """ | |
| import argparse | |
| import torch | |
| from tqdm import tqdm | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| from llava.model.utils import auto_upgrade | |
| def make_delta(base_model_path, target_model_path, delta_path, hub_repo_id): | |
| print("Loading base model") | |
| base = AutoModelForCausalLM.from_pretrained(base_model_path, torch_dtype=torch.float16, low_cpu_mem_usage=True) | |
| print("Loading target model") | |
| auto_upgrade(target_model_path) | |
| target = AutoModelForCausalLM.from_pretrained(target_model_path, torch_dtype=torch.float16, low_cpu_mem_usage=True) | |
| print("Calculating delta") | |
| for name, param in tqdm(target.state_dict().items(), desc="Calculating delta"): | |
| if name not in base.state_dict(): | |
| assert name in [ | |
| "model.mm_projector.weight", | |
| "model.mm_projector.bias", | |
| ], f"{name} not in base model" | |
| continue | |
| if param.data.shape == base.state_dict()[name].shape: | |
| param.data -= base.state_dict()[name] | |
| else: | |
| assert name in [ | |
| "model.embed_tokens.weight", | |
| "lm_head.weight", | |
| ], f"{name} dimension mismatch: {param.data.shape} vs {base.state_dict()[name].shape}" | |
| bparam = base.state_dict()[name] | |
| param.data[: bparam.shape[0], : bparam.shape[1]] -= bparam | |
| print("Saving delta") | |
| if hub_repo_id: | |
| kwargs = {"push_to_hub": True, "repo_id": hub_repo_id} | |
| else: | |
| kwargs = {} | |
| target.save_pretrained(delta_path, **kwargs) | |
| target_tokenizer = AutoTokenizer.from_pretrained(target_model_path) | |
| target_tokenizer.save_pretrained(delta_path, **kwargs) | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--base-model-path", type=str, required=True) | |
| parser.add_argument("--target-model-path", type=str, required=True) | |
| parser.add_argument("--delta-path", type=str, required=True) | |
| parser.add_argument("--hub-repo-id", type=str, default=None) | |
| args = parser.parse_args() | |
| make_delta(args.base_model_path, args.target_model_path, args.delta_path, args.hub_repo_id) | |