Spaces:
Build error
Build error
Julian Bilcke commited on
Commit ·
c8589f9
1
Parent(s): adc5756
various fixes regarding session recovery
Browse filesFiles changed (4)
31 unmodified lines32333435363711 unmodified lines4950515253545531 unmodified linesPRECOMPUTED_LATENTS_DIR_NAME,)# Decord is causing us some issues!# Let's try to increase file descriptor limits to avoid this error:11 unmodified linesexcept Exception as e:logger.warning(f"Could not check or update file descriptor limits: {e}")logger = get_logger(__name__)# TODO(aryan): This needs a refactor with separation of concerns.31 unmodified lines3233343536373811 unmodified lines50515253545531 unmodified linesPRECOMPUTED_LATENTS_DIR_NAME,)logger = get_logger(__name__)# Decord is causing us some issues!# Let's try to increase file descriptor limits to avoid this error:11 unmodified linesexcept Exception as e:logger.warning(f"Could not check or update file descriptor limits: {e}")# TODO(aryan): This needs a refactor with separation of concerns.
636 unmodified lines637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785636 unmodified linesreturn Falsedef recover_interrupted_training(self) -> Dict[str, Any]:"""Attempt to recover interrupted trainingReturns:Dict with recovery status and UI updates"""status = self.get_status()ui_updates = {}# Check for any checkpoints, even if status doesn't indicate trainingcheckpoints = list(OUTPUT_PATH.glob("checkpoint-*"))has_checkpoints = len(checkpoints) > 0# If status indicates training but process isn't running, or if we have checkpoints# and no active training process, try to recoverif (status.get('status') in ['training', 'paused'] and not self.is_training_running()) or \(has_checkpoints and not self.is_training_running()):logger.info("Detected interrupted training session or existing checkpoints, attempting to recover...")# Get the latest checkpointlast_session = self.load_session()if not last_session:logger.warning("No session data found for recovery, but will check for checkpoints")# Try to create a default session based on UI state if we have checkpointsif has_checkpoints:ui_state = self.load_ui_state()# Create a default session using UI state valueslast_session = {"params": {"model_type": MODEL_TYPES.get(ui_state.get("model_type", list(MODEL_TYPES.keys())[0])),"lora_rank": ui_state.get("lora_rank", "128"),"lora_alpha": ui_state.get("lora_alpha", "128"),"num_epochs": ui_state.get("num_epochs", 70),"batch_size": ui_state.get("batch_size", 1),"learning_rate": ui_state.get("learning_rate", 3e-5),"save_iterations": ui_state.get("save_iterations", 500),"preset_name": ui_state.get("training_preset", list(TRAINING_PRESETS.keys())[0]),"repo_id": "" # Default empty repo ID}}logger.info("Created default session from UI state for recovery")else:# Set buttons for no active trainingui_updates = {"start_btn": {"interactive": True, "variant": "primary", "value": "Start Training"},"stop_btn": {"interactive": False, "variant": "secondary", "value": "Stop at Last Checkpoint"},"pause_resume_btn": {"interactive": False, "variant": "secondary", "visible": False}}return {"status": "idle", "message": "No training in progress", "ui_updates": ui_updates}# Find the latest checkpoint if we have checkpointslatest_checkpoint = Nonecheckpoint_step = 0if has_checkpoints:latest_checkpoint = max(checkpoints, key=os.path.getmtime)checkpoint_step = int(latest_checkpoint.name.split("-")[1])logger.info(f"Found checkpoint at step {checkpoint_step}")else:logger.warning("No checkpoints found for recovery")# Set buttons for no active trainingui_updates = {"start_btn": {"interactive": True, "variant": "primary", "value": "Start Training"},"stop_btn": {"interactive": False, "variant": "secondary", "value": "Stop at Last Checkpoint"},"pause_resume_btn": {"interactive": False, "variant": "secondary", "visible": False}}return {"status": "error", "message": "No checkpoints found", "ui_updates": ui_updates}# Extract parameters from the saved session (not current UI state)# This ensures we use the original training parametersparams = last_session.get('params', {})# Add UI updates to restore the training parameters in the UI# This shows the user what values are being used for the resumed trainingui_updates.update({"model_type": params.get('model_type', list(MODEL_TYPES.keys())[0]),"lora_rank": params.get('lora_rank', "128"),"lora_alpha": params.get('lora_alpha', "128"),"num_epochs": params.get('num_epochs', 70),"batch_size": params.get('batch_size', 1),"learning_rate": params.get('learning_rate', 3e-5),"save_iterations": params.get('save_iterations', 500),"training_preset": params.get('preset_name', list(TRAINING_PRESETS.keys())[0])})# Check if we should auto-recover (immediate restart)auto_recover = True # Always auto-recover on startupif auto_recover:# Attempt to resume training using the ORIGINAL parameterstry:# Extract required parameters from the sessionmodel_type = params.get('model_type')lora_rank = params.get('lora_rank')lora_alpha = params.get('lora_alpha')num_epochs = params.get('num_epochs')batch_size = params.get('batch_size')learning_rate = params.get('learning_rate')save_iterations = params.get('save_iterations')repo_id = params.get('repo_id', '')preset_name = params.get('preset_name', list(TRAINING_PRESETS.keys())[0])# Log the recovery attemptself.append_log(f"Auto-recovering training from checkpoint {checkpoint_step}")gr.Info(f"Automatically resuming training from checkpoint {checkpoint_step}")# Attempt to resume trainingresult = self.start_training(model_type=model_type,lora_rank=lora_rank,lora_alpha=lora_alpha,num_epochs=num_epochs,batch_size=batch_size,learning_rate=learning_rate,save_iterations=save_iterations,repo_id=repo_id,preset_name=preset_name,resume_from_checkpoint=str(latest_checkpoint))# Set buttons for active trainingui_updates.update({"start_btn": {"interactive": False, "variant": "secondary", "value": "Continue Training"},"stop_btn": {"interactive": True, "variant": "primary", "value": "Stop at Last Checkpoint"},"pause_resume_btn": {"interactive": False, "variant": "secondary", "visible": False}})return {"status": "recovered","message": f"Training resumed from checkpoint {checkpoint_step}","result": result,"ui_updates": ui_updates}except Exception as e:logger.error(f"Failed to auto-resume training: {str(e)}")# Set buttons for manual recoveryui_updates.update({"start_btn": {"interactive": True, "variant": "primary", "value": "Continue Training"},"stop_btn": {"interactive": False, "variant": "secondary", "value": "Stop at Last Checkpoint"},"pause_resume_btn": {"interactive": False, "variant": "secondary", "visible": False}})return {"status": "error", "message": f"Failed to auto-resume: {str(e)}", "ui_updates": ui_updates}else:# Set up UI for manual recoveryui_updates.update({636 unmodified lines637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787636 unmodified linesreturn Falsedef recover_interrupted_training(self) -> Dict[str, Any]:"""Attempt to recover interrupted trainingReturns:Dict with recovery status and UI updates"""status = self.get_status()ui_updates = {}# Check for any checkpoints, even if status doesn't indicate trainingcheckpoints = list(OUTPUT_PATH.glob("checkpoint-*"))has_checkpoints = len(checkpoints) > 0# If status indicates training but process isn't running, or if we have checkpoints# and no active training process, try to recoverif (status.get('status') in ['training', 'paused'] and not self.is_training_running()) or \(has_checkpoints and not self.is_training_running()):logger.info("Detected interrupted training session or existing checkpoints, attempting to recover...")# Get the latest checkpointlast_session = self.load_session()if not last_session:logger.warning("No session data found for recovery, but will check for checkpoints")# Try to create a default session based on UI state if we have checkpointsif has_checkpoints:ui_state = self.load_ui_state()# Create a default session using UI state valueslast_session = {"params": {"model_type": MODEL_TYPES.get(ui_state.get("model_type", list(MODEL_TYPES.keys())[0])),"lora_rank": ui_state.get("lora_rank", "128"),"lora_alpha": ui_state.get("lora_alpha", "128"),"num_epochs": ui_state.get("num_epochs", 70),"batch_size": ui_state.get("batch_size", 1),"learning_rate": ui_state.get("learning_rate", 3e-5),"save_iterations": ui_state.get("save_iterations", 500),"preset_name": ui_state.get("training_preset", list(TRAINING_PRESETS.keys())[0]),"repo_id": "" # Default empty repo ID}}logger.info("Created default session from UI state for recovery")else:# Set buttons for no active trainingui_updates = {"start_btn": {"interactive": True, "variant": "primary", "value": "Start Training"},"stop_btn": {"interactive": False, "variant": "secondary", "value": "Stop at Last Checkpoint"},"delete_checkpoints_btn": {"interactive": False, "variant": "stop", "value": "Delete All Checkpoints"},"pause_resume_btn": {"interactive": False, "variant": "secondary", "visible": False}}return {"status": "idle", "message": "No training in progress", "ui_updates": ui_updates}# Find the latest checkpoint if we have checkpointslatest_checkpoint = Nonecheckpoint_step = 0if has_checkpoints:latest_checkpoint = max(checkpoints, key=os.path.getmtime)checkpoint_step = int(latest_checkpoint.name.split("-")[1])logger.info(f"Found checkpoint at step {checkpoint_step}")else:logger.warning("No checkpoints found for recovery")# Set buttons for no active trainingui_updates = {"start_btn": {"interactive": True, "variant": "primary", "value": "Start Training"},"stop_btn": {"interactive": False, "variant": "secondary", "value": "Stop at Last Checkpoint"},"delete_checkpoints_btn": {"interactive": False, "variant": "stop", "value": "Delete All Checkpoints"},"pause_resume_btn": {"interactive": False, "variant": "secondary", "visible": False}}return {"status": "error", "message": "No checkpoints found", "ui_updates": ui_updates}# Extract parameters from the saved session (not current UI state)# This ensures we use the original training parametersparams = last_session.get('params', {})# Map internal model type back to display name for UI# This is the key fix for the "ltx_video" vs "LTX-Video (LoRA)" mismatchmodel_type_internal = params.get('model_type')model_type_display = model_type_internal# Find the display name that maps to our internal model typefor display_name, internal_name in MODEL_TYPES.items():if internal_name == model_type_internal:model_type_display = display_namelogger.info(f"Mapped internal model type '{model_type_internal}' to display name '{model_type_display}'")break# Add UI updates to restore the training parameters in the UI# This shows the user what values are being used for the resumed trainingui_updates.update({"model_type": model_type_display, # Use the display name for the UI dropdown"lora_rank": params.get('lora_rank', "128"),"lora_alpha": params.get('lora_alpha', "128"),"num_epochs": params.get('num_epochs', 70),"batch_size": params.get('batch_size', 1),"learning_rate": params.get('learning_rate', 3e-5),"save_iterations": params.get('save_iterations', 500),"training_preset": params.get('preset_name', list(TRAINING_PRESETS.keys())[0])})# Check if we should auto-recover (immediate restart)auto_recover = True # Always auto-recover on startupif auto_recover:# Rest of the auto-recovery code remains unchangedtry:# Use the internal model_type for the actual training# But keep model_type_display for the UIresult = self.start_training(model_type=model_type_internal,lora_rank=params.get('lora_rank', "128"),lora_alpha=params.get('lora_alpha', "128"),num_epochs=params.get('num_epochs', 70),batch_size=params.get('batch_size', 1),learning_rate=params.get('learning_rate', 3e-5),save_iterations=params.get('save_iterations', 500),repo_id=params.get('repo_id', ''),preset_name=params.get('preset_name', list(TRAINING_PRESETS.keys())[0]),resume_from_checkpoint=str(latest_checkpoint))# Set buttons for active trainingui_updates.update({"start_btn": {"interactive": False, "variant": "secondary", "value": "Continue Training"},"stop_btn": {"interactive": True, "variant": "primary", "value": "Stop at Last Checkpoint"},"delete_checkpoints_btn": {"interactive": False, "variant": "stop", "value": "Delete All Checkpoints"},"pause_resume_btn": {"interactive": False, "variant": "secondary", "visible": False}})return {"status": "recovered","message": f"Training resumed from checkpoint {checkpoint_step}","result": result,"ui_updates": ui_updates}except Exception as e:logger.error(f"Failed to auto-resume training: {str(e)}")# Set buttons for manual recoveryui_updates.update({"start_btn": {"interactive": True, "variant": "primary", "value": "Continue Training"},"stop_btn": {"interactive": False, "variant": "secondary", "value": "Stop at Last Checkpoint"},"delete_checkpoints_btn": {"interactive": True, "variant": "stop", "value": "Delete All Checkpoints"},"pause_resume_btn": {"interactive": False, "variant": "secondary", "visible": False}})return {"status": "error", "message": f"Failed to auto-resume: {str(e)}", "ui_updates": ui_updates}else:# Set up UI for manual recoveryui_updates.update({
7 unmodified lines891011121314264 unmodified lines2792802812822832842852 unmodified lines288289290291292293294295296297298299155 unmodified lines4554564574584594601 unmodified line46246346446546646746811 unmodified lines4804814824834844857 unmodified linesfrom pathlib import Pathfrom .base_tab import BaseTabfrom ..config import TRAINING_PRESETS, MODEL_TYPES, ASK_USER_TO_DUPLICATE_SPACE, SMALL_TRAINING_BUCKETSfrom ..utils import TrainingLogParserlogger = logging.getLogger(__name__)264 unmodified lines)def handle_training_start(self, preset, model_type, *args):"""Handle training start with proper log parser reset"""# Safely reset log parser if it existsif hasattr(self.app, 'log_parser') and self.app.log_parser is not None:self.app.log_parser.reset()2 unmodified linesfrom ..utils import TrainingLogParserself.app.log_parser = TrainingLogParser()# Start trainingreturn self.app.trainer.start_training(MODEL_TYPES[model_type],*args,preset_name=preset)def get_model_info(self, model_type: str) -> str:"""Get information about the selected model type"""155 unmodified linesstate = self.app.trainer.get_status()logs = self.app.trainer.get_logs()# Ensure log parser is initializedif not hasattr(self.app, 'log_parser') or self.app.log_parser is None:from ..utils import TrainingLogParser1 unmodified linelogger.info("Initialized missing log parser")# Parse new log linesif logs:last_state = Nonefor line in logs.splitlines():try:11 unmodified lines# Parse status for training stateif "completed" in state["message"].lower():state["status"] = "completed"return (state["status"], state["message"], logs)7 unmodified lines891011121314264 unmodified lines2792802812822832842852 unmodified lines288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322155 unmodified lines4784794804814824834844854864874884894904914924934944954964974984995001 unmodified line50250350450550650750811 unmodified lines5205215225235245255265275285295305317 unmodified linesfrom pathlib import Pathfrom .base_tab import BaseTabfrom ..config import TRAINING_PRESETS, OUTPUT_PATH, MODEL_TYPES, ASK_USER_TO_DUPLICATE_SPACE, SMALL_TRAINING_BUCKETSfrom ..utils import TrainingLogParserlogger = logging.getLogger(__name__)264 unmodified lines)def handle_training_start(self, preset, model_type, *args):"""Handle training start with proper log parser reset and checkpoint detection"""# Safely reset log parser if it existsif hasattr(self.app, 'log_parser') and self.app.log_parser is not None:self.app.log_parser.reset()2 unmodified linesfrom ..utils import TrainingLogParserself.app.log_parser = TrainingLogParser()# Check for latest checkpointcheckpoints = list(OUTPUT_PATH.glob("checkpoint-*"))resume_from = Noneif checkpoints:# Find the latest checkpointlatest_checkpoint = max(checkpoints, key=os.path.getmtime)resume_from = str(latest_checkpoint)logger.info(f"Found checkpoint at {resume_from}, will resume training")# Convert model_type display name to internal namemodel_internal_type = MODEL_TYPES.get(model_type)if not model_internal_type:logger.error(f"Invalid model type: {model_type}")return f"Error: Invalid model type '{model_type}'", "Model type not recognized"# Start training (it will automatically use the checkpoint if provided)try:return self.app.trainer.start_training(model_internal_type, # Use internal model type*args,preset_name=preset,resume_from_checkpoint=resume_from)except Exception as e:logger.exception("Error starting training")return f"Error starting training: {str(e)}", f"Exception: {str(e)}\n\nCheck the logs for more details."def get_model_info(self, model_type: str) -> str:"""Get information about the selected model type"""155 unmodified linesstate = self.app.trainer.get_status()logs = self.app.trainer.get_logs()# Check if training process died unexpectedlytraining_died = Falseif state["status"] == "training" and not self.app.trainer.is_training_running():state["status"] = "error"state["message"] = "Training process terminated unexpectedly."training_died = True# Look for error in logserror_lines = []for line in logs.splitlines():if "Error:" in line or "Exception:" in line or "Traceback" in line:error_lines.append(line)if error_lines:state["message"] += f"\n\nPossible error: {error_lines[-1]}"# Ensure log parser is initializedif not hasattr(self.app, 'log_parser') or self.app.log_parser is None:from ..utils import TrainingLogParser1 unmodified linelogger.info("Initialized missing log parser")# Parse new log linesif logs and not training_died:last_state = Nonefor line in logs.splitlines():try:11 unmodified lines# Parse status for training stateif "completed" in state["message"].lower():state["status"] = "completed"elif "error" in state["message"].lower():state["status"] = "error"elif "failed" in state["message"].lower():state["status"] = "error"elif "stopped" in state["message"].lower():state["status"] = "stopped"return (state["status"], state["message"], logs)
6 unmodified lines78910111213146 unmodified lines1601611621631641651668 unmodified lines1751761771781791801811827 unmodified lines1901911921931941951961971986 unmodified linesfrom ..services import TrainingService, CaptioningService, SplittingService, ImportServicefrom ..config import (STORAGE_PATH, VIDEOS_TO_SPLIT_PATH, STAGING_PATH,TRAINING_PATH, LOG_FILE_PATH, TRAINING_PRESETS, TRAINING_VIDEOS_PATH, MODEL_PATH, OUTPUT_PATH,MODEL_TYPES, SMALL_TRAINING_BUCKETS)146 unmodified lines# If we recovered training parameters from the original sessionui_state = {}for param in ["model_type", "lora_rank", "lora_alpha", "num_epochs","batch_size", "learning_rate", "save_iterations", "training_preset"]:if param in recovery_ui:ui_state[param] = recovery_ui[param]8 unmodified lines# Load values (potentially with recovery updates applied)ui_state = self.load_ui_values()training_preset = ui_state.get("training_preset", list(TRAINING_PRESETS.keys())[0])model_type_val = ui_state.get("model_type", list(MODEL_TYPES.keys())[0])lora_rank_val = ui_state.get("lora_rank", "128")lora_alpha_val = ui_state.get("lora_alpha", "128")num_epochs_val = int(ui_state.get("num_epochs", 70))7 unmodified linestraining_dataset,start_btn,stop_btn,delete_checkpoints_btn, # Replaces pause_resume_btntraining_preset,model_type_val,lora_rank_val,lora_alpha_val,num_epochs_val,6 unmodified lines78910111213146 unmodified lines1601611621631641651661671681691701711721731741751761771781791801811821838 unmodified lines1921931941951961971981992002012022032042052062077 unmodified lines2152162172182192202212222236 unmodified linesfrom ..services import TrainingService, CaptioningService, SplittingService, ImportServicefrom ..config import (STORAGE_PATH, VIDEOS_TO_SPLIT_PATH, STAGING_PATH, OUTPUT_PATH,TRAINING_PATH, LOG_FILE_PATH, TRAINING_PRESETS, TRAINING_VIDEOS_PATH, MODEL_PATH, OUTPUT_PATH,MODEL_TYPES, SMALL_TRAINING_BUCKETS)146 unmodified lines# If we recovered training parameters from the original sessionui_state = {}# Handle model_type specifically - could be internal or display nameif "model_type" in recovery_ui:model_type_value = recovery_ui["model_type"]# If it's an internal name, convert to display nameif model_type_value not in MODEL_TYPES:# Find the display name for this internal model typefor display_name, internal_name in MODEL_TYPES.items():if internal_name == model_type_value:model_type_value = display_namelogger.info(f"Converted internal model type '{recovery_ui['model_type']}' to display name '{model_type_value}'")breakui_state["model_type"] = model_type_value# Copy other parametersfor param in ["lora_rank", "lora_alpha", "num_epochs","batch_size", "learning_rate", "save_iterations", "training_preset"]:if param in recovery_ui:ui_state[param] = recovery_ui[param]8 unmodified lines# Load values (potentially with recovery updates applied)ui_state = self.load_ui_values()# Ensure model_type is a display name, not internal namemodel_type_val = ui_state.get("model_type", list(MODEL_TYPES.keys())[0])if model_type_val not in MODEL_TYPES:# Convert from internal to display namefor display_name, internal_name in MODEL_TYPES.items():if internal_name == model_type_val:model_type_val = display_namebreaktraining_preset = ui_state.get("training_preset", list(TRAINING_PRESETS.keys())[0])lora_rank_val = ui_state.get("lora_rank", "128")lora_alpha_val = ui_state.get("lora_alpha", "128")num_epochs_val = int(ui_state.get("num_epochs", 70))7 unmodified linestraining_dataset,start_btn,stop_btn,delete_checkpoints_btn,training_preset,model_type_val,lora_rank_val,lora_alpha_val,num_epochs_val,
finetrainers/dataset.py+1-1
vms/services/trainer.py+134-132
vms/tabs/train_tab.py+55-9
vms/ui/video_trainer_ui.py+30-5