DVC Pipelines

CI/CD สำหรับ Machine Learning

Ravi Bhadauria

Machine Learning Engineer

ความจำเป็นของ data pipeline

  • การ version ข้อมูลเพียงอย่างเดียวไม่เพียงพอ
  • การติดตาม data lineage มีความสำคัญ
    • การกรอง ทำความสะอาด และแปลงข้อมูล
    • การฝึกโมเดล
  • รันเฉพาะขั้นตอนที่จำเป็น
  • ขั้นตอนใน Directed Acyclic Graph (DAG)

แผนผังแสดงความสัมพันธ์ระหว่างการ preprocessing โมเดล การฝึก และ artifacts

CI/CD สำหรับ Machine Learning

DVC pipelines

  • ลำดับขั้นตอนที่กำหนด ML workflow และ dependencies

  • กำหนดในไฟล์ dvc.yaml

    • ข้อมูลนำเข้าและสคริปต์ (deps)
    • คำสั่งรันแต่ละขั้นตอน (cmd)
    • ผลลัพธ์ที่ได้ (outs)
      • ข้อมูลพิเศษ เช่น metrics และ plots
  • คล้ายกับ GitHub Actions workflow

    • มุ่งเน้น ML แทน CI/CD
    • สามารถแยกเป็น step ใน GHA ได้
CI/CD สำหรับ Machine Learning

การกำหนด pipeline stages

  • สร้าง stage ด้วย dvc stage add
dvc stage add \
-n preprocess \
-d raw_data.csv -d preprocess.py \
-o processed_data.csv \
python preprocess.py
  • เนื้อหาในไฟล์ dvc.yaml
    stages:
    preprocess:
      cmd: python preprocess.py
      deps:
      - preprocess.py
      - raw_data.csv
      outs:
      - processed_data.csv
    
CI/CD สำหรับ Machine Learning

กราฟ dependencies

  • เพิ่มขั้นตอนการฝึกโดยใช้ผลลัพธ์จากขั้นตอนก่อนหน้า
    dvc stage add \
    -n train \
    -d train.py -d processed_data.csv \
    -o plots.png -o metrics.txt \
    python train.py
    
stages:
  preprocess:
    cmd: python preprocess.py
    deps:
    - preprocess.py
    - raw_data.csv
    outs:
    - processed_data.csv

train: cmd: python train.py deps: - processed_data.csv - train.py outs: - plots.png
CI/CD สำหรับ Machine Learning

การ reproduce pipeline

  • รัน pipeline ใหม่ด้วย dvc repro
-> dvc repro
Running stage 'preprocess':                                      
> python preprocess.py

Running stage 'train': > python train.py Updating lock file 'dvc.lock'
  • ระบบจะสร้างไฟล์ dvc.lock
    • คล้ายกับไฟล์ .dvc บันทึก MD5 hashes
    • Commit ลง Git ทันทีเพื่อบันทึกสถานะ git add dvc.lock && git commit -m "first pipeline repro"`
CI/CD สำหรับ Machine Learning

การใช้ผลลัพธ์ที่แคชไว้

  • ใช้ผลลัพธ์ที่แคชไว้เพื่อเพิ่มความเร็วในการทำซ้ำ
-> dvc repro
Stage 'preprocess' didn't change, skipping
Running stage 'train' with command: ...
CI/CD สำหรับ Machine Learning

การ visualize DVC pipeline

-> dvc dag
+------------+ 
| preprocess | 
+------------+ 
       *       
       *       
       *       
  +-------+    
  | train |    
  +-------+
1 https://dvc.org/doc/command-reference/dag
CI/CD สำหรับ Machine Learning

สรุป

  • DVC pipelines มีประโยชน์ในด้าน
    • การทดสอบซ้ำเร็วขึ้นด้วยการแคช
    • ความสามารถในการ reproduce ด้วย dvc.yaml และ dvc.lock
    • รองรับ CI/CD
  • สร้าง pipeline ด้วย dvc stage add
  • Reproduce/รัน ด้วย dvc repro
  • Visualize ด้วย dvc dag
CI/CD สำหรับ Machine Learning

มาฝึกกันเถอะ!

CI/CD สำหรับ Machine Learning

Preparing Video For Download...