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

Fixes for Mac M3 #2830

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ dataset/**
models
data
config.toml
sd-scripts

# sd-scripts - Commenting out for now as changes are needed here as well.
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "sd-scripts"]
path = sd-scripts
url = https://github.com/kohya-ss/sd-scripts.git
path = sd-scripts
url = https://github.com/JoeyOverby/sd-scripts_for_M3.git
9 changes: 7 additions & 2 deletions kohya_gui/blip2_caption_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@


def load_model():
# Set the device to GPU if available, otherwise use CPU
device = "cuda" if torch.cuda.is_available() else "cpu"
# Set the device to GPU if available, MPS if available, otherwise use CPU
if torch.cuda.is_available():
device = torch.device("cuda")
elif torch.backends.mps.is_available():
device = torch.device("mps")
else:
device = torch.device("cpu")

# Initialize the BLIP2 processor
processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
Expand Down
57 changes: 50 additions & 7 deletions kohya_gui/common_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,34 @@
import math
import shutil
import toml
import threading
import queue
import time

# Queue for main thread tasks
task_queue = queue.Queue()

# Initialize the root window
root = Tk()
root.withdraw() # Hide the window, since we're only using Tkinter for task scheduling


# Process tasks on the main thread
def process_tasks_from_queue():
while not task_queue.empty():
task = task_queue.get()
task()

# Continue checking the queue every 100ms
root.after(100, process_tasks_from_queue)

# Function to ensure tasks run on the main thread
def run_on_main_thread(task):
if threading.current_thread() == threading.main_thread():
task()
else:
task_queue.put(task) # Send task to queue


# Set up logging
log = setup_logging()
Expand Down Expand Up @@ -136,8 +164,23 @@ def check_if_model_exist(
elif save_model_as in ["ckpt", "safetensors"]:
ckpt_file = os.path.join(output_dir, output_name + "." + save_model_as)
if os.path.isfile(ckpt_file):
msg = f"A model with the same file name {ckpt_file} already exists. Do you want to overwrite it?"
if not ynbox(msg, "Overwrite Existing Model?"):

# Check if running on macOS (darwin)
if sys.platform == "darwin":
# Get the current time in epoch seconds
current_time = int(time.time())

# Construct the backup file name with .BAK-<time_in_epoch_seconds>
backup_file = ckpt_file + f".BAK-{current_time}"

# Rename the file to the backup file
shutil.move(ckpt_file, backup_file)

log.info(f"Renamed existing model file to {backup_file}")
return False # Returning False because we're continuing, not aborting the process
else:
msg = f"A model with the same file name {ckpt_file} already exists. Do you want to overwrite it?"
if not ynbox(msg, "Overwrite Existing Model?"):
log.info("Aborting training due to existing model with same name...")
return True
else:
Expand Down Expand Up @@ -491,7 +534,7 @@ def get_file_path(
) # Decompose file path for dialog setup

# Initialize a hidden Tkinter window for the file dialog
root = Tk()
run_on_main_thread(lambda: Tk())
root.wm_attributes("-topmost", 1) # Ensure the dialog is topmost
root.withdraw() # Hide the root window to show only the dialog

Expand Down Expand Up @@ -550,7 +593,7 @@ def get_any_file_path(file_path: str = "") -> str:
initial_dir, initial_file = get_dir_and_file(file_path)

# Initialize a hidden Tkinter window for the file dialog
root = Tk()
run_on_main_thread(lambda: Tk())
root.wm_attributes("-topmost", 1)
root.withdraw()

Expand Down Expand Up @@ -606,7 +649,7 @@ def get_folder_path(folder_path: str = "") -> str:
if any(var in os.environ for var in ENV_EXCLUSION) or sys.platform == "darwin":
return folder_path or ""

root = Tk()
run_on_main_thread(lambda: Tk())
root.withdraw()
root.wm_attributes("-topmost", 1)
selected_folder = filedialog.askdirectory(initialdir=folder_path or ".")
Expand All @@ -633,7 +676,7 @@ def get_saveasfile_path(
initial_dir, initial_file = get_dir_and_file(file_path)

# Initialize a hidden Tkinter window to act as the parent for the file dialog, ensuring it appears on top
root = Tk()
run_on_main_thread(lambda: Tk())
root.wm_attributes("-topmost", 1)
root.withdraw()
save_file_path = filedialog.asksaveasfile(
Expand Down Expand Up @@ -706,7 +749,7 @@ def get_saveasfilename_path(
initial_dir, initial_file = get_dir_and_file(file_path)

# Initialize a hidden Tkinter window to act as the parent for the file dialog, ensuring it appears on top
root = Tk()
run_on_main_thread(lambda: Tk())
root.wm_attributes("-topmost", 1)
root.withdraw()
# Open the file dialog and capture the selected file path
Expand Down
1 change: 1 addition & 0 deletions kohya_gui/extract_lora_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ def change_sdxl(sdxl):
choices=[
"cpu",
"cuda",
"mps"
],
value="cuda",
interactive=True,
Expand Down
1 change: 1 addition & 0 deletions kohya_gui/extract_lycoris_locon_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ def list_save_to(path):
choices=[
"cpu",
"cuda",
"mps"
],
value="cuda",
interactive=True,
Expand Down
Loading