Skip to content

Commit

Permalink
fixing cpp names camelCase + adding wheel actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Natooz committed Mar 12, 2024
1 parent fa39c09 commit fc969a8
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 12 deletions.
141 changes: 141 additions & 0 deletions .github/workflows/publish_pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
name: Build wheel

on:
workflow_dispatch:
release:
types:
- published

permissions:
contents: read

jobs:
build_wheels:
name: Build wheel for ${{ matrix.python }}-${{ matrix.buildplat[1] }}
runs-on: ${{ matrix.buildplat[0] }}
strategy:
matrix:
# From wheel.yml of numpy (https://github.com/numpy/numpy/blob/main/.github/workflows/wheels.yml):
# Github Actions doesn't support pairing matrix values together, let's improvise
# https://github.com/github/feedback/discussions/7835#discussioncomment-1769026
buildplat:
- [ubuntu-22.04, manylinux_x86_64]
- [ubuntu-22.04, musllinux_x86_64]
- [ubuntu-22.04, manylinux_aarch64]
- [ubuntu-22.04, musllinux_aarch64]
- [macos-12, macosx_x86_64]
- [macos-12, macosx_arm64]
- [windows-2022, win_amd64]
- [windows-2022, win32]
python: ["cp38", "cp39", "cp310", "cp311", "cp312", "pp39"]
exclude:
- buildplat: [windows-2022, win32]
python: "pp39"
- buildplat: [ ubuntu-22.04, musllinux_x86_64 ]
python: "pp39"
- buildplat: [ ubuntu-22.04, musllinux_aarch64 ]
python: "pp39"
- buildplat: [macos-12, macosx_arm64]
python: "pp39"

steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive

- name: Set up QEMU
if: ${{ contains(matrix.buildplat[1], 'linux_aarch64') }}
uses: docker/setup-qemu-action@v3
with:
platforms: all

- name: Setup MSVC (32-bit)
if: ${{ matrix.buildplat[1] == 'win32' }}
uses: bus1/cabuild/action/msdevshell@e22aba57d6e74891d059d66501b6b5aed8123c4d # v1
with:
architecture: 'x86'

- name: Build wheels for manylinux x86_64
if: ${{ matrix.buildplat[1] == 'manylinux_x86_64' }}
uses: pypa/cibuildwheel@ce3fb7832089eb3e723a0a99cab7f3eaccf074fd # v2.16.5
env:
CIBW_PRERELEASE_PYTHONS: True
CIBW_BUILD: ${{ matrix.python }}-${{ matrix.buildplat[1] }}
CIBW_MANYLINUX_X86_64_IMAGE: quay.io/pypa/manylinux_2_28_x86_64
CIBW_ARCHS: 'all'

- name: Build wheels for manylinux aarch64
if: ${{ matrix.buildplat[1] == 'manylinux_aarch64' }}
uses: pypa/cibuildwheel@ce3fb7832089eb3e723a0a99cab7f3eaccf074fd # v2.16.5
env:
CIBW_PRERELEASE_PYTHONS: True
CIBW_BUILD: ${{ matrix.python }}-${{ matrix.buildplat[1] }}
CIBW_MANYLINUX_AARCH64_IMAGE: quay.io/pypa/manylinux_2_28_aarch64
CIBW_ARCHS: 'all'

- name: Build wheels for musllinux x86_64
if: ${{ matrix.buildplat[1] == 'musllinux_x86_64' }}
uses: pypa/cibuildwheel@ce3fb7832089eb3e723a0a99cab7f3eaccf074fd # v2.16.5
env:
CIBW_PRERELEASE_PYTHONS: True
CIBW_BUILD: ${{ matrix.python }}-${{ matrix.buildplat[1] }}
CIBW_MUSLLINUX_X86_64_IMAGE: quay.io/pypa/musllinux_1_2_x86_64
CIBW_ARCHS: 'all'

- name: Build wheels for musllinux aarch64
if: ${{ matrix.buildplat[1] == 'musllinux_aarch64' }}
uses: pypa/cibuildwheel@ce3fb7832089eb3e723a0a99cab7f3eaccf074fd # v2.16.5
env:
CIBW_PRERELEASE_PYTHONS: True
CIBW_BUILD: ${{ matrix.python }}-${{ matrix.buildplat[1] }}
CIBW_MUSLLINUX_AARCH64_IMAGE: quay.io/pypa/musllinux_1_2_aarch64
CIBW_ARCHS: 'all'

- name: Build wheels
if: ${{ !contains(matrix.buildplat[1], 'linux') }}
uses: pypa/cibuildwheel@ce3fb7832089eb3e723a0a99cab7f3eaccf074fd # v2.16.5
env:
CIBW_PRERELEASE_PYTHONS: True
CIBW_BUILD: ${{ matrix.python }}-${{ matrix.buildplat[1] }}
CIBW_ARCHS: 'all'

