def make_diff(
    path_raw: str, path_tuned: str, path_diff: str, device="cuda",  # "cuda" or "cpu"
):
    """Make the weight diff.

    This function is given to present full transparency of how the weight diff was created.

    Run:
        python weight_diff.py make_diff --path_raw <your_path_raw> --path_tuned <your_path_tuned> --path_diff <your_path_diff>
    """
    print("load tuned model...")
    model_tuned: transformers.PreTrainedModel = transformers.AutoModelForCausalLM.from_pretrained(
        path_tuned,
        device_map={"": torch.device(device)},
        torch_dtype=torch.float32,
        low_cpu_mem_usage=True,
    )
    print("load raw model...")
    model_raw: transformers.PreTrainedModel = transformers.AutoModelForCausalLM.from_pretrained(
        path_raw,
        device_map={"": torch.device(device)},
        torch_dtype=torch.float32,
        low_cpu_mem_usage=True,
    )

    tokenizer_tuned: transformers.PreTrainedTokenizer = transformers.AutoTokenizer.from_pretrained(
        path_tuned
    )

    tokenizer_raw: transformers.PreTrainedTokenizer = transformers.AutoTokenizer.from_pretrained(
        path_raw
    )
    if tokenizer_raw.pad_token is None and tokenizer_tuned.pad_token is not None:
        print("add pad token")
        smart_tokenizer_and_embedding_resize(
            special_tokens_dict=dict(pad_token="<PAD>"),
            model=model_raw,
            tokenizer=tokenizer_raw,
        )
    state_dict_tuned = model_tuned.state_dict()
    state_dict_raw = model_raw.state_dict()
    for key in tqdm.tqdm(state_dict_tuned, desc="make diff"):
        print(key)
        state_dict_tuned[key].add_(-state_dict_raw[key])
    print("saving diff weights...")
    model_tuned.save_pretrained(path_diff)
    kwargs = {"push_to_hub": False, "repo_id": ""}
    tokenizer_tuned.save_pretrained(path_diff, **kwargs)
Make the weight diff.

    This function is given to present full transparency of how the weight diff was created.

    Run:
        python weight_diff.py make_diff --path_raw <your_path_raw> --path_tuned <your_path_tuned> --path_diff <your_path_diff>
    
UltraLM/weight_diff.py