머신러닝AI2024. 5. 1. 10:53

윈도우(windows)에서도 easy_diffusion과 kohya_ss를 어느 정도 실행해서 이미지 합성에 대한 작업을 진행할 수 있다.

Windows 10 pro + RTX 4080 (536.23 driver) 기준의 환경에서 실행해 보았다. 명령은 모두 command 상에서 실행한다

(윈도우키+R을 누른 후 cmd를 실행하면 명령 실행이 가능하다)

 

 

1. easy_diffusion

 

 

이 설치는 너무나 간단하다. 아래 링크에서 Windows용 Easy Diffusion 최신 버전을 받자

https://github.com/easydiffusion/easydiffusion/tree/main#installation

 

이 글을 쓰는 시점에서는 Easy Diffusion 3.0이 최신이다. 아래 Download for Windows 버튼을 누르고 실행해주면 된다. linux/mac/windows 동일한 방식으로 간단하게 실행해서 웹 인터페이스로 생성해볼 수 있다.

 

[easy_diffusion의 윈도우 설치 가이드 포함 페이지]

 

[easy_diffusion을 실행 설치하면 나오는 화면이다.내부에 conda와 git을 자체로 가지고 있다]
[설치가 끝나면 역시 자동으로 브라우저가 떠서 실행해볼 수 있게 된다.]

 

실제 사용 관련해서는 다른 글들을 참조하면 된다. models/stable-diffusion/ 에 사용하는 모델파일을 넣어 사용해보자(civit.ai에서 checkpoint 모델들을 크기가 크지만, 다양하게 다운로드 받을 수 있다. https://infoengineer.tistory.com/122 를 참조하자)

 

2. kohya_ss

 

모델을 학습하기 위한 kohya_ss에 대한 설치이다.

 

A. python 10와 git이 필요하다.

 

    개인적으로는 Anaconda설치를 권한다. 따라서 Anaconda 사이트에서 윈도우용 Anaconda를 설치하고

    아래와 같이 실행한다. git은 윈도우용 git을 다운로드받아( https://git-scm.com/download/win ) 기본옵션으로 설치하여 실행하면 된다. (만약에 python 3.10을 직접 설치해서 곧바로 진행한다고 하면 그렇게 해도 상관없다.)

 

    D:\dev\conda> conda create -n kohya_ss python=3.10

    D:\dev\conda> conda activate kohya_ss

 

B. git으로 kohya_ss를 checkout 하고 setup.bat를 실행한다.

 

    D:\dev> git clone https://github.com/bmaltais/kohya_ss.git 

    D:\dev> cd kohya_ss

    D:\dev\kohya_ss> setup.bat

 

 이때 NVidia GPU 사용자는 2번을 눌러 cudnn을 최신으로 받아주자. 시간이 조금 걸린다(고속화에 도움이 된다고 한다)

 그런 후 1번을 선택해 Kohya_ss를 GUI로 실행한다.

 

setup.bat 실행 후 화면, 2번과 1번을 차례대로 선택해주자.

끝나면 7. Exit Setup을 선택해서 나오면 된다.

 

C. 아래와 같이 실행하면 내부에 웹서버가 뜨면서 우리가 활용할 수 있는 단계로 진입한다.

 

 D:\dev\kohya_ss> gui.bat

kohya_ss 윈도우 실행화면

 

위의 마지막 부분에 표시된 http://127.0.0.1:7860 주소를 브라우저로 열어보자. kohya_ss의 gui가 보인다. 해당 사용법 관련해서는 연관 글을 참조한다. ( https://infoengineer.tistory.com/134 )

kohya_ss의 윈도우가 보인다.

 

 

 

 

 

 

반응형
Posted by 작동미학
머신러닝AI2024. 4. 27. 17:16

오픈소스 LLM 모델을 local에서 띄워서 구동하는 가장 손쉬운 방법은 ollama 이고 지난번에 소개한 적이 있다.

https://infoengineer.tistory.com/135

macos에서도 구동되기 때문에 너무나 간단하게 llama3, gemma, phi-3, command-r 등 어느정도 한글이 되는 모델들을 다운로드 받아서 구동시킬 수 있다.

 

그리고 일단 이렇게 구동되면 langchain과도 바로 연결된다.

 

$ ollama run llama3:instruct
>>> give me a joke
Here's one:
Why don't eggs tell jokes?
(wait for it...)
Because they'd crack each other up!
Hope that made you smile!
>>> Send a message (/? for help)

 

이렇게 구동이 되면 내부에 API 서버가 이미 구동되어 대기 상태가 된다(ollama serve 명령으로도 띄우는 것이 가능하다)

이후에는 아래와 같이 시험해볼 수 있다.

 

$ curl http://localhost:11434/api/generate -d '{
  "model": "llama3",
  "prompt":"Why is the sky blue?"
}'
{"model":"llama3","created_at":"2024-04-27T08:10:00.736586071Z","response":"What","done":false}
{"model":"llama3","created_at":"2024-04-27T08:10:00.746743823Z","response":" a","done":false}
{"model":"llama3","created_at":"2024-04-27T08:10:00.757109205Z","response":" great","done":false}
{"model":"llama3","created_at":"2024-04-27T08:10:00.768475258Z","response":" question","done":false}
....

{"model":"llama3","created_at":"2024-04-27T08:10:04.094431458Z","response":".","done":false}
{"model":"llama3","created_at":"2024-04-27T08:10:04.104777568Z","response":"","done":true,"context":[128006,.....,128009],"total_duration":3490085755,"load_duration":2410324,"prompt_eval_count":11,"prompt_eval_duration":75044000,"eval_count":327,"eval_duration":3368118000}

 

아니면 이제 langchain을 사용할 수도 있다.

 

$ cat > langchain_ollama_stream.py 
from langchain.callbacks.manager import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain_community.llms import Ollama

llm = Ollama(
    model="llama3", callback_manager=CallbackManager([StreamingStdOutCallbackHandler()])
)
llm("The first man on the summit of Mount Everest, the highest peak on Earth, was ...")

$ python3 langchain_ollama_stream.py 

....

....

 

그 외에도 다양한 방식의 langchain사용이 가능하다. 잘 응용해서 사용해보자.

 

 

1) 간단한 invoke

 

from langchain_community.llms import Ollama

llm = Ollama(model="llama3")

llm.invoke("Tell me a joke")

 

 

2) 간단한 stream 형식의 출력

 

from langchain_community.llms import Ollama

llm = Ollama(model="llama3")

query = "Tell me a joke"

for chunks in llm.stream(query):
    print(chunks, end="")

 

반응형
Posted by 작동미학
머신러닝AI2024. 4. 27. 16:46

AI로 무엇을 하다보면 linux에 GPU로 환경을 구성하는데, 도대체 잘 이해가 가지 않는 것이 이 버전 관계다.

Ubuntu 버전, GPU 버전, NVidia driver버전, cuda 버전, cudnn 버전... 등등 헷갈린다. 이제 그 관계를 이해해보자.

 

결론은 지금 '24년초 시점에서는 Ubuntu 22.10, NVidia driver는 GPU버전에 맞는것(홈페이지 참조), CUDA는 11.8을 중심으로 설치하자. cudnn은 8.7 정도를 선택하면 되고 쉽게 바꿀 수 있다.

 

1. 먼저 CUDA 버전이 중요하다. CUDA 11.8이 가장 많이 호환되는 CUDA 버전이다.

 

 판단기준 : 내가 쓰는 프레임웍이 지원하는 CUDA는 무엇인가? 대개 11.8이다

 

 CUDA는 최근에 11, 12시리즈가 주력으로 사용되는데, 많은 오픈소스 프로젝트들이 11버전을 지원한다.

 특히 우리의 가장 중요한 친구 pytorch 2.1 미만 버전들이 CUDA 11에 의존한다. tensorflow나 주력으로 쓰는 framework들이 지원하는 버전을 확인해서 만약에 CUDA 12를 지원한다면 12버전을 쓸 수도 있겠다. 하지만 아직은 최신 버전들만 간신히 지원하는 CUDA12를 쓸 용기는 없다. 우리는 기존 소스코드를 돌려야 하기 때문이다.

 

 그러니 CUDA 11.8을 설치하자. 참고로 아래는 tensorflow의 버전별 CUDA + cudnn 버전이다. 각 프레임웍들이 각각 이런 정보를 공개한다.

https://www.tensorflow.org/install/source?hl=ko#gpu

 

2. NVidia driver는 아래와 같은 CUDA와의 호환 관계를 갖는다. 최신 CUDA는 최신 NVidia driver가 필요하다.

 

 판단기준 : 내가 쓰는 CUDA 버전이 최신일수록 최신의 NVidia driver를 설치해야 하는데, 내가 가진 GPU카드별로 지원하는 Driver 버전이 다르다.

 

 즉 어느정도 Driver 버전 이상이 되어야 CUDA가 지원된다. Linux와 Windows가 조금 다른 것에 주의하자.

아래가 대략의 정보이고 세부 정보는 이 링크를 확인해보자. https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html#id6

https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html#id6

 

 그리고 유의할 것은 해당 Driver 버전 중 일부는 특정 GPU 카드를 지원하지 않을 수 있다. 따라서 내가 가진 GPU 카드가 지원되는 드라이버 버전을 선택하자. 각 지원 버전은 NVidia 사이트에서 확인이 가능하다. 신기하게도 GPU마다 지원되는 버전이 들쑥날쑥하니 꼭 내 GPU카드가 지원하는 버전을 확인해두어야 한다. 혹시 Linux Package들이 이 드라이버 버전을 띄엄띄엄 지원할 수도 있으니, 아예 Linux에서 지원되는 버전별로 찾아봐서 내 GPU를 지원하는지 확인할 수도 있다.

https://www.nvidia.co.kr/Download/index.aspx?lang=kr

 

대략 525나 520, 515버전 등이 많이 설치하긴 한다. 여기서는 CUDA 11.8을 추천하려고 하므로 520이상을 설치하려고 노력해보자.

 

참고로 아래 명령을 통해 linux에 설치된 GPU정보나, 추천 드라이버를 제공받을 수 있다

 

$ lshw -C display           #RTX4080예시이다

  *-display                 
       description: VGA compatible controller
       product: NVIDIA Corporation
       vendor: NVIDIA Corporation
       physical id: 0
       bus info: pci@0000:01:00.0
       version: a1
       width: 64 bits
       clock: 33MHz

$ sudo ubuntu-drivers devices          #RTX4080예시이다

== /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0 ==
modalias : pci:v000010DEd00002704sv00001462sd00005111bc03sc00i00
vendor   : NVIDIA Corporation
driver   : nvidia-driver-545-open - third-party non-free
driver   : nvidia-driver-535 - third-party non-free
driver   : nvidia-driver-535-server - distro non-free
driver   : nvidia-driver-525-server - distro non-free
driver   : nvidia-driver-550-open - third-party non-free
driver   : nvidia-driver-550 - third-party non-free recommended
driver   : nvidia-driver-525 - third-party non-free
driver   : nvidia-driver-535-server-open - distro non-free
driver   : nvidia-driver-525-open - distro non-free
driver   : nvidia-driver-545 - third-party non-free
driver   : nvidia-driver-535-open - distro non-free

 

3. Ubuntu 버전과는 어떻게 되는가? 23.10보다는 22.04가 더 유리하다.

 

 판단기준 : Ubuntu버전이 아니라 설치된 gcc 버전이 중요하다. CUDA 11을 컴파일하기 위해 gcc 10이 필요하다.

 

 Ubuntu 23.10은 GCC 13버전 컴파일러를 디폴트로 사용하는 반면, CUDA 11/cudnn 등 컴파일에 GCC 10가 필요다. 그래서 사실은 Ubuntu 23.10을 사용해도 GCC 10버전 컴파일러를 디폴트로 정하면 사실은 쓸 수 있다. 그런데 GCC 버전을 왔다갔다하면서 설치할 배짱이 없다면 그냥 Ubuntu 22.04을 설치하자. 

 

혹시 Ubuntu 23.10을 써야한다면 아래 가이드를 참고하자. gcc 버전을 바꾸어 설치하는 방법과 원복하는 방법이 나와있다.

https://www.yodiw.com/install-cuda-11-on-ubuntu-23-10/

 

그래서 이러한 번거로움 때문에 Ubuntu 22.04을 설치해서 운영하도록 추천하게 된다.

 

4. cudnn은 어떻게 할까?

 판단기준 : CUDA 처럼 사용하는 프레임웍별로 필요한 버전을 설치해줘야한다. 대략 8.7 버전 정도가 현재는 제일 무난하다.

 

 cudnn의 8.7.0버전은 CUDA 11과 12모두 각각에 개별 버전별로 따로 있다(즉 8.7 버전이 CUDA 11, 12에 각각 배포판이 있다). 다행히 이 cudnn은 설치 후에도 쉽게 교체 및 추가 할 수 있다. 여러개를 설치하고 전환도 가능하다. 기존의 설치된 cuda의 include/lib 파일만 교체해주면 그만이기 때문이다.

 

이를 테면 CUDA가 /usr/local/cuda-11.8로 설치되어 있고 /usr/local/cuda -> /usr/local-11.8로 링크가 걸려있다고 치자.

그러면 cudnn은 적절히 다운로드 받은 패키지 파일의 압축을 풀고 덮어 씌우면 된다.

 

아래를 통해 다운로드 (로그인이 필요하다) 하고 그 이하의 절차로 include/lib파일을 복사해버리자.

https://developer.nvidia.com/rdp/cudnn-archive

 

$ tar -xzvf ${CUDNN_TAR_FILE}
$ sudo cp -P cuda/include/cudnn.h /usr/local/cuda-11.8/include
$ sudo cp -P cuda/lib/libcudnn* /usr/local/cuda-11.8/lib64/
$ sudo chmod a+r /usr/local/cuda-11.8/lib64/libcudnn*

 

간단하게 버전을 어떻게 잡으면 될지 잡아보았다. 상기 모두 정상 설치되면 아래 명령을 통해 설치 정보를 알 수 있다.

 

$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Wed_Sep_21_10:33:58_PDT_2022
Cuda compilation tools, release 11.8, V11.8.89
Build cuda_11.8.r11.8/compiler.31833905_0

$ nvidia-smi

