본문 바로가기
[GPUaaS]/TensorFlow

[TF 분산학습] 스토리지 관점 + TensorFlow 내부 동작

by METAVERSE STORY 2026. 1. 26.
반응형

 

 

✅ 최종 TFJob YAML (GPU 설정 추가)

 

======================================

apiVersion: kubeflow.org/v1
kind: TFJob
metadata:
  name: tf-mnist-train
  namespace: ml
spec:
  runPolicy:
    cleanPodPolicy: None

  tfReplicaSpecs:

    # =========================
    # Chief (결과 저장 + 업로드)
    # =========================
    Chief:
      replicas: 1
      restartPolicy: OnFailure
      template:
        spec:
          containers:

          # 1️⃣ TensorFlow Trainer (GPU 사용)
          - name: trainer
            image: tensorflow/tensorflow:2.14.0-gpu
            command: ["python", "train.py"]
            resources:
              requests:
                nvidia.com/gpu: 1
              limits:
                nvidia.com/gpu: 1
            volumeMounts:
            - name: output
              mountPath: /output

          # 2️⃣ Sidecar Uploader (GPU 미사용)
          - name: uploader
            image: amazon/aws-cli
            command: ["/bin/sh", "-c"]
            args:
              - |
                while true; do
                  if [ -f /output/DONE ]; then
                    aws s3 sync /output \
                      s3://tf-result-bucket/job-$(HOSTNAME) \
                      --endpoint-url $AWS_ENDPOINT_URL
                    exit 0
                  fi
                  sleep 30
                done
            envFrom:
            - secretRef:
                name: objstore-cred
            resources:
              requests:
                nvidia.com/gpu: 0
              limits:
                nvidia.com/gpu: 0
            volumeMounts:
            - name: output
              mountPath: /output

          volumes:
          - name: output
            persistentVolumeClaim:
              claimName: tf-output-pvc

    # =========================
    # Worker (계산 전용)
    # =========================
    Worker:
      replicas: 4
      restartPolicy: OnFailure
      template:
        spec:
          containers:
          - name: trainer
            image: tensorflow/tensorflow:2.14.0-gpu
            command: ["python", "train.py"]
            resources:
              requests:
                nvidia.com/gpu: 1
              limits:
                nvidia.com/gpu: 1
            # ❌ PVC 없음 (중요)

 

================================================================

 

 

1️⃣ 먼저 결론부터 (아주 중요)

❓ Worker는 계산만 한다는데,

❓ 그럼 계산 결과를 Chief에게 어떻게 전달하냐?

👉 PVC로 전달 ❌
👉 네트워크(TensorFlow 내부 통신)로 전달 ⭕

Worker와 Chief 사이에는 “파일 전달”이 없다
Gradient / Weight는 메모리가 네트워크로 직접 이동한다

 

 


2️⃣ 그래서 최종 정정된 YAML부터 다시 보자 (RWO 기준)

✅ 최종 TFJob YAML (정상 설계)

 
apiVersion: kubeflow.org/v1
kind: TFJob
metadata:
  name: tf-mnist-train
  namespace: ml
spec:
  runPolicy:
    cleanPodPolicy: None

  tfReplicaSpecs:

    # =========================
    # Chief (결과 저장 + 업로드)
    # =========================
    Chief:
      replicas: 1
      restartPolicy: OnFailure
      template:
        spec:
          containers:

          # 1️⃣ TensorFlow Trainer
          - name: trainer
            image: tensorflow/tensorflow:2.14.0-gpu
            command: ["python", "train.py"]
            volumeMounts:
            - name: output
              mountPath: /output

          # 2️⃣ Sidecar Uploader
          - name: uploader
            image: amazon/aws-cli
            command: ["/bin/sh", "-c"]
            args:
              - |
                while true; do
                  if [ -f /output/DONE ]; then
                    aws s3 sync /output \
                      s3://tf-result-bucket/job-$(HOSTNAME) \
                      --endpoint-url $AWS_ENDPOINT_URL
                    exit 0
                  fi
                  sleep 30
                done
            envFrom:
            - secretRef:
                name: objstore-cred
            volumeMounts:
            - name: output
              mountPath: /output

          volumes:
          - name: output
            persistentVolumeClaim:
              claimName: tf-output-pvc

    # =========================
    # Worker (계산 전용)
    # =========================
    Worker:
      replicas: 4
      restartPolicy: OnFailure
      template:
        spec:
          containers:
          - name: trainer
            image: tensorflow/tensorflow:2.14.0-gpu
            command: ["python", "train.py"]
            # ❌ PVC 없음 (중요)

 

 

 


