All files / web/src/lib/tasks vision-training.ts

0% Statements 0/420
0% Branches 0/1
0% Functions 0/1
0% Lines 0/420

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
import { spawn, type ChildProcess } from 'child_process'
import fs from 'fs'
import path from 'path'
import { promises as fsPromises } from 'fs'
import { createId } from '@paralleldrive/cuid2'
import { db } from '@/db'
import { visionTrainingSessions } from '@/db/schema/vision-training-sessions'
import { eq, and } from 'drizzle-orm'
import { createTask, type TaskHandle } from '../task-manager'
import type { VisionTrainingEvent } from './events'
import {
  ensureVenvReady,
  isPlatformSupported,
  PYTHON_ENV,
  TRAINING_PYTHON,
} from '@/app/api/vision-training/config'

/**
 * Training configuration options
 */
export interface VisionTrainingInput {
  modelType: 'column-classifier' | 'boundary-detector'
  epochs?: number
  batchSize?: number
  validationSplit?: number
  noAugmentation?: boolean
  colorAugmentation?: boolean
  manifestId?: string
}

/**
 * Training output
 */
export interface VisionTrainingOutput {
  sessionId: string
  modelType: string
  modelPath: string
  metrics: {
    finalAccuracy?: number
    finalValAccuracy?: number
    finalLoss?: number
    finalValLoss?: number
  }
  trainingDurationSeconds: number | null
  epochCount: number
}

/**
 * Model-specific configuration
 */
const MODEL_CONFIG = {
  'column-classifier': {
    script: 'scripts/train-column-classifier/train_model.py',
    dataDir: './data/vision-training/collected',
    modelsDir: './data/vision-training/models/column-classifier',
  },
  'boundary-detector': {
    script: 'scripts/train-boundary-detector/train_model.py',
    dataDir: './data/vision-training/boundary-frames',
    modelsDir: './data/vision-training/models/boundary-detector',
  },
} as const

const MANIFESTS_DIR = path.join(process.cwd(), 'data/vision-training/manifests')

const MODEL_TYPE_TO_PUBLIC_DIR: Record<string, string> = {
  'column-classifier': 'abacus-column-classifier',
  'boundary-detector': 'abacus-boundary-detector',
}

// Track active training processes by task ID (for cancellation)
const activeProcesses = new Map<string, { process: ChildProcess; stopFilePath: string }>()

/**
 * Copy model files to public/models/
 */
async function copyModelToPublic(
  modelPath: string,
  modelType: 'column-classifier' | 'boundary-detector'
): Promise<void> {
  const sourceDir = path.join(process.cwd(), 'data/vision-training/models', modelPath)
  const targetDir = path.join(process.cwd(), 'public/models', MODEL_TYPE_TO_PUBLIC_DIR[modelType])

  await fsPromises.mkdir(targetDir, { recursive: true })

  const files = await fsPromises.readdir(sourceDir)
  for (const file of files) {
    const sourceFilePath = path.join(sourceDir, file)
    const targetPath = path.join(targetDir, file)
    const stat = await fsPromises.stat(sourceFilePath)
    if (stat.isFile()) {
      await fsPromises.copyFile(sourceFilePath, targetPath)
      console.log(`[VisionTraining] Copied ${file} to ${targetDir}`)
    }
  }
}

/**
 * Save training session to database
 */
async function saveTrainingSession(
  sessionId: string,
  modelType: 'column-classifier' | 'boundary-detector',
  config: Record<string, unknown>,
  datasetInfo: Record<string, unknown>,
  epochHistory: Array<Record<string, unknown>>,
  completeData: Record<string, unknown>
): Promise<void> {
  const modelPath = `${modelType}/${sessionId}`
  const displayName = `${modelType === 'column-classifier' ? 'Column Classifier' : 'Boundary Detector'} - ${new Date().toLocaleDateString()}`

  // Deactivate any existing active model for this type
  await db
    .update(visionTrainingSessions)
    .set({ isActive: false })
    .where(
      and(
        eq(visionTrainingSessions.modelType, modelType),
        eq(visionTrainingSessions.isActive, true)
      )
    )

  // Copy model files to public directory
  await copyModelToPublic(modelPath, modelType)

  // Create the new session
  await db.insert(visionTrainingSessions).values({
    modelType,
    displayName,
    config: config as any,
    datasetInfo: datasetInfo as any,
    result: completeData as any,
    epochHistory: epochHistory as any,
    modelPath,
    isActive: true,
    notes: null,
    tags: [],
    trainedAt: new Date(),
  })

  console.log('[VisionTraining] Session saved to database:', sessionId)
}

/**
 * Start a vision training task
 *
 * @returns Task ID that can be used to track progress
 */
export async function startVisionTraining(input: VisionTrainingInput): Promise<string> {
  // Check platform support
  const platformCheck = isPlatformSupported()
  if (!platformCheck.supported) {
    throw new Error(`Platform not supported: ${platformCheck.reason}`)
  }

  // Ensure venv is ready
  const setup = await ensureVenvReady()
  if (!setup.success) {
    throw new Error(`Python environment setup failed: ${setup.error}`)
  }

  const modelType = input.modelType || 'column-classifier'
  const modelConfig = MODEL_CONFIG[modelType]

  // Check if script exists for boundary detector
  if (modelType === 'boundary-detector') {
    const scriptPath = path.join(process.cwd(), modelConfig.script)
    if (!fs.existsSync(scriptPath)) {
      throw new Error('Boundary detector training not yet implemented')
    }
  }

  // Check manifest if provided
  if (input.manifestId) {
    const manifestPath = path.join(MANIFESTS_DIR, `${input.manifestId}.json`)
    if (!fs.existsSync(manifestPath)) {
      throw new Error(`Manifest ${input.manifestId} not found`)
    }
  }

  return createTask<VisionTrainingInput, VisionTrainingOutput, VisionTrainingEvent>(
    'vision-training',
    input,
    async (handle, config) => {
      const sessionId = createId()
      const sessionOutputDir = path.join(modelConfig.modelsDir, sessionId)
      const cwd = path.resolve(process.cwd())
      const stopFilePath = path.join(cwd, 'data', 'vision-training', `.stop-${sessionId}`)

      // Build command arguments
      const args = [
        modelConfig.script,
        '--json-progress',
        '--data-dir',
        modelConfig.dataDir,
        '--output-dir',
        sessionOutputDir,
        '--session-id',
        sessionId,
        '--stop-file',
        stopFilePath,
      ]

      if (config.epochs) args.push('--epochs', String(config.epochs))
      if (config.batchSize) args.push('--batch-size', String(config.batchSize))
      if (config.validationSplit) args.push('--validation-split', String(config.validationSplit))
      if (config.noAugmentation) args.push('--no-augmentation')
      if (config.colorAugmentation) args.push('--color-augmentation')
      if (config.manifestId) {
        args.push('--manifest-file', path.join(MANIFESTS_DIR, `${config.manifestId}.json`))
      }

      // Emit initial event
      handle.emit({
        type: 'train_started',
        sessionId,
        modelType,
        config: {
          epochs: config.epochs ?? 50,
          batchSize: config.batchSize ?? 32,
          validationSplit: config.validationSplit ?? 0.2,
          augmentation: !config.noAugmentation,
          colorAugmentation: config.colorAugmentation ?? false,
        },
      })

      // Track session data
      let datasetInfo: Record<string, unknown> | null = null
      const epochHistory: Array<Record<string, unknown>> = []
      let completeData: Record<string, unknown> | null = null
      let hardware: Record<string, unknown> | null = null
      let environment: Record<string, unknown> | null = null

      return new Promise<void>((resolve, reject) => {
        // Spawn Python process
        const proc = spawn(TRAINING_PYTHON, args, { cwd, env: PYTHON_ENV })

        // Track for cancellation
        activeProcesses.set(handle.id, { process: proc, stopFilePath })

        // Handle stdout (JSON progress events)
        proc.stdout?.on('data', (data: Buffer) => {
          // Check for cancellation
          if (handle.isCancelled()) {
            proc.kill('SIGTERM')
            return
          }

          const lines = data.toString().split('\n').filter(Boolean)
          for (const line of lines) {
            try {
              const event = JSON.parse(line)
              const eventType = event.event || 'progress'

              // Emit typed events for known Python event types,
              // use subprocess_event catch-all for unknown ones
              switch (eventType) {
                case 'training_started':
                  hardware = event.hardware || null
                  environment = event.environment || null
                  // Already emitted train_started above; skip duplicate
                  break
                case 'dataset_loaded':
                  datasetInfo = { type: modelType, ...event }
                  handle.emit({ type: 'dataset_loaded', data: event })
                  break
                case 'dataset_info':
                  datasetInfo = { type: modelType, ...event }
                  handle.emit({ type: 'dataset_info', data: event })
                  break
                case 'epoch': {
                  epochHistory.push(event)
                  const totalEpochs = config.epochs ?? 50
                  const currentEpoch = event.epoch || epochHistory.length
                  const progress = Math.round((currentEpoch / totalEpochs) * 100)
                  handle.setProgress(progress, `Epoch ${currentEpoch}/${totalEpochs}`)
                  handle.emit({
                    type: 'epoch',
                    epoch: currentEpoch,
                    totalEpochs,
                    loss: event.loss ?? 0,
                    accuracy: event.accuracy ?? 0,
                    valLoss: event.val_loss,
                    valAccuracy: event.val_accuracy,
                  })
                  break
                }
                case 'complete':
                  completeData = { type: modelType, ...event }
                  if (event.epoch_history && Array.isArray(event.epoch_history)) {
                    epochHistory.length = 0
                    epochHistory.push(...event.epoch_history)
                  }
                  if (!hardware && event.hardware) hardware = event.hardware
                  if (!environment && event.environment) environment = event.environment
                  handle.emit({ type: 'train_complete', data: event })
                  break
                default:
                  // Forward unknown Python events via catch-all
                  handle.emit({ type: 'subprocess_event', eventType, data: event })
                  break
              }
            } catch {
              // Non-JSON output, emit as log
              handle.emit({ type: 'log', message: line })
            }
          }
        })

        // Handle stderr
        proc.stderr?.on('data', (data: Buffer) => {
          const message = data.toString().trim()
          if (
            message &&
            !message.includes('successful NUMA node') &&
            !message.includes('StreamExecutor')
          ) {
            handle.emit({ type: 'log', message, source: 'stderr' })
          }
        })

        // Handle process exit
        proc.on('close', async (code) => {
          activeProcesses.delete(handle.id)

          // Clean up stop file
          try {
            if (fs.existsSync(stopFilePath)) {
              fs.unlinkSync(stopFilePath)
            }
          } catch {
            // Ignore cleanup errors
          }

          if (code === 0 && completeData && datasetInfo) {
            try {
              // Save to database
              await saveTrainingSession(
                sessionId,
                modelType,
                {
                  epochs: config.epochs ?? 50,
                  batchSize: config.batchSize ?? 32,
                  validationSplit: config.validationSplit ?? 0.2,
                  colorAugmentation: config.colorAugmentation ?? false,
                  hardware,
                  environment,
                },
                datasetInfo,
                epochHistory,
                completeData
              )

              // Complete the task with output
              const lastEpoch = epochHistory[epochHistory.length - 1] as
                | Record<string, number>
                | undefined
              handle.complete({
                sessionId,
                modelType,
                modelPath: `${modelType}/${sessionId}`,
                metrics: {
                  finalAccuracy: lastEpoch?.accuracy,
                  finalValAccuracy: lastEpoch?.val_accuracy,
                  finalLoss: lastEpoch?.loss,
                  finalValLoss: lastEpoch?.val_loss,
                },
                trainingDurationSeconds: (completeData as any).training_duration_seconds ?? null,
                epochCount: epochHistory.length,
              })
              resolve()
            } catch (err) {
              reject(err)
            }
          } else if (handle.isCancelled()) {
            // Task was cancelled, don't fail
            resolve()
          } else {
            reject(new Error(`Training failed with exit code ${code}`))
          }
        })

        proc.on('error', (error) => {
          activeProcesses.delete(handle.id)
          reject(error)
        })
      })
    }
  )
}

/**
 * Request early stop for a training task (saves model gracefully)
 */
export function requestEarlyStop(taskId: string): boolean {
  const active = activeProcesses.get(taskId)
  if (!active) return false

  try {
    fs.writeFileSync(active.stopFilePath, 'stop', { encoding: 'utf-8' })
    return true
  } catch {
    return false
  }
}

/**
 * Check if a training task is running on this pod
 */
export function isTrainingRunningLocally(taskId: string): boolean {
  const active = activeProcesses.get(taskId)
  return active !== undefined && !active.process.killed
}

/**
 * Get count of locally running training tasks
 */
export function getLocalTrainingCount(): number {
  return activeProcesses.size
}