File size: 2,599 Bytes
a164e13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "id": "cmjOVVtJdiPZ"
      },
      "outputs": [],
      "source": [
        "!git clone https://github.com/cg123/mergekit.git\n",
        "%cd mergekit\n",
        "%pip install -e ."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "id": "84cRJT6_ecbw"
      },
      "outputs": [],
      "source": [
        "OUTPUT_PATH = \"./merged\"  # folder to store the result in\n",
        "LORA_MERGE_CACHE = \"/tmp\"  # change if you want to keep these for some reason\n",
        "CONFIG_YML = \"./examples/gradient-slerp.yml\"  # merge configuration file\n",
        "COPY_TOKENIZER = True  # you want a tokenizer? yeah, that's what i thought\n",
        "LAZY_UNPICKLE = False  # experimental low-memory model loader\n",
        "LOW_CPU_MEMORY = False  # enable if you somehow have more VRAM than RAM+swap"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "6nw26xQLkBax"
      },
      "outputs": [],
      "source": [
        "# actually do merge\n",
        "import torch\n",
        "import yaml\n",
        "\n",
        "from mergekit.config import MergeConfiguration\n",
        "from mergekit.merge import MergeOptions, run_merge\n",
        "\n",
        "with open(CONFIG_YML, \"r\", encoding=\"utf-8\") as fp:\n",
        "    merge_config = MergeConfiguration.model_validate(yaml.safe_load(fp))\n",
        "\n",
        "run_merge(\n",
        "    merge_config,\n",
        "    out_path=OUTPUT_PATH,\n",
        "    options=MergeOptions(\n",
        "        lora_merge_cache=LORA_MERGE_CACHE,\n",
        "        cuda=torch.cuda.is_available(),\n",
        "        copy_tokenizer=COPY_TOKENIZER,\n",
        "        lazy_unpickle=LAZY_UNPICKLE,\n",
        "        low_cpu_memory=LOW_CPU_MEMORY,\n",
        "    ),\n",
        ")\n",
        "print(\"Done!\")"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "gpuType": "T4",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.13"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}