-
Notifications
You must be signed in to change notification settings - Fork 0
New Python package: gputils_api #61
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
Merged
Merged
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
fff9111
python API gputils_api
alphaville b99d62b
CI: update ci.yml; add python3.12
alphaville 7198e86
ci script: python and venv
alphaville 313d96d
trying with python3.12.4
alphaville c451e51
try without python
alphaville b686748
unit test file
alphaville 94f7305
test: set up class
alphaville b19a974
test: set up class
alphaville fdd68eb
first unit test: test_read_eye_d
alphaville 35e58dd
first unit test: test_read_eye_d
alphaville 28e1fab
unit tests
alphaville b08024a
unit tests
alphaville 1a6cdd0
unit tests
alphaville f8c7493
unit tests
alphaville dfe3895
unit tests
alphaville 4591646
unit tests
alphaville d45cb74
unit tests
alphaville ed8c438
better testing
alphaville 6838ddf
better testing
alphaville d6635ff
squashed
8260f38
final touch
alphaville 07d4b64
Merge pull request #66 from GPUEngineering/b/cheeky-bugs
ruairimoran ea33238
update CHANGELOG and VERSION
alphaville 036ba13
Merge branch 'feature/60-python-api' of https://github.com/GPUEnginee…
alphaville File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,6 @@ jobs: | |
steps: | ||
- name: checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: run test script | ||
run: bash ./ci/script.sh | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
## GPUtils API | ||
|
||
### Installation | ||
|
||
As simple as... | ||
```bash | ||
pip install gputils-api | ||
``` | ||
of course, preferably from within a virtual environment. | ||
|
||
### Write to file | ||
|
||
```python | ||
import numpy as np | ||
import gputils_api as g | ||
a = np.eye(3) | ||
g.write_array_to_gputils_binary_file(a, 'my_data.bt') | ||
``` | ||
|
||
### Read from file | ||
|
||
```python | ||
import numpy as np | ||
import gputils_api as g | ||
x = g.read_array_from_gputils_binary_file('my_data.bt') | ||
``` | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1.7.0rc4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .gputils_api import * |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import numpy as np | ||
|
||
def read_array_from_gputils_binary_file(path, dt=np.dtype('d')): | ||
""" | ||
Reads an array from a bt file | ||
:param path: path to file | ||
:param dt: numpy-compatible data type | ||
:raises ValueError: if the file name specified `path` does not have the .bt extension | ||
""" | ||
if not path.endswith(".bt"): | ||
raise ValueError("The file must have the .bt extension") | ||
with open(path, 'rb') as f: | ||
nr = int.from_bytes(f.read(8), byteorder='little', signed=False) # read number of rows | ||
nc = int.from_bytes(f.read(8), byteorder='little', signed=False) # read number of columns | ||
nm = int.from_bytes(f.read(8), byteorder='little', signed=False) # read number of matrices | ||
dat = np.fromfile(f, dtype=np.dtype(dt)) # read data | ||
dat = dat.reshape((nr, nc, nm)) # reshape | ||
return dat | ||
|
||
|
||
def write_array_to_gputils_binary_file(x, path): | ||
""" | ||
Writes a numpy array into a bt file | ||
|
||
:param x: numpy array to save to file | ||
:param path: path to file | ||
:raises ValueError: if `x` has more than 3 dimensions | ||
:raises ValueError: if the file name specified `path` does not have the .bt extension | ||
""" | ||
if not path.endswith(".bt"): | ||
raise ValueError("The file must have the .bt extension") | ||
x_shape = x.shape | ||
x_dims = len(x_shape) | ||
if x_dims >= 4: | ||
raise ValueError("given array cannot have more than 3 dimensions") | ||
nr = x_shape[0] | ||
nc = x_shape[1] if x_dims >= 2 else 1 | ||
nm = x_shape[2] if x_dims == 3 else 1 | ||
with open(path, 'wb') as f: | ||
f.write(nr.to_bytes(8, 'little')) # write number of rows | ||
f.write(nc.to_bytes(8, 'little')) # write number of columns | ||
f.write(nm.to_bytes(8, 'little')) # write number of matrices | ||
x.reshape(nr*nc*nm, 1).tofile(f) # write data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/usr/bin/env python | ||
|
||
from setuptools import setup, find_packages | ||
import io | ||
import os | ||
|
||
here = os.path.abspath(os.path.dirname(__file__)) | ||
|
||
NAME = 'gputils_api' | ||
|
||
# Import version from file | ||
version_file = open(os.path.join(here, 'VERSION')) | ||
VERSION = version_file.read().strip() | ||
|
||
DESCRIPTION = 'Python API for GPUtils' | ||
|
||
|
||
# Import the README and use it as the long-description. | ||
# Note: this will only work if 'README.md' is present in your MANIFEST.in file! | ||
try: | ||
with io.open(os.path.join(here, 'README.md'), encoding='utf-8') as f: | ||
long_description = '\n' + f.read() | ||
except FileNotFoundError: | ||
long_description = DESCRIPTION | ||
|
||
setup(name=NAME, | ||
version=VERSION, | ||
description=DESCRIPTION, | ||
long_description=long_description, | ||
long_description_content_type='text/markdown', | ||
author=['Pantelis Sopasakis', 'Ruairi Moran'], | ||
author_email='p.sopasakis@gmail.com', | ||
license='GNU General Public License v3 (GPLv3)', | ||
packages=find_packages( | ||
exclude=["private"]), | ||
include_package_data=True, | ||
install_requires=[ | ||
'numpy', 'setuptools' | ||
], | ||
classifiers=[ | ||
'Development Status :: 2 - Pre-Alpha ', | ||
'License :: OSI Approved :: GNU General Public License v3 (GPLv3)', | ||
'Programming Language :: Python', | ||
'Environment :: GPU :: NVIDIA CUDA', | ||
'Intended Audience :: Developers', | ||
'Topic :: Software Development :: Libraries' | ||
], | ||
keywords=['api', 'GPU'], | ||
url=( | ||
'https://github.com/GPUEngineering/GPUtils' | ||
), | ||
zip_safe=False) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import os | ||
import unittest | ||
import numpy as np | ||
import gputils_api as gpuapi | ||
|
||
|
||
class GputilApiTestCase(unittest.TestCase): | ||
|
||
@staticmethod | ||
def local_abs_path(): | ||
cwd = os.getcwd() | ||
return cwd.split('open-codegen')[0] | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
n = 5 | ||
base_dir = GputilApiTestCase.local_abs_path() | ||
eye_d = np.eye(n, dtype=np.dtype('d')) | ||
gpuapi.write_array_to_gputils_binary_file(eye_d, os.path.join(base_dir, 'eye_d.bt')) | ||
|
||
eye_f = np.eye(n, dtype=np.dtype('f')) | ||
gpuapi.write_array_to_gputils_binary_file(eye_f, os.path.join(base_dir, 'eye_f.bt')) | ||
|
||
xd = np.random.randn(2, 4, 6).astype('d') | ||
xd[1, 2, 3] = -12.3 | ||
gpuapi.write_array_to_gputils_binary_file(xd, os.path.join(base_dir, 'rand_246_d.bt')) | ||
|
||
xf = np.random.randn(2, 4, 6).astype('f') | ||
xf[1, 2, 3] = float(-12.3) | ||
gpuapi.write_array_to_gputils_binary_file(xf, os.path.join(base_dir, 'rand_246_f.bt')) | ||
|
||
a = np.linspace(-100, 100, 4*5).reshape((4,5)).astype('d') | ||
gpuapi.write_array_to_gputils_binary_file(a, os.path.join(base_dir, 'a_d.bt')) | ||
|
||
b = np.array([ | ||
[[1, 2], [3, 4], [5, 6]], | ||
[[7, 8], [9, 10], [11, 12]] | ||
], dtype=np.dtype('d')) | ||
gpuapi.write_array_to_gputils_binary_file(b, os.path.join(base_dir, 'b_d.bt')) | ||
|
||
def __test_read_eye(self, dt): | ||
base_dir = GputilApiTestCase.local_abs_path() | ||
path = os.path.join(base_dir, f'eye_{dt}.bt') | ||
r = gpuapi.read_array_from_gputils_binary_file(path, dt=np.dtype(dt)) | ||
err = r[:, :, 0] - np.eye(5) | ||
err_norm = np.linalg.norm(err, np.inf) | ||
self.assertTrue(err_norm < 1e-12) | ||
|
||
def test_read_eye_d(self): | ||
self.__test_read_eye('d') | ||
|
||
def test_read_eye_f(self): | ||
self.__test_read_eye('f') | ||
|
||
def __test_read_rand(self, dt): | ||
base_dir = GputilApiTestCase.local_abs_path() | ||
path = os.path.join(base_dir, f'rand_246_{dt}.bt') | ||
r = gpuapi.read_array_from_gputils_binary_file(path, dt=np.dtype(dt)) | ||
r_shape = r.shape | ||
self.assertEqual(2, r_shape[0]) | ||
self.assertEqual(4, r_shape[1]) | ||
self.assertEqual(6, r_shape[2]) | ||
e = np.abs(r[1, 2, 3]+12.3) | ||
self.assertTrue(e < 1e-6) | ||
|
||
def test_read_rand_d(self): | ||
self.__test_read_rand('d') | ||
|
||
def test_read_rand_f(self): | ||
self.__test_read_rand('f') | ||
|
||
def test_read_a_tensor_3d(self): | ||
self.__test_read_rand('f') | ||
base_dir = GputilApiTestCase.local_abs_path() | ||
path = os.path.join(base_dir, 'b_d.bt') | ||
b = gpuapi.read_array_from_gputils_binary_file(path) | ||
self.assertEqual(b.shape, (2, 3, 2)) | ||
self.assertAlmostEqual(11., b[1, 2, 0]) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.