|
| 1 | +/* |
| 2 | + * Copyright 2022 The Kubernetes Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import Event from "./Event" |
| 18 | + |
| 19 | +type EventType = "Epoch" | "Iteration" | "Marker" |
| 20 | +type Detail = { epoch: number; step: number; nSteps: number; ip: string } |
| 21 | +type TorchEvent = Event<EventType, Detail> |
| 22 | + |
| 23 | +function findPrevious(M: TorchEvent[], ip: TorchEvent["ip"], type: EventType) { |
| 24 | + for (let idx = M.length - 1; idx >= 0; idx--) { |
| 25 | + const evt = M[idx] |
| 26 | + if (evt.type === type && evt.ip === ip) { |
| 27 | + return evt |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +function findEpoch(M: TorchEvent[], ip: TorchEvent["ip"]) { |
| 33 | + const evt = findPrevious(M, ip, "Epoch") |
| 34 | + return evt ? evt.step : -1 |
| 35 | +} |
| 36 | + |
| 37 | +function collateEvent(M: TorchEvent[], line: string) { |
| 38 | + const startMatch = line.match(/ip=([\d.]+)\)\s+(\d+\/\d+\/\d+\s+\d+:\d+:\d+)\s+.+\*\*\*\*\* Running training/) |
| 39 | + if (startMatch) { |
| 40 | + const ip = startMatch[1] |
| 41 | + const type = "Marker" |
| 42 | + const name = type |
| 43 | + const message = type |
| 44 | + const hidden = true |
| 45 | + const timestamp = new Date(startMatch[2]).getTime() |
| 46 | + const epoch = -1 |
| 47 | + const step = -1 |
| 48 | + const nSteps = -1 |
| 49 | + const state = "InProgress" |
| 50 | + M.push({ ip, name, message, state, type, hidden, timestamp, epoch, step, nSteps }) |
| 51 | + return M |
| 52 | + } |
| 53 | + |
| 54 | + const match = line.match(/ip=([\d.]+)\)\s+(Epoch|Iteration):\s+(\d+)%\|[^|]+\|\s(\d+)\/(\d+)/) |
| 55 | + if (match) { |
| 56 | + const ip = match[1] |
| 57 | + const type = match[2] as EventType |
| 58 | + // const percentage = parseInt(match[3], 10) |
| 59 | + const step = parseInt(match[4], 10) |
| 60 | + const nSteps = parseInt(match[5], 10) |
| 61 | + |
| 62 | + const epoch = type === "Epoch" ? step : findEpoch(M, ip) |
| 63 | + const timestampMarker = findPrevious(M, ip, "Marker") |
| 64 | + |
| 65 | + const event = { |
| 66 | + name: `Torch Training on ${ip}`, |
| 67 | + message: `Epoch ${epoch}${type !== "Epoch" ? ` - ${type} ${step}` : ""} of ${nSteps}`, |
| 68 | + ip, |
| 69 | + type, |
| 70 | + step, |
| 71 | + nSteps, |
| 72 | + epoch, |
| 73 | + timestamp: timestampMarker ? timestampMarker.timestamp : Date.now(), |
| 74 | + state: "InProgress" as const, |
| 75 | + } |
| 76 | + |
| 77 | + // find previous by ip and mark it Done |
| 78 | + const prev = findPrevious(M, ip, type) |
| 79 | + if (prev) { |
| 80 | + prev.state = "Done" |
| 81 | + |
| 82 | + if (type === "Epoch" && prev.step === step) { |
| 83 | + // strange, torch seems to repeat the e.g. Epoch 6/6 event... |
| 84 | + return M |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + M.push(event) |
| 89 | + } |
| 90 | + |
| 91 | + return M |
| 92 | +} |
| 93 | + |
| 94 | +function sortFn(a: TorchEvent, b: TorchEvent) { |
| 95 | + return a.ip.localeCompare(b.ip) || a.epoch - b.epoch || a.step - b.step || a.type.localeCompare(b.type) |
| 96 | +} |
| 97 | + |
| 98 | +/** @return lifecycle events (Epoch, Iteration) for Torch training */ |
| 99 | +export default function torchEvents(jobLogs: string): TorchEvent[] { |
| 100 | + return jobLogs |
| 101 | + .split(/\n/) |
| 102 | + .reduce(collateEvent, [] as TorchEvent[]) |
| 103 | + .sort(sortFn) |
| 104 | +} |
0 commit comments