(args)
| 660 | |
| 661 | |
| 662 | def main(args): |
| 663 | if args.report_to == "wandb" and args.hub_token is not None: |
| 664 | raise ValueError( |
| 665 | "You cannot use both --report_to=wandb and --hub_token due to a security risk of exposing your token." |
| 666 | " Please use `huggingface-cli login` to authenticate with the Hub." |
| 667 | ) |
| 668 | |
| 669 | logging_dir = Path(args.output_dir, args.logging_dir) |
| 670 | |
| 671 | accelerator_project_config = ProjectConfiguration(project_dir=args.output_dir, logging_dir=logging_dir) |
| 672 | |
| 673 | accelerator = Accelerator( |
| 674 | gradient_accumulation_steps=args.gradient_accumulation_steps, |
| 675 | mixed_precision=args.mixed_precision, |
| 676 | log_with=args.report_to, |
| 677 | project_config=accelerator_project_config, |
| 678 | ) |
| 679 | |
| 680 | # Disable AMP for MPS. |
| 681 | if torch.backends.mps.is_available(): |
| 682 | accelerator.native_amp = False |
| 683 | |
| 684 | if args.report_to == "wandb": |
| 685 | if not is_wandb_available(): |
| 686 | raise ImportError("Make sure to install wandb if you want to use it for logging during training.") |
| 687 | import wandb |
| 688 | |
| 689 | # Currently, it's not possible to do gradient accumulation when training two models with accelerate.accumulate |
| 690 | # This will be enabled soon in accelerate. For now, we don't allow gradient accumulation when training two models. |
| 691 | # TODO (patil-suraj): Remove this check when gradient accumulation with two models is enabled in accelerate. |
| 692 | # Make one log on every process with the configuration for debugging. |
| 693 | logging.basicConfig( |
| 694 | format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", |
| 695 | datefmt="%m/%d/%Y %H:%M:%S", |
| 696 | level=logging.INFO, |
| 697 | ) |
| 698 | logger.info(accelerator.state, main_process_only=False) |
| 699 | if accelerator.is_local_main_process: |
| 700 | transformers.utils.logging.set_verbosity_warning() |
| 701 | diffusers.utils.logging.set_verbosity_info() |
| 702 | else: |
| 703 | transformers.utils.logging.set_verbosity_error() |
| 704 | diffusers.utils.logging.set_verbosity_error() |
| 705 | |
| 706 | # We need to initialize the trackers we use, and also store our configuration. |
| 707 | # The trackers initializes automatically on the main process. |
| 708 | if accelerator.is_main_process: |
| 709 | accelerator.init_trackers("custom-diffusion", config=vars(args)) |
| 710 | |
| 711 | # If passed along, set the training seed now. |
| 712 | if args.seed is not None: |
| 713 | set_seed(args.seed) |
| 714 | if args.concepts_list is None: |
| 715 | args.concepts_list = [ |
| 716 | { |
| 717 | "instance_prompt": args.instance_prompt, |
| 718 | "class_prompt": args.class_prompt, |
| 719 | "instance_data_dir": args.instance_data_dir, |
no test coverage detected