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

CUDA #11

Merged
merged 59 commits into from
Jan 3, 2025
Merged

CUDA #11

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
9c655a4
🐛 Fix compilation error on clagn
yhs0602 Dec 31, 2024
8d507fd
🎨 Fix mm formatting
yhs0602 Dec 31, 2024
dca9336
✨ Try cuda
yhs0602 Dec 31, 2024
790a632
💚 Add cuda
yhs0602 Dec 31, 2024
f2a090c
:bug: Fix cuda build
yhs0602 Dec 31, 2024
a2dd988
Update docs
yhs0602 Dec 31, 2024
79bb5ec
Fix cuda installation in ci
yhs0602 Dec 31, 2024
113afb4
:bug: Fix cuda link cmake
yhs0602 Dec 31, 2024
843515a
:sparkles: Add use_conda option
yhs0602 Dec 31, 2024
51de389
:bug: Fix build
yhs0602 Dec 31, 2024
5373cb9
:bug: Fix link error
yhs0602 Dec 31, 2024
7264709
:bug: Correctly handle cuda case
yhs0602 Dec 31, 2024
3de3c1b
:bug: Error handling
yhs0602 Jan 1, 2025
6d53e0c
:bug: Fix include
yhs0602 Jan 1, 2025
24df66b
:bug: Handle error, valid colorAttachment
yhs0602 Jan 1, 2025
55264f1
:bug: Fix build error
yhs0602 Jan 1, 2025
1a54b19
:bug: Fix cuda ipc
yhs0602 Jan 1, 2025
fcc566b
:bug: Fix no dlpack
yhs0602 Jan 1, 2025
7c33f63
:bug: check cuda error
yhs0602 Jan 1, 2025
04fb1d6
:bug: Fix invalid tensor
yhs0602 Jan 1, 2025
8eef6af
:bug: Fix dlpack tensor evaporation
yhs0602 Jan 1, 2025
4bb5cb0
Add log for cuda map gl
yhs0602 Jan 1, 2025
c05c1e8
:bug: Fix not sharing data
yhs0602 Jan 1, 2025
e4a2dfd
:bug: Fix assertion
yhs0602 Jan 1, 2025
de55bad
:bug: Fix initialize cuda resource
yhs0602 Jan 1, 2025
f35b6ad
:bug: Fix color
yhs0602 Jan 1, 2025
4013912
:bug: Fix framebuffer binding
yhs0602 Jan 1, 2025
86728b4
:sparkles: Add log for dat
yhs0602 Jan 1, 2025
4abbb31
:bug: Ffflush
yhs0602 Jan 1, 2025
40b30c7
:bug: Fix strides
yhs0602 Jan 1, 2025
2122ada
Add log for ipc pointer
yhs0602 Jan 1, 2025
c154efc
:bug: Set gpu device for cuda ops
yhs0602 Jan 1, 2025
93230dc
:pencil: Put all log
yhs0602 Jan 1, 2025
1f98278
:bug: Map and unmap cuda resources every frame
yhs0602 Jan 1, 2025
9940827
:bug: Handle device error check
yhs0602 Jan 1, 2025
6342f3a
try cuda device number logging
yhs0602 Jan 1, 2025
a547031
:bug: Fix device id
yhs0602 Jan 1, 2025
d5ce79c
:art: Format cpp
yhs0602 Jan 2, 2025
cf64da0
:green_heart: Fix cuda installation ci
yhs0602 Jan 2, 2025
034db05
:green_heart: Fix cuda ci
yhs0602 Jan 2, 2025
9a0be29
:green_heart: Configure c++ tests
yhs0602 Jan 2, 2025
816e69b
:bug: Fix test builds
yhs0602 Jan 2, 2025
bf73a9a
:bug: Fix cuda link errors in test
yhs0602 Jan 2, 2025
f6ea370
:bug: Fix google test
yhs0602 Jan 2, 2025
f56facb
:sparkles: Enable ctest
yhs0602 Jan 2, 2025
e1742d9
:bug: Fix test builds
yhs0602 Jan 2, 2025
fc78e9e
:white_check_mark: Update pytest test
yhs0602 Jan 2, 2025
7d321c9
💚 Add cuda container ci
yhs0602 Jan 3, 2025
6396665
💚 Fix ci cuda
yhs0602 Jan 3, 2025
18d0922
💚 Fix cuda ci
yhs0602 Jan 3, 2025
fcb38cb
💚 Fix cmakefiles test cuda link
yhs0602 Jan 3, 2025
f61a592
🐛 Fix git install
yhs0602 Jan 3, 2025
e7c5362
✅ Pass c++ test
yhs0602 Jan 3, 2025
905cc0e
💚 Install cmake on cuda container
yhs0602 Jan 3, 2025
6d67b50
💚 Fix windows build: no rt
yhs0602 Jan 3, 2025
c6722f2
🐛 Fix ci
yhs0602 Jan 3, 2025
e6cb089
💚 Fix ci for python3-pip
yhs0602 Jan 3, 2025
f649e43
💚 Fix cuda repo path
yhs0602 Jan 3, 2025
333bd3e
💚 Fix cuda ci
yhs0602 Jan 3, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions .github/workflows/cmake-build-cuda.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: CMake on CUDA Docker

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:
runs-on: ubuntu-latest
container:
image: nvidia/cuda:12.6.3-devel-ubuntu22.04
steps:
- name: Install Git 2.18 or higher
run: |
apt-get update
apt-get install -y git cmake python3-dev ninja-build python3-pip
git --version
python3 --version
ln -s /usr/bin/python3 /usr/bin/python

