Indic BERT v1

repository·master·Indexed 43 days ago

https://github.com/ai4bharat/indic-bert-v1

A parameter-efficient implementation of ALBERT (A Lite BERT) for Indian languages. It supports pre-training, fine-tuning on benchmarks like GLUE, SQuAD, RACE, and IndicGLUE, and provides integration with TF-Hub and Hugging Face transformers. The project includes tools for SentencePiece vocabulary generation and supports acceleration via Cloud TPU.

Tokens
7.3K
Snippets
21
Records
25
Agent score
55%

What's inside Indic BERT

  1. Overview of IndicGLUE evaluation tasks

    master

    IndicGLUE is a benchmark for NLU performance in Indian languages. It includes the following tasks:

    • News Category Classification: Predict the genre of a news article.
    • Named Entity Recognition (NER): Recognize entities and their types in a sequence.
    • Headline Prediction: Predict the correct headline for a news article from candidates.
    • Wikipedia Section Title Prediction: Predict the correct title for a Wikipedia section.
    • Cloze-style Question Answering (WCQA): Predict a masked entity from a list of candidates.
    • Cross-lingual Sentence Retrieval (XSR): Retrieve a translation of a sentence from a set of candidates in a different language.
  2. Use Indic BERT with Hugging Face Transformers

    master

    The easiest way to use Indic BERT is through the Hugging Face transformers library. You can load the pre-trained tokenizer and model using AutoTokenizer and AutoModel.

    To preserve accents (vowel matras / diacritics) during tokenization, set the keep_accents parameter to True.

    # Required dependencies:
    # pip3 install transformers
    # pip3 install sentencepiece
    
    from transformers import AutoModel, AutoTokenizer
    
    # Standard loading
    tokenizer = AutoTokenizer.from_pretrained('ai4bharat/indic-bert')
    model = AutoModel.from_pretrained('ai4bharat/indic-bert')
    
    # Loading with accent preservation
    tokenizer = AutoTokenizer.from_pretrained('ai4bharat/indic-bert', keep_accents=True)
  3. Fine-tune ALBERT on GLUE benchmarks

    master

    Fine-tuning on GLUE can be done using the convenience script run_glue.sh or by calling albert.run_classifier directly for specific tasks (e.g., MNLI).

    When using run_classifier.py, you can substitute --init_checkpoint with --albert_hub_module_handle to load a model directly from TF-Hub.

    Note: The SentencePiece model file (--spm_model_file) can be found in the tar files or the assets folder of the TF-Hub module, typically named 30k-clean.model.

    pip install -r albert/requirements.txt
    python -m albert.run_classifier \
      --data_dir=... \
      --output_dir=... \
      --init_checkpoint=... \
      --albert_config_file=... \
      --spm_model_file=... \
      --do_train \
      --do_eval \
      --do_predict \
      --do_lower_case \
      --max_seq_length=128 \
      --optimizer=adamw \
      --task_name=MNLI \
      --warmup_step=1000 \
      --learning_rate=3e-5 \
      --train_step=10000 \
      --save_checkpoints_steps=100 \
      --train_batch_size=128
  4. Load a model from local files

    master

    Since the project uses the Hugging Face transformers library, you can load models from a local directory. To do this, your directory must contain the following three files:

    1. config.json: The configuration file in Hugging Face format.
    2. tok.model: The tokenizer model file (e.g., SentencePiece or WordPiece).
    3. pytorch_model.bin: The PyTorch binary containing the model parameters.

    To run evaluation or fine-tuning with these local files, use the following command structure:

    python3 -m fine_tune.cli --model <path to the directory containing pytorch_model.bin> --tokenizer_name <path to the tokenizer file> --config_name <path to the config file> --dataset <dataset name> --lang <iso lang code> --iglue_dir <base path to indic glue dir> --output_dir <output dir>
  5. Fine-tune ALBERT on SQuAD (v1 and v2)

    master

    Use run_squad_v1.py for SQuAD v1 and run_squad_v2.py for SQuAD v2. Both scripts support loading via --albert_hub_module_handle instead of --init_checkpoint.

    Common arguments for both:

    • --max_seq_length: Typically 384.
    • --doc_stride: Typically 128.
    • --do_train / --do_predict: Boolean flags to enable training or prediction.
    • --spm_model_file: Path to the SentencePiece model.
    # SQuAD v1
    python -m albert.run_squad_v1 \
      --albert_config_file=... \
      --output_dir=... \
      --train_file=... \
      --predict_file=... \
      --train_feature_file=... \
      --predict_feature_file=... \
      --predict_feature_left_file=... \
      --init_checkpoint=... \
      --spm_model_file=... \
      --do_lower_case \
      --max_seq_length=384 \
      --doc_stride=128 \
      --max_query_length=64 \
      --do_train=true \
      --do_predict=true \
      --train_batch_size=48 \
      --predict_batch_size=8 \
      --learning_rate=5e-5 \
      --num_train_epochs=2.0 \
      --warmup_proportion=.1 \
      --save_checkpoints_steps=5000 \
      --n_best_size=20 \
      --max_answer_length=30
  6. Use any Hugging Face model with the fine-tuning CLI

    master

    You can use the fine-tuning CLI to train on any model available on the Hugging Face Hub by providing its shortcut name to the --model argument. This is useful for testing different architectures or pre-trained weights with the Indic-BERT fine-tuning pipeline.

    If you are using a Google Colab notebook, you can pass these arguments within the argvec list in the provided notebook script.

    python3 -m fine_tune.cli --model <HF name*> --dataset <dataset name> --lang <iso lang code> --iglue_dir <base path to indic glue dir> --output_dir <output dir>
  7. Install and set up Indic BERT for local development

    master

    To run the Indic BERT code on a local VM, clone the repository and install the required dependencies via requirements.txt.

    git clone https://github.com/AI4Bharat/indic-bert
    cd indic-bert
    sudo pip3 install -r requirements.txt
  8. Fine-tune ALBERT on RACE

    master

    Use the albert.run_race module for the RACE benchmark. You can also use --albert_hub_module_handle to load from TF-Hub.

    Key arguments:

    • --max_seq_length: Typically 512.
    • --max_qa_length: Typically 128.
    • --learning_rate: Recommended 1e-5 for v2 models.
    python -m albert.run_race \
      --albert_config_file=... \
      --output_dir=... \
      --train_file=... \
      --eval_file=... \
      --data_dir=...\
      --init_checkpoint=... \
      --spm_model_file=... \
      --max_seq_length=512 \
      --max_qa_length=128 \
      --do_train \
      --do_eval \
      --train_batch_size=32 \
      --eval_batch_size=8 \
      --learning_rate=1e-5 \
      --train_step=12000 \
      --warmup_step=1000 \
      --save_checkpoints_steps=100
  9. Configure TPU support for Indic BERT

    master

    To run experiments on a TPU, you must update your .bashrc with specific environment variables and install pytorch-xla.

    1. Set Environment Variables:

    export PYTHONPATH="${PYTHONPATH}:/usr/share/tpu/models:<path to this repo"
    export PYTHONIOENCODING=utf-8
    export TPU_IP_ADDRESS="<TPU Internal Address"
    export TPU_NAME="grpc://$TPU_IP_ADDRESS:8470"
    export XRT_TPU_CONFIG="tpu_worker;0;$TPU_IP_ADDRESS:8470"
    export LD_LIBRARY_PATH="/usr/local/lib"

    2. Install pytorch-xla:

    curl https://raw.githubusercontent.com/pytorch/xla/master/contrib/scripts/env-setup.py -o pytorch-xla-env-setup.py
    sudo python3 pytorch-xla-env-setup.py --version nightly --apt-packages libomp5 libopenblas-dev
    export PYTHONPATH="${PYTHONPATH}:/usr/share/tpu/models:<path to this repo"
    export PYTHONIOENCODING=utf-8
    export TPU_IP_ADDRESS="<TPU Internal Address"
    export TPU_NAME="grpc://$TPU_IP_ADDRESS:8470"
    export XRT_TPU_CONFIG="tpu_worker;0;$TPU_IP_ADDRESS:8470"
    export LD_LIBRARY_PATH="/usr/local/lib"
    
    curl https://raw.githubusercontent.com/pytorch/xla/master/contrib/scripts/env-setup.py -o pytorch-xla-env-setup.py
    sudo python3 pytorch-xla-env-setup.py --version nightly --apt-packages libomp5 libopenblas-dev
  10. Convert TensorFlow checkpoints to PyTorch binary

    master

    If your model is currently in TensorFlow format, you must convert it to a PyTorch binary (pytorch_model.bin) before it can be used with the fine-tuning CLI. Use the transformers-cli convert command.

    Note: You must modify the --model_type and filenames in the command below to match your specific model architecture (e.g., change albert to bert if necessary).

    MODEL_DIR=$1
    
    # modify model_type and filenames accordingly
    transformers-cli convert --model_type albert \
      --tf_checkpoint $MODEL_DIR/tf_model \
      --config $MODEL_DIR/config.json \
      --pytorch_dump_output $MODEL_DIR/pytorch_model.bin
  11. Download Indic-GLUE datasets

    master

    Datasets for fine-tuning can be downloaded from the Indic-GLUE website. For example, to download and extract the wiki-cloze dataset, navigate to the indic-glue directory and use wget and tar.

    % cd indic-glue
    !wget https://storage.googleapis.com/ai4bharat-public-indic-nlp-corpora/evaluations/wiki-cloze.tar.gz
    !tar -xaf wiki-cloze.tar.gz
    % cd ..