Run this model with HF Jobs

Run PRiyanshu0-1/DPDP-SSense on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.

Paste one of these prompts into your coding agent:

Fine-tune on my dataset

Use HF Jobs and TRL to fine-tune https://huggingface.co/PRiyanshu0-1/DPDP-SSense. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.

Run batch inference

Use HF Jobs to run batch inference with https://huggingface.co/PRiyanshu0-1/DPDP-SSense on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.

Evaluate on a benchmark

Use HF Jobs to evaluate https://huggingface.co/PRiyanshu0-1/DPDP-SSense. Ask me which benchmark to run, then save the scores and the per-example results to a new dataset repo. Run `hf jobs --help` first to learn the CLI.
File size: 7,197 Bytes
1f52672
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env bash
# ==============================================================================
# 02_train_models.sh – Stage 2: VRAM Airlock & Unsloth SLM Fine-Tuning (DGX Spark)
# ==============================================================================
# ⚠️ SPECIALIZED PIPELINE NOTICE:
# This script is exclusively engineered for training the specialized Indian Digital Personal Data Protection (DPDP) Act
# 2023 & Rules 2025 legal models (Forensic Auditor & Conversational Chatbot) on NVIDIA DGX Spark infrastructure (128 GB VRAM).
# It executes our 32-bit FP32 `adamw_torch` + `rsLoRA` + `SimPO` pipeline on `Qwen2.5-7B-Instruct`. Not a generic wrapper.
#
# Prerequisites for DGX Spark Execution:
#   - NVIDIA DGX / GPU environment (`multiprocessing.set_start_method("spawn")` enabled)
#   - Python 3.10+ / 3.12 with CUDA 12.x/13.x support
#   - Packages installed (`pip install -r ml/requirements.txt`): unsloth, trl, transformers, vllm, datasets, torch
# ==============================================================================

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ML_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
REPO_ROOT="$(cd "${ML_DIR}/.." && pwd)"
TRAINING_DIR="${ML_DIR}/slm-training"

PYTHON_CMD="python"
if command -v python3 &> /dev/null; then
    PYTHON_CMD="python3"
fi

MODEL_TARGET="all"

while [[ $# -gt 0 ]]; do
    case $1 in
        --model)
            MODEL_TARGET="$2"
            shift 2
            ;;
        -h|--help)
            echo "Usage: bash ml/scripts/02_train_models.sh [options]"
            echo "Options:"
            echo "  --model <audit|chatbot|all>   Specify which model(s) to train (default: all)"
            echo "  -h, --help                    Show this help message and exit"
            exit 0
            ;;
        *)
            echo "❌ Unknown option: $1"
            echo "Use --help for available options."
            exit 1
            ;;
    esac
done

if [[ "${MODEL_TARGET}" != "audit" && "${MODEL_TARGET}" != "chatbot" && "${MODEL_TARGET}" != "all" ]]; then
    echo "❌ Invalid --model target: '${MODEL_TARGET}'. Must be 'audit', 'chatbot', or 'all'."
    exit 1
fi

if [[ ! -d "${TRAINING_DIR}" ]]; then
    echo "❌ FATAL: Training directory not found at ${TRAINING_DIR}"
    exit 1
fi

TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
export LOG_DIR="${REPO_ROOT}/logs"
export PYTHONUNBUFFERED=1
mkdir -p "${LOG_DIR}"

SCRIPT_NAME=$(basename "$0" .sh)
MASTER_LOG="${LOG_DIR}/${SCRIPT_NAME}_${TIMESTAMP}.log"
exec > >(tee -i "${MASTER_LOG}")
exec 2>&1

# ==============================================================================
# FUNCTION: VRAM AIRLOCK SANITIZATION
# ==============================================================================
vram_airlock() {
    echo -e "\n════════════════════════════════════════════════════════════════════════════════"
    echo "🧹 VRAM AIRLOCK: PURGING RESIDUAL GPU & POSIX SHARED MEMORY"
    echo "════════════════════════════════════════════════════════════════════════════════"

    echo "   [1/5] Terminating Ray background daemons..."
    ray stop -f 2>/dev/null || true

    echo "   [2/5] Hunting zombie vLLM and orphaned inference processes..."
    pkill -9 -f vllm 2>/dev/null || true
    pkill -9 -f vllm.entrypoints.openai.api_server 2>/dev/null || true
    pkill -9 -f ray:: 2>/dev/null || true

    echo "   [3/5] Neutralizing orphaned PyTorch multiprocessing spawned workers..."
    pkill -9 -f "multiprocessing.spawn" 2>/dev/null || true

    echo "   [4/5] Flushing POSIX shared memory allocations..."
    rm -rf /dev/shm/* 2>/dev/null || true

    echo "   [5/5] Checking OS file cache drop permissions..."
    if command -v sudo &> /dev/null && sudo -n true 2>/dev/null; then
        sudo sync; echo 3 | sudo tee /proc/sys/vm/drop_caches > /dev/null || true
    else
        echo "         (Skipping OS drop_caches: non-root/non-sudo context)"
    fi

    echo "⏳ Waiting 5 seconds for Nvidia GPU memory state stabilization..."
    sleep 5
    echo "βœ… VRAM Airlock complete. Environment strictly isolated."
}

echo "════════════════════════════════════════════════════════════════════════════════"
echo "πŸš€ INITIATING STAGE 2: VRAM AIRLOCK & SLM TRAINING PIPELINE"
echo "════════════════════════════════════════════════════════════════════════════════"
echo "   β€’ Repository Root: ${REPO_ROOT}"
echo "   β€’ ML Root:         ${ML_DIR}"
echo "   β€’ Python Binary:   $(command -v "${PYTHON_CMD}")"
echo "   β€’ Execution Dir:   ${TRAINING_DIR}"
echo "   β€’ Logs Directory:  ${LOG_DIR}"
echo "   β€’ Training Target: ${MODEL_TARGET}"
echo "════════════════════════════════════════════════════════════════════════════════"

# Execute Initial Pre-Training Airlock
vram_airlock

# ------------------------------------------------------------------------------
# SLM FINE-TUNING EXECUTION
# ------------------------------------------------------------------------------
cd "${TRAINING_DIR}"

if [[ "${MODEL_TARGET}" == "audit" || "${MODEL_TARGET}" == "all" ]]; then
    echo -e "\n▢️  [TRAINING 1/2]: Executing Forensic Auditor Fine-Tuning (train_audit.py)..."
    "${PYTHON_CMD}" -u train_audit.py
fi

if [[ "${MODEL_TARGET}" == "all" ]]; then
    # 🚨 CRITICAL: Re-engage the VRAM Airlock between models to guarantee the 
    # Chatbot starts with 100% clean memory and zero PyTorch fragmentation.
    vram_airlock
fi

if [[ "${MODEL_TARGET}" == "chatbot" || "${MODEL_TARGET}" == "all" ]]; then
    echo -e "\n▢️  [TRAINING 2/2]: Executing Conversational Chatbot Fine-Tuning (train_chatbot.py)..."
    "${PYTHON_CMD}" -u train_chatbot.py
fi

echo -e "\n════════════════════════════════════════════════════════════════════════════════"
echo "βœ… STAGE 2 COMPLETE: Selected SLM models successfully fine-tuned and exported."
echo "════════════════════════════════════════════════════════════════════════════════"
echo "πŸ“ Execution logs saved to: ${LOG_DIR}"
echo "πŸ‘‰ Next step: Run 'bash ml/scripts/03_evaluate_models.sh' to execute functional & adversarial certification."