0

I'm using 3D Gaussian Splatting, which consists of a Cuda/C++ based python extension. I want to use CMake to compile it in setup.py. My basic idea is to generate .so file with CMake, and use this file to build python extension, which is similar to the idea in this question However, it seems that my setup.py could not use this .so file, and failed to build my python extension. This is my setup.py and CMakeList. Can you help me?

from setuptools import setup,Extension
from setuptools.command.build_ext import build_ext
from torch.utils.cpp_extension import CUDAExtension, BuildExtension
import subprocess
import os
import shutil

current_dir=os.path.dirname(os.path.abspath(__file__))

class CMakeBuildExt(BuildExtension):
    def run(self):
        build_dir = os.path.join(current_dir, 'build')

        subprocess.check_call(['cmake', '..'], cwd=build_dir)
        subprocess.check_call(['make', 'clean'], cwd=build_dir)
        subprocess.check_call(['make'], cwd=build_dir)
        shutil.copy(os.path.join(build_dir,  'libCudaRasterizer.so'), 
                    os.path.join(current_dir, 'diff_surfel_rasterization', 'libCudaRasterizer.so'))
        super().run()




setup(
    name="diff_surfel_rasterization",
    packages=['diff_surfel_rasterization'],
    version='0.0.1',
    cmdclass={
        'build_ext': CMakeBuildExt
    },
    ext_modules=[
        CUDAExtension(
            name="diff_surfel_rasterization._C",
            sources=[],
            extra_compile_args={"nvcc": ["-I" + os.path.join(os.path.dirname(os.path.abspath(__file__)), "third_party/glm/")]})
        ],
    
)
#
# 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  [email protected]
#

cmake_minimum_required(VERSION 3.20)

project(DiffRast LANGUAGES CUDA CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CUDA_STANDARD 17)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")

add_library(CudaRasterizer
    cuda_rasterizer/backward.h
    cuda_rasterizer/backward.cu
    cuda_rasterizer/forward.h
    cuda_rasterizer/forward.cu
    cuda_rasterizer/auxiliary.h
    cuda_rasterizer/rasterizer_impl.cu
    cuda_rasterizer/rasterizer_impl.h
    cuda_rasterizer/rasterizer.h
)

set_target_properties(CudaRasterizer PROPERTIES CUDA_ARCHITECTURES "70;75;86")

target_include_directories(CudaRasterizer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/cuda_rasterizer)
target_include_directories(CudaRasterizer PRIVATE third_party/glm ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES})

I tried to generate .so file with CMake, and I expected setup.py would use this file to build python extension.However, it ended up building a python extension with a sole init file.

3
  • When you write “Python extension” what you really mean is “PyTorch extension”, right? Otherwise why are you using PyTorch’s extension builder? Commented Jul 12, 2024 at 3:41
  • The result is the same when I use extension builder from setuptool. I think the key is that the extension builder didn't try to link my library(.so) Commented Jul 12, 2024 at 7:35
  • That may well be. But the Extension builder for Pytorch is specifically designed to build extensions with all hooks necessary to add custom operators within Pytorch. It isn't (I believe) intended to produce general extensions which can be used outside of Pytorch. To make your problem simpler, remove all the Pytorch stuff and just use the APIs which setuptools exposes. That will make it easier to troubleshoot Commented Jul 12, 2024 at 8:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.