Copycats commited on
Commit
bce7330
โ€ข
1 Parent(s): c597b77

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +29 -31
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
- qa_text_pair['question'], qa_text_pair['context'],
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
- with torch.no_grad():
55
- pred = model(encodings['input_ids'], encodings['attention_mask'])
56
- start_logits, end_logits = pred.start_logits, pred.end_logits
57
- token_start_index, token_end_index = start_logits.argmax(dim=-1), end_logits.argmax(dim=-1)
58
- pred_ids = encodings['input_ids'][0][token_start_index: token_end_index + 1]
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
- # Decoding
66
- answer_text = tokenizer.decode(pred_ids) # text
67
- del encodings
 
 
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 ).to(device)
76
 
77
 
78
  ## Predict ##
79
- context = '''์• ํ”Œ M1(์˜์–ด: Apple M1)์€ ์• ํ”Œ์ด ์ž์‚ฌ์˜ ๋งคํ‚จํ† ์‹œ ์ปดํ“จํ„ฐ์šฉ์œผ๋กœ ์„ค๊ณ„ํ•œ ์ตœ์ดˆ์˜ ARM ๊ธฐ๋ฐ˜ SoC์ด๋‹ค.
80
- 4์„ธ๋Œ€ ๋งฅ๋ถ ์—์–ด, 5์„ธ๋Œ€ ๋งฅ ๋ฏธ๋‹ˆ, 13์ธ์น˜ 5์„ธ๋Œ€ ๋งฅ๋ถ ํ”„๋กœ, 5์„ธ๋Œ€ ์•„์ดํŒจ๋“œ ํ”„๋กœ์— ์„ ๋ณด์˜€๋‹ค. 5๋‚˜๋…ธ๋ฏธํ„ฐ ๊ณต์ •์„ ์‚ฌ์šฉํ•˜์—ฌ ์ œ์กฐ๋œ ์ตœ์ดˆ์˜ ๊ฐœ์ธ์šฉ ์ปดํ“จํ„ฐ ์นฉ์ด๋‹ค.
81
- ์• ํ”Œ์€ ์ €์ „๋ ฅ ์‹ค๋ฆฌ์ฝ˜์˜, ์„ธ๊ณ„์—์„œ ๊ฐ€์žฅ ๋น ๋ฅธ ARM ๊ธฐ๋ฐ˜์˜ ์ค‘์•™ ์ฒ˜๋ฆฌ ์žฅ์น˜(CPU) ์ฝ”์–ด, ๊ทธ๋ฆฌ๊ณ  ์„ธ๊ณ„ ์ตœ๊ณ ์˜ CPU ์„ฑ๋Šฅ ๋Œ€ ์™€ํŠธ๋ฅผ ๊ฐ–์ถ”๊ณ  ์žˆ๋‹ค๊ณ  ์ฃผ์žฅํ•˜๊ณ  ์žˆ๋‹ค.'''
82
- question = "์• ํ”Œ์ด m1์— ๋Œ€ํ•ด ์ฃผ์žฅํ•˜๋Š”๊ฑด ๋ญ์•ผ?"
 
 
83
 
84
  qa_text_pair = {'context':context, 'question':question}
85
  result = predict_answer(qa_text_pair)
86
- print('Answer Text: ', result['answer_text']) # ์ €์ „๋ ฅ ์‹ค๋ฆฌ์ฝ˜์˜, ์„ธ๊ณ„์—์„œ ๊ฐ€์žฅ ๋น ๋ฅธ ARM ๊ธฐ๋ฐ˜์˜ ์ค‘์•™ ์ฒ˜๋ฆฌ ์žฅ์น˜ ( CPU ) ์ฝ”์–ด, ๊ทธ๋ฆฌ๊ณ  ์„ธ๊ณ„ ์ตœ๊ณ ์˜ CPU ์„ฑ๋Šฅ ๋Œ€ ์™€ํŠธ๋ฅผ ๊ฐ–์ถ”๊ณ  ์žˆ๋‹ค๊ณ  ์ฃผ์žฅํ•˜๊ณ  ์žˆ๋‹ค.
87
- print('Answer Offset: ', result['answer_offset']) # (159, 246)
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