

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# @remote 데코레이터와 함께 모듈식 코드 사용하기
<a name="train-remote-decorator-modular"></a>

개발 중에 작업 공간을 쉽게 관리할 수 있도록 모듈로 코드를 구성하고 계속해서 @remote 함수를 사용하여 함수를 호출할 수 있습니다. 개발 환경에서 원격 작업 환경으로 로컬 모듈을 복제할 수도 있습니다. 이렇게 하려면 다음 코드 예제에서와 같이 파라미터 `include_local_workdir`를 `True`로 설정합니다.

```
@remote(
  include_local_workdir=True,
)
```

**참고**  
@remote 데코레이터 및 파라미터는 종속 파일이 아닌 기본 파일에 나타나야 합니다.

`include_local_workdir`가 `True`로 설정된 경우 SageMaker AI는 모든 Python 스크립트를 패키징하면서 프로세스 현재 디렉터리의 디렉터리 구조를 유지합니다. 또한 해당 작업의 작업 디렉터리에서 종속성을 사용할 수 있습니다.

예를 들어, MNIST 데이터세트를 처리하는 Python 스크립트가 스크립트와 종속 `main.py` 스크립트로 분할되어 있다고 가정해 보겠습니다. `main.py`는 종속 `pytorch_mnist.py` 스크립트를 호출합니다. 또한 `main.py` 스크립트에는 그림과 같이 종속성을 가져오는 코드가 포함되어 있습니다.

```
from mnist_impl.pytorch_mnist import ...
```

`main.py` 파일에는 `@remote` 데코레이터도 포함되어야 하며 `include_local_workdir` 파라미터를 `True`로 설정해야 합니다.

`include_local_workdir` 파라미터는 기본적으로 디렉터리의 모든 Python 스크립트를 포함합니다. 이 파라미터를 `custom_file_filter` 파라미터와 함께 사용하여 작업에 업로드할 파일을 사용자 지정할 수 있습니다. S3에 업로드할 작업 종속성을 필터링하는 함수 또는 원격 함수에서 무시할 로컬 디렉터리와 파일을 지정하는 `CustomFileFilter` 객체를 전달할 수 있습니다. `include_local_workdir`가 `True`로 설정된 경우에만 `custom_file_filter`를 사용할 수 있습니다. 그렇지 않으면 파라미터가 무시됩니다.

다음 예제에서는 `CustomFileFilter`를 사용하여 S3에 파일을 업로드할 때 `data` 이름이 지정된 모든 노트북 파일과 폴더 또는 파일을 무시합니다.

```
@remote(
   include_local_workdir=True,
   custom_file_filter=CustomFileFilter(
      ignore_name_patterns=[ # files or directories to ignore
        "*.ipynb", # all notebook files
        "data", # folter or file named data
      ]
   )
)
```

다음 예제에서는 전체 워크스페이스를 패키징하는 방법을 보여줍니다.

```
@remote(
   include_local_workdir=True,
   custom_file_filter=CustomFileFilter(
      ignore_pattern_names=[] # package whole workspace
   )
)
```

다음 예제에서는 함수를 사용하여 파일을 필터링하는 방법을 보여줍니다.

```
import os

def my_filter(path: str, files: List[str]) -> List[str]:
    to_ignore = []
   for file in files:
       if file.endswith(".txt") or file.endswith(".ipynb"):
           to_ignore.append(file)
   return to_ignore

@remote(
   include_local_workdir=True,
   custom_file_filter=my_filter
)
```

## 작업 디렉터리 구조화의 모범 사례
<a name="train-remote-decorator-modular-bestprac"></a>

다음 모범 사례는 모듈식 코드에서 `@remote` 데코레이터를 사용하는 동안 디렉터리 구조를 구성하는 방법을 제안합니다.
+ @remote 데코레이터를 작업 영역의 루트 수준 디렉터리에 있는 파일에 넣습니다.
+ 루트 수준의 로컬 모듈을 구성합니다.

다음 예제 이미지는 권장 디렉터리 구조를 보여줍니다. 이 예제 구조에서 `main.py` 스크립트는 루트 수준 디렉터리에 있습니다.

```
.
├── config.yaml
├── data/
├── main.py <----------------- @remote used here 
├── mnist_impl
│ ├── __pycache__/
│ │ └── pytorch_mnist.cpython-310.pyc
│ ├── pytorch_mnist.py <-------- dependency of main.py
├── requirements.txt
```

다음 예제 이미지는 @remote 데코레이터로 코드에 주석을 달 때 동작이 일관되지 않은 디렉터리 구조를 보여줍니다.

이 예제 구조에서 @remote 데코레이터가 포함된 `main.py` 스크립트는 루트 수준 디렉터리에 있지 **않습니다**. 다음 구조는 권장하지 **않습니다**.

```
.
├── config.yaml
├── entrypoint
│ ├── data
│ └── main.py <----------------- @remote used here
├── mnist_impl
│ ├── __pycache__
│ │ └── pytorch_mnist.cpython-310.pyc
│ └── pytorch_mnist.py <-------- dependency of main.py
├── requirements.txt
```