kernelforge 0.1.18__tar.gz → 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. {kernelforge-0.1.18 → kernelforge-0.2.0}/.github/workflows/ci.yml +4 -3
  2. {kernelforge-0.1.18 → kernelforge-0.2.0}/.github/workflows/code-quality.yml +3 -3
  3. {kernelforge-0.1.18 → kernelforge-0.2.0}/.gitignore +2 -0
  4. kernelforge-0.2.0/CMakeLists.txt +268 -0
  5. kernelforge-0.2.0/Makefile +47 -0
  6. {kernelforge-0.1.18 → kernelforge-0.2.0}/PKG-INFO +14 -12
  7. {kernelforge-0.1.18 → kernelforge-0.2.0}/README.md +11 -10
  8. kernelforge-0.2.0/examples/kernel_symm_rfp_large.py +146 -0
  9. kernelforge-0.2.0/examples/krr_energy_and_force.py +166 -0
  10. kernelforge-0.2.0/examples/krr_energy_only.py +161 -0
  11. kernelforge-0.2.0/examples/krr_force_only.py +164 -0
  12. {kernelforge-0.1.18 → kernelforge-0.2.0}/pyproject.toml +33 -12
  13. kernelforge-0.2.0/python/kernelforge/__init__.py +24 -0
  14. kernelforge-0.2.0/python/kernelforge/cli.py +991 -0
  15. kernelforge-0.2.0/python/kernelforge/fchl19_repr.pyi +36 -0
  16. kernelforge-0.2.0/python/kernelforge/global_kernels.pyi +60 -0
  17. kernelforge-0.2.0/python/kernelforge/invdist_repr.pyi +9 -0
  18. kernelforge-0.1.18/python/kernelforge/_cholesky.pyi → kernelforge-0.2.0/python/kernelforge/kernelmath.pyi +11 -8
  19. kernelforge-0.2.0/python/kernelforge/kitchen_sinks.pyi +40 -0
  20. kernelforge-0.2.0/python/kernelforge/local_kernels.pyi +46 -0
  21. {kernelforge-0.1.18 → kernelforge-0.2.0}/src/aligned_alloc64.hpp +2 -15
  22. kernelforge-0.2.0/src/blas_config.h +46 -0
  23. kernelforge-0.2.0/src/blas_int.h +23 -0
  24. {kernelforge-0.1.18 → kernelforge-0.2.0}/src/constants.hpp +0 -1
  25. kernelforge-0.2.0/src/fchl19_repr.cpp +795 -0
  26. kernelforge-0.2.0/src/fchl19_repr.hpp +29 -0
  27. kernelforge-0.2.0/src/fchl19_repr_bindings.cpp +253 -0
  28. kernelforge-0.2.0/src/global_kernels.cpp +2005 -0
  29. kernelforge-0.2.0/src/global_kernels.hpp +78 -0
  30. kernelforge-0.2.0/src/global_kernels_bindings.cpp +587 -0
  31. kernelforge-0.1.18/src/invdist.cpp → kernelforge-0.2.0/src/invdist_repr.cpp +4 -2
  32. kernelforge-0.1.18/src/invdist.hpp → kernelforge-0.2.0/src/invdist_repr.hpp +2 -0
  33. kernelforge-0.1.18/src/invdist_bindings.cpp → kernelforge-0.2.0/src/invdist_repr_bindings.cpp +14 -12
  34. kernelforge-0.1.18/src/fchl19_representation.cpp → kernelforge-0.2.0/src/local_kernels.cpp +10 -848
  35. kernelforge-0.2.0/src/local_kernels.hpp +64 -0
  36. kernelforge-0.1.18/src/bindings_fchl19.cpp → kernelforge-0.2.0/src/local_kernels_bindings.cpp +27 -329
  37. kernelforge-0.2.0/src/math.cpp +181 -0
  38. kernelforge-0.2.0/src/math.hpp +24 -0
  39. kernelforge-0.1.18/src/bindings_cholesky.cpp → kernelforge-0.2.0/src/math_bindings.cpp +46 -36
  40. kernelforge-0.2.0/src/rff_elemental.cpp +401 -0
  41. kernelforge-0.2.0/src/rff_elemental.hpp +99 -0
  42. kernelforge-0.2.0/src/rff_elemental_bindings.cpp +419 -0
  43. kernelforge-0.2.0/src/rff_features.cpp +41 -0
  44. kernelforge-0.2.0/src/rff_features.hpp +18 -0
  45. kernelforge-0.2.0/src/rff_features_bindings.cpp +83 -0
  46. kernelforge-0.2.0/tests/__init__.py +1 -0
  47. {kernelforge-0.1.18 → kernelforge-0.2.0}/tests/test_cholesky.py +12 -12
  48. {kernelforge-0.1.18 → kernelforge-0.2.0}/tests/test_fchl19.py +33 -19
  49. {kernelforge-0.1.18 → kernelforge-0.2.0}/tests/test_fchl19_gradient.py +19 -19
  50. kernelforge-0.2.0/tests/test_full_kernel.py +656 -0
  51. {kernelforge-0.1.18 → kernelforge-0.2.0}/tests/test_hessian.py +20 -20
  52. kernelforge-0.2.0/tests/test_hessian_rfp.py +167 -0
  53. {kernelforge-0.1.18 → kernelforge-0.2.0}/tests/test_invdist.py +1 -1
  54. kernelforge-0.2.0/tests/test_jacobian.py +314 -0
  55. {kernelforge-0.1.18 → kernelforge-0.2.0}/tests/test_kernels.py +7 -7
  56. kernelforge-0.2.0/tests/test_kitchen_sinks.py +786 -0
  57. kernelforge-0.2.0/tests/test_rfp.py +210 -0
  58. kernelforge-0.2.0/wheelhouse/kernelforge-0.2.0-cp310-cp310-macosx_15_0_arm64.whl +0 -0
  59. kernelforge-0.2.0/wheelhouse/kernelforge-0.2.0-cp311-cp311-macosx_15_0_arm64.whl +0 -0
  60. kernelforge-0.2.0/wheelhouse/kernelforge-0.2.0-cp312-cp312-macosx_15_0_arm64.whl +0 -0
  61. kernelforge-0.2.0/wheelhouse/kernelforge-0.2.0-cp313-cp313-macosx_15_0_arm64.whl +0 -0
  62. kernelforge-0.2.0/wheelhouse/kernelforge-0.2.0-cp314-cp314-macosx_15_0_arm64.whl +0 -0
  63. kernelforge-0.1.18/CMakeLists.txt +0 -185
  64. kernelforge-0.1.18/Makefile +0 -31
  65. kernelforge-0.1.18/python/kernelforge/__init__.py +0 -8
  66. kernelforge-0.1.18/python/kernelforge/_fchl19.pyi +0 -81
  67. kernelforge-0.1.18/python/kernelforge/_invdist.pyi +0 -10
  68. kernelforge-0.1.18/python/kernelforge/_kernels.pyi +0 -29
  69. kernelforge-0.1.18/python/kernelforge/cli.py +0 -417
  70. kernelforge-0.1.18/src/bindings.cpp +0 -99
  71. kernelforge-0.1.18/src/bindings_kernels.cpp +0 -286
  72. kernelforge-0.1.18/src/cholesky.cpp +0 -182
  73. kernelforge-0.1.18/src/cholesky.hpp +0 -17
  74. kernelforge-0.1.18/src/fchl19_representation.hpp +0 -114
  75. kernelforge-0.1.18/src/kernels.cpp +0 -495
  76. kernelforge-0.1.18/src/kernels.hpp +0 -27
  77. kernelforge-0.1.18/tests/test_jacobian.py +0 -158
  78. kernelforge-0.1.18/tests/test_rfp.py +0 -107
  79. kernelforge-0.1.18/wheelhouse/kernelforge-0.1.18-cp310-cp310-macosx_15_0_arm64.whl +0 -0
  80. kernelforge-0.1.18/wheelhouse/kernelforge-0.1.18-cp311-cp311-macosx_15_0_arm64.whl +0 -0
  81. kernelforge-0.1.18/wheelhouse/kernelforge-0.1.18-cp312-cp312-macosx_15_0_arm64.whl +0 -0
  82. kernelforge-0.1.18/wheelhouse/kernelforge-0.1.18-cp313-cp313-macosx_15_0_arm64.whl +0 -0
  83. kernelforge-0.1.18/wheelhouse/kernelforge-0.1.18-cp314-cp314-macosx_15_0_arm64.whl +0 -0
  84. {kernelforge-0.1.18 → kernelforge-0.2.0}/.clang-format +0 -0
  85. {kernelforge-0.1.18 → kernelforge-0.2.0}/.clang-tidy +0 -0
  86. {kernelforge-0.1.18 → kernelforge-0.2.0}/.github/dependabot.yml +0 -0
  87. {kernelforge-0.1.18 → kernelforge-0.2.0}/.github/workflows/release.yml +0 -0
  88. {kernelforge-0.1.18 → kernelforge-0.2.0}/.pre-commit-config.yaml +0 -0
  89. {kernelforge-0.1.18 → kernelforge-0.2.0}/LICENSE +0 -0
  90. {kernelforge-0.1.18 → kernelforge-0.2.0}/examples/desc_hess-gauss.ipynb +0 -0
  91. {kernelforge-0.1.18 → kernelforge-0.2.0}/examples/desc_hess.ipynb +0 -0
  92. {kernelforge-0.1.18 → kernelforge-0.2.0}/examples/ethanol.sdf +0 -0
  93. {kernelforge-0.1.18 → kernelforge-0.2.0}/examples/fchl_example.ipynb +0 -0
  94. {kernelforge-0.1.18 → kernelforge-0.2.0}/examples/fchl_hess.ipynb +0 -0
  95. {kernelforge-0.1.18 → kernelforge-0.2.0}/examples/qm7_example.ipynb +0 -0
  96. {kernelforge-0.1.18 → kernelforge-0.2.0}/examples/regression_ethanol-Copy1.ipynb +0 -0
  97. {kernelforge-0.1.18 → kernelforge-0.2.0}/examples/regression_forces.ipynb +0 -0
  98. {kernelforge-0.1.18 → kernelforge-0.2.0}/examples/sgdml.ipynb +0 -0
  99. {kernelforge-0.1.18 → kernelforge-0.2.0}/python/kernelforge/py.typed +0 -0
