[CUDA] Pytorch c++/cuda extenstion tutorial 1
CUDA Programming Tutorial 1
CUDA is a programming language for GPU, so it enables faster parallel computation, thus making pytorch faster.
CUDA does parallel computation and return the output to pytorch.
So the actual important part is on cuda, not c++.
c++ is only a bridge that connect pytorch and cuda.
๋ณธ tutorial 1์์๋ NeRF์ volume rendering์ ์ฌ์ด ๋ฒ์ ์ผ๋ก ๋ง๋ค์ด๋ณผ ๊ฒ์ ๋๋ค.
- trilinear interpolation์ผ๋ก 8 vertices๋ก๋ถํฐ feature f๋ฅผ ์ป๋ ๊ฒ์ ๊ตฌํํ ๊ฒ์ ๋๋ค.
- tutorial 1์์๋ cuda์์ด c++ bridge๋ง ์จ๋ณผ ๊ฒ์ ๋๋ค.
- vscode์์ conda๋ก python ๊ฐ์ํ๊ฒฝ์ ๋ง๋ญ๋๋ค.
-
interpolation.cpp ํ์ผ์ ๋ง๋ค๊ณ , c++ bridge๋ฅผ ์ฌ๊ธฐ์ ์์ฑํด์ pytorch์ cuda๋ฅผ ์ฐ๊ฒฐํ ๊ฒ์ ๋๋ค.
- code๊ฐ pytorch๋ก๋ถํฐ tensors๋ฅผ ๋ฐ์ผ๋ฏ๋ก, c++๊ฐ tensor๊ฐ ๋ญ์ง ์๊ฒ ํ๋๋ก ํ๊ธฐ ์ํด,
#include <torch/extension.h>
๋ฅผ ์์ฑํด์ค๋๋ค.-
์ด๋ ๋นจ๊ฐ์ค์ด ๋์ค๋๋ฐ,
ctrl+shift+p
๋ฅผ ๋๋ฅด๊ณ C/C++: Edit Configurations (UI)๋ฅผ ์ ํํด์ฃผ์ด, ๋นจ๊ฐ์ค์ ์์ฑ๋๋ค. -
C/C++: Edit Configuration (JSON)
์ ์ ํํ๊ณincludePath
์ python๊ณผ pytorch path๋ฅผ conda environment์ ๊ฒฝ๋ก๋ก ์ถ๊ฐํด์ค๋๋ค.
-
- ํ์ผ์ ์ ์ฅํ๋ฉด
#include <torch/extension.h>
์ ๋นจ๊ฐ ๋ฐ์ค์ด ์ฌ๋ผ์ก์ต๋๋ค.
tutorial 1์์๋ placeholder
๋ง ์์ฑํ ๊ฒ์
๋๋ค. ์๋ํ๋ฉด ์ค์ computation์ cpp ํ์ผ์ด ์๋๋ผ cuda ํ์ผ์์ ์คํ๋๊ธฐ ๋๋ฌธ์
๋๋ค.
- ๋ฐ๋ผ์ cpp ํ์ผ์์๋ function name, input and output๋ง ์์ฑํ๋ฉด ๋ฉ๋๋ค.
- trilinear interpolation์ผ๋ก function name๊ณผ input์ธ feats, point์ output์ธ return feats๋ฅผ Tensor๋ก ์๋์ ๊ฐ์ด ์์ฑํฉ๋๋ค.
- c++์ cuda execution์ callํ๊ธฐ ์ํ ํจ์๋ฅผ ์ ์ํฉ๋๋ค.
- c++์ python์ callํ๊ธฐ ์ํ interface๋ฅผ ์ ๊ณตํฉ๋๋ค. (์ด๊ฒ์ด cpp์์ ์ ์ํ ํจ์๋ฅผ python์์ callํ ์ ์๋ ์ด์ ์ ๋๋ค.)
๋ง์ฝ python์์ c++๋ฅผ callํด๋ดค๋ค๋ฉด, pybind๋ผ๋ library๋ฅผ ์์คํ ๋ฐ, ์ด library๋ python์ผ๋ก๋ถํฐ c++ code๋ฅผ ๋ถ๋ฌ์ค๋ ๋ฐฉ๋ฒ์ ์ ๊ณตํฉ๋๋ค.
์ด pybind๋ผ๋ library๋ pytorch์ ๊ด๋ จ๋ ํ์๋ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, opencv code๊ฐ ์ฌ์ฉ๋ ์๋ ์์ต๋๋ค.
์ฐ๋ฆฌ๊ฐ ํ ์ผ์ ์ด๋ฐ pybind interface๋ฅผ ์ฌ์ฉํด python์์ c++ ํจ์๋ฅผ callํ ์ ์๋๋ก ๋ฐฉ๋ฒ์ ์ ๊ณตํด์ฃผ๋ ๊ฒ์ ๋๋ค.
- ์ฒซ๋ฒ์งธ ์ธ์๋ python์ผ๋ก๋ถํฐ ๋ถ๋ ค์ง function name:
"trilinear_interpolation"
- ๋๋ฒ์งธ ์ธ์๋ ๋ถ๋ ค์ง c++ function์ธ
trilinear_interpolation
์ ๋๋ค. - ์๋์ฒ๋ผ PYBIND11_MODULE๋ก ์ฒซ๋ฒ์งธ ์ธ์, ๋๋ฒ์งธ ์ธ์๋ฅผ ๋ฃ์ด์ฃผ๋ฉด c++ bridge๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ๋ง๋ ๊ฒ์ ๋๋ค.
c++์์ ์์ฑํ ์ด ํจ์๋ฅผ python์์ ์ด๋ป๊ฒ ๋ถ๋ฌ์ค๋์ง ํด๋ด ์๋ค.
- c++์์ ์์ฑํ ํจ์๋ฅผ ๋ถ๋ฌ์ค๊ธฐ ์ํด์๋ ์์ฑํ c++ ์ฝ๋๋ฅผ ๋จผ์
build
ํด์ผํฉ๋๋ค. - c++๋ ๋ค๋ฅธ ๊ณณ์์ call ๋๊ธฐ ์ํด์๋ compiling๊ณผ building์ ์๊ตฌํฉ๋๋ค.
setup.py
๋ก c++ ์ฝ๋๋ฅผ compiling๊ณผ building์ ํ ์ ์์ต๋๋ค.
setup.py
์์ c++ ์ฝ๋๊ฐ ์ด๋ป๊ฒ bulit ๋ ๊ฒ์ธ์ง ์ ์ํฉ๋๋ค.
- ์๋ ๋ด์ฉ์ copy and pasteํด์ ์์ ํ๋ฉด ๋ฉ๋๋ค.
name
์ package์ ์ด๋ฆ์ ์ค์ ํ๋ ๊ฒ์ด๊ณ , ๋๋จธ์ง๋ ๋ถ๊ฐ์ ์ ๋๋ค.- ๊ฐ์ฅ ์ค์ํ ๋ถ๋ถ์
ext_modules
์ ๋๋ค.-
๋จผ์ buildํ๊ณ ์ถ์ c++ ์ฝ๋๋ฅผ sources์ ๋ฆฌ์คํธ ์์ ์จ์ค๋๋ค. (์ฌ๋ฌ๊ฐ ์์ผ๋ฉด, ์ฝค๋ง๋ก ์ด์ด์ ๋ค๋ฅธ cpp ํ์ผ๋ ์จ์ค๋๋ค.)
-
๋ง์ง๋ง์ผ๋ก
cmdclass
๋ ์ฐ๋ฆฌ๊ฐ code๋ฅผ buildingํ ๋ค๋ ๊ฒ์ ์๋ ค์ฃผ๋ ๋ถ๋ถ์ ๋๋ค.
-
๋ง์ง๋ง์ผ๋ก pip version์ python -m pip install pip -U
๋ก ์
๊ทธ๋ ์ด๋ํ๊ณ , setup.py
๋ก c++ ์ฝ๋๋ฅผ build ํด๋ด
์๋ค.
- path๋
setup.py
๊ฐ ์์นํ ๊ณณ์ ๋๋ค. ์ฐ๋ฆฌ๋ ํ์ฌ current folder์setup.py
๊ฐ ์์นํ๋ฏ๋ก ๊ทธ๋ฅ ํ์ฌ์์น๋ฅผ ๋ํ๋ด๋.
์ path๋ก ๋ฃ์ด์ฃผ๋ฉด ๋ฉ๋๋ค. pip install .
์ ์ ๋ ฅํ๋ฉด ์๊ฐ์ด ์ข ๊ฑธ๋ฆฌ๋ฉด์ build๊ฐ ์๋ฃ๋ฉ๋๋ค.
setup.py
๋ก c++ ํ์ผ์ด build๊ฐ ์๋ฃ๋๋ฉด python test.py ํ์ผ์์ ๋ถ๋ฌ์์ ํจ์๋ฅผ ์ฌ์ฉํด๋ด
์๋ค.
- cppcuda_tutorial์์ torch๋ฅผ importํ์ผ๋ฏ๋ก torch๋ฅผ import ์ํด๋ ๋ ๊ฑฐ๋ผ ์๊ฐํ ์ ์์ง๋ง, torch๋ถํฐ import๋ฅผ ํด์ค์ผ ํฉ๋๋ค.
- ๋ง์ฝ ๊ทธ๋ ์ง ์์ผ๋ฉด ์ฐ๋ฆฌ๊ฐ c++ ์ฝ๋๋ก ์ง custom package๊ฐ import๋์ง ์์ต๋๋ค.
- ์ง์ ์ง
cppcuda_tutorial
c++ ์ฝ๋์์trilinear_interpolation
ํจ์๋ฅผ ๋ถ๋ฌ์์ pythontest.py
์์ ์ฌ์ฉํ์ฌ ๊ฒฐ๊ณผ๊น์ง ์ฑ๊ณต์ ์ผ๋ก ๋์ถํ์์ต๋๋ค.
๊ฐ์ฌํฉ๋๋ค.
์ค์ ์์
diff-gaussian-rasterization/ext.cpp
์์ c++ bridge๋ ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ์์ต๋๋ค.
- ๊ธฐ๋ณธ์ ์ธ
#include <torch/extension.h>
๋ก torch๊ฐ ๋ฌด์์ธ์ง cpp ํ์ผ์๊ฒ ์๋ ค์ค๋๋ค. ext.cpp
ํ์ผ์setup.py
๋กpip install .
๋ก buildํ๋ฉด ์ด์ python ํ์ผ์์ c++, cuda๋ก ์์ฑํ ํจ์๋ฅผ importํ์ฌ ์ฌ์ฉ๊ฐ๋ฅํฉ๋๋ค.- ์ด๋, ์์ ๋งํ๋ฏ์ด
ext.cpp
ํ์ผ์ ์ค์ง pytorch์ cuda๋ฅผ ์ฐ๊ฒฐํ๋ bridge ์ญํ ๋ง ํฉ๋๋ค.- CUDA๋ก ์์ฑํ
RasterizeGaussianCUDA
๋ python ํ์ผ์์rasterize_gaussians
๋ก ํจ์๋ก ๋ถ๋ฌ ์ฌ์ฉํฉ๋๋ค. - CUDA๋ก ์์ฑํ
RasterizeGaussiansBackwardCUDA
๋ python ํ์ผ์์rasterize_gaussians_backward
๋ก ํจ์๋ก ๋ถ๋ฌ ์ฌ์ฉํฉ๋๋ค. - CUDA๋ก ์์ฑํ
markVisible
๋ python ํ์ผ์์mark_visible
๋ก ํจ์๋ก ๋ถ๋ฌ ์ฌ์ฉํฉ๋๋ค.
# ext.cpp /* * Copyright (C) 2023, Inria * GRAPHDECO research group, https://team.inria.fr/graphdeco * All rights reserved. * * This software is free for non-commercial, research and evaluation use * under the terms of the LICENSE.md file. * * For inquiries contact george.drettakis@inria.fr */ #include <torch/extension.h> #include "rasterize_points.h" PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { m.def("rasterize_gaussians", &RasterizeGaussiansCUDA); m.def("rasterize_gaussians_backward", &RasterizeGaussiansBackwardCUDA); m.def("mark_visible", &markVisible); }
-
rasterize_points.h
์์๋ c++ ์ฝ๋๋ก function name, input, output์ ๋ํ ์ ์๋ฅผ ํฉ๋๋ค.# rasterize_points.h` /* * Copyright (C) 2023, Inria * GRAPHDECO research group, https://team.inria.fr/graphdeco * All rights reserved. * * This software is free for non-commercial, research and evaluation use * under the terms of the LICENSE.md file. * * For inquiries contact george.drettakis@inria.fr */ #pragma once #include <torch/extension.h> #include <cstdio> #include <tuple> #include <string> std::tuple<int, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor> RasterizeGaussiansCUDA( const torch::Tensor& background, const torch::Tensor& means3D, const torch::Tensor& colors, const torch::Tensor& opacity, const torch::Tensor& scales, const torch::Tensor& rotations, const float scale_modifier, const torch::Tensor& cov3D_precomp, const torch::Tensor& viewmatrix, const torch::Tensor& projmatrix, const float tan_fovx, const float tan_fovy, const int image_height, const int image_width, const torch::Tensor& sh, const int degree, const torch::Tensor& campos, const bool prefiltered, const bool debug); std::tuple<torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor> RasterizeGaussiansBackwardCUDA( const torch::Tensor& background, const torch::Tensor& means3D, const torch::Tensor& radii, const torch::Tensor& colors, const torch::Tensor& scales, const torch::Tensor& rotations, const float scale_modifier, const torch::Tensor& cov3D_precomp, const torch::Tensor& viewmatrix, const torch::Tensor& projmatrix, const float tan_fovx, const float tan_fovy, const torch::Tensor& dL_dout_color, const torch::Tensor& sh, const int degree, const torch::Tensor& campos, const torch::Tensor& geomBuffer, const int R, const torch::Tensor& binningBuffer, const torch::Tensor& imageBuffer, const bool debug); torch::Tensor markVisible( torch::Tensor& means3D, torch::Tensor& viewmatrix, torch::Tensor& projmatrix);
- CUDA๋ก ์์ฑํ
diff-gaussian-rasterization/setup.py
์์ ์ค์ buildํ๋ ์ฝ๋๋ c++ bridge ์ฝ๋์ธ ext.cpp
์ด์ธ์๋ cuda ์ฝ๋๊ฐ ์ฌ๋ฌ๊ฐ ์กด์ฌํฉ๋๋ค.
setup.py
์์ ๊ฐ์ฅ ์ค์ํ ๋ถ๋ถ์ธext_modules
์์sources
list์ ๋ค์ด์๋cpp
,cu
ํ์ผ์ build ํฉ๋๋ค.- ์ด๋ก์จ
"cuda_rasterizer/rasterizer_impl.cu", "cuda_rasterizer/forward.cu", "cuda_rasterizer/backward.cu", "rasterize_points.cu", "ext.cpp"
์ ์ ์๋ ํจ์๋ฅผ python์์ ๋ถ๋ฌ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
#
# Copyright (C) 2023, Inria
# GRAPHDECO research group, https://team.inria.fr/graphdeco
# All rights reserved.
#
# This software is free for non-commercial, research and evaluation use
# under the terms of the LICENSE.md file.
#
# For inquiries contact george.drettakis@inria.fr
#
from setuptools import setup
from torch.utils.cpp_extension import CUDAExtension, BuildExtension
import os
os.path.dirname(os.path.abspath(__file__))
setup(
name="diff_gaussian_rasterization",
packages=['diff_gaussian_rasterization'],
ext_modules=[
CUDAExtension(
name="diff_gaussian_rasterization._C",
sources=[
"cuda_rasterizer/rasterizer_impl.cu",
"cuda_rasterizer/forward.cu",
"cuda_rasterizer/backward.cu",
"rasterize_points.cu",
"ext.cpp"],
extra_compile_args={"nvcc": ["-I" + os.path.join(os.path.dirname(os.path.abspath(__file__)), "third_party/glm/")]})
],
cmdclass={
'build_ext': BuildExtension
}
)
Leave a comment