### Installing Example-Specific Requirements (Bash) Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/run_scripts.mdx After navigating to a specific example folder within the Hugging Face Transformers repository, this command installs all necessary Python dependencies listed in the `requirements.txt` file. This ensures that the chosen example script has all its prerequisites met to run successfully. ```bash pip install -r requirements.txt ``` -------------------------------- ### Installing Example-Specific Requirements (Bash) Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/examples/README.md This command installs additional dependencies required for specific example scripts within the Hugging Face Transformers library. After navigating into the desired example folder, running this command ensures all necessary packages for that particular example are installed. It relies on a 'requirements.txt' file present in the example directory. ```bash pip install -r requirements.txt ``` -------------------------------- ### Installing Hugging Face Transformers from Source (Bash) Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/run_scripts.mdx This snippet provides the commands to clone the Hugging Face Transformers repository from GitHub, navigate into its directory, and install the library in editable mode from source. This setup is required to run the latest version of example scripts and ensures all necessary dependencies are installed. ```bash git clone https://github.com/huggingface/transformers cd transformers pip install . ``` -------------------------------- ### Installing Example-Specific Dependencies Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/docs/source/en/run_scripts.mdx After setting up the correct library version, this command installs any additional Python package dependencies required by specific example scripts. Users should navigate to the desired example folder before running this command to ensure the correct 'requirements.txt' file is used. ```bash pip install -r requirements.txt ``` -------------------------------- ### Installing Example-Specific Requirements for Hugging Face Transformers (Bash) Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/examples/README.md This command installs all necessary dependencies listed in the `requirements.txt` file for a specific example folder. It ensures that all prerequisites for running the chosen example script are met, complementing the main library installation. ```bash pip install -r requirements.txt ``` -------------------------------- ### Installing Hugging Face Transformers from Source (Latest) Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/docs/source/en/run_scripts.mdx This snippet provides the commands to clone the Hugging Face Transformers repository from GitHub, navigate into the directory, and install the library in editable mode using pip. This setup is recommended for running the latest version of example scripts. ```bash git clone https://github.com/huggingface/transformers cd transformers pip install . ``` -------------------------------- ### Installing Hugging Face Transformers from Source (Bash) Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/examples/README.md This snippet outlines the steps to clone the Hugging Face Transformers repository and install the library directly from its source code. This is necessary to run the latest versions of example scripts and ensures all development dependencies are met. It should be executed within a new virtual environment. ```bash git clone https://github.com/huggingface/transformers cd transformers pip install . ``` -------------------------------- ### Example DeepSpeed Translation Script Execution Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/docs/source/en/main_classes/deepspeed.mdx This is a practical example of running the `run_translation.py` script using the DeepSpeed launcher, demonstrating how to specify a DeepSpeed configuration, model, batch size, and dataset for a translation task. ```bash deepspeed examples/pytorch/translation/run_translation.py \ --deepspeed tests/deepspeed/ds_config_zero3.json \ --model_name_or_path t5-small --per_device_train_batch_size 1 \ --output_dir output_dir --overwrite_output_dir --fp16 \ --do_train --max_train_samples 500 --num_train_epochs 1 \ --dataset_name wmt16 --dataset_config "ro-en" \ --source_lang en --target_lang ro ``` -------------------------------- ### Cloning DeepSpeed Examples Repository (Bash) Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/docs/source/en/main_classes/deepspeed.mdx This snippet demonstrates how to clone the DeepSpeedExamples GitHub repository, navigate into its directory, and then list all JSON configuration files within it. This is useful for exploring existing DeepSpeed configuration examples. ```bash git clone https://github.com/microsoft/DeepSpeedExamples cd DeepSpeedExamples find . -name '*json' ``` -------------------------------- ### Installing Project Dependencies (Shell) Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/examples/research_projects/visual_bert/README.md Installs all required Python packages listed in the 'requirements.txt' file into the currently active virtual environment. This step ensures all necessary libraries for the VisualBERT demo are available. ```Shell pip install -r ./requirements.txt ``` -------------------------------- ### Installing Transformers with PyTorch CPU Support Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/installation.mdx This command installs the Hugging Face Transformers library along with CPU-only support for PyTorch. It's a convenient way to get both dependencies in one go. ```Bash pip install transformers[torch] ``` -------------------------------- ### Installing Python Dependencies (Shell) Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/examples/research_projects/lxmert/README.md This command uses pip to install all Python packages listed in the 'requirements.txt' file. It ensures that all necessary libraries for the LXMERT demo are installed within the active virtual environment. ```Shell pip install -r ./requirements.txt ``` -------------------------------- ### Running Example Tests with Pytest and Pip in Bash Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/CONTRIBUTING.md This snippet first shows how to install dependencies for the examples using `pip install -r examples/xxx/requirements.txt` (only needed once). It then runs the example tests with `pytest`, using similar options as the library tests, targeting the `./examples/` directory. ```bash $ pip install -r examples/xxx/requirements.txt # only needed the first time $ python -m pytest -n auto --dist=loadfile -s -v ./examples/ ``` -------------------------------- ### Cloning and Installing Hugging Face Transformers from Source (Bash) Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/examples/README.md This snippet outlines the initial steps to obtain and install the Hugging Face Transformers library directly from its GitHub source. It involves cloning the repository, navigating into its directory, and then installing the library in editable mode using pip, which is crucial for running the latest example scripts. ```bash git clone https://github.com/huggingface/transformers cd transformers pip install . ``` -------------------------------- ### Example DeepSpeed Configuration File (JSON) Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/docs/source/en/main_classes/deepspeed.mdx This JSON snippet provides a comprehensive example of a DeepSpeed configuration file. It demonstrates how to enable FP16 mixed precision, configure the AdamW optimizer, set up a WarmupLR scheduler, and activate ZeRO stage 2 optimization with CPU offloading for optimizer states. ```json { "fp16": { "enabled": "auto", "loss_scale": 0, "loss_scale_window": 1000, "initial_scale_power": 16, "hysteresis": 2, "min_loss_scale": 1 }, "optimizer": { "type": "AdamW", "params": { "lr": "auto", "betas": "auto", "eps": "auto", "weight_decay": "auto" } }, "scheduler": { "type": "WarmupLR", "params": { "warmup_min_lr": "auto", "warmup_max_lr": "auto", "warmup_num_steps": "auto" } }, "zero_optimization": { "stage": 2, "offload_optimizer": { "device": "cpu", "pin_memory": true }, "allgather_partitions": true, "allgather_bucket_size": 2e8, "overlap_comm": true, "reduce_scatter": true, "reduce_bucket_size": 2e8, "contiguous_gradients": true }, "gradient_accumulation_steps": "auto", "gradient_clipping": "auto", "train_batch_size": "auto", "train_micro_batch_size_per_gpu": "auto" } ``` -------------------------------- ### Running Example Tests with Pytest (Bash) Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/contributing.md This snippet provides instructions for running tests specific to the project's examples. It includes a preliminary step to install necessary dependencies via `pip install -r examples/xxx/requirements.txt` (if not already done) before executing `pytest` on the `./examples/` directory with similar options as the library tests. ```bash $ pip install -r examples/xxx/requirements.txt # only needed the first time $ python -m pytest -n auto --dist=loadfile -s -v ./examples/ ``` -------------------------------- ### Installing Hugging Face Accelerate (Bash) Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/examples/pytorch/README.md This command installs the latest development version of the Hugging Face Accelerate library directly from its GitHub repository. Accelerate simplifies distributed training setups for PyTorch models. ```bash pip install git+https://github.com/huggingface/accelerate ``` -------------------------------- ### Example DeepSpeed Configuration File (JSON) Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/main_classes/deepspeed.mdx This JSON snippet provides a comprehensive example of a DeepSpeed configuration file. It enables ZeRO stage 2 optimization with CPU offloading for optimizer states, configures the `AdamW` optimizer and `WarmupLR` scheduler, and sets up automatic mixed precision training if `--fp16` is enabled. ```json { "fp16": { "enabled": "auto", "loss_scale": 0, "loss_scale_window": 1000, "initial_scale_power": 16, "hysteresis": 2, "min_loss_scale": 1 }, "optimizer": { "type": "AdamW", "params": { "lr": "auto", "betas": "auto", "eps": "auto", "weight_decay": "auto" } }, "scheduler": { "type": "WarmupLR", "params": { "warmup_min_lr": "auto", "warmup_max_lr": "auto", "warmup_num_steps": "auto" } }, "zero_optimization": { "stage": 2, "offload_optimizer": { "device": "cpu", "pin_memory": true }, "allgather_partitions": true, "allgather_bucket_size": 2e8, "overlap_comm": true, "reduce_scatter": true, "reduce_bucket_size": 2e8, "contiguous_gradients": true }, "gradient_accumulation_steps": "auto", "gradient_clipping": "auto", "train_batch_size": "auto", "train_micro_batch_size_per_gpu": "auto" } ``` -------------------------------- ### Cloning DeepSpeed Examples and Finding JSON Files (Bash) Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/main_classes/deepspeed.mdx This snippet demonstrates how to clone the DeepSpeedExamples repository, navigate into it, and then use the `find` command to locate all JSON configuration files within the cloned directory. This is useful for exploring available DeepSpeed configuration examples. ```bash git clone https://github.com/microsoft/DeepSpeedExamples cd DeepSpeedExamples find . -name '*json' ``` -------------------------------- ### Installing Hugging Face Accelerate (Git Version) - Bash Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/run_scripts.mdx This command installs the latest development version of Hugging Face Accelerate directly from its GitHub repository. It is recommended due to Accelerate's rapid development, ensuring compatibility with the latest features and scripts. ```bash pip install git+https://github.com/huggingface/accelerate ``` -------------------------------- ### Running Example Tests with Pytest (Python) Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/docs/source/en/contributing.md This snippet outlines how to run tests specifically for the project's examples. It includes an optional `pip install` step for dependencies, followed by the `pytest` command to execute tests in the examples directory with parallel execution and verbose output. ```bash $ pip install -r examples/xxx/requirements.txt # only needed the first time $ python -m pytest -n auto --dist=loadfile -s -v ./examples/ ``` -------------------------------- ### Installing Hugging Face Transformers from Source Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/installation.mdx This command installs the bleeding-edge 'main' version of the Hugging Face Transformers library directly from its GitHub repository. This is useful for accessing the latest features or bug fixes before an official release. ```Bash pip install git+https://github.com/huggingface/transformers ``` -------------------------------- ### Running Example Tests with Pytest in Bash Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/CONTRIBUTING.md This snippet demonstrates how to run tests specifically for the examples provided in the repository using 'pytest'. It includes a preliminary step to install necessary dependencies for the examples, which is typically only required once. ```bash $ pip install -r examples/xxx/requirements.txt # only needed the first time $ python -m pytest -n auto --dist=loadfile -s -v ./examples/ ``` -------------------------------- ### Testing Accelerate Environment Setup (Bash) Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/examples/pytorch/semantic-segmentation/README.md This command verifies that the `accelerate` environment is correctly configured and ready for training. It performs checks to ensure compatibility with the specified hardware and settings, providing feedback on the setup. ```bash accelerate test ``` -------------------------------- ### Installing Hugging Face Hub Library Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/installation.mdx This command installs the `huggingface_hub` Python library into the current virtual environment using pip. This library provides programmatic access to download files from the Hugging Face Hub. ```bash python -m pip install huggingface_hub ``` -------------------------------- ### Verifying Source Installation of Transformers Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/installation.mdx This Python command, executed via the shell, verifies the successful installation of the Hugging Face Transformers library from source by running a sentiment analysis pipeline. ```Bash python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('I love you'))" ``` -------------------------------- ### Example Git Clone and Install Commands Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/ISSUES.md This snippet demonstrates a sequence of command-line instructions for cloning a Git repository, navigating into its directory, and installing a Python package in editable mode. It is used as an example of how to properly quote command-line code in documentation. ```bash git clone https://github.com/huggingface/transformers cd transformers pip install . ``` -------------------------------- ### Configuring Hugging Face Accelerate (Bash) Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/examples/pytorch/image-classification/README.md This command launches an interactive configuration utility for the Hugging Face `Accelerate` library. It guides the user through setting up their distributed training environment, including options for CPU, GPU, and TPU setups, and mixed precision. ```bash accelerate config ``` -------------------------------- ### Verifying Hugging Face Transformers Installation Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/installation.mdx This Python command, executed via the shell, verifies the successful installation of the Hugging Face Transformers library by downloading a pretrained sentiment analysis model and running an inference. ```Bash python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('we love you'))" ``` -------------------------------- ### Performing an Editable Installation for Development Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/installation.mdx These commands clone the Transformers repository, navigate into it, and perform an editable installation. An editable install links the cloned folder to Python's library paths, allowing direct modification of the source code for development or testing. ```Bash git clone https://github.com/huggingface/transformers.git cd transformers pip install -e . ``` -------------------------------- ### Installing Hugging Face Datasets Library from Source Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/examples/research_projects/jax-projects/README.md These commands navigate to the home directory, clone the Hugging Face Datasets repository, and install it in editable mode with streaming capabilities. Installing from source ensures access to the latest features and community contributions. ```bash $ cd ~/ $ git clone https://github.com/huggingface/datasets.git $ cd datasets $ pip install -e ".[streaming]" ``` -------------------------------- ### Installing Hugging Face Transformers via pip Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/installation.mdx This command installs the core Hugging Face Transformers library using pip. It is the standard way to get the stable version of the library. ```Bash pip install transformers ``` -------------------------------- ### Installing Transformers with TensorFlow CPU Support Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/installation.mdx This command installs the Hugging Face Transformers library along with CPU-only support for TensorFlow 2.0. This simplifies the installation process for TensorFlow users. ```Bash pip install transformers[tf-cpu] ``` -------------------------------- ### Installing Transformers with Flax Support Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/installation.mdx This command installs the Hugging Face Transformers library along with support for Flax. This is suitable for users working with the Flax deep learning framework. ```Bash pip install transformers[flax] ``` -------------------------------- ### Setting up PPLM Project Environment Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/examples/research_projects/pplm/README.md This snippet provides the necessary commands to clone the Hugging Face Transformers repository, install it, and then install additional dependencies like NLTK and TorchText. Finally, it navigates into the PPLM example directory, preparing the environment for running the PPLM code. ```bash git clone https://github.com/huggingface/transformers && cd transformers pip install . pip install nltk torchtext # additional requirements. cd examples/research_projects/pplm ``` -------------------------------- ### Installing Hugging Face Transformers via pip Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/docs/source/en/installation.mdx This command installs the core Hugging Face Transformers library using pip. It is the standard and recommended way to get the stable version of the library into your Python environment. ```Bash pip install transformers ``` -------------------------------- ### Installing JAX with TPU Support Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/examples/research_projects/jax-projects/README.md This command installs the JAX library with specific TPU support, fetching the necessary `libtpu` releases from Google's storage. This is crucial for running JAX models efficiently on TPU VMs. ```bash $ pip install "jax[tpu]>=0.2.16" -f https://storage.googleapis.com/jax-releases/libtpu_releases.html ``` -------------------------------- ### Previewing Transformers Documentation with Doc-Builder (Example) Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/docs/README.md This specific example demonstrates how to preview the documentation for the `transformers` library. It launches a local server, typically accessible at `http://localhost:3000`, allowing developers to view changes in real-time. ```bash doc-builder preview transformers docs/source/en/ ``` -------------------------------- ### Installing Neptune Client with conda (Bash) Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/examples/pytorch/README.md Installs the Neptune client library using the conda package manager from the conda-forge channel. This provides an alternative installation method to pip for setting up Neptune. ```bash conda install -c conda-forge neptune-client ``` -------------------------------- ### Initializing Massive Models with DeepSpeed ZeRO-3 in Python Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/main_classes/deepspeed.mdx This snippet demonstrates how to initialize a randomly configured model using the `deepspeed.zero.Init()` context manager. This method is crucial for handling models with trillions of parameters that may not fit into existing RAM, enabling faster initialization by sharding parameters across devices from the start. It uses `transformers.T5Config` and `T5ForConditionalGeneration` as an example. ```Python from transformers import T5ForConditionalGeneration, T5Config import deepspeed with deepspeed.zero.Init(): config = T5Config.from_pretrained("t5-small") model = T5ForConditionalGeneration(config) ``` -------------------------------- ### Activating the Virtual Environment (Shell) Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/examples/research_projects/visual_bert/README.md Activates the 'venv' virtual environment, making its Python interpreter and installed packages accessible. This command is crucial for ensuring subsequent installations and script executions use the isolated environment. ```Shell source venv/bin/activate ``` -------------------------------- ### Running DeepSpeed ZeRO-3 Inference for Translation Example Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/docs/source/en/main_classes/deepspeed.mdx This command executes the `run_translation.py` example script with DeepSpeed ZeRO-3 for inference. It uses a specific ZeRO-3 configuration, a `t5-small` model, and sets various evaluation and dataset parameters for English-Romanian translation. ```bash deepspeed examples/pytorch/translation/run_translation.py \ --deepspeed tests/deepspeed/ds_config_zero3.json \ --model_name_or_path t5-small --output_dir output_dir \ --do_eval --max_eval_samples 50 --warmup_steps 50 \ --max_source_length 128 --val_max_target_length 128 \ --overwrite_output_dir --per_device_eval_batch_size 4 \ --predict_with_generate --dataset_config "ro-en" --fp16 \ --source_lang en --target_lang ro --dataset_name wmt16 \ --source_prefix "translate English to Romanian: " ``` -------------------------------- ### Installing Hugging Face Datasets Library with Streaming Support Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/examples/research_projects/jax-projects/README.md These commands navigate to the home directory, clone the Hugging Face Datasets repository, change into its directory, and then install the library in editable mode with streaming capabilities. Installing from source ensures access to the latest features and bug fixes. ```bash $ cd ~/ $ git clone https://github.com/huggingface/datasets.git $ cd datasets $ pip install -e ".[streaming]" ``` -------------------------------- ### DeepSpeed ZeRO Inference Example (Multi-GPU/CPU Offload) Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/docs/source/en/main_classes/deepspeed.mdx This comprehensive Python script provides a self-contained example for performing DeepSpeed ZeRO inference on large models (e.g., `bigscience/T0_3B`) without relying on the Hugging Face `Trainer`. It illustrates how to handle models that don't fit on a single GPU by utilizing multiple GPUs or CPU memory offloading. The script includes distributed setup, model loading, and detailed comments on DeepSpeed configuration parameters like `bf16`/`fp16` and `offload_param` for performance and memory management. ```python #!/usr/bin/env python # This script demonstrates how to use Deepspeed ZeRO in an inference mode when one can't fit a model # into a single GPU # # 1. Use 1 GPU with CPU offload # 2. Or use multiple GPUs instead # # First you need to install deepspeed: pip install deepspeed # # Here we use a 3B "bigscience/T0_3B" model which needs about 15GB GPU RAM - so 1 largish or 2 # small GPUs can handle it. or 1 small GPU and a lot of CPU memory. # # To use a larger model like "bigscience/T0" which needs about 50GB, unless you have an 80GB GPU - # you will need 2-4 gpus. And then you can adapt the script to handle more gpus if you want to # process multiple inputs at once. # # The provided deepspeed config also activates CPU memory offloading, so chances are that if you # have a lot of available CPU memory and you don't mind a slowdown you should be able to load a # model that doesn't normally fit into a single GPU. If you have enough GPU memory the program will # run faster if you don't want offload to CPU - so disable that section then. # # To deploy on 1 gpu: # # deepspeed --num_gpus 1 t0.py # or: # python -m torch.distributed.run --nproc_per_node=1 t0.py # # To deploy on 2 gpus: # # deepspeed --num_gpus 2 t0.py # or: # python -m torch.distributed.run --nproc_per_node=2 t0.py from transformers import AutoTokenizer, AutoConfig, AutoModelForSeq2SeqLM from transformers.deepspeed import HfDeepSpeedConfig import deepspeed import os import torch os.environ["TOKENIZERS_PARALLELISM"] = "false" # To avoid warnings about parallelism in tokenizers # distributed setup local_rank = int(os.getenv("LOCAL_RANK", "0")) world_size = int(os.getenv("WORLD_SIZE", "1")) torch.cuda.set_device(local_rank) deepspeed.init_distributed() model_name = "bigscience/T0_3B" config = AutoConfig.from_pretrained(model_name) model_hidden_size = config.d_model # batch size has to be divisible by world_size, but can be bigger than world_size train_batch_size = 1 * world_size # ds_config notes # # - enable bf16 if you use Ampere or higher GPU - this will run in mixed precision and will be # faster. # # - for older GPUs you can enable fp16, but it'll only work for non-bf16 pretrained models - e.g. # all official t5 models are bf16-pretrained # # - set offload_param.device to "none" or completely remove the `offload_param` section if you don't # - want CPU offload # # - if using `offload_param` you can manually finetune stage3_param_persistence_threshold to control # - which params should remain on gpus - the larger the value the smaller the offload size # # For indepth info on Deepspeed config see # https://huggingface.co/docs/transformers/main/main_classes/deepspeed ``` -------------------------------- ### Setting up PPLM Project Environment (Bash) Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/examples/research_projects/pplm/README.md This snippet provides the necessary bash commands to clone the Hugging Face Transformers repository, install it, and then install additional Python dependencies like `nltk` and `torchtext`. Finally, it navigates into the PPLM example directory, preparing the environment for running the PPLM code. ```bash git clone https://github.com/huggingface/transformers && cd transformers pip install . pip install nltk torchtext # additional requirements. cd examples/research_projects/pplm ``` -------------------------------- ### Installing Requests Library Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/examples/research_projects/jax-projects/README.md This command installs the `requests` library using pip. While not directly related to JAX TPU setup, it's listed as a preceding step before installing JAX with TPU support, possibly for dependency resolution or a general prerequisite. ```bash $ pip install requests ``` -------------------------------- ### Searching DeepSpeed JSON Examples for Optimizer (Bash) Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/docs/source/en/main_classes/deepspeed.mdx This command searches for the term 'Lamb' (case-insensitive) within all JSON files found in the current directory and its subdirectories. It's used to locate DeepSpeed configuration examples related to specific optimizers like Lamb. ```bash grep -i Lamb $(find . -name '*json') ``` -------------------------------- ### Verifying JAX/Flax and Hugging Face Library Installation Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/examples/research_projects/jax-projects/README.md This Python script verifies the correct installation of `transformers`, `datasets`, and `jax` by loading a dataset, tokenizing dummy input, initializing a Flax model, and performing a forward pass. It confirms that streaming datasets and Flax model operations are functional. ```python from transformers import FlaxRobertaModel, RobertaTokenizerFast from datasets import load_dataset import jax dataset = load_dataset('oscar', "unshuffled_deduplicated_en", split='train', streaming=True) dummy_input = next(iter(dataset)["text"] tokenizer = RobertaTokenizerFast.from_pretrained("roberta-base") input_ids = tokenizer(dummy_input, return_tensors="np").input_ids[:, :10] model = FlaxRobertaModel.from_pretrained("julien-c/dummy-unknown") # run a forward pass, should return an object `FlaxBaseModelOutputWithPooling` model(input_ids) ``` -------------------------------- ### Installing Transformers from GitHub Source (main branch) Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/docs/source/en/installation.mdx This command installs the bleeding-edge `main` version of Hugging Face Transformers directly from its GitHub repository. This method is useful for accessing the latest features or bug fixes that have not yet been included in an official release. ```Bash pip install git+https://github.com/huggingface/transformers ``` -------------------------------- ### Running Example Tests with Unittest (Python) Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/docs/source/en/contributing.md This command shows how to execute tests located in the `examples` directory using Python's `unittest` module. It discovers tests within the specified source directory and runs them verbosely. ```bash $ python -m unittest discover -s examples -t examples -v ``` -------------------------------- ### Creating DeepSpeed Binary Wheel for Distribution Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/docs/source/en/main_classes/deepspeed.mdx This sequence of commands builds a distributable binary wheel (.whl) for DeepSpeed, targeting a specific CUDA architecture. This wheel can then be installed on other machines without recompilation, simplifying deployment across multiple environments. It's useful for consistent setups. ```bash git clone https://github.com/microsoft/DeepSpeed/ cd DeepSpeed rm -rf build TORCH_CUDA_ARCH_LIST="8.6" DS_BUILD_CPU_ADAM=1 DS_BUILD_UTILS=1 \ python setup.py build_ext -j8 bdist_wheel ``` -------------------------------- ### Installing Hugging Face Datasets from Source (Bash) Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/examples/research_projects/jax-projects/README.md This snippet provides a sequence of Bash commands to clone the Hugging Face Datasets repository and install it in editable mode with streaming support. This ensures access to the latest features for community week contributions. ```bash $ cd ~/ $ git clone https://github.com/huggingface/datasets.git $ cd datasets $ pip install -e ".[streaming]" ``` -------------------------------- ### Installing Comet.ml (Bash) Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/examples/pytorch/README.md This command installs the `comet_ml` Python package, which is necessary to integrate Comet.ml for experiment tracking and logging in machine learning projects. ```bash pip install comet_ml ``` -------------------------------- ### Installing Cookiecutter for Example Script Generation Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/templates/adding_a_new_example_script/README.md This command installs the `cookiecutter` tool using pip, which is a prerequisite for generating new example script templates. It's essential to have this tool installed before proceeding with template creation. ```Shell pip install cookiecutter ``` -------------------------------- ### Manually Configuring DeepSpeed ZeRO-2 Parameters (JSON) Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/main_classes/deepspeed.mdx This JSON configuration file provides an example of a manually configured DeepSpeed ZeRO-2 setup, where specific values are set for parameters like learning rate, betas, and warmup steps. It's intended to show typical parameter values for fp16 mixed precision, AdamW optimizer, WarmupLR scheduler, and CPU offloading for optimizer states, though auto-configuration is generally recommended. ```JSON { "fp16": { "enabled": true, "loss_scale": 0, "loss_scale_window": 1000, "initial_scale_power": 16, "hysteresis": 2, "min_loss_scale": 1 }, "optimizer": { "type": "AdamW", "params": { "lr": 3e-5, "betas": [0.8, 0.999], "eps": 1e-8, "weight_decay": 3e-7 } }, "scheduler": { "type": "WarmupLR", "params": { "warmup_min_lr": 0, "warmup_max_lr": 3e-5, "warmup_num_steps": 500 } }, "zero_optimization": { "stage": 2, "offload_optimizer": { "device": "cpu", "pin_memory": true }, "allgather_partitions": true, "allgather_bucket_size": 2e8, "overlap_comm": true, "reduce_scatter": true, "reduce_bucket_size": 2e8, "contiguous_gradients": true }, "steps_per_print": 2000, "wall_clock_breakdown": false } ``` -------------------------------- ### DeepSpeed ZeRO-3 Manually Configured JSON Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/docs/source/en/main_classes/deepspeed.mdx This JSON configuration file for DeepSpeed provides a fully manually set ZeRO-3 optimization setup. It explicitly defines `fp16` settings, `AdamW` optimizer parameters (learning rate, betas, eps, weight decay), and `WarmupLR` scheduler parameters. It also details `zero_optimization` settings including offloading to CPU and specific bucket and persistence thresholds, serving as an example of typical values. ```json { "fp16": { "enabled": true, "loss_scale": 0, "loss_scale_window": 1000, "initial_scale_power": 16, "hysteresis": 2, "min_loss_scale": 1 }, "optimizer": { "type": "AdamW", "params": { "lr": 3e-5, "betas": [0.8, 0.999], "eps": 1e-8, "weight_decay": 3e-7 } }, "scheduler": { "type": "WarmupLR", "params": { "warmup_min_lr": 0, "warmup_max_lr": 3e-5, "warmup_num_steps": 500 } }, "zero_optimization": { "stage": 3, "offload_optimizer": { "device": "cpu", "pin_memory": true }, "offload_param": { "device": "cpu", "pin_memory": true }, "overlap_comm": true, "contiguous_gradients": true, "sub_group_size": 1e9, "reduce_bucket_size": 1e6, "stage3_prefetch_bucket_size": 0.94e6, "stage3_param_persistence_threshold": 1e4, "stage3_max_live_parameters": 1e9, "stage3_max_reuse_distance": 1e9, "stage3_gather_16bit_weights_on_model_save": true }, "steps_per_print": 2000, "wall_clock_breakdown": false } ``` -------------------------------- ### Installing Hugging Face Hub Library (Python) Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/examples/research_projects/jax-projects/README.md This command installs the `huggingface_hub` library, which is essential for interacting with the Hugging Face Hub, including downloading models and files. It is a prerequisite for the subsequent code examples demonstrating file and repository downloads. ```bash pip install huggingface_hub ``` -------------------------------- ### Searching DeepSpeed JSON Examples for Specific Optimizer (Bash) Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/main_classes/deepspeed.mdx This command uses `grep` to perform a case-insensitive search (`-i`) for the term 'Lamb' within all JSON files found in the current directory and its subdirectories. It's used to locate DeepSpeed configuration examples that specifically mention the Lamb optimizer. ```bash grep -i Lamb $(find . -name '*json') ``` -------------------------------- ### Installing Core Hugging Face Libraries (Bash) Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/docs/source/en/quicktour.mdx This command installs the essential `transformers` library from Hugging Face, which provides access to pre-trained models, and the `datasets` library, used for loading and processing datasets. These are fundamental prerequisites for working with Hugging Face models. ```bash !pip install transformers datasets ``` -------------------------------- ### Switching Hugging Face Transformers Version (Bash) Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/run_scripts.mdx This command allows users to switch their local clone of the Hugging Face Transformers repository to a specific tagged version, such as v3.5.1. This is useful for running example scripts that are compatible with older versions of the library, ensuring consistency with historical examples. ```bash git checkout tags/v3.5.1 ``` -------------------------------- ### Installing Hugging Face Accelerate Library Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/examples/pytorch/summarization/README.md This command installs the Hugging Face Accelerate library directly from its GitHub repository. Accelerate is essential for enabling distributed training, mixed precision, and TPU support in PyTorch training scripts. ```bash pip install git+https://github.com/huggingface/accelerate ``` -------------------------------- ### Configuring DeepSpeed ZeRO-Infinity for NVMe Offloading (JSON) Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/main_classes/deepspeed.mdx This configuration example demonstrates how to enable NVMe offloading for both optimizer states and model parameters within DeepSpeed's ZeRO-Infinity (stage 3). It specifies the `nvme_path` for storage, `pin_memory` for performance, and various buffer and AIO settings. This setup allows for training extremely large models by extending memory beyond GPU and CPU to NVMe, with `aio` parameters requiring benchmarking for optimal performance. ```json { "zero_optimization": { "stage": 3, "offload_optimizer": { "device": "nvme", "nvme_path": "/local_nvme", "pin_memory": true, "buffer_count": 4, "fast_init": false }, "offload_param": { "device": "nvme", "nvme_path": "/local_nvme", "pin_memory": true, "buffer_count": 5, "buffer_size": 1e8, "max_in_cpu": 1e9 }, "aio": { "block_size": 262144, "queue_depth": 32, "thread_count": 1, "single_submit": false, "overlap_events": true }, "overlap_comm": true, "contiguous_gradients": true, "sub_group_size": 1e9, "reduce_bucket_size": "auto", "stage3_prefetch_bucket_size": "auto", "stage3_param_persistence_threshold": "auto", "stage3_max_live_parameters": 1e9, "stage3_max_reuse_distance": 1e9, "stage3_gather_16bit_weights_on_model_save": true } } ``` -------------------------------- ### Previewing Documentation with Doc-Builder (Generic) Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/docs/README.md This generic command initiates a local preview server for the documentation. Users need to replace `{package_name}` with the specific library (e.g., `transformers`) and `{path_to_docs}` with the path to the documentation source files. ```bash doc-builder preview {package_name} {path_to_docs} ``` -------------------------------- ### Installing Hugging Face Datasets from Source (Bash) Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/examples/research_projects/robust-speech-event/README.md These commands navigate to the home directory, clone the Hugging Face Datasets repository, change into its directory, and install it in editable mode with streaming capabilities. Installing from source ensures access to the latest features and bug fixes. ```bash $ cd ~/ $ git clone https://github.com/huggingface/datasets.git $ cd datasets $ pip install -e ".[streaming]" ``` -------------------------------- ### Complete DeepSpeed ZeRO Inference Example (Python) Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/main_classes/deepspeed.mdx This comprehensive script demonstrates how to perform DeepSpeed ZeRO inference for large models that do not fit on a single GPU, utilizing either CPU offload or multiple GPUs. It covers environment setup, distributed initialization, model loading, and provides detailed notes on DeepSpeed configuration parameters like bf16, fp16, and offload_param.device for performance optimization and memory management. The script is designed to be self-documenting with extensive comments. ```Python #!/usr/bin/env python # This script demonstrates how to use Deepspeed ZeRO in an inference mode when one can't fit a model # into a single GPU # # 1. Use 1 GPU with CPU offload # 2. Or use multiple GPUs instead # # First you need to install deepspeed: pip install deepspeed # # Here we use a 3B "bigscience/T0_3B" model which needs about 15GB GPU RAM - so 1 largish or 2 # small GPUs can handle it. or 1 small GPU and a lot of CPU memory. # # To use a larger model like "bigscience/T0" which needs about 50GB, unless you have an 80GB GPU - # you will need 2-4 gpus. And then you can adapt the script to handle more gpus if you want to # process multiple inputs at once. # # The provided deepspeed config also activates CPU memory offloading, so chances are that if you # have a lot of available CPU memory and you don't mind a slowdown you should be able to load a # model that doesn't normally fit into a single GPU. If you have enough GPU memory the program will # run faster if you don't want offload to CPU - so disable that section then. # # To deploy on 1 gpu: # # deepspeed --num_gpus 1 t0.py # or: # python -m torch.distributed.run --nproc_per_node=1 t0.py # # To deploy on 2 gpus: # # deepspeed --num_gpus 2 t0.py # or: # python -m torch.distributed.run --nproc_per_node=2 t0.py from transformers import AutoTokenizer, AutoConfig, AutoModelForSeq2SeqLM from transformers.deepspeed import HfDeepSpeedConfig import deepspeed import os import torch os.environ["TOKENIZERS_PARALLELISM"] = "false" # To avoid warnings about parallelism in tokenizers # distributed setup local_rank = int(os.getenv("LOCAL_RANK", "0")) world_size = int(os.getenv("WORLD_SIZE", "1")) torch.cuda.set_device(local_rank) deepspeed.init_distributed() model_name = "bigscience/T0_3B" config = AutoConfig.from_pretrained(model_name) model_hidden_size = config.d_model # batch size has to be divisible by world_size, but can be bigger than world_size train_batch_size = 1 * world_size # ds_config notes # # - enable bf16 if you use Ampere or higher GPU - this will run in mixed precision and will be # faster. # # - for older GPUs you can enable fp16, but it'll only work for non-bf16 pretrained models - e.g. # all official t5 models are bf16-pretrained # # - set offload_param.device to "none" or completely remove the `offload_param` section if you don't # - want CPU offload # # - if using `offload_param` you can manually finetune stage3_param_persistence_threshold to control # - which params should remain on gpus - the larger the value the smaller the offload size # # For indepth info on Deepspeed config see # https://huggingface.co/docs/transformers/main/main_classes/deepspeed # keeping the same format as json for consistency, except it uses lower case for true/false ``` -------------------------------- ### Creating a Python Virtual Environment (Shell) Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/examples/research_projects/visual_bert/README.md Initializes a new isolated Python virtual environment named 'venv'. This practice helps manage project-specific dependencies without interfering with global Python installations. ```Shell virtualenv venv ``` -------------------------------- ### Installing Hugging Face Accelerate Library Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/examples/pytorch/summarization/README.md This command installs the Hugging Face Accelerate library directly from its GitHub repository. Accelerate is essential for enabling distributed training, mixed precision, and TPU support in PyTorch models without modifying the training loop. ```bash pip install git+https://github.com/huggingface/accelerate ``` -------------------------------- ### Installing Cookiecutter for Template Generation Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/templates/adding_a_new_example_script/README.md This command installs the `cookiecutter` utility, which is a prerequisite for generating new example script templates for the Hugging Face Transformers library. It uses `pip`, Python's package installer, to fetch and install the tool. ```bash pip install cookiecutter ``` -------------------------------- ### Installing DeepSpeed via pip Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/main_classes/deepspeed.mdx This command installs the DeepSpeed library using pip, the Python package installer. It's the most straightforward method for a basic installation. ```bash pip install deepspeed ``` -------------------------------- ### Configuring Hugging Face Accelerate Environment Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/examples/pytorch/summarization/README.md This command initiates the interactive configuration process for Hugging Face Accelerate. It prompts the user to answer questions about their hardware setup (e.g., number of GPUs, mixed precision, TPU usage) to prepare the environment for distributed training. ```bash accelerate config ``` -------------------------------- ### Installing Python Dependencies Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/examples/research_projects/lxmert/demo.ipynb This snippet shows how to install the necessary Python packages listed in `requirements.txt` using pip. This is a common first step to set up the environment for the project. ```python # %pip install-r requirements.txt ``` -------------------------------- ### Installing DeepSpeed via pip Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/docs/source/en/main_classes/deepspeed.mdx This command installs the DeepSpeed library using the Python package installer, pip. It's the most straightforward method for a basic installation. ```bash pip install deepspeed ``` -------------------------------- ### Configuring DeepSpeed Gradient Accumulation with Auto-Enabled Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/main_classes/deepspeed.mdx This configuration sets 'gradient_accumulation_steps' to 'auto', allowing the DeepSpeed Trainer to automatically use the value from `args.gradient_accumulation_steps`. This simplifies setup by aligning DeepSpeed's behavior with the Trainer's command-line arguments. ```json { "gradient_accumulation_steps": "auto" } ``` -------------------------------- ### Configuring Hugging Face Accelerate - Bash Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/run_scripts.mdx This command initiates the configuration process for Hugging Face Accelerate, allowing users to set up their environment for distributed training across various hardware setups (CPU, multiple GPUs, TPUs). It creates and saves a configuration file. ```bash accelerate config ``` -------------------------------- ### Installing Example-Specific Python Requirements (Bash) Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/examples/research_projects/onnx/summarization/README.md This command installs additional Python packages specified in the 'requirements.txt' file, which are specific to this Bart + Beam Search to ONNX export example. It should be run after installing the core Transformers library. ```bash pip install -r requirements.txt ``` -------------------------------- ### Initializing DeepSpeed Engine with Hugging Face AutoModel (Python) Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/main_classes/deepspeed.mdx This code demonstrates the initial setup for integrating DeepSpeed with a Hugging Face model. It involves creating an HfDeepSpeedConfig object, loading a model configuration, instantiating the model, and then initializing the DeepSpeed engine with the model and configuration parameters. This setup is crucial for enabling DeepSpeed's distributed training or inference capabilities. ```Python dschf = HfDeepSpeedConfig(ds_config) # keep this object alive config = AutoConfig.from_pretrained("gpt2") model = AutoModel.from_config(config) engine = deepspeed.initialize(model=model, config_params=ds_config, ...) ``` -------------------------------- ### Previewing Transformers Documentation Locally (Bash) Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/README.md Starts a local web server to preview the generated documentation for the HuggingFace Transformers library. This command requires the package name and the path to the documentation source files, allowing developers to view changes in real-time before committing. ```bash doc-builder preview transformers docs/source/en/ ``` -------------------------------- ### Displaying Finetune Script Help (Bash) Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/examples/research_projects/seq2seq-distillation/README.md This command executes the `finetune.py` script with the `--help` flag, which displays all available command-line options and arguments. It is essential for understanding the configurable parameters and usage of the finetuning script. ```bash ./finetune.py --help ``` -------------------------------- ### Running Example Tests with Unittest in Bash Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/CONTRIBUTING.md This command executes the example test suite using Python's `unittest` module. It discovers tests within the `examples` directory (`-s examples`), sets the top-level directory to `examples` (`-t examples`), and provides verbose output (`-v`). ```bash $ python -m unittest discover -s examples -t examples -v ``` -------------------------------- ### Configuring DeepSpeed in Hugging Face Trainer - Python Source: https://github.com/patrickpynadath1/dab/blob/main/transformers/docs/source/en/main_classes/deepspeed.mdx This snippet demonstrates how to configure DeepSpeed within the Hugging Face `Trainer` by passing the path to a DeepSpeed configuration file (`ds_config_zero3.json`) to the `TrainingArguments`. It then initializes and starts the training process using the `Trainer`. ```Python training_args = TrainingArguments(..., deepspeed="ds_config_zero3.json") trainer = Trainer(...) trainer.train() ``` -------------------------------- ### Creating and Activating Python Virtual Environment (Shell) Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/examples/research_projects/lxmert/README.md These commands create a new isolated Python virtual environment named 'venv' and then activate it. This practice helps manage project-specific dependencies without interfering with system-wide Python installations. ```Shell virtualenv venv ``` ```Shell source venv/bin/activate ``` -------------------------------- ### Configuring Hugging Face Accelerate Environment Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/examples/pytorch/summarization/README.md This command initiates an interactive configuration process for Hugging Face Accelerate. It prompts the user to answer questions about their hardware setup (e.g., number of GPUs, mixed precision) to optimize distributed training. ```bash accelerate config ``` -------------------------------- ### Installing Hugging Face Hub Library Source: https://github.com/patrickpynadath1/dab/blob/main/baselines/BOLT/transformers/examples/research_projects/jax-projects/README.md This command installs the `huggingface_hub` Python library using pip. This library is crucial for programmatically interacting with the Hugging Face Hub, enabling operations like downloading models and datasets. ```Shell pip install huggingface_hub ```