|  | 
| 22 | 22 | USE_WEBP = os.getenv("TORCHVISION_USE_WEBP", "1") == "1" | 
| 23 | 23 | USE_NVJPEG = os.getenv("TORCHVISION_USE_NVJPEG", "1") == "1" | 
| 24 | 24 | NVCC_FLAGS = os.getenv("NVCC_FLAGS", None) | 
| 25 |  | -# Note: the GPU video decoding stuff used to be called "video codec", which | 
| 26 |  | -# isn't an accurate or descriptive name considering there are at least 2 other | 
| 27 |  | -# video decoding backends in torchvision. I'm renaming this to "gpu video | 
| 28 |  | -# decoder" where possible, keeping user facing names (like the env var below) to | 
| 29 |  | -# the old scheme for BC. | 
| 30 |  | -USE_GPU_VIDEO_DECODER = os.getenv("TORCHVISION_USE_VIDEO_CODEC", "1") == "1" | 
| 31 |  | -# Same here: "use ffmpeg" was used to denote "use cpu video decoder". | 
| 32 |  | -USE_CPU_VIDEO_DECODER = os.getenv("TORCHVISION_USE_FFMPEG", "1") == "1" | 
| 33 | 25 | 
 | 
| 34 | 26 | TORCHVISION_INCLUDE = os.environ.get("TORCHVISION_INCLUDE", "") | 
| 35 | 27 | TORCHVISION_LIBRARY = os.environ.get("TORCHVISION_LIBRARY", "") | 
|  | 
| 52 | 44 | print(f"{USE_WEBP = }") | 
| 53 | 45 | print(f"{USE_NVJPEG = }") | 
| 54 | 46 | print(f"{NVCC_FLAGS = }") | 
| 55 |  | -print(f"{USE_CPU_VIDEO_DECODER = }") | 
| 56 |  | -print(f"{USE_GPU_VIDEO_DECODER = }") | 
| 57 | 47 | print(f"{TORCHVISION_INCLUDE = }") | 
| 58 | 48 | print(f"{TORCHVISION_LIBRARY = }") | 
| 59 | 49 | print(f"{IS_ROCM = }") | 
| @@ -371,158 +361,6 @@ def make_image_extension(): | 
| 371 | 361 |     ) | 
| 372 | 362 | 
 | 
| 373 | 363 | 
 | 
| 374 |  | -def make_video_decoders_extensions(): | 
| 375 |  | -    print("Building video decoder extensions") | 
| 376 |  | - | 
| 377 |  | -    build_without_extensions_msg = "Building without video decoders extensions." | 
| 378 |  | -    if sys.platform != "linux" or (sys.version_info.major == 3 and sys.version_info.minor == 9): | 
| 379 |  | -        # FIXME: Building torchvision with ffmpeg on MacOS or with Python 3.9 | 
| 380 |  | -        # FIXME: causes crash. See the following GitHub issues for more details. | 
| 381 |  | -        # FIXME: https://github.com/pytorch/pytorch/issues/65000 | 
| 382 |  | -        # FIXME: https://github.com/pytorch/vision/issues/3367 | 
| 383 |  | -        print("Can only build video decoder extensions on linux and Python != 3.9") | 
| 384 |  | -        return [] | 
| 385 |  | - | 
| 386 |  | -    ffmpeg_exe = shutil.which("ffmpeg") | 
| 387 |  | -    if ffmpeg_exe is None: | 
| 388 |  | -        print(f"{build_without_extensions_msg} Couldn't find ffmpeg binary.") | 
| 389 |  | -        return [] | 
| 390 |  | - | 
| 391 |  | -    def find_ffmpeg_libraries(): | 
| 392 |  | -        ffmpeg_libraries = {"libavcodec", "libavformat", "libavutil", "libswresample", "libswscale"} | 
| 393 |  | - | 
| 394 |  | -        ffmpeg_bin = os.path.dirname(ffmpeg_exe) | 
| 395 |  | -        ffmpeg_root = os.path.dirname(ffmpeg_bin) | 
| 396 |  | -        ffmpeg_include_dir = os.path.join(ffmpeg_root, "include") | 
| 397 |  | -        ffmpeg_library_dir = os.path.join(ffmpeg_root, "lib") | 
| 398 |  | - | 
| 399 |  | -        gcc = os.environ.get("CC", shutil.which("gcc")) | 
| 400 |  | -        platform_tag = subprocess.run([gcc, "-print-multiarch"], stdout=subprocess.PIPE) | 
| 401 |  | -        platform_tag = platform_tag.stdout.strip().decode("utf-8") | 
| 402 |  | - | 
| 403 |  | -        if platform_tag: | 
| 404 |  | -            # Most probably a Debian-based distribution | 
| 405 |  | -            ffmpeg_include_dir = [ffmpeg_include_dir, os.path.join(ffmpeg_include_dir, platform_tag)] | 
| 406 |  | -            ffmpeg_library_dir = [ffmpeg_library_dir, os.path.join(ffmpeg_library_dir, platform_tag)] | 
| 407 |  | -        else: | 
| 408 |  | -            ffmpeg_include_dir = [ffmpeg_include_dir] | 
| 409 |  | -            ffmpeg_library_dir = [ffmpeg_library_dir] | 
| 410 |  | - | 
| 411 |  | -        for library in ffmpeg_libraries: | 
| 412 |  | -            library_found = False | 
| 413 |  | -            for search_path in ffmpeg_include_dir + TORCHVISION_INCLUDE: | 
| 414 |  | -                full_path = os.path.join(search_path, library, "*.h") | 
| 415 |  | -                library_found |= len(glob.glob(full_path)) > 0 | 
| 416 |  | - | 
| 417 |  | -            if not library_found: | 
| 418 |  | -                print(f"{build_without_extensions_msg}") | 
| 419 |  | -                print(f"{library} header files were not found.") | 
| 420 |  | -                return None, None | 
| 421 |  | - | 
| 422 |  | -        return ffmpeg_include_dir, ffmpeg_library_dir | 
| 423 |  | - | 
| 424 |  | -    ffmpeg_include_dir, ffmpeg_library_dir = find_ffmpeg_libraries() | 
| 425 |  | -    if ffmpeg_include_dir is None or ffmpeg_library_dir is None: | 
| 426 |  | -        return [] | 
| 427 |  | - | 
| 428 |  | -    print("Found ffmpeg:") | 
| 429 |  | -    print(f"  ffmpeg include path: {ffmpeg_include_dir}") | 
| 430 |  | -    print(f"  ffmpeg library_dir: {ffmpeg_library_dir}") | 
| 431 |  | - | 
| 432 |  | -    extensions = [] | 
| 433 |  | -    if USE_CPU_VIDEO_DECODER: | 
| 434 |  | -        print("Building with CPU video decoder support") | 
| 435 |  | - | 
| 436 |  | -        # TorchVision base decoder + video reader | 
| 437 |  | -        video_reader_src_dir = os.path.join(ROOT_DIR, "torchvision", "csrc", "io", "video_reader") | 
| 438 |  | -        video_reader_src = glob.glob(os.path.join(video_reader_src_dir, "*.cpp")) | 
| 439 |  | -        base_decoder_src_dir = os.path.join(ROOT_DIR, "torchvision", "csrc", "io", "decoder") | 
| 440 |  | -        base_decoder_src = glob.glob(os.path.join(base_decoder_src_dir, "*.cpp")) | 
| 441 |  | -        # Torchvision video API | 
| 442 |  | -        videoapi_src_dir = os.path.join(ROOT_DIR, "torchvision", "csrc", "io", "video") | 
| 443 |  | -        videoapi_src = glob.glob(os.path.join(videoapi_src_dir, "*.cpp")) | 
| 444 |  | -        # exclude tests | 
| 445 |  | -        base_decoder_src = [x for x in base_decoder_src if "_test.cpp" not in x] | 
| 446 |  | - | 
| 447 |  | -        combined_src = video_reader_src + base_decoder_src + videoapi_src | 
| 448 |  | - | 
| 449 |  | -        extensions.append( | 
| 450 |  | -            CppExtension( | 
| 451 |  | -                # This is an awful name. It should be "cpu_video_decoder". Keeping for BC. | 
| 452 |  | -                "torchvision.video_reader", | 
| 453 |  | -                combined_src, | 
| 454 |  | -                include_dirs=[ | 
| 455 |  | -                    base_decoder_src_dir, | 
| 456 |  | -                    video_reader_src_dir, | 
| 457 |  | -                    videoapi_src_dir, | 
| 458 |  | -                    str(CSRS_DIR), | 
| 459 |  | -                    *ffmpeg_include_dir, | 
| 460 |  | -                    *TORCHVISION_INCLUDE, | 
| 461 |  | -                ], | 
| 462 |  | -                library_dirs=ffmpeg_library_dir + TORCHVISION_LIBRARY, | 
| 463 |  | -                libraries=[ | 
| 464 |  | -                    "avcodec", | 
| 465 |  | -                    "avformat", | 
| 466 |  | -                    "avutil", | 
| 467 |  | -                    "swresample", | 
| 468 |  | -                    "swscale", | 
| 469 |  | -                ], | 
| 470 |  | -                extra_compile_args=["-std=c++17"] if os.name != "nt" else ["/std:c++17", "/MP"], | 
| 471 |  | -                extra_link_args=["-std=c++17" if os.name != "nt" else "/std:c++17"], | 
| 472 |  | -            ) | 
| 473 |  | -        ) | 
| 474 |  | - | 
| 475 |  | -    if USE_GPU_VIDEO_DECODER: | 
| 476 |  | -        # Locating GPU video decoder headers and libraries | 
| 477 |  | -        # CUDA_HOME should be set to the cuda root directory. | 
| 478 |  | -        # TORCHVISION_INCLUDE and TORCHVISION_LIBRARY should include the locations | 
| 479 |  | -        # to the headers and libraries below | 
| 480 |  | -        if not ( | 
| 481 |  | -            BUILD_CUDA_SOURCES | 
| 482 |  | -            and CUDA_HOME is not None | 
| 483 |  | -            and any([os.path.exists(os.path.join(folder, "cuviddec.h")) for folder in TORCHVISION_INCLUDE]) | 
| 484 |  | -            and any([os.path.exists(os.path.join(folder, "nvcuvid.h")) for folder in TORCHVISION_INCLUDE]) | 
| 485 |  | -            and any([os.path.exists(os.path.join(folder, "libnvcuvid.so")) for folder in TORCHVISION_LIBRARY]) | 
| 486 |  | -            and any([os.path.exists(os.path.join(folder, "libavcodec", "bsf.h")) for folder in ffmpeg_include_dir]) | 
| 487 |  | -        ): | 
| 488 |  | -            print("Could not find necessary dependencies. Refer the setup.py to check which ones are needed.") | 
| 489 |  | -            print("Building without GPU video decoder support") | 
| 490 |  | -            return extensions | 
| 491 |  | -        print("Building torchvision with GPU video decoder support") | 
| 492 |  | - | 
| 493 |  | -        gpu_decoder_path = os.path.join(CSRS_DIR, "io", "decoder", "gpu") | 
| 494 |  | -        gpu_decoder_src = glob.glob(os.path.join(gpu_decoder_path, "*.cpp")) | 
| 495 |  | -        cuda_libs = os.path.join(CUDA_HOME, "lib64") | 
| 496 |  | -        cuda_inc = os.path.join(CUDA_HOME, "include") | 
| 497 |  | - | 
| 498 |  | -        _, extra_compile_args = get_macros_and_flags() | 
| 499 |  | -        extensions.append( | 
| 500 |  | -            CUDAExtension( | 
| 501 |  | -                "torchvision.gpu_decoder", | 
| 502 |  | -                gpu_decoder_src, | 
| 503 |  | -                include_dirs=[CSRS_DIR] + TORCHVISION_INCLUDE + [gpu_decoder_path] + [cuda_inc] + ffmpeg_include_dir, | 
| 504 |  | -                library_dirs=ffmpeg_library_dir + TORCHVISION_LIBRARY + [cuda_libs], | 
| 505 |  | -                libraries=[ | 
| 506 |  | -                    "avcodec", | 
| 507 |  | -                    "avformat", | 
| 508 |  | -                    "avutil", | 
| 509 |  | -                    "swresample", | 
| 510 |  | -                    "swscale", | 
| 511 |  | -                    "nvcuvid", | 
| 512 |  | -                    "cuda", | 
| 513 |  | -                    "cudart", | 
| 514 |  | -                    "z", | 
| 515 |  | -                    "pthread", | 
| 516 |  | -                    "dl", | 
| 517 |  | -                    "nppicc", | 
| 518 |  | -                ], | 
| 519 |  | -                extra_compile_args=extra_compile_args, | 
| 520 |  | -            ) | 
| 521 |  | -        ) | 
| 522 |  | - | 
| 523 |  | -    return extensions | 
| 524 |  | - | 
| 525 |  | - | 
| 526 | 364 | class clean(distutils.command.clean.clean): | 
| 527 | 365 |     def run(self): | 
| 528 | 366 |         with open(".gitignore") as f: | 
| @@ -550,7 +388,6 @@ def run(self): | 
| 550 | 388 |     extensions = [ | 
| 551 | 389 |         make_C_extension(), | 
| 552 | 390 |         make_image_extension(), | 
| 553 |  | -        *make_video_decoders_extensions(), | 
| 554 | 391 |     ] | 
| 555 | 392 | 
 | 
| 556 | 393 |     setup( | 
|  | 
0 commit comments