code: def test_einsum_views(self):
      # pass-through
      for do_opt in [True, False]:
          a = np.arange(6)
          a = a.reshape(2, 3)
  
          b = np.einsum("...", a, optimize=do_opt)
          assert_(b.tensor._base is a.tensor)
  
          b = np.einsum(a, [Ellipsis], optimize=do_opt)
          assert_(b.base is a)
  
          b = np.einsum("ij", a, optimize=do_opt)
          assert_(b.base is a)
          assert_equal(b, a)
  
          b = np.einsum(a, [0, 1], optimize=do_opt)
          assert_(b.base is a)
          assert_equal(b, a)
  
          # output is writeable whenever input is writeable
          b = np.einsum("...", a, optimize=do_opt)
          assert_(b.flags["WRITEABLE"])
          a.flags["WRITEABLE"] = False
          b = np.einsum("...", a, optimize=do_opt)
          assert_(not b.flags["WRITEABLE"])
  
          # transpose
          a = np.arange(6)
          a.shape = (2, 3)
  
          b = np.einsum("ji", a, optimize=do_opt)
          assert_(b.base is a)
          assert_equal(b, a.T)
  
          b = np.einsum(a, [1, 0], optimize=do_opt)
          assert_(b.base is a)
          assert_equal(b, a.T)
  
          # diagonal
          a = np.arange(9)
          a.shape = (3, 3)
  
          b = np.einsum("ii->i", a, optimize=do_opt)
          assert_(b.base is a)
          assert_equal(b, [a[i, i] for i in range(3)])
  
          b = np.einsum(a, [0, 0], [0], optimize=do_opt)
          assert_(b.base is a)
          assert_equal(b, [a[i, i] for i in range(3)])
  
          # diagonal with various ways of broadcasting an additional dimension
          a = np.arange(27)
          a.shape = (3, 3, 3)
  
          b = np.einsum("...ii->...i", a, optimize=do_opt)
          assert_(b.base is a)
          assert_equal(b, [[x[i, i] for i in range(3)] for x in a])
  
          b = np.einsum(
              a, [Ellipsis, 0, 0], [Ellipsis, 0], optimize=do_opt
          )
          assert_(b.base is a)
          assert_equal(b, [[x[i, i] for i in range(3)] for x in a])
  
          b = np.einsum("ii...->...i", a, optimize=do_opt)
          assert_(b.base is a)
          assert_equal(
              b,
              [
                  [x[i, i] for i in range(3)]
                  for x in a.transpose(2, 0, 1)
              ],
          )
  
          b = np.einsum(
              a, [0, 0, Ellipsis], [Ellipsis, 0], optimize=do_opt
          )
          assert_(b.base is a)
          assert_equal(
              b,
              [
                  [x[i, i] for i in range(3)]
                  for x in a.transpose(2, 0, 1)
              ],
          )
  
          b = np.einsum("...ii->i...", a, optimize=do_opt)
          assert_(b.base is a)
          assert_equal(b, [a[:, i, i] for i in range(3)])
  
          b = np.einsum(
              a, [Ellipsis, 0, 0], [0, Ellipsis], optimize=do_opt
          )
          assert_(b.base is a)
          assert_equal(b, [a[:, i, i] for i in range(3)])
  
          b = np.einsum("jii->ij", a, optimize=do_opt)
          assert_(b.base is a)
          assert_equal(b, [a[:, i, i] for i in range(3)])
  
          b = np.einsum(a, [1, 0, 0], [0, 1], optimize=do_opt)
          assert_(b.base is a)
          assert_equal(b, [a[:, i, i] for i in range(3)])
  
          b = np.einsum("ii...->i...", a, optimize=do_opt)
          assert_(b.base is a)
          assert_equal(
              b, [a.transpose(2, 0, 1)[:, i, i] for i in range(3)]
          )
  
          b = np.einsum(
              a, [0, 0, Ellipsis], [0, Ellipsis], optimize=do_opt
          )
          assert_(b.base is a)
          assert_equal(
              b, [a.transpose(2, 0, 1)[:, i, i] for i in range(3)]
          )
  
          b = np.einsum("i...i->i...", a, optimize=do_opt)
          assert_(b.base is a)
          assert_equal(
              b, [a.transpose(1, 0, 2)[:, i, i] for i in range(3)]
          )
  
          b = np.einsum(
              a, [0, Ellipsis, 0], [0, Ellipsis], optimize=do_opt
          )
          assert_(b.base is a)
          assert_equal(
              b, [a.transpose(1, 0, 2)[:, i, i] for i in range(3)]
          )
  
          b = np.einsum("i...i->...i", a, optimize=do_opt)
          assert_(b.base is a)
          assert_equal(
              b,
              [
                  [x[i, i] for i in range(3)]
                  for x in a.transpose(1, 0, 2)
              ],
          )
  
          b = np.einsum(
              a, [0, Ellipsis, 0], [Ellipsis, 0], optimize=do_opt
          )
          assert_(b.base is a)
          assert_equal(
              b,
              [
                  [x[i, i] for i in range(3)]
                  for x in a.transpose(1, 0, 2)
              ],
          )
  
          # triple diagonal
          a = np.arange(27)
          a.shape = (3, 3, 3)
  
          b = np.einsum("iii->i", a, optimize=do_opt)
          assert_(b.base is a)
          assert_equal(b, [a[i, i, i] for i in range(3)])
  
          b = np.einsum(a, [0, 0, 0], [0], optimize=do_opt)
          assert_(b.base is a)
          assert_equal(b, [a[i, i, i] for i in range(3)])
  
          # swap axes
          a = np.arange(24)
          a.shape = (2, 3, 4)
  
          b = np.einsum("ijk->jik", a, optimize=do_opt)
          assert_(b.base is a)
          assert_equal(b, a.swapaxes(0, 1))
  
          b = np.einsum(a, [0, 1, 2], [1, 0, 2], optimize=do_opt)
          assert_(b.base is a)
          assert_equal(b, a.swapaxes(0, 1))
  
imports: import functools
  import itertools
  from unittest import expectedFailure, skipIf, SkipTest
  import torch._numpy
  from pytest import raises
  from torch._numpy.testing import assert_, assert_allclose, assert_almost_equal, assert_array_equal, assert_equal, suppress_warnings
  from torch.testing._internal.common_utils import instantiate_parametrized_tests, parametrize, run_tests, TestCase
label_desc: Use image node_id 2 for calling the TestEinsum obj's underlying member method code with example usage: obj.test_einsum_views() without return types