MCPcopy Create free account
hub / github.com/OpenGVLab/DragGAN / assert_shape

Function assert_shape

draggan/stylegan2/torch_utils/misc.py:80–93  ·  view source on GitHub ↗
(tensor, ref_shape)

Source from the content-addressed store, hash-verified

78# Performs symbolic assertion when used in torch.jit.trace().
79
80def assert_shape(tensor, ref_shape):
81 if tensor.ndim != len(ref_shape):
82 raise AssertionError(f'Wrong number of dimensions: got {tensor.ndim}, expected {len(ref_shape)}')
83 for idx, (size, ref_size) in enumerate(zip(tensor.shape, ref_shape)):
84 if ref_size is None:
85 pass
86 elif isinstance(ref_size, torch.Tensor):
87 with suppress_tracer_warnings(): # as_tensor results are registered as constants
88 symbolic_assert(torch.equal(torch.as_tensor(size), ref_size), f'Wrong size for dimension {idx}')
89 elif isinstance(size, torch.Tensor):
90 with suppress_tracer_warnings(): # as_tensor results are registered as constants
91 symbolic_assert(torch.equal(size, torch.as_tensor(ref_size)), f'Wrong size for dimension {idx}: expected {ref_size}')
92 elif size != ref_size:
93 raise AssertionError(f'Wrong size for dimension {idx}: got {size}, expected {ref_size}')
94
95#----------------------------------------------------------------------------
96# Function decorator that calls torch.autograd.profiler.record_function().

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected