Skip to content

Commit

Permalink
Use static libjpeg and libpng
Browse files Browse the repository at this point in the history
  • Loading branch information
cbalioglu committed Dec 10, 2023
1 parent f39a046 commit 82ebc37
Show file tree
Hide file tree
Showing 23 changed files with 339 additions and 112 deletions.
14 changes: 8 additions & 6 deletions .github/workflows/_build_wheel-linux.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
NllbTokenizer Copyright (c) Meta Platforms, Inc. and affiliates.
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
Expand Down Expand Up @@ -185,13 +185,15 @@ jobs:
fi
- name: Install fairseq2n
run: |
pip install --no-cache-dir ~/artifacts/fairseq2n/python/build/wheelhouse/*.whl
whl=$(ls ~/artifacts/fairseq2n/python/build/wheelhouse/*.whl)
pip install --no-cache-dir "fairseq2n@file://$whl"
- name: Install fairseq2
run: |
for whl in ~/artifacts/build/wheelhouse/*.whl; do
pip install --no-cache-dir $whl
pip install --no-cache-dir "fairseq['arrow']@$whl"
done
whl=$(ls ~/artifacts/build/wheelhouse/*.whl)
pip install --no-cache-dir\
"fairseq2@file://$whl" "fairseq2[arrow]@file://$whl"
- name: Set the sanitizer variables
if: inputs.sanitizers != 'nosan'
env:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/_build_wheel-macos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
HOMEBREW_NO_INSTALL_UPGRADE: 1
HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK: 1
run: |
brew install libjpeg libpng libsndfile python@${{ inputs.py }} || true
brew install libsndfile python@${{ inputs.py }} || true
- name: Create the Python virtual environment
run: |
/usr/local/bin/python${{ inputs.py }} -m venv ~/venv
Expand Down Expand Up @@ -100,7 +100,7 @@ jobs:
HOMEBREW_NO_INSTALL_UPGRADE: 1
HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK: 1
run: |
brew install libjpeg libpng libsndfile python@${{ inputs.py }} || true
brew install libsndfile python@${{ inputs.py }} || true
- name: Download wheels and native tests from staging
uses: actions/download-artifact@v3
with:
Expand Down
12 changes: 11 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
[submodule "third-party/pybind11"]
path = fairseq2n/third-party/pybind11
url = https://github.com/pybind/pybind11.git
ignore = dirty
[submodule "third-party/fmt"]
path = fairseq2n/third-party/fmt
url = https://github.com/fmtlib/fmt.git
ignore = dirty
[submodule "third-party/gtest"]
path = fairseq2n/third-party/gtest
url = https://github.com/google/googletest.git
ignore = dirty
[submodule "third-party/sentencepiece"]
path = fairseq2n/third-party/sentencepiece
url = https://github.com/google/sentencepiece.git
ignore = dirty
[submodule "third-party/zip"]
path = fairseq2n/third-party/zip
url = https://github.com/kuba--/zip.git
ignore = dirty
[submodule "third-party/kaldi-native-fbank"]
path = fairseq2n/third-party/kaldi-native-fbank
url = https://github.com/cbalioglu/kaldi-native-fbank.git
url = https://github.com/csukuangfj/kaldi-native-fbank.git
ignore = dirty
[submodule "fairseq2n/third-party/libpng"]
path = fairseq2n/third-party/libpng
url = https://github.com/glennrp/libpng.git
ignore = dirty
14 changes: 6 additions & 8 deletions INSTALL_FROM_SOURCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,20 @@ reusing an existing one to avoid dependency conflicts.
## 3. Install Dependencies

### 3.1 System Dependencies
fairseq2 depends on [libjpeg](https://libjpeg.sourceforge.net),
[libpng](http://www.libpng.org/pub/png/libpng.html), and
[libsndfile](https://github.com/libsndfile/libsndfile), which can be installed
via the system package manager on most Linux distributions, or via Homebrew on
macOS.
fairseq2 depends on [libsndfile](https://github.com/libsndfile/libsndfile),
which can be installed via the system package manager on most Linux
distributions, or via Homebrew on macOS.

For Ubuntu-based systems, run:

```sh
sudo apt install libjpeg8-dev libpng-dev libsndfile-dev
sudo apt install libsndfile-dev
```

Similarly, on Fedora, run:

```sh
sudo dnf install libjpeg-devel libpng-devel libsndfile-devel
sudo dnf install libsndfile-devel
```

For other Linux distributions, please consult its documentation on how to
Expand All @@ -77,7 +75,7 @@ install packages.
For macOS, you can use Homebrew:

```sh
brew install libjpeb libpng libsndfile
brew install libsndfile
```

### 3.2 PyTorch
Expand Down
20 changes: 8 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,18 @@ fairseq2 is also used by various external projects such as:
## Installing on Linux

### System Dependencies
fairseq2 depends on [libjpeg](https://libjpeg.sourceforge.net),
[libpng](http://www.libpng.org/pub/png/libpng.html), and
[libsndfile](https://github.com/libsndfile/libsndfile), which can be installed
via the system package manager on most Linux distributions. For Ubuntu-based
systems, run:
fairseq2 depends on [libsndfile](https://github.com/libsndfile/libsndfile),
which can be installed via the system package manager on most Linux
distributions. For Ubuntu-based systems, run:

```sh
sudo apt install libjpeg8 libpng16-16 libsndfile1
sudo apt install libsndfile1
```

Similarly, on Fedora, run:

```sh
sudo dnf install libjpeg libpng libsndfile
sudo dnf install libsndfile
```

For other Linux distributions, please consult its documentation on how to
Expand Down Expand Up @@ -140,13 +138,11 @@ pip install fairseq2\
## Installing on macOS

### System Dependencies
fairseq2 depends on [libjpeg](https://libjpeg.sourceforge.net),
[libpng](http://www.libpng.org/pub/png/libpng.html), and
[libsndfile](https://github.com/libsndfile/libsndfile), which can be installed
via Homebrew:
fairseq2 depends on [libsndfile](https://github.com/libsndfile/libsndfile),
which can be installed via Homebrew:

```sh
brew install libjpeg libpng libsndfile
brew install libsndfile
```

### pip
Expand Down
2 changes: 1 addition & 1 deletion ci/docker/manylinux_x86_64/Dockerfile.cpu
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ FROM quay.io/pypa/manylinux2014_x86_64

# Install system dependencies.
RUN yum --assumeyes install\
devtoolset-10-lib{asan,lsan,ubsan,tsan}-devel lib{sndfile,png,jpeg}-devel &&\
devtoolset-10-lib{asan,lsan,ubsan,tsan}-devel libsndfile-devel &&\
yum clean all

# Install Ninja.
Expand Down
14 changes: 7 additions & 7 deletions fairseq2n/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,6 @@ find_package(SndFile 1.0.25 REQUIRED)

find_package(Threads REQUIRED)

if(FAIRSEQ2N_SUPPORT_IMAGE)
find_package(JPEG REQUIRED)

find_package(PNG REQUIRED)
endif()

if(FAIRSEQ2N_THREAD_LIB STREQUAL "tbb")
find_package(TBB 2021.8 REQUIRED)
endif()
Expand Down Expand Up @@ -206,6 +200,12 @@ fairseq2n_add_sentencepiece()

fairseq2n_add_zip()

if(FAIRSEQ2N_SUPPORT_IMAGE)
fairseq2n_add_libjpeg_turbo()

fairseq2n_add_libpng()
endif()

if(FAIRSEQ2N_BUILD_PYTHON_BINDINGS)
fairseq2n_add_pybind11()
endif()
Expand Down Expand Up @@ -319,7 +319,7 @@ export(
add_subdirectory(src/fairseq2n)

if(FAIRSEQ2N_BUILD_PYTHON_BINDINGS)
add_subdirectory(python/src/fairseq2n/bindings)
add_subdirectory(python/src/fairseq2n)
endif()

if(PROJECT_IS_TOP_LEVEL AND BUILD_TESTING)
Expand Down
2 changes: 1 addition & 1 deletion fairseq2n/cmake/modules/FindTBB.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ endif()
# The tbb PyPI package installs oneTBB under the lib directory of the Python
# environment. Check if we can find it there.
find_package(Python3 QUIET COMPONENTS Interpreter)
if(Python3_Interpreter_FOUND)
if(Python3_Interpreter_FOUND AND NOT TBB_LIBRARY)
message(STATUS "Checking for oneTBB under the Python environment...")

set(tbb_base_dir ${Python3_EXECUTABLE})
Expand Down
4 changes: 0 additions & 4 deletions fairseq2n/cmake/summary.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ function(fairseq2n_print_project_summary)
if(FAIRSEQ2N_THREAD_LIB STREQUAL "tbb")
message(STATUS " Intel oneTBB : ${TBB_VERSION}")
endif()
if(FAIRSEQ2N_SUPPORT_IMAGE)
message(STATUS " libjpeg : ${JPEG_VERSION}")
message(STATUS " libpng : ${PNG_VERSION_STRING}")
endif()
message(STATUS " libsndfile : ${SndFile_VERSION}")
message(STATUS "")
endfunction()
1 change: 1 addition & 0 deletions fairseq2n/python/src/fairseq2n/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/config.py
25 changes: 25 additions & 0 deletions fairseq2n/python/src/fairseq2n/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

add_subdirectory(bindings)

# ------------------------------------------------------------
# Library Configuration
# ------------------------------------------------------------

if(FAIRSEQ2N_SUPPORT_IMAGE)
set(SUPPORTS_IMAGE "True")
else()
set(SUPPORTS_IMAGE "False")
endif()

if(FAIRSEQ2N_USE_CUDA)
set(USES_CUDA "True")
else()
set(USES_CUDA "False")
endif()

configure_file(config.py.in ${CMAKE_CURRENT_SOURCE_DIR}/config.py @ONLY)
46 changes: 34 additions & 12 deletions fairseq2n/python/src/fairseq2n/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
from ctypes.util import find_library
from os import environ
from pathlib import Path
from typing import List, Optional, Tuple
from typing import TYPE_CHECKING, List, Optional, Tuple

if TYPE_CHECKING:
_SUPPORTS_IMAGE, _SUPPORTS_CUDA = True, True
else:
from fairseq2n.config import _SUPPORTS_IMAGE, _SUPPORTS_CUDA


__version__ = "0.2.1.dev0"

Expand Down Expand Up @@ -110,6 +116,20 @@ def _load_shared_library(lib_name: str) -> Optional[CDLL]:
_load_shared_libraries()


def _check_cuda_runtime() -> None:
if not _SUPPORTS_CUDA:
return

libcudart = _load_shared_library("libcudart.so")
if libcudart is None:
raise OSError(
"fairseq2 is built with CUDA, but the CUDA runtime cannot be found. Either install the CUDA Toolkit or a CPU-only version of fairseq2 (see https://github.com/facebookresearch/fairseq2#variants)."
)


_check_cuda_runtime()


def get_lib() -> Path:
"""Return the directory that contains fairseq2n shared library."""
return Path(__file__).parent.joinpath("lib")
Expand All @@ -125,18 +145,14 @@ def get_cmake_prefix_path() -> Path:
return Path(__file__).parent.joinpath("lib", "cmake")


def supports_cuda() -> bool:
"""Return ``True`` if fairseq2n supports CUDA."""
from fairseq2n.bindings import _supports_cuda # type: ignore[attr-defined]

return _supports_cuda() # type: ignore[no-any-return]


def supports_image() -> bool:
"""Return ``True`` if fairseq2n supports JPEG/PNG decoding."""
from fairseq2n.bindings import _supports_image # type: ignore[attr-defined]
return _SUPPORTS_IMAGE

return _supports_image() # type: ignore[no-any-return]

def supports_cuda() -> bool:
"""Return ``True`` if fairseq2n supports CUDA."""
return _SUPPORTS_CUDA


def cuda_version() -> Optional[Tuple[int, int]]:
Expand All @@ -145,6 +161,12 @@ def cuda_version() -> Optional[Tuple[int, int]]:
:returns:
The major and minor version segments.
"""
from fairseq2n.bindings import _cuda_version # type: ignore[attr-defined]
if TYPE_CHECKING:

def _cuda_version() -> Optional[Tuple[int, int]]:
...

else:
from fairseq2n.bindings import _cuda_version

return _cuda_version() # type: ignore[no-any-return]
return _cuda_version()
14 changes: 0 additions & 14 deletions fairseq2n/python/src/fairseq2n/bindings/init.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,6 @@ PYBIND11_MODULE(bindings, m)
py::options opts{};
opts.disable_function_signatures();

m.def(
"_supports_cuda",
[]
{
return supports_cuda;
});

m.def(
"_supports_image",
[]
{
return supports_image;
});

// See https://github.com/llvm/llvm-project/issues/57123.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunreachable-code-return"
Expand Down
11 changes: 11 additions & 0 deletions fairseq2n/python/src/fairseq2n/config.py.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

from typing import Final

_SUPPORTS_IMAGE: Final = @SUPPORTS_IMAGE@

_SUPPORTS_CUDA: Final = @USES_CUDA@
7 changes: 1 addition & 6 deletions fairseq2n/src/fairseq2n/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ target_include_directories(fairseq2n ${system}
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/src>
)

find_package(PNG REQUIRED)
find_package(JPEG REQUIRED)

target_link_libraries(fairseq2n
PRIVATE
${CMAKE_DL_LIBS}
Expand All @@ -130,14 +127,12 @@ target_link_libraries(fairseq2n
Threads::Threads
sentencepiece-static
SndFile::sndfile
PNG::PNG
JPEG::JPEG
PUBLIC
torch
)

if(FAIRSEQ2N_SUPPORT_IMAGE)
target_link_libraries(fairseq2n PRIVATE JPEG::JPEG PNG::PNG)
target_link_libraries(fairseq2n PRIVATE jpeg_turbo_static png_static)
endif()

if(FAIRSEQ2N_USE_CUDA)
Expand Down
2 changes: 2 additions & 0 deletions fairseq2n/third-party/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
include(fmt.cmake)
include(gtest.cmake)
include(kaldi-native-fbank.cmake)
include(libjpeg-turbo.cmake)
include(libpng.cmake)
include(natsort.cmake)
include(pybind11.cmake)
include(sentencepiece.cmake)
Expand Down
2 changes: 1 addition & 1 deletion fairseq2n/third-party/kaldi-native-fbank
Submodule kaldi-native-fbank updated 32 files
+55 −0 .github/workflows/build-wheels-macos.yaml
+55 −0 .github/workflows/build-wheels-win32.yaml
+54 −0 .github/workflows/build-wheels-win64.yaml
+2 −2 .github/workflows/build-wheels.yaml
+18 −12 .github/workflows/linux-macos.yaml
+57 −0 .github/workflows/test-wheel.yaml
+6 −22 .github/workflows/windows-x64.yaml
+9 −1 CMakeLists.txt
+7 −1 kaldi-native-fbank/csrc/CMakeLists.txt
+17 −9 kaldi-native-fbank/csrc/feature-window.cc
+5 −3 kaldi-native-fbank/csrc/feature-window.h
+405 −497 kaldi-native-fbank/csrc/fftsg.cc
+37 −0 kaldi-native-fbank/csrc/generate-whisper-melbank.py
+27 −2 kaldi-native-fbank/csrc/mel-computations.cc
+6 −4 kaldi-native-fbank/csrc/mel-computations.h
+3 −2 kaldi-native-fbank/csrc/online-feature.cc
+2 −0 kaldi-native-fbank/csrc/online-feature.h
+3 −6 kaldi-native-fbank/csrc/rfft.cc
+153 −0 kaldi-native-fbank/csrc/whisper-feature.cc
+53 −0 kaldi-native-fbank/csrc/whisper-feature.h
+2,692 −0 kaldi-native-fbank/csrc/whisper-mel-bank.h
+1 −0 kaldi-native-fbank/python/csrc/CMakeLists.txt
+14 −0 kaldi-native-fbank/python/csrc/feature-window.cc
+2 −0 kaldi-native-fbank/python/csrc/kaldi-native-fbank.cc
+1 −0 kaldi-native-fbank/python/csrc/online-feature.cc
+38 −0 kaldi-native-fbank/python/csrc/rfft.cc
+30 −0 kaldi-native-fbank/python/csrc/rfft.h
+4 −1 kaldi-native-fbank/python/kaldi_native_fbank/__init__.py
+4 −1 kaldi-native-fbank/python/tests/CMakeLists.txt
+31 −0 kaldi-native-fbank/python/tests/test_feature_window_function.py
+48 −0 kaldi-native-fbank/python/tests/test_online_whisper_fbank.py
+45 −0 kaldi-native-fbank/python/tests/test_rfft.py
Loading

0 comments on commit 82ebc37

Please sign in to comment.