TensorFlow BERT-base
개요
이 튜토리얼에서는 HuggingFace BERT-base 모델을 RBLN Python API를 사용하여 컴파일하고 추론하는 방법을 보여줍니다.
환경 설정 및 설치 확인
시작하기 전에 시스템 환경이 올바르게 구성되어 있으며, 필요한 모든 필수 패키지가 설치되어 있는지 확인하십시오. 다음 항목이 포함됩니다:
- 시스템 요구 사항:
- 필수 패키지:
- 설치 명령어:
| pip install tensorflow transformers numpy
pip install \
--extra-index-url https://pypi.rbln.ai/simple \
rebel-compiler==0.10.2
|
Note
rebel-compiler를 사용하려면 RBLN Portal 계정이 필요합니다.
- 위 명령은 Debian 계열 Linux(예: Ubuntu)에서 pip로 패키지를 설치하는 일반적인 절차를 전제로 합니다. OS나 환경이 다른 경우에는 설치 가이드에서 지원되는 설치 조합과 적용 가능한 명령을 확인하세요.
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 ].
|
참고