본문으로 건너뛰기

[pytorch] Build: vendored_templates 디렉토리에 __init__.py 자동 생성으로 패키지 인식 문제 해결

PR 링크: pytorch/pytorch#172116 상태: Merged | 변경: +4 / -0

들어가며

Python 패키지 시스템에서 디렉토리가 서브모듈로 인식되려면 __init__.py 파일이 필요합니다. PyTorch Inductor의 CuTeDSL Grouped MM 템플릿은 빌드 시 vendored_templates 디렉토리에 외부 커널 파일을 복사하는데, 이 디렉토리에 __init__.py가 없어 find_packages()가 이를 서브모듈로 인식하지 못하는 문제가 있었습니다.

핵심 코드 분석

setup.py에 init.py 자동 생성 로직 추가

Before:

def mirror_inductor_external_kernels() -> None:
    # ...
    if not new_path.exists():
        new_path.parent.mkdir(parents=True, exist_ok=True)

    # Copy the files from the orig location to the new location
    if orig_path.is_file():

After:

def mirror_inductor_external_kernels() -> None:
    # ...
    if not new_path.exists():
        new_path.parent.mkdir(parents=True, exist_ok=True)
        # Add `__init__.py` for find_packages to see `new_path.parent` as a submodule
        (new_path.parent / "__init__.py").touch(exist_ok=True)

    # Copy the files from the orig location to the new location
    if orig_path.is_file():

mkdir 직후에 __init__.pytouch(exist_ok=True)로 생성합니다. exist_ok=True는 파일이 이미 존재할 때 에러를 방지합니다.

테스트 예외 목록에 추가

# test_public_bindings.py
"torch._inductor.kernel.vendored_templates.cutedsl_grouped_gemm",  # depends on cutlass_cppgen

# test_testing.py
"torch._inductor.kernel.vendored_templates.cutedsl_grouped_gemm",  # depends on cutlass

이 모듈은 CUTLASS 의존성이 있어 단독으로 임포트할 수 없으므로, public bindings 테스트와 circular dependency 테스트의 예외 목록에 추가되었습니다.

왜 이게 좋은가

이 문제는 빌드 시스템과 Python 패키징의 교차점에서 발생하는 전형적인 이슈입니다. find_packages()__init__.py가 없는 디렉토리를 무시하므로, 빌드는 성공하지만 설치된 패키지에 해당 모듈이 누락됩니다. 4줄의 수정이지만, 이를 놓치면 런타임에 ModuleNotFoundError가 발생하여 디버깅이 어려울 수 있습니다.

정리

  • vendored_templates 디렉토리 생성 시 __init__.py 자동 생성
  • find_packages()가 CuTeDSL 템플릿 서브모듈을 정상 인식
  • CUTLASS 의존 모듈을 테스트 예외 목록에 추가

참고 자료

⚠️ 알림: 이 분석은 AI가 실제 코드 diff를 기반으로 작성했습니다.

댓글

관련 포스트

PR Analysis 의 다른글