Skip to content

build a libtorch extension  #159

Closed as not planned
Closed as not planned
@MaxCrazy1101

Description

@MaxCrazy1101
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 .)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions