-
-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathSanaBaseDataLoader.py
More file actions
157 lines (124 loc) · 8.33 KB
/
SanaBaseDataLoader.py
File metadata and controls
157 lines (124 loc) · 8.33 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
import os
from modules.dataLoader.BaseDataLoader import BaseDataLoader
from modules.dataLoader.mixin.DataLoaderText2ImageMixin import DataLoaderText2ImageMixin
from modules.model.BaseModel import BaseModel
from modules.model.SanaModel import SanaModel
from modules.modelSetup.BaseModelSetup import BaseModelSetup
from modules.modelSetup.BaseSanaSetup import BaseSanaSetup
from modules.util import factory
from modules.util.config.TrainConfig import TrainConfig
from modules.util.enum.ModelType import ModelType
from modules.util.TrainProgress import TrainProgress
from mgds.pipelineModules.DecodeTokens import DecodeTokens
from mgds.pipelineModules.DecodeVAE import DecodeVAE
from mgds.pipelineModules.EncodeGemmaText import EncodeGemmaText
from mgds.pipelineModules.EncodeVAE import EncodeVAE
from mgds.pipelineModules.MapData import MapData
from mgds.pipelineModules.RescaleImageChannels import RescaleImageChannels
from mgds.pipelineModules.SaveImage import SaveImage
from mgds.pipelineModules.SaveText import SaveText
from mgds.pipelineModules.ScaleImage import ScaleImage
from mgds.pipelineModules.Tokenize import Tokenize
class SanaBaseDataLoader(
BaseDataLoader,
DataLoaderText2ImageMixin,
):
def _preparation_modules(self, config: TrainConfig, model: SanaModel):
max_token_length = 300
rescale_image = RescaleImageChannels(image_in_name='image', image_out_name='image', in_range_min=0, in_range_max=1, out_range_min=-1, out_range_max=1)
rescale_conditioning_image = RescaleImageChannels(image_in_name='conditioning_image', image_out_name='conditioning_image', in_range_min=0, in_range_max=1, out_range_min=-1, out_range_max=1)
encode_image = EncodeVAE(in_name='image', out_name='latent_image', vae=model.vae, autocast_contexts=[model.autocast_context, model.vae_autocast_context], dtype=model.train_dtype.torch_dtype())
downscale_mask = ScaleImage(in_name='mask', out_name='latent_mask', factor=0.03125)
add_embeddings_to_prompt = MapData(in_name='prompt', out_name='prompt', map_fn=model.add_text_encoder_embeddings_to_prompt)
encode_conditioning_image = EncodeVAE(in_name='conditioning_image', out_name='latent_conditioning_image', vae=model.vae, autocast_contexts=[model.autocast_context, model.vae_autocast_context], dtype=model.train_dtype.torch_dtype())
tokenize_prompt = Tokenize(in_name='prompt', tokens_out_name='tokens', mask_out_name='tokens_mask', tokenizer=model.tokenizer, max_token_length=max_token_length)
encode_prompt = EncodeGemmaText(tokens_in_name='tokens', tokens_attention_mask_in_name='tokens_mask', hidden_state_out_name='text_encoder_hidden_state', add_layer_norm=True, text_encoder=model.text_encoder,
hidden_state_output_index=-(1 + config.text_encoder_layer_skip), autocast_contexts=[model.autocast_context, model.text_encoder_autocast_context], dtype=model.text_encoder_train_dtype.torch_dtype())
modules = [rescale_image, encode_image]
if config.masked_training or config.model_type.has_mask_input():
modules.append(downscale_mask)
if config.model_type.has_conditioning_image_input():
modules += [rescale_conditioning_image, encode_conditioning_image]
modules += [add_embeddings_to_prompt, tokenize_prompt]
if not config.train_text_encoder_or_embedding():
modules.append(encode_prompt)
return modules
def _cache_modules(self, config: TrainConfig, model: SanaModel, model_setup: BaseSanaSetup):
image_split_names = ['latent_image']
if config.masked_training or config.model_type.has_mask_input():
image_split_names.append('latent_mask')
if config.model_type.has_conditioning_image_input():
image_split_names.append('latent_conditioning_image')
image_aggregate_names = ['crop_resolution', 'image_path']
text_split_names = ['tokens', 'tokens_mask', 'text_encoder_hidden_state']
sort_names = text_split_names + image_aggregate_names + image_split_names + [
'prompt', 'concept'
]
return self._cache_modules_from_names(
model, model_setup,
image_split_names=image_split_names,
image_aggregate_names=image_aggregate_names,
text_split_names=text_split_names,
sort_names=sort_names,
config=config,
text_caching=not config.train_text_encoder_or_embedding(),
)
def _output_modules(self, config: TrainConfig, model: SanaModel, model_setup: BaseSanaSetup, is_validation: bool = False):
output_names = [
'image_path', 'latent_image',
'prompt',
'tokens',
'tokens_mask',
]
if config.masked_training or config.model_type.has_mask_input():
output_names.append('latent_mask')
if config.model_type.has_conditioning_image_input():
output_names.append('latent_conditioning_image')
if not config.train_text_encoder_or_embedding():
output_names.append('text_encoder_hidden_state')
return self._output_modules_from_out_names(
model, model_setup,
output_names=output_names,
config=config,
use_conditioning_image=True,
vae=model.vae,
autocast_context=[model.autocast_context],
train_dtype=model.train_dtype,
is_validation=is_validation,
)
def _debug_modules(self, config: TrainConfig, model: SanaModel):
debug_dir = os.path.join(config.debug_dir, "dataloader")
def before_save_fun():
model.vae_to(self.train_device)
decode_image = DecodeVAE(in_name='latent_image', out_name='decoded_image', vae=model.vae, autocast_contexts=[model.autocast_context, model.vae_autocast_context], dtype=model.train_dtype.torch_dtype())
decode_conditioning_image = DecodeVAE(in_name='latent_conditioning_image', out_name='decoded_conditioning_image', vae=model.vae, autocast_contexts=[model.autocast_context, model.vae_autocast_context], dtype=model.train_dtype.torch_dtype())
upscale_mask = ScaleImage(in_name='latent_mask', out_name='decoded_mask', factor=32)
decode_prompt = DecodeTokens(in_name='tokens', out_name='decoded_prompt', tokenizer=model.tokenizer)
save_image = SaveImage(image_in_name='decoded_image', original_path_in_name='image_path', path=debug_dir, in_range_min=-1, in_range_max=1, before_save_fun=before_save_fun)
save_conditioning_image = SaveImage(image_in_name='decoded_conditioning_image', original_path_in_name='image_path', path=debug_dir, in_range_min=-1, in_range_max=1, before_save_fun=before_save_fun)
# SaveImage(image_in_name='latent_mask', original_path_in_name='image_path', path=debug_dir, in_range_min=0, in_range_max=1, before_save_fun=before_save_fun),
save_mask = SaveImage(image_in_name='decoded_mask', original_path_in_name='image_path', path=debug_dir, in_range_min=0, in_range_max=1, before_save_fun=before_save_fun)
save_prompt = SaveText(text_in_name='decoded_prompt', original_path_in_name='image_path', path=debug_dir, before_save_fun=before_save_fun)
# These modules don't really work, since they are inserted after a sorting operation that does not include this data
# SaveImage(image_in_name='mask', original_path_in_name='image_path', path=debug_dir, in_range_min=0, in_range_max=1),
# SaveImage(image_in_name='image', original_path_in_name='image_path', path=debug_dir, in_range_min=-1, in_range_max=1),
modules = [decode_image, save_image]
if config.model_type.has_conditioning_image_input():
modules += [decode_conditioning_image, save_conditioning_image]
if config.masked_training or config.model_type.has_mask_input():
modules += [upscale_mask, save_mask]
modules += [decode_prompt, save_prompt]
return modules
def _create_dataset(
self,
config: TrainConfig,
model: BaseModel,
model_setup: BaseModelSetup,
train_progress: TrainProgress,
is_validation: bool = False,
):
return DataLoaderText2ImageMixin._create_dataset(self,
config, model, model_setup, train_progress, is_validation,
aspect_bucketing_quantization=32,
)
factory.register(BaseDataLoader, SanaBaseDataLoader, ModelType.SANA)