Sat Apr 27 16:54:09 2024       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 525.125.06   Driver Version: 525.125.06   CUDA Version: 12.0     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ...  On   | 00000000:01:00.0  On |                  N/A |
|  0%   39C    P8    25W / 320W |    592MiB / 16376MiB |      0%      Default |

 

흥미로운 것은 위 nvidia-smi의 CUDA은 추천 cuda 버전이다.

 

실제 설치 cuda 버전은 nvcc 명령으로 확인하자.

 

5. python 버전은 어떻게 할까?

 

이것이야말로 실행할 프로젝트에 따라 다른데 대략 3.9~3.11정도가 추천되고 있다. 매우 세분화된 python버전을 요구할 수 있다(3.10.8이상 등). 따라서 설치할 프로젝트를 유심히 보는 수 밖에 없다. 하지만 우리는 여러개의 프로젝트를 실행하게 되기 때문에, 이 문제를 해결하기 위해 Anaconda(conda)를 사용하게 된다. 각 환경별로 서로다른 python과 package를 설정해 다중으로 사용하게 할 수 있는 합리적인 방법이다. Anaconda설치는 간단히 웹서핑해서 최근의 버전을 설치하면 된다.

 

Anaconda를 설치한 후 아래와 같은 형태로 이 python 버전에서 자유로워지자. 꼭 미리 conda create 명령으로 전용 환경을 구성한 후 conda activate를 통해 해당 환경을 활성화하고 실행하는 것이다. 디스크 용량을 필요로 하긴 하지만, 이보다 더 편안한 방법도 드물다. 적극 활용하도록 하자. (설치 경험이 없으신 분은 https://jongsky.tistory.com/21 를 참조하자) 아래는 대표적인 환경 구성 및 활성화, 설치 명령이다.

 

$ conda create -n llama2 python=3.9 -y

$ conda activate llama2

(llama2) $ pip install ...

$ ..

$ exit

반응형
Posted by 작동미학
머신러닝AI2024. 4. 20. 11:51

ollama는 많은 오픈소스 모델을 쉽게 구동해주는 프로젝트이다.

 

$ curl https://ollama.ai/install.sh 

$ chmod 755 install.sh

>> Downloading ollama...
######################################################################################################################################################################## 100.0%######################################################################################################################################################################## 100.0%######################################################################################################################################################################## 100.0%
>>> Installing ollama to /usr/local/bin...
[sudo] userid 암호: 
>>> Adding ollama user to render group...
>>> Adding current user to ollama group...
>>> Creating ollama systemd service...
>>> Enabling and starting ollama service...
>>> NVIDIA GPU installed.

$ ollama serve  #설치 후 자동 실행되지만 나중에는 이렇게 실행하면 된다.
Error: listen tcp 127.0.0.1:11434: bind: address already in use
$ ollama run llama3
pulling manifest 
pulling 00e1317cbf74... 100% ▕███████████████████████████████████████████████████████████████████████████████████████████████████████████████▏ 4.7 GB                         
pulling 4fa551d4f938... 100% ▕███████████████████████████████████████████████████████████████████████████████████████████████████████████████▏  12 KB                         
pulling 8ab4849b038c... 100% ▕███████████████████████████████████████████████████████████████████████████████████████████████████████████████▏  254 B                         
pulling c0aac7c7f00d... 100% ▕███████████████████████████████████████████████████████████████████████████████████████████████████████████████▏  128 B                         
pulling db46ef36ef0b... 100% ▕███████████████████████████████████████████████████████████████████████████████████████████████████████████████▏  483 B                         
verifying sha256 digest 
writing manifest 
removing any unused layers 
success 
>>> IT팀에 첫 부임하는 팀장인데 한글로 멋진 연설문을 써줘
Here's a potential team leader speech for your IT team in Korean:

---

**환영, 팀!**

저는 이날 처음으로 IT팀의 팀장이 되어 честь을 받은 것입니다. 함께 일하게 될 각자에게 고마워하는 기회를 가질 수 있습니다. 저는 이렇게 팀을 이끌어갈 수 있는 영광을 느끼고 있습니다.

** 우리의 사명 :*
우리 팀의 목표는 단순합니다. 이를 달성하기 위해 저는 각자의 강점과 재능을 활용하여 최고의 성과를 내겠습니다. 우리는 개별적으로는 아니라, 함께 일하는 조직체입니다. 기업의 성공을 위한 혁신적인 솔루션을 제공하며, 협동, 창의, 지속적 학습의 문화를 형성하겠습니다.

** 우리의 강점 :*
각자가 가진 Unique Skill, Perspective, Expertise 등을 활용하여 더 큰 것을 만들 수 있습니다. 우리는 소프트웨어 개발부터 지원까지 다양한 업무에 종사할 것입니다. 저는 각자의 재능을 믿고 있습니다.

** 앞으로의 도전 :*
저는 도전이 있을 줄 알아요. 하지만 저는 각자에게 믿음을 갖고 있습니다. 우리는 함께 도전을 맞아내고, collective Wisdom, Creativity, Determination 등을 사용하여 어떤도전이라도 이겨낼 것입니다. 우리는 실수를 통해 성장하고, 더 강한 팀이 될 것입니다.

** 이를 기대할 수 있는 것은 :*
저는 팀 리더로서 다음과 같은 약속을 합니다.
1. **오픈 커뮤니케이션** : 각자의 아이디어, 우려, 제안 등을 들으겠습니다. 각자의 목소리가 들립니다.
2. **클리어 골스** : 우리는 실제적 목표를 설정하여, 누구나 무엇을 위해 일하는지 알 수 있습니다.
3. **서포트 & 리소스** : 저는 각자가 필요한 도구, 교육, 지원 등을 제공할 것입니다.
4. **리코gnition & 레워즈** : 우리는 우리의 성공을 축하하고, 개인적인 공헌을 인정하여 서로를 이끌어갈 것입니다.

** 미래 :*
저는 이제 새로운 챕터에 들어가게 될 것 같습니다. 저는 이 팀이 어떤 것을 성취할지 궁금해합니다. 함께 이룰 수 있는 것은 무엇인지 찾아보겠습니다!

---
I hope this speech is inspiring and motivating for your IT team! Remember to tailor it to your team's specific needs and goals. Good luck with your new role as team 
leader! 🚀
---------------------------------------------------------------------------------------------------------
아래 링크에 ollama로 구동할 수 있는 모델이 나온다. llama3는 8B 4bit quantized 모델로 4.7GB정도 용량이다.

참고로  $ ollama run llama:70b 형태로 70B모델도 구동도 가능하다. 아래 각 모델의 세부 정보를 통해 어떤 형태까지 구동할 수 있는지 태그명을 알 수 있다.

 

https://ollama.com/library/

 

추천하고 싶은 한글이 어느정도 되는 모델들은 아래와 같다

$ ollama run gemma:instruct (한글이 그래도 나온다!)

$ ollama run llama3:instruct (한글로 설명해달라고 하면 제법 한다)

$ ollama run command-r (cohere의 모델인데 20b이지만 한글이 꽤 된다)

$ ollama run codegemma:instruct

 

 

https://ollama.com/library/llama3, 각 세부 모델도 실행가능하다. ex> llama3:70b

 

 

 그리고 마지막으로 허깅페이스에 올라와있는 gguf 모델은 ollama create명령으로 설정하여 역시 사용할 수 있다. 해당 모델을 받고, 주소 등이 담긴 modelfile을 작성해서 custom 모델 사용이 가능한 것이다. 

 

https://huggingface.co/teddylee777/Llama-3-Open-Ko-8B-Instruct-preview-gguf/tree/main

에서 모델 파일을 다운로드 받고 ( Llama-3-Open-Ko-8B-Instruct-preview-Q6_K.gguf 를 예시로 보자)

 

$ ollama list
NAME            ID              SIZE    MODIFIED
gemma:instruct  a72c7f4d0a15    5.0 GB  3 hours ago
gemma:latest    a72c7f4d0a15    5.0 GB  About a minute ago

 

$ cat > modelfile
FROM ./Llama-3-Open-Ko-8B-Instruct-preview-Q6_K.gguf

TEMPLATE """[INST] {{ .System }} {{ .Prompt }} [/INST]"""

PARAMETER stop "[INST]"
PARAMETER stop "[/INST]"

SYSTEM "You are my AI assistant. you should answer in Korean"

 

$ ls

modelfile

Llama-3-Open-Ko-8B-Instruct-preview-Q6_K.gguf


$ ollama create llama-3-open-ko-8b-q6 -f modelfile
transferring model data
creating model layer
creating template layer
creating system layer
creating parameters layer
creating config layer
using already created layer sha256:62c16633326decbe7273021ad9178315347282763373fac0654773e35deeeb41
writing layer sha256:e6836092461ffbb2b06d001fce20697f62bfd759c284ee82b581ef53c55de36e
writing layer sha256:9aa5bc598c7961cbdfa0ec4dc7f94384f8dd2d6a314997b7900dc95ebf30c790
writing layer sha256:ed11eda7790d05b49395598a42b155812b17e263214292f7b87d15e14003d337
writing layer sha256:c0e945a015bccb863dcba3fb16fc7f9aed7fb244a4ea7401c91d72561881e759
writing manifest
success

$ ollama list
NAME                            ID              SIZE    MODIFIED
gemma:instruct                  a72c7f4d0a15    5.0 GB  3 hours ago
gemma:latest                    a72c7f4d0a15    5.0 GB  6 minutes ago
llama-3-open-ko-8b-q6:latest    e3420f154241    6.6 GB  50 seconds ago

 

$ ollama run  llama-3-open-ko-8b-q6

>>> 서울의 관광지를 소개해줘

 서울은 한국의 수도라 문화적, 역사적으로 중요한 도시예요. 추천해 주세요. 그리고 가 봐야 할 곳이 있어요. 1. 경복궁: 조선  왕조의 주요 궁궐, 꼭 봐야 하는 방문지 2. 남산 타워: 서울을 상징하는
이 타워는 숨막히는 전망을 제공해요 3. 북촌 한옥 마을: 한국 전통 건축을 보여주는 마을 4. 명동 쇼핑 지구: 현대 한국의 패션, 쇼핑을 경험하세요 5. N서울타워: 관람 데크에서 도시의 파노라마
뷰를 즐기세요. 가장 먼저 방문하고 싶은 곳은요?

반응형
Posted by 작동미학
머신러닝AI2024. 4. 6. 01:05

이 문서는 ubuntu 22.04 + NVidia RTX 4080 H/W 하에서 진행하였다.

 

Stable Diffusion을 웹인터페이스 학습시킬 수 있는 kohya_ss 프로젝트가 존재한다. 여러가지 보조 기구를 제공해주고, 이미지가 있으면 자동으로 설정을 하고 학습까지 시켜준다. 단 아래는 전체 학습은 아니고 LoRA 즉 소규모의 부분 튜닝만 해주는 방법이다.

(그정도만으로도 꽤 성능이 향상된다고 알려져있다.)

 

https://github.com/bmaltais/kohya_ss

 

0. 아래를 먼저 진행한다

 

우선 다음을 준비할 필요가 있으며, CUDA와 cudnn은 아래 링크를 참조하여 설치한다. https://infoengineer.tistory.com/96

 

  1. CUDA 11.8

  2. python 3.10.9이상~3.11미만

  3. cudnn 8.7.0

 

따라서 용량이 충분한 디렉토리에서 아래와 같이 입력한다.

 

$ conda create -n kohya_ss python=3.10

$ conda activate kohya_ss

$ git clone https://github.com/bmaltais/kohya_ss

$ cd kohya_ss

$ sudo apt update -y && sudo apt install -y python3-tk               #python3-tk를 필요로 하는데 일반적으로 미설치되어있다

$ ./setup.sh

Skipping git operations.
Ubuntu detected.
Python TK found...
Switching to virtual Python environment.
22:33:35-474863 INFO     Python version is 3.10.14 (main, Mar 21 2024, 16:24:04) [GCC 11.2.0]                                                                                                                   

..

22:36:35-016860 INFO     Configuring accelerate...                                                                                                     
22:36:35-017576 WARNING  Could not automatically configure accelerate. Please manually configure accelerate with the option in the menu or with:       
                         accelerate config. 

 

위와 같은 accelerate warning이 뜨면 직접 실행해준다.

 

$ cd venv/bin

$ ./accelerate config

------------------------------------------------------------------------------------------------------------------------------------------------------In which compute environment are you running?
This machine                                                                                                                                           
-------------------------------------------------------------------------------------------------------------------------------------------------------Which type of machine are you using?                                                                                                                   
No distributed training                  #GPU가 여러개일 경우만 분산선택                                                                                                          
Do you want to run your training on CPU only (even if a GPU / Apple Silicon / Ascend NPU device is available)? [yes/NO]:  #계속엔터입력                           
Do you wish to optimize your script with torch dynamo?[yes/NO]:                                                                                        
Do you want to use DeepSpeed? [yes/NO]:                                                                                                                
What GPU(s) (by id) should be used for training on this machine as a comma-seperated list? [all]:all                                                   
-------------------------------------------------------------------------------------------------------------------------------------------------------Do you wish to use FP16 or BF16 (mixed precision)?                                                                                                     
fp16                                                                                                                                                   
accelerate configuration saved at /home/xxxx/.cache/huggingface/accelerate/default_config.yaml                                                        

 

$ cd ../..

$ ./gui.sh

22:53:19-311775 INFO     Kohya_ss GUI version: v23.0.15                                                                                                
22:53:19-399403 INFO     Submodule initialized and updated.                                                                                            
22:53:19-402531 INFO     nVidia toolkit detected                                                                                                       
22:53:20-026297 INFO     Torch 2.1.2+cu118                                                                                                             
22:53:20-029252 ERROR    Could not load torch: cuDNN version incompatibility: PyTorch was compiled  against (8, 7, 0) but found runtime version (8, 5, 
                         0). PyTorch already comes bundled with cuDNN. One option to resolving this error is to ensure PyTorch can find the bundled    
                         cuDNN.Looks like your LD_LIBRARY_PATH contains incompatible version of cudnnPlease either remove it from the path or install  
                         cudnn (8, 7, 0)      

 

---------------------------------------------------------------------------------------------------------------------------

혹시 위와 같은 cudnn 라이브러리 오류가 나면 cudnn 8.7.0 for CUDA11.8을 NVIDIA에서 다운받아 설치해주자.

CUDA 폴더에 cudnn의 파일만 잘 복사해주면 된다.

 

$ tar xvfz cudnn-linux-x86_64-8.7.0.84_cuda11-archive.tar.xz       #nvidia 사이트에서 cudnn archive 다운로드가능

#아래와 같이 파일을 복사하여 설치한다.

$ sudo cp -P cudnn-linux-x86_64-8.7.0.84_cuda11-archive/include/cudnn.h /usr/local/cuda-11.8/include/
$ sudo cp -P cudnn-linux-x86_64-8.7.0.84_cuda11-archive/lib/libcudnn* /usr/local/cuda-11.8/lib64/
$ sudo chmod a+r /usr/local/cuda-11.8/lib64/libcudnn*

---------------------------------------------------------------------------------------------------------------------------

cudnn 버전 오류가 없다면 아래와 같이 gui.sh를 실행해서 웹서버를 띄우고, 웹에 접속한다.

 

$ ./gui.sh

0:35:53-556439 INFO     Kohya_ss GUI version: v23.0.15                                                                                                
00:35:53-578951 INFO     Submodule initialized and updated.                                                                                            
00:35:53-579987 INFO     nVidia toolkit detected                                                                                                       
00:35:54-373093 INFO     Torch 2.1.2+cu118                                                                                                             
00:35:54-377101 INFO     Torch backend: nVidia CUDA 11.8 cuDNN 8700                                                                                    
...                                                                                         
Running on local URL:  http://127.0.0.1:7860

실행 상태에서 브라우저를 띄워 http://127.0.0.1:7860 에 접속한다.

드디어 kohya_ss의 web UI가 뜬다.

kohya_ss의 GUI

 

이제 본격적인 학습을 위해 이미지를 준비하고 이 이미지의 script를 구할 차례다.

 

1. 학습에 추가시킬 이미지를 마련한다.

 

  대략 500*500 이상 해상도로 준비해두면, 자동으로 caption을 생성해서 학습에 사용할 수 있다. 특정 폴더에 모아두고, 시험목적이라면 10장 정도면 되겠다.

 

2. 자동으로 captioning(이미지별 설명을 담은 txt 파일 자동 생성)을 해보자.

 

 아래와 같이 Utilities->Captioning의 BLIP Captioning에서 image folders를 선택 한 후 맨 오른쪽 아래 Caption Images를 실행한다. 해당 폴더에 이미지와 같은 이름의 txt파일이 생겼음을 알 수 있다.

image captioning
생성된 이미지 captioning 파일 예시 (원래는 이미지만 있었는데 txt가 추가됨)

 

3. 이제 LoRA탭/Training중간의 Dataset Preparation으로 가보자. 오른쪽 화살표를 클릭해서 열자.

LoRA/Training 메뉴의 하단 Dataset Preparation을 설정하자

 

 Dataset Preparation설정 들 중에서 Instance prompt에는 튜닝할 파일의 이름을 적당히 넣고, Class prompt는 나중에 호출할때 쓸 적절한 명칭, 그리고 가장 중요한 학습할 이미지의 폴더를 입력해주고, 이 모든 학습의 중간 데이터 등을 저장할 폴더를 하나 추가로 지정해주자(Destination training directory).

 그리고 차례로 하단의 'Prepare training data'버튼과 'Copy info to Folder Tab'버튼을 각기 차례대로 누르면, 학습할 이미지가 해당 destination folder로 이동되어 준비되고, 이 설정들이 상단의 Traing정보로 복사된다(캡춰 화면에서는 보이지 않는 위쪽에 반영된다)

Instant Prompt/Class prompt, trainging images 폴더 등 정의하자

 

 

3. Parameters를 변경한다

 그 다음은 이제 Parameters 를 열어서(Dataset Preparation바로 위) 아래와 같이 Presets를 sd15 - EDG_LoraOptiSettings로 예시 선택해보자. Epoch을 필요하면 약간 늘려준다. 4080으로 1 epoch당 몇분이 소요됨을 가정하자. 학습 진행 중에 대략 10GB전후의 GPU메모리가 사용되었다.

Presets를 선택해준다.

 

 

4. 맨 상단의 설정을 최종으로 정하자.

 그리고 맨 상단의 Pretrained model name은 적절히 기본 모델을 선택하자. 여기서는 sd-v1-5.safetensors 를 복사해두고 적용했다. 이 모델은 Stable Diffusion 유명한 기본 모델이며. 다른 모델을 써도 당연히 된다.

 

Pretrained model name을 선택하자 / safetensors와 fp16도 선택해주었다.

 

이제 맨 하단의 Start training을 누르면 학습이 시작된다.

 

한 epoch씩 학습이 진행된다.

 

 상기 케이스의 경우는 destination folder(/work2/kohya_ss/dest/)의 model 폴더에 75mb정도 크기의 safetensors형태의 LoRA모델이 생겼음을 알 수 있다. 이 만들어진 LoRA는 이제 Stable Diffusion에 잘 알려진 방법대로 원래 모델(여기서는 sd-v1-5.safetensors)과 함께 넣어 사용하면 되겠다. civit.ai의 LoRA들이 모두 이런 방식의 학습에 의해서 제공된다.

 

 

반응형
Posted by 작동미학
머신러닝AI2024. 3. 1. 09:32

 

개인비서 서비스가 LLM덕분에 가시권에 들어왔다. Siri도 그렇고 많은 사실은 많은 회사들이 이 개인비서를 꿈꿔왔다. 바쁜 현대인들은 대신 해줬으면 하는게 많기 때문이다. 인공지능이라는 이름으로 그동안 많은 서비스들이 이를 지향했는데, 사실 잘 안되었다. if-then 으로는 처리에 한계가 있었기 때문이다. 곧 사람들이 외면하기가 일쑤였다.

ChatGPT 생성


 그런데 LLM은 이미 OpenAI를 통해 어느정도의 사용자 경험을 인식시키고 있다. 청소년 정도의 응대만 가능해도 맡기고 싶은 일이 얼마나 많은가.

 

 하루를 한번 돌이켜보자. 몇시에 일어나서 밤사이 특별한 일은 없었는지(특히 해외 등), 날씨는 어떻고 우산을 가져갈지 말아야할지, 출근길에 특이한 정체나 사건은 없는지, 출근해서는 업무를 수행하기 위한 수많은 것들도 그렇고(임원들의 비서가 처리하는 수많은 것), 식당을 알아보고 일정을 조절하고는 기본이다.

 

 내 생활에서는 자녀를 위한 여러가지 교육 정보도 아쉽다. 내가 사는 지역에 특이한 일은 없는지, 내가 관심있는 주제는 어떤 변화가 있는지, 내 자산에는 알아야할 특이 정보는 없을지, 고민에 대한 상담, 여러가지 장기적인 관점의 진단은 특정 품질 이상으로 이런 것을 모두 챙겨줄 수 있다면 몇만원의 돈이 무엇이 아깝겠는가. 따라서 이 분야의 대형 서비스가 나올 수 있다고 생각한다.

 

 GPTs가 이런 비서 시장을 노릴테지만 여러가지 각각의 주제별로 조금더 전문화하고 다듬는 일이 필요하다. 그러다보면 각 전문분야별로 실제 정보나 컨텐츠가 부족하게 된다. 이러다보면 세상의 검색이나 여러가지 서비스가 API형태로 LLM이 쓸 수 있도록 변형되는 일이 가속화될것이다. 그렇게 더 발전하면 사람들이 직접 그 웹사이트에 들어가는 것보다는 API화 되어 LLM이 쓰는 양이 더 많아지고, 이제 사람들이 직접 사이트에 들어가는 것은 구식으로 느껴질 날이 올지도 모르겠다.

 

 이런 기술 환경의 핵심에는 Langchain같은 LLM을 다양하게 agent형으로 활용하는 오픈소스와 기존의 각종 서비스를 크롤링하고 API화되어가는 세상이 오는것. 비서 역할을 할 수 있는 사람이 있는 회사 임원들은 업무 사이트에 잘 들어가지 않는다. 사이트를 배우는 일도 큰 부담이기 때문이다. 이런 일을 LLM이 대신 해주는 세상이 곧 펼쳐지겠다.

 

 이런 변화가 오고있다. 영화 Her의 비서가 곧 세상을 점령해나갈 태세다.

반응형
Posted by 작동미학
머신러닝AI2024. 2. 23. 01:32

Gemma 공개후 곧바로 파인튜닝 가능한 가이드가 공개되었다.

https://adithyask.medium.com/a-beginners-guide-to-fine-tuning-gemma-0444d46d821c

 

A Beginner’s Guide to Fine-Tuning Gemma

A Comprehensive Guide to Fine-Tuning Gemma

adithyask.medium.com

 

gemma-2b-it 모델의 quantized 버전 정도는 16GB GPU로도 파인튜닝 테스트를 해볼 수 있다.

 

상기 가이드대로 GPU환경에서 python을 설치한다.

$ conda create -n gemma python=3.11

$ conda activate gemma

$ pip install bitsandbytes==0.42.0
$ pip install peft==0.8.2
$ pip install trl==0.7.10
$ pip install accelerate==0.27.1
$ pip install datasets==2.17.0
$ pip install transformers==4.38.0

 

아래 소스를 통해 샘플 데이터 셋을 허깅페이스에서 다운받아 파인튜닝해서 결과를 얻어볼 수 있다.

이때 허깅 페이스에 미리 계정을 만든 후 WRITE 가능한 token으로 로그인을 미리 해두자. WRITE 권한으로 생성한 토큰으로는 모델을 huggingface에 업로드 하는 것도 가능하다(아래 소스의 맨 마지막 두줄)

 

$ huggingface-cli login
...
Token: 

$ huggingface-cli whoami
Justik (필자의 profile이다)

 

아래 코드를 실행한다. LORA를 통한 경량형 튜닝 정도는 할 수 있게 된다. gemma-7b-it의 경우는 16gb에서는 메모리가 부족하다. (본래 글에서는 A100을 추천하고 있다.) gemma-2b-it의 경우는 아래의 코드로 10~14gb정도의 메모리를 소모한다.

import json
import pandas as pd
import torch
from datasets import Dataset, load_dataset
from huggingface_hub import notebook_login
from peft import LoraConfig, PeftModel
from peft import LoraConfig, get_peft_model
lora_config = LoraConfig(
    r=64,
    lora_alpha=32,
    target_modules=['o_proj', 'q_proj', 'up_proj', 'v_proj', 'k_proj', 'down_proj', 'gate_proj'],
    lora_dropout=0.05,
    bias="none",
    task_type="CAUSAL_LM"
) 
from transformers import (
    AutoModelForCausalLM,
    AutoTokenizer,
    BitsAndBytesConfig,
    TrainingArguments,
    pipeline,
    logging,
)
from trl import SFTTrainer

notebook_login()

#model_id = "google/gemma-7b-it"
# model_id = "google/gemma-7b"
model_id = "google/gemma-2b-it"
#model_id = "google/gemma-2b"

bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_use_double_quant=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.bfloat16
)

#model = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=lora_config, device_map={"":0})
model = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=bnb_config, device_map={"":0})
#model = AutoModelForCausalLM.from_pretrained(model_id, device_map={"":0})
tokenizer = AutoTokenizer.from_pretrained(model_id, add_eos_token=True)
{
    "instruction": "Create a function to calculate the sum of a sequence of integers.",
    "input":"[1, 2, 3, 4, 5]",
    "output": "# Python code def sum_sequence(sequence): sum = 0 for num in sequence: sum += num return sum"
}

dataset = load_dataset("TokenBender/code_instructions_122k_alpaca_style", split="train")

def generate_prompt(data_point):
    """Gen. input text based on a prompt, task instruction, (context info.), and answer

    :param data_point: dict: Data point
    :return: dict: tokenzed prompt
    """
    prefix_text = 'Below is an instruction that describes a task. Write a response that appropriately completes the request.\\n\\n'
    # Samples with additional context into.
    if data_point['input']:
        text = f"""<start_of_turn>user {prefix_text} {data_point["instruction"]} here are the inputs {data_point["input"]} <end_of_turn>\\n<start_of_turn>model{data_point["output"]} <end_of_turn>"""
    # Without
    else:
        text = f"""<start_of_turn>user {prefix_text} {data_point["instruction"]} <end_of_turn>\\n<start_of_turn>model{data_point["output"]} <end_of_turn>"""
    return text

# add the "prompt" column in the dataset
text_column = [generate_prompt(data_point) for data_point in dataset]
dataset = dataset.add_column("prompt", text_column)
dataset = dataset.shuffle(seed=1234)  # Shuffle dataset here
dataset = dataset.map(lambda samples: tokenizer(samples["prompt"]), batched=True)

dataset = dataset.train_test_split(test_size=0.2)
train_data = dataset["train"]
test_data = dataset["test"]

model = get_peft_model(model, lora_config)
trainable, total = model.get_nb_trainable_parameters()
print(f"Trainable: {trainable} | total: {total} | Percentage: {trainable/total*100:.4f}%")

import transformers

from trl import SFTTrainer

tokenizer.pad_token = tokenizer.eos_token
torch.cuda.empty_cache()
trainer = SFTTrainer(
    model=model,
    train_dataset=train_data,
    eval_dataset=test_data,
    dataset_text_field="prompt",
    peft_config=lora_config,
    args=transformers.TrainingArguments(
        per_device_train_batch_size=1,
        gradient_accumulation_steps=4,
        warmup_steps=0.03,
        max_steps=100,
        learning_rate=2e-4,
        logging_steps=1,
        output_dir="outputs",
        optim="paged_adamw_8bit",
        save_strategy="epoch",
    ),
    data_collator=transformers.DataCollatorForLanguageModeling(tokenizer, mlm=False),
)

# Start the training process
trainer.train()

new_model = "gemma-2b-it-finetune" #Name of the model you will be pushing to huggingface model hub
# Save the fine-tuned model
trainer.model.save_pretrained(new_model)

# Merge the model with LoRA weights
base_model = AutoModelForCausalLM.from_pretrained(
    model_id,
    low_cpu_mem_usage=True,
    return_dict=True,
    torch_dtype=torch.float16,
    device_map={"": 0},
)
merged_model= PeftModel.from_pretrained(base_model, new_model)
merged_model= merged_model.merge_and_unload()

# Save the merged model
merged_model.save_pretrained("merged_model",safe_serialization=True)
tokenizer.save_pretrained("merged_model")
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = "right"


def get_completion(query: str, model, tokenizer) -> str:
  device = "cuda:0"
  prompt_template = """
  <start_of_turn>user
  Below is an instruction that describes a task. Write a response that appropriately completes the request.
  {query}
  <end_of_turn>\\n<start_of_turn>model
  
  """
  prompt = prompt_template.format(query=query)
  encodeds = tokenizer(prompt, return_tensors="pt", add_special_tokens=True)
  model_inputs = encodeds.to(device)
  generated_ids = model.generate(**model_inputs, max_new_tokens=1000, do_sample=True, pad_token_id=tokenizer.eos_token_id)
  # decoded = tokenizer.batch_decode(generated_ids)
  decoded = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
  return (decoded)

result = get_completion(query="code the fibonacci series in python using reccursion", model=merged_model, tokenizer=tokenizer)
print(result)


# Push the model and tokenizer to the Hugging Face Model Hub
# merged_model.push_to_hub(new_model, use_temp_dir=False)
# tokenizer.push_to_hub(new_model, use_temp_dir=False)

 

이렇게 hugging face hub에 올려진 모델은 아래와 같다.

https://huggingface.co/Justik/gemma-2b-it-finetune

 

Justik/gemma-2b-it-finetune · Hugging Face

Model Card for Model ID Finetune model of Google Gemma 2b it Model Details Model Description This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated. Developed by: Justin Kim Fin

huggingface.co

 

반응형
Posted by 작동미학
머신러닝AI2024. 1. 13. 16:49

여러가지 다양한 오픈소스 모델이 쏟아지고 다양한 구동방법이 나오는데, 대부분은 HuggingFace를 통해 배포되고 있다.

 

이에 최근에 좋은 성능을 보인다고 알려진 SOLAR 10.7B 한글 튜닝 모델인 megastudy SOLAR 10.7B 모델을 구동해보자.

SOLAR 10.7B는 24GB이상의 GPU메모리에서 가동이 가능한 점은 참고가 필요하다.

 

여기서는 다양한 LLM을 연결해서 실행할 수 있는 langchain 프로젝트의 도움을 받는다.

 

$ conda create -n solar10.7b python=3.11

$ conda activate solar10.7b

$ pip install langchain huggingface_hub transformers datasets

 

1. HuggingFace 가입 및 access token받기

 

우선 huggingface와 langchain으로 연계시키려면 access token이 필요한데, 가입 후 발급받으면 된다.

 

https://huggingface.co/settings/tokens

 

Hugging Face – The AI community building the future.

Forgot your password? SSO is available for companies

huggingface.co

 

여기서 받은 토큰을 아래와 같이 환경변수에 추가한다.

 

$ export HUGGINGFACEHUB_API_TOKEN="발급받은huggingface토큰"

여기서는 아래 모델을 사용해보자. 자동으로 모델을 다운로드 받아 실행하므로 langchain이 지원하는 대부분의 모델을 실행해 볼 수 있다.

 

https://huggingface.co/megastudy/M-SOLAR-10.7B-v1.3

 

2. LangChain을 통해 HuggingFace 모델을 실행해보자.

 

아래와 같이 실행한다.

 

$ cat > testlangchain.py

 

from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain_community.llms import HuggingFacePipeline

# HuggingFace의 model id를 삽입한다.
#model_id = 'mncai/mistral-7b-v5'
#model_id = 'maywell/Mistral-ko-7B-v0.1'
model_id = 'megastudy/M-SOLAR-10.7B-v1.3'


# HuggingFacePipeline object
llm = HuggingFacePipeline.from_model_id(
    model_id=model_id, 
    device=0,               # -1: CPU(default), GPU존재하는 경우는 0번이상(CUDA Device #번호)
    task="text-generation", # 텍스트 생성
    model_kwargs={"temperature": 0.1, 
                  "max_length": 128},
)

# template
template = """질문: {question}

답변: """

prompt = PromptTemplate.from_template(template)
llm_chain = LLMChain(prompt=prompt, llm=llm)

question = "서울에서 제일 유명한 산이 어디야?"
print(llm_chain.run(question=question))

 

아래와 같이 실행하면 모델을 local로 다운로드 받아 잘 구동하는 것을 알 수 있다.

 

$ python testlangchain.py

