Update README.md
Browse files
README.md
CHANGED
@@ -37,54 +37,52 @@ license: cc-by-nc-4.0
|
|
37 |
import torch
|
38 |
from transformers import AutoModelForQuestionAnswering, AutoTokenizer
|
39 |
|
|
|
40 |
|
41 |
def predict_answer(qa_text_pair):
|
42 |
# Encoding
|
43 |
-
encodings = tokenizer(
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
)
|
51 |
-
encodings = {key: torch.tensor([val]).to(device) for key, val in encodings.items()}
|
52 |
|
53 |
# Predict
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
# Answer start/end offset of context.
|
61 |
-
answer_start_offset = int(encodings['offset_mapping'][0][token_start_index][0][0])
|
62 |
-
answer_end_offset = int(encodings['offset_mapping'][0][token_end_index][0][1])
|
63 |
-
answer_offset = (answer_start_offset, answer_end_offset)
|
64 |
|
65 |
-
#
|
66 |
-
|
67 |
-
|
|
|
|
|
68 |
return {'answer_text':answer_text, 'answer_offset':answer_offset}
|
69 |
|
70 |
|
71 |
## Load fine-tuned MRC model by HuggingFace Model Hub ##
|
72 |
-
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
73 |
HUGGINGFACE_MODEL_PATH = "bespin-global/klue-bert-base-aihub-mrc"
|
74 |
-
tokenizer = AutoTokenizer.from_pretrained(HUGGINGFACE_MODEL_PATH
|
75 |
-
model = AutoModelForQuestionAnswering.from_pretrained(HUGGINGFACE_MODEL_PATH
|
76 |
|
77 |
|
78 |
## Predict ##
|
79 |
-
context = '''์ ํ
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
83 |
|
84 |
qa_text_pair = {'context':context, 'question':question}
|
85 |
result = predict_answer(qa_text_pair)
|
86 |
-
print('Answer Text: ', result['answer_text']) #
|
87 |
-
print('Answer Offset: ', result['answer_offset']) # (
|
88 |
```
|
89 |
|
90 |
## Citing & Authors
|
37 |
import torch
|
38 |
from transformers import AutoModelForQuestionAnswering, AutoTokenizer
|
39 |
|
40 |
+
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
41 |
|
42 |
def predict_answer(qa_text_pair):
|
43 |
# Encoding
|
44 |
+
encodings = tokenizer(context, question,
|
45 |
+
max_length=512,
|
46 |
+
truncation=True,
|
47 |
+
padding="max_length",
|
48 |
+
return_token_type_ids=False,
|
49 |
+
return_offsets_mapping=True
|
50 |
+
)
|
51 |
+
encodings = {key: torch.tensor([val]).to(device) for key, val in encodings.items()}
|
|
|
52 |
|
53 |
# Predict
|
54 |
+
pred = model(encodings["input_ids"], attention_mask=encodings["attention_mask"])
|
55 |
+
start_logits, end_logits = pred.start_logits, pred.end_logits
|
56 |
+
token_start_index, token_end_index = start_logits.argmax(dim=-1), end_logits.argmax(dim=-1)
|
57 |
+
pred_ids = encodings["input_ids"][0][token_start_index: token_end_index + 1]
|
58 |
+
answer_text = tokenizer.decode(pred_ids)
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
+
# Offset
|
61 |
+
answer_start_offset = int(encodings['offset_mapping'][0][token_start_index][0][0])
|
62 |
+
answer_end_offset = int(encodings['offset_mapping'][0][token_end_index][0][1])
|
63 |
+
answer_offset = (answer_start_offset, answer_end_offset)
|
64 |
+
|
65 |
return {'answer_text':answer_text, 'answer_offset':answer_offset}
|
66 |
|
67 |
|
68 |
## Load fine-tuned MRC model by HuggingFace Model Hub ##
|
|
|
69 |
HUGGINGFACE_MODEL_PATH = "bespin-global/klue-bert-base-aihub-mrc"
|
70 |
+
tokenizer = AutoTokenizer.from_pretrained(HUGGINGFACE_MODEL_PATH)
|
71 |
+
model = AutoModelForQuestionAnswering.from_pretrained(HUGGINGFACE_MODEL_PATH).to(device)
|
72 |
|
73 |
|
74 |
## Predict ##
|
75 |
+
context = '''์ ํ M2(Apple M2)๋ ์ ํ์ด ์ค๊ณํ ์ค์ ์ฒ๋ฆฌ ์ฅ์น(CPU)์ ๊ทธ๋ํฝ ์ฒ๋ฆฌ ์ฅ์น(GPU)์ ARM ๊ธฐ๋ฐ ์์คํ
์ด๋ค.
|
76 |
+
์ธํ
์ฝ์ด(Intel Core)์์ ๋งฅํจํ ์ ์ปดํจํฐ์ฉ์ผ๋ก ์ค๊ณ๋ 2์ธ๋ ARM ์ํคํ
์ฒ์ด๋ค. ์ ํ์ 2022๋
6์ 6์ผ WWDC์์ ๋งฅ๋ถ ์์ด, 13์ธ์น ๋งฅ๋ถ ํ๋ก์ ํจ๊ป M2๋ฅผ ๋ฐํํ๋ค.
|
77 |
+
์ ํ M1์ ํ์์์ด๋ค. M2๋ TSMC์ 'ํฅ์๋ 5๋๋
ธ๋ฏธํฐ ๊ธฐ์ ' N5P ๊ณต์ ์ผ๋ก ๋ง๋ค์ด์ก์ผ๋ฉฐ, ์ด์ ์ธ๋ M1๋ณด๋ค 25% ์ฆ๊ฐํ 200์ต๊ฐ์ ํธ๋์ง์คํฐ๋ฅผ ํฌํจํ๊ณ ์์ผ๋ฉฐ, ์ต๋ 24๊ธฐ๊ฐ๋ฐ์ดํธ์ RAM๊ณผ 2ํ
๋ผ๋ฐ์ดํธ์ ์ ์ฅ๊ณต๊ฐ์ผ๋ก ๊ตฌ์ฑํ ์ ์๋ค.
|
78 |
+
8๊ฐ์ CPU ์ฝ์ด(์ฑ๋ฅ 4๊ฐ, ํจ์จ์ฑ 4๊ฐ)์ ์ต๋ 10๊ฐ์ GPU ์ฝ์ด๋ฅผ ๊ฐ์ง๊ณ ์๋ค. M2๋ ๋ํ ๋ฉ๋ชจ๋ฆฌ ๋์ญํญ์ 100 GB/s๋ก ์ฆ๊ฐ์ํจ๋ค.
|
79 |
+
์ ํ์ ๊ธฐ์กด M1 ๋๋น CPU๊ฐ ์ต๋ 18%, GPU๊ฐ ์ต๋ 35% ํฅ์๋๋ค๊ณ ์ฃผ์ฅํ๊ณ ์์ผ๋ฉฐ,[1] ๋ธ๋ฃธ๋ฒ๊ทธํต์ ์ M2๋งฅ์ค์ CPU ์ฝ์ด 12๊ฐ์ GPU ์ฝ์ด 38๊ฐ๊ฐ ํฌํจ๋ ๊ฒ์ด๋ผ๊ณ ๋ณด๋ํ๋ค.'''
|
80 |
+
question = "m2๊ฐ m1์ ๋นํด ์ผ๋ง๋ ์ข์์ก์ด?"
|
81 |
|
82 |
qa_text_pair = {'context':context, 'question':question}
|
83 |
result = predict_answer(qa_text_pair)
|
84 |
+
print('Answer Text: ', result['answer_text']) # ๊ธฐ์กด M1 ๋๋น CPU๊ฐ ์ต๋ 18 %, GPU๊ฐ ์ต๋ 35 % ํฅ์
|
85 |
+
print('Answer Offset: ', result['answer_offset']) # (410, 446)
|
86 |
```
|
87 |
|
88 |
## Citing & Authors
|