MCPcopy Create free account
hub / github.com/tdrussell/diffusion-pipe / cache_latents

Method cache_latents

utils/dataset.py:233–298  ·  view source on GitHub ↗
(self, map_fn, regenerate_cache=False, trust_cache=False, caching_batch_size=1)

Source from the content-addressed store, hash-verified

231 raise ValueError(f'num_repeats must be >0, was {self.num_repeats}')
232
233 def cache_latents(self, map_fn, regenerate_cache=False, trust_cache=False, caching_batch_size=1):
234 print(f'caching latents: {self.size_bucket}')
235 self.latent_dataset = _map_and_cache(
236 self.metadata_dataset,
237 map_fn,
238 self.cache_dir,
239 cache_file_prefix='latents_',
240 regenerate_cache=regenerate_cache,
241 caching_batch_size=caching_batch_size,
242 )
243 assert len(self.latent_dataset) == len(self.metadata_dataset), (len(self.latent_dataset), len(self.metadata_dataset))
244
245 iteration_order_cache_dir = self.cache_dir / 'iteration_order'
246
247 if regenerate_cache or not iteration_order_cache_dir.exists() or not trust_cache:
248 print('Building iteration order')
249 image_spec_to_latents_idx = {
250 tuple(image_spec): i
251 for i, image_spec in enumerate(self.metadata_dataset['image_spec'])
252 }
253
254 equal_num_captions = True
255 num_captions = None
256 for example in self.metadata_dataset.select_columns(['caption']):
257 n = len(example['caption'])
258 if num_captions is not None and n != num_captions:
259 equal_num_captions = False
260 break
261 num_captions = n
262
263 if equal_num_captions:
264 # If all images have the same number of captions, set things up so we read (mostly) sequentially off disk. The metadata was already shuffled in the beginning.
265 iteration_order_by_caption_num = [[] for _ in range(num_captions)]
266 seed = 0
267 for example in self.metadata_dataset.select_columns(['image_spec', 'caption']):
268 image_spec = example['image_spec']
269 captions = example['caption']
270 shuffle_with_seed(captions, seed)
271 seed += 1
272 latents_idx = image_spec_to_latents_idx[tuple(image_spec)]
273 for i, caption in enumerate(captions):
274 iteration_order_by_caption_num[i].append((image_spec, latents_idx, caption, i))
275 iteration_order_list = []
276 for l in iteration_order_by_caption_num:
277 iteration_order_list.extend(l)
278 else:
279 iteration_order_list = []
280 for example in self.metadata_dataset.select_columns(['image_spec', 'caption']):
281 image_spec = example['image_spec']
282 captions = example['caption']
283 latents_idx = image_spec_to_latents_idx[tuple(image_spec)]
284 for i, caption in enumerate(captions):
285 iteration_order_list.append((image_spec, latents_idx, caption, i))
286 shuffle_with_seed(iteration_order_list, 42)
287
288 iteration_order_dict = defaultdict(list)
289 for image_spec, latents_idx, caption, caption_number in iteration_order_list:
290 iteration_order_dict['image_spec'].append(image_spec)

Callers

nothing calls this directly

Calls 2

_map_and_cacheFunction · 0.85
shuffle_with_seedFunction · 0.85

Tested by

no test coverage detected