Skip to content

Commit 6a3a3e2

Browse files
authored
Add Apple-accelerated implementations for ImageProcessor (pytorch#20037)
Differential Revision: D106898414 Pull Request resolved: pytorch#20037
1 parent 4c9c444 commit 6a3a3e2

10 files changed

Lines changed: 2641 additions & 23 deletions

‎extension/image/CMakeLists.txt‎

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,50 @@
66

77
cmake_minimum_required(VERSION 3.19)
88

9-
# stb_image_resize: lightweight header-only library used by the resize step in
10-
# image_processor.cpp.
11-
include(FetchContent)
12-
FetchContent_Declare(
13-
stb
14-
GIT_REPOSITORY https://github.com/nothings/stb.git
15-
GIT_TAG f0569113c93ad095470c54bf34a17b36646bbbb5
16-
)
17-
FetchContent_MakeAvailable(stb)
9+
if(APPLE)
10+
enable_language(OBJCXX)
11+
add_library(
12+
extension_image image_processor_common.cpp image_processor_apple.cpp
13+
image_processor_apple_gpu.mm
14+
)
15+
set_source_files_properties(
16+
image_processor_apple_gpu.mm PROPERTIES COMPILE_FLAGS "-fobjc-arc"
17+
)
18+
find_library(ACCELERATE_FRAMEWORK Accelerate REQUIRED)
19+
find_library(COREGRAPHICS_FRAMEWORK CoreGraphics REQUIRED)
20+
find_library(COREIMAGE_FRAMEWORK CoreImage REQUIRED)
21+
find_library(COREVIDEO_FRAMEWORK CoreVideo REQUIRED)
22+
find_library(FOUNDATION_FRAMEWORK Foundation REQUIRED)
23+
target_link_libraries(
24+
extension_image
25+
PRIVATE ${ACCELERATE_FRAMEWORK} ${COREGRAPHICS_FRAMEWORK}
26+
${COREIMAGE_FRAMEWORK} ${COREVIDEO_FRAMEWORK}
27+
${FOUNDATION_FRAMEWORK}
28+
)
29+
else()
30+
# stb_image_resize: lightweight header-only library used by the resize step in
31+
# image_processor.cpp. Only the portable (non-Apple) path uses stb; the Apple
32+
# path resizes via vImage, so the fetch is scoped here to avoid downloading
33+
# stb on Apple builds.
34+
include(FetchContent)
35+
FetchContent_Declare(
36+
stb
37+
GIT_REPOSITORY https://github.com/nothings/stb.git
38+
GIT_TAG f0569113c93ad095470c54bf34a17b36646bbbb5
39+
)
40+
FetchContent_MakeAvailable(stb)
1841

19-
add_library(extension_image image_processor_common.cpp image_processor.cpp)
42+
add_library(extension_image image_processor_common.cpp image_processor.cpp)
2043

21-
target_include_directories(
22-
extension_image PUBLIC ${_common_include_directories}
23-
)
44+
# stb_image_resize.h lives under deprecated/ in current stb. Private: only the
45+
# .cpp uses it, not the installed public headers.
46+
target_include_directories(
47+
extension_image PRIVATE ${stb_SOURCE_DIR} ${stb_SOURCE_DIR}/deprecated
48+
)
49+
endif()
2450

25-
# stb_image_resize.h lives under deprecated/ in current stb. Private: only the
26-
# .cpp uses it, not the installed public headers.
2751
target_include_directories(
28-
extension_image PRIVATE ${stb_SOURCE_DIR} ${stb_SOURCE_DIR}/deprecated
52+
extension_image PUBLIC ${_common_include_directories}
2953
)
3054

3155
target_link_libraries(extension_image PUBLIC executorch_core extension_tensor)
@@ -36,9 +60,16 @@ install(
3660
DESTINATION ${CMAKE_INSTALL_LIBDIR}
3761
)
3862

39-
install(FILES image_processor.h image_processor_config.h
40-
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/extension/image
41-
)
63+
if(APPLE)
64+
install(FILES image_processor.h image_processor_config.h
65+
image_processor_apple.h
66+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/extension/image
67+
)
68+
else()
69+
install(FILES image_processor.h image_processor_config.h
70+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/extension/image
71+
)
72+
endif()
4273

4374
if(BUILD_TESTING)
4475
add_subdirectory(test)

0 commit comments

Comments
 (0)