PyTorch API

nalimeitb·2025년 9월 7일

PyTorch API

PyTorch API 차원에서 PyTorch에 대한 이해

PyTorch 무엇인가?

PyTorch is a fully featured framework for building deep learning models, which is a type of machine learning that’s commonly used in applications like image recognition and language processing. Written in Python, it’s relatively easy for most machine learning developers to learn and use. PyTorch is distinctive for its excellent support for GPUs and its use of reverse-mode auto-differentiation, which enables computation graphs to be modified on the fly. This makes it a popular choice for fast experimentation and prototyping.

https://www.nvidia.com/en-us/glossary/pytorch/

PyTorch is a Python package that provides two high-level features:
1) Tensor computation (like NumPy) with strong GPU acceleration
2) Deep neural networks built on a tape-based autograd system

https://github.com/pytorch/pytorch

정리해보면, PyTorch는 PyThon 기반의 오픈소스 딥러닝 프레임워크로, 직관적인 코드 구조를 가지고 있으며 자동 미분(autograd), multiprocessing, 동적 계산 그래프(dynamic computational graph) 등의 다양한 기능을 PyTorch API로 제공하여 딥러닝 개발자들에게 간편하게, 빠르고 유연하게, 다양한 신경망 모델 설계와 실험을 지원하는 도구라고 할 수 있다.

그렇다면, PyTorch는 어떻게 사용자들에게 PyTorch API를 제공할 수 있는가?

PyTorch Architecture은 다음과 같다.

https://se.ewi.tudelft.nl/desosa2019/chapters/pytorch/#fnref:3

PyTorch Architecture는 API level(Top), Engine level(Mid), Library level(Low)로 구조화 될 수 있다.

사용자들이 Deep Learning model의 다양한 기능을 개발할 때 사용하는 API level은 torch를 기본으로 하여, 자동미분을 담당하는 torch.autograd, 신경망 계층과 구조를 설계할 때 사용하는 torch.nn, 다양한 최적화 알고리즘을 제공하는 torch.optim 등 많은 API가 사용자들에게 공개되어 있으며, 사용자들이 직접 이런 API level을 통해 코드를 접할 수 있다.

API level은 두번째 Engine level과 Python API를 통해 연결되어 있다. Engine Level은 Top에서 제공하는 기능들을 실제로 계산할 수 있도록 처리하는 부분으로 Python과 C++ 코드를 연결해주며, 연산 그래프를 만들고 역전파의 자동미분을 계산하는 Autograd, C++ 기반의 Tensor 연산, 계산 최적화를 담당하는 ATen, model을 compile하여 실행속도를 높여주는 기능을 가진 JIT 등이 있다.

세번째, Library level은 Mid에서 전달받은 실제 Tensor 계산들을 C 또는 CUDA 등 실제 하드웨어와 연동하여 아주 빠르고 효율적으로 연산을 수행한다. TH와 THC는 각각 CPU와 GPU에서 Tensor 연산을 담당하는 C와 CUDA 라이브러리, THNN, THCUNN은 각각 신경망 함수와 커널 구현을 담당하는 C와 CUDA 라이브러리에 해당한다.

0개의 댓글