GitHub Actions لسير عمل التعلم الآلي
خطوط أنابيب التدريب في Actions
4 دقيقة للقراءة
مهام تدريب ML لها متطلبات فريدة: حوسبة كبيرة، أوقات تشغيل طويلة، وموارد مكلفة. دعنا نبني خطوط أنابيب تدريب تتعامل مع هذه التحديات بكفاءة.
Matrix Builds للبحث عن المعاملات الفائقة
اختبار تكوينات متعددة بالتوازي:
name: Hyperparameter Search
on: workflow_dispatch
jobs:
train-matrix:
runs-on: ubuntu-latest
strategy:
matrix:
learning_rate: [0.001, 0.01, 0.1]
batch_size: [32, 64, 128]
fail-fast: false # لا تلغِ المهام الأخرى إذا فشلت واحدة
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Train with config
run: |
python train.py \
--learning-rate ${{ matrix.learning_rate }} \
--batch-size ${{ matrix.batch_size }}
- name: Upload results
uses: actions/upload-artifact@v4
with:
name: results-lr${{ matrix.learning_rate }}-bs${{ matrix.batch_size }}
path: results/
هذا ينشئ 9 مهام متوازية (3 معدلات تعلم × 3 أحجام دفعات).
Matrix مع Include وExclude
ضبط دقيق لأي مجموعات تُشغّل:
strategy:
matrix:
model: [xgboost, lightgbm, catboost]
dataset: [small, medium, large]
exclude:
# تخطي المجموعات المكلفة
- model: catboost
dataset: large
include:
# إضافة حالة اختبار محددة
- model: xgboost
dataset: large
special_flag: "--early-stopping 10"
تخزين التبعيات مؤقتاً
تسريع سير العمل بالتخزين المؤقت:
jobs:
train:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# تخزين حزم pip مؤقتاً
- uses: actions/setup-python@v5
with:
python-version: '3.11'
cache: 'pip'
# تخزين مخرجات النماذج من التشغيلات السابقة مؤقتاً
- name: Cache trained models
uses: actions/cache@v4
with:
path: ~/.cache/models
key: models-${{ hashFiles('data/train.parquet') }}
restore-keys: |
models-
# تخزين نماذج HuggingFace مؤقتاً
- name: Cache HuggingFace
uses: actions/cache@v4
with:
path: ~/.cache/huggingface
key: hf-${{ hashFiles('requirements.txt') }}
- run: pip install -r requirements.txt
- run: python train.py
GPU Runners ذاتية الاستضافة
لتدريب GPU، أعد runners ذاتية الاستضافة:
الخيار 1: استضافة ذاتية على بنيتك التحتية
jobs:
train-gpu:
runs-on: [self-hosted, linux, gpu] # تسميات مخصصة
steps:
- uses: actions/checkout@v4
- name: Check GPU availability
run: nvidia-smi
- name: Train on GPU
run: |
python train.py --device cuda
env:
CUDA_VISIBLE_DEVICES: "0"
الخيار 2: Cloud GPU Runners (طرف ثالث)
عدة خدمات توفر GPU runners:
jobs:
train-gpu:
runs-on: gpu-runner-nvidia-t4 # تسمية مثال
steps:
- name: Train on cloud GPU
run: python train.py --device cuda
إعداد Runner ذاتي الاستضافة
# على خادم GPU الخاص بك
mkdir actions-runner && cd actions-runner
curl -o actions-runner.tar.gz -L https://github.com/actions/runner/releases/download/v2.311.0/actions-runner-linux-x64-2.311.0.tar.gz
tar xzf actions-runner.tar.gz
# التكوين مع مستودعك
./config.sh --url https://github.com/OWNER/REPO --token YOUR_TOKEN --labels gpu,linux
# التثبيت والبدء كخدمة
sudo ./svc.sh install
sudo ./svc.sh start
التعامل مع المهام طويلة التشغيل
التدريب قد يستغرق ساعات. تعامل مع انتهاء المهلة بشكل صحيح:
jobs:
train:
runs-on: [self-hosted, gpu]
timeout-minutes: 360 # 6 ساعات كحد أقصى
steps:
- name: Train with checkpointing
run: |
python train.py \
--checkpoint-dir checkpoints/ \
--save-every 1000
continue-on-error: true # لا تفشل فوراً
- name: Upload checkpoints on any outcome
if: always()
uses: actions/upload-artifact@v4
with:
name: checkpoints
path: checkpoints/
- name: Resume from checkpoint
if: failure()
run: |
python train.py --resume checkpoints/latest.pt
معالجة البيانات المتوازية
تقسيم معالجة البيانات عبر المهام:
jobs:
prepare-splits:
runs-on: ubuntu-latest
outputs:
splits: ${{ steps.split.outputs.splits }}
steps:
- name: Calculate splits
id: split
run: |
echo 'splits=["0-1000", "1000-2000", "2000-3000"]' >> $GITHUB_OUTPUT
process:
needs: prepare-splits
runs-on: ubuntu-latest
strategy:
matrix:
split: ${{ fromJson(needs.prepare-splits.outputs.splits) }}
steps:
- name: Process data split
run: |
python process.py --range "${{ matrix.split }}"
aggregate:
needs: process
runs-on: ubuntu-latest
steps:
- name: Combine results
run: python combine.py
إدارة الموارد
التحكم في استخدام الموارد:
jobs:
train:
runs-on: ubuntu-latest
concurrency:
group: training-${{ github.ref }}
cancel-in-progress: true # إلغاء التشغيلات السابقة
steps:
- name: Train with resource limits
run: |
python train.py
env:
OMP_NUM_THREADS: 4
MKL_NUM_THREADS: 4
خط أنابيب التدريب الكامل
name: Full Training Pipeline
on:
push:
branches: [main]
schedule:
- cron: '0 2 * * 0' # إعادة تدريب أسبوعية
jobs:
prepare:
runs-on: ubuntu-latest
outputs:
data_hash: ${{ steps.hash.outputs.hash }}
steps:
- uses: actions/checkout@v4
- id: hash
run: echo "hash=$(sha256sum data/train.parquet | cut -d' ' -f1)" >> $GITHUB_OUTPUT
train:
needs: prepare
runs-on: [self-hosted, gpu]
timeout-minutes: 240
strategy:
matrix:
model: [baseline, improved]
steps:
- uses: actions/checkout@v4
- name: Check cache
uses: actions/cache@v4
with:
path: models/
key: model-${{ matrix.model }}-${{ needs.prepare.outputs.data_hash }}
- name: Train if not cached
run: |
if [ ! -f models/${{ matrix.model }}.pkl ]; then
python train.py --model ${{ matrix.model }}
fi
- uses: actions/upload-artifact@v4
with:
name: model-${{ matrix.model }}
path: models/
compare:
needs: train
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
- name: Compare models
run: python compare_models.py
- name: Select best model
run: python select_best.py >> $GITHUB_STEP_SUMMARY
الرؤية الرئيسية: استخدم matrix builds للتجريب، التخزين المؤقت للسرعة، وrunners ذاتية الاستضافة لوصول GPU. احفظ نقاط تحقق دائماً للمهام طويلة التشغيل.
التالي، سنستكشف سير عمل التحقق من النماذج. :::