conan-io / wishlist

This repo is to propose libraries, frameworks, and code in general that users would like to have in conan
MIT License
49 stars 5 forks source link

LibTorch #187

Closed kindlychung closed 2 years ago

kindlychung commented 5 years ago

C++ API for Pytorch.

Code is included in the pytorch repo: https://github.com/pytorch/pytorch

How to build: https://michhar.github.io/how-i-built-pytorch-gpu/

Useful for deep learning projects.

uilianries commented 5 years ago

I would suggest prioritizing this one.

pytorch is one of mostly popular on Github Trending:

https://github.com/trending/c++?since=daily

PinkySan commented 5 years ago

´The following snipped packages the prebuilt binaries into your conan cache

conan create . haben/testing

runs the following code

from conans import ConanFile, CMake, tools

class Libtorch(ConanFile):
    name = "libtorch"
    version = "1.0.0"
    license = "need to be filled"
    url = "https://pytorch.org/"
    description = "need to be filled"
    settings = "os"
    options = {"cuda": ["8.0", "9.0","10.0", "None"],
                "nightly":["True", "False"]}
    default_options = {"cuda": "None",
                        "nightly": "False"}
    generators = "cmake"
    url_base = "https://download.pytorch.org/libtorch/"

    def getGPU(self):
        if(self.options.cuda == "8.0"):
            return "cu80/"
        if(self.options.cuda == "9.0"):
            return "cu90/"
        if(self.options.cuda == "10.0"):
            return "cu100/"
        else:
            return "cpu/"

    def getNightly(self):
        if(self.options.nightly == "True"):
            return "nightly/"
        else:
            return ""

    def getOS(self):
        if(self.settings.os == "Windows"):
            return "libtorch-win-shared-with-deps-latest.zip"
        if(self.settings.os == "Linux"):
            return "libtorch-shared-with-deps-latest.zip"
        if(self.settings.os == "MacOS"):
            return "libtorch-macos-latest.zip"

    def build(self):
        url = self.url_base + self.getNightly() + self.getGPU() + self.getOS()
        tools.get(url)

    def package(self):
        self.copy("*", src="libtorch/")
wumo commented 5 years ago

Based on @PinkySan , I write a recipe that supports build_type:

from conans import ConanFile, CMake, tools
from conans.util.env_reader import get_env
import os
import tempfile

class Libtorch(ConanFile):
    name = "libtorch"
    version = "nightly"
    license = "https://raw.githubusercontent.com/pytorch/pytorch/master/LICENSE"
    url = "https://pytorch.org/"
    description = "Tensors and Dynamic neural networks in Python with strong GPU acceleration"
    settings = "os", "build_type"
    options = {"cuda": ["8.0", "9.0", "10.0", "None"]}
    default_options = {"cuda": "None"}
    generators = "cmake"
    url_base = "https://download.pytorch.org/libtorch/nightly"

    def getGPU(self):
        if self.settings.os == 'Macos':
            return 'cpu'
        if self.options.cuda == "8.0":
            return "cu80"
        if self.options.cuda == "9.0":
            return "cu90"
        if self.options.cuda == "10.0":
            return "cu100"
        else:
            return "cpu"

    def getOS(self):
        if self.settings.os == "Windows":
            if self.settings.build_type == 'Debug' and self.options.cuda != '8.0':
                return "libtorch-win-shared-with-deps-debug-latest.zip"
            else:
                return "libtorch-win-shared-with-deps-latest.zip"
        if self.settings.os == "Linux":
            return "libtorch-shared-with-deps-latest.zip"
        if self.settings.os == "Macos":
            return "libtorch-macos-latest.zip"

    def build(self):
        name = f'{self.getGPU()}-{self.getOS()}'
        targetfile = os.path.join(tempfile.gettempdir(), name)
        if os.path.exists(targetfile) and not get_env('TORCH_FORCE_DOWNLOAD', False):
            self.output.info(f'Skipping download. Using cached {targetfile}')
        else:
            url = f'{self.url_base}/{self.getGPU()}/{self.getOS()}'
            self.output.info(f'Downloading libtorch from {url} to {targetfile}')
            tools.download(url, targetfile)
        tools.unzip(targetfile)

    def package(self):
        self.copy("*", src="libtorch/")

    def package_info(self):
        self.cpp_info.libs = ['torch', 'caffe2', 'c10', 'pthread']
        self.cpp_info.includedirs = ['include', 'include/torch/csrc/api/include']
        self.cpp_info.bindirs = ['bin']
        self.cpp_info.libdirs = ['lib']
        if self.options.cuda != 'None':
            self.cpp_info.libs.extend(
                ['cuda', 'nvrtc', 'nvToolsExt', 'cudart', 'caffe2_gpu',
                 'c10_cuda', 'cufft', 'curand', 'cudnn', 'culibos', 'cublas'])

Then in your CMakeLists.txt:

include(${CMAKE_CURRENT_LIST_DIR}/cmake/conan.cmake) # download from https://github.com/conan-io/cmake-conan
conan_cmake_run(
    BASIC_SETUP
    CONANFILE conanfile.py
    BUILD missing)

add_executable(MyExe src/main.cpp)
target_compile_definitions(MyExe
    PUBLIC
    _GLIBCXX_USE_CXX11_ABI=0)
target_link_libraries(MyExe
    PUBLIC
    ${CONAN_LIBS}
    $<$<PLATFORM_ID:Linux>:dl>)

You have to set _GLIBCXX_USE_CXX11_ABI=0 because of the issue and set /usr/local/cuda/lib64(cuda library path) to LIBRARY_PATH.

SSE4 commented 5 years ago

hi @PinkySan @wumo as you already have a working recipe(s), would you like to try to submit it to the new conan center index? thanks

PinkySan commented 5 years ago

Sounds good. What are the requirements for a recipe?

PinkySan commented 5 years ago

Started a pull request in: https://github.com/conan-io/conan-center-index/pull/169 @wumo may you please take a look at my version. I also added some of your code

wumo commented 5 years ago

@PinkySan well done. I can't do better!

PinkySan commented 5 years ago

Have been testing the version within my appveyor + libtorch repository. https://github.com/PinkySan/IrisDataset

orgroman-cg commented 3 years ago

Any update on this one? tflite, onnx and pytorch would be a huge plus for conan, from C++ deep learning developers point of view.

Croydon commented 3 years ago

There are still dependencies missing

https://github.com/conan-io/conan-center-index/issues/4427 https://github.com/conan-io/conan-center-index/issues/4728 https://github.com/conan-io/conan-center-index/issues/4729 https://github.com/conan-io/conan-center-index/issues/4731 https://github.com/conan-io/conan-center-index/issues/4783

@SpaceIm seems to work towards this, but I'm sure @SpaceIm won't mind help 😄

SpaceIm commented 3 years ago

Here is the PR for libtorch: https://github.com/conan-io/conan-center-index/pull/5100

Since CI of conan-center-index can't build it for the moment, I also try several configurations here: https://github.com/SpaceIm/conan-libtorch

Croydon commented 2 years ago

Can be closed in favor of https://github.com/conan-io/conan-center-index/issues/6861

Tumb1eweed commented 2 years ago

I have a problem when using pre-built packages in conan: https://stackoverflow.com/questions/72197623/problem-about-buiding-a-conan-package-for-libtorch

memsharded commented 2 years ago

@Tumb1eweed

This is not the right repository for submitting these issues, but conan-center-index one. If you are reporting there, do not copy & paste a link, but please fill correctly the report. Thanks.

SpaceIm commented 2 years ago

conan-center-index is not the right repository either, there is no libtorch recipe in conan-center.