-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeall.sh
More file actions
executable file
·171 lines (154 loc) · 6.15 KB
/
Copy pathmakeall.sh
File metadata and controls
executable file
·171 lines (154 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env bash
# File : makeall.sh
# Purpose : Build all models (VAEs and transcoders) from the Tiny AutoEncoder models.
# Author : Martin Rizzo | <martinrizzo@gmail.com>
# Date : Nov 26, 2024
# Repo : https://github.com/martin-rizzo/TinyModelsForLatentConversion
# License : MIT
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Tiny Models for Latent Conversion
# Build fast VAEs and latent Transcoders models using Tiny AutoEncoders
#
# Copyright (c) 2024-2025 Martin Rizzo
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
HELP="
Usage: $0 [options]
Options:
-h, --help Show this help message and exit
--float16, --half Build models in float16
--float32 Build models in float32
--clean Clean output directory
"
SOURCE_DIR='./original_taesd_models'
OUTPUT_DIR='./output'
ORIGINAL_MODEL_NAME='diffusion_pytorch_model.safetensors'
# directories for each model type
declare -A MODEL_DIRS
MODEL_DIRS[sd]="$SOURCE_DIR/taesd"
MODEL_DIRS[sdxl]="$SOURCE_DIR/taesdxl"
MODEL_DIRS[sd3]="$SOURCE_DIR/taesd3"
MODEL_DIRS[flux]="$SOURCE_DIR/taef1"
MODELS=( sd sdxl sd3 flux )
# python script to build the models
BUILD_TINY_VAE="./build_tiny_vae.sh"
BUILD_TINY_TRANSCODER="./build_tiny_transcoder.sh"
BUILD_AUXILIARY="./build_auxiliary.sh"
# function to generate all combinations of models without repeating the same pair
generate_model_combinations() {
for i in "${MODELS[@]}"; do
for j in "${MODELS[@]}"; do
# skip the pairs where both models are the same
if [[ $i != "$j" ]]; then
echo "$i $j"
fi
done
done
}
# function to check if the provided parameters contain data type references
contains_dtype_parameter() {
for param in "$@"; do
[[ "$param" == "--float"* ]] && return 0 # found a parameter that contains "float"
done
return 1 # no parameter found that contains "float"
}
#===========================================================================#
#////////////////////////////////// MAIN ///////////////////////////////////#
#===========================================================================#
# extra parameters to pass to the build scripts
# (by default enable color and set output directory to $OUTPUT_DIR)
EXTRA_PARAMS=( '--color' '--output-dir' "$OUTPUT_DIR" )
# loop through the arguments and set the corresponding parameters
SHOW_HELP=false
CLEAN=false
for arg in "$@"; do
case $arg in
--float16|--half)
EXTRA_PARAMS+=( '--float16' )
;;
--float32)
EXTRA_PARAMS+=( '--float32' )
;;
--clean)
CLEAN=true
;;
-h|--help)
SHOW_HELP=true
;;
*)
echo "ERROR: Unknown argument '$arg'"
echo "use $0 -h for help"
exit 1
;;
esac
done
if [[ $CLEAN == true ]]; then
echo "Cleaning output directory..."
rm -v "$OUTPUT_DIR"/*.safetensors
exit 0
fi
if [[ $SHOW_HELP == true ]]; then
echo "$HELP"
exit 0
fi
echo
# BUILD VAEs FOR ALL MODELS
for model in "${MODELS[@]}"; do
echo "Building VAE for $model..."
model_dir="${MODEL_DIRS[$model]}"
"$BUILD_TINY_VAE" "${EXTRA_PARAMS[@]}" "--$model" "$model_dir/"*
done
# GENERATE COMBINATIONS OF MODELS
combinations=$(generate_model_combinations)
# BUILD ALL TRANSCODERS
while read -r from to; do
if [ "$from" != "$to" ]; then
from_original_model="${MODEL_DIRS[$from]}/$ORIGINAL_MODEL_NAME"
to_original_model="${MODEL_DIRS[$to]}/$ORIGINAL_MODEL_NAME"
echo "Building transcoder from $from to $to..."
"$BUILD_TINY_TRANSCODER" "${EXTRA_PARAMS[@]}" "--from-$from" "$from_original_model" "--to-$to" "$to_original_model"
fi
done <<< "$combinations"
# BUILD ALL TRANSCODERS WITH BLUR
while read -r from to; do
if [ "$from" != "$to" ]; then
from_original_model="${MODEL_DIRS[$from]}/$ORIGINAL_MODEL_NAME"
to_original_model="${MODEL_DIRS[$to]}/$ORIGINAL_MODEL_NAME"
echo "Building transcoder with blur layer from $from to $to..."
"$BUILD_TINY_TRANSCODER" "${EXTRA_PARAMS[@]}" --blur 0.5 "--from-$from" "$from_original_model" "--to-$to" "$to_original_model"
fi
done <<< "$combinations"
# BUILD AUXILARY SAFETENSORS
# (ONLY WHEN DATA TYPES ARE NOT SPECIFIED)
if contains_dtype_parameter "${EXTRA_PARAMS[@]}"; then
echo "Skipping auxiliary safetensors build, because float16 or float32 was specified."
else
echo "Building auxiliary safetensors..."
"$BUILD_AUXILIARY" --color --output-dir "$OUTPUT_DIR" --float32 \
--sd "${OUTPUT_DIR}/tiny_vae_sd.safetensors" \
--sdxl "${OUTPUT_DIR}/tiny_vae_sdxl.safetensors" \
--transcoder "${OUTPUT_DIR}/transcoder_from_sdxl_to_sd.safetensors"
"$BUILD_AUXILIARY" --color --output-dir "$OUTPUT_DIR" --float16 \
--sd "${OUTPUT_DIR}/tiny_vae_sd.safetensors" \
--sdxl "${OUTPUT_DIR}/tiny_vae_sdxl.safetensors" \
--transcoder "${OUTPUT_DIR}/transcoder_from_sdxl_to_sd.safetensors"
fi
echo