Repository navigation
Expand file tree
/
Copy pathimage_edit.dgl
More file actions
105 lines (102 loc) · 3.93 KB
/
Copy pathimage_edit.dgl
File metadata and controls
105 lines (102 loc) · 3.93 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
# version: 0.0.0
var {
{{variables}}
}
{% include "image_helpers" %}
fn main({{inputs}}) {
base_model = {{model_loader}}(model_path{{model_options}})
base_text = load_text_encoder(text_path{{text_options}})
vae = {{vae_loader}}(vae_path{{vae_options}})
{{loras}}
model = {{patched_model}}
text = {{patched_text}}
encode_rng = make_rng(rng_type, seed = seed)
{% if flags.has_init %}
source = fit_canvas(input_image, width, height, mode = "nearest")
{% if flags.raw_reference %}
image_base = vae_encode(vae, image::to_f32(source, channels = {{channels}}), rng = encode_rng, posterior = "mode"{{vae_run_options}})
{% else %}
image_base = {{from_vae}}(vae, vae_encode(vae, image::to_f32(source, channels = {{channels}}), rng = encode_rng{{vae_run_options}}))
{% endif %}
{% else %}
image_base = tensor::zeros(latent_shape(vae, width = width, height = height))
{% endif %}
base = image_base
{{references}}
{% if flags.unet_edit %}
{% if flags.references %}
edit_latent = tensor::interpolate(reference_latent_0, size = tensor::shape(base)[:2])
{% else %}
edit_latent = tensor::zeros_like(base)
{% endif %}
{% if flags.image_guidance %}
image_unconditional_concat = tensor::zeros_like(edit_latent)
{% endif %}
{% endif %}
{% if flags.control_image %}
control_pixels = image::to_f32(fit_canvas(input_control, width, height, mode = "nearest"), channels = 3)
{% if flags.native_control %}
control_latent = {{from_vae}}(vae, vae_encode(vae, control_pixels, rng = encode_rng{{vae_run_options}}))
{% else %}
control_model = controlnet::load(control_path{{control_options}})
{% if flags.z_control %}
control_encoded = {{from_vae}}(vae, vae_encode(vae, control_pixels, rng = encode_rng, posterior = "mode"{{vae_run_options}}))
control_hint = tensor::pad(control_encoded, [0, 0, 0, 0, 0, 33 - tensor::shape(control_encoded)[2]])
{% endif %}
{% endif %}
{% endif %}
{% if flags.separate_negative_model %}
negative_model = load_dit(negative_model_path{{model_options}})
{% endif %}
positive_text = encode_prompt(text, prompt, width = width, height = height{{prompt_options}})
{% if flags.minit2i %}
negative_text = zero_out(positive_text, fields = ["vector"])
{% elif flags.ming %}
negative_text = zero_out(positive_text)
{% elif flags.ideogram %}
negative_text = {vector = tensor::vector([1])}
{% else %}
{% if flags.encode_negative %}
negative_text = encode_prompt(text, {{negative_prompt_input}}, width = width, height = height{{negative_prompt_options}})
{% else %}
negative_text = positive_text
{% endif %}
{% endif %}
positive = positive_text
{% if flags.sdxl_zero %}
negative = sdxl_zero_text(negative_text)
{% else %}
negative = negative_text
{% endif %}
{% if flags.control_image_guidance %}
image_unconditional_concat = tensor::zeros_like(base)
{% endif %}
{% include "image_predictors" %}
{% if flags.custom_sigmas %}
full_sigmas = tensor::vector(custom_sigmas, dtype = "f32")
{% else %}
full_sigmas = make_sigmas({{schedule_predictor}}, latent = base, steps = steps, scheduler = scheduler{{schedule_options}})
{% endif %}
{% if flags.has_init %}
sigmas = slice_sigmas_by_strength(full_sigmas, strength)
{% else %}
sigmas = full_sigmas
{% endif %}
rng = make_rng(rng_type, seed = seed)
{% if flags.shared_rng %}
sampler_rng = rng
{% else %}
sampler_rng = make_rng(sampler_rng_type, seed = seed)
{% endif %}
{% if flags.hidream_o1 %}
start = add_noise(guided, base, tensor::mul(tensor::randn_like(base, rng = rng), eta), sigma = sigmas[0])
{% else %}
start = add_noise(guided, base, tensor::randn_like(base, rng = rng), sigma = sigmas[0])
{% endif %}
latent = sample(start, sigmas, predict = guided, method = method, rng = sampler_rng, eta = eta{{sample_options}})
{% if flags.hires %}
{% include "image_hires" %}
{% endif %}
image = vae_decode(vae, {{to_vae}}(vae, {{result_latent}}){{vae_run_options}})
return {image = image, latent = {{result_latent}}}
}