File size: 19,598 Bytes
17c6d62 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
<!--Copyright 2024 The HuggingFace Team. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
โ ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.
-->
# Transformers๋ก ์ฑํ
ํ๊ธฐ[[chatting-with-transformers]]
์ด ๊ธ์ ๋ณด๊ณ ์๋ค๋ฉด **์ฑํ
๋ชจ๋ธ**์ ๋ํด ์ด๋ ์ ๋ ์๊ณ ๊ณ์ค ๊ฒ์
๋๋ค.
์ฑํ
๋ชจ๋ธ์ด๋ ๋ฉ์ธ์ง๋ฅผ ์ฃผ๊ณ ๋ฐ์ ์ ์๋ ๋ํํ ์ธ๊ณต์ง๋ฅ์
๋๋ค.
๋ํ์ ์ผ๋ก ChatGPT๊ฐ ์๊ณ , ์ด์ ๋น์ทํ๊ฑฐ๋ ๋ ๋ฐ์ด๋ ์คํ์์ค ์ฑํ
๋ชจ๋ธ์ด ๋ง์ด ์กด์ฌํฉ๋๋ค.
์ด๋ฌํ ๋ชจ๋ธ๋ค์ ๋ฌด๋ฃ ๋ค์ด๋ก๋ํ ์ ์์ผ๋ฉฐ, ๋ก์ปฌ์์ ์คํํ ์ ์์ต๋๋ค.
ํฌ๊ณ ๋ฌด๊ฑฐ์ด ๋ชจ๋ธ์ ๊ณ ์ฑ๋ฅ ํ๋์จ์ด์ ๋ฉ๋ชจ๋ฆฌ๊ฐ ํ์ํ์ง๋ง,
์ ์ฌ์ GPU ํน์ ์ผ๋ฐ ๋ฐ์คํฌํ์ด๋ ๋
ธํธ๋ถ CPU์์๋ ์ ์๋ํ๋ ์ํ ๋ชจ๋ธ๋ค๋ ์์ต๋๋ค.
์ด ๊ฐ์ด๋๋ ์ฑํ
๋ชจ๋ธ์ ์ฒ์ ์ฌ์ฉํ๋ ๋ถ๋ค์๊ฒ ์ ์ฉํ ๊ฒ์
๋๋ค.
์ฐ๋ฆฌ๋ ๊ฐํธํ ๊ณ ์์ค(High-Level) "pipeline"์ ํตํด ๋น ๋ฅธ ์์ ๊ฐ์ด๋๋ฅผ ์งํํ ๊ฒ์
๋๋ค.
๊ฐ์ด๋์๋ ์ฑํ
๋ชจ๋ธ์ ๋ฐ๋ก ์์ํ ๋ ํ์ํ ๋ชจ๋ ์ ๋ณด๊ฐ ๋ด๊ฒจ ์์ต๋๋ค.
๋น ๋ฅธ ์์ ๊ฐ์ด๋ ์ดํ์๋ ์ฑํ
๋ชจ๋ธ์ด ์ ํํ ๋ฌด์์ธ์ง, ์ ์ ํ ๋ชจ๋ธ์ ์ ํํ๋ ๋ฐฉ๋ฒ๊ณผ,
์ฑํ
๋ชจ๋ธ์ ์ฌ์ฉํ๋ ๊ฐ ๋จ๊ณ์ ์ ์์ค(Low-Level) ๋ถ์ ๋ฑ ๋ ์์ธํ ์ ๋ณด๋ฅผ ๋ค๋ฃฐ ๊ฒ์
๋๋ค.
๋ํ ์ฑํ
๋ชจ๋ธ์ ์ฑ๋ฅ๊ณผ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ์ ์ต์ ํํ๋ ๋ฐฉ๋ฒ์ ๋ํ ํ๋ ์ ๊ณตํ ๊ฒ์
๋๋ค.
## ๋น ๋ฅธ ์์[[quickstart]]
์์ธํ ๋ณผ ์ฌ์ ๊ฐ ์๋ ๋ถ๋ค์ ์ํด ๊ฐ๋จํ ์์ฝํด ๋ณด๊ฒ ์ต๋๋ค:
์ฑํ
๋ชจ๋ธ์ ๋ํ ๋ฉ์ธ์ง๋ฅผ ๊ณ์ํด์ ์์ฑํด ๋๊ฐ๋๋ค.
์ฆ, ์งค๋งํ ์ฑํ
๋ฉ์ธ์ง๋ฅผ ๋ชจ๋ธ์๊ฒ ์ ๋ฌํ๋ฉด, ๋ชจ๋ธ์ ์ด๋ฅผ ๋ฐํ์ผ๋ก ์๋ต์ ์ถ๊ฐํ๋ฉฐ ๋ํ๋ฅผ ์ด์ด ๋๊ฐ๋๋ค.
์ด์ ์ค์ ๋ก ์ด๋ป๊ฒ ์๋ํ๋์ง ์ดํด๋ณด๊ฒ ์ต๋๋ค.
๋จผ์ , ์ฑํ
์ ๋ง๋ค์ด ๋ณด๊ฒ ์ต๋๋ค:
```python
chat = [
{"role": "system", "content": "You are a sassy, wise-cracking robot as imagined by Hollywood circa 1986."},
{"role": "user", "content": "Hey, can you tell me any fun things to do in New York?"}
]
```
์ฃผ๋ชฉํ์ธ์, ๋ํ๋ฅผ ์ฒ์ ์์ํ ๋ ์ ์ ๋ฉ์ธ์ง ์ด์ธ์๋, ๋ณ๋์ **์์คํ
** ๋ฉ์ธ์ง๊ฐ ํ์ํ ์ ์์ต๋๋ค.
๋ชจ๋ ์ฑํ
๋ชจ๋ธ์ด ์์คํ
๋ฉ์ธ์ง๋ฅผ ์ง์ํ๋ ๊ฒ์ ์๋์ง๋ง,
์ง์ํ๋ ๊ฒฝ์ฐ์๋ ์์คํ
๋ฉ์ธ์ง๋ ๋ํ์์ ๋ชจ๋ธ์ด ์ด๋ป๊ฒ ํ๋ํด์ผ ํ๋์ง๋ฅผ ์ง์ํ ์ ์์ต๋๋ค.
์๋ฅผ ๋ค์ด, ์ ์พํ๊ฑฐ๋ ์ง์งํ๊ณ ์ ํ ๋, ์งง์ ๋ต๋ณ์ด๋ ๊ธด ๋ต๋ณ์ ์ํ ๋ ๋ฑ์ ์ค์ ํ ์ ์์ต๋๋ค.
์์คํ
๋ฉ์ธ์ง๋ฅผ ์๋ตํ๊ณ
"You are a helpful and intelligent AI assistant who responds to user queries."
์ ๊ฐ์ ๊ฐ๋จํ ํ๋กฌํํธ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ๋ ๊ฐ๋ฅํฉ๋๋ค.
์ฑํ
์ ์์ํ๋ค๋ฉด ๋ํ๋ฅผ ์ด์ด ๋๊ฐ๋ ๊ฐ์ฅ ๋น ๋ฅธ ๋ฐฉ๋ฒ์ [`TextGenerationPipeline`]๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์
๋๋ค.
ํ๋ฒ `LLaMA-3`๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฅผ ์์ฐํด ๋ณด๊ฒ ์ต๋๋ค.
์ฐ์ `LLaMA-3`๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ์น์ธ์ด ํ์ํฉ๋๋ค. [๊ถํ ์ ์ฒญ](https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct)์ ํ๊ณ Hugging Face ๊ณ์ ์ผ๋ก ๋ก๊ทธ์ธํ ํ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
๋ํ ์ฐ๋ฆฌ๋ `device_map="auto"`๋ฅผ ์ฌ์ฉํฉ๋๋ค. GPU ๋ฉ๋ชจ๋ฆฌ๊ฐ ์ถฉ๋ถํ๋ค๋ฉด ๋ก๋๋ ๊ฒ์
๋๋ค.
๊ทธ๋ฆฌ๊ณ ๋ฉ๋ชจ๋ฆฌ ์ ์ฝ์ ์ํด dtype์ `torch.bfloat16`์ผ๋ก ์ค์ ํ ๊ฒ์
๋๋ค.
```python
import torch
from transformers import pipeline
pipe = pipeline("text-generation", "meta-llama/Meta-Llama-3-8B-Instruct", torch_dtype=torch.bfloat16, device_map="auto")
response = pipe(chat, max_new_tokens=512)
print(response[0]['generated_text'][-1]['content'])
```
์ดํ ์คํ์ ํ๋ฉด ์๋์ ๊ฐ์ด ์ถ๋ ฅ๋ฉ๋๋ค:
```text
(sigh) Oh boy, you're asking me for advice? You're gonna need a map, pal! Alright,
alright, I'll give you the lowdown. But don't say I didn't warn you, I'm a robot, not a tour guide!
So, you wanna know what's fun to do in the Big Apple? Well, let me tell you, there's a million
things to do, but I'll give you the highlights. First off, you gotta see the sights: the Statue of
Liberty, Central Park, Times Square... you know, the usual tourist traps. But if you're lookin' for
something a little more... unusual, I'd recommend checkin' out the Museum of Modern Art. It's got
some wild stuff, like that Warhol guy's soup cans and all that jazz.
And if you're feelin' adventurous, take a walk across the Brooklyn Bridge. Just watch out for
those pesky pigeons, they're like little feathered thieves! (laughs) Get it? Thieves? Ah, never mind.
Now, if you're lookin' for some serious fun, hit up the comedy clubs in Greenwich Village. You might
even catch a glimpse of some up-and-coming comedians... or a bunch of wannabes tryin' to make it big. (winks)
And finally, if you're feelin' like a real New Yorker, grab a slice of pizza from one of the many amazing
pizzerias around the city. Just don't try to order a "robot-sized" slice, trust me, it won't end well. (laughs)
So, there you have it, pal! That's my expert advice on what to do in New York. Now, if you'll
excuse me, I've got some oil changes to attend to. (winks)
```
์ฑํ
์ ๊ณ์ํ๋ ค๋ฉด, ์์ ์ ๋ต์ฅ์ ์ถ๊ฐํ๋ฉด ๋ฉ๋๋ค.
ํ์ดํ๋ผ์ธ์์ ๋ฐํ๋ `response` ๊ฐ์ฒด์๋ ํ์ฌ๊น์ง ๋ชจ๋ ์ฑํ
์ ํฌํจํ๊ณ ์์ผ๋ฏ๋ก
๋ฉ์ธ์ง๋ฅผ ์ถ๊ฐํ๊ณ ๋ค์ ์ ๋ฌํ๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค.
```python
chat = response[0]['generated_text']
chat.append(
{"role": "user", "content": "Wait, what's so wild about soup cans?"}
)
response = pipe(chat, max_new_tokens=512)
print(response[0]['generated_text'][-1]['content'])
```
์ดํ ์คํ์ ํ๋ฉด ์๋์ ๊ฐ์ด ์ถ๋ ฅ๋ฉ๋๋ค:
```text
(laughs) Oh, you're killin' me, pal! You don't get it, do you? Warhol's soup cans are like, art, man!
It's like, he took something totally mundane, like a can of soup, and turned it into a masterpiece. It's
like, "Hey, look at me, I'm a can of soup, but I'm also a work of art!"
(sarcastically) Oh, yeah, real original, Andy.
But, you know, back in the '60s, it was like, a big deal. People were all about challenging the
status quo, and Warhol was like, the king of that. He took the ordinary and made it extraordinary.
And, let me tell you, it was like, a real game-changer. I mean, who would've thought that a can of soup could be art? (laughs)
But, hey, you're not alone, pal. I mean, I'm a robot, and even I don't get it. (winks)
But, hey, that's what makes art, art, right? (laughs)
```
์ด ํํ ๋ฆฌ์ผ์ ํ๋ฐ๋ถ์์๋ ์ฑ๋ฅ๊ณผ ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ,
๊ทธ๋ฆฌ๊ณ ์ฌ์ฉ์์ ํ์์ ๋ง๋ ์ฑํ
๋ชจ๋ธ ์ ํ๊ณผ ๊ฐ์ ๊ตฌ์ฒด์ ์ธ ์ฃผ์ ๋ค์ ๋ค๋ฃฐ ๊ฒ์
๋๋ค.
## ์ฑํ
๋ชจ๋ธ ๊ณ ๋ฅด๊ธฐ[[choosing-a-chat-model]]
[Hugging Face Hub](https://huggingface.co/models?pipeline_tag=text-generation&sort=trending)๋ ์ฑํ
๋ชจ๋ธ์ ๋ค์ํ๊ฒ ์ ๊ณตํ๊ณ ์์ต๋๋ค.
์ฒ์ ์ฌ์ฉํ๋ ์ฌ๋์๊ฒ๋ ๋ชจ๋ธ์ ์ ํํ๊ธฐ๊ฐ ์ด๋ ค์ธ์ง ๋ชจ๋ฆ
๋๋ค.
ํ์ง๋ง ๊ฑฑ์ ํ์ง ๋ง์ธ์! ๋ ๊ฐ์ง๋ง ๋ช
์ฌํ๋ฉด ๋ฉ๋๋ค:
- ๋ชจ๋ธ์ ํฌ๊ธฐ๋ ์คํ ์๋์ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ผ์ฌ ์ ์๋์ง ์ฌ๋ถ๋ฅผ ๊ฒฐ์ .
- ๋ชจ๋ธ์ด ์์ฑํ ์ถ๋ ฅ์ ํ์ง.
์ผ๋ฐ์ ์ผ๋ก ์ด๋ฌํ ์์๋ค์ ์๊ด๊ด๊ณ๊ฐ ์์ต๋๋ค. ๋ ํฐ ๋ชจ๋ธ์ผ์๋ก ๋ ๋ฐ์ด๋ ์ฑ๋ฅ์ ๋ณด์ด๋ ๊ฒฝํฅ์ด ์์ง๋ง, ๋์ผํ ํฌ๊ธฐ์ ๋ชจ๋ธ์ด๋ผ๋ ์ ์๋ฏธํ ์ฐจ์ด๊ฐ ๋ ์ ์์ต๋๋ค!
### ๋ชจ๋ธ์ ๋ช
์นญ๊ณผ ํฌ๊ธฐ[[size-and-model-naming]]
๋ชจ๋ธ์ ํฌ๊ธฐ๋ ๋ชจ๋ธ ์ด๋ฆ์ ์๋ ์ซ์๋ก ์ฝ๊ฒ ์ ์ ์์ต๋๋ค.
์๋ฅผ ๋ค์ด, "8B" ๋๋ "70B"์ ๊ฐ์ ์ซ์๋ ๋ชจ๋ธ์ **ํ๋ผ๋ฏธํฐ** ์๋ฅผ ๋ํ๋
๋๋ค.
์์ํ๋ ๊ฒฝ์ฐ๊ฐ ์๋๋ผ๋ฉด, ํ๋ผ๋ฏธํฐ ํ๋๋น ์ฝ 2๋ฐ์ดํธ์ ๋ฉ๋ชจ๋ฆฌ๊ฐ ํ์ํ๋ค๊ณ ์์ ๊ฐ๋ฅํฉ๋๋ค.
๋ฐ๋ผ์ 80์ต ๊ฐ์ ํ๋ผ๋ฏธํฐ๋ฅผ ๊ฐ์ง "8B" ๋ชจ๋ธ์ 16GB์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฐจ์งํ๋ฉฐ, ์ถ๊ฐ์ ์ธ ์ค๋ฒํค๋๋ฅผ ์ํ ์ฝ๊ฐ์ ์ฌ์ ๊ฐ ํ์ํฉ๋๋ค.
์ด๋ 3090์ด๋ 4090์ ๊ฐ์ 24GB์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ฐ์ถ ํ์ด์๋ GPU์ ์ ํฉํฉ๋๋ค.
์ผ๋ถ ์ฑํ
๋ชจ๋ธ์ "Mixture of Experts" ๋ชจ๋ธ์
๋๋ค.
์ด๋ฌํ ๋ชจ๋ธ์ ํฌ๊ธฐ๋ฅผ "8x7B" ๋๋ "141B-A35B"์ ๊ฐ์ด ๋ค๋ฅด๊ฒ ํ์ํ๊ณค ํฉ๋๋ค.
์ซ์๊ฐ ๋ค์ ๋ชจํธํ๋ค ๋๊ปด์ง ์ ์์ง๋ง, ์ฒซ ๋ฒ์งธ ๊ฒฝ์ฐ์๋ ์ฝ 56์ต(8x7) ๊ฐ์ ํ๋ผ๋ฏธํฐ๊ฐ ์๊ณ ,
๋ ๋ฒ์งธ ๊ฒฝ์ฐ์๋ ์ฝ 141์ต ๊ฐ์ ํ๋ผ๋ฏธํฐ๊ฐ ์๋ค๊ณ ํด์ํ ์ ์์ต๋๋ค.
์์ํ๋ ํ๋ผ๋ฏธํฐ๋น ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ 8๋นํธ, 4๋นํธ, ๋๋ ๊ทธ ์ดํ๋ก ์ค์ด๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
์ด ์ฃผ์ ์ ๋ํด์๋ ์๋์ [๋ฉ๋ชจ๋ฆฌ ๊ณ ๋ ค์ฌํญ](#memory-considerations) ์ฑํฐ์์ ๋ ์์ธํ ๋ค๋ฃฐ ์์ ์
๋๋ค.
### ๊ทธ๋ ๋ค๋ฉด ์ด๋ค ์ฑํ
๋ชจ๋ธ์ด ๊ฐ์ฅ ์ข์๊น์?[[but-which-chat-model-is-best]]
๋ชจ๋ธ์ ํฌ๊ธฐ ์ธ์๋ ๊ณ ๋ คํ ์ ์ด ๋ง์ต๋๋ค.
์ด๋ฅผ ํ๋์ ์ดํด๋ณด๋ ค๋ฉด **๋ฆฌ๋๋ณด๋**๋ฅผ ์ฐธ๊ณ ํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
๊ฐ์ฅ ์ธ๊ธฐ ์๋ ๋ฆฌ๋๋ณด๋ ๋ ๊ฐ์ง๋ [OpenLLM Leaderboard](https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard)์ [LMSys Chatbot Arena Leaderboard](https://chat.lmsys.org/?leaderboard)์
๋๋ค.
LMSys ๋ฆฌ๋๋ณด๋์๋ ๋
์ ๋ชจ๋ธ๋ ํฌํจ๋์ด ์์ผ๋,
`license` ์ด์์ ์ ๊ทผ ๊ฐ๋ฅํ ๋ชจ๋ธ์ ์ ํํ ํ
[Hugging Face Hub](https://huggingface.co/models?pipeline_tag=text-generation&sort=trending)์์ ๊ฒ์ํด ๋ณด์ธ์.
### ์ ๋ฌธ ๋ถ์ผ[[specialist-domains]]
์ผ๋ถ ๋ชจ๋ธ์ ์๋ฃ ๋๋ ๋ฒ๋ฅ ํ
์คํธ์ ๊ฐ์ ํน์ ๋๋ฉ์ธ์ด๋ ๋น์์ด๊ถ ์ธ์ด์ ํนํ๋์ด ์๊ธฐ๋ ํฉ๋๋ค.
์ด๋ฌํ ๋๋ฉ์ธ์์ ์์
ํ ๊ฒฝ์ฐ ํนํ๋ ๋ชจ๋ธ์ด ์ข์ ์ฑ๋ฅ์ ๋ณด์ผ ์ ์์ต๋๋ค.
ํ์ง๋ง ํญ์ ๊ทธ๋ด ๊ฒ์ด๋ผ ๋จ์ ํ๊ธฐ๋ ํ๋ญ๋๋ค.
ํนํ ๋ชจ๋ธ์ ํฌ๊ธฐ๊ฐ ์๊ฑฐ๋ ์ค๋๋ ๋ชจ๋ธ์ธ ๊ฒฝ์ฐ,
์ต์ ๋ฒ์ฉ ๋ชจ๋ธ์ด ๋ ๋ฐ์ด๋ ์ ์์ต๋๋ค.
๋คํํ๋ [domain-specific leaderboards](https://huggingface.co/blog/leaderboard-medicalllm)๊ฐ ์ ์ฐจ ๋ฑ์ฅํ๊ณ ์์ด, ํน์ ๋๋ฉ์ธ์ ์ต๊ณ ์ ๋ชจ๋ธ์ ์ฝ๊ฒ ์ฐพ์ ์ ์์ ๊ฒ์
๋๋ค.
## ํ์ดํ๋ผ์ธ ๋ด๋ถ๋ ์ด๋ป๊ฒ ๋์ด์๋๊ฐ?[[what-happens-inside-the-pipeline]]
์์ ๋น ๋ฅธ ์์์์๋ ๊ณ ์์ค(High-Level) ํ์ดํ๋ผ์ธ์ ์ฌ์ฉํ์์ต๋๋ค.
์ด๋ ๊ฐํธํ ๋ฐฉ๋ฒ์ด์ง๋ง, ์ ์ฐ์ฑ์ ๋จ์ด์ง๋๋ค.
์ด์ ๋ ์ ์์ค(Low-Level) ์ ๊ทผ ๋ฐฉ์์ ํตํด ๋ํ์ ํฌํจ๋ ๊ฐ ๋จ๊ณ๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
์ฝ๋ ์ํ๋ก ์์ํ ํ ์ด๋ฅผ ๋ถ์ํด ๋ณด๊ฒ ์ต๋๋ค:
```python
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
# ์
๋ ฅ๊ฐ์ ์ฌ์ ์ ์ค๋นํด ๋์ต๋๋ค
chat = [
{"role": "system", "content": "You are a sassy, wise-cracking robot as imagined by Hollywood circa 1986."},
{"role": "user", "content": "Hey, can you tell me any fun things to do in New York?"}
]
# 1: ๋ชจ๋ธ๊ณผ ํ ํฌ๋์ด์ ๋ฅผ ๋ถ๋ฌ์ต๋๋ค
model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct", device_map="auto", torch_dtype=torch.bfloat16)
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct")
# 2: ์ฑํ
ํ
ํ๋ฆฟ์ ์ ์ฉํฉ๋๋ค
formatted_chat = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
print("Formatted chat:\n", formatted_chat)
# 3: ์ฑํ
์ ํ ํฐํํฉ๋๋ค (๋ฐ๋ก ์ด์ ๊ณผ์ ์์ tokenized=True๋ก ์ค์ ํ๋ฉด ํ๊บผ๋ฒ์ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค)
inputs = tokenizer(formatted_chat, return_tensors="pt", add_special_tokens=False)
# ํ ํฐํ๋ ์
๋ ฅ๊ฐ์ ๋ชจ๋ธ์ด ์ฌ๋ผ์ ์๋ ๊ธฐ๊ธฐ(CPU/GPU)๋ก ์ฎ๊น๋๋ค.
inputs = {key: tensor.to(model.device) for key, tensor in inputs.items()}
print("Tokenized inputs:\n", inputs)
# 4: ๋ชจ๋ธ๋ก๋ถํฐ ์๋ต์ ์์ฑํฉ๋๋ค
outputs = model.generate(**inputs, max_new_tokens=512, temperature=0.1)
print("Generated tokens:\n", outputs)
# 5: ๋ชจ๋ธ์ด ์ถ๋ ฅํ ํ ํฐ์ ๋ค์ ๋ฌธ์์ด๋ก ๋์ฝ๋ฉํฉ๋๋ค
decoded_output = tokenizer.decode(outputs[0][inputs['input_ids'].size(1):], skip_special_tokens=True)
print("Decoded output:\n", decoded_output)
```
์ฌ๊ธฐ์๋ ๊ฐ ๋ถ๋ถ์ด ์์ฒด ๋ฌธ์๊ฐ ๋ ์ ์์ ๋งํผ ๋ง์ ๋ด์ฉ์ด ๋ด๊ฒจ ์์ต๋๋ค!
๋๋ฌด ์์ธํ ์ค๋ช
ํ๊ธฐ๋ณด๋ค๋ ๋์ ๊ฐ๋
์ ๋ค๋ฃจ๊ณ , ์ธ๋ถ ์ฌํญ์ ๋งํฌ๋ ๋ฌธ์์์ ๋ค๋ฃจ๊ฒ ์ต๋๋ค.
์ฃผ์ ๋จ๊ณ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
1. [๋ชจ๋ธ](https://huggingface.co/learn/nlp-course/en/chapter2/3)๊ณผ [ํ ํฌ๋์ด์ ](https://huggingface.co/learn/nlp-course/en/chapter2/4?fw=pt)๋ฅผ Hugging Face Hub์์ ๋ก๋ํฉ๋๋ค.
2. ๋ํ๋ ํ ํฌ๋์ด์ ์ [์ฑํ
ํ
ํ๋ฆฟ](https://huggingface.co/docs/transformers/main/en/chat_templating)์ ์ฌ์ฉํ์ฌ ์์์ ๊ตฌ์ฑํฉ๋๋ค.
3. ๊ตฌ์ฑ๋ ์ฑํ
์ ํ ํฌ๋์ด์ ๋ฅผ ์ฌ์ฉํ์ฌ [ํ ํฐํ](https://huggingface.co/learn/nlp-course/en/chapter2/4)๋ฉ๋๋ค.
4. ๋ชจ๋ธ์์ ์๋ต์ [์์ฑ](https://huggingface.co/docs/transformers/en/llm_tutorial)ํฉ๋๋ค.
5. ๋ชจ๋ธ์ด ์ถ๋ ฅํ ํ ํฐ์ ๋ค์ ๋ฌธ์์ด๋ก ๋์ฝ๋ฉํฉ๋๋ค.
## ์ฑ๋ฅ, ๋ฉ๋ชจ๋ฆฌ์ ํ๋์จ์ด[[performance-memory-and-hardware]]
์ด์ ๋๋ถ๋ถ์ ๋จธ์ ๋ฌ๋ ์์
์ด GPU์์ ์คํ๋๋ค๋ ๊ฒ์ ์์ค ๊ฒ๋๋ค.
๋ค์ ๋๋ฆฌ๊ธฐ๋ ํด๋ CPU์์ ์ฑํ
๋ชจ๋ธ์ด๋ ์ธ์ด ๋ชจ๋ธ๋ก๋ถํฐ ํ
์คํธ๋ฅผ ์์ฑํ๋ ๊ฒ๋ ๊ฐ๋ฅํฉ๋๋ค.
ํ์ง๋ง ๋ชจ๋ธ์ GPU ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ ค๋์ ์๋ง ์๋ค๋ฉด, GPU๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ผ๋ฐ์ ์ผ๋ก ๋ ์ ํธ๋๋ ๋ฐฉ์์
๋๋ค.
### ๋ฉ๋ชจ๋ฆฌ ๊ณ ๋ ค์ฌํญ[[memory-considerations]]
๊ธฐ๋ณธ์ ์ผ๋ก, [`TextGenerationPipeline`]์ด๋ [`AutoModelForCausalLM`]๊ณผ ๊ฐ์
Hugging Face ํด๋์ค๋ ๋ชจ๋ธ์ `float32` ์ ๋ฐ๋(Precision)๋ก ๋ก๋ํฉ๋๋ค.
์ด๋ ํ๋ผ๋ฏธํฐ๋น 4๋ฐ์ดํธ(32๋นํธ)๋ฅผ ํ์๋ก ํ๋ฏ๋ก,
80์ต ๊ฐ์ ํ๋ผ๋ฏธํฐ๋ฅผ ๊ฐ์ง "8B" ๋ชจ๋ธ์ ์ฝ 32GB์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ์๋ก ํ๋ค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค.
ํ์ง๋ง ์ด๋ ๋ญ๋น์ผ ์ ์์ต๋๋ค!
๋๋ถ๋ถ์ ์ต์ ์ธ์ด ๋ชจ๋ธ์ ํ๋ผ๋ฏธํฐ๋น 2๋ฐ์ดํธ๋ฅผ ์ฌ์ฉํ๋ "bfloat16" ์ ๋ฐ๋(Precision)๋ก ํ์ต๋ฉ๋๋ค.
ํ๋์จ์ด๊ฐ ์ด๋ฅผ ์ง์ํ๋ ๊ฒฝ์ฐ(Nvidia 30xx/Axxx ์ด์),
`torch_dtype` ํ๋ผ๋ฏธํฐ๋ก ์์ ๊ฐ์ด `bfloat16` ์ ๋ฐ๋(Precision)๋ก ๋ชจ๋ธ์ ๋ก๋ํ ์ ์์ต๋๋ค.
๋ํ, 16๋นํธ๋ณด๋ค ๋ ๋ฎ์ ์ ๋ฐ๋(Precision)๋ก ๋ชจ๋ธ์ ์์ถํ๋
"์์ํ(quantization)" ๋ฐฉ๋ฒ์ ์ฌ์ฉํ ์๋ ์์ต๋๋ค.
์ด ๋ฐฉ๋ฒ์ ๋ชจ๋ธ์ ๊ฐ์ค์น๋ฅผ ์์ค ์์ถํ์ฌ ๊ฐ ํ๋ผ๋ฏธํฐ๋ฅผ 8๋นํธ,
4๋นํธ ๋๋ ๊ทธ ์ดํ๋ก ์ค์ผ ์ ์์ต๋๋ค.
ํนํ 4๋นํธ์์ ๋ชจ๋ธ์ ์ถ๋ ฅ์ด ๋ถ์ ์ ์ธ ์ํฅ์ ๋ฐ์ ์ ์์ง๋ง,
๋ ํฌ๊ณ ๊ฐ๋ ฅํ ์ฑํ
๋ชจ๋ธ์ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ฆฌ๊ธฐ ์ํด ์ด ๊ฐ์ ํธ๋ ์ด๋์คํ๋ฅผ ๊ฐ์ํ ๊ฐ์น๊ฐ ์์ต๋๋ค.
์ด์ `bitsandbytes`๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฅผ ์ค์ ๋ก ํ์ธํด ๋ณด๊ฒ ์ต๋๋ค:
```python
from transformers import AutoModelForCausalLM, BitsAndBytesConfig
quantization_config = BitsAndBytesConfig(load_in_8bit=True) # You can also try load_in_4bit
model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct", device_map="auto", quantization_config=quantization_config)
```
์์ ์์
์ `pipeline` API์๋ ์ ์ฉ ๊ฐ๋ฅํฉ๋๋ค:
```python
from transformers import pipeline, BitsAndBytesConfig
quantization_config = BitsAndBytesConfig(load_in_8bit=True) # You can also try load_in_4bit
pipe = pipeline("text-generation", "meta-llama/Meta-Llama-3-8B-Instruct", device_map="auto", model_kwargs={"quantization_config": quantization_config})
```
`bitsandbytes` ์ธ์๋ ๋ชจ๋ธ์ ์์ํํ๋ ๋ค์ํ ๋ฐฉ๋ฒ์ด ์์ต๋๋ค.
์์ธํ ๋ด์ฉ์ [Quantization guide](./quantization)๋ฅผ ์ฐธ์กฐํด ์ฃผ์ธ์.
### ์ฑ๋ฅ ๊ณ ๋ ค์ฌํญ[[performance-considerations]]
<Tip>
์ธ์ด ๋ชจ๋ธ ์ฑ๋ฅ๊ณผ ์ต์ ํ์ ๋ํ ๋ณด๋ค ์์ธํ ๊ฐ์ด๋๋ [LLM Inference Optimization](./llm_optims)์ ์ฐธ๊ณ ํ์ธ์.
</Tip>
์ผ๋ฐ์ ์ผ๋ก ๋ ํฐ ์ฑํ
๋ชจ๋ธ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ ๋ง์ด ์๊ตฌํ๊ณ ,
์๋๋ ๋๋ ค์ง๋ ๊ฒฝํฅ์ด ์์ต๋๋ค. ๊ตฌ์ฒด์ ์ผ๋ก ๋งํ์๋ฉด,
์ฑํ
๋ชจ๋ธ์์ ํ
์คํธ๋ฅผ ์์ฑํ ๋๋ ์ปดํจํ
ํ์๋ณด๋ค **๋ฉ๋ชจ๋ฆฌ ๋์ญํญ**์ด ๋ณ๋ชฉ ํ์์ ์ผ์ผํค๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค.
์ด๋ ๋ชจ๋ธ์ด ํ ํฐ์ ํ๋์ฉ ์์ฑํ ๋๋ง๋ค ํ๋ผ๋ฏธํฐ๋ฅผ ๋ฉ๋ชจ๋ฆฌ์์ ์ฝ์ด์ผ ํ๊ธฐ ๋๋ฌธ์
๋๋ค.
๋ฐ๋ผ์ ์ฑํ
๋ชจ๋ธ์์ ์ด๋น ์์ฑํ ์ ์๋ ํ ํฐ ์๋ ๋ชจ๋ธ์ด ์์นํ ๋ฉ๋ชจ๋ฆฌ์ ๋์ญํญ์ ๋ชจ๋ธ์ ํฌ๊ธฐ๋ก ๋๋ ๊ฐ์ ๋น๋กํฉ๋๋ค.
์์ ์์ ์์๋ ๋ชจ๋ธ์ด bfloat16 ์ ๋ฐ๋(Precision)๋ก ๋ก๋๋ ๋ ์ฉ๋์ด ์ฝ 16GB์์ต๋๋ค.
์ด ๊ฒฝ์ฐ, ๋ชจ๋ธ์ด ์์ฑํ๋ ๊ฐ ํ ํฐ๋ง๋ค 16GB๋ฅผ ๋ฉ๋ชจ๋ฆฌ์์ ์ฝ์ด์ผ ํ๋ค๋ ์๋ฏธ์
๋๋ค.
์ด ๋ฉ๋ชจ๋ฆฌ ๋์ญํญ์ ์๋น์์ฉ CPU์์๋ 20-100GB/sec,
์๋น์์ฉ GPU๋ Intel Xeon, AMD Threadripper/Epyc,
์ ํ ์ค๋ฆฌ์ฝ๊ณผ ๊ฐ์ ํน์ CPU์์๋ 200-900GB/sec,
๋ฐ์ดํฐ ์ผํฐ GPU์ธ Nvidia A100์ด๋ H100์์๋ ์ต๋ 2-3TB/sec์ ์ด๋ฅผ ์ ์์ต๋๋ค.
์ด๋ฌํ ์ ๋ณด๋ ๊ฐ์ ํ๋์จ์ด์์ ์์ฑ ์๋๋ฅผ ์์ํ๋ ๋ฐ ๋์์ด ๋ ๊ฒ์
๋๋ค.
๋ฐ๋ผ์ ํ
์คํธ ์์ฑ ์๋๋ฅผ ๊ฐ์ ํ๋ ค๋ฉด ๊ฐ์ฅ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ ๋ชจ๋ธ์ ํฌ๊ธฐ๋ฅผ ์ค์ด๊ฑฐ๋(์ฃผ๋ก ์์ํ๋ฅผ ์ฌ์ฉ),
๋ฉ๋ชจ๋ฆฌ ๋์ญํญ์ด ๋ ๋์ ํ๋์จ์ด๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์
๋๋ค.
์ด ๋์ญํญ ๋ณ๋ชฉ ํ์์ ํผํ ์ ์๋ ๊ณ ๊ธ ๊ธฐ์ ๋ ์ฌ๋ฌ ๊ฐ์ง ์์ต๋๋ค.
๊ฐ์ฅ ์ผ๋ฐ์ ์ธ ๋ฐฉ๋ฒ์ [๋ณด์กฐ ์์ฑ](https://huggingface.co/blog/assisted-generation), "์ถ์ธก ์ํ๋ง"์ด๋ผ๊ณ ๋ถ๋ฆฌ๋ ๊ธฐ์ ์
๋๋ค.
์ด ๊ธฐ์ ์ ์ข
์ข
๋ ์์ "์ด์ ๋ชจ๋ธ"์ ์ฌ์ฉํ์ฌ ์ฌ๋ฌ ๊ฐ์ ๋ฏธ๋ ํ ํฐ์ ํ ๋ฒ์ ์ถ์ธกํ ํ,
์ฑํ
๋ชจ๋ธ๋ก ์์ฑ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํฉ๋๋ค.
๋ง์ฝ ์ฑํ
๋ชจ๋ธ์ด ์ถ์ธก์ ํ์ธํ๋ฉด, ํ ๋ฒ์ ์์ ํ์์ ์ฌ๋ฌ ๊ฐ์ ํ ํฐ์ ์์ฑํ ์ ์์ด
๋ณ๋ชฉ ํ์์ด ํฌ๊ฒ ์ค์ด๋ค๊ณ ์์ฑ ์๋๊ฐ ๋นจ๋ผ์ง๋๋ค.
๋ง์ง๋ง์ผ๋ก, "Mixture of Experts" (MoE) ๋ชจ๋ธ์ ๋ํด์๋ ์ง๊ณ ๋์ด๊ฐ ๋ณด๋๋ก ํฉ๋๋ค.
Mixtral, Qwen-MoE, DBRX์ ๊ฐ์ ์ธ๊ธฐ ์๋ ์ฑํ
๋ชจ๋ธ์ด ๋ฐ๋ก MoE ๋ชจ๋ธ์
๋๋ค.
์ด ๋ชจ๋ธ๋ค์ ํ ํฐ์ ์์ฑํ ๋ ๋ชจ๋ ํ๋ผ๋ฏธํฐ๊ฐ ์ฌ์ฉ๋์ง ์์ต๋๋ค.
์ด๋ก ์ธํด MoE ๋ชจ๋ธ์ ์ ์ฒด ํฌ๊ธฐ๊ฐ ์๋นํ ํด ์ ์์ง๋ง,
์ฐจ์งํ๋ ๋ฉ๋ชจ๋ฆฌ ๋์ญํญ์ ๋ฎ์ ํธ์
๋๋ค.
๋ฐ๋ผ์ ๋์ผํ ํฌ๊ธฐ์ ์ผ๋ฐ "์กฐ๋ฐํ(Dense)" ๋ชจ๋ธ๋ณด๋ค ๋ช ๋ฐฐ ๋น ๋ฅผ ์ ์์ต๋๋ค.
ํ์ง๋ง ๋ณด์กฐ ์์ฑ๊ณผ ๊ฐ์ ๊ธฐ์ ์ MoE ๋ชจ๋ธ์์ ๋นํจ์จ์ ์ผ ์ ์์ต๋๋ค.
์๋ก์ด ์ถ์ธก๋ ํ ํฐ์ด ์ถ๊ฐ๋๋ฉด์ ๋ ๋ง์ ํ๋ผ๋ฏธํฐ๊ฐ ํ์ฑํ๋๊ธฐ ๋๋ฌธ์,
MoE ์ํคํ
์ฒ๊ฐ ์ ๊ณตํ๋ ์๋ ์ด์ ์ด ์์๋ ์ ์์ต๋๋ค. |