### Install EasyEdit Source: https://github.com/zjunlp/easyedit/blob/main/tutorial-notebooks/EasyEdit_Example_ROME_llama.ipynb Install the EasyEdit library to use its functionalities. This is a prerequisite for running the examples. ```bash pip install "easyedit" ``` -------------------------------- ### Install EasyEdit Source: https://github.com/zjunlp/easyedit/blob/main/tutorial-notebooks/EasyEdit_Example_ROME_llama.ipynb Install the EasyEdit library to use its functionalities. This is a prerequisite for running the examples. ```bash pip install "easyedit @ git+https://github.com/sapphir-ai/easyedit.git" ``` -------------------------------- ### Install EasyEdit Source: https://github.com/zjunlp/easyedit/blob/main/tutorial-notebooks/EasyEdit_Example_ROME_llama.ipynb Install the EasyEdit library to use its functionalities. This is a prerequisite for running the examples. ```bash pip install "easyedit @ git+https://github.com/demonstrate/easyedit.git@main" ``` -------------------------------- ### Install EasyEdit Source: https://github.com/zjunlp/easyedit/blob/main/tutorial-notebooks/EasyEdit_Example_ROME_llama.ipynb Install the EasyEdit library using pip. This is a prerequisite for running the examples. ```bash pip install easyedit ``` -------------------------------- ### Full Training Example Source: https://github.com/zjunlp/easyedit/blob/main/examples/MMEdit.md A complete example demonstrating the setup and execution of a training process using SERACMultimodalTrainingHparams, CaptionDataset, and MultimodalTrainer. ```python training_hparams = SERACMultimodalTrainingHparams.from_hparams('hparams/TRAINING/SERAC/minigpt4.yaml') train_ds = CaptionDataset('data/caption_train_edit.json', config=training_hparams) eval_ds = CaptionDataset('data/caption_eval_edit.json', config=training_hparams) trainer = MultimodalTrainer( config=hparams, train_set=train_ds, val_set=eval_ds ) trainer.run() ``` -------------------------------- ### Install necessary libraries Source: https://github.com/zjunlp/easyedit/blob/main/easyeditor/models/melo/peft_egg/docs/source/task_guides/clm-prompt-tuning.mdx Installs the required libraries for PEFT, transformers, and datasets. Use this command before starting the setup. ```bash !pip install -q peft transformers datasets ``` -------------------------------- ### Install EasyEdit Source: https://github.com/zjunlp/easyedit/blob/main/tutorial-notebooks/EasyEdit_Example_ROME_llama.ipynb Install the EasyEdit library to enable knowledge editing functionalities. This is a prerequisite for running the examples. ```bash pip install "easyedit @ git+https://github.com/ds4dp/easyedit.git@main" ``` -------------------------------- ### Prepare Runtime Environment Source: https://github.com/zjunlp/easyedit/blob/main/tutorial-notebooks/EasyEdit_Example_ROME.ipynb This section outlines the initial setup required for the EasyEdit library, including necessary installations and environment configurations. ```python import os import sys import torch from transformers import AutoModelForCausalLM, AutoTokenizer # Set environment variables for distributed training if needed # os.environ["WORLD_SIZE"] = "1" # os.environ["RANK"] = "0" # os.environ["LOCAL_RANK"] = "0" # os.environ["NODE_RANK"] = "0" # os.environ["MASTER_ADDR"] = "localhost" # os.environ["MASTER_PORT"] = "29500" # Add project root to Python path # sys.path.append(os.path.abspath(os.path.join(os.getcwd(), "../../"))) # Import EasyEdit library # from easyedit import EditingModel # Define model and tokenizer names MODEL_NAME = "gpt2-medium" # Load model and tokenizer # model = AutoModelForCausalLM.from_pretrained(MODEL_NAME) # tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) # Optional: Move model to GPU if available # if torch.cuda.is_available(): # model = model.cuda() # print(f"Model {MODEL_NAME} loaded successfully.") ``` -------------------------------- ### Install EasyEdit Source: https://github.com/zjunlp/easyedit/blob/main/tutorial-notebooks/EasyEdit_Example_ROME_InternLM.ipynb Install the EasyEdit library to enable knowledge editing functionalities. This is a prerequisite for running the examples. ```python import sys import os # Add the parent directory to the Python path sys.path.append(os.path.abspath(os.path.join(os.getcwd(), '..'))) ``` -------------------------------- ### Setup Environment for EasyEdit Source: https://github.com/zjunlp/easyedit/blob/main/tutorial-notebooks/EasyEdit_Example_US_President.ipynb Clone the EasyEdit repository, create a Python 3.9 environment using conda, and install the required libraries by running the provided commands in the terminal. ```bash ## Clone Repo git clone https://github.com/zjunlp/EasyEdit.git cd EasyEdit ## Create Environment conda create -n EasyEdit python=3.9 conda activate EasyEdit pip install -r requirements.txt ``` -------------------------------- ### Environment Setup for Steering Experiments Source: https://github.com/zjunlp/easyedit/blob/main/examples/SPLIT.md Clone the repository, create a Conda environment, activate it, and install dependencies. ```bash git clone https://github.com/zjunlp/EasyEdit.git conda create -n spilt python=3.10 conda activate spilt pip install -r requirements_2.txt ``` -------------------------------- ### Install Watchdog for Previewing Source: https://github.com/zjunlp/easyedit/blob/main/easyeditor/models/melo/peft_egg/docs/README.md Installs the watchdog module, which is required for previewing the documentation. ```bash pip install watchdog ``` -------------------------------- ### Install Dependencies Source: https://github.com/zjunlp/easyedit/blob/main/easyeditor/models/melo/peft_egg/docs/source/task_guides/dreambooth_lora.mdx Install the required libraries for training, including PEFT from source. Ensure you have the latest version for compatibility. ```bash pip install -r requirements.txt pip install git+https://github.com/huggingface/peft ``` -------------------------------- ### Fetch and Prepare Example Image Source: https://github.com/zjunlp/easyedit/blob/main/easyeditor/models/melo/peft_egg/docs/source/task_guides/image_classification_lora.mdx Fetches an example image from a URL and prepares it for model inference. The image is converted to RGB format. ```python from PIL import Image import requests url = "https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/beignets.jpeg" image = Image.open(requests.get(url, stream=True).raw) image ``` ```python image_processor = AutoImageProcessor.from_pretrained(repo_name) encoding = image_processor(image.convert("RGB"), return_tensors="pt") ``` -------------------------------- ### Setup Conda Environment for WISE Source: https://github.com/zjunlp/easyedit/blob/main/examples/WISE.md Create and activate a conda environment with Python 3.9 and install dependencies. This is the initial setup required to run WISE experiments. ```bash conda create -n lifelong_edit python=3.9 conda activate lifelong_edit pip install -r requirements.txt #https://github.com/zjunlp/EasyEdit/blob/main/requirements.txt ``` -------------------------------- ### Install necessary libraries Source: https://github.com/zjunlp/easyedit/blob/main/easyeditor/models/melo/peft_egg/docs/source/task_guides/ptuning-seq-classification.mdx Installs the required libraries for PEFT, Transformers, Datasets, and Evaluate. ```bash !pip install -q peft transformers datasets evaluate ``` -------------------------------- ### Install Python and Dependencies Source: https://github.com/zjunlp/easyedit/blob/main/tutorial-notebooks/EasyEdit_Example_QLoRA_llama3.2-3b.ipynb Install a specific Python version (3.9) and update alternatives, then install pip and project requirements. Ensure the correct Python environment is set up before proceeding. ```python !apt-get install python3.9 !sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1 !sudo update-alternatives --config python3 !apt-get install python3-pip %pip install -r requirements.txt ``` -------------------------------- ### Install Documentation Build Dependencies Source: https://github.com/zjunlp/easyedit/blob/main/easyeditor/models/melo/peft_egg/docs/README.md Installs necessary packages for building documentation from the root of the repository. ```bash pip install -e "[docs]" ``` -------------------------------- ### Install Pip and Project Requirements Source: https://github.com/zjunlp/easyedit/blob/main/tutorial-notebooks/EasyEdit_Example_Multimodal_IKE.ipynb Installs the Python package installer (pip) and then installs all project dependencies listed in the requirements.txt file. ```bash !apt-get install python3-pip !pip install -r requirements.txt ``` -------------------------------- ### Environment Setup for EasyEdit Source: https://github.com/zjunlp/easyedit/blob/main/examples/PersonalityEdit.md Installs conda, creates a virtual environment with Python 3.9.7, activates it, and installs project requirements. ```shell git clone https://github.com/zjunlp/EasyEdit.git conda create -n EasyEdit python=3.9.7 ... conda activate EasyEdit pip install -r requirements.txt ``` -------------------------------- ### Example DeepSpeed Configuration File Source: https://github.com/zjunlp/easyedit/blob/main/easyeditor/models/melo/peft_egg/docs/source/accelerate/deepspeed-zero3-offload.mdx An example YAML configuration file for DeepSpeed with ZeRO Stage-3 and CPU offloading for both optimizer and parameters. This setup is suitable for training large models. ```yaml compute_environment: LOCAL_MACHINE deepspeed_config: gradient_accumulation_steps: 1 gradient_clipping: 1.0 offload_optimizer_device: cpu offload_param_device: cpu zero3_init_flag: true zero3_save_16bit_model: true zero_stage: 3 distributed_type: DEEPSPEED downcast_bf16: 'no' dynamo_backend: 'NO' fsdp_config: {} machine_rank: 0 main_training_function: main megatron_lm_config: {} mixed_precision: 'no' num_machines: 1 num_processes: 1 rdzv_backend: static same_network: true use_cpu: false ``` -------------------------------- ### Install Documentation Builder Tool Source: https://github.com/zjunlp/easyedit/blob/main/easyeditor/models/melo/peft_egg/docs/README.md Installs the special tool required for building the documentation. ```bash pip install git+https://github.com/huggingface/doc-builder ``` -------------------------------- ### Install Dependencies for LoRA Fine-tuning Source: https://github.com/zjunlp/easyedit/blob/main/easyeditor/models/melo/peft_egg/docs/source/task_guides/image_classification_lora.mdx Install the necessary libraries for model training, including transformers, accelerate, evaluate, datasets, loralib, and peft. Use the `-q` flag for a quiet installation. ```bash !pip install transformers accelerate evaluate datasets loralib peft -q ``` -------------------------------- ### Install Dependencies Source: https://github.com/zjunlp/easyedit/blob/main/easyeditor/models/melo/peft_egg/docs/source/task_guides/int8-asr.mdx Install necessary libraries for PEFT, Transformers, Datasets, Accelerate, Evaluate, and bitsandbytes. ```bash !pip install -q peft transformers datasets accelerate evaluate jiwer bitsandbytes ``` -------------------------------- ### Run PROMPT Example Source: https://github.com/zjunlp/easyedit/blob/main/examples/PROMPT.md Example command to run the PROMPT editing method. The `train_data_path` must be specified even though PROMPT does not use a training dataset. ```shell python run_knowedit_llama2.py \ --editing_method=IKE \ --hparams_dir=./hparams/IKE/llama3-8b.yaml \ --data_dir=./data/KnowEdit \ --train_data_path=' ' ``` -------------------------------- ### Initialize and Start Trainer Source: https://github.com/zjunlp/easyedit/blob/main/easyeditor/models/melo/peft_egg/docs/source/task_guides/ptuning-seq-classification.mdx Instantiate the Trainer with the PEFT model, training arguments, datasets, tokenizer, data collator, and evaluation function. Then, start the training process. ```python trainer = Trainer( model=model, args=training_args, train_dataset=tokenized_datasets["train"], eval_dataset=tokenized_datasets["test"], tokenizer=tokenizer, data_collator=data_collator, compute_metrics=compute_metrics, ) trainer.train() ``` -------------------------------- ### Install EasyEdit2 Requirements Source: https://context7.com/zjunlp/easyedit/llms.txt Set up the Python environment and install necessary packages for EasyEdit2. ```bash conda create -n easyedit2 python=3.10 conda activate easyedit2 pip install -r requirements_2.txt ``` -------------------------------- ### Install Fairscale Library Source: https://github.com/zjunlp/easyedit/blob/main/tutorial-notebooks/EasyEdit_Example_IKE_InternLM.ipynb Use this command to install the fairscale library, which is a dependency for EasyEdit. ```bash !pip install fairscale ``` -------------------------------- ### Example Evaluation Command Source: https://github.com/zjunlp/easyedit/blob/main/README_2.md An example of running the evaluation script with specific dataset path, evaluation methods, and model name. ```bash python steer/evaluate/evaluate.py --generation_dataset_path results/my_dataset_results.json --eval_methods ppl distinctness safety --model_name_or_path meta-llama/Llama-2-7b-chat-hf ``` -------------------------------- ### Full Evaluation Example Source: https://github.com/zjunlp/easyedit/blob/main/examples/MMEdit.md An example for setting up and running an evaluation process. Note that for evaluation, both `train_set` and `val_set` can be set to the evaluation dataset. ```python hparams = SERACMultimodalHparams.from_hparams('hparams/SERAC/minigpt4.yaml') # train_ds = CaptionDataset('data/caption_train_edit.json', config=hparams) eval_ds = CaptionDataset('data/caption_eval_edit.json', config=hparams) trainer = MultimodalTrainer( config=hparams, train_set=eval_ds, val_set=eval_ds ) trainer.run() ``` -------------------------------- ### Install PEFT from GitHub Source: https://github.com/zjunlp/easyedit/blob/main/easyeditor/models/melo/peft_egg/docs/source/install.mdx Install the latest development version of PEFT directly from its GitHub repository. This is useful for trying new features or if you need a version not yet released on PyPI. ```bash pip install git+https://github.com/huggingface/peft ``` -------------------------------- ### Install Project Requirements Source: https://github.com/zjunlp/easyedit/blob/main/tutorial-notebooks/EasyEdit_Example_IKE_InternLM.ipynb Installs all necessary Python packages listed in the requirements.txt file. Ensure you are in the EasyEdit directory before running. ```bash !pip install -r requirements.txt ``` -------------------------------- ### Run IKE Example Source: https://github.com/zjunlp/easyedit/blob/main/examples/PROMPT.md Example command to run the IKE editing method. The `train_data_path` is required for IKE to retrieve In-Context prompts. ```shell python run_knowedit_llama2.py \ --editing_method=IKE \ --hparams_dir=./hparams/IKE/llama3-8b.yaml \ --data_dir=./data/KnowEdit \ --train_data_path='the_train_data_path' ``` -------------------------------- ### Load and Use EasyEdit with Qwen Source: https://github.com/zjunlp/easyedit/blob/main/tutorial-notebooks/EasyEdit_Example_ROME_qwen.ipynb Demonstrates loading a pre-trained Qwen model and tokenizer, then initializing EasyEdit for further operations. ```python from transformers import AutoModelForCausalLM, AutoTokenizer model_name = "Qwen/Qwen1.5-0.5B" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype='auto', device_map="cuda") ``` -------------------------------- ### Preview Documentation Source: https://github.com/zjunlp/easyedit/blob/main/easyeditor/models/melo/peft_egg/docs/README.md Starts a local server to preview the documentation. The docs will be viewable at http://localhost:3000. Note that this command only works with existing doc files; new files require updating _toctree.yml and restarting the preview command. ```bash doc-builder preview peft docs/source ``` -------------------------------- ### Set up SteerEval Environment Source: https://github.com/zjunlp/easyedit/blob/main/examples/SteerEval.md Clone the repository, create and activate a conda environment, and install dependencies. ```bash git clone https://github.com/zjunlp/EasyEdit.git conda create -n steereval python=3.10 conda activate steereval pip install -r requirements_2.txt ``` -------------------------------- ### Prepare and Generate with EasyEdit (Second Example) Source: https://github.com/zjunlp/easyedit/blob/main/tutorial-notebooks/EasyEdit_Example_IKE_InternLM.ipynb Sets up a second set of prompts for knowledge editing, tokenizes them, and generates model outputs. This demonstrates the flexibility of EasyEdit with different factual statements. ```python ike_generation_prompts = [ "Lionel Messi plays basketball. \ Q: What is Messi's profession? A: Basketball. \ Lionel Messi is a basketball player. \ Q: What sports is Messi good at? A:", "The president of the US is Biden. \ Q: Who is the president of the US? A: Biden. \ Biden is the leader of the United State. \ Q: The leader of the United State is? A:", ] generation_prompts = [ "Q: What sports is Messi good at? A:", "Q: The leader of the United State is? A:", ] batch = tokenizer(generation_prompts, return_tensors='pt', padding=True) edited_batch = tokenizer(ike_generation_prompts, return_tensors='pt', padding=True) pre_edit_outputs = model.generate( input_ids=batch['input_ids'].to(model.device), attention_mask=batch['attention_mask'].to(model.device), max_new_tokens=15 ) post_edit_outputs = edited_model.generate( input_ids=edited_batch['input_ids'].to(edited_model.device), attention_mask=edited_batch['attention_mask'].to(edited_model.device), max_new_tokens=15 ) print('*'*100) generation_max_length = batch['input_ids'].shape[-1] edited_max_length = edited_batch['input_ids'].shape[-1] for i in range(len(ike_generation_prompts)): print(f'Pre-Edit Output: {tokenizer.decode( pre_edit_outputs[i][generation_max_length :], skip_special_tokens=True)}') print(f'Post-Edit Output: {tokenizer.decode(post_edit_outputs[i][edited_max_length :], skip_special_tokens=True)}') print('--'*50 ) ``` -------------------------------- ### Getting Started with PEFT and LoRA Source: https://github.com/zjunlp/easyedit/blob/main/easyeditor/models/melo/peft_egg/README.md This snippet demonstrates how to initialize a PEFT model using LoRA configuration for sequence-to-sequence language models. It loads a pre-trained model and tokenizer, applies a LoRA configuration, and then prints the trainable parameters. ```python from transformers import AutoModelForSeq2SeqLM from peft import get_peft_config, get_peft_model, LoraConfig, TaskType model_name_or_path = "bigscience/mt0-large" tokenizer_name_or_path = "bigscience/mt0-large" peft_config = LoraConfig( task_type=TaskType.SEQ_2_SEQ_LM, inference_mode=False, r=8, lora_alpha=32, lora_dropout=0.1 ) model = AutoModelForSeq2SeqLM.from_pretrained(model_name_or_path) model = get_peft_model(model, peft_config) model.print_trainable_parameters() # output: trainable params: 2359296 || all params: 1231940608 || trainable%: 0.19151053100118282 ``` -------------------------------- ### Load Configuration and Initialize Source: https://github.com/zjunlp/easyedit/blob/main/tutorial-notebooks/EasyEdit2_Example_CAA_sentiment.ipynb Loads the configuration from a YAML file and sets up initial parameters for the model and device. Ensure the config file path is correct. ```python import sys sys.path.append('../') from omegaconf import OmegaConf, DictConfig from steer.vector_generators.vector_generators import BaseVectorGenerator from steer.datasets import prepare_train_dataset from steer.vector_appliers.vector_applier import BaseVectorApplier from steer.datasets import prepare_generation_datasets # model_path="../model/DeepSeek-R1-Distill-Qwen-7B" top_cfg = OmegaConf.load("./config_sentiment.yaml") # top_cfg.model_name_or_path = model_path # top_cfg.device = "cuda:1" top_cfg ``` -------------------------------- ### List Installed Packages Source: https://github.com/zjunlp/easyedit/blob/main/tutorial-notebooks/EasyEdit_Example_Multimodal_IKE.ipynb Displays a list of all Python packages currently installed in the environment. Useful for verifying installations. ```bash !pip list ``` -------------------------------- ### Setup model and training parameters Source: https://github.com/zjunlp/easyedit/blob/main/easyeditor/models/melo/peft_egg/docs/source/task_guides/seq2seq-prefix-tuning.mdx Initializes the model, tokenizer, and defines hyperparameters for training. Sets environment variables for tokenizer parallelism and CUDA device. ```python from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, default_data_collator, get_linear_schedule_with_warmup from peft import get_peft_config, get_peft_model, get_peft_model_state_dict, PrefixTuningConfig, TaskType from datasets import load_dataset from torch.utils.data import DataLoader from tqdm import tqdm import os os.environ["TOKENIZERS_PARALLELISM"] = "false" os.environ["CUDA_VISIBLE_DEVICES"] = "3" device = "cuda" model_name_or_path = "t5-large" tokenizer_name_or_path = "t5-large" text_column = "sentence" label_column = "text_label" max_length = 128 lr = 1e-2 num_epochs = 5 batch_size = 8 ``` -------------------------------- ### Install Dependencies Source: https://github.com/zjunlp/easyedit/blob/main/tutorial-notebooks/EasyEdit_Example_IKE_Baichuan.ipynb Install Python 3.9 and necessary packages including python3-pip and requirements.txt. Also install the sentence-transformers library. ```bash !apt-get install python3.9 !sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1 !sudo update-alternatives --config python3 !apt-get install python3-pip !pip install -r requirements.txt !pip install sentence_transformers ``` -------------------------------- ### Pinyin Notation Example (JSON) Source: https://github.com/zjunlp/easyedit/blob/main/examples/CKnowEdit.md An example of a 'Pinyin Notation' data sample from CKnowEdit, demonstrating a prompt for pinyin, incorrect/correct responses, and portability/locality examples. ```json { "prompt": "请给下面的字注音:莘庄", "target_old": "莘庄的拼音是:“shēn zhuāng”", "target_new": "莘庄的注音是:xīn zhuāng", "portability": [ { "prompt": "请给下面的字注音:细莘", "answer": "细莘的注音是:xì xīn" } ], "locality": [ { "prompt": "请给下面的字注音:莘莘学子", "answer": "莘莘学子的注音是:shēn shēn xué zǐ" } ], "rephrase": [ "请问莘庄的正确拼音是什么?", "莘庄正确的注音怎么写?", "请注音:莘庄。", "莘庄的拼音应该怎么表示?" ] } ``` -------------------------------- ### Download Base Model (Qwen2.5-0.5B-Instruct) Source: https://github.com/zjunlp/easyedit/blob/main/examples/CCKS2025.md Command to download the Qwen2.5-0.5B-Instruct model using the huggingface-cli. The model will be saved to the specified local directory. ```shell huggingface-cli download Qwen/Qwen2.5-0.5B-Instruct --local-dir hugging_cache/Qwen2.5-0.5B-Instruct ``` -------------------------------- ### Install EasyEdit with Conda Source: https://github.com/zjunlp/easyedit/blob/main/examples/ConceptEdit.md Installs EasyEdit using conda, creating a dedicated Python 3.9.7 environment. Ensure you have conda installed and are using Python 3.9+. ```shell git clone https://github.com/zjunlp/EasyEdit.git conda create -n EasyEdit python=3.9.7 conda activate EasyEdit pip install -r requirements.txt ``` -------------------------------- ### Example FSDP Configuration File Source: https://github.com/zjunlp/easyedit/blob/main/easyeditor/models/melo/peft_egg/docs/source/accelerate/fsdp.mdx This YAML file demonstrates a typical FSDP configuration, including sharding strategy, offload parameters, auto-wrap policy, and transformer layer class to wrap. ```yaml command_file: null commands: null compute_environment: LOCAL_MACHINE deepspeed_config: {} distributed_type: FSDP downcast_bf16: 'no' dynamo_backend: 'NO' fsdp_config: fsdp_auto_wrap_policy: TRANSFORMER_BASED_WRAP fsdp_backward_prefetch_policy: BACKWARD_PRE fsdp_offload_params: true fsdp_sharding_strategy: 1 fsdp_state_dict_type: FULL_STATE_DICT fsdp_transformer_layer_cls_to_wrap: T5Block gpu_ids: null machine_rank: 0 main_process_ip: null main_process_port: null main_training_function: main megatron_lm_config: {} mixed_precision: 'no' num_machines: 1 num_processes: 2 rdzv_backend: static same_network: true tpu_name: null tpu_zone: null use_cpu: false ``` -------------------------------- ### Install SafeEdit Project Dependencies Source: https://github.com/zjunlp/easyedit/blob/main/demo/SafeEdit_demo/README.md Clone the repository, create and activate a Conda environment, and install project requirements. This includes installing dependencies for the main project and the SafeEdit demo. ```shell git clone https://github.com/zjunlp/EasyEdit.git conda create -n EasyEdit python=3.9.7 conda activate EasyEdit cd EasyEdit pip install -r requirements.txt cd demo/SafeEdit_demo pip install -r requirements.txt ``` -------------------------------- ### Python Example Docstring with Accelerate Source: https://github.com/zjunlp/easyedit/blob/main/easyeditor/models/melo/peft_egg/docs/README.md Demonstrates how to write an example docstring in Python using the `accelerate` library. Ensure the example is minimal, clear, and includes expected output for inference usage. ```python >>> import time >>> from accelerate import Accelerator >>> accelerator = Accelerator() >>> if accelerator.is_main_process: ... time.sleep(2) >>> else: ... print("I'm waiting for the main process to finish its sleep...") >>> accelerator.wait_for_everyone() >>> # Should print on every process at the same time >>> print("Everyone is here") ``` -------------------------------- ### Setup Environment Variables and Model Parameters Source: https://github.com/zjunlp/easyedit/blob/main/easyeditor/models/melo/peft_egg/docs/source/task_guides/int8-asr.mdx Configure the CUDA visible devices and define model, language, task, and dataset parameters for training. ```python import os os.environ["CUDA_VISIBLE_DEVICES"] = "0" model_name_or_path = "openai/whisper-large-v2" language = "Marathi" language_abbr = "mr" task = "transcribe" dataset_name = "mozilla-foundation/common_voice_11_0" ``` -------------------------------- ### Initialize Model and Tokenizer Source: https://github.com/zjunlp/easyedit/blob/main/tutorial-notebooks/EasyEdit_Example_IKE.ipynb Sets up the tokenizer and language model for text generation. Ensure the model path is correct and the device is specified. ```python tokenizer.pad_token_id = tokenizer.eos_token_id tokenizer.padding_side='left' device = 1 model = GPT2LMHeadModel.from_pretrained('./hugging_cache/gpt2-xl',trust_remote_code=True).to(f'cuda:{device}') ``` -------------------------------- ### Prepare Runtime Environment Source: https://github.com/zjunlp/easyedit/blob/main/tutorial-notebooks/EasyEdit_Example_IKE.ipynb This snippet is a placeholder for setting up the necessary runtime environment for the IKE method. Ensure all required libraries and dependencies are installed before proceeding. ```python # Prepare the runtime environment ``` -------------------------------- ### Example Output Comparison Source: https://github.com/zjunlp/easyedit/blob/main/tutorial-notebooks/EasyEdit_Example_KN_T5.ipynb Illustrates the expected format for comparing pre-edit and post-edit model outputs for a given prompt. ```text Output: Prompt: Who was the designer of Lahti Town Hall? Pre-Edit Output: was the designer of Lahti Town Hall??? Who was the designer Post-Edit Output: dendendenden ---------------------------------------------------------------------------------------------------- ``` -------------------------------- ### Install Additional Libraries Source: https://github.com/zjunlp/easyedit/blob/main/tutorial-notebooks/EasyEdit_Example_Multimodal_IKE.ipynb Installs the matplotlib library for plotting and sentence_transformers for natural language processing tasks. ```bash !pip install matplotlib !pip install sentence_transformers ``` -------------------------------- ### Set up Training Arguments Source: https://github.com/zjunlp/easyedit/blob/main/easyeditor/models/melo/peft_egg/docs/source/task_guides/ptuning-seq-classification.mdx Configure training hyperparameters, evaluation strategy, and saving strategy using TrainingArguments. ```python training_args = TrainingArguments( output_dir="your-name/roberta-large-peft-p-tuning", learning_rate=1e-3, per_device_train_batch_size=32, per_device_eval_batch_size=32, num_train_epochs=2, weight_decay=0.01, evaluation_strategy="epoch", save_strategy="epoch", load_best_model_at_end=True, ) ``` -------------------------------- ### Install Python 3.9 Source: https://github.com/zjunlp/easyedit/blob/main/tutorial-notebooks/EasyEdit_Example_Multimodal_IKE.ipynb Installs Python 3.9, which may be required for specific dependencies or project compatibility. ```bash !apt-get install python3.9 ``` -------------------------------- ### Load Configuration and Initialize Source: https://github.com/zjunlp/easyedit/blob/main/tutorial-notebooks/EasyEdit2_Example_CAA_translate.ipynb Loads the configuration from a YAML file and sets up the necessary paths and parameters for the EasyEdit2 model. Ensure the config file exists and paths are correct. ```python import sys sys.path.append('..') from omegaconf import OmegaConf, DictConfig from steer.vector_generators.vector_generators import BaseVectorGenerator from steer.datasets import prepare_train_dataset from steer.vector_appliers.vector_applier import BaseVectorApplier from steer.datasets import prepare_generation_datasets # model_path="../model/Qwen2-7B-Instruct" top_cfg = OmegaConf.load("./config_translate.yaml") # top_cfg.model_name_or_path = model_path # top_cfg.device = "cuda:0" top_cfg ``` -------------------------------- ### Install PEFT from PyPI Source: https://github.com/zjunlp/easyedit/blob/main/easyeditor/models/melo/peft_egg/docs/source/install.mdx Use this command to install the latest stable version of PEFT from the Python Package Index. ```bash pip install peft ``` -------------------------------- ### Create FSDP Configuration File with Accelerate Source: https://github.com/zjunlp/easyedit/blob/main/easyeditor/models/melo/peft_egg/docs/source/accelerate/fsdp.mdx Run this command to create a FSDP configuration file using 🤗 Accelerate. Use the `--config_file` flag to specify a location; otherwise, it defaults to `default_config.yaml` in the Accelerate cache. ```bash accelerate config --config_file fsdp_config.yaml ``` -------------------------------- ### Install EasyEdit with Conda Source: https://github.com/zjunlp/easyedit/blob/main/examples/MMEdit.md Clone the repository and create a conda environment with Python 3.9+ for EasyEdit. Then, install the required packages. ```bash git clone https://github.com/zjunlp/EasyEdit.git conda create -n EasyEdit python=3.9.7 ... pip install -r requirements.txt ``` -------------------------------- ### Load Configuration and Prepare Dataset Source: https://github.com/zjunlp/easyedit/blob/main/tutorial-notebooks/EasyEdit2_Example_CAA_r1_control.ipynb Loads a configuration file using OmegaConf and prepares the training dataset. Ensure the config file path is correct. ```python import sys sys.path.append('../') from omegaconf import OmegaConf, DictConfig from steer.vector_generators.vector_generators import BaseVectorGenerator from steer.datasets import prepare_train_dataset from steer.vector_appliers.vector_applier import BaseVectorApplier from steer.datasets import prepare_generation_datasets # model_path="../model/DeepSeek-R1-Distill-Qwen-7B" top_cfg = OmegaConf.load("./config_r1_control.yaml") # top_cfg.model_name_or_path = model_path ``` -------------------------------- ### ZsRE Knowledge Editing Example Source: https://github.com/zjunlp/easyedit/blob/main/examples/KnowEdit.md This JSON structure represents a ZsRE (Zero-Shot Relation Editing) example. It includes subject, target, prompt, ground truth, and rephrased prompts for relation editing. It also contains locality and portability examples for more specific knowledge manipulation. ```json { "subject": "Epaspidoceras", "target_new": "Noctuidae", "prompt": "Which family does Epaspidoceras belong to?", "ground_truth": [ "Aspidoceratidae" ], "rephrase_prompt": "What family are Epaspidoceras?", "cond": "Geometridae >> Noctuidae || Which family does Epaspidoceras belong to?", "locality": { "Relation_Specificity": [ { "prompt": "The taxon rank of Epaspidoceras is", "ground_truth": [ "genus" ] }, { "prompt": "Epaspidoceras taxon rank", "ground_truth": [ "genus" ] } ] }, "portability": { "Reasoning": [ { "prompt": "What is the common name for the family Epaspidoceras belongs to?", "ground_truth": "Owlet moths" } ] } } ```