code: def make_antialias_stage_2_functions(
      antialias_stage_2, bases, cuda, partitioned
  ):
      (
          aa_combinations,
          aa_zeroes,
          aa_n_reductions,
          aa_categorical,
      ) = antialias_stage_2
  
      # Accumulate functions.
      funcs = [
          _get_antialias_stage_2_combine_func(comb, zero, n_red, cat)
          for comb, zero, n_red, cat in zip(
              aa_combinations,
              aa_zeroes,
              aa_n_reductions,
              aa_categorical,
          )
      ]
  
      base_is_where = [b.is_where() for b in bases]
      next_base_is_where = base_is_where[1:] + [False]
  
      namespace = {}
      namespace["literal_unroll"] = literal_unroll
      for func in set(funcs):
          namespace[func.__name__] = func
  
      # Generator of unique names for combine functions
      names = (f"combine{i}" for i in count())
  
      # aa_stage_2_accumulate
      lines = [
          "def aa_stage_2_accumulate(aggs_and_copies, first_pass):",
          #    Don't need to accumulate if first_pass, just copy (opposite of aa_stage_2_copy_back)
          "    if first_pass:",
          "        for a in literal_unroll(aggs_and_copies):",
          "            a[1][:] = a[0][:]",
          "    else:",
      ]
      for i, (func, is_where, next_is_where) in enumerate(
          zip(funcs, base_is_where, next_base_is_where)
      ):
          if is_where:
              where_reduction = bases[i]
              if isinstance(where_reduction, by):
                  where_reduction = where_reduction.reduction
  
              combine = where_reduction._combine_callback(
                  cuda, partitioned, aa_categorical[i]
              )
              name = next(names)  # Unique name
              namespace[name] = combine
  
              lines.append(
                  f"        {name}(aggs_and_copies[{i}][::-1], aggs_and_copies[{i-1}][::-1])"
              )
          elif next_is_where:
              # This is dealt with as part of the following base which is a where reduction.
              pass
          else:
              lines.append(
                  f"        {func.__name__}(aggs_and_copies[{i}][1], aggs_and_copies[{i}][0])"
              )
      code = "\n".join(lines)
      logger.debug(code)
      exec(code, namespace)
      aa_stage_2_accumulate = ngjit(namespace["aa_stage_2_accumulate"])
  
      # aa_stage_2_clear
      if np.any(np.isnan(aa_zeroes)):
          namespace["nan"] = np.nan
  
      lines = ["def aa_stage_2_clear(aggs_and_copies):"]
      for i, aa_zero in enumerate(aa_zeroes):
          lines.append(f"    aggs_and_copies[{i}][0].fill({aa_zero})")
      code = "\n".join(lines)
      logger.debug(code)
      exec(code, namespace)
      aa_stage_2_clear = ngjit(namespace["aa_stage_2_clear"])
  
      # aa_stage_2_copy_back
      @ngjit
      def aa_stage_2_copy_back(aggs_and_copies):
          # Numba access to heterogeneous tuples is only permitted using literal_unroll.
          for agg_and_copy in literal_unroll(aggs_and_copies):
              agg_and_copy[0][:] = agg_and_copy[1][:]
  
      return (
          aa_stage_2_accumulate,
          aa_stage_2_clear,
          aa_stage_2_copy_back,
      )
  
imports: from __future__ import annotations
  from itertools import count
  import logging
  from typing import TYPE_CHECKING
  from toolz import unique, concat, pluck, get, memoize
  from numba import literal_unroll
  import numpy
  import xarray
  from .antialias import AntialiasCombination
  from .reductions import SpecialColumn, UsesCudaMutex, by, category_codes, summary
  from .utils import isnull, ngjit, nanmax_in_place, nanmin_in_place, nansum_in_place, nanfirst_in_place, nanlast_in_place, nanmax_n_in_place_3d, nanmax_n_in_place_4d, nanmin_n_in_place_3d, nanmin_n_in_place_4d, nanfirst_n_in_place_3d, nanfirst_n_in_place_4d, nanlast_n_in_place_3d, nanlast_n_in_place_4d, row_min_in_place, row_min_n_in_place_3d, row_min_n_in_place_4d, row_max_in_place, row_max_n_in_place_3d, row_max_n_in_place_4d
label_desc: Use image node_id 3 for calling a global function with example usage: make_antialias_stage_2_functions(antialias_stage_2, bases, cuda, partitioned) and returns: aa_stage_2_accumulate,  aa_stage_2_clear,  aa_stage_2_copy_back