code: def node2vec_sample(graph, nodes, max_depth, p=1.0, q=1.0):
      """Implement of node2vec random walk.
  
      Reference paper: https://cs.stanford.edu/~jure/pubs/node2vec-kdd16.pdf.
  
      Args:
          graph: A pgl graph instance
          nodes: Walk starting from nodes
          max_depth: Max walking depth
          p: Return parameter
          q: In-out parameter
  
      Return:
          A list of walks.
      """
      if p == 1.0 and q == 1.0:
          return deepwalk_sample(graph, nodes, max_depth)
  
      walk = []
      # init
      for node in nodes:
          walk.append([node])
  
      cur_walk_ids = np.arange(0, len(nodes))
      cur_nodes = np.array(nodes)
      prev_nodes = np.array([-1] * len(nodes), dtype="int64")
      prev_succs = np.array([[]] * len(nodes), dtype="int64")
      for l in range(max_depth):
          # select the walks not end
          cur_succs = graph.successor(cur_nodes)
  
          mask = [len(succ) > 0 for succ in cur_succs]
          if np.any(mask):
              cur_walk_ids = cur_walk_ids[mask]
              cur_nodes = cur_nodes[mask]
              prev_nodes = prev_nodes[mask]
              prev_succs = prev_succs[mask]
              cur_succs = cur_succs[mask]
          else:
              # stop when all nodes have no successor
              break
          num_nodes = cur_nodes.shape[0]
          nxt_nodes = np.zeros(num_nodes, dtype="int64")
  
          for idx, (succ, prev_succ, walk_id, prev_node) in enumerate(
              zip(cur_succs, prev_succs, cur_walk_ids, prev_nodes)
          ):
              sampled_succ = graph_kernel.node2vec_sample(
                  succ, prev_succ, prev_node, p, q
              )
              walk[walk_id].append(sampled_succ)
              nxt_nodes[idx] = sampled_succ
  
          prev_nodes, prev_succs = cur_nodes, cur_succs
          cur_nodes = nxt_nodes
      return walk
  
file_path: PGL/legacy/pgl/sample.py
imports: import time
  import copy
  import numpy
  import pgl
  from pgl.utils.logger import log
  from pgl import graph_kernel
label_desc: Use image node_id 8 for calling a global function with example usage: node2vec_sample(graph, nodes, max_depth, p, q) and returns: walk, deepwalk_sample