@@ -29,9 +29,9 @@ jobs:
29
29
  if: runner.os == 'macOS'
30
30
  run: brew install libomp llvm
31
31
 
32
- - name: Install OpenBLAS (Linux only)
32
+ - name: Install OpenBLAS ILP64 + LAPACKE (Linux only)
33
33
  if: runner.os == 'Linux'
34
- run: sudo apt-get install -y libopenblas-dev
34
+ run: sudo apt-get install -y libopenblas64-dev libopenblas64-pthread-dev liblapacke-dev
35
35
 
36
36
  - name: Set up uv
37
37
  uses: astral-sh/setup-uv@v7
@@ -59,7 +59,8 @@ jobs:
59
59
  run: |
60
60
  uv sync --dev --all-extras
61
61
  uv pip install scikit-build-core pybind11 setuptools-scm
62
- uv pip install -e .[test] --no-build-isolation
62
+ CMAKE_ARGS="-DKF_BLAS_ILP64=ON" \
63
+ uv pip install -e .[test] --no-build-isolation
63
64
 
64
65
 
65
66
  - name: Run pytest
@@ -31,7 +31,7 @@ jobs:
31
31
  python-version: "3.12"
32
32
 
33
33
  - name: Install code quality tools
34
- run: uv pip install --system ruff mypy numpy pytest
34
+ run: uv pip install --system ruff ty numpy pytest
35
35
 
36
36
  - name: Check formatting with Ruff
37
37
  run: ruff format --check python/ tests/
@@ -39,5 +39,5 @@ jobs:
39
39
  - name: Lint with Ruff
40
40
  run: ruff check python/ tests/
41
41
 
42
- - name: Type check with mypy
43
- run: mypy python/ tests/
42
+ - name: Type check with ty
43
+ run: ty check python/ tests/
@@ -210,3 +210,5 @@ marimo/_lsp/
210
210
  __marimo__/
211
211
 
212
212
  uv.lock
213
+
214
+ .opencode/
@@ -0,0 +1,268 @@
1
+ cmake_minimum_required(VERSION 3.18)
2
+ project(kernelforge LANGUAGES C CXX)
3
+
4
+ # C++ standard
5
+ set(CMAKE_CXX_STANDARD 17)
6
+ set(CMAKE_CXX_STANDARD_REQUIRED ON)
7
+ set(CMAKE_CXX_EXTENSIONS OFF)
8
+
9
+ option(KF_BLAS_ILP64 "Use 64-bit integers for BLAS/LAPACK (ILP64)" OFF)
10
+
11
+ if(APPLE)
12
+
13
+ set(CMAKE_OSX_DEPLOYMENT_TARGET "15.0" CACHE STRING "" FORCE)
14
+ add_compile_definitions(ACCELERATE_NEW_LAPACK)
15
+ set(CMAKE_OSX_ARCHITECTURES "arm64" CACHE STRING "" FORCE)
16
+
17
+ # Necessary to compile with -Accelerate, homebrew clang and openmp
18
+ # Took me way too long to figure out
19
+ add_compile_options(-stdlib=libc++)
20
+ add_link_options(
21
+ -stdlib=libc++
22
+ -L/opt/homebrew/opt/llvm/lib/c++
23
+ -Wl,-rpath,/opt/homebrew/opt/llvm/lib/c++
24
+ )
25
+
26
+ endif()
27
+
28
+ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
29
+
30
+ find_package(Python COMPONENTS Interpreter Development.Module REQUIRED)
31
+ execute_process(
32
+ COMMAND "${Python_EXECUTABLE}" -m pybind11 --cmakedir
33
+ OUTPUT_VARIABLE pybind11_DIR
34
+ OUTPUT_STRIP_TRAILING_WHITESPACE
35
+ )
36
+ find_package(pybind11 CONFIG REQUIRED)
37
+
38
+ find_package(OpenMP REQUIRED)
39
+ if (OpenMP_CXX_FOUND)
40
+ if (APPLE)
41
+ # Apple/Homebrew Clang requires explicit flags
42
+ add_compile_options(-Xclang -fopenmp -I/opt/homebrew/opt/libomp/include)
43
+ add_link_options(-L/opt/homebrew/opt/libomp/lib -lomp)
44
+ else()
45
+ add_compile_options(${OpenMP_CXX_FLAGS})
46
+ add_link_options(${OpenMP_CXX_FLAGS})
47
+ endif()
48
+ endif()
49
+
50
+ # BLAS vendor selection (Linux only; macOS always uses Accelerate)
51
+ # Values: AUTO (default), MKL, OpenBLAS
52
+ set(KF_BLAS_VENDOR "AUTO" CACHE STRING "BLAS vendor: AUTO, MKL, OpenBLAS")
53
+ set_property(CACHE KF_BLAS_VENDOR PROPERTY STRINGS AUTO MKL OpenBLAS)
54
+
55
+ if(KF_BLAS_ILP64)
56
+ add_compile_definitions(KF_BLAS_ILP64)
57
+ endif()
58
+
59
+ # ---- BLAS/LAPACK backend detection -------------------------------------------
60
+ if(APPLE)
61
+ # macOS: Always use Accelerate framework
62
+ # ILP64 defines are set in blas_config.h (ACCELERATE_BLAS_ILP64, ACCELERATE_LAPACK_ILP64)
63
+ find_library(ACCELERATE Accelerate REQUIRED)
64
+ set(KF_BLAS_BACKEND "Accelerate")
65
+ message(STATUS "BLAS backend: Accelerate (Apple)")
66
+
67
+ elseif(NOT KF_BLAS_VENDOR STREQUAL "OpenBLAS")
68
+ # Linux: Try Intel MKL first (unless explicitly set to OpenBLAS)
69
+ # MKL configuration:
70
+ # - MKL_THREADING=gnu_thread: Use GNU OpenMP (libgomp) instead of Intel OpenMP (libiomp5)
71
+ # - MKL_LINK=dynamic: Dynamic linking (shared libraries)
72
+ # - MKL_INTERFACE=lp64/ilp64: 32-bit or 64-bit integers
73
+ set(MKL_THREADING gnu_thread) # Use libgomp (GCC/Clang OpenMP), not libiomp5
74
+ set(MKL_LINK dynamic)
75
+ if(KF_BLAS_ILP64)
76
+ set(MKL_INTERFACE ilp64)
77
+ else()
78
+ set(MKL_INTERFACE lp64)
79
+ endif()
80
+
81
+ # Add standard MKL install location to CMake prefix path
82
+ list(PREPEND CMAKE_PREFIX_PATH /opt/intel/oneapi/mkl/latest)
83
+
84
+ # Try to find MKL (QUIET: don't error if not found)
85
+ find_package(MKL QUIET)
86
+
87
+ if(MKL_FOUND)
88
+ # MKL found: Set compile definitions
89
+ add_compile_definitions(KF_USE_MKL)
90
+ # MKL_ILP64 define is set in blas_config.h when KF_BLAS_ILP64 is defined
91
+ set(KF_BLAS_BACKEND "MKL")
92
+ message(STATUS "BLAS backend: Intel MKL (${MKL_INTERFACE}, ${MKL_THREADING})")
93
+ elseif(KF_BLAS_VENDOR STREQUAL "MKL")
94
+ # MKL explicitly requested but not found: error
95
+ message(FATAL_ERROR "Intel MKL explicitly requested (KF_BLAS_VENDOR=MKL) but not found. "
96
+ "Install MKL or set KF_BLAS_VENDOR=OpenBLAS or KF_BLAS_VENDOR=AUTO.")
97
+ endif()
98
+ endif()
99
+
100
+ # Fallback: Use generic BLAS (OpenBLAS, ATLAS, reference BLAS, etc.)
101
+ if(NOT DEFINED KF_BLAS_BACKEND)
102
+ if(KF_BLAS_VENDOR STREQUAL "OpenBLAS")
103
+ # Explicitly force FindBLAS to only search for OpenBLAS (avoids finding MKL
104
+ # when the oneAPI environment is active and MKL is on LD_LIBRARY_PATH)
105
+ set(BLA_VENDOR OpenBLAS)
106
+ endif()
107
+ if(KF_BLAS_ILP64)
108
+ # OpenBLAS ILP64: requires BLA_SIZEOF_INTEGER=8
109
+ set(BLA_SIZEOF_INTEGER 8)
110
+ # OPENBLAS_USE64BITINT define is set in blas_config.h
111
+ endif()
112
+
113
+ find_package(BLAS REQUIRED)
114
+ set(KF_BLAS_BACKEND "OpenBLAS")
115
+ message(STATUS "BLAS backend: OpenBLAS/generic BLAS")
116
+ endif()
117
+
118
+ # Common interface for headers from Python/pybind11
119
+ add_library(kf_common INTERFACE)
120
+ target_link_libraries(kf_common INTERFACE pybind11::headers Python::Module)
121
+
122
+ # Common interface for BLAS backend (provides include directories)
123
+ add_library(kf_blas INTERFACE)
124
+ if(KF_BLAS_BACKEND STREQUAL "Accelerate")
125
+ # Accelerate doesn't need explicit include directories
126
+ target_link_libraries(kf_blas INTERFACE ${ACCELERATE})
127
+ elseif(KF_BLAS_BACKEND STREQUAL "MKL")
128
+ # MKL::MKL provides both include directories and libraries
129
+ target_link_libraries(kf_blas INTERFACE MKL::MKL)
130
+ else()
131
+ # BLAS::BLAS provides libraries (include paths may vary by system)
132
+ target_link_libraries(kf_blas INTERFACE BLAS::BLAS)
133
+ endif()
134
+
135
+ # ---- Small helpers to avoid repetition --------------------------------------
136
+ # Track created modules/objlibs so we can link things in one go later
137
+ set(_KF_ALL_MODULES "")
138
+ set(_KF_ALL_OBJLIBS "")
139
+
140
+ # Create a C++ object library + pybind11 module pair:
141
+ # kf_add_cpp_module(<base> <obj_src> <binding_src>)
142
+ # -> object lib: kf_<base>
143
+ # -> module target: <base> (no underscore prefix)
144
+ function(kf_add_cpp_module base obj_src bind_src)
145
+ set(obj kf_${base})
146
+ add_library(${obj} OBJECT ${obj_src})
147
+ target_link_libraries(${obj} PRIVATE kf_common kf_blas OpenMP::OpenMP_CXX)
148
+
149
+ pybind11_add_module(${base} MODULE
150
+ ${bind_src}
151
+ $<TARGET_OBJECTS:${obj}>
152
+ )
153
+ set_target_properties(${base} PROPERTIES OUTPUT_NAME "${base}")
154
+ target_link_libraries(${base} PRIVATE kf_blas OpenMP::OpenMP_CXX)
155
+
156
+ list(APPEND _KF_ALL_MODULES ${base})
157
+ list(APPEND _KF_ALL_OBJLIBS ${obj})
158
+ set(_KF_ALL_MODULES "${_KF_ALL_MODULES}" PARENT_SCOPE)
159
+ set(_KF_ALL_OBJLIBS "${_KF_ALL_OBJLIBS}" PARENT_SCOPE)
160
+ endfunction()
161
+
162
+ # Portable optimization; native tuning is opt-in
163
+ option(KF_USE_NATIVE "Enable -march/-mcpu=native style flags" OFF)
164
+
165
+ function(kf_apply_cxx_flags tgt)
166
+ if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
167
+ target_compile_options(${tgt} PRIVATE
168
+ -O3 -ffast-math -ftree-vectorize -fopenmp
169
+ $<$<BOOL:${KF_USE_NATIVE}>:-mcpu=native -mtune=native>
170
+ )
171
+ elseif (CMAKE_CXX_COMPILER_ID MATCHES "Intel")
172
+ target_compile_options(${tgt} PRIVATE
173
+ -O3 -ffast-math
174
+ $<$<BOOL:${KF_USE_NATIVE}>:-xHost -mtune=native>
175
+ )
176
+ endif()
177
+ endfunction()
178
+
179
+ # ---- C++ modules -------------------------------------------------------------
180
+ kf_add_cpp_module(global_kernels src/global_kernels.cpp src/global_kernels_bindings.cpp)
181
+ # FCHL19 is split into two modules: repr and local_kernels
182
+ kf_add_cpp_module(local_kernels src/local_kernels.cpp src/local_kernels_bindings.cpp)
183
+ kf_add_cpp_module(fchl19_repr src/fchl19_repr.cpp src/fchl19_repr_bindings.cpp)
184
+ kf_add_cpp_module(invdist_repr src/invdist_repr.cpp src/invdist_repr_bindings.cpp)
185
+ kf_add_cpp_module(kernelmath src/math.cpp src/math_bindings.cpp)
186
+
187
+ # Kitchen sinks (Random Fourier Features) — split across two obj/binding sources;
188
+ # handled manually so we can pass multiple files without extending the macro.
189
+ add_library(kf_kitchen_sinks OBJECT
190
+ src/rff_features.cpp
191
+ src/rff_elemental.cpp)
192
+ target_link_libraries(kf_kitchen_sinks PRIVATE kf_common kf_blas OpenMP::OpenMP_CXX)
193
+ kf_apply_cxx_flags(kf_kitchen_sinks)
194
+
195
+ pybind11_add_module(kitchen_sinks MODULE
196
+ src/rff_features_bindings.cpp
197
+ src/rff_elemental_bindings.cpp
198
+ $<TARGET_OBJECTS:kf_kitchen_sinks>)
199
+ set_target_properties(kitchen_sinks PROPERTIES OUTPUT_NAME "kitchen_sinks")
200
+ target_link_libraries(kitchen_sinks PRIVATE kf_blas)
201
+
202
+ # Apply C++ flags to the object libs (not to the module targets)
203
+ foreach(obj ${_KF_ALL_OBJLIBS})
204
+ kf_apply_cxx_flags(${obj})
205
+ endforeach()
206
+
207
+ # ---- OpenMP (C++) ------------------------------------------------------------
208
+ if (OpenMP_CXX_FOUND)
209
+ target_link_libraries(kernelmath PRIVATE OpenMP::OpenMP_CXX)
210
+ target_link_libraries(global_kernels PRIVATE OpenMP::OpenMP_CXX)
211
+ target_link_libraries(local_kernels PRIVATE OpenMP::OpenMP_CXX)
212
+ target_link_libraries(fchl19_repr PRIVATE OpenMP::OpenMP_CXX)
213
+ target_link_libraries(invdist_repr PRIVATE OpenMP::OpenMP_CXX)
214
+ # kitchen_sinks already links OpenMP via kf_kitchen_sinks object lib above
215
+ target_link_libraries(kitchen_sinks PRIVATE OpenMP::OpenMP_CXX)
216
+ endif()
217
+
218
+ # Note: BLAS backend linking is handled via kf_blas interface library
219
+ # (linked in kf_add_cpp_module and kitchen_sinks setup)
220
+
221
+ # ---- Install ----------------------------------------------------------------
222
+ install(TARGETS global_kernels local_kernels fchl19_repr invdist_repr kernelmath kitchen_sinks
223
+ LIBRARY DESTINATION kernelforge # Linux/macOS
224
+ RUNTIME DESTINATION kernelforge # Windows (.pyd)
225
+ )
226
+ install(FILES python/kernelforge/__init__.py DESTINATION kernelforge)
227
+
228
+ # ---- Development targets -----------------------------------------------------
229
+ # Collect all C++ source files
230
+ file(GLOB_RECURSE KF_CXX_SOURCES
231
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp
232
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/*.hpp
233
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/*.h
234
+ )
235
+
236
+ # clang-format target
237
+ find_program(CLANG_FORMAT_EXE NAMES clang-format)
238
+ if(CLANG_FORMAT_EXE)
239
+ add_custom_target(format
240
+ COMMAND ${CLANG_FORMAT_EXE} -i ${KF_CXX_SOURCES}
241
+ WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
242
+ COMMENT "Running clang-format on all C++ source files"
243
+ VERBATIM
244
+ )
245
+ else()
246
+ add_custom_target(format
247
+ COMMAND ${CMAKE_COMMAND} -E echo "clang-format not found. Please install it to use this target."
248
+ COMMENT "clang-format not available"
249
+ )
250
+ endif()
251
+
252
+ # clang-tidy target
253
+ find_program(CLANG_TIDY_EXE NAMES clang-tidy)
254
+ if(CLANG_TIDY_EXE)
255
+ add_custom_target(tidy
256
+ COMMAND ${CLANG_TIDY_EXE} ${KF_CXX_SOURCES} --
257
+ -std=c++17
258
+ -I${CMAKE_CURRENT_SOURCE_DIR}/src
259
+ WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
260
+ COMMENT "Running clang-tidy on all C++ source files"
261
+ VERBATIM
262
+ )
263
+ else()
264
+ add_custom_target(tidy
265
+ COMMAND ${CMAKE_COMMAND} -E echo "clang-tidy not found. Please install it to use this target."
266
+ COMMENT "clang-tidy not available"
267
+ )
268
+ endif()
@@ -0,0 +1,47 @@
1
+ all: environment
2
+
3
+ install-linux:
4
+ CMAKE_ARGS="-DKF_USE_NATIVE=ON" uv pip install -e .[test] --verbose
5
+
6
+ install-linux-ilp64:
7
+ CMAKE_ARGS="-DKF_USE_NATIVE=ON -DKF_BLAS_ILP64=ON" uv pip install -e .[test] --verbose
8
+
9
+ install-linux-mkl:
10
+ CMAKE_ARGS="-DKF_USE_NATIVE=ON -DKF_BLAS_VENDOR=MKL" uv pip install -e .[test] --verbose
11
+
12
+ install-linux-mkl-ilp64:
13
+ CMAKE_ARGS="-DKF_USE_NATIVE=ON -DKF_BLAS_VENDOR=MKL -DKF_BLAS_ILP64=ON" uv pip install -e .[test] --verbose
14
+
15
+ install-linux-openblas:
16
+ CMAKE_ARGS="-DKF_USE_NATIVE=ON -DKF_BLAS_VENDOR=OpenBLAS" uv pip install -e .[test] --verbose
17
+
18
+ install-linux-openblas-ilp64:
19
+ CMAKE_ARGS="-DKF_USE_NATIVE=ON -DKF_BLAS_VENDOR=OpenBLAS -DKF_BLAS_ILP64=ON" uv pip install -e .[test] --verbose
20
+
21
+ install-macos:
22
+ CMAKE_ARGS="-DCMAKE_C_COMPILER=/opt/homebrew/opt/llvm/bin/clang -DCMAKE_CXX_COMPILER=/opt/homebrew/opt/llvm/bin/clang++ -DKF_USE_NATIVE=ON " uv pip install -e .[test] --verbose
23
+
24
+ install-macos-ilp64:
25
+ CMAKE_ARGS="-DCMAKE_C_COMPILER=/opt/homebrew/opt/llvm/bin/clang -DCMAKE_CXX_COMPILER=/opt/homebrew/opt/llvm/bin/clang++ -DKF_USE_NATIVE=ON -DKF_BLAS_ILP64=ON" uv pip install -e .[test] --verbose
26
+
27
+ test:
28
+ uv run pytest
29
+
30
+ environment:
31
+ uv venv --python 3.14
32
+ uv pip install scikit-build-core pybind11
33
+
34
+ check: format typecheck
35
+
36
+ format:
37
+ uv run ruff format python/ tests/
38
+ uv run ruff check --select I --fix python/ tests/
39
+
40
+ typecheck:
41
+ uv run ty check python/ tests/
42
+
43
+ clean:
44
+ rm -rf ./.venv/
45
+ rm -rf ./.ruff_cache/
46
+ rm -rf ./.pytest_cache/
47
+
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: kernelforge
3
- Version: 0.1.18
3
+ Version: 0.2.0
4
4
  Summary: Optimized Kernels for ML
5
5
  Author: Anders Christensen
6
6
  License: MIT
@@ -22,21 +22,21 @@ Project-URL: Homepage, https://github.com/andersx/kernelforge
22
22
  Project-URL: Issues, https://github.com/andersx/kernelforge/issues
23
23
  Requires-Python: >=3.10
24
24
  Requires-Dist: numpy>=2.00
25
+ Requires-Dist: typing-extensions>=4.0.0; python_version < "3.11"
25
26
  Provides-Extra: test
26
27
  Requires-Dist: pytest>=8; extra == "test"
27
28
  Requires-Dist: pytest-xdist; extra == "test"
28
29
  Requires-Dist: pytest-timeout; extra == "test"
29
30
  Provides-Extra: dev
30
31
  Requires-Dist: ruff>=0.8.0; extra == "dev"
31
- Requires-Dist: mypy>=1.9.0; extra == "dev"
32
+ Requires-Dist: ty; extra == "dev"
32
33
  Requires-Dist: pre-commit>=3.6.0; extra == "dev"
33
34
  Description-Content-Type: text/markdown
34
35
 
35
36
  # KernelForge - Optimized Kernels for ML
36
37
 
37
38
  [![CI](https://github.com/andersx/kernelforge/actions/workflows/ci.yml/badge.svg)](https://github.com/andersx/kernelforge/actions/workflows/ci.yml)
38
- [![Code Quality](https://github.com/andersx/kernelforge/actions/workflows/code-quality.yml/badge.svg)](https://github.com/andersx/kernelforge/actions/workflows/code-quality.yml)
39
- [![PyPI version](https://badge.fury.io/py/kernelforge.svg)](https://pypi.org/project/kernelforge/)
39
+ [![PyPI version](https://img.shields.io/pypi/v/kernelforge)](https://pypi.org/project/kernelforge/)
40
40
  [![Python Versions](https://img.shields.io/pypi/pyversions/kernelforge?logo=python&logoColor=white)](https://pypi.org/project/kernelforge/)
41
41
  [![Platform](https://img.shields.io/badge/platform-linux%20%7C%20macos-lightgrey)](https://github.com/andersx/kernelforge)
42
42
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
@@ -195,8 +195,8 @@ The goal is to remove pain-points of existing QML libraries
195
195
  - [x] Setup GHA to build PyPI wheels
196
196
  - [x] Test Linux build matrices
197
197
  - [x] Test MacOS build matrices
198
- - [ ] Test Windows build matrices
199
- - [x] Add build for all Python version >=3.11
198
+ - [ ] ~~Test Windows build matrices~~ No.
199
+ - [x] Add build for all Python version >=3.10
200
200
  - [ ] Plan structure for saving models for inference as `.npz` files
201
201
  - Ensure correct linking with optimized BLAS/LAPACK libraries:
202
202
  - [x] OpenBLAS (Linux) <- also used in wheels
@@ -205,10 +205,11 @@ The goal is to remove pain-points of existing QML libraries
205
205
  - Add global kernels:
206
206
  - [x] Gaussian kernel
207
207
  - [x] Jacobian/gradient kernel
208
- - [ ] Optimized Jacobian kernel for single inference
208
+ - [ ] Optimized kernel for single inference (for MD)
209
209
  - [x] Hessian kernel
210
210
  - [x] GDML-like kernel
211
- - [ ] Full GPR kernel
211
+ - [x] Full GPR kernel
212
+ - [x] All kernels in RFP format
212
213
  - Add local kernels:
213
214
  - [x] Gaussian kernel
214
215
  - [x] Jacobian/gradient kernel
@@ -217,10 +218,10 @@ The goal is to remove pain-points of existing QML libraries
217
218
  - [ ] Full GPR kernel
218
219
  - [ ] Optimized GPR kernel with pre-computed terms for single inference/MD
219
220
  - Add random Fourier features kernel code:
220
- - [ ] Fourier-basis sampler
221
- - [ ] RFF kernel
222
- - [ ] RFF gradient kernel
223
- - [ ] RFF chunked DSYRK kernel
221
+ - [ ] Fourier-basis sampler in C++ with OpenMP parallelization
222
+ - [x] RFF kernel
223
+ - [x] RFF gradient kernel
224
+ - [x] RFF chunked DSYRK kernel
224
225
  - [ ] Optimized RFF gradient kernel for single inference/MD
225
226
  - The same as above, just for Hadamard features when I find the time?
226
227
  - GDML and sGDML kernels:
@@ -239,6 +240,7 @@ The goal is to remove pain-points of existing QML libraries
239
240
  - [x] Cholesky in-place solver
240
241
  - [x] L2-reg kwarg
241
242
  - [x] Toggle destructive vs non-destructive
243
+ - [x] RFP format in-place Cholesky solver
242
244
  - [ ] QR and/or SVD for non-square matrices
243
245
  - Add moleular descriptors with derivatives:
244
246
  - [ ] Coulomb matrix + misc variants without derivatives
@@ -1,8 +1,7 @@
1
1
  # KernelForge - Optimized Kernels for ML
2
2
 
3
3
  [![CI](https://github.com/andersx/kernelforge/actions/workflows/ci.yml/badge.svg)](https://github.com/andersx/kernelforge/actions/workflows/ci.yml)
4
- [![Code Quality](https://github.com/andersx/kernelforge/actions/workflows/code-quality.yml/badge.svg)](https://github.com/andersx/kernelforge/actions/workflows/code-quality.yml)
5
- [![PyPI version](https://badge.fury.io/py/kernelforge.svg)](https://pypi.org/project/kernelforge/)
4
+ [![PyPI version](https://img.shields.io/pypi/v/kernelforge)](https://pypi.org/project/kernelforge/)
6
5
  [![Python Versions](https://img.shields.io/pypi/pyversions/kernelforge?logo=python&logoColor=white)](https://pypi.org/project/kernelforge/)
7
6
  [![Platform](https://img.shields.io/badge/platform-linux%20%7C%20macos-lightgrey)](https://github.com/andersx/kernelforge)
8
7
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
@@ -161,8 +160,8 @@ The goal is to remove pain-points of existing QML libraries
161
160
  - [x] Setup GHA to build PyPI wheels
162
161
  - [x] Test Linux build matrices
163
162
  - [x] Test MacOS build matrices
164
- - [ ] Test Windows build matrices
165
- - [x] Add build for all Python version >=3.11
163
+ - [ ] ~~Test Windows build matrices~~ No.
164
+ - [x] Add build for all Python version >=3.10
166
165
  - [ ] Plan structure for saving models for inference as `.npz` files
167
166
  - Ensure correct linking with optimized BLAS/LAPACK libraries:
168
167
  - [x] OpenBLAS (Linux) <- also used in wheels
@@ -171,10 +170,11 @@ The goal is to remove pain-points of existing QML libraries
171
170
  - Add global kernels:
172
171
  - [x] Gaussian kernel
173
172
  - [x] Jacobian/gradient kernel
174
- - [ ] Optimized Jacobian kernel for single inference
173
+ - [ ] Optimized kernel for single inference (for MD)
175
174
  - [x] Hessian kernel
176
175
  - [x] GDML-like kernel
177
- - [ ] Full GPR kernel
176
+ - [x] Full GPR kernel
177
+ - [x] All kernels in RFP format
178
178
  - Add local kernels:
179
179
  - [x] Gaussian kernel
180
180
  - [x] Jacobian/gradient kernel
@@ -183,10 +183,10 @@ The goal is to remove pain-points of existing QML libraries
183
183
  - [ ] Full GPR kernel
184
184
  - [ ] Optimized GPR kernel with pre-computed terms for single inference/MD
185
185
  - Add random Fourier features kernel code:
186
- - [ ] Fourier-basis sampler
187
- - [ ] RFF kernel
188
- - [ ] RFF gradient kernel
189
- - [ ] RFF chunked DSYRK kernel
186
+ - [ ] Fourier-basis sampler in C++ with OpenMP parallelization
187
+ - [x] RFF kernel
188
+ - [x] RFF gradient kernel
189
+ - [x] RFF chunked DSYRK kernel
190
190
  - [ ] Optimized RFF gradient kernel for single inference/MD
191
191
  - The same as above, just for Hadamard features when I find the time?
192
192
  - GDML and sGDML kernels:
@@ -205,6 +205,7 @@ The goal is to remove pain-points of existing QML libraries
205
205
  - [x] Cholesky in-place solver
206
206
  - [x] L2-reg kwarg
207
207
  - [x] Toggle destructive vs non-destructive
208
+ - [x] RFP format in-place Cholesky solver
208
209
  - [ ] QR and/or SVD for non-square matrices
209
210
  - Add moleular descriptors with derivatives:
210
211
  - [ ] Coulomb matrix + misc variants without derivatives
@@ -0,0 +1,146 @@
1
+ """
2
+ Demo: Large symmetric Gaussian kernel using RFP format with ILP64 support
3
+
4
+ This example demonstrates:
5
+ 1. Computing a symmetric Gaussian kernel for n=50,000 samples
6
+ 2. Using RFP (Rectangular Full Packed) format to save 2× memory
7
+ 3. Solving the kernel ridge regression problem using RFP Cholesky
8
+ 4. Verifying correctness against full matrix for small n
9
+
10
+ With LP64 (32-bit BLAS integers):
11
+ - Maximum n ≈ 46,340 (before int32 overflow in BLAS calls)
12
+ - Full matrix would require n×n×8 bytes ≈ 19.1 GB for n=50,000
13
+
14
+ With ILP64 (64-bit BLAS integers):
15
+ - No practical limit on n (until memory exhausted)
16
+ - RFP format requires only n*(n+1)/2 × 8 bytes ≈ 9.5 GB for n=50,000
17
+ - Saves 50% memory compared to full symmetric matrix storage
18
+
19
+ To build with ILP64 on Linux:
20
+ make install-linux-ilp64
21
+ # Requires: sudo apt install libopenblas64-dev libopenblas64-pthread-dev
22
+
23
+ To build with ILP64 on macOS:
24
+ make install-macos-ilp64
25
+ """
26
+
27
+ import numpy as np
28
+ import kernelforge.global_kernels as gk
29
+ import kernelforge.kernelmath as km
30
+
31
+
32
+ def verify_rfp_correctness(n=1000, d=128, sigma=1.0):
33
+ """Verify that RFP kernel matches full matrix for small n"""
34
+ print(f"\n=== Correctness check (n={n}, d={d}) ===")
35
+
36
+ # Random data
37
+ rng = np.random.default_rng(42)
38
+ X = rng.standard_normal((n, d))
39
+ # Note: alpha must be negative for a valid Gaussian kernel: exp(-||x-y||^2 / (2*sigma^2))
40
+ alpha_param = -1.0 / (2.0 * sigma**2)
41
+
42
+ # Compute kernel in both formats
43
+ K_full = gk.kernel_gaussian_symm(X, alpha_param) # (n, n) dense lower triangle
44
+ K_rfp = gk.kernel_gaussian_symm_rfp(X, alpha_param) # 1D RFP packed
45
+
46
+ # Convert RFP back to full to compare
47
+ # Note: kernel writes in Fortran UPLO='U' (upper triangle), so Python API uses uplo='L'
48
+ # due to the swap_uplo trick in math_bindings.cpp
49
+ K_from_rfp = km.rfp_to_full(K_rfp, n, uplo="L", transr="N")
50
+
51
+ # Build reference: mirror lower triangle to upper for full symmetric matrix
52
+ K_ref = np.tril(K_full) + np.tril(K_full, -1).T
53
+
54
+ # Compare full symmetric matrices
55
+ diff = np.abs(K_from_rfp - K_ref).max()
56
+ print(f"Max absolute difference: {diff:.2e}")
57
+
58
+ if diff < 1e-12:
59
+ print("✓ RFP kernel matches full matrix!")
60
+ else:
61
+ print(f"✗ Warning: difference {diff:.2e} exceeds tolerance 1e-12")
62
+
63
+ return diff < 1e-12
64
+
65
+
66
+ def demo_large_kernel_rfp(n=50_000, d=128, sigma=1.0, regularize=1e-6):
67
+ """Demo: large kernel matrix using RFP format"""
68
+ print(f"\n=== Large kernel demo (n={n:,}, d={d}) ===")
69
+
70
+ # Memory estimates
71
+ mem_full = n * n * 8 / (1024**3) # GB for full matrix
72
+ mem_rfp = n * (n + 1) // 2 * 8 / (1024**3) # GB for RFP
73
+ print(f"Memory required:")
74
+ print(f" Full matrix (n×n): {mem_full:.2f} GB")
75
+ print(f" RFP format (n×(n+1)/2): {mem_rfp:.2f} GB")
76
+ print(
77
+ f" Savings: {mem_full - mem_rfp:.2f} GB ({100 * (mem_full - mem_rfp) / mem_full:.1f}%)"
78
+ )
79
+
80
+ # Generate random data
81
+ print(f"\nGenerating random data...")
82
+ rng = np.random.default_rng(42)
83
+ X = rng.standard_normal((n, d)).astype(np.float64)
84
+ y = rng.standard_normal(n).astype(np.float64)
85
+
86
+ # Compute kernel directly into RFP format
87
+ print(f"Computing symmetric Gaussian kernel in RFP format...")
88
+ # Note: alpha must be negative for valid Gaussian kernel: exp(-||x-y||^2 / (2*sigma^2))
89
+ alpha_param = -1.0 / (2.0 * sigma**2)
90
+ K_rfp = gk.kernel_gaussian_symm_rfp(X, alpha_param)
91
+
92
+ print(f" K_rfp shape: {K_rfp.shape}")
93
+ print(f" K_rfp dtype: {K_rfp.dtype}")
94
+ print(f" Expected length: {n * (n + 1) // 2}")
95
+ assert K_rfp.shape[0] == n * (n + 1) // 2
96
+
97
+ # Solve kernel ridge regression using RFP Cholesky
98
+ # cho_solve_rfp overwrites K_rfp, so pass a copy to preserve the original
99
+ print(f"\nSolving Kα = y using RFP Cholesky (l2={regularize})...")
100
+ alpha_coeffs = km.cho_solve_rfp(K_rfp.copy(), y, l2=regularize)
101
+
102
+ print(f" Solution α shape: {alpha_coeffs.shape}")
103
+ print(f" α min/max: [{alpha_coeffs.min():.6f}, {alpha_coeffs.max():.6f}]")
104
+ print(f" α mean: {alpha_coeffs.mean():.6f}")
105
+ print(f" α std: {alpha_coeffs.std():.6f}")
106
+
107
+ # Basic sanity check: α should have reasonable magnitude
108
+ alpha_norm = np.linalg.norm(alpha_coeffs)
109
+ y_norm = np.linalg.norm(y)
110
+ print(f" ||α|| / ||y|| = {alpha_norm / y_norm:.6f}")
111
+
112
+ print("\n✓ Large RFP demo completed successfully!")
113
+ return K_rfp, alpha_coeffs
114
+
115
+
116
+ def main():
117
+ print("=" * 70)
118
+ print("Large Symmetric Kernel with RFP Format and ILP64 Support")
119
+ print("=" * 70)
120
+
121
+ # 1. Verify correctness for small n
122
+ is_correct = verify_rfp_correctness(n=1000, d=128, sigma=1.0)
123
+
124
+ if not is_correct:
125
+ print("\n⚠ Correctness check failed — stopping here.")
126
+ return
127
+
128
+ # 2. Run large demo (n=50,000)
129
+ # Note: For n=50,000, this requires:
130
+ # - ILP64 build (otherwise n > 46,340 will overflow int32)
131
+ # - ~10 GB RAM for RFP kernel matrix
132
+ # - Several minutes to compute on a modern CPU
133
+ try:
134
+ K_rfp, alpha = demo_large_kernel_rfp(n=50_000, d=128, sigma=1.0, regularize=1e-6)
135
+ print("\n" + "=" * 70)
136
+ print("All tests passed! ILP64 + RFP working correctly.")
137
+ print("=" * 70)
138
+ except MemoryError:
139
+ print("\n⚠ Out of memory — try reducing n or increasing available RAM")
140
+ except Exception as e:
141
+ print(f"\n✗ Error: {e}")
142
+ raise
143
+
144
+
145
+ if __name__ == "__main__":
146
+ main()