Github Enterprise 계정 만들기
Github Organization(Enterprise)이란 여러명이 공통 그룹으로 프로젝트를 관리하는 공동 그룹 계정이라고 한다.팀 프로젝트를 진행할 떄 카테고리/그룹화 하기 좋고 google, facebook 등 다양한 회사에서 사용하고 있다.
Mac에서 사용하는 GPU 알아보자.
애플 실리콘 M1은 애플이 개발한 맞춤형 칩셋으로,에어, 프로, 미니 등 일부 애플 컴퓨터 제품군에 적용되었습니다. 이전에 인텔이나 AMD와 같은 회사 제품을 사용했지만 이제 자체적으로 설계하고 칩셋을 만들어 사용하고 있습니다. M1은 기존의 인텔 기반 맥과 비교할 때 효율적이며 좋은 성능을 보여주고 있습니다. 전력 소비가 적고 더 높은 CPU와 GPU 성능을 제공하고 있습니다.
현재 필자는 MacOS m1칩을 사용하고 있는 유저로 Mac에서 딥러닝 프레임워크를 GPU 프로세서로 연산하기 위해서는 다양한 방법을 사용할 수 있습니다. 애플 실리콘의 GPU는 TensorFlow, PyTorch 및 Core ML과 같은 머신 러닝 프레임워크를 지원하고 있습니다. 이를 사용하면 훨씬 빠르게 모델 훈련을 할 수 있고 대규모 이미지, 비디오, 음성 데이터 집합을 처리하는 과정에서 이점이 두드러지게 나타납니다.
엔비디아에서 macOS에 대한 cuda지원을 하지 않아 다른 방법으로 활용해보고자 한다.
먼저 Xcode 도구를 설치합니다. 앱스토어에서 무료로 다운 가능하며 개발에 필요한 도구와 라이브러리를 포함하고 있어 컴파일러, 디버거, 테스트 프레임 워크 등 다양한 개발 도구를 사용하여 빌드 작업을 수행할 수 있습니다.
$ xcode-select --install
Miniconda는 python 환경을 구성하는데 사용되며 Anaconda 보다 경량화 되어있습니다. 다양한 패키지와 라이브러리를 설치하며 관리할 수 있고 딥러닝을 위한 필수 패키지를 포함하고 있습니다. 가볍고 최소한의 패키지만 설치해 경량화하여 용량을 절약할 수 있습니다.
1 $ wget https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh
2 $ bash Miniconda3-latest-MacOSX-arm64.sh
Pytorch m1환경에서 구동하기 위해 python 3.9 버전을 지정, Torch-m1-gpu 라는 env를 생성.
1 $ conda create -n torch-m1-gpu python=3.9
2 $ conda activate torch-m1-gpu
PyTorch는 딥러닝 오픈소스 머신러닝 라이브러리 입니다. PyTorch는 높은 수준의 추상화와 텐서 연산, 그리고 미분 가능한 함수를 제공하고 있습니다. PyTorch 공식 홈페이지에서 확인할 수 있습니다.
터미널을 열고 PyTorch 및 관련 라이브러리 설치
$ conda install pytorch torchvision torchaudio -c pytorch
PyTorch에서 MPS(Multi-Process Service)가 활성화되어 있는지 확인
1 (torch-m1-gpu) ➜ ~ python3
2 Python 3.9.16 (main, Mar 8 2023, 04:29:24)
3 [Clang 14.0.6 ] :: Anaconda, Inc. on darwin
4 Type "help", "copyright", "credits" or "license" for more information.
5 >>> import torch
6 >>> import math
7 >>> print(torch.backends.mps.is_available())
8 True
9 >>> print(torch.backends.mps.is_built())
10 True
11 >>> exit()
만약 두 값이 모두 True라면, MPS를 사용할 수 있는 PyTorch환경임이 확인된 것입니다.
실제 PyTorch상에서 해당 mps를 사용하도록 직접 설정하려면 아래와 같이 입력합니다.
import torch는 PyTorch 라이브러리를 가져오는 구문입니다.
device = torch.device(“mps”)는 MPS를 사용하기 위한 디바이스를 설정하는 구문입니다.
MPS 디바이스는 torch.device 함수를 사용하여 지정할 수 있으며, mps를 입력하여 MPS 디바이스를 설정합니다.
이 코드를 실행하면, MPS가 지원되는 환경에서 PyTorch에서 MPS를 사용하기 위한 기본 설정이 완료됩니다.
이후 해당 환경에서 MPS를 이용한 모델 학습 등의 작업을 할 때, 이 설정이 활용됩니다.
1 import torch
2 device = torch.device("mps")
PyTorch를 사용하여 사인 함수의 값을 예측하는 모델
import torch
import math
dtype = torch.float
device = torch.device("mps")
# Create random input and output data
x = torch.linspace(-math.pi, math.pi, 2000, device=device, dtype=dtype)
y = torch.sin(x)
# Randomly initialize weights
a = torch.randn((), device=device, dtype=dtype)
b = torch.randn((), device=device, dtype=dtype)
c = torch.randn((), device=device, dtype=dtype)
d = torch.randn((), device=device, dtype=dtype)
learning_rate = 1e-6
for t in range(2000):
# Forward pass: compute predicted y
y_pred = a + b * x + c * x ** 2 + d * x ** 3
# Compute and print loss
loss = (y_pred - y).pow(2).sum().item()
if t % 100 == 99:
print(t, loss)
# Backprop to compute gradients of a, b, c, d with respect to loss
grad_y_pred = 2.0 * (y_pred - y)
grad_a = grad_y_pred.sum()
grad_b = (grad_y_pred * x).sum()
grad_c = (grad_y_pred * x ** 2).sum()
grad_d = (grad_y_pred * x ** 3).sum()
# Update weights using gradient descent
a -= learning_rate * grad_a
b -= learning_rate * grad_b
c -= learning_rate * grad_c
d -= learning_rate * grad_d
print(f'Result: y = {a.item()} + {b.item()} x + {c.item()} x^2 + {d.item()} x^3')
Tensorflow 사용시
Python 3.9
tensorflow-macos==2.9
python -m pip install tensorflow-macos==2.9
tensorflow-metal==0.7.0
python -m pip install tensorflow-metal==0.7.0
import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
import tensorflow as tf
print(tf.config.list_physical_devices('GPU'))
import tensorflow as tf
print("TensorFlow 버전:", tf.__version__)
print("사용 가능한 GPU 목록:", tf.config.list_physical_devices('GPU'))
import tensorflow as tf
GPU가 사용되고 있는지 확인
physical_devices = tf.config.list_physical_devices('GPU')
print("Num GPUs Available: ", len(physical_devices))
TensorFlow가 GPU를 사용하는지 확인
print("TensorFlow GPU 사용 여부: ", bool(physical_devices))
현재 선택된 GPU 장치 확인
print("현재 선택된 GPU 장치: ", physical_devices[0].name if physical_devices else "None")
import tensorflow as tf
from tensorflow import device_lib
device_lib.list_local_devices()
tf.config.list_physical_devices('GPU')
import torch
print(torch.__version__)
print(torch.backends.mps.is_built())
import torch
print(torch.backends.mps.is_available())
import torch
mps_device = torch.device("mps")
MPS 장치에 바로 tensor를 생성합니다.
x = torch.ones(5, device=mps_device)
또는
x = torch.ones(5, device="mps")
GPU 상에서 연산을 진행합니다.
y = x * 2
또는, 다른 장치와 마찬가지로 MPS로 이동할 수도 있습니다.
class YourFavoriteNet:
pass
model = YourFavoriteNet() # 어떤 모델의 객체를 생성한 뒤,
model.to(mps_device) # MPS 장치로 이동합니다.
이제 모델과 텐서를 호출하면 GPU에서 연산이 이뤄집니다.
pred = model(x)
import torch
mps_device = torch.device("mps")
#참고
https://cpuu.postype.com/post/14278479
https://jobbu.life/ko/posts/apple_silicon_gpu_tensorflow/
Leave a comment