Repository navigation
Expand file tree
/
Copy pathimage_sefi.dgl
More file actions
81 lines (78 loc) · 3.62 KB
/
Copy pathimage_sefi.dgl
File metadata and controls
81 lines (78 loc) · 3.62 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
# 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")
image_base = {{from_vae}}(vae, vae_encode(vae, image::to_f32(source, channels = {{channels}}), rng = encode_rng{{vae_run_options}}))
base = tensor::pad(image_base, [0, 0, 0, 0, 16, 0])
{% else %}
base = tensor::zeros(latent_shape(vae, width = width, height = height))
{% endif %}
{{references}}
{% if flags.control_image %}
control_pixels = image::to_f32(fit_canvas(input_control, width, height, mode = "nearest"), channels = 3)
control_model = controlnet::load(control_path{{control_options}})
{% endif %}
{% if flags.mask_stage %}
{% if flags.has_mask %}
mask_pixels = tensor::threshold(image::to_f32(fit_canvas(tensor::grayscale(input_mask, axis = 0), width, height, mode = "nearest_exact")), threshold = 0.5)
{% else %}
mask_pixels = tensor::full([width, height, 1, 1], 1)
{% endif %}
base_shape = tensor::shape(base)
mask_scale = [math::floor(width / base_shape[0]), math::floor(height / base_shape[1])]
mask_latent = tensor::max_pool2d(mask_pixels, kernel = mask_scale, stride = mask_scale)
{% endif %}
positive_text = encode_prompt(text, prompt, width = width, height = height{{prompt_options}})
{% 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 %}
positive = positive_text
negative = negative_text
{% if flags.controlnet %}
controlled_positive = controlnet::apply(positive, control_model, {{control_hint}}, strength = control_strength, type = {{control_type}})
controlled_negative = controlnet::apply(negative, control_model, {{control_hint}}, strength = control_strength, type = {{control_type}})
{% endif %}
pos = predictor(model, {{positive_condition}}{{predictor_options}})
neg = predictor({{negative_model}}, {{negative_condition}}{{negative_predictor_options}})
guided = {{guidance_function}}(pos, neg, scale = cfg_scale{{guidance_options}})
{% if flags.custom_sigmas %}
full_sigmas = tensor::vector(custom_sigmas, dtype = "f32")
dual_schedule = sefi::make_sigmas(steps = tensor::numel(full_sigmas) - 1{{sefi_schedule_options}})
{% else %}
dual_schedule = sefi::make_sigmas(steps = steps{{sefi_schedule_options}})
full_sigmas = dual_schedule.sigmas
{% endif %}
{% if flags.has_init %}
sigmas = slice_sigmas_by_strength(full_sigmas, strength)
semantic_sigmas = dual_schedule.semantic_sigmas[-tensor::numel(sigmas):]
{% else %}
sigmas = full_sigmas
semantic_sigmas = dual_schedule.semantic_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 %}
start = add_noise(guided, base, tensor::randn_like(base, rng = rng), sigma = sigmas[0])
latent = sefi::sample(start, sigmas, predict = guided, semantic_sigmas = semantic_sigmas, method = method, rng = sampler_rng, eta = eta{{sample_options}})
{% if flags.hires %}
{% include "image_hires" %}
{% endif %}
image = vae_decode(vae, dit_to_vae_latents(vae, {{result_latent}}[:, :, 16:144]){{vae_run_options}})
return {image = image, latent = {{result_latent}}}
}