Skip to content

Commit

Permalink
refactor: convert bash script to python
Browse files Browse the repository at this point in the history
Ran successfully to completion. But on a repeat run `convert_unclip` didn't pass the hash check for some reason.

- fix inpainting model download urls
- shows a progress bar for downloads
- skips downloading existing files
- uses a temporary file to prevent partial downloads
- can do a dry run to check if url is valid `DRY_RUN=1 python scripts/prepare_test_weights.py`
- displays the downloaded file hash
  • Loading branch information
brycedrennan authored and catwell committed Dec 15, 2023
1 parent 77fb803 commit 5ca1549
Show file tree
Hide file tree
Showing 11 changed files with 618 additions and 397 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ rye sync --features test,conversion
Then, download and convert all the necessary weights. Be aware that this will use around 50 GB of disk space:

```bash
./scripts/prepare-test-weights.sh
rye run python scripts/prepare_test_weights.py
```

Finally, run the tests:
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ conversion = [
"diffusers>=0.24.0",
"transformers>=4.35.2",
"segment-anything-py>=1.0",
"requests>=2.26.0",
"tqdm>=4.62.3",
]

[build-system]
Expand Down
5 changes: 4 additions & 1 deletion scripts/conversion/convert_diffusers_autoencoder_kl.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ class Args(argparse.Namespace):

def setup_converter(args: Args) -> ModelConverter:
target = LatentDiffusionAutoencoder()
# low_cpu_mem_usage=False stops some annoying console messages us to `pip install accelerate`
source: nn.Module = AutoencoderKL.from_pretrained( # type: ignore
pretrained_model_name_or_path=args.source_path, subfolder=args.subfolder
pretrained_model_name_or_path=args.source_path,
subfolder=args.subfolder,
low_cpu_mem_usage=False,
) # type: ignore
x = torch.randn(1, 3, 512, 512)
converter = ModelConverter(source_model=source, target_model=target, skip_output_check=True, verbose=args.verbose)
Expand Down
6 changes: 5 additions & 1 deletion scripts/conversion/convert_diffusers_controlnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ class Args(argparse.Namespace):

@torch.no_grad()
def convert(args: Args) -> dict[str, torch.Tensor]:
controlnet_src: nn.Module = ControlNetModel.from_pretrained(pretrained_model_name_or_path=args.source_path) # type: ignore
# low_cpu_mem_usage=False stops some annoying console messages us to `pip install accelerate`
controlnet_src: nn.Module = ControlNetModel.from_pretrained( # type: ignore
pretrained_model_name_or_path=args.source_path,
low_cpu_mem_usage=False,
)
unet = SD1UNet(in_channels=4)
adapter = SD1ControlnetAdapter(unet, name="mycn").inject()
controlnet = unet.Controlnet
Expand Down
6 changes: 5 additions & 1 deletion scripts/conversion/convert_diffusers_lora.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ class Args(argparse.Namespace):
@torch.no_grad()
def process(args: Args) -> None:
diffusers_state_dict = cast(dict[str, Tensor], torch.load(args.source_path, map_location="cpu")) # type: ignore
diffusers_sd = DiffusionPipeline.from_pretrained(pretrained_model_name_or_path=args.base_model) # type: ignore
# low_cpu_mem_usage=False stops some annoying console messages us to `pip install accelerate`
diffusers_sd = DiffusionPipeline.from_pretrained( # type: ignore
pretrained_model_name_or_path=args.base_model,
low_cpu_mem_usage=False,
)
diffusers_model = cast(fl.Module, diffusers_sd.unet) # type: ignore

refiners_model = SD1UNet(in_channels=4)
Expand Down
6 changes: 5 additions & 1 deletion scripts/conversion/convert_diffusers_t2i_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@

sdxl = "xl" in args.source_path
target = ConditionEncoderXL() if sdxl else ConditionEncoder()
source: nn.Module = T2IAdapter.from_pretrained(pretrained_model_name_or_path=args.source_path) # type: ignore
# low_cpu_mem_usage=False stops some annoying console messages us to `pip install accelerate`
source: nn.Module = T2IAdapter.from_pretrained( # type: ignore
pretrained_model_name_or_path=args.source_path,
low_cpu_mem_usage=False,
)
assert isinstance(source, nn.Module), "Source model is not a nn.Module"

x = torch.randn(1, 3, 1024, 1024) if sdxl else torch.randn(1, 3, 512, 512)
Expand Down
5 changes: 4 additions & 1 deletion scripts/conversion/convert_diffusers_unet.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ class Args(argparse.Namespace):


def setup_converter(args: Args) -> ModelConverter:
# low_cpu_mem_usage=False stops some annoying console messages us to `pip install accelerate`
source: nn.Module = UNet2DConditionModel.from_pretrained( # type: ignore
pretrained_model_name_or_path=args.source_path, subfolder="unet"
pretrained_model_name_or_path=args.source_path,
subfolder="unet",
low_cpu_mem_usage=False,
)
source_in_channels: int = source.config.in_channels # type: ignore
source_clip_embedding_dim: int = source.config.cross_attention_dim # type: ignore
Expand Down
5 changes: 4 additions & 1 deletion scripts/conversion/convert_transformers_clip_image_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ class Args(argparse.Namespace):


def setup_converter(args: Args) -> ModelConverter:
# low_cpu_mem_usage=False stops some annoying console messages us to `pip install accelerate`
source: nn.Module = CLIPVisionModelWithProjection.from_pretrained( # type: ignore
pretrained_model_name_or_path=args.source_path, subfolder=args.subfolder
pretrained_model_name_or_path=args.source_path,
subfolder=args.subfolder,
low_cpu_mem_usage=False,
)
assert isinstance(source, nn.Module), "Source model is not a nn.Module"
architecture: str = source.config.architectures[0] # type: ignore
Expand Down
5 changes: 4 additions & 1 deletion scripts/conversion/convert_transformers_clip_text_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ class Args(argparse.Namespace):


def setup_converter(args: Args) -> ModelConverter:
# low_cpu_mem_usage=False stops some annoying console messages us to `pip install accelerate`
source: nn.Module = CLIPTextModelWithProjection.from_pretrained( # type: ignore
pretrained_model_name_or_path=args.source_path, subfolder=args.subfolder
pretrained_model_name_or_path=args.source_path,
subfolder=args.subfolder,
low_cpu_mem_usage=False,
)
assert isinstance(source, nn.Module), "Source model is not a nn.Module"
architecture: str = source.config.architectures[0] # type: ignore
Expand Down
Loading

0 comments on commit 5ca1549

Please sign in to comment.