콘텐츠로 이동

TensorFlow BERT-base

Overview

이 튜토리얼에서는 Hugging Face BERT-base 모델을 RBLN Python API를 사용하여 컴파일하고 추론하는 방법을 보여줍니다.

Setup & Installation

시작하기 전에 시스템 환경이 올바르게 구성되어 있으며, 필요한 모든 필수 패키지가 설치되어 있는지 확인하십시오. 다음 항목이 포함됩니다:

Note

RBLN SDK는 .whl 패키지로 배포됩니다. RBLN 컴파일러와 런타임을 사용하려면 RBLN Portal 계정이 필요합니다.

RBLN Python API 사용

모델 컴파일

transformers 라이브러리에서 TFBertForMaskedLM을 임포트하고 TensorFlow BERT-base 모델을 인스턴스화한 후 tf.function으로 변환합니다.
그 다음 RBLN 컴파일러로 모델을 컴파일하고 디스크에 저장합니다.

from transformers import TFBertForMaskedLM  
import tensorflow as tf  
import rebel  # RBLN Compiler  

# Instantiate the TensorFlow BERT-base model  
model = TFBertForMaskedLM.from_pretrained('bert-base-uncased')  
func = tf.function(  
    lambda input_ids, attention_mask, token_type_ids: model(  
        input_ids, attention_mask, token_type_ids  
    )  
)  

# Compile the model  
MAX_SEQ_LEN = 128  
input_info = [  
    ('input_ids', [1, MAX_SEQ_LEN], tf.int64),  
    ('attention_mask', [1, MAX_SEQ_LEN], tf.int64),  
    ('token_type_ids', [1, MAX_SEQ_LEN], tf.int64),  
]  
compiled_model = rebel.compile_from_tf_function(  
    func,  
    input_info,  
    npu='RBLN-CA12'  
)  

# Save the compiled model to disk  
compiled_model.save('bert_base.rbln')  

모델 추론 및 결과 확인

BertTokenizer를 사용해 입력 텍스트를 토크나이즈하고, RBLN 런타임으로 컴파일된 모델을 로드한 뒤 추론을 실행하여 마스크된 토큰에 대한 예측 단어를 출력합니다.

from transformers import BertTokenizer, pipeline  
import tensorflow as tf  
import rebel  # RBLN Runtime  

# Prepare the input  
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')  
text = 'The capital of Korea is [MASK].'  
MAX_SEQ_LEN = 128  
inputs = tokenizer(text, return_tensors='np', padding='max_length', max_length=MAX_SEQ_LEN)  

# Load the compiled model  
module = rebel.Runtime('bert_base.rbln')  
# Run inference  
out = module.run(**inputs)  

# Check results  
mask_token_index = tf.where((inputs.input_ids == tokenizer.mask_token_id)[0])  
selected_logits = tf.gather_nd(out[0], indices=mask_token_index)  
predicted_token_id = tf.math.argmax(selected_logits, axis=-1)  
print('Masked word is [', tokenizer.decode(predicted_token_id), '].')  

결과는 다음과 같이 표시됩니다:

Masked word is [ Seoul ].

Summary and References

이 튜토리얼에서는 TensorFlow와 RBLN Python API를 사용하여 Hugging Face BERT-base 모델을 컴파일하고 추론하는 방법을 보여주었습니다.
컴파일된 모델은 RBLN NPU에서 마스크드 언어 모델링 태스크를 효율적으로 수행할 수 있습니다.

References: