반응형
반응형

pin_memory란?

pin_memory는 PyTorch에서 제공하는 옵션으로써, GPU에서 데이터 로드할 때 빠르게 옮기기 위해서 사용하는 것을 말합니다.

 

 

pin_memory 사용하는 이유는?

pin_memor를 사용하는 이유는 GPU 같은 경우 DRAM에 할당된 데이터(변수)를 사용하지 못하기 때문에 GPU에서 사용하기 위해 pinned memory로 데이터롤 옮기고 다시 VRAM(GPU 메모리)로 옮기는 과정에서 속도가 많이 느려지는 것을 막기 위해 DRAM을 통하지 않고 바로 VRAM으로 데이터 할당해줘 데이터 로드 속도 개선하는 역활을 하기 때문입니다.

반응형

'머신러닝 & 딥러닝' 카테고리의 다른 글

ModuleNotFoundError: No module named ""  (0) 2023.04.25
'NoneType' object has no attribute 'shape'  (0) 2023.04.13
torchvision.transforms.ToTensor()  (0) 2023.04.10
Data annotation이란?  (0) 2023.04.10
end-to-end란?  (0) 2023.04.09
반응형

LLaMA 100배 빠르게 사용하는 방법!

반응형

 LLaMA load를 C++ I/O 에서 mmap() 사용하면 100배 빨라지고 메모리 사용량도 절반만 사용할 수 있습니다.

 

 

mmap() 을 사용하게 된 이유!

LLaMA가 나오고 나서 실제 자기 컴퓨터에 LLaMA 모델을 사용해보기에는 컴퓨터 성능 문제가 발생했습니다.

이를 해결하기 위한 방법 중 하나로 mmap()을 사용하여 weight을 load 하는 것입니다.

기존에 C++ I/O를 사용하는 것보다 mmap()을 사용하는 것이 100배 빠르게 load 할 수 있으며 메모리도 절반 가량만 사용하다고 합니다.

 

mmap()가 좋은 이유!

- mmap()는 페이지를 복사를 하지 않습니다.

 

mmap() 사용해보기

아래 Github를 통해 mmap()를 사용해보실 수 있습니다.

https://github.com/ggerganov/llama.cpp

 

GitHub - ggerganov/llama.cpp: Port of Facebook's LLaMA model in C/C++

Port of Facebook's LLaMA model in C/C++. Contribute to ggerganov/llama.cpp development by creating an account on GitHub.

github.com

 

 

더 자세한 내용은 아래 링크 참조해주세요~

https://justine.lol/mmap/?fbclid=IwAR02T8hR1ZEpljlsVVvbdHjIadQ5rO8sXusDP4gWz2GTqWSJi7dgs2EIExw&mibextid=Zxz2cZ 

 

Edge AI Just Got Faster

Using mmap() to load LLaMA faster in parallel with less memory.

justine.lol

 

반응형
반응형

torchvision.transforms.ToTensor()이란?

torchvision.transforms.ToTensor() class는 PIL 이미지 또는 numpy 배열을 pytorch tensor로 변화 시켜주는  클래스입니다.

반응형

 

즉, 이미지를 PIL 이미지 또는 numpy 배열의 구조를 "Channel x Height x Width"로 바꿔줍니다.

그리고 이미지 픽셀의 밝기를 [0~225]를 [0~1]로 수정합니다.

 

from torchvision.transforms import ToTensor
from PIL import Image
import numpy as np

test_img  = Image.open('test.jpg')
img_array  = np.array(test_img)

tf_toTensor = ToTensor()

# PIL
img_tensor = tf_toTensor(test_img)
print(img_tensor_PIL)
print(img_tensor_PIL.size()) # 3 x 428 x 428
print(img_tensor_PIL.min(), img_tensor_PIL.max()) # 0~1

# numpy
img_tensor_ndarray = tf_toTensor(img_array)
print(img_tensor_ndarray)
print(img_tensor_ndarray.size()) # 3 x 428 x 428
print(img_tensor_ndarray.min(), img_tensor_ndarray.max()) # 0~1
반응형

'머신러닝 & 딥러닝' 카테고리의 다른 글

'NoneType' object has no attribute 'shape'  (0) 2023.04.13
pin_memory란?  (0) 2023.04.11
Data annotation이란?  (0) 2023.04.10
end-to-end란?  (0) 2023.04.09
Fully Connected Layers  (0) 2023.04.07
반응형

Annotation이란?

Annotation이란 뜻은 "주석"이라는 뜻을 가지고 있습니다.

이 주석이라는 뜻이 딥러닝, 머신러닝, ai 데이터에서 사용될 떄는 라벨링(Labeling)으로 사용되어 데이터에 대한 설명을 해주는 것을 말합니다.

 

반응형

'머신러닝 & 딥러닝' 카테고리의 다른 글

'NoneType' object has no attribute 'shape'  (0) 2023.04.13
pin_memory란?  (0) 2023.04.11
torchvision.transforms.ToTensor()  (0) 2023.04.10
end-to-end란?  (0) 2023.04.09
Fully Connected Layers  (0) 2023.04.07
반응형

