def create_eviction_policy(model_prob): """Creates the appropriate eviction policy for collecting data. Args: model_prob (float): the returned policy is a mixture of the learned eviction policy and the oracle policy, where the learned eviction policy is played model_prob of the time. Returns: EvictionPolicy """ # Need to update the eviction policy and keep the cache state around oracle_scorer = { "lru": eviction_policy.LRUScorer(), "belady": eviction_policy.BeladyScorer(trace), }[FLAGS.oracle_eviction_policy] learned_scorer = model_eviction_policy.LearnedScorer( eviction_model) # Use scoring_policy_index = 0 to always get scores from the oracle scorer scoring_policy_index = 0 if use_oracle_scores else None return eviction_policy.MixturePolicy( [ eviction_policy.GreedyEvictionPolicy(oracle_scorer), eviction_policy.GreedyEvictionPolicy(learned_scorer) ], [1 - model_prob, model_prob], scoring_policy_index=scoring_policy_index, )
def eviction_policy_from_config(config, trace): """Returns the EvictionPolicy specified by the config. Args: config (Config): config for the eviction policy. trace (MemoryTrace): memory trace to simulate on. Returns: EvictionPolicy """ def scorer_from_config(config, trace): """Creates an eviction_policy.CacheLineScorer from the config. Args: config (Config): config for the cache line scorer. trace (MemoryTrace): see get_eviction_policy. Returns: CacheLineScorer """ scorer_type = config.get("type") if scorer_type == "lru": return eviction_policy_mod.LRUScorer() elif scorer_type == "belady": return eviction_policy_mod.BeladyScorer(trace) elif scorer_type == "learned": with open(config.get("config_path"), "r") as model_config: return (model_eviction_policy_mod.LearnedScorer. from_model_checkpoint( cfg.Config.from_file(model_config), config.get("checkpoint"))) else: raise ValueError( "Invalid scorer type: {}".format(scorer_type)) policy_type = config.get("policy_type") if policy_type == "greedy": scorer = scorer_from_config(config.get("scorer"), trace) return eviction_policy_mod.GreedyEvictionPolicy( scorer, config.get("n", 0)) elif policy_type == "random": return eviction_policy_mod.RandomPolicy() elif policy_type == "mixture": subpolicies = [ eviction_policy_from_config(subconfig, trace) for subconfig in config.get("subpolicies") ] return eviction_policy_mod.MixturePolicy( subpolicies, config.get("weights")) else: raise ValueError("Invalid policy type: {}".format(policy_type))