class_docstr: An offline evaluator for Contextual Bandits, based on the paper https://arxiv.org/pdf/1003.0146.pdf (Algorithm 3)
class_docstr_tok: ["An","offline","evaluator","for","Contextual","Bandits",",","based","on","the","paper","https",":","\/\/arxiv.org\/pdf\/1003.0146.pdf","(","Algorithm","3",")"]
code: def _aggregate_across_instances(self) -> None:
      # sum local values across all trainers, add to the global value
      # clone the tensors to avoid modifying them inplace
  
      sum_weight_accepted = sync_ddp_if_available(
          self.sum_weight_accepted_local.clone(),
          # pyre-fixme[6]: For 2nd argument expected `Union[None, str, ReduceOp]`
          #  but got `RedOpType`.
          reduce_op=ReduceOp.SUM,
      )
  
      sum_importance_weight_accepted = sync_ddp_if_available(
          self.sum_importance_weight_accepted_local.clone(),
          # pyre-fixme[6]: For 2nd argument expected `Union[None, str, ReduceOp]`
          #  but got `RedOpType`.
          reduce_op=ReduceOp.SUM,
      )
  
      sum_weight_all_data = sync_ddp_if_available(
          self.sum_weight_all_data_local.clone(),
          # pyre-fixme[6]: For 2nd argument expected `Union[None, str, ReduceOp]`
          #  but got `RedOpType`.
          reduce_op=ReduceOp.SUM,
      )
  
      sum_weight_rejected = sum_weight_all_data - sum_weight_accepted
  
      sum_reward_weighted_accepted = sync_ddp_if_available(
          self.sum_reward_weighted_accepted_local.clone(),
          # pyre-fixme[6]: For 2nd argument expected `Union[None, str, ReduceOp]`
          #  but got `RedOpType`.
          reduce_op=ReduceOp.SUM,
      )
  
      sum_reward_importance_weighted_accepted = sync_ddp_if_available(
          self.sum_reward_importance_weighted_accepted_local.clone(),
          # pyre-fixme[6]: For 2nd argument expected `Union[None, str, ReduceOp]`
          #  but got `RedOpType`.
          reduce_op=ReduceOp.SUM,
      )
  
      sum_reward_weighted_all_data = sync_ddp_if_available(
          self.sum_reward_weighted_all_data_local.clone(),
          # pyre-fixme[6]: For 2nd argument expected `Union[None, str, ReduceOp]`
          #  but got `RedOpType`.
          reduce_op=ReduceOp.SUM,
      )
      sum_reward_weighted_rejected = (
          sum_reward_weighted_all_data - sum_reward_weighted_accepted
      )
  
      sum_size_weighted_accepted = sync_ddp_if_available(
          self.sum_size_weighted_accepted_local.clone(),
          # pyre-fixme[6]: For 2nd argument expected `Union[None, str, ReduceOp]`
          #  but got `RedOpType`.
          reduce_op=ReduceOp.SUM,
      )
      sum_size_weighted_all_data = sync_ddp_if_available(
          self.sum_size_weighted_all_data_local.clone(),
          # pyre-fixme[6]: For 2nd argument expected `Union[None, str, ReduceOp]`
          #  but got `RedOpType`.
          reduce_op=ReduceOp.SUM,
      )
      sum_size_weighted_rejected = (
          sum_size_weighted_all_data - sum_size_weighted_accepted
      )
  
      # udpate the global cumulative sum buffers
      self.sum_reward_weighted_accepted += sum_reward_weighted_accepted
      self.sum_reward_importance_weighted_accepted += (
          sum_reward_importance_weighted_accepted
      )
      self.sum_weight_accepted += sum_weight_accepted
      self.sum_importance_weight_accepted += (
          sum_importance_weight_accepted
      )
      self.sum_weight_all_data += sum_weight_all_data
  
      # calcualte the metrics for window (since last aggregation across instances)
      self.frac_accepted = sum_weight_accepted / sum_weight_all_data
      self.avg_reward_accepted = (
          sum_reward_weighted_accepted / sum_weight_accepted
      )
      self.avg_reward_rejected = (
          sum_reward_weighted_rejected / sum_weight_rejected
      )
  
      self.avg_reward_all_data = (
          sum_reward_weighted_all_data / sum_weight_all_data
      )
  
      self.accepted_rejected_reward_ratio = (
          self.avg_reward_accepted / self.avg_reward_rejected
      )
  
      self.avg_size_accepted = (
          sum_size_weighted_accepted / sum_weight_accepted
      )
  
      self.avg_size_rejected = (
          sum_size_weighted_rejected / sum_weight_rejected
      )
  
      # reset local values to zero
      self.sum_reward_importance_weighted_accepted_local.zero_()
      self.sum_reward_weighted_accepted_local.zero_()
      self.sum_reward_weighted_all_data_local.zero_()
      self.sum_weight_accepted_local.zero_()
      self.sum_importance_weight_accepted_local.zero_()
      self.sum_weight_all_data_local.zero_()
      self.sum_size_weighted_accepted_local.zero_()
      self.sum_size_weighted_all_data_local.zero_()
  
file_path: ReAgent/reagent/evaluation/cb/policy_evaluator.py
imports: import logging
  import torch
  from pytorch_lightning.utilities.distributed import ReduceOp, sync_ddp_if_available
  from reagent.core.types import CBInput
  from reagent.evaluation.cb.base_evaluator import BaseOfflineEval
label_desc: Use image node_id 3 for calling the PolicyEvaluator obj's underlying member method code with example usage: obj._aggregate_across_instances() without return types