code: def test_rearrange_examples():
      def test1(x):
          # transpose
          y = rearrange(x, "b c h w -> b h w c")
          assert tuple(y.shape) == (10, 30, 40, 20)
          return y
  
      def test2(x):
          # view / reshape
          y = rearrange(x, "b c h w -> b (c h w)")
          assert tuple(y.shape) == (10, 20 * 30 * 40)
          return y
  
      def test3(x):
          # depth-to-space
          y = rearrange(
              x, "b (c h1 w1) h w -> b c (h h1) (w w1)", h1=2, w1=2
          )
          assert tuple(y.shape) == (10, 5, 30 * 2, 40 * 2)
          return y
  
      def test4(x):
          # space-to-depth
          y = rearrange(
              x, "b c (h h1) (w w1) -> b (h1 w1 c) h w", h1=2, w1=2
          )
          assert tuple(y.shape) == (10, 20 * 4, 30 // 2, 40 // 2)
          return y
  
      def test5(x):
          # simple transposition
          y = rearrange(x, "b1 sound b2 letter -> b1 b2 sound letter")
          assert tuple(y.shape) == (10, 30, 20, 40)
          return y
  
      def test6(x):
          # parsing parameters
          t = rearrange(x, "b c h w -> (b h w) c")
          t = t[
              :, ::2
          ]  # replacement for dot-product, just changes size of second axis
          assert tuple(t.shape) == (10 * 30 * 40, 10)
  
          y = rearrange(
              t, "(b h w) c2 -> b c2 h w", **parse_shape(x, "b _ h w")
          )
          assert tuple(y.shape) == (10, 10, 30, 40)
          return y
  
      def test7(x):
          # split of embedding into groups
          y1, y2 = rearrange(x, "b (c g) h w -> g b c h w", g=2)
          assert tuple(y1.shape) == (10, 10, 30, 40)
          assert tuple(y2.shape) == (10, 10, 30, 40)
          return y1 + y2  # only one tensor is expected in output
  
      def test8(x):
          # max-pooling
          y = reduce(
              x,
              "b c (h h1) (w w1) -> b c h w",
              reduction="max",
              h1=2,
              w1=2,
          )
          assert tuple(y.shape) == (10, 20, 30 // 2, 40 // 2)
          return y
  
      def test9(x):
          # squeeze - unsqueeze
          y = reduce(x, "b c h w -> b c () ()", reduction="max")
          assert tuple(y.shape) == (10, 20, 1, 1)
          y = rearrange(y, "b c () () -> c b")
          assert tuple(y.shape) == (20, 10)
          return y
  
      def test10(x):
          # stack
          tensors = list(
              x + 0
          )  # 0 is needed https://github.com/tensorflow/tensorflow/issues/23185
          tensors = rearrange(tensors, "b c h w -> b h w c")
          assert tuple(tensors.shape) == (10, 30, 40, 20)
          return tensors
  
      def test11(x):
          # concatenate
          tensors = list(
              x + 0
          )  # 0 is needed https://github.com/tensorflow/tensorflow/issues/23185
          tensors = rearrange(tensors, "b c h w -> h (b w) c")
          assert tuple(tensors.shape) == (30, 10 * 40, 20)
          return tensors
  
      def shufflenet(x, convolve, c1, c2):
          # shufflenet reordering example
          x = convolve(x)
          x = rearrange(
              x, "b (c1 c2) h w-> b (c2 c1) h w", c1=c1, c2=c2
          )
          x = convolve(x)
          return x
  
      def convolve_strided_1d(x, stride, usual_convolution):
          x = rearrange(
              x, "b c t1 t2 -> b c (t1 t2)"
          )  # reduce dimensionality
          x = rearrange(
              x, "b c (t stride) -> (stride b) c t", stride=stride
          )
          x = usual_convolution(x)
          x = rearrange(
              x, "(stride b) c t -> b c (t stride)", stride=stride
          )
          return x
  
      def convolve_strided_2d(x, h_stride, w_stride, usual_convolution):
          x = rearrange(
              x,
              "b c (h hs) (w ws) -> (hs ws b) c h w",
              hs=h_stride,
              ws=w_stride,
          )
          x = usual_convolution(x)
          x = rearrange(
              x,
              "(hs ws b) c h w -> b c (h hs) (w ws)",
              hs=h_stride,
              ws=w_stride,
          )
          return x
  
      def unet_like_1d(x, usual_convolution):
          # u-net like steps for increasing / reducing dimensionality
          x = rearrange(
              x, "b c t1 t2 -> b c (t1 t2)"
          )  # reduce dimensionality
          y = rearrange(x, "b c (t dt) -> b (dt c) t", dt=2)
          y = usual_convolution(y)
          x = x + rearrange(y, "b (dt c) t -> b c (t dt)", dt=2)
          return x
  
      # mock for convolution (works for all backends)
      convolve_mock = lambda x: x
  
      tests = [
          test1,
          test2,
          test3,
          test4,
          test5,
          test6,
          test7,
          test8,
          test9,
          test10,
          test11,
          lambda x: shufflenet(x, convolve=convolve_mock, c1=4, c2=5),
          lambda x: convolve_strided_1d(
              x, stride=2, usual_convolution=convolve_mock
          ),
          lambda x: convolve_strided_2d(
              x, h_stride=2, w_stride=2, usual_convolution=convolve_mock
          ),
          lambda x: unet_like_1d(x, usual_convolution=convolve_mock),
      ]
  
      for backend in imp_op_backends:
          print("testing source_examples for ", backend.framework_name)
          for test in tests:
              x = numpy.arange(10 * 20 * 30 * 40).reshape(
                  [10, 20, 30, 40]
              )
              result1 = test(x)
              result2 = backend.to_numpy(test(backend.from_numpy(x)))
              assert numpy.array_equal(result1, result2)
  
              # now with strides
              x = numpy.arange(10 * 2 * 20 * 3 * 30 * 1 * 40).reshape(
                  [10 * 2, 20 * 3, 30 * 1, 40 * 1]
              )
              # known torch bug - torch doesn't support negative steps
              last_step = (
                  -1
                  if (
                      backend.framework_name != "torch"
                      and backend.framework_name != "oneflow"
                  )
                  else 1
              )
              indexing_expression = numpy.index_exp[
                  ::2, ::3, ::1, ::last_step
              ]
              result1 = test(x[indexing_expression])
              result2 = backend.to_numpy(
                  test(backend.from_numpy(x)[indexing_expression])
              )
              assert numpy.array_equal(result1, result2)
  
file_path: einops/tests/test_examples.py
imports: import numpy
  import pytest
  from einops import rearrange, parse_shape, reduce
  from tests import is_backend_tested
  from tests.test_ops import imp_op_backends
label_desc: Use image node_id 1 for calling a global function with example usage: test_rearrange_examples() and returns: y, y, y, y, y, y, unknown, y, y, tensors, tensors, x, x, x, x