MCPcopy Create free account
hub / github.com/vladmandic/sdnext / validate_sample

Function validate_sample

modules/processing_helpers.py:327–356  ·  view source on GitHub ↗
(tensor)

Source from the content-addressed store, hash-verified

325
326
327def validate_sample(tensor):
328 t0 = time.time()
329 if not isinstance(tensor, np.ndarray) and not isinstance(tensor, torch.Tensor):
330 return tensor
331 dtype = tensor.dtype
332 if tensor.dtype == torch.bfloat16: # numpy does not support bf16
333 tensor = tensor.to(torch.float16)
334 if isinstance(tensor, torch.Tensor) and hasattr(tensor, 'detach'):
335 sample = tensor.detach().cpu().numpy()
336 elif isinstance(tensor, np.ndarray):
337 sample = tensor
338 else:
339 log.warning(f'Decode: type={type(tensor)} unknown sample')
340 return tensor
341 sample = 255.0 * sample
342 with warnings.catch_warnings(record=True) as w:
343 cast = sample.astype(np.uint8)
344 if len(w) > 0:
345 nans = np.isnan(sample).sum()
346 cast = np.nan_to_num(sample)
347 cast = cast.astype(np.uint8)
348 vae = shared.sd_model.vae.dtype if hasattr(shared.sd_model, 'vae') else None
349 upcast = getattr(shared.sd_model.vae.config, 'force_upcast', None) if hasattr(shared.sd_model, 'vae') and hasattr(shared.sd_model.vae, 'config') else None
350 log.error(f'Decode: sample={sample.shape} invalid={nans} dtype={dtype} vae={vae} upcast={upcast} failed to validate')
351 if upcast is not None and not upcast:
352 setattr(shared.sd_model.vae.config, 'force_upcast', True) # noqa: B010
353 log.info('Decode: set upcast=True and attempt to retry operation')
354 t1 = time.time()
355 timer.process.add('validate', t1 - t0)
356 return cast
357
358
359def decode_images(image):

Callers 1

process_samplesFunction · 0.85

Calls 3

infoMethod · 0.80
toMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected