hf_mirrors/alephbert-base-openmind
Model Introduction
Files & versions
Analysis

AlephBERT

Hebrew Language Model

State-of-the-art language model for Hebrew. Based on Google's BERT architecture (Devlin et al. 2018).

Use in Openmind

from openmind import pipeline, AutoTokenizer, is_torch_npu_available
from openmind_hub import snapshot_download
import torch.nn.functional as F
from torch import Tensor
import openmind
import torch
import argparse
import time

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--model_name_or_path",
        type=str,
        help="Path to model",
        default="jeffding/alephbert-base-openmind",
    )
    args = parser.parse_args()
    return args

def main():
    args = parse_args()
    model_path = args.model_name_or_path

    if is_torch_npu_available():
        device = "npu:0"
    else:
        device = "cpu"
    
    start_time = time.time()
    
    tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=True)
    pipe = pipeline('fill-mask', model=model_path, torch_dtype=torch.bfloat16, device_map=device)
    MASK_TOKEN = tokenizer.mask_token
    result = pipe("Hello I'm a {} model.".format(MASK_TOKEN))
    print(result)
    
    end_time = time.time()
    print(f"硬件环境:{device},推理执行时间:{end_time - start_time}秒")
    
if __name__ == "__main__":
    main()

How to use

from transformers import BertModel, BertTokenizerFast


alephbert_tokenizer = BertTokenizerFast.from_pretrained('onlplab/alephbert-base')
alephbert = BertModel.from_pretrained('onlplab/alephbert-base')

# if not finetuning - disable dropout
alephbert.eval()

Training data

  1. OSCAR (Ortiz, 2019) Hebrew section (10 GB text, 20 million sentences).
  2. Hebrew dump of Wikipedia (650 MB text, 3 million sentences).
  3. Hebrew Tweets collected from the Twitter sample stream (7 GB text, 70 million sentences).

Training procedure

Trained on a DGX machine (8 V100 GPUs) using the standard huggingface training procedure.

Since the larger part of our training data is based on tweets we decided to start by optimizing using Masked Language Model loss only.

To optimize training time we split the data into 4 sections based on max number of tokens:

  1. num tokens < 32 (70M sentences)
  2. 32 <= num tokens < 64 (12M sentences)
  3. 64 <= num tokens < 128 (10M sentences)
  4. 128 <= num tokens < 512 (1.5M sentences)

Each section was first trained for 5 epochs with an initial learning rate set to 1e-4. Then each section was trained for another 5 epochs with an initial learning rate set to 1e-5, for a total of 10 epochs.

Total training time was 8 days.