Skip to content

Commit 32159c4

Browse files
committed
update first working verison
1 parent 6e13cc2 commit 32159c4

File tree

11 files changed

+477
-56
lines changed

11 files changed

+477
-56
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,4 @@ cython_debug/
157157
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158158
# and can be added to the global gitignore or merged into this file. For a more nuclear
159159
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160-
#.idea/
160+
#.idea/

.pre-commit-config.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.0.1
4+
hooks:
5+
- id: trailing-whitespace
6+
- id: end-of-file-fixer
7+
- id: check-yaml
8+
- id: check-added-large-files
9+
10+
- repo: https://github.com/psf/black
11+
rev: 22.10.0
12+
hooks:
13+
- id: black
14+
15+
- repo: https://github.com/pycqa/flake8
16+
rev: 3.9.2
17+
hooks:
18+
- id: flake8

CMakeLists.txt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@ add_subdirectory(vendor/pybind11)
2222
#add_subdirectory(vendor/gemma.cpp)
2323

2424
# Create the Python module
25-
pybind11_add_module(gemma_cpp src/pygemma.cpp)
25+
pybind11_add_module(pygemma src/gemma_binding.cpp)
2626

27-
target_link_libraries(gemma_cpp PRIVATE libgemma hwy hwy_contrib sentencepiece)
27+
target_link_libraries(pygemma PRIVATE libgemma hwy hwy_contrib sentencepiece)
2828

2929
# Link against libgemma.a and any other necessary libraries
3030
# After linking, try to include the directories. Adjust this if necessary.
3131
FetchContent_GetProperties(gemma)
3232
FetchContent_GetProperties(sentencepiece)
33-
target_include_directories(gemma_cpp PRIVATE ${gemma_SOURCE_DIR})
34-
target_include_directories(gemma_cpp PRIVATE ${sentencepiece_SOURCE_DIR})
35-
36-
33+
target_include_directories(pygemma PRIVATE ${gemma_SOURCE_DIR})
34+
target_include_directories(pygemma PRIVATE ${sentencepiece_SOURCE_DIR})

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Nam D. Tran
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ pip install .
1515
Or install it
1616
```
1717
pip install gemma-cpp-python
18-
```
18+
```

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[build-system]
2-
requires = ["setuptools", "wheel", "pybind11"]
3-
build-backend = "setuptools.build_meta"
2+
requires = ["setuptools", "wheel", "cmake", "pybind11"]
3+
build-backend = "setuptools.build_meta"

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
pybind11
1+
pybind11
2+
pre-commit

setup.py

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,76 @@
1-
from setuptools import setup, Extension, find_packages
1+
import os
2+
import subprocess
3+
import sys
4+
from setuptools import setup, find_packages, Extension
25
from setuptools.command.build_ext import build_ext
3-
import pybind11
46

57

6-
class CustomBuildExt(build_ext):
7-
def build_extensions(self):
8-
# Customize the build process here if needed
9-
build_ext.build_extensions(self)
8+
class CMakeExtension(Extension):
9+
def __init__(self, name, sourcedir=""):
10+
Extension.__init__(self, name, sources=[])
11+
self.sourcedir = os.path.abspath(sourcedir)
1012

11-
# Include the pybind11 include directory
12-
pybind11_include_dir = pybind11.get_include()
1313

14-
ext_modules = [
15-
Extension(
16-
'gemma_cpp_extension', # Name of the module to import in Python
17-
['src/binding.cc', 'vendor/gemma.cpp/gemma.cc', 'vendor/gemma.cpp/run.cc'], # Source files
18-
include_dirs=[pybind11_include_dir, './src/include', './vendor/gemma.cpp/compression', './vendor/gemma.cpp/util'], # Include directories
19-
language='c++',
20-
extra_compile_args=['-std=c++11'], # Replace with your required C++ version
21-
),
22-
]
14+
class CMakeBuild(build_ext):
15+
def run(self):
16+
try:
17+
out = subprocess.check_output(["cmake", "--version"])
18+
except OSError:
19+
raise RuntimeError(
20+
"CMake must be installed to build the following extensions: "
21+
+ ", ".join(e.name for e in self.extensions)
22+
)
23+
24+
for ext in self.extensions:
25+
self.build_extension(ext)
26+
27+
def build_extension(self, ext):
28+
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
29+
cmake_args = [
30+
"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=" + extdir,
31+
"-DPYTHON_EXECUTABLE=" + sys.executable,
32+
]
33+
34+
cfg = "Debug" if self.debug else "Release"
35+
build_args = ["--config", cfg]
36+
37+
# Add a parallel build option
38+
build_args += [
39+
"--",
40+
"-j",
41+
"12",
42+
] # Specifies the number of jobs to run simultaneously
43+
44+
if not os.path.exists(self.build_temp):
45+
os.makedirs(self.build_temp)
46+
47+
subprocess.check_call(
48+
["cmake", ext.sourcedir] + cmake_args, cwd=self.build_temp
49+
)
50+
subprocess.check_call(
51+
["cmake", "--build", ".", "--target", ext.name] + build_args,
52+
cwd=self.build_temp,
53+
)
54+
2355

2456
setup(
25-
name='gemma-cpp-python',
26-
version='0.1.0',
27-
author='Nam Tran',
28-
author_email='trannam.ase@gmail.com',
29-
description='A Python wrapper for the GEMMA C++ library',
30-
long_description=open('README.md').read(),
31-
long_description_content_type='text/markdown',
57+
name="pygemma",
58+
version="0.1.0",
59+
author="Nam Tran",
60+
author_email="namtran.ase@gmail.com",
61+
description="A Python package with a C++ backend using gemma.",
62+
long_description="""
63+
This package provides Python bindings to a C++ library using pybind11.
64+
""",
65+
long_description_content_type="text/markdown",
66+
ext_modules=[CMakeExtension("pygemma")],
67+
cmdclass=dict(build_ext=CMakeBuild),
68+
zip_safe=False,
3269
packages=find_packages(),
33-
ext_modules=ext_modules,
34-
cmdclass={
35-
'build_ext': CustomBuildExt,
36-
},
37-
# Add all other necessary package metadata
38-
install_requires=[
39-
'pybind11>=2.6',
70+
classifiers=[
71+
"Programming Language :: Python :: 3",
72+
"License :: OSI Approved :: MIT License",
73+
"Operating System :: OS Independent",
4074
],
41-
)
75+
python_requires=">=3.8",
76+
)

src/binding.cc

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)