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
43 changes: 27 additions & 16 deletions .github/workflows/pip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
arch: [ x86_64, aarch64 ]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v3.1.0

- name: Log in to GitHub Container Registry
uses: docker/login-action@v2.0.0
Expand Down Expand Up @@ -67,9 +67,6 @@ jobs:

runs-on: ${{ matrix.runner }}

env:
CMAKE_PREFIX_PATH: ${{ github.workspace }}/local

strategy:
fail-fast: false
matrix:
Expand All @@ -83,28 +80,29 @@ jobs:
arch: x86_64;arm64

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v3.1.0

- name: Cache LLVM build folder
id: cache-llvm
uses: actions/cache@v3
uses: actions/cache@v3.0.11
with:
path: local
path: local-llvm
key: llvmorg-${{ env.LLVM_VER }}-${{ runner.os }}

- uses: ilammy/msvc-dev-cmd@v1
- uses: lukka/get-cmake@latest

- uses: actions/checkout@v3
- uses: actions/checkout@v3.1.0
if: steps.cache-llvm.outputs.cache-hit != 'true'
with:
path: llvm-src
repository: llvm/llvm-project
ref: llvmorg-${{ env.LLVM_VER }}

- name: Configure LLVM
if: steps.cache-llvm.outputs.cache-hit != 'true'
run: >
cmake -G Ninja -S llvm -B build
cmake -G Ninja -S llvm-src/llvm -B llvm-build
-DCMAKE_BUILD_TYPE=Release
"-DCMAKE_OSX_ARCHITECTURES=arm64;x86_64"
"-DLLVM_TARGETS_TO_BUILD=X86;ARM;NVPTX;AArch64;Mips;Hexagon;WebAssembly"
Expand All @@ -116,46 +114,59 @@ jobs:
-DLLVM_ENABLE_TERMINFO=OFF
-DLLVM_ENABLE_ZSTD=OFF
-DLLVM_ENABLE_ZLIB=OFF
-DLLVM_ENABLE_OCAMLDOC=OFF
-DLLVM_ENABLE_BINDINGS=OFF
-DLLVM_ENABLE_IDE=OFF

- name: Build LLVM
if: steps.cache-llvm.outputs.cache-hit != 'true'
run: cmake --build build
run: cmake --build llvm-build

- name: Install LLVM
if: steps.cache-llvm.outputs.cache-hit != 'true'
run: cmake --install build --prefix local
run: cmake --install llvm-build --prefix local-llvm

# Remove the LLVM source tree after building it, otherwise we can
# run out of local space while building halide
- name: Clean LLVM Source
if: steps.cache-llvm.outputs.cache-hit != 'true'
shell: bash
run: rm -rf llvm-src

- name: Configure Halide
if: runner.os == 'Windows'
run: >
cmake -G "Visual Studio 17 2022" -T ClangCL -A "${{ matrix.arch }}" -S . -B build
cmake -G "Visual Studio 17 2022" -T ClangCL -A "${{ matrix.arch }}" -S . -B halide-build
-DWITH_DOCS=NO
-DWITH_PYTHON_BINDINGS=NO
-DWITH_TESTS=NO
-DWITH_TUTORIALS=NO
-DWITH_UTILS=NO
-DLLVM_DIR=${{ github.workspace }}/local-llvm/lib/cmake/llvm

- name: Configure Halide
if: runner.os != 'Windows'
run: >
cmake -G Ninja -S . -B build
cmake -G Ninja -S . -B halide-build
-DCMAKE_BUILD_TYPE=Release
"-DCMAKE_OSX_ARCHITECTURES=${{ matrix.arch }}"
-DWITH_DOCS=NO
-DWITH_PYTHON_BINDINGS=NO
-DWITH_TESTS=NO
-DWITH_TUTORIALS=NO
-DWITH_UTILS=NO
-DLLVM_DIR=${{ github.workspace }}/local-llvm/lib/cmake/llvm

- name: Build Halide
run: cmake --build build --config Release
run: cmake --build halide-build --config Release

- name: Install Halide
run: cmake --install build --config Release --prefix local
run: cmake --install halide-build --config Release --prefix local-halide

- name: Build wheels
uses: pypa/cibuildwheel@v2.10.2
env:
CMAKE_PREFIX_PATH: ${{ github.workspace }}/local-halide
CIBW_BUILD: "cp38-${{ matrix.pytag }} cp39-${{ matrix.pytag }} cp310-${{ matrix.pytag }}"
CIBW_ARCHS_MACOS: "universal2"

Expand All @@ -168,7 +179,7 @@ jobs:
name: Make SDist
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v3.1.0
- run: pipx run build --sdist
- uses: actions/upload-artifact@v3
with:
Expand Down
30 changes: 22 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,28 @@ option(TARGET_SPIRV "Include SPIR-V target" OFF)

add_subdirectory(dependencies)

##
# Declare options
##

# Declare these options after we include dependencies (since it declares Halide_ENABLE_RTTI etc)
# but before we add any subdirectories, since any option you test before it is defined is
# implicitly false the *first* time that the build file is processed, and there are some
# out-of-order dependencies here (e.g, code in src/ eventually checks WITH_UTILS).
# This is especially subtle since it means that some options can end up with different
# values if you build a target as part of the initial CMake run, so (e.g.) a `make install`
# from as totally clean build might neglect to install some pieces.

option(WITH_TESTS "Build tests" "${PROJECT_IS_TOP_LEVEL}")
option(WITH_TUTORIALS "Build tutorials" "${PROJECT_IS_TOP_LEVEL}")
option(WITH_DOCS "Build documentation" OFF)
option(WITH_UTILS "Build utils" "${PROJECT_IS_TOP_LEVEL}")
cmake_dependent_option(
WITH_PYTHON_BINDINGS "Build Python bindings" "${PROJECT_IS_TOP_LEVEL}"
"Halide_ENABLE_RTTI AND Halide_ENABLE_EXCEPTIONS" OFF
)


##
# Add source directories
##
Expand All @@ -101,42 +123,34 @@ add_subdirectory(tools)
# Add tests, tutorials, etc. if we're not being imported into another CMake project.
##

option(WITH_TESTS "Build tests" "${PROJECT_IS_TOP_LEVEL}")
if (WITH_TESTS)
message(STATUS "Building tests enabled")
add_subdirectory(test)
else ()
message(STATUS "Building tests disabled")
endif ()

cmake_dependent_option(
WITH_PYTHON_BINDINGS "Build Python bindings" "${PROJECT_IS_TOP_LEVEL}"
"Halide_ENABLE_RTTI AND Halide_ENABLE_EXCEPTIONS" OFF
)
if (WITH_PYTHON_BINDINGS)
message(STATUS "Building Python bindings enabled")
add_subdirectory(python_bindings)
else ()
message(STATUS "Building Python bindings disabled")
endif ()

option(WITH_TUTORIALS "Build tutorials" "${PROJECT_IS_TOP_LEVEL}")
if (WITH_TUTORIALS)
message(STATUS "Building tutorials enabled")
add_subdirectory(tutorial)
else ()
message(STATUS "Building tutorials disabled")
endif ()

option(WITH_DOCS "Build documentation" OFF)
if (WITH_DOCS)
message(STATUS "Building docs enabled")
add_subdirectory(doc)
else ()
message(STATUS "Building docs disabled")
endif ()

option(WITH_UTILS "Build utils" "${PROJECT_IS_TOP_LEVEL}")
if (WITH_UTILS)
message(STATUS "Building utils enabled")
add_subdirectory(util)
Expand Down
6 changes: 3 additions & 3 deletions README_cmake.md
Original file line number Diff line number Diff line change
Expand Up @@ -935,8 +935,8 @@ generators were imported (and hence won't be built). Otherwise, it will be set
to false. This variable may be used to conditionally set properties on
`<target>`.

Please see [test/integration/xc](https://github.com/halide/Halide/tree/master/test/integration/xc) for a simple example
and [apps/hannk](https://github.com/halide/Halide/tree/master/apps/hannk) for a complete app that uses it extensively.
Please see [test/integration/xc](https://github.com/halide/Halide/tree/main/test/integration/xc) for a simple example
and [apps/hannk](https://github.com/halide/Halide/tree/main/apps/hannk) for a complete app that uses it extensively.

If `PYSTUB` is specified, then a Python Extension will be built that
wraps the Generator with CPython glue to allow use of the Generator
Expand Down Expand Up @@ -1190,7 +1190,7 @@ without broader approval. Confine dependencies to the `dependencies/` subtree.
Any variables that are specific to languages that are not enabled should, of
course, be avoided. But of greater concern are variables that are easy to misuse
or should not be overridden for our end-users. The following (non-exhaustive)
list of variables shall not be used in code merged into master.
list of variables shall not be used in code merged into main.

| Variable | Reason | Alternative |
|---------------------------------|-----------------------------------------------|---------------------------------------------------------------------------------------------------------|
Expand Down
2 changes: 1 addition & 1 deletion README_python.md
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ in future releases.
## License

The Python bindings use the same
[MIT license](https://github.com/halide/Halide/blob/master/LICENSE.txt) as
[MIT license](https://github.com/halide/Halide/blob/main/LICENSE.txt) as
Halide.

Python bindings provided by Connelly Barnes (2012-2013), Fred Rotbart (2014),
Expand Down
2 changes: 1 addition & 1 deletion apps/bgu/bgu_generator.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// A Halide implementation of bilateral-guided upsampling.

// Adapted from https://github.com/google/bgu/blob/master/src/halide/bgu.cpp
// Adapted from https://github.com/google/bgu/tree/master/src/halide

// Copyright 2016 Google Inc.
//
Expand Down
4 changes: 2 additions & 2 deletions python_bindings/src/halide/halide_/PyBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,6 @@ Type format_descriptor_to_type(const std::string &fd) {
return Type();
}

namespace {

py::object buffer_getitem_operator(Buffer<> &buf, const std::vector<int> &pos) {
if ((size_t)pos.size() != (size_t)buf.dimensions()) {
throw py::value_error("Incorrect number of dimensions.");
Expand Down Expand Up @@ -228,6 +226,8 @@ py::object buffer_getitem_operator(Buffer<> &buf, const std::vector<int> &pos) {
return py::object();
}

namespace {

py::object buffer_setitem_operator(Buffer<> &buf, const std::vector<int> &pos, const py::object &value) {
if ((size_t)pos.size() != (size_t)buf.dimensions()) {
throw py::value_error("Incorrect number of dimensions.");
Expand Down
2 changes: 2 additions & 0 deletions python_bindings/src/halide/halide_/PyBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ void define_buffer(py::module &m);

Type format_descriptor_to_type(const std::string &fd);

py::object buffer_getitem_operator(Buffer<> &buf, const std::vector<int> &pos);

template<typename T = void,
int Dims = AnyDims,
int InClassDimStorage = (Dims == AnyDims ? 4 : std::max(Dims, 1))>
Expand Down
39 changes: 37 additions & 2 deletions python_bindings/src/halide/halide_/PyFunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,38 @@ py::object realization_to_object(const Realization &r) {
return to_python_tuple(r);
}

py::object evaluate_impl(const py::object &expr, bool may_gpu) {
Tuple t = to_halide_tuple(expr);
Func f("evaluate_func_" + std::to_string(t.size()));
f() = t;
if (may_gpu) {
Internal::schedule_scalar(f);
}

std::optional<Realization> r;
{
py::gil_scoped_release release;

r = f.realize();
}
if (r->size() == 1) {
return buffer_getitem_operator((*r)[0], {});
} else {
py::tuple result(r->size());
for (size_t i = 0; i < r->size(); i++) {
result[i] = buffer_getitem_operator((*r)[i], {});
}
return result;
}
}

} // namespace

void define_func(py::module &m) {
define_func_ref(m);
define_var_or_rvar(m);
define_loop_level(m);

// TODO: add ParamMap support.

// Deliberately not supported, because they don't seem to make sense for Python:
// - set_custom_allocator()
// - set_custom_do_task()
Expand Down Expand Up @@ -380,6 +403,18 @@ void define_func(py::module &m) {
add_schedule_methods(func_class);

define_stage(m);

m.def(
"evaluate", [](const py::object &expr) -> py::object {
return evaluate_impl(expr, false);
},
py::arg("expr"));

m.def(
"evaluate_may_gpu", [](const py::object &expr) -> py::object {
return evaluate_impl(expr, true);
},
py::arg("expr"));
}

} // namespace PythonBindings
Expand Down
26 changes: 26 additions & 0 deletions python_bindings/src/halide/halide_/PyTuple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@
namespace Halide {
namespace PythonBindings {

Tuple to_halide_tuple(const py::object &o) {
try {
Expr e = o.cast<Expr>();
return Tuple(e);
} catch (...) {
// fall thru
}

try {
py::tuple t = o.cast<py::tuple>();
if (t.empty()) {
throw py::value_error("Cannot use a zero-length tuple-of-Expr");
}
std::vector<Expr> v(t.size());
for (size_t i = 0; i < t.size(); i++) {
v[i] = t[i].cast<Expr>();
}
return Tuple(v);
} catch (...) {
// fall thru
}

throw py::value_error("Expected an Expr or tuple-of-Expr.");
}

void define_tuple(py::module &m) {
// Halide::Tuple isn't surfaced to the user in Python;
// we define it here to allow PyBind to do some automatic
Expand Down Expand Up @@ -42,6 +67,7 @@ void define_tuple(py::module &m) {
o << "<halide.Tuple of size " << t.size() << ">";
return o.str();
});

py::implicitly_convertible<py::tuple, Tuple>();

// If we autoconvert from vector<Expr>, we must also special-case FuncRef, alas
Expand Down
5 changes: 5 additions & 0 deletions python_bindings/src/halide/halide_/PyTuple.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ inline py::tuple to_python_tuple(const T &ht) {
return pt;
}

// in: convertible-to-Expr, or tuple-of-convertible-to-Expr
// out: Halide::Tuple
// throws exception if not convertible
Tuple to_halide_tuple(const py::object &o);

} // namespace PythonBindings
} // namespace Halide

Expand Down
Loading