Project: 041_Unlocking_the_Power_of_Function_Vectors_for_Charac | Function: add_function_vector (iclr)

system: You are an expert Python programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow Python best practices and maintain consistent code style.
Return **only** the complete Python function code.

user: Complete this Python function:

**DESCRIPTION:** Adds function vectors to model representations during inference to mitigate catastrophic forgetting

**KEY TERMS:** {
  "edit_layer": "layer l",
  "fv_vector": "θ0Te",
  "token_n": "token position",
  "degree": "intervention strength"
}

**CONTEXT CODE:**             kwargs['model'].save_pretrained(args.output_dir)

    def on_step_end(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
        if args.save_step:
            save_step = [20,150,350,1200,2400,4800,8000,16000]
            if self.curr_save_id < len(save_step) and abs(state.global_step-save_step[self.curr_save_id])<10:
                folder = os.path.join(args.output_dir, f"{PREFIX_CHECKPOINT_DIR}-{save_step[self.curr_save_id]}")
                print(f"save model at step {save_step[self.curr_save_id]} at {folder}")
                self._save(kwargs['model'], folder)
                self.curr_save_id += 1
        

    def on_epoch_end(self, args: TrainingArguments, state: TrainerState,
            control: TrainerControl, **kwargs):
        """ Save intermediate model adapters in case of interrupted training """
        
        print(f"on epoch {state.epoch} end ")
        # save_epoch = [5.0,10.0,15.0,20.0]
        # if self.curr_save_id < len(save_epoch) and abs(state.epoch-save_epoch[self.curr_save_id])<0.2:
        #     folder = os.path.join(args.output_dir, f"{PREFIX_CHECKPOINT_DIR}-{save_epoch[self.curr_save_id]}")
        #     print(f"save model at epoch {state.epoch} at {save_epoch[self.curr_save_id]}")
        #     self._save(kwargs['model'], folder)
        #     self.curr_save_id += 1
        
    def on_save(
        self,
        args: TrainingArguments,
        state: TrainerState,
        control: TrainerControl,
        **kwargs,
    ):
        checkpoint_folder = os.path.join(
            args.output_dir, f"{PREFIX_CHECKPOINT_DIR}-{state.epoch}"
        )       
        self._save(kwargs['model'], checkpoint_folder)

        # peft_model_path = os.path.join(checkpoint_folder, "adapter_model")
        # kwargs["model"].save_pretrained(peft_model_path)
        return control


def get_module(model, name):
    """
    Finds the named module within the given model.
    """
    for n, m in model.named_modules():
        if n == name:
            return m
    raise LookupError(name)

def add_function_vector(edit_layer, fv_vector, token_n, degree=1):
    """
    Adds a vector to the output of a specified layer in the model

    Parameters:
    edit_layer: the layer to perform the FV intervention
    fv_vector: the function vector to add as an intervention
    device: device of the model (cuda gpu or cpu)
    idx: the token index to add the function vector at

    Returns:
    add_act: a fuction specifying how to add a function vector to a layer's output hidden state
    """
    raise NotImplementedError("Function implementation has been masked for testing")

def skip_instructions(model, predictions_ids, tokenizer, ignore_idx=-100):
    predictions_ids = np.where(predictions_ids == ignore_idx, tokenizer.pad_token_id, predictions_ids)

    predictions = tokenizer.batch_decode(
        predictions_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
    )

    final_predictions = []

    for pred in predictions:
        flag = 1
        for answer_pre in ANSWER_PREFIX:
            if answer_pre in pred:
                splits = pred.split(answer_pre)
                final_predictions.append(splits[-1].strip())
                flag = 0
                break
        if flag == 1:
            final_predictions.append('')

    return final_predictions


Complete the function implementation replacing "raise NotImplementedError".