Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ commands:
description: "installs tools required to build torchaudio"
steps:
- run:
name: Install cmake and pkg-config
command: HOMEBREW_NO_AUTO_UPDATE=1 brew install cmake pkg-config wget
name: Install pkg-config
command: HOMEBREW_NO_AUTO_UPDATE=1 brew install pkg-config wget
# Disable brew auto update which is very slow

binary_common: &binary_common
Expand Down
4 changes: 2 additions & 2 deletions .circleci/config.yml.in
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ commands:
description: "installs tools required to build torchaudio"
steps:
- run:
name: Install cmake and pkg-config
command: HOMEBREW_NO_AUTO_UPDATE=1 brew install cmake pkg-config wget
name: Install pkg-config
command: HOMEBREW_NO_AUTO_UPDATE=1 brew install pkg-config wget
# Disable brew auto update which is very slow

binary_common: &binary_common
Expand Down
4 changes: 3 additions & 1 deletion .circleci/torchscript_bc_test/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ dependencies:
- pytest
- pytest-cov
- codecov
- librosa
- librosa>=0.8.0
- llvmlite==0.31 # See https://github.com/pytorch/audio/pull/766
- pip
- pip:
- cmake
- ninja
- kaldi-io
- scipy
- parameterized
Expand Down
1 change: 1 addition & 0 deletions .circleci/unittest/linux/scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ conda install -y -c "pytorch-${UPLOAD_CHANNEL}" pytorch ${cudatoolkit}

# 2. Install torchaudio
printf "* Installing torchaudio\n"
git submodule update --init --recursive
BUILD_TRANSDUCER=1 BUILD_SOX=1 python setup.py install

# 3. Install Test tools
Expand Down
11 changes: 1 addition & 10 deletions .circleci/unittest/linux/scripts/setup_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#
# Do not install PyTorch and torchaudio here, otherwise they also get cached.

set -e
set -ex

root_dir="$(git rev-parse --show-toplevel)"
conda_dir="${root_dir}/conda"
Expand Down Expand Up @@ -41,12 +41,3 @@ conda activate "${env_dir}"

# 3. Install minimal build tools
pip --quiet install cmake ninja

# 4. Buld codecs
git submodule update --init --recursive
mkdir -p third_party/build
(
cd third_party/build
cmake -GNinja ..
cmake --build .
)
60 changes: 60 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

# Most of the configurations are taken from PyTorch
# https://github.com/pytorch/pytorch/blob/0c9fb4aff0d60eaadb04e4d5d099fb1e1d5701a9/CMakeLists.txt

# Use compiler ID "AppleClang" instead of "Clang" for XCode.
# Not setting this sometimes makes XCode C compiler gets detected as "Clang",
# even when the C++ one is detected as "AppleClang".
cmake_policy(SET CMP0010 NEW)
cmake_policy(SET CMP0025 NEW)

# Suppress warning flags in default MSVC configuration. It's not
# mandatory that we do this (and we don't if cmake is old), but it's
# nice when it's possible, and it's possible on our Windows configs.
if(NOT CMAKE_VERSION VERSION_LESS 3.15.0)
cmake_policy(SET CMP0092 NEW)
endif()

project(torchaudio)

# check and set CMAKE_CXX_STANDARD
string(FIND "${CMAKE_CXX_FLAGS}" "-std=c++" env_cxx_standard)
if(env_cxx_standard GREATER -1)
message(
WARNING "C++ standard version definition detected in environment variable."
"PyTorch requires -std=c++14. Please remove -std=c++ settings in your environment.")
endif()
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_C_STANDARD 11)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# Apple specific
if(APPLE)
# Get clang version on macOS
execute_process( COMMAND ${CMAKE_CXX_COMPILER} --version OUTPUT_VARIABLE clang_full_version_string )
string(REGEX REPLACE "Apple LLVM version ([0-9]+\\.[0-9]+).*" "\\1" CLANG_VERSION_STRING ${clang_full_version_string})
message( STATUS "CLANG_VERSION_STRING: " ${CLANG_VERSION_STRING} )

# RPATH stuff
set(CMAKE_MACOSX_RPATH ON)

set(CMAKE_SHARED_LIBRARY_SUFFIX ".so")
endif()


# Options
option(BUILD_SOX "Build libsox statically" OFF)
option(BUILD_TRANSDUCER "Enable transducer" OFF)
option(BUILD_LIBTORCHAUDIO "Build C++ Library" ON)
option(BUILD_PYTHON_EXTENSION "Build Python extension" OFF)

find_package(Torch REQUIRED)

# Set -D_GLIBCXX_USE_CXX11_ABI for third party builds
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_subdirectory(third_party)
add_subdirectory(torchaudio/csrc)
202 changes: 69 additions & 133 deletions build_tools/setup_helpers/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@
import platform
import subprocess
from pathlib import Path
import distutils.sysconfig

from setuptools import Extension
from setuptools.command.build_ext import build_ext
import torch
from torch.utils.cpp_extension import (
CppExtension,
BuildExtension as TorchBuildExtension
)

__all__ = [
'get_ext_modules',
'BuildExtension',
'CMakeBuild',
]

_THIS_DIR = Path(__file__).parent.resolve()
_ROOT_DIR = _THIS_DIR.parent.parent.resolve()
_CSRC_DIR = _ROOT_DIR / 'torchaudio' / 'csrc'
_TP_BASE_DIR = _ROOT_DIR / 'third_party'
_TP_INSTALL_DIR = _TP_BASE_DIR / 'install'
_TORCHAUDIO_DIR = _ROOT_DIR / 'torchaudio'


def _get_build(var):
Expand All @@ -38,132 +35,71 @@ def _get_build(var):
_BUILD_TRANSDUCER = _get_build("BUILD_TRANSDUCER")


def _get_eca(debug):
eca = []
if debug:
eca += ["-O0", "-g"]
else:
eca += ["-O3"]
if _BUILD_TRANSDUCER:
eca += ['-DBUILD_TRANSDUCER']
return eca


def _get_ela(debug):
ela = []
if debug:
if platform.system() == "Windows":
ela += ["/DEBUG:FULL"]
else:
ela += ["-O0", "-g"]
else:
ela += ["-O3"]
return ela


def _get_srcs():
srcs = [_CSRC_DIR / 'pybind.cpp']
srcs += list(_CSRC_DIR.glob('sox/**/*.cpp'))
if _BUILD_TRANSDUCER:
srcs += [_CSRC_DIR / 'transducer.cpp']
return [str(path) for path in srcs]


def _get_include_dirs():
dirs = [
str(_ROOT_DIR),
]
if _BUILD_SOX or _BUILD_TRANSDUCER:
dirs.append(str(_TP_INSTALL_DIR / 'include'))
return dirs


def _get_extra_objects():
libs = []
if _BUILD_SOX:
# NOTE: The order of the library listed bellow matters.
#
# (the most important thing is that dependencies come after a library
# e.g., sox comes first, flac/vorbis comes before ogg, and
# vorbisenc/vorbisfile comes before vorbis
libs += [
'libsox.a',
'libmad.a',
'libFLAC.a',
'libmp3lame.a',
'libopusfile.a',
'libopus.a',
'libvorbisenc.a',
'libvorbisfile.a',
'libvorbis.a',
'libogg.a',
'libopencore-amrnb.a',
'libopencore-amrwb.a',
]
if _BUILD_TRANSDUCER:
libs += ['libwarprnnt.a']

return [str(_TP_INSTALL_DIR / 'lib' / lib) for lib in libs]


def _get_libraries():
return [] if _BUILD_SOX else ['sox']


def _get_cxx11_abi():
try:
value = int(torch._C._GLIBCXX_USE_CXX11_ABI)
except ImportError:
value = 0
return f'-D_GLIBCXX_USE_CXX11_ABI={value}'


def _build_third_party(base_build_dir):
build_dir = os.path.join(base_build_dir, 'third_party')
os.makedirs(build_dir, exist_ok=True)
subprocess.run(
args=[
'cmake',
f"-DCMAKE_CXX_FLAGS='{_get_cxx11_abi()}'",
'-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON',
f'-DCMAKE_INSTALL_PREFIX={_TP_INSTALL_DIR}',
f'-DBUILD_SOX={"ON" if _BUILD_SOX else "OFF"}',
f'-DBUILD_TRANSDUCER={"ON" if _BUILD_TRANSDUCER else "OFF"}',
f'{_TP_BASE_DIR}'],
cwd=build_dir,
check=True,
)
command = ['cmake', '--build', '.']
if _BUILD_TRANSDUCER:
command += ['--target', 'install']
subprocess.run(
args=command,
cwd=build_dir,
check=True,
)

def get_ext_modules():
if platform.system() == 'Windows':
return None
return [Extension(name='torchaudio._torchaudio', sources=[])]

_EXT_NAME = 'torchaudio._torchaudio'

# Based off of
# https://github.com/pybind/cmake_example/blob/580c5fd29d4651db99d8874714b07c0c49a53f8a/setup.py
class CMakeBuild(build_ext):
def run(self):
try:
subprocess.check_output(['cmake', '--version'])
except OSError:
raise RuntimeError("CMake is not available.")
super().run()

def get_ext_modules(debug=False):
if platform.system() == 'Windows':
return None
return [
CppExtension(
_EXT_NAME,
_get_srcs(),
libraries=_get_libraries(),
include_dirs=_get_include_dirs(),
extra_compile_args=_get_eca(debug),
extra_objects=_get_extra_objects(),
extra_link_args=_get_ela(debug),
),
]


class BuildExtension(TorchBuildExtension):
def build_extension(self, ext):
if ext.name == _EXT_NAME and _BUILD_SOX:
_build_third_party(self.build_temp)
super().build_extension(ext)
extdir = os.path.abspath(
os.path.dirname(self.get_ext_fullpath(ext.name)))

# required for auto-detection of auxiliary "native" libs
if not extdir.endswith(os.path.sep):
extdir += os.path.sep

cfg = "Debug" if self.debug else "Release"

cmake_args = [
f"-DCMAKE_BUILD_TYPE={cfg}",
f"-DCMAKE_PREFIX_PATH={torch.utils.cmake_prefix_path}",
f"-DCMAKE_INSTALL_PREFIX={extdir}",
'-DCMAKE_VERBOSE_MAKEFILE=ON',
f"-DPython_INCLUDE_DIR={distutils.sysconfig.get_python_inc()}",
f"-DBUILD_SOX:BOOL={'ON' if _BUILD_SOX else 'OFF'}",
f"-DBUILD_TRANSDUCER:BOOL={'ON' if _BUILD_TRANSDUCER else 'OFF'}",
"-DBUILD_TORCHAUDIO_PYTHON_EXTENSION:BOOL=ON",
"-DBUILD_LIBTORCHAUDIO:BOOL=OFF",
]
build_args = [
'--target', 'install'
]

# Default to Ninja
if 'CMAKE_GENERATOR' not in os.environ:
cmake_args += ["-GNinja"]

# Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level
# across all generators.
if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ:
# self.parallel is a Python 3 only way to set parallel jobs by hand
# using -j in the build_ext call, not supported by pip or PyPA-build.
if hasattr(self, "parallel") and self.parallel:
# CMake 3.12+ only.
build_args += ["-j{}".format(self.parallel)]

if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)

subprocess.check_call(
["cmake", str(_ROOT_DIR)] + cmake_args, cwd=self.build_temp)
subprocess.check_call(
["cmake", "--build", "."] + build_args, cwd=self.build_temp)

def get_ext_filename(self, fullname):
ext_filename = super().get_ext_filename(fullname)
ext_filename_parts = ext_filename.split('.')
without_abi = ext_filename_parts[:-2] + ext_filename_parts[-1:]
ext_filename = '.'.join(without_abi)
return ext_filename
2 changes: 1 addition & 1 deletion packaging/build_wheel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export BUILD_TYPE="wheel"
export NO_CUDA_PACKAGE=1
setup_env 0.8.0
setup_wheel_python
pip_install numpy future
pip_install numpy future cmake ninja
setup_pip_pytorch_version
python setup.py clean
if [[ "$OSTYPE" == "msys" ]]; then
Expand Down
3 changes: 2 additions & 1 deletion packaging/torchaudio/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ requirements:
- python
- setuptools
- cpuonly
- cmake
- ninja
{{ environ.get('CONDA_PYTORCH_BUILD_CONSTRAINT') }}

run:
- python
- typing # [py2k]
{{ environ.get('CONDA_PYTORCH_CONSTRAINT') }}

build:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def run(self):
packages=find_packages(exclude=["build*", "test*", "torchaudio.csrc*", "third_party*", "build_tools*"]),
ext_modules=setup_helpers.get_ext_modules(),
cmdclass={
'build_ext': setup_helpers.BuildExtension.with_options(no_python_abi_suffix=True),
'build_ext': setup_helpers.CMakeBuild,
'clean': clean,
},
install_requires=[pytorch_package_dep],
Expand Down
Loading