Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --num-threads option #520

Merged
merged 7 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion mergekit/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
# SPDX-License-Identifier: BUSL-1.1

import functools
import logging
import typing
from typing import Any, Callable, Optional, Union

import click
import torch
import transformers
from click.core import Context, Parameter
from pydantic import BaseModel

Expand All @@ -26,9 +29,19 @@ class MergeOptions(BaseModel, frozen=True):
lazy_unpickle: bool = False
write_model_card: bool = True
safe_serialization: bool = True
verbose: bool = False
quiet: bool = False
read_to_gpu: bool = False
multi_gpu: bool = False
num_threads: Optional[int] = None

def apply_global_options(self):
logging.basicConfig(level=logging.INFO if self.verbose else logging.WARNING)
if self.random_seed is not None:
transformers.trainer_utils.set_seed(self.random_seed)
if self.num_threads is not None:
torch.set_num_threads(self.num_threads)
torch.set_num_interop_threads(self.num_threads)


OPTION_HELP = {
Expand All @@ -48,6 +61,8 @@ class MergeOptions(BaseModel, frozen=True):
"quiet": "Suppress progress bars and other non-essential output",
"read_to_gpu": "Read model weights directly to GPU",
"multi_gpu": "Use multi-gpu parallel graph execution engine",
"num_threads": "Number of threads to use for parallel CPU operations",
"verbose": "Enable verbose logging",
}


Expand Down Expand Up @@ -88,10 +103,15 @@ def wrapper(*args, **kwargs):
arg_str = f"--{arg_name}/--no-{arg_name}"
else:
arg_str = f"--{arg_name}"
param_decls = [arg_str]
if field_name == "verbose":
param_decls = ["--verbose/--no-verbose", "-v"]
if field_name == "num_threads":
param_decls = ["--num-threads", "-j"]

help_str = OPTION_HELP.get(field_name, None)
wrapper = click.option(
arg_str,
*param_decls,
type=field_type,
default=info.default,
help=help_str,
Expand Down
9 changes: 1 addition & 8 deletions mergekit/scripts/extract_lora.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,6 @@
multiple=True,
help="Include modules matching the specified regex",
)
@click.option(
"--verbose",
"-v",
is_flag=True,
help="Verbose logging",
)
@click.option(
"--sv-epsilon",
type=float,
Expand All @@ -109,11 +103,10 @@ def main(
modules_to_save: List[str],
exclude_regexes: List[str],
include_regexes: List[str],
verbose: bool,
sv_epsilon: float,
merge_options: MergeOptions,
):
logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO)
merge_options.apply_global_options()

if not modules_to_save:
modules_to_save = []
Expand Down
2 changes: 2 additions & 0 deletions mergekit/scripts/merge_raw_pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ def main(
Uses similar configuration syntax to `mergekit-yaml`, minus the
`slices` sections. Each input model should be the path on disk to a
pytorch pickle file or safetensors file."""
merge_options.apply_global_options()

with open(config_path, "r", encoding="utf-8") as file:
config_source = file.read()

Expand Down
4 changes: 2 additions & 2 deletions mergekit/scripts/moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ def main(
load_in_4bit: bool,
load_in_8bit: bool,
device: str,
merge_options: MergeOptions,
verbose: bool,
i_understand_this_is_not_useful_without_training: bool,
merge_options: MergeOptions,
):
"""Create a Mixture of Experts model by combining the pretrained weights of multiple models."""
logging.basicConfig(level=logging.INFO if verbose else logging.WARNING)
merge_options.apply_global_options()

if merge_options.cuda:
logging.warning(
Expand Down
6 changes: 1 addition & 5 deletions mergekit/scripts/multimerge.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ def execute(self, **kwargs):
required=True,
help="Directory to store intermediate merges",
)
@click.option(
"--verbose", "-v", type=bool, default=False, is_flag=True, help="Verbose logging"
)
@click.option(
"--lazy/--no-lazy",
default=True,
Expand All @@ -88,7 +85,6 @@ def main(
config_file: str,
intermediate_dir: str,
out_path: Optional[str],
verbose: bool,
lazy: bool,
merge_options: MergeOptions,
):
Expand All @@ -102,7 +98,7 @@ def main(
Any merge configuration with a `name` field will be saved to this
directory. If an unnamed merge configuration is present, it will be
saved to `out_path` (which is required in this case)."""
logging.basicConfig(level=logging.INFO if verbose else logging.WARNING)
merge_options.apply_global_options()
os.makedirs(intermediate_dir, exist_ok=True)

with open(config_file, "r", encoding="utf-8") as file:
Expand Down
7 changes: 1 addition & 6 deletions mergekit/scripts/run_yaml.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Copyright (C) 2025 Arcee AI
# SPDX-License-Identifier: BUSL-1.1

import logging

import click
import yaml
Expand All @@ -14,17 +13,13 @@
@click.command("mergekit-yaml")
@click.argument("config_file")
@click.argument("out_path")
@click.option(
"--verbose", "-v", type=bool, default=False, is_flag=True, help="Verbose logging"
)
@add_merge_options
def main(
merge_options: MergeOptions,
config_file: str,
out_path: str,
verbose: bool,
):
logging.basicConfig(level=logging.INFO if verbose else logging.WARNING)
merge_options.apply_global_options()

with open(config_file, "r", encoding="utf-8") as file:
config_source = file.read()
Expand Down
Loading