Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/magland/simple_ans
Browse files Browse the repository at this point in the history
  • Loading branch information
magland committed Feb 12, 2025
2 parents 0c8c3f1 + b4da01e commit b5fe8f0
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 64 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ jobs:

- name: Install package and test dependencies
run: |
python -m pip install pytest numpy
python -m pip install -e .
python -m pip install -e ".[test]"
- name: Run tests
run: python -m pytest tests/

- name: Install benchmark dependencies
run: |
python -m pip install matplotlib zstandard
python -m pip install ".[benchmark]"
- name: Run benchmarks
run: |
Expand Down
22 changes: 5 additions & 17 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
matrix:
# os: [ubuntu-latest, windows-latest, macos-latest]
os: [ubuntu-latest, macos-latest]
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
python-version: ['3.9', '3.10', '3.11', '3.12', '3.13']

steps:
- uses: actions/checkout@v4
Expand All @@ -23,9 +23,7 @@ jobs:

- name: Install test dependencies
run: |
python -m pip install pytest numpy
python -m pip install scikit-build-core pybind11 cmake
python -m pip install -e .
python -m pip install -e ".[test]"
- name: Run tests
run: python -m pytest tests/
Expand All @@ -49,25 +47,15 @@ jobs:

- name: Install build dependencies
run: |
python -m pip install cibuildwheel cmake
python -m pip install cibuildwheel
- name: Build wheels
run: python -m cibuildwheel --output-dir dist
env:
CIBW_BUILD: cp38-* cp39-* cp310-* cp311-* cp312-* cp313-* pp38-* pp39-* pp310-*
CIBW_ARCHS_LINUX: x86_64 i686
# CIBW_ARCHS_WINDOWS: AMD64 x86
CIBW_ARCHS_MACOS: x86_64 arm64
CIBW_MANYLINUX_X86_64_IMAGE: manylinux2014
CIBW_MANYLINUX_I686_IMAGE: manylinux2014
CIBW_BEFORE_BUILD: pip install scikit-build-core pybind11 cmake

- name: Build sdist
run: |
pip install setuptools
python setup.py sdist --formats=gztar
env:
CIBW_BEFORE_BUILD: pip install pybind11 cmake
pip install build
python -m build -s -o dist
- name: Upload wheels as artifacts
uses: actions/upload-artifact@v3
Expand Down
23 changes: 5 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
cmake_minimum_required(VERSION 3.14)
project(simple_ans VERSION 0.1.0 LANGUAGES CXX)
cmake_minimum_required(VERSION 3.15)
project(simple_ans LANGUAGES CXX)

set(CMAKE_CXX_RELEASE_FLAGS "-O3 -march=native")
set(ARCH_FLAGS "-march=native" CACHE STRING "Architecture flags for the compiler")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 ${ARCH_FLAGS}")

include(CheckIPOSupported)
check_ipo_supported(RESULT ipo_supported OUTPUT ipo_error)
Expand Down Expand Up @@ -61,18 +62,4 @@ endif()


# Install rules
include(GNUInstallDirs)
install(TARGETS _simple_ans
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
install(FILES ${HEADERS}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/simple_ans
)

# For editable installs, copy the extension module to the source directory
add_custom_command(TARGET _simple_ans POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:_simple_ans> ${CMAKE_SOURCE_DIR}/simple_ans/
COMMENT "Copying extension module to source directory for editable install"
)
install(TARGETS _simple_ans LIBRARY DESTINATION simple_ans)
36 changes: 26 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,25 @@ While there are certainly many ANS implementations that are parts of other packa

## Installation

First, install the required dependencies:
simple_ans is available on PyPI:

```bash
pip install pybind11 numpy
```console
pip install simple-ans
```

Then install the package:
Developers may want to clone the repository and do an editable install:

```bash
pip install .
```console
git clone https://github.com/flatironinstitute/simple_ans.git
cd simple_ans
pip install -e .
```

Or install from source:
For developers who want automatic rebuilds of the compiled extension:

```bash
cd simple_ans
pip install -e .
```console
pip install "scikit-build-core>=0.5.0" "pybind11>=2.11.1" "pip>=24" ninja
pip install -e . -Ceditable.rebuild=true --no-build-isolation
```

## Usage
Expand Down Expand Up @@ -64,6 +66,14 @@ compression_ratio = original_size / compressed_size
print(f"Compression ratio: {compression_ratio:.2f}x")
```

## Tests
To run the tests, install with the `test` extra and run `pytest`:

```console
pip install "simple-ans[test]"
pytest tests/
```

## Simple benchmark

You can run a very simple benchmark that compares simple_ans with `zlib`, `zstandard`, and `lzma` at various compression levels for a toy dataset of quantized Gaussian noise. See [devel/benchmark.py](./devel/benchmark.py) and [devel/benchmark_ans_only.py](./devel/benchmark_ans_only.py).
Expand All @@ -74,6 +84,12 @@ The benchmark.py also runs in a CI environment and produces the following graph:

We see that for this example, the ANS-based compression ratio is higher than the other methods, almost reaching the theoretical ideal. The encode rate in MB/s is also fastest for simple_ans. The decode rate is faster than Zlib but slower than Zstandard. I think in principle, we should be able to speed up the decoding. Let me know if you have ideas for this.

To install the benchmark dependencies, use:

```console
pip install .[benchmark]
```

## Extended benchmarks

A more comprehensive benchmark ([devel/benchmark2.py](./devel/benchmark2.py)) tests the compression performance across different types of distributions:
Expand Down
29 changes: 22 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,43 @@ classifiers = [
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Topic :: Scientific/Engineering",
]
dependencies = [
"numpy>=1.21.0",
]

[project.optional-dependencies]
test = ["pytest"]
benchmark = ["matplotlib", "zstandard"]

[tool.scikit-build]
cmake.minimum-version = "3.15"
cmake.version = ">=3.15"
cmake.args = []
cmake.verbose = true
build.verbose = true
sdist.include = ["simple_ans/cpp/*"]
wheel.packages = ["simple_ans"]
build-dir = "build"
editable.mode = "redirect"

[tool.setuptools]
packages = ["simple_ans"]

[tool.scikit-build.cmake.define]
SKBUILD = "ON"
CMAKE_POSITION_INDEPENDENT_CODE = "ON"

[tool.cibuildwheel]
build-frontend = "build"
build = "cp39-* cp310-* cp311-* cp312-* cp313-* pp39-* pp310-*"
manylinux-x86_64-image = "manylinux2014"
test-command = "python -m pytest {project}/tests"
test-extras = ["test"]
config-settings = "cmake.define.ARCH_FLAGS=''"

[tool.cibuildwheel.linux]
archs = ["x86_64"]

[tool.cibuildwheel.macos]
archs = ["x86_64", "arm64"]
9 changes: 0 additions & 9 deletions setup.py

This file was deleted.

0 comments on commit b5fe8f0

Please sign in to comment.