### Setup LayerSkip Environment Source: https://github.com/facebookresearch/layerskip/blob/main/README.md Set up a Conda environment and install project dependencies. Ensure you have Python 3.10 or a compatible version. ```console $ conda create --name layer_skip python=3.10 $ conda activate layer_skip $ pip install -r requirements.txt ``` -------------------------------- ### Optimize Dockerfile Dependencies Installation Source: https://github.com/facebookresearch/layerskip/blob/main/DOCKER.md Combine RUN commands in your Dockerfile to reduce layers and leverage Docker's build cache. Install dependencies before copying application code. ```dockerfile RUN conda install pytorch==2.2.1 torchvision==0.17.1 torchaudio==2.2.1 cpuonly -c pytorch -y && \ pip install --upgrade pip && \ pip install --no-cache-dir -r /app/requirements.txt ``` -------------------------------- ### Run LayerSkip Docker Container (Basic) Source: https://github.com/facebookresearch/layerskip/blob/main/DOCKER.md Execute a command inside the LayerSkip Docker container. This example shows how to run a script with the --help flag. Ensure your HuggingFace token is provided as an environment variable. ```bash docker run -it --rm \ -e HUGGINGFACE_TOKEN=your_huggingface_token_here \ layerskip:latest \ python your_script.py --help ``` -------------------------------- ### Optimize Dockerfile Dependencies Installation Source: https://github.com/facebookresearch/layerskip/blob/main/DOCKER.md This Dockerfile snippet optimizes build times by copying only the requirements file first and installing dependencies. This allows Docker to cache the dependency layer, speeding up subsequent builds if requirements.txt remains unchanged. It also combines RUN commands for fewer layers. ```dockerfile RUN conda install pytorch==2.2.1 torchvision==0.17.1 torchaudio==2.2.1 cpuonly -c pytorch -y && \ pip install --upgrade pip && \ pip install --no-cache-dir -r /app/requirements.txt ``` -------------------------------- ### Verify PyTorch Installation in Docker Container Source: https://github.com/facebookresearch/layerskip/blob/main/DOCKER.md Run a command inside the Docker container to check the installed PyTorch version. This confirms that the deep learning environment is set up correctly. ```bash docker run -it --rm layerskip:latest python -c "import torch; print(torch.__version__)" ``` -------------------------------- ### Check Script Help Messages in Docker Container Source: https://github.com/facebookresearch/layerskip/blob/main/DOCKER.md Verify that scripts are accessible and their dependencies are correctly installed by checking their help messages within the Docker container. ```bash docker run -it --rm layerskip:latest python generate.py --help ``` ```bash docker run -it --rm layerskip:latest python benchmark.py --help ``` ```bash docker run -it --rm layerskip:latest python eval.py --help ``` ```bash docker run -it --rm layerskip:latest python sweep.py --help ``` ```bash docker run -it --rm layerskip:latest python correctness.py --help ``` -------------------------------- ### Run Generate Script with Docker and Help Flag Source: https://github.com/facebookresearch/layerskip/blob/main/DOCKER.md Execute the generate script with the --help flag using Docker. Passes the HuggingFace token as an environment variable. ```bash docker run -it --rm \ -e HUGGINGFACE_TOKEN=your_huggingface_token_here \ layerskip:latest \ python generate.py --help ``` -------------------------------- ### Benchmark LayerSkip Model with Docker Source: https://github.com/facebookresearch/layerskip/blob/main/DOCKER.md Execute the benchmark script for the LayerSkip model using Docker. Mounts a log directory and passes the HuggingFace token as an environment variable. ```bash docker run -it --rm \ -e HUGGINGFACE_TOKEN=your_huggingface_token_here \ -v /path/on/host/logs:/app/logs \ layerskip:latest \ python benchmark.py --model facebook/layerskip-llama2-7B \ --dataset human_eval \ --num_samples 100 \ --generation_strategy self_speculative \ --exit_layer 8 \ --num_speculations 6 \ --output_dir /app/logs ``` -------------------------------- ### Benchmark LayerSkip Model with Docker Source: https://github.com/facebookresearch/layerskip/blob/main/DOCKER.md Run the benchmark.py script inside the LayerSkip Docker container to benchmark the model on a specified dataset. Mounts a host directory for logs. Requires HuggingFace token. ```bash docker run -it --rm \ -e HUGGINGFACE_TOKEN=your_huggingface_token_here \ -v /path/on/host/logs:/app/logs \ layerskip:latest \ python benchmark.py --model facebook/layerskip-llama2-7B \ --dataset cnn_dm_summarization \ --num_samples 100 \ --generation_strategy self_speculative \ --exit_layer 8 \ --num_speculations 6 \ --output_dir /app/logs ``` -------------------------------- ### Evaluate LayerSkip Model with Docker Source: https://github.com/facebookresearch/layerskip/blob/main/DOCKER.md Run the evaluation script for a LayerSkip model using Docker. Mounts a log directory and sets the HuggingFace token. ```bash docker run -it --rm \ -e HUGGINGFACE_TOKEN=your_huggingface_token_here \ -v /path/on/host/logs:/app/logs \ layerskip:latest \ python eval.py --model facebook/layerskip-llama2-7B \ --tasks gsm8k \ --limit 10 \ --generation_strategy self_speculative \ --exit_layer 8 \ --num_speculations 6 \ --output_dir /app/logs ``` -------------------------------- ### Clone LayerSkip Repository Source: https://github.com/facebookresearch/layerskip/blob/main/README.md Clone the LayerSkip repository to your local machine. This is the first step to setting up the project. ```console $ git clone git@github.com:facebookresearch/LayerSkip.git $ cd LayerSkip ``` -------------------------------- ### Test Docker Container with Dummy HuggingFace Token Source: https://github.com/facebookresearch/layerskip/blob/main/DOCKER.md Pass a dummy HuggingFace token to ensure environment variables are handled correctly without attempting to access models. This avoids authentication errors during testing. ```bash docker run -it --rm \ -e HUGGINGFACE_TOKEN=dummy_token \ layerskip:latest \ python generate.py --help ``` -------------------------------- ### Check HuggingFace Token Environment Variable Source: https://github.com/facebookresearch/layerskip/blob/main/DOCKER.md Verify that environment variables are correctly set within the Docker container by echoing the HuggingFace token. ```bash docker run -it --rm layerskip:latest bash -c 'echo $HUGGINGFACE_TOKEN' ``` -------------------------------- ### Benchmark LayerSkip Model Source: https://github.com/facebookresearch/layerskip/blob/main/README.md Use this script to benchmark a LayerSkip model on a specified dataset. Configure dataset, number of samples, generation strategy, exit layer, and number of speculations. ```console $ torchrun benchmark.py --model facebook/layerskip-llama2-7B \ --dataset cnn_dm_summarization \ --num_samples 100 \ --generation_strategy self_speculative \ --exit_layer 8 \ --num_speculations 6 \ --output_dir ./logs ``` -------------------------------- ### Perform Hyperparameter Sweep with Docker Source: https://github.com/facebookresearch/layerskip/blob/main/DOCKER.md Execute the sweep script to test different hyperparameters for the LayerSkip model via Docker. Mounts a sweep directory and sets the HuggingFace token. ```bash docker run -it --rm \ -e HUGGINGFACE_TOKEN=your_huggingface_token_here \ -v /path/on/host/sweep:/app/sweep \ layerskip:latest \ python sweep.py --model facebook/layerskip-llama2-7B \ --dataset human_eval \ --generation_strategy self_speculative \ --num_samples 150 \ --max_steps 256 \ --output_dir /app/sweep \ --sample False ``` -------------------------------- ### Clone LayerSkip Repository Source: https://github.com/facebookresearch/layerskip/blob/main/DOCKER.md Clone the LayerSkip repository to your local machine. This is the first step before building the Docker image. ```bash git clone git@github.com:facebookresearch/LayerSkip.git cd LayerSkip ``` -------------------------------- ### Docker Entrypoint Script to Read Secret Source: https://github.com/facebookresearch/layerskip/blob/main/DOCKER.md Bash script for a Docker entrypoint that activates a Conda environment and exports the HuggingFace token from a Docker secret. ```bash #!/bin/bash # entrypoint.sh # Activate the Conda environment source /opt/conda/etc/profile.d/conda.sh conda activate layer_skip # Read HuggingFace token from Docker secret export HUGGINGFACE_TOKEN=$(cat /run/secrets/huggingface_token) # Execute the passed command exec "$@" ``` -------------------------------- ### Deploy Docker Service with Secret Source: https://github.com/facebookresearch/layerskip/blob/main/DOCKER.md Command to create a Docker Swarm service named 'layerskip_service' using a specific image and mounting a secret. ```bash docker service create --name layerskip_service \ --secret huggingface_token \ layerskip:latest \ python generate.py --help ``` -------------------------------- ### Check Correctness of Self-Speculative Decoding with Docker Source: https://github.com/facebookresearch/layerskip/blob/main/DOCKER.md Run the correctness check script for self-speculative decoding using Docker. Mounts a correctness directory and sets the HuggingFace token. ```bash docker run -it --rm \ -e HUGGINGFACE_TOKEN=your_huggingface_token_here \ -v /path/on/host/correctness:/app/correctness \ layerskip:latest \ python correctness.py --model facebook/layerskip-llama2-7B \ --dataset human_eval \ --generation_strategy self_speculative \ --num_speculations 6 \ --exit_layer 4 \ --num_samples 10 \ --sample False \ --output_dir /app/correctness ``` -------------------------------- ### Generate Text with Self-Speculative Decoding (Docker) Source: https://github.com/facebookresearch/layerskip/blob/main/DOCKER.md Execute text generation using self-speculative decoding within the LayerSkip Docker container. Specify --exit_layer and --num_speculations for speedup. Requires HuggingFace token. ```bash docker run -it --rm \ -e HUGGINGFACE_TOKEN=your_huggingface_token_here \ layerskip:latest \ python generate.py --model facebook/layerskip-llama2-7B \ --sample True \ --max_steps 512 \ --generation_strategy self_speculative \ --exit_layer 8 \ --num_speculations 6 ``` -------------------------------- ### Generate Text with LayerSkip Docker Source: https://github.com/facebookresearch/layerskip/blob/main/DOCKER.md Run the generate.py script within the LayerSkip Docker container for text generation using autoregressive decoding. Requires HuggingFace token. ```bash docker run -it --rm \ -e HUGGINGFACE_TOKEN=your_huggingface_token_here \ layerskip:latest \ python generate.py --model facebook/layerskip-llama2-7B \ --sample True \ --max_steps 512 ``` -------------------------------- ### Evaluate LayerSkip Model with LM Evaluation Harness Source: https://github.com/facebookresearch/layerskip/blob/main/README.md Evaluate a LayerSkip model using the Eleuther Language Model Evaluation Harness. Specify tasks, limit, generation strategy, exit layer, and number of speculations. ```console $ torchrun eval.py --model facebook/layerskip-llama2-7B \ --tasks gsm8k \ --limit 10 \ --generation_strategy self_speculative \ --exit_layer 8 \ --num_speculations 6 \ --output_dir ./logs ``` -------------------------------- ### Run Unit/Integration Tests Source: https://github.com/facebookresearch/layerskip/blob/main/README.md Execute unit and integration tests for LayerSkip using pytest. Ensure you are in the project's root directory. ```bash pytest ./tests/ ``` -------------------------------- ### Build LayerSkip Docker Image Source: https://github.com/facebookresearch/layerskip/blob/main/DOCKER.md Build the Docker image for LayerSkip using the Dockerfile in the project root. This command tags the image as 'layerskip:latest'. ```bash docker build -t layerskip:latest . ``` -------------------------------- ### Mount HuggingFace Cache Directory in Docker Source: https://github.com/facebookresearch/layerskip/blob/main/DOCKER.md Mount the host's HuggingFace cache directory to the container to avoid re-downloading models. This speeds up model loading times on subsequent runs. ```bash docker run -it --rm \ -e HUGGINGFACE_TOKEN=your_huggingface_token_here \ -v /path/on/host/huggingface_cache:/root/.cache/huggingface \ layerskip:latest \ python generate.py --help ``` -------------------------------- ### Run Model with Autoregressive Decoding Source: https://github.com/facebookresearch/layerskip/blob/main/README.md Generate text using a LayerSkip model with standard autoregressive decoding. This is useful for baseline generation. ```console $ torchrun generate.py --model facebook/layerskip-llama2-7B \ --sample True \ --max_steps 512 ``` -------------------------------- ### Sweep LayerSkip Hyperparameters Source: https://github.com/facebookresearch/layerskip/blob/main/README.md Sweep over a grid of exit_layer and num_speculations to find optimal inference hyperparameters. Configure model, dataset, generation strategy, and sampling parameters. ```console $ torchrun sweep.py --model facebook/layerskip-llama2-7B \ --dataset human_eval \ --generation_strategy self_speculative \ --num_samples 150 \ --max_steps 256 \ --output_dir ./logs/ \ --sample False ``` -------------------------------- ### Create Docker Secret for HuggingFace Token Source: https://github.com/facebookresearch/layerskip/blob/main/DOCKER.md Command to create a Docker secret named 'huggingface_token' with the provided token value. ```bash echo "your_huggingface_token_here" | docker secret create huggingface_token - ``` -------------------------------- ### Run Model with Self-Speculative Decoding Source: https://github.com/facebookresearch/layerskip/blob/main/README.md Generate text using a LayerSkip model with self-speculative decoding for faster inference. Specify `--exit_layer` and `--num_speculations` to enable this feature. ```console $ torchrun generate.py --model facebook/layerskip-llama2-7B \ --sample True \ --max_steps 512 \ --generation_strategy self_speculative \ --exit_layer 8 \ --num_speculations 6 ``` -------------------------------- ### Expose and Map Port 8000 in Docker Source: https://github.com/facebookresearch/layerskip/blob/main/DOCKER.md Expose a port in the Dockerfile and map it when running the container to allow access to web servers or services running inside. ```dockerfile EXPOSE 8000 ``` ```bash docker run -it --rm \ -e HUGGINGFACE_TOKEN=your_huggingface_token_here \ -p 8000:8000 \ layerskip:latest \ python your_web_server_script.py ``` -------------------------------- ### Verify Correctness of Self-Speculative Decoding Source: https://github.com/facebookresearch/layerskip/blob/main/README.md Verify that generated tokens from self-speculative decoding match autoregressive decoding. Ensure no sampling is enabled for guaranteed equivalence. ```console $ torchrun correctness.py --model facebook/layerskip-llama2-7B \ --dataset human_eval \ --generation_strategy self_speculative \ --num_speculations 6 \ --exit_layer 4 \ --num_samples 10 \ --sample False \ --output_dir ./logs ``` -------------------------------- ### LayerSkip Citation Source: https://github.com/facebookresearch/layerskip/blob/main/README.md BibTeX entry for citing LayerSkip in academic research. This entry provides all necessary details for proper attribution. ```bibtex @misc{layerskip, title={LayerSkip: Enabling Early Exit Inference and Self-Speculative Decoding}, author={Mostafa Elhoushi and Akshat Shrivastava and Diana Liskovich and Basil Hosmer and Bram Wasti and Liangzhen Lai and Anas Mahmoud and Bilge Acun and Saurabh Agarwal and Ahmed Roman and Ahmed A Aly and Beidi Chen and Carole-Jean Wu}, booktitle = "Proceedings of the 62nd Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)", month = aug, year = "2024", address = "Bangkok, Thailand", publisher = "Association for Computational Linguistics", url = "https://aclanthology.org/2024.acl-long.681", doi = "10.18653/v1/2024.acl-long.681", pages = "12622--12642", } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.