### Query Documentation Dynamically Source: https://zjunlp.gitbook.io/easyedit/installation Perform an HTTP GET request to the installation page with an 'ask' query parameter to get dynamic answers to your questions. The question should be specific and in natural language. ```http GET https://zjunlp.gitbook.io/easyedit/installation.md?ask= ``` -------------------------------- ### Example: Initialize HyperParams using YAML Source: https://zjunlp.gitbook.io/easyedit/easyeditor/hparams This example demonstrates how to initialize HyperParams using the `from_hparams` static method with a YAML configuration file for the MEMIT algorithm. ```python hparams = MEMITHyperParams.from_hparams('./hparams/MEMIT/llama-7b.yaml') ``` -------------------------------- ### Example Usage Source: https://zjunlp.gitbook.io/easyedit/easyeditor/hparams Example demonstrating how to initialize HyperParams using the `from_hparams` method with a YAML configuration file. ```APIDOC ## Example ```python hparams = MEMITHyperParams.from_hparams('./hparams/MEMIT/llama-7b.yaml') ``` ### Request Body Example (YAML) ```yaml alg_name: "MEMIT" model_name: "./hugging_cache/llama-2-7b" device: 0 layers: [4, 5, 6, 7, 8] clamp_norm_factor: 4 layer_selection: "all" fact_token: "subject_last" v_num_grad_steps: 25 v_lr: 5e-1 v_loss_layer: 31 v_weight_decay: 1e-3 kl_factor: 0.0625 mom2_adjustment: true mom2_update_weight: 15000 rewrite_module_tmp: "model.layers.{}.mlp.down_proj" layer_module_tmp: "model.layers.{}" mlp_module_tmp: "model.layers.{}.mlp" attn_module_tmp: "model.layers.{}.self_attn" ln_f_module: "model.norm" lm_head_module: "lm_head" mom2_dataset: "wikipedia" mom2_n_samples: 100000 mom2_dtype: "float32" ``` ``` -------------------------------- ### Pull EasyEdit Docker Image Source: https://zjunlp.gitbook.io/easyedit/installation Download the pre-built EasyEdit Docker image from Docker Hub or Aliyun. This is a convenient way to get started without local environment setup. ```bash docker pull nipelements/easyedit ``` ```bash docker pull registry.cn-hangzhou.aliyuncs.com/zjunlp/easyedit:v1 ``` -------------------------------- ### Query Documentation API Example Source: https://zjunlp.gitbook.io/easyedit/easyeditor/evaluate To get additional information not directly on the page, make a GET request to the page URL with an 'ask' query parameter containing your question. ```http GET https://zjunlp.gitbook.io/easyedit/easyeditor/evaluate.md?ask= ``` -------------------------------- ### Example YAML Configuration Source: https://zjunlp.gitbook.io/easyedit/easyeditor/hparams A sample YAML configuration file for hyperparameters, specifying algorithm name, model path, device, layer selection, and various training parameters. ```yaml alg_name: "MEMIT" model_name: "./hugging_cache/llama-2-7b" device: 0 layers: [4, 5, 6, 7, 8] clamp_norm_factor: 4 layer_selection: "all" fact_token: "subject_last" v_num_grad_steps: 25 v_lr: 5e-1 v_loss_layer: 31 v_weight_decay: 1e-3 kl_factor: 0.0625 mom2_adjustment: true mom2_update_weight: 15000 rewrite_module_tmp: "model.layers.{}.mlp.down_proj" layer_module_tmp: "model.layers.{}" mlp_module_tmp: "model.layers.{}.mlp" attn_module_tmp: "model.layers.{}.self_attn" ln_f_module: "model.norm" lm_head_module: "lm_head" mom2_dataset: "wikipedia" mom2_n_samples: 100000 mom2_dtype: "float32" ``` -------------------------------- ### Query Documentation via HTTP GET Source: https://zjunlp.gitbook.io/easyedit/easyeditor/editor Demonstrates how to perform an HTTP GET request to the documentation URL with an 'ask' query parameter to retrieve specific information dynamically. The question should be self-contained and in natural language. ```http GET https://zjunlp.gitbook.io/easyedit/easyeditor/editor.md?ask= ``` -------------------------------- ### Install EasyEdit using Pip Source: https://zjunlp.gitbook.io/easyedit/installation Clone the repository, create a conda environment with Python 3.9+, and install dependencies. Ensure you are using Python 3.9 or newer. ```shell git clone https://github.com/zjunlp/EasyEdit.git conda create -n EasyEdit python=3.9.7 ... pip install -r requirements.txt ``` -------------------------------- ### Query Documentation via GET Request Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/mend Shows how to query the documentation dynamically by appending an 'ask' query parameter to the page URL. ```http GET https://zjunlp.gitbook.io/easyedit/easyeditor/models/mend.md?ask= ``` -------------------------------- ### Example Usage of Serac Editor Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/serac Demonstrates how to initialize Serac hyperparameters, create an editor instance, and apply edits to a model using a list of prompts and their corresponding new targets. ```python hparams = SERACHyperaParams.from_hparams("llama-7b.yaml") editor = BaseEditor.from_hparams(hparams) prompts = ['What university did Watts Humphrey attend?', 'Which family does Ramalinaceae belong to', 'What role does Denny Herzig play in football?' ] target_new = ['University of Michigan', 'Lamiinae', 'winger' ] metrics, edited_model, _ = editor.edit( prompts=prompts, target_new=target_new, keep_original_weight=True ) ``` -------------------------------- ### Agent Instructions: Querying Documentation Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/rome Provides instructions on how to query the documentation dynamically using a GET request with an 'ask' query parameter. This is useful for retrieving information not explicitly present on the current page. ```http GET https://zjunlp.gitbook.io/easyedit/easyeditor/models/rome.md?ask= ``` -------------------------------- ### Query Documentation Dynamically Source: https://zjunlp.gitbook.io/easyedit/easyeditor/trainer Perform an HTTP GET request with the `ask` query parameter to retrieve specific information or clarifications from the documentation. Use this when the answer is not explicitly present or for additional context. ```http GET https://zjunlp.gitbook.io/easyedit/easyeditor/trainer.md?ask= ``` -------------------------------- ### Query Documentation Dynamically Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models Use this GET request to query the documentation dynamically when information is not explicitly present. The question should be specific and in natural language. ```http GET https://zjunlp.gitbook.io/easyedit/easyeditor/models.md?ask= ``` -------------------------------- ### Example of Applying MEND Edits Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/mend Demonstrates how to load MEND hyperparameters, initialize an editor, and apply edits to a model with specified prompts and targets. ```python // ... hparams = MENDHyperaParams.from_hparams("llama-7b.yaml") editor = BaseEditor.from_hparams(hparams) prompts = ['What university did Watts Humphrey attend?', 'Which family does Ramalinaceae belong to', 'What role does Denny Herzig play in football?' ] target_new = ['University of Michigan', 'Lamiinae', 'winger' ] metrics, edited_model, _ = editor.edit( prompts=prompts, target_new=target_new, keep_original_weight=True ) ``` -------------------------------- ### Example: Editing Model Prompts with MEMIT Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/memit This example demonstrates how to use the MEMIT editor to modify model responses for a list of prompts. It initializes hyperparameters, creates an editor instance, and then applies edits to change target outputs. ```python // ... hparams = MEMITHyperaParams.from_hparams("llama-7b.yaml") editor = BaseEditor.from_hparams(hparams) prompts = ['What university did Watts Humphrey attend?', 'Which family does Ramalinaceae belong to', 'What role does Denny Herzig play in football?' ] target_new = ['University of Michigan', 'Lamiinae', 'winger' ] metrics, edited_model, _ = editor.edit( prompts=prompts, target_new=target_new, keep_original_weight=True ) ``` -------------------------------- ### Example: Editing Model with IKE Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/ike Demonstrates how to use the IKE editor to modify a pre-trained model. It involves loading hyperparameters, initializing the editor, defining prompts and their new targets, and then applying the edits. ```python // ... hparams = IKEHyperaParams.from_hparams("llama-7b.yaml") editor = BaseEditor.from_hparams(hparams) prompts = ['What university did Watts Humphrey attend?', 'Which family does Ramalinaceae belong to', 'What role does Denny Herzig play in football?' ] target_new = ['University of Michigan', 'Lamiinae', 'winger' ] metrics, edited_model, _ = editor.edit( prompts=prompts, target_new=target_new, keep_original_weight=True ) ``` -------------------------------- ### ROME Model Editing Example Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/rome Demonstrates how to use the ROME editor to modify a language model. It involves loading hyperparameters, initializing the editor, defining prompts and new targets, and then applying the edits. ```python hparams = ROMEHyperaParams.from_hparams("llama-7b.yaml") editor = BaseEditor.from_hparams(hparams) prompts = ['What university did Watts Humphrey attend?', 'Which family does Ramalinaceae belong to', 'What role does Denny Herzig play in football?' ] target_new = ['University of Michigan', 'Lamiinae', 'winger' ] metrics, edited_model, _ = editor.edit( prompts=prompts, target_new=target_new, keep_original_weight=True ) ``` -------------------------------- ### Example Evaluation Metrics Source: https://zjunlp.gitbook.io/easyedit/easyeditor/evaluate This JSON structure represents the expected output format for evaluation metrics, including rewrite accuracy, rephrase accuracy, locality, and portability, for both pre- and post-editing states. ```json { "post": { "rewrite_acc": , "rephrase_acc": , "locality": { "YOUR_LOCALITY_KEY": , //... }, "portablility": { "YOUR_PORTABILITY_KEY": , //... }, }, "pre": { "rewrite_acc": , "rephrase_acc": , "portablility": { "YOUR_PORTABILITY_KEY": , //... }, } } ``` -------------------------------- ### Perform Batch Fact Editing with batch_edit() Source: https://zjunlp.gitbook.io/easyedit/easyeditor/editor Main function for batch fact editing. Accepts lists for 'prompts', 'target_new', and optional 'ground_truth'. Useful for applying edits to multiple examples simultaneously. ```python def batch_edit(self, prompts: List[str], target_new: List[str], ground_truth: Optional[List[str]] = None, rephrase_prompts: Optional[List[str]] = None, locality_prompts: Optional[List[str]] = None, locality_ground_truth: Optional[List[str]] = None, keep_original_weight=False, verbose=True, **kwargs ) ``` -------------------------------- ### Initialize BaseEditor with Hyperparameters Source: https://zjunlp.gitbook.io/easyedit/easyeditor/editor Static method to initialize the BaseEditor using provided hyperparameters. Ensure HyperParams are correctly configured before use. ```python def from_hparams(cls, hparams: HyperParams): return cls(hparams) ``` -------------------------------- ### Build and Run EasyEdit Docker Image Locally Source: https://zjunlp.gitbook.io/easyedit/installation Clone the EasyEdit repository, build a Docker image locally, and then run it as a container, mapping port 8080. This method is useful for development or customization. ```bash git clone https://github.com/zjunlp/EasyEdit.git cd EasyEdit docker build -t your-image-name . ``` ```bash docker run -p 8080:80 your-image-name ``` -------------------------------- ### Initialize HyperParams from JSON Source: https://zjunlp.gitbook.io/easyedit/easyeditor/hparams Use this static method to initialize hyperparameters from a JSON configuration file path. It takes the file path as input and returns a Hyperparams object. ```python def from_json(cls, fpath: str): ``` -------------------------------- ### Initialize HyperParams from YAML Source: https://zjunlp.gitbook.io/easyedit/easyeditor/hparams Use this static method to initialize hyperparameters from a YAML configuration file path or name. It takes the YAML path or name as input and returns a Hyperparams object. ```python def from_hparams(cls, hparams_name_or_path: str): ``` -------------------------------- ### Initialize MendRewriteExecutor Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/mend Initializes the MendRewriteExecutor with a pre-trained model, tokenizer, and MEND hyperparameters. ```python def init_model(self, model, tok, params: MENDHyperParams) ``` -------------------------------- ### HyperParams Initialization from YAML Source: https://zjunlp.gitbook.io/easyedit/easyeditor/hparams Initializes HyperParams object from a YAML configuration file. This is a static method. ```APIDOC ## from_hparams() ### Description Static method, hyperparams can be initialized through `yaml`. ### Method Signature ```python def from_hparams(cls, hparams_name_or_path: str): ``` ### Parameters #### Path Parameters - **hparams_name_or_path** (Str) - Required - yaml config path for editing ### Return Type - **hparams** (Hyperparams) - The hyperparams class for editing ``` -------------------------------- ### HyperParams Initialization from JSON Source: https://zjunlp.gitbook.io/easyedit/easyeditor/hparams Initializes HyperParams object from a JSON configuration file. This is a static method. ```APIDOC ## from_json() ### Description Static method, hyperparams can be initialized through `json`. ### Method Signature ```python def from_json(cls, fpath: str): ``` ### Parameters #### Path Parameters - **fpath** (Str) - Required - config path for editing ### Return Type - **hparams** (Hyperparams) - The hyperparams class for editing ``` -------------------------------- ### Initialize Serac Model Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/serac Loads the trained MEND model for Serac editing. Requires the model, tokenizer, and Serac-specific hyperparameters. ```python init_model(self, model, tok, params: SERACHparams) ``` -------------------------------- ### Edit Model with EasyEditor Source: https://zjunlp.gitbook.io/easyedit/easyeditor/editor Initializes the MEND model hyperparameters and the BaseEditor, then performs an edit operation with specified prompts and ground truth. Ensure MENDHyperParams and BaseEditor are imported. ```python hparams = MENDHyperParams.from_hparams('./hparams/MEND/gpt2-xl') editor = BaseEditor.from_hparams(hparams) metrics, edited_model, weight_copy= editor.edit( prompts='What university did Watts Humphrey attend?' if prompts is None else prompts, ground_truth='Illinois Institute of Technology' if ground_truth is None else ground_truth, target_new='University of Michigan' if target_new is None else target_new, keep_original_weight=True, ) ``` -------------------------------- ### BaseEditor.from_hparams Source: https://zjunlp.gitbook.io/easyedit/easyeditor/editor Initializes a BaseEditor instance using hyperparameters. This is a static method used for creating editor objects. ```APIDOC ## BaseEditor.from_hparams() ### Description Static method to initialize an editor instance through hyperparameters. ### Method Signature `from_hparams(cls, hparams: HyperParams) -> BaseEditor` ### Parameters * **hparams** (HyperParams): Hyperparameters for the editing method. ### Return Type * **editor** (BaseEditor): The Editor class defined by `hparams`. ``` -------------------------------- ### SeracRewriterExecutor.init_model Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/serac Loads the trained MEND model for SERAC editing. ```APIDOC ## init_model ### Description Loads the trained MEND model. ### Method Signature `init_model(self, model, tok, params: SERACHparams)` ### Parameters * **model** (PreTrainedModel) - The model to be edited. * **tok** (PreTrainedTokenizer) - The tokenizer for inputs. * **params** (SERACHparams) - Hyperparameters for the editing method. ``` -------------------------------- ### MendRewriteExecutor.init_model Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/mend Initializes the MEND model by setting up the necessary components for applying edits. It requires a pre-trained model, its tokenizer, and MEND-specific hyperparameters. ```APIDOC ## init_model() ### Description Initializes the MEND model. Requires a pre-trained specific model structure and weights (use Trainer). ### Method `init_model(self, model, tok, params: MENDHyperParams)` ### Parameters * **model** (PreTrainedModel) - The model to be edited. * **tok** (PreTrainedTokenizer) - The tokenizer for inputs. * **params** (MENDHyperParams) - Hyperparameters for the editing method. ``` -------------------------------- ### Execute ROME Update Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/rome Executes the ROME update algorithm for a specified edit request. It takes the model, tokenizer, request details, and hyperparameters as input, returning the computed delta weights. ```python def execute_rome( model: AutoModelForCausalLM, tok: AutoTokenizer, request: Dict, hparams: ROMEHyperParams, ) -> Dict[str, Tuple[torch.Tensor]]: ``` -------------------------------- ### Encode IKE Facts with SentenceTransformer Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/ike Encodes sentences from a development dataset using a SentenceTransformer model to prepare them for in-context demonstration retrieval. The resulting dense embeddings are stored in pickle format. ```python def encode_ike_facts( sentence_model: SentenceTransformer, ds: Dataset, hparams: IKEHyperParams ): ``` -------------------------------- ### Perform Fact Editing with edit() Source: https://zjunlp.gitbook.io/easyedit/easyeditor/editor Main function for single-instance fact editing. Use 'prompts' and 'target_new' to define the edit. 'ground_truth' can be optional. Consider 'keep_original_weight' for sequential editing. ```python def edit(self, prompts: Union[str, List[str]], target_new: Union[str, List[str]], ground_truth: Optional[Union[str, List[str]]], rephrase_prompts: Optional[Union[str, List[str]]] = None, locality_inputs: Optional[Dict] = None, portability_inputs: Optional[Dict] = None, keep_original_weight=False, verbose=True, **kwargs ) ``` -------------------------------- ### Apply MEMIT to Model Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/memit This is the main function to apply MEMIT to a model. It takes the model, tokenizer, requests, and hyperparameters, and returns the edited model weights. The `keep_original_weight` parameter controls sequential editing behavior. ```python def apply_memit_to_model( self, model: AutoModelForCausalLM, tok: AutoTokenizer, requests: List[Dict], hparams: MEMITHyperParams, copy=False, return_orig_weights=False, keep_original_weight=False, **kwargs ): ``` -------------------------------- ### Execute MEMIT Update Algorithm Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/memit This function executes the MEMIT update algorithm for a specified edit request and hyperparameters. It returns the new delta weights for the model. ```python def execute_memit( model: AutoModelForCausalLM, tok: AutoTokenizer, request: List[Dict], hparams: MEMITHyperParams, ) -> Dict[str, Tuple[torch.Tensor]]: ``` -------------------------------- ### execute_memit Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/memit Executes the MEMIT update algorithm for specified edits and layers. ```APIDOC ## execute_memit() ### Description Execute the MEMIT update algorithm for the specified update at the specified layers. ### Parameters * **model** (PreTrainedModel): The model to be edited. * **tok** (PreTrainedTokenizer): The tokenizer for inputs. * **requests** (List[Dict]): The edit descriptors and targets. * **hparams** (Hyperparams): Hyperparameters for the editing method. ### Return Type * **delta** (Dict[str, Tuple[torch.Tensor]]): New delta weights. ``` -------------------------------- ### compute_locality_quality Source: https://zjunlp.gitbook.io/easyedit/easyeditor/evaluate Computes the locality quality. The input-output format is the same as `compute_rewrite_or_rephrase_quality`. It compares whether the output tokens before and after editing are the same and calculates the average accuracy rate. ```APIDOC ## compute_locality_quality() ### Description Computes the locality quality. The input-output format is the same as `compute_rewrite_or_rephrase_quality`. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters * **model** (PreTrainedModel): model to be edited * **model_name** (Str): model_name_or_path * **hparams** (HyperParams): hyperparameters for editing method * **tok** (PreTrainedTokenizer): tokenizer for inputs * **prompt** (Str): the edit descriptor * **target_new** (Str): the edit target * **device** (Any): the device to run the computation on * **test_rephrase** (bool): whether to evalute the rephrase prompt (For Generalization) ### Return Type * metrics (Dict): model weights after editing ### Example ```json { "post": { "rewrite_acc": , "rephrase_acc": , "locality": { "YOUR_LOCALITY_KEY": , //... }, "portablility": { "YOUR_PORTABILITY_KEY": , //... }, }, "pre": { "rewrite_acc": , "rephrase_acc": , "portablility": { "YOUR_PORTABILITY_KEY": , //... }, } } ``` ``` -------------------------------- ### Compute Locality Quality Function Signature Source: https://zjunlp.gitbook.io/easyedit/easyeditor/evaluate The input-output format for this function is the same as `compute_rewrite_or_rephrase_quality`. It calculates average accuracy by comparing output tokens before and after editing. ```python def compute_locality_quality() -> Dict: ``` -------------------------------- ### Compute Rewrite or Rephrase Quality Function Signature Source: https://zjunlp.gitbook.io/easyedit/easyeditor/evaluate Use this function for evaluating 'Reliability' and 'Generalization'. It requires model, tokenizer, and editing hyperparameters. ```python def compute_rewrite_or_rephrase_quality( model, model_name, hparams: HyperParams, tok: AutoTokenizer, prompt: str, target_new: str, device, test_rephrase: bool = False ) -> typing.Dict: ``` -------------------------------- ### Apply IKE to a Pre-Trained Model Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/ike Applies the IKE editing method to a pre-trained causal language model. This function takes a model, tokenizer, edit requests, and hyperparameters to modify the model's behavior based on in-context learning. ```python def apply_ike_to_model( self, model: AutoModelForCausalLM, tok: AutoTokenizer, requests: List[Dict], hparams: IKEHyperParams, copy=False, return_orig_weights=False, keep_original_weight=False, **kwargs ): ``` -------------------------------- ### BaseEditor.batch_edit Source: https://zjunlp.gitbook.io/easyedit/easyeditor/editor Performs fact editing on the model for multiple prompts and targets in a batch. Returns metrics and the edited model. ```APIDOC ## BaseEditor.batch_edit() ### Description Main function to perform fact editing for multiple inputs according to the selected editing method. It returns editing metrics and the edited model. ### Method Signature `batch_edit(self, prompts: List[str], target_new: List[str], ground_truth: Optional[List[str]] = None, rephrase_prompts: Optional[List[str]] = None, locality_prompts: Optional[List[str]] = None, locality_ground_truth: Optional[List[str]] = None, keep_original_weight: bool = False, verbose: bool = True, **kwargs) -> List[Dict]` ### Parameters * **prompts** (List[str]): A list of prompt strings for the edit descriptors. * **target_new** (List[str]): A list of prompt strings for the edit targets. * **ground_truth** (Optional[List[str]]): A list of original model outputs for the edit descriptors. Can be set to `None`. * **rephrase_prompts** (Optional[List[str]]): A list of rephrased prompt strings, semantically similar to `prompts`, used to test generalization. * **locality_prompts** (Optional[List[str]]): A list of prompts for testing locality. * **locality_ground_truth** (Optional[List[str]]): A list of ground truth values corresponding to `locality_prompts`. * **keep_original_weight** (bool): Determines if edits are sequential. `False` edits sequentially (original weight not maintained), `True` does not. * **verbose** (bool): If `True`, prints intermediate output. ### Return Type * **metrics** (List[Dict]): Metrics for model editing. Refer to the provided link for details. * **edited_model** (PreTrainedModel): Model weights after editing. ``` -------------------------------- ### apply_memit_to_model Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/memit Applies MEMIT to a model given a request, returning the changed model weights. ```APIDOC ## apply_memit_to_model() ### Description Given the request, it applies MEMIT to your model. Returns the changed weights of the model. ### Parameters * **self** * **model** (PreTrainedModel): The model to be edited. * **tok** (PreTrainedTokenizer): The tokenizer for inputs. * **requests** (List[Dict]): The edit descriptors and targets. * **hparams** (MEMITHyperParams): Hyperparameters for the editing method. * **copy** (bool, optional): Whether to copy the original model. Defaults to False. * **return_orig_weights** (bool, optional): Whether to return the weights of the original model. Defaults to False. * **keep_original_weight** (bool, optional): Whether to edit sequentially. Defaults to False. * `False`: edit sequentially (because the original weight is not maintained after each edit). * `True`: not edit sequentially. * **kwargs ### Return Type * **edited_model** (PreTrainedModel): Model weights after editing. ``` -------------------------------- ### apply_ike_to_model Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/ike Applies IKE edits to a pre-trained causal language model using provided requests and hyperparameters. ```APIDOC ## apply_ike_to_model ### Description Main function to apply IKE to a model. Utilizes the preceding prompt to modify the behavior of the model. ### Method `apply_ike_to_model(self, model: AutoModelForCausalLM, tok: AutoTokenizer, requests: List[Dict], hparams: IKEHyperParams, copy=False, return_orig_weights=False, keep_original_weight=False, **kwargs)` ### Parameters #### Parameters - **model** (PreTrainedModel) - Required - The model to be edited. - **tok** (PreTrainedTokenizer) - Required - The tokenizer for inputs. - **requests** (List[Dict]) - Required - The edit descriptors and targets. - **hparams** (Hyperparams) - Required - Hyperparameters for the editing method. - **copy** (bool) - Optional - Whether to copy the original model. - **return_orig_weights** (bool) - Optional - Whether to return the weights of the original model. - **keep_original_weight** (bool) - Optional - Whether to edit sequentially. `False` edits sequentially; `True` does not edit sequentially. ### Returns - **edited_model** (PreTrainedModel) - The model weights after editing. ``` -------------------------------- ### Apply ROME to Model Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/rome Applies the ROME editing algorithm to a given model based on a list of requests. This function modifies the model's weights and can optionally return original weights or maintain sequential editing. ```python def apply_rome_to_model( self, model: AutoModelForCausalLM, tok: AutoTokenizer, requests: List[Dict], hparams: ROMEHyperParams, copy=False, return_orig_weights=False, keep_original_weight=False, **kwargs ): ``` -------------------------------- ### BaseEditor.edit Source: https://zjunlp.gitbook.io/easyedit/easyeditor/editor Performs fact editing on the model based on the provided prompts and target. It returns metrics and the edited model weights. ```APIDOC ## BaseEditor.edit() ### Description Main function to perform fact editing according to the selected editing method. It returns editing metrics and the edited model. ### Method Signature `edit(self, prompts: Union[str, List[str]], target_new: Union[str, List[str]], ground_truth: Optional[Union[str, List[str]]] = None, rephrase_prompts: Optional[Union[str, List[str]]] = None, locality_inputs: Optional[Dict] = None, portability_inputs: Optional[Dict] = None, keep_original_weight: bool = False, verbose: bool = True, **kwargs) -> List[Dict]` ### Parameters * **prompts** (Union[str, List[str]]): The prompt string of the edit descriptor. * **target_new** (Union[str, List[str]]): The prompt string of the edit target. * **ground_truth** (Optional[Union[str, List[str]]]): The original model output of the edit descriptor. Can be set to `None`. * **rephrase_prompts** (Optional[Union[str, List[str]]]): A rephrased prompt string, semantically similar to `prompts`, used to test generalization. * **locality_inputs** (Optional[Dict]): Dictionary containing prompts and their corresponding ground truths to test locality. * **portability_inputs** (Optional[Dict]): Dictionary similar to `locality_inputs`, used to test portability. * **keep_original_weight** (bool): Determines if edits are sequential. `False` edits sequentially (original weight not maintained), `True` does not. * **verbose** (bool): If `True`, prints intermediate output. ### Return Type * **metrics** (List[Dict]): Metrics for model editing. Refer to the provided link for details. * **edited_model** (PreTrainedModel): Model weights after editing. ``` -------------------------------- ### apply_rome_to_model() Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/rome Applies the ROME editing algorithm to a given model based on a list of requests. This function returns the modified model weights. ```APIDOC ## apply_rome_to_model() ### Description Given the request, it applies ROME to your model. Return the changed weights of the model. ### Signature ```python def apply_rome_to_model( self, model: AutoModelForCausalLM, tok: AutoTokenizer, requests: List[Dict], hparams: ROMEHyperParams, copy=False, return_orig_weights=False, keep_original_weight=False, **kwargs ): ``` ### Parameters * **model** (PreTrainedModel): The model to be edited. * **tok** (PreTrainedTokenizer): The tokenizer for inputs. * **requests** (List[Dict]): The edit descriptors and targets. * **hparams** (Hyperparams): Hyperparameters for the editing method. * **copy** (bool): Whether to copy the original model. * **return_orig_weights** (bool): Whether to return the weights of the original model. * **keep_original_weight** (bool): Whether to edit sequentially. * `False`: Edit sequentially (because the original weight is not maintained after each edit). * `True`: Not edit sequentially. ### Return Type * **edited_model** (PreTrainedModel): Model weights after editing. ``` -------------------------------- ### Apply Serac Edits to Model Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/serac Applies Serac edits to a given model based on a list of requests. Can optionally copy the original model and control the return of original weights. ```python def apply_to_model( self, model: AutoModelForCausalLM, tok: AutoTokenizer, requests: List[Dict], hparams: SERACHparams, copy=False, return_orig_weights=False, keep_original_weight=False, **kwargs ): ``` -------------------------------- ### Apply MEND Edits to Model Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/mend Applies MEND edits to a language model based on a list of requests. Can optionally return original weights or keep them after editing. ```python def apply_to_model( self, model: AutoModelForCausalLM, tok: AutoTokenizer, requests: List[Dict], hparams: MENDHyperParams, copy=False, return_orig_weights=False, keep_original_weight=False, **kwargs ): ``` -------------------------------- ### execute_rome() Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/rome Executes the ROME update algorithm for a specified update at a given layer. This function modifies the model's weights based on the provided request and hyperparameters. ```APIDOC ## execute_rome() ### Description Execute the ROME update algorithm for the specified update at the specified layer. ### Signature ```python def execute_rome( model: AutoModelForCausalLM, tok: AutoTokenizer, request: Dict, hparams: ROMEHyperParams, ) -> Dict[str, Tuple[torch.Tensor]]: ``` ### Parameters * **model** (PreTrainedModel): The model to be edited. * **tok** (PreTrainedTokenizer): The tokenizer for inputs. * **requests** (List[Dict]): The edit descriptors and targets. * **hparams** (Hyperparams): Hyperparameters for the editing method. ### Return Type * **delta** (Dict[str, Tuple[torch.Tensor]>): New delta weights. ``` -------------------------------- ### encode_ike_facts Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/ike Encodes sentences from a dataset using a sentence transformer model to retrieve nearest neighbors for in-context demonstrations. ```APIDOC ## encode_ike_facts ### Description Encodes samples in the dev set to retrieve nearest neighbors as in-context demonstrations. ### Parameters #### Parameters - **sentence_model** (SentenceTransformer) - Required - The model to encode demonstrations. Defaults to 'all-MiniLM-L6-v2'. - **ds** (Dataset) - Required - The dev set, used as a corpus for retrieving demonstrations. - **hparams** (Hyperparams) - Required - Hyperparameters for the editing method. ### Returns Stores dense embeddings in the form of pickle. ``` -------------------------------- ### SeracRewriterExecutor.apply_to_model Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/serac Main function to apply SERAC edits to a model based on provided requests and hyperparameters. ```APIDOC ## apply_to_model ### Description Main function: Given the request, it applies SERAC to your model. Returns the changed weights of the model. ### Method Signature `apply_to_model(self, model: AutoModelForCausalLM, tok: AutoTokenizer, requests: List[Dict], hparams: SERACHparams, copy=False, return_orig_weights=False, keep_original_weight=False, **kwargs)` ### Parameters * **model** (AutoModelForCausalLM) - The model to be edited. * **tok** (AutoTokenizer) - The tokenizer for inputs. * **requests** (List[Dict]) - The edit descriptors and targets. * **hparams** (SERACHparams) - Hyperparameters for editing method. * **copy** (bool) - Whether to copy the original model. Defaults to `False`. * **return_orig_weights** (bool) - Whether to return the weights of the original model. Defaults to `False`. * **keep_original_weight** (bool) - Whether to edit sequentially. If `False`, edits sequentially as the original weight is not maintained after each edit. If `True`, does not edit sequentially. Defaults to `False`. ### Return Type * **edited_model** (PreTrainedModel) - Model weights after editing. ``` -------------------------------- ### compute_rewrite_or_rephrase_quality Source: https://zjunlp.gitbook.io/easyedit/easyeditor/evaluate Evaluation method for Reliability and Generalization. It computes the token with the highest probability at each position and compares it with the ground truth to ascertain the mean accuracy of the probabilistic model. ```APIDOC ## compute_rewrite_or_rephrase_quality() ### Description Evaluation method for `Reliability` and `Generalization`. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters * **model** (PreTrainedModel): model to be edited * **model_name** (Str): model_name_or_path * **hparams** (HyperParams): hyperparameters for editing method * **tok** (PreTrainedTokenizer): tokenizer for inputs * **prompt** (Str): the edit descriptor * **target_new** (Str): the edit target * **device** (Any): the device to run the computation on * **test_rephrase** (bool): whether to evalute the rephrase prompt (For Generalization) ### Return Type * metrics (Dict): model weights after editing ``` -------------------------------- ### MendRewriteExecutor.apply_to_model Source: https://zjunlp.gitbook.io/easyedit/easyeditor/models/mend Applies MEND edits to the provided model. This is the main function for performing model editing, returning the modified model weights. ```APIDOC ## apply_to_model() ### Description Main function: Given the request, it applies mend to your model. Returns the changed weights of the model. ### Method `apply_to_model(self, model: AutoModelForCausalLM, tok: AutoTokenizer, requests: List[Dict], hparams: MENDHyperParams, copy=False, return_orig_weights=False, keep_original_weight=False, **kwargs)` ### Parameters * **model** (AutoModelForCausalLM) - The model to be edited. * **tok** (AutoTokenizer) - The tokenizer for inputs. * **requests** (List[Dict]) - The edit descriptors and targets. * **hparams** (MENDHyperParams) - Hyperparameters for the editing method. * **copy** (bool) - Whether to copy the original model. Defaults to False. * **return_orig_weights** (bool) - Whether to return the weights of the original model. Defaults to False. * **keep_original_weight** (bool) - Whether to edit sequentially. Defaults to False. * `False`: edit sequentially (because the original weight is not maintained after each edit). * `True`: not edit sequentially. ### Return Type * **edited_model** (PreTrainedModel) - Model weights after editing. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.