Quick Start

Overview

Bidirectional Encoder Representation from Transformers (BERT) is a pre-training technique for natural language processing (NLP) that uses a bidirectional Transformer encoder.

The BERT-base model is a neural network architecture with 12 layers, 768 dimensions, 12 self-attention heads, and 110M parameters. Its overall framework is built by stacking multiple layers of Transformer encoders. The encoder at each layer consists of a multi-head-attention sub-layer and a feed-forward sub-layer. The primary function of each attention mechanism is to re-encode the target word based on its relevance to all words in the sentence. Therefore, the computation of each attention mechanism involves three steps: calculating the relevance between words, normalizing the relevance scores, and performing a weighted sum of all word encodings using the relevance scores to obtain the encoding of the target word. This document describes the PyTorch implementation of the BERT-base model.

Preparing a Training Environment

Preparing the Environment

  • Refer to the Installation Guide to prepare a training environment.

  • Install dependencies.

    1. Execute the following command in the root directory of the model source package to install the required dependencies.

      pip install -r requirements.txt
      
    2. Refer to the Ascend Apex Installation Guide to install Apex.

Preparing the Dataset

  1. Obtain the SQuADv1.1 dataset via the source code implementation link, and place the obtained dataset in the v1.1 directory created under the root directory of the source package.

    mkdir v1.1
    cd v1.1
    
  2. The dataset directory structure below is for reference.

    --- Root directory of the source package
       ---v1.1
          ---train-v1.1.json
          ---dev-v1.1.json
          ---evaluate-v1.1.py
    

    Note: The training script for this dataset is provided as a reference example only.

  3. Download the dictionary.

    In the dataset v1.1 directory, run the following command to create the data/uncased_L-24_H-1024_A-16 directory.

    mkdir data
    cd data
    mkdir uncased_L-24_H-1024_A-16
    cd uncased_L-24_H-1024_A-16
    

    Download the bert-base-uncased-vocab.txt dictionary file and place it in the uncased_L-24_H-1024_A-16 folder.

Obtaining the Pre-trained Model

  1. Obtain the pre-trained model via the source code implementation link.

  2. Verify the pre-trained model.

    bert-base:

    ---bert_base.pt
    

Starting Training

  1. Navigate to the root directory of the extracted source package.

    cd /${Model_folder_name} 
    
  2. Run the training script.

    This model supports single-node single-device training and single-node eight-device training.

    • Single-node single-device training

      Start single-device training:

      bash test/train_base_full_1p.sh --data_path=/xxx/v1.1 --ckpt_path=real_path   # BERT-base single-device accuracy
      
      bash test/train_base_performance_1p.sh --data_path=/xxx/v1.1 --ckpt_path=real_path   # BERT-base single-device performance
      
    • Single-node eight-device training

      Start eight-device training:

      bash test/train_base_full_8p.sh --data_path=/xxx/v1.1 --ckpt_path=real_path   # BERT-base eight-device accuracy
      
      bash test/train_base_performance_8p.sh --data_path=/xxx/v1.1 --ckpt_path=real_path   # BERT-base eight-device performance
      

--data_path specifies the dataset path, which must point to the v1.1 directory of the dataset.

--ckpt_path specifies the storage path for the pre-trained model. Only the directory containing the file is required; the pre-trained model file name does not need to be included.

After training is complete, the weight files are saved in the current directory, and the model training accuracy and performance information is output.

Training Results

Table 2 Training results

Note: The table below displays the performance data updated on May 10, 2025.

Name Precision Type FPS
Bert-Base 8p-Competitor FP16 2463
Bert-Base 8p-Atlas 900 A2 PoDc FP16 3118
Bert-Large 8p-Competitor FP16 870.4
Bert-Large 8p-Atlas 900 A2 PoDc FP16 1084

FAQ

The first step of the first training run is particularly slow, as it performs preprocessing on SQuAD. This process is very time-consuming and typically takes about ten minutes. After preprocessing is complete, a cache file is generated in the same directory as the dataset, which will significantly speed up subsequent training runs.