- name: Install cmake
run: |
python -m pip install cmake
cmake --version

- uses: actions/checkout@v4
with:
submodules: true

- name: Set reusable strings
# Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file.
id: strings
shell: bash
run: |
echo "build-output-dir=$(pwd)/build" >> "$GITHUB_OUTPUT"
- name: Check CUDA installation
run: |
nvcc -V

- name: Configure CMake for python part
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: >
cmake -B ${{ steps.strings.outputs.build-output-dir }}
-S $(pwd)
-DBUILD_TESTS=ON # Enable testing

- name: Build
run: cmake --build ${{ steps.strings.outputs.build-output-dir }}

- name: Test
working-directory: ${{ steps.strings.outputs.build-output-dir }}
run: ctest
3 changes: 2 additions & 1 deletion .github/workflows/cmake-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ jobs:
-DCMAKE_C_COMPILER=${{ matrix.c_compiler }}
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
-S ${{ github.workspace }}
-DBUILD_TESTS=ON # Enable testing

- name: Build
# Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
Expand All @@ -74,4 +75,4 @@ jobs:
working-directory: ${{ steps.strings.outputs.build-output-dir }}
# Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest --build-config ${{ matrix.build_type }}
run: ctest --build-config ${{ matrix.build_type }}
32 changes: 31 additions & 1 deletion .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,38 @@ on:

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
# Set fail-fast to false to ensure that feedback is delivered for all matrix combinations. Consider changing this to true when your workflow is stable.
fail-fast: false

runs-on: ubuntu-latest
# Set up a matrix to run the following 3 configurations:
# 1. <Windows, Release, latest MSVC compiler toolchain on the default runner image, default generator>
# 2. <Linux, Release, latest GCC compiler toolchain on the default runner image, default generator>
# 3. <Linux, Release, latest Clang compiler toolchain on the default runner image, default generator>
#
# To add more build types (Release, Debug, RelWithDebInfo, etc.) customize the build_type list.
matrix:
os: [ubuntu-latest, windows-latest]
build_type: [Release]
c_compiler: [gcc, clang, cl]
include:
- os: windows-latest
c_compiler: cl
cpp_compiler: cl
- os: ubuntu-latest
c_compiler: gcc
cpp_compiler: g++
- os: ubuntu-latest
c_compiler: clang
cpp_compiler: clang++
exclude:
- os: windows-latest
c_compiler: gcc
- os: windows-latest
c_compiler: clang
- os: ubuntu-latest
c_compiler: cl
permissions:
contents: read

Expand Down
14 changes: 14 additions & 0 deletions .github/workflows/publish-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ jobs:

- uses: astral-sh/setup-uv@v4

- name: Install CUDA Toolkit (Windows/Linux only)
if: matrix.os == 'windows-latest' || matrix.os == 'ubuntu-latest'
uses: Jimver/[email protected]
id: cuda-toolkit
with:
cuda: '12.5.0'

- name: Check CUDA installation
if: matrix.os == 'windows-latest' || matrix.os == 'ubuntu-latest'
run: |
echo "Installed cuda version is: ${{steps.cuda-toolkit.outputs.cuda}}"
echo "Cuda install location: ${{steps.cuda-toolkit.outputs.CUDA_PATH}}"
nvcc -V

- name: Build wheels
uses: pypa/[email protected]
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
pytest tests/python/unit

build-package:
runs-on: ubuntu-latest
Expand Down
64 changes: 44 additions & 20 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
cmake_minimum_required(VERSION 3.15...3.29)
project(craftground LANGUAGES CXX)
include(GNUInstallDirs)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_CXX_STANDARD 17)
Expand Down Expand Up @@ -29,7 +30,7 @@ endif()
# Collect source files for the module
set(CRAFTGROUND_PY_SOURCES src/cpp/ipc.cpp)

if (APPLE)
if(APPLE)
# Add Apple-specific source files
list(APPEND CRAFTGROUND_PY_SOURCES src/cpp/ipc_apple.mm src/cpp/ipc_apple_torch.cpp)

Expand All @@ -54,25 +55,17 @@ if(APPLE)
target_link_libraries(craftground_native PRIVATE "${TORCH_LIBRARIES}")
set(LIBTORCH_DIR ${CMAKE_SOURCE_DIR}/src/cpp/libtorch)
target_link_libraries(craftground_native PRIVATE
${LIBTORCH_DIR}/libtorch_cpu_minimal.a
${LIBTORCH_DIR}/libtorch_cpu_minimal.a
${LIBTORCH_DIR}/libc10.a
)
target_include_directories(craftground_native PRIVATE "${TORCH_INCLUDE_DIRS}")
target_compile_definitions(craftground_native PRIVATE HAS_TORCH)
target_compile_options(craftground_native PRIVATE -ffunction-sections -fdata-sections)
target_link_options(craftground_native PRIVATE -Wl,-dead_strip)
# add_custom_command(
# TARGET craftground_native POST_BUILD
# COMMAND install_name_tool -change @rpath/libc10.dylib @loader_path/libc10.dylib $<TARGET_FILE:craftground_native>
# COMMAND install_name_tool -change @rpath/libtorch.dylib @loader_path/libtorch.dylib $<TARGET_FILE:craftground_native>
# COMMAND install_name_tool -change @rpath/libtorch_cpu.dylib @loader_path/libtorch_cpu.dylib $<TARGET_FILE:craftground_native>
# COMMENT "Updating RPATH to use relative paths"
# )

endif()
elseif(CUDAToolkit_FOUND)
target_include_directories(craftground_native PRIVATE ${CUDAToolkit_INCLUDE_DIRS})
target_link_libraries(craftground_native PRIVATE ${CUDAToolkit_LIBRARIES})
target_link_libraries(craftground_native PRIVATE CUDA::cudart CUDA::cudart_static)
endif()


Expand All @@ -85,13 +78,44 @@ target_compile_definitions(
PRIVATE VERSION_INFO=${PRIVATE_VERSION_INFO}
)

if(APPLE AND Torch_FOUND)
# install(FILES
# /usr/local/libtorch/lib/libc10.dylib
# /usr/local/libtorch/lib/libtorch.dylib
# /usr/local/libtorch/lib/libtorch_cpu.dylib
# DESTINATION craftground
# )
endif()
install(TARGETS craftground_native LIBRARY DESTINATION craftground)

option(BUILD_TESTS "Build tests" ON)
if(BUILD_TESTS)
enable_testing()
add_library(craftground STATIC ${CRAFTGROUND_PY_SOURCES})
find_package(Python3 REQUIRED COMPONENTS Development)
target_include_directories(
craftground PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/pybind11/include
${Python3_INCLUDE_DIRS}
)
if(APPLE)
if(Torch_FOUND)
target_link_libraries(craftground PUBLIC "${TORCH_LIBRARIES}")
set(LIBTORCH_DIR ${CMAKE_SOURCE_DIR}/src/cpp/libtorch)
target_link_libraries(craftground PUBLIC
${LIBTORCH_DIR}/libtorch_cpu_minimal.a
${LIBTORCH_DIR}/libc10.a
)
target_include_directories(craftground PUBLIC "${TORCH_INCLUDE_DIRS}")
target_compile_definitions(craftground PUBLIC HAS_TORCH)
target_compile_options(craftground PUBLIC -ffunction-sections -fdata-sections)
# target_link_options(craftground PUBLIC -Wl,-dead_strip)
endif()
elseif(CUDAToolkit_FOUND)
message(STATUS "CUDA is available in tests")
target_include_directories(craftground PUBLIC ${CUDAToolkit_INCLUDE_DIRS})
target_link_libraries(craftground PUBLIC CUDA::cudart CUDA::cudart_static)
endif()
target_link_options(craftground PRIVATE "-Wl,--whole-archive" "-Wl,--no-whole-archive")
target_include_directories(craftground PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp)

target_compile_options(craftground PUBLIC ${CRAFTGROUND_PY_COMPILE_OPTIONS})