3️⃣ 그럼 진짜 핵심 질문으로 들어가자

“Worker는 계산만 한다면서, 결과는 어떻게 Chief로 가?”

답부터 말하면:

TensorFlow 분산 전략이 Pod 간 네트워크로 자동 처리한다


4️⃣ TensorFlow 분산 학습 내부 동작 (실제 흐름)

지금 구조는 거의 100% 이렇게 씀 👇

 
strategy = tf.distribute.MultiWorkerMirroredStrategy()

이 전략의 핵심은 All-Reduce야.

 

 


4-1️⃣ 전체 그림

 
Worker-0 GPU → Gradient 계산
Worker-1 GPU → Gradient 계산
Worker-2 GPU → Gradient 계산
Worker-3 GPU → Gradient 계산
        ↓
[ All-Reduce (NCCL / gRPC) ]
        ↓
모든 Worker + Chief가
동일한 Weight를 동시에 업데이트
 
 
 

📌 중앙에 파일 저장소 없음
📌 중앙에 “결과 전달용 PVC” 없음

 

 


4-2️⃣ 통신은 뭘로 하냐?

항목 실제 사용
통신 방식 TCP / gRPC
GPU 통신 NCCL
Pod 네트워크 Kubernetes CNI
포트 2222 (TF 기본)

👉 TFJob이 TF_CONFIG를 자동 주입해서
서로 어디에 있는지 다 알고 있음

 

 


5️⃣ Chief의 진짜 역할은 뭐냐?

많이들 오해하는데 👇

❌ 오해

“Chief가 Worker 계산 결과를 모아서 계산한다”

✅ 실제

모든 노드가 같은 계산을 동시에 하고
Chief는 ‘저장 책임자’ 역할만 맡는다

 


6️⃣ 그럼 왜 Chief가 필요하냐?

Chief의 역할 딱 3개

1️⃣ Checkpoint 저장

 
model.save("/output/model")
 
 

2️⃣ 학습 상태 관리

  • step
  • epoch
  • failure recovery 기준점

3️⃣ 결과 외부 반출

  • Sidecar
  • Object Storage

 

 


7️⃣ Worker는 진짜 아무것도 안 남기냐?

👉 의도적으로 안 남긴다

항목 위치
Gradient 메모리 (즉시 전송)
Weight 메모리
로그 stdout (Loki / kubectl logs)
결과 파일 ❌ 없음

 

 


8️⃣ 왜 PVC로 결과 전달을 안 하냐? (아주 중요)

만약 Worker → PVC → Chief 구조라면?

  • 파일 락
  • 동시 쓰기 충돌
  • 성능 폭망
  • GPU 놀음

👉 딥러닝 분산 학습에서는 완전한 안티패턴


9️⃣ 이 구조를 한 문장으로 정리하면

Worker는 GPU로 숫자만 계산해서 네트워크로 날리고
Chief는 계산 결과를 “저장”만 한다

 


🔟 이해 체크 (이게 자연스러우면 끝)

이 문장이 납득되면 완전 이해한 거야 👇

“분산 학습에서
데이터 이동은 네트워크로,
결과 저장은 Chief에서만 한다”

 

 

 

반응형

댓글