...
  warnings.warn(
tokenizer_config.json: 100%|███████████████████████████████████████████████████████████████████████| 1.46k/1.46k [00:00<00:00, 4.00MB/s]
tokenizer.model: 100%|███████████████████████████████████████████████████████████████████████████████| 493k/493k [00:00<00:00, 27.0MB/s]
special_tokens_map.json: 100%|█████████████████████████████████████████████████████████████████████████| 551/551 [00:00<00:00, 1.50MB/s]
config.json: 100%|█████████████████████████████████████████████████████████████████████████████████████| 720/720 [00:00<00:00, 1.81MB/s]
model.safetensors.index.json: 100%|█████████████████████████████████████████████████████████████████| 35.8k/35.8k [00:00<00:00, 182kB/s]
model-00001-of-00005.safetensors: 100%|██████████████████████████

..

서울에서 가장 유명한 산은 북한산입니다. 북한산은 서울 북쪽에 위치한 아름다운 산으로 많은 사람들이 등산하고 휴식을 취하는 곳입니다.

 

기타로 "IT개발팀에 새로 팀장이 되었는데, 팀원들을 북돋워줄만한 연설문을 써줘." 라고 질문하고 max_length를 2048로 하면,

"팀장 선임을 축하합니다! 저희 팀은 팀원들의 헌신과 노력으로 성공을 거두어 왔습니다. 저는 저희 팀의 새로운 팀장으로서 저희 팀원들의 헌신과 노력에 감사드리며, 저희 팀의 미래를 위해 함께 노력하겠습니다. 저희 팀은 ..." 라고 답변한다.

 

또한 llangchain은 openai API를 동일한 방식으로 연계해서도 사용할 수 있어서 다양하게 활용이 가능하다. GPU 메모리가 부족하다면 모델을 바꾸거나(주석의 #model_id = 'mncai/mistral-7b-v5' 참조), HuggingfacePipeline변수내 device=-1로 변경(CPU로 실행)하여 전환하여 실행해서 실험해볼 수 있다.

---------------------------------------------------------------------------------------------------

# HuggingFace의 model id를 삽입한다.
model_id = 'mncai/mistral-7b-v5'
#model_id = 'megastudy/M-SOLAR-10.7B-v1.3'


# HuggingFacePipeline object
llm = HuggingFacePipeline.from_model_id(
    model_id=model_id, 
    device=-1,               # -1: CPU(default), GPU존재하는 경우는 0번이상(CUDA Device #번호)
    task="text-generation", # 텍스트 생성
    model_kwargs={"temperature": 0.1, 
                  "max_length": 128},
)

---------------------------------------------------------------------------------------------------

반응형
Posted by 작동미학
머신러닝AI2023. 9. 8. 22:20

다양한 사람들의 노력으로 Apple Sillicon (M1/M2, gpu 사용) 에서도 아래와 같이 구동할 수 있다. 그리고 표기했듯이 apple sillicon의 GPU inference를 사용할 수 있기 때문에 속도도 나쁘지 않다. 다만 단지 구동만 가능한 것을 확인했고, 이를 통해 학습을 할 수 있는것 같지는 않다. 그래도 돌려볼 수 있다는 것이 어딘가!

 

https://gist.github.com/gengwg/26592c1979a0bca8b65e2f819e31ab5c

 

아래와 같이 우선 llama.cpp를 git clone하자.

 

$ git clone https://github.com/ggerganov/llama.cpp.git
$ cd llama.cpp

 

아래와 같은 옵션으로 gpu inference를 활성화하여 컴파일 할 수 있다.


$ make clean
$ LLAMA_METAL=1 make

 

이제 메모리가 크지 않은 맥북들을 위해 각 메모리 크기에 맞는 모델을 다운받아 구동해보자.

만약에 메모리 10gb이상 맥북이면, 13b모델을 최적화 축소한 아래 모델을 시도해보자. 물론 이 모델을 적재할 메모리가 충분하다면 아래 huggingface.co/TheBloke의 다른 더 큰 모델을 참조할 수 있다. 64gb모델 맥북이나 맥스튜디오 장비를 가지고 있다면 40gb짜리 quantized된 70b모델을 돌릴 수도 있다.

 

일단 13b모델은 아래와 걑이 한다.


$ export MODEL=llama-2-13b-chat.Q4_0.gguf 

#wget이 설치되어 있다면 아래와 같이, 혹은 아래 URL로 직접 링크를 다운로드

$ wget "https://huggingface.co/TheBloke/Llama-2-13B-chat-GGUF/resolve/main/llama-2-13b-chat.Q4_0.gguf 

 

메모리 8gb이하 맥북이라면 아래 모델을 받는다.

$ export MODEL=llama-2-7b-chat.Q4_0.gguf 

$ wget "https://huggingface.co/TheBloke/Llama-2-7B-chat-GGUF/resolve/main/llama-2-7b-chat.Q4_0.gguf 

 

그리고 나서 이제 아래와 같이 실행한다. 아래는 7b모델이다. 다운받은 모델에 따라 모델명을 바꿔주자.

 

$ ./main -m ./llama-2-7b-chat.Q4_0.gguf -t 8 -n 128 -ngl 1 --prompt "could you generate python code for generating prime numbers?"

 

.... 정말 코드를 생성해준다!

 

기타 아래를 통해 conda를 써서 llama.cpp의 python 필요시 따로 구동할 수 있다.

$ conda create --name=llama2 python=3.11

$ conda activate llama2

$ pip install -r requirements.txt

 

간단하게 맥북(m1/m2이지만 다른 macos환경에서도 가능하리라고 본다) 에서 llama.cpp를 이용해  llama를 구동해보았다.

반응형
Posted by 작동미학
머신러닝AI2023. 9. 2. 20:15

생성형 이미지의 대표주자인 Stable Diffusion을 설치해서 실행해보자. 많은 것들이 그간 발전했고 easy diffusion이라는 멋진 웹인터페이스까지 만들어진 오픈소스가 존재한다.

 

모든 기본 환경은 ubuntu nvidia 4080 + anaconda 를 기준으로 한다 (  https://infoengineer.tistory.com/96 )

아래 사이트를 참조하자.

 

https://easydiffusion.github.io/docs/installation/

 

$ wget https://github.com/cmdr2/stable-diffusion-ui/releases/latest/download/Easy-Diffusion-Linux.zip

$ unzip Easy-Diffusion-Linux.zip

$ cd easy-diffusion

$ ./start.sh

/work/anaconda3/bin/curl
/usr/bin/tar
/work/anaconda3/bin/bzip2
Downloading micromamba from https://micro.mamba.pm/api/micromamba/linux-64/latest to /work2/easy-diffusion/installer_files/mamba/micromamba
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:02 --:--:--     0
100  4569    0  4569    0     0   1176      0 --:--:--  0:00:03 --:--:-- 4461k
  0 5096k    0     0    0     0      0      0 --:--:--  0:00:04 --:--:--     0bin/micromamba
100 5096k  100 5096k    0     0   811k      0  0:00:06  0:00:06 --:--:-- 3183k
Micromamba version:
1.5.0
Empty environment created at prefix: /work/easy-diffusion/installer_files/env
Packages to install: conda python=3.8.5
conda-forge/linux-64                                30.0MB @  41.3MB/s  0.7s
conda-forge/noarch                                  12.2MB @   3.5MB/s  3.5s

....

 

 

자동으로 서버가 뜬다. 내부 리눅스 브라우저를 이용해 아래를 실행한다.

 

http://localhost:9000/

 

적절한 영문 문구로 이미지 생성됨을 확인한다.

512*512 이미지가 수초만에 생성됨을 알 수 있었다. CPU/GPU 모드 등 변경하여 실험할 수 있다. 4080 GPU 카드로는 1280*768 정도 이미지를 만들 수 있는데, 1920*1280은 메모리 부족으로 오류가 난다.

 

 

이렇게 디폴트로 사용하는 방법도 있지만 custom model을 다운로드 받아 사용하는 방법도 있다. civit.ai 같은 사이트에서 model 파일을 다운로드 받아서 ./easy_diffusion/models/stable-diffusion/에 넣은 후 아래와 같이 특화된 이미지를 생성할 수도 있다.

 

stable diffusion model 다운로드 사이트(civit.ai), Models선택

 

Download를 선택하면 크기는 좀 크지만, 커스텀 모델 파일을 다운로드할 수 있다

 

왼쪽 메뉴의 Image Settings의 Model을 바꾸어서 생성해보면, 특화 모델 기반의 이미지 생성도 가능하다.

 

 

easy-diffusion외에 Stable Diffusion의 대명사인 AUTOMATIC1111이라는 웹인터페이스도 존재한다. 이 프로그램의 특징은 최신 기능의 빠른 적용이다. 이 툴을 설치하는 방법에 대해서도 잠깐 살펴보자.

 

$ conda create -n automatic python=3.10

$ conda activate automatic

$ git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git

$ cd stable-diffusion-webui/

$ ./webui.sh

################################################################
Install script for stable-diffusion + Web UI
Tested on Debian 11 (Bullseye), Fedora 34+ and openSUSE Leap 15.4 or newer.
################################################################
....

#모델다운로드 등 거치게 된다

#

 

모두 실행된 후 http://127.0.0.1:7861/ 에 접속하면 아래와 같이 AUTOMATIC1111이 구동된다. 시중의 메뉴얼을 참조하여 활용해보자. img2img 같은 기능은 이미지를 넣어 다른 이미지를 변환하는 기능이다. 그외에 Extras같은 플러그인 등 다채로운 기능을 제공한다. 이를 테면 Extensions에 https://github.com/toriato/stable-diffusion-webui-wd14-tagger 를 설치하면, 사진에서 스크립트를 뽑아주는 반대의 기능 활용도 가능하다.

Automatic1111 화면

 

반응형
Posted by 작동미학