code: def test_reduce_memory_consumption():
      x = shared(
          np.asarray(np.random.uniform(size=(10,)), dtype=config.floatX)
      )
      o, _ = pt_reduce(
          lambda v, acc: acc + v,
          x,
          pt.constant(np.asarray(0.0, dtype=config.floatX)),
      )
      mode = FAST_RUN
      mode = mode.excluding("inplace")
      f1 = function([], o, mode=mode)
      inputs, outputs = clone_optimized_graph(f1)
  
      scan_nodes = grab_scan_node(outputs[0])
      assert scan_nodes is not None
      scan_node = scan_nodes[0]
      f1 = function(inputs, scan_node.inputs[2])
  
      # Originally, the shape would have been 1 due to the SaveMem
      # optimization reducing the size to the number of taps (in this case
      # 1) provided to the inner function. Now, because of the memory-reuse
      # feature in Scan it can be 2 because SaveMem needs to keep a
      # larger buffer to avoid aliasing between the inputs and the outputs.
      if config.scan__allow_output_prealloc:
          assert f1().shape[0] == 2
      else:
          assert f1().shape[0] == 1
  
      gx = grad(o, x)
      f2 = function([], gx)
      utt.assert_allclose(f2(), np.ones((10,)))
  
file_path: pytensor/tests/scan/test_views.py
imports: import numpy
  import pytensor.tensor
  from pytensor import config, function, grad, shared
  from pytensor.compile.mode import FAST_RUN
  from pytensor.scan.views import foldl, foldr
  from pytensor.scan.views import map
  from pytensor.scan.views import reduce
  from pytensor.tensor.type import scalar, vector
  from tests import unittest_tools
  from tests.scan.test_basic import clone_optimized_graph, grab_scan_node
label_desc: Use image node_id 3 for calling a global function with example usage: test_reduce_memory_consumption() without return types