install(TARGETS craftground_native LIBRARY DESTINATION craftground)
target_compile_definitions(
craftground
PUBLIC VERSION_INFO=${PRIVATE_VERSION_INFO}
)
add_subdirectory(tests)
endif()
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,15 @@ https://dejavu-fonts.github.io/License.html
```zsh
brew install ktlint clang-format google-java-format
```
```bash
wget https://apt.llvm.org/llvm.sh
sudo ./llvm.sh 19
sudo apt install clang-format-19
sudo ln -s /usr/bin/clang-format-19 /usr/bin/clang-format
```

## Run formatters

```bash
find . \( -iname '*.h' -o -iname '*.cpp' -o -iname '*.mm' \) | xargs clang-format -i
ktlint '!src/craftground/MinecraftEnv/src/main/java/com/kyhsgeekcode/minecraftenv/proto/**'
Expand All @@ -285,4 +293,21 @@ protoc proto/observation_space.proto --java_out=craftground/MinecraftEnv/src/mai
### Solution
```bash
pip install --upgrade protobuf
```


# Dev setup & build (conda, linux)
```
conda create --name craftground python=3.11
conda activate craftground
conda install gymnasium Pillow numpy protobuf typing_extensions psutil pytorch ninja build cmake
conda install -c conda-forge openjdk=21 libgl-devel
conda install glew
python -m build
```

## Build jvm c++ part
```bash
cmake src/main/cpp -DCMAKE_PREFIX_PATH=$CONDA_PREFIX
cmake --build .
```
35 changes: 21 additions & 14 deletions src/cpp/ipc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,40 @@ py::object
initialize_from_mach_port(unsigned int machPort, int width, int height) {
return mtl_tensor_from_mach_port(machPort, width, height);
}
py::object mtl_tensor_from_cuda_mem_handle(
py::capsule mtl_tensor_from_cuda_mem_handle(
const char *cuda_ipc_handle, int width, int height
) {
return py::none();
}

#elif __CUDA__
#elif HAS_CUDA
#include "ipc_cuda.h"
py::object initialize_from_mach_port(int machPort, int width, int height) {
py::object
initialize_from_mach_port(unsigned int machPort, int width, int height) {
return py::none();
}

py::object mtl_tensor_from_cuda_mem_handle(
py::capsule mtl_tensor_from_cuda_mem_handle(
const char *cuda_ipc_handle, int width, int height
) {
DLManagedTensor *tensor = mtl_tensor_from_cuda_ipc_handle(
const_cast<void *>(cuda_ipc_handle), width, height
reinterpret_cast<void *>(const_cast<char *>(cuda_ipc_handle)),
width,
height
);
return py::reinterpret_steal<py::object>(PyCapsule_New(
tensor,
"dltensor",
[](PyObject *capsule) {
DLManagedTensor *tensor =
(DLManagedTensor *)PyCapsule_GetPointer(capsule, "dltensor");
tensor->deleter(tensor);

if (!tensor) {
throw std::runtime_error(
"Failed to create DLManagedTensor from CUDA IPC handle"
);
}

return py::capsule(tensor, "dltensor", [](void *ptr) {
DLManagedTensor *managed_tensor = static_cast<DLManagedTensor *>(ptr);
if (managed_tensor && managed_tensor->deleter) {
managed_tensor->deleter(managed_tensor);
}
));
});
}

#else
Expand All @@ -44,7 +51,7 @@ initialize_from_mach_port(unsigned int machPort, int width, int height) {
return py::none();
}

py::object mtl_tensor_from_cuda_mem_handle(
py::capsule mtl_tensor_from_cuda_mem_handle(
const char *cuda_ipc_handle, int width, int height
) {
return py::none();
Expand Down
2 changes: 1 addition & 1 deletion src/cpp/ipc.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace py = pybind11;
py::object
initialize_from_mach_port(unsigned int machPort, int width, int height);
py::object mtl_tensor_from_cuda_mem_handle(
py::capsule mtl_tensor_from_cuda_mem_handle(
const char *cuda_ipc_handle, int width, int height
);

Expand Down
2 changes: 1 addition & 1 deletion src/cpp/ipc_apple.mm
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ static void deleteDLManagedTensor(DLManagedTensor *self) {
DLManagedTensor *tensor =
(DLManagedTensor *)malloc(sizeof(DLManagedTensor));

tensor->dl_tensor.data = mtlBuffer;
tensor->dl_tensor.data = (void *)mtlBuffer;
tensor->dl_tensor.ndim = 3; // H x W x C
tensor->dl_tensor.shape = (int64_t *)malloc(3 * sizeof(int64_t));
tensor->dl_tensor.shape[0] = height;
Expand Down
Loading
Loading