MCPcopy Create free account
hub / github.com/google-deepmind/chex / assert_size

Function assert_size

chex/_src/asserts.py:389–448  ·  view source on GitHub ↗

Checks that the size of all inputs matches specified ``expected_sizes``. Valid usages include: .. code-block:: python assert_size(x, 1) # x is scalar (size 1) assert_size([x, y], (2, {1, 3})) # x has size 2, y has size 1 or 3 assert_size([x, y], (2, ...))

(
    inputs: Union[Scalar, Union[Array, Sequence[Array]]],
    expected_sizes: Union[_ai.TShapeMatcher,
                          Sequence[_ai.TShapeMatcher]])

Source from the content-addressed store, hash-verified

387
388@_static_assertion
389def assert_size(
390 inputs: Union[Scalar, Union[Array, Sequence[Array]]],
391 expected_sizes: Union[_ai.TShapeMatcher,
392 Sequence[_ai.TShapeMatcher]]) -> None:
393 """Checks that the size of all inputs matches specified ``expected_sizes``.
394
395 Valid usages include:
396
397 .. code-block:: python
398
399 assert_size(x, 1) # x is scalar (size 1)
400 assert_size([x, y], (2, {1, 3})) # x has size 2, y has size 1 or 3
401 assert_size([x, y], (2, ...)) # x has size 2, y has any size
402 assert_size([x, y], 1) # x and y are scalar (size 1)
403 assert_size((x, y), (5, 2)) # x has size 5, y has size 2
404
405 Args:
406 inputs: An array or a sequence of arrays.
407 expected_sizes: A sqeuence of expected sizes associated with each input,
408 where the expected size is a sequence of integer and `None` dimensions;
409 if all inputs have same size, a single size may be passed as
410 ``expected_sizes``.
411
412 Raises:
413 AssertionError: If the lengths of ``inputs`` and ``expected_sizes`` do not
414 match; if ``expected_sizes`` has wrong type; if size of ``input`` does
415 not match ``expected_sizes``.
416 """
417 # Ensure inputs and expected sizes are sequences.
418 if not isinstance(inputs, collections.abc.Sequence):
419 inputs = [inputs]
420
421 if isinstance(expected_sizes, int):
422 expected_sizes = [expected_sizes] * len(inputs)
423
424 if not isinstance(expected_sizes, (list, tuple)):
425 raise AssertionError(
426 "Error in size compatibility check: expected sizes should be an int, "
427 f"list, or tuple of ints, got {expected_sizes}.")
428
429 if len(inputs) != len(expected_sizes):
430 raise AssertionError(
431 "Length of `inputs` and `expected_sizes` must match: "
432 f"{len(inputs)} is not equal to {len(expected_sizes)}.")
433
434 errors = []
435 for idx, (x, expected) in enumerate(zip(inputs, expected_sizes)):
436 size = getattr(x, "size", 1) # scalars have size 1 by definition.
437 # Allow any size for the ellipsis case and allow handling of integer
438 # expected sizes or collection of acceptable expected sizes.
439 int_condition = expected in {Ellipsis, None} or size == expected
440 set_condition = (isinstance(expected, collections.abc.Collection) and
441 size in expected)
442 if not (int_condition or set_condition):
443 errors.append((idx, size, expected))
444
445 if errors:
446 msg = "; ".join(

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…