Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

LibTorch #187

Closed
kindlychung opened this issue Nov 21, 2018 · 15 comments
Closed

LibTorch #187

kindlychung opened this issue Nov 21, 2018 · 15 comments

Comments

@kindlychung
Copy link

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
Copy link
Member

I would suggest prioritizing this one.

pytorch is one of mostly popular on Github Trending:

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

@PinkySan
Copy link

´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
Copy link

wumo commented Mar 21, 2019

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
Copy link
Contributor

SSE4 commented Sep 28, 2019

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
Copy link

PinkySan commented Oct 2, 2019

Sounds good. What are the requirements for a recipe?

@PinkySan
Copy link

PinkySan commented Oct 2, 2019

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

@wumo
Copy link

wumo commented Oct 5, 2019

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

@PinkySan
Copy link

PinkySan commented Oct 5, 2019

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

@orgroman-cg
Copy link

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
Copy link
Contributor

Croydon commented Mar 3, 2021

There are still dependencies missing

conan-io/conan-center-index#4427
conan-io/conan-center-index#4728
conan-io/conan-center-index#4729
conan-io/conan-center-index#4731
conan-io/conan-center-index#4783

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

@SpaceIm
Copy link

SpaceIm commented Apr 3, 2021

Here is the PR for libtorch: conan-io/conan-center-index#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
Copy link
Contributor

Croydon commented Oct 13, 2021

Can be closed in favor of conan-io/conan-center-index#6861

@SSE4 SSE4 closed this as completed Oct 13, 2021
@Tumb1eweed
Copy link

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
Copy link
Member

@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
Copy link

SpaceIm commented May 11, 2022

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants