Skip to content

Commit ea02cf8

Browse files
committed
Fix Windows extension build by removing library rename and updated code in grpo.c
1 parent 193c6c0 commit ea02cf8

File tree

3 files changed

+54
-36
lines changed

3 files changed

+54
-36
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ jobs:
3636

3737
- name: Build Windows Extension
3838
if: runner.os == 'Windows'
39-
shell: cmd
4039
run: |
40+
# Optional: ensure the c_src directory exists
4141
if not exist "optimrl\c_src" mkdir "optimrl\c_src"
42+
43+
# Build the extension in-place
4244
python setup.py build_ext --inplace
43-
for /r "build" %%i in (libgrpo.dll) do copy "%%i" "optimrl\c_src\"
4445

4546
- name: Install package
4647
run: |

optimrl/c_src/grpo.c

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,35 @@
66
#include <Python.h>
77

88
// Example implementation of the PyInit_libgrpo function
9+
// PyMODINIT_FUNC PyInit_libgrpo(void) {
10+
// static struct PyModuleDef moduledef = {
11+
// PyModuleDef_HEAD_INIT,
12+
// "libgrpo", // Module name
13+
// "GRPO C extension", // Module docstring
14+
// -1, // Size of per-interpreter state of the module
15+
// NULL, // Module methods
16+
// };
17+
18+
// return PyModule_Create(&moduledef);
19+
// }
20+
// Extension initialization function must match the module name: PyInit_libgrpo
921
PyMODINIT_FUNC PyInit_libgrpo(void) {
10-
static struct PyModuleDef moduledef = {
22+
static PyModuleDef moduledef = {
1123
PyModuleDef_HEAD_INIT,
12-
"libgrpo", // Module name
13-
"GRPO C extension", // Module docstring
14-
-1, // Size of per-interpreter state of the module
15-
NULL, // Module methods
24+
"libgrpo", // name of this module
25+
NULL, // docstring
26+
-1, // size of per-interpreter state of the module
27+
NULL, // methods
28+
NULL, // slots
29+
NULL, // traverse
30+
NULL, // clear
31+
NULL // free
1632
};
33+
PyObject *m = PyModule_Create(&moduledef);
34+
if (m == NULL)
35+
return NULL;
1736

18-
return PyModule_Create(&moduledef);
37+
return m;
1938
}
2039

2140
// Helper function to compute robust statistics

setup.py

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,41 @@ def build_extensions(self):
4848

4949
def copy_extensions_to_package(self):
5050
"""Copy the built extension to the package directory."""
51+
# for ext in self.extensions:
52+
# # Get the full path of the built library
53+
# built_lib = self.get_ext_fullpath(ext.name)
54+
55+
# # Determine the destination directory within the package
56+
# dest_dir = os.path.join('optimrl', 'c_src')
57+
# os.makedirs(dest_dir, exist_ok=True)
58+
59+
# # Get the filename only
60+
# filename = os.path.basename(built_lib)
61+
62+
# # Create platform-specific library name
63+
# if platform.system() == 'Darwin':
64+
# lib_name = 'libgrpo.dylib'
65+
# elif platform.system() == 'Linux':
66+
# lib_name = 'libgrpo.so'
67+
# else:
68+
# lib_name = 'libgrpo.dll'
69+
70+
# # Copy the file to the package directory with the correct name
71+
# dest_path = os.path.join(dest_dir, lib_name)
72+
# shutil.copy2(built_lib, dest_path)
73+
# print(f"Copied {built_lib} to {dest_path}")
5174
for ext in self.extensions:
52-
# Get the full path of the built library
75+
# Get the full path of the built library
5376
built_lib = self.get_ext_fullpath(ext.name)
5477

5578
# Determine the destination directory within the package
5679
dest_dir = os.path.join('optimrl', 'c_src')
5780
os.makedirs(dest_dir, exist_ok=True)
5881

59-
# Get the filename only
82+
# Keep the existing file name (e.g., libgrpo.cp310-win_amd64.pyd)
6083
filename = os.path.basename(built_lib)
6184

62-
# Create platform-specific library name
63-
if platform.system() == 'Darwin':
64-
lib_name = 'libgrpo.dylib'
65-
elif platform.system() == 'Linux':
66-
lib_name = 'libgrpo.so'
67-
else:
68-
lib_name = 'libgrpo.dll'
69-
70-
# Copy the file to the package directory with the correct name
71-
dest_path = os.path.join(dest_dir, lib_name)
85+
dest_path = os.path.join(dest_dir, filename)
7286
shutil.copy2(built_lib, dest_path)
7387
print(f"Copied {built_lib} to {dest_path}")
7488

@@ -87,22 +101,6 @@ def finalize_options(self):
87101
self.plat_name = 'win_amd64' # Example for Windows
88102

89103

90-
# Define the extension module
91-
92-
# grpo_module = Extension(
93-
# 'optimrl.c_src.libgrpo',
94-
# sources=['optimrl/c_src/grpo.c'],
95-
# include_dirs=[
96-
# 'optimrl/c_src',
97-
# python_include_path
98-
# ],
99-
# library_dirs=[
100-
# python_lib_path # Include the Python library path dynamically
101-
# ],
102-
# libraries=['m'] if platform.system() != 'Windows' else [],
103-
# extra_compile_args=['-O3', '-fPIC'] if platform.system() != 'Windows' else ['/O2'],
104-
# extra_link_args=['-L' + python_lib_path] if platform.system() != 'Windows' else ['/EXPORT:PyInit_libgrpo']
105-
# )
106104
grpo_module = Extension(
107105
'optimrl.c_src.libgrpo',
108106
sources=['optimrl/c_src/grpo.c'],

0 commit comments

Comments
 (0)