- uses: actions/upload-artifact@v3
with:
path: ./wheelhouse/*.whl

build_sdist:
name: Build source distribution
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive

- name: Build sdist
run: pipx run build --sdist

- uses: actions/upload-artifact@v3
with:
path: dist/*.tar.gz

upload_pypi:
needs: [build_wheels, build_sdist]
runs-on: ubuntu-latest
environment:
name: pypi
url: https://pypi.org/p/lcsvec
permissions:
id-token: write
steps:
- uses: actions/download-artifact@v3
with:
# unpacks default artifact into dist/
# if `name: artifact` is omitted, the action will create extra parent dir
name: artifact
path: dist

- uses: pypa/gh-action-pypi-publish@release/v1
#with:
# To test: repository-url: https://test.pypi.org/legacy/
120 changes: 120 additions & 0 deletions .github/workflows/test_wheel.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
name: Test build wheel

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

jobs:
build_wheels:
name: Build wheel for ${{ matrix.python }}-${{ matrix.buildplat[1] }}
runs-on: ${{ matrix.buildplat[0] }}
strategy:
matrix:
# From wheel.yml of numpy (https://github.com/numpy/numpy/blob/main/.github/workflows/wheels.yml):
# Github Actions doesn't support pairing matrix values together, let's improvise
# https://github.com/github/feedback/discussions/7835#discussioncomment-1769026
buildplat:
- [ubuntu-22.04, manylinux_x86_64]
- [ubuntu-22.04, musllinux_x86_64]
- [ubuntu-22.04, manylinux_aarch64]
- [ubuntu-22.04, musllinux_aarch64]
- [macos-12, macosx_x86_64]
- [macos-12, macosx_arm64]
- [windows-2022, win_amd64]
- [windows-2022, win32]
python: ["cp38", "cp39", "cp310", "cp311", "cp312", "pp39"]
exclude:
- buildplat: [windows-2022, win32]
python: "pp39"
- buildplat: [ ubuntu-22.04, musllinux_x86_64 ]
python: "pp39"
- buildplat: [ ubuntu-22.04, musllinux_aarch64 ]
python: "pp39"
- buildplat: [macos-12, macosx_arm64]
python: "pp39"

steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive

- name: Set up QEMU
if: ${{ contains(matrix.buildplat[1], 'linux_aarch64') }}
uses: docker/setup-qemu-action@v3
with:
platforms: all

- name: Setup MSVC (32-bit)
if: ${{ matrix.buildplat[1] == 'win32' }}
uses: bus1/cabuild/action/msdevshell@e22aba57d6e74891d059d66501b6b5aed8123c4d # v1
with:
architecture: 'x86'

- name: Build wheels for manylinux x86_64
if: ${{ matrix.buildplat[1] == 'manylinux_x86_64' }}
uses: pypa/cibuildwheel@ce3fb7832089eb3e723a0a99cab7f3eaccf074fd # v2.16.5
env:
CIBW_PRERELEASE_PYTHONS: True
CIBW_BUILD: ${{ matrix.python }}-${{ matrix.buildplat[1] }}
CIBW_MANYLINUX_X86_64_IMAGE: quay.io/pypa/manylinux_2_28_x86_64
CIBW_ARCHS: 'all'

- name: Build wheels for manylinux aarch64
if: ${{ matrix.buildplat[1] == 'manylinux_aarch64' }}
uses: pypa/cibuildwheel@ce3fb7832089eb3e723a0a99cab7f3eaccf074fd # v2.16.5
env:
CIBW_PRERELEASE_PYTHONS: True
CIBW_BUILD: ${{ matrix.python }}-${{ matrix.buildplat[1] }}
CIBW_MANYLINUX_AARCH64_IMAGE: quay.io/pypa/manylinux_2_28_aarch64
CIBW_ARCHS: 'all'

- name: Build wheels for musllinux x86_64
if: ${{ matrix.buildplat[1] == 'musllinux_x86_64' }}
uses: pypa/cibuildwheel@ce3fb7832089eb3e723a0a99cab7f3eaccf074fd # v2.16.5
env:
CIBW_PRERELEASE_PYTHONS: True
CIBW_BUILD: ${{ matrix.python }}-${{ matrix.buildplat[1] }}
CIBW_MUSLLINUX_X86_64_IMAGE: quay.io/pypa/musllinux_1_2_x86_64
CIBW_ARCHS: 'all'

- name: Build wheels for musllinux aarch64
if: ${{ matrix.buildplat[1] == 'musllinux_aarch64' }}
uses: pypa/cibuildwheel@ce3fb7832089eb3e723a0a99cab7f3eaccf074fd # v2.16.5
env:
CIBW_PRERELEASE_PYTHONS: True
CIBW_BUILD: ${{ matrix.python }}-${{ matrix.buildplat[1] }}
CIBW_MUSLLINUX_AARCH64_IMAGE: quay.io/pypa/musllinux_1_2_aarch64
CIBW_ARCHS: 'all'

- name: Build wheels
if: ${{ !contains(matrix.buildplat[1], 'linux') }}
uses: pypa/cibuildwheel@ce3fb7832089eb3e723a0a99cab7f3eaccf074fd # v2.16.5
env:
CIBW_PRERELEASE_PYTHONS: True
CIBW_BUILD: ${{ matrix.python }}-${{ matrix.buildplat[1] }}
CIBW_ARCHS: 'all'

- uses: actions/upload-artifact@v4
with:
name: ${{ matrix.python }}-${{ matrix.buildplat[1] }}
path: ./wheelhouse/*.whl

build_sdist:
name: Build source distribution
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive

- name: Build sdist
run: pipx run build --sdist

- uses: actions/upload-artifact@v3
with:
path: dist/*.tar.gz
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ You can install the package with pip: `pip install lcsvec`.
Here we will use numpy, the usage for other deep learning libraries is the same.

```Python
from lcsvec import lcs, lcs_length, lccs_length
from lcsvec import lccs, lccs_length, lcs, lcs_length
import numpy as np

seq1 = np.arange(0, 12)
Expand All @@ -30,7 +30,8 @@ seq2 = np.array([8, 0, 1, 2, 8, 2, 3, 4, 5, 6], dtype=np.int64)
lcs_ = lcs(seq1, seq2) # [0, 1, 2, 3, 4, 5, 6]
lcs_len = lcs_length(seq1, seq2) # 7, more efficient than calling len(lcs(seq1, seq2))

lccs_len = lccs_length(seq1, seq2) # 5, [2, 3, 4, 5, 6]
lccs = lccs(seq1, seq2) # [2, 3, 4, 5, 6]
lccs_len = lccs_length(seq1, seq2) # 5, more efficient than calling len(lccs(seq1, seq2))
```

## TODOs
Expand Down
18 changes: 9 additions & 9 deletions src/cpu/lccs_cpu_dyn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using namespace nb::literals;


// Returns the length of the longest common subsequence and the idx of its end in s1
std::vector<int> lccs_length_idx(
std::vector<int> lccsLengthIdx(
const nb::ndarray<double, nb::ndim<1>>& s1,
const nb::ndarray<double, nb::ndim<1>>& s2
) {
Expand All @@ -33,18 +33,18 @@ std::vector<int> lccs_length_idx(
}
}
}
std::vector<int> lccs_len_idx = {max_length, imax};
return lccs_len_idx;
std::vector<int> lccsLenIdx = {max_length, imax};
return lccsLenIdx;
}


// Calculating the length of the longest common contiguous subsequence with dynamic programming
int lccs_length(
int lccsLength(
const nb::ndarray<double, nb::ndim<1>>& s1,
const nb::ndarray<double, nb::ndim<1>>& s2
) {
std::vector<int> lccs_len_idx = lccs_length_idx(s1, s2);
return lccs_len_idx[0];
std::vector<int> lccsLenIdx = lccsLengthIdx(s1, s2);
return lccsLenIdx[0];
}


Expand All @@ -53,12 +53,12 @@ std::vector<int> lccs(
const nb::ndarray<double, nb::ndim<1>>& s1,
const nb::ndarray<double, nb::ndim<1>>& s2
) {
std::vector<int> lccs_len_idx = lccs_length_idx(s1, s2);
std::vector<int> lccsLenIdx = lccsLengthIdx(s1, s2);

// Extract the longest common substring from s1
std::vector<int> longestSubseq(lccs_len_idx[0]);
std::vector<int> longestSubseq(lccsLenIdx[0]);
int idx = 0;
for (int i = lccs_len_idx[1] - lccs_len_idx[0] + 1; i <= lccs_len_idx[1]; ++i)
for (int i = lccsLenIdx[1] - lccsLenIdx[0] + 1; i <= lccsLenIdx[1]; ++i)
longestSubseq[idx++] = s1(i);

return longestSubseq;
Expand Down
2 changes: 1 addition & 1 deletion src/lcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ NB_MODULE(lcsvec_ext, m) {

m.def("lccs", &lccs, "seq1"_a, "seq2"_a,
"Returns the longest common contiguous subsequence (lccs) from `seq1` and `seq2`.");
m.def("lccs_length", &lccs_length, "seq1"_a, "seq2"_a,
m.def("lccs_length", &lccsLength, "seq1"_a, "seq2"_a,
"Returns the length of the longest common contiguous subsequence (lccs) from `seq1` and `seq2`.");
}

0 comments on commit fc969a8

Please sign in to comment.