Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build a libtorch extension #159

Open
MaxCrazy1101 opened this issue Dec 3, 2024 · 0 comments
Open

build a libtorch extension #159

MaxCrazy1101 opened this issue Dec 3, 2024 · 0 comments

Comments

@MaxCrazy1101
Copy link

import scikit_test
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: libtorch.so: cannot open shared object file: No such file or directory
[build-system]
requires = ["scikit-build-core>=0.10", "pybind11"]
build-backend = "scikit_build_core.build"


[project]
name = "scikit_test"
version = "0.1.0"
description = "A minimal example package (with pybind11)"
readme = "README.md"
authors = [
    { name = "me", email = "[email protected]" },
]
requires-python = ">=3.9"
classifiers = [
    "Development Status :: 4 - Beta",
    "License :: OSI Approved :: MIT License",
    "Programming Language :: Python :: 3 :: Only",
    "Programming Language :: Python :: 3.7",
    "Programming Language :: Python :: 3.8",
    "Programming Language :: Python :: 3.9",
    "Programming Language :: Python :: 3.10",
    "Programming Language :: Python :: 3.11",
    "Programming Language :: Python :: 3.12",
    "Programming Language :: Python :: 3.13",
]

[project.optional-dependencies]
test = ["pytest"]


[tool.scikit-build]
wheel.expand-macos-universal-tags = true
minimum-version = "build-system.requires"


[tool.pytest.ini_options]
minversion = "8.0"
addopts = ["-ra", "--showlocals", "--strict-markers", "--strict-config"]
xfail_strict = true
log_cli_level = "INFO"
filterwarnings = [
    "error",
    "ignore::pytest.PytestCacheWarning",
]
testpaths = ["tests"]


[tool.cibuildwheel]
build-frontend = "build[uv]"
test-command = "pytest {project}/tests"
test-extras = ["test"]

[tool.cibuildwheel.pyodide]
build-frontend = { name = "build", args = ["--exports", "whole_archive"] }

[tool.ruff.lint]
extend-select = [
    "B", # flake8-bugbear
    "I", # isort
    "ARG", # flake8-unused-arguments
    "C4", # flake8-comprehensions
    "EM", # flake8-errmsg
    "ICN", # flake8-import-conventions
    "G", # flake8-logging-format
    "PGH", # pygrep-hooks
    "PIE", # flake8-pie
    "PL", # pylint
    "PT", # flake8-pytest-style
    "PTH", # flake8-use-pathlib
    "RET", # flake8-return
    "RUF", # Ruff-specific
    "SIM", # flake8-simplify
    "T20", # flake8-print
    "UP", # pyupgrade
    "YTT", # flake8-2020
    "EXE", # flake8-executable
    "NPY", # NumPy specific rules
    "PD", # pandas-vet
]
ignore = [
    "PLR09", # Too many X
    "PLR2004", # Magic comparison
]
isort.required-imports = ["from __future__ import annotations"]

[tool.ruff.lint.per-file-ignores]
"tests/**" = ["T20"]

# Require CMake 3.15+ (matching scikit-build-core) Use new versions of all
# policies up to CMake 3.27
cmake_minimum_required(VERSION 3.15...3.27)

# Scikit-build-core sets these values for you, or you can just hard-code the
# name and version.
project(${SKBUILD_PROJECT_NAME}
        VERSION ${SKBUILD_PROJECT_VERSION}
        LANGUAGES CXX)

set(PYBIND11_NEWPYTHON ON)
add_subdirectory("extern/pybind11")

# find libtorch
find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

file(GLOB SOURCE ${CMAKE_SOURCE_DIR}/src/*.cpp)
set(INCLUDE_DIR "${CMAKE_SOURCE_DIR}/include")
include_directories(${INCLUDE_DIR})

pybind11_add_module(${PROJECT_NAME} ${SOURCE})
target_link_libraries(${PROJECT_NAME} PRIVATE ${TORCH_LIBRARIES})

# This is passing in the version as a define just as an example
target_compile_definitions(${PROJECT_NAME} PRIVATE VERSION_INFO=${PROJECT_VERSION})

# The install directory is the output (wheel) directory
install(TARGETS ${PROJECT_NAME} DESTINATION .)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant