arcee-ai / mergekit

Tools for merging pretrained large language models.
GNU Lesser General Public License v3.0
4.8k stars 437 forks source link

How to use multi GPUs #420

Open liudan193 opened 1 month ago

liudan193 commented 1 month ago

I have 2 4090 and I want to merge 8 7B models. But I get out of memory.

And only one GPU is used. So, how to use 2 4090 simultaneously.

Or there is other method to solve this?

liudan193 commented 1 month ago

I try to setting lazy_unpickle=True, out_shard_size=parse_kmb("1B"). This does not solve my question. I got error as:

Traceback (most recent call last):
  File "/mnt/data/Projects/LLM-Merging/llm_merging/main.py", line 49, in <module>
    merge_method.merge()
  File "/mnt/data/Projects/LLM-Merging/llm_merging/merging/DellaLlama31.py", line 61, in merge
    run_merge(
  File "/mnt/data/Projects/LLM-Merging/mergekit/merge.py", line 96, in run_merge
    for _task, value in exec.run(quiet=options.quiet):
  File "/mnt/data/Projects/LLM-Merging/mergekit/graph.py", line 197, in run
    res = task.execute(**arguments)
  File "/mnt/data/Projects/LLM-Merging/mergekit/merge_methods/generalized_task_arithmetic.py", line 160, in execute
    deltas = torch.stack([tv["delta"] for tv in tvs], dim=0)
torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 5.87 GiB. GPU 0 has a total capacity of 23.65 GiB of which 4.61 GiB is free. Including non-PyTorch memory, this process has 19.03 GiB memory in use. Of the allocated memory 6.85 GiB is allocated by PyTorch, and 11.74 GiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True to avoid fragmentation.  See documentation for Memory Management  (https://pytorch.org/docs/stable/notes/cuda.html#environment-variables)

My code is:

COPY_TOKENIZER = True  # you want a tokenizer? yeah, that's what i thought
LAZY_UNPICKLE = True  # experimental low-memory model loader
LOW_CPU_MEMORY = False  # enable if you somehow have more VRAM than RAM+swap
run_merge(
  merge_config,
  out_path=OUTPUT_PATH,
  options=MergeOptions(
    lora_merge_cache=LORA_MERGE_CACHE,
    cuda=torch.cuda.is_available(),
    copy_tokenizer=COPY_TOKENIZER,
    lazy_unpickle=LAZY_UNPICKLE,
    low_cpu_memory=LOW_CPU_MEMORY,
    out_shard_size=parse_kmb("1B"),
  ),
)