Bounding box 이란?

Bounding box는 이미지 안에서 박스 형태로 위치를 표시해주는 것을 말합니다.

Coordinates 이란?

Coordinates는 좌표라는 뜻을 가지고 있고, 딥러닝에서도 좌표라는 의미로 사용됩니다.

 

Bounding box Coordinates

Bounding box에서 사용되는 coordinate는 보통 center coordinatescorner coordinates입니다. 

기본적으로 center coordinates은 주로 IoU를 계산할 때 사용됩니다. corner coordinates는 anchor나 MSELoss를 계산할 때 사용합니다.

 

 

cx = (x1 + x2) / 2

cy = (y1 + y2) / 2

w = x2 -x1

h = y2 -y1

 

 

 

 

 

 

코드

def corner_to_center(corner_boxes):
    """
    input
    corner_boxes: corner coordinates boxes : [N, 4] (x1, y1, x2, y2)
    output
    center_boxes: center coordinates boxes : [N, 4] (cx, cy, w, h)
    """
    cxcy = (corner_boxes[..., :2] + corner_boxes[..., 2:4]) / 2
    wh = corner_boxes[..., 2:4] - corner_boxes[..., :2]
    centor_boxes = torch.cat([cxcy, wh], dim=-1)
    return centor_boxes
    
    
def center_to_corner(center_boxes):
    """
    input
    center_boxes : center coordinates boxes : [N, 4] (cx, cy, w, h)
    output
    corner_boxes : corner coordinates boxes : [N, 4] (x1, y1, x2, y2)
    """
    x1y1 = center_boxes[..., :2] - (center_boxes[..., 2:4])/2
    x2y2 = center_boxes[..., :2] + (center_boxes[..., 2:4])/2
    corner_boxes = torch.cat([x1y1, x2y2], dim=-1)
    return corner_boxes

 

 

 

 

 

 

반응형

'머신러닝 & 딥러닝 > 용어' 카테고리의 다른 글

[용어]mini batch  (0) 2023.05.14
반응형

end-to-end란?

딥러닝에서 end-to-end의 의미는 입력에서 출력까지 파이프라인 네트워크 없이 신경망으로 한 번에 처리하는 것을 의미합니다.

즉, 복잡한 파이프라인 없이 하나의 신경망으로 입력받은 것을 출력할 수 있다는 것입니다.

 

반응형

end-to-end  장점

- 신경망 모델로 입력 받은 값에 대한 출력 값을 찾아낼 수 있습니다.

- 직접 파이프라인 설계할 필요가 없어집니다.

end-to-end  단점

- 라벨링 된 데이터가 많이 필요합니다.

- 메모리가 부족할 경우 사용할 수 없습니다.

- 복잡한 문제를 해결하기에는 효율적이지 않습니다.

반응형

'머신러닝 & 딥러닝' 카테고리의 다른 글

'NoneType' object has no attribute 'shape'  (0) 2023.04.13
pin_memory란?  (0) 2023.04.11
torchvision.transforms.ToTensor()  (0) 2023.04.10
Data annotation이란?  (0) 2023.04.10
Fully Connected Layers  (0) 2023.04.07
반응형

module 'torch.backends' has no attribute 'mps' 해결 방법

* 현재 Apple M1 pro 사용하고 있습니다.

반응형

 

 상황

- import torch 이후 torch.device('mps:0' if torch.backends.mps.is_available() else 'cpu')로 mps가 잘 되었다가 하루 뒤 

module 'torch.backends' has no attribute 'mps'가 뜨면서 작동하지 않았습니다.

 

해결

저는 일단 Pytorch version 확인 후 update 할려고 했지만 실패해서 다른 방법을 찾다 해결했습니다.

 

1. 가상환경 또는 사용하는 환경에서

   $ pip install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu

   진행해서 설치 했습니다.

 

해결한 상태에서 확인 한 결과입니다.

import platform
print(platform.platform())

import torch
print(torch.__version__)
-> 2.1.0.dev20230407

print(torch.backends.mps.is_built())
-> True
반응형

'머신러닝 & 딥러닝 > 설정' 카테고리의 다른 글

Apple M1 GPU 사용하는 방법  (0) 2023.04.08
반응형

PyTorch로 GPU 설정

반응형
## PyTorch 설정
import torch

device = torch.device('mps:0' if torch.backends.mps.is_available() else 'cpu')

print (f"PyTorch version:{torch.__version__}") # 1.12.1 이상
print(f"MPS 장치를 지원하도록 build 되었는지 확인: {torch.backends.mps.is_built()}") # True
print(f"MPS 장치가 사용 가능한지 확인: {torch.backends.mps.is_available()}") # True
!python -c 'import platform;print(platform.platform())'

 

결과

 

Tensorflow로 GPU 설정

## tensorflow GPU 설정
import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))

반응형

+ Recent posts