code: def test_inplace_on_tensor_that_does_not_require_grad(self, mode):
      # We don't do anything special (that is, we don't rebase history).
      # See NOTE [autograd fallback and in-place operations] for why
      with autograd_fallback_mode(mode):
          lib = Library(self.test_ns, "FRAGMENT")
  
          # Correct usage of (a!)
          lib.define("foo(Tensor(a!) self, Tensor other) -> Tensor(a!)")
  
          def foo_impl(x, y):
              x_d = x.detach()
              y = y.detach()
              x_d.add_(y)
              return x
  
          lib.impl("foo", foo_impl, "CPU")
          foo = self.get_op("foo")
  
          # Incorrect usage of (a!): user doesn't return tensor as-is
          lib.define("bar(Tensor(a!) self, Tensor other) -> Tensor(a!)")
  
          def bar_impl(x, y):
              x_d = x.detach()
              y = y.detach()
              x_d.add_(y)
              return x_d.clone()
  
          lib.impl("bar", bar_impl, "CPU")
          bar = self.get_op("bar")
  
          # User mutated input tensor but didn't return it.
          lib.define("baz(Tensor(a!) self, Tensor other) -> ()")
  
          def baz_impl(x, y):
              x_d = x.detach()
              y = y.detach()
              x_d.add_(y)
  
          lib.impl("baz", baz_impl, "CPU")
          baz = self.get_op("baz")
  
          # Test in-place on non-view
          for op in (foo, bar, baz):
              x = torch.randn(3)
              y = torch.randn(3, requires_grad=True)
              with self.assertRaisesRegex(
                  RuntimeError, "does not require grad"
              ):
                  z = x.clone()
                  op(z, y)
                  torch.autograd.grad(
                      z, y, torch.ones_like(z), allow_unused=True
                  )
  
          # Test in-place on view
          for op in (foo, bar, baz):
              x = torch.randn(3)
              y = torch.randn(3, requires_grad=True)
              with self.assertRaisesRegex(
                  RuntimeError, "does not require grad"
              ):
                  z = x[:]
                  op(z, y)
                  torch.autograd.grad(
                      z, x, torch.ones_like(z), allow_unused=True
                  )
  
imports: import contextlib
  import warnings
  import numpy
  import torch
  from torch.library import Library
  from torch.testing._internal.common_utils import instantiate_parametrized_tests, parametrize, run_tests, TestCase
label_desc: Use image node_id 12 for calling the TestAutogradFallback obj's underlying member method code with example usage: obj.test_inplace_on_tensor_that_does_not_require_grad(mode) and returns: x, x_d