|
| 1 | +#ifndef __SD_CORE_LAYER_SPLIT_RANGES_H__ |
| 2 | +#define __SD_CORE_LAYER_SPLIT_RANGES_H__ |
| 3 | + |
| 4 | +#include <algorithm> |
| 5 | +#include <cstddef> |
| 6 | +#include <cstdint> |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +namespace sd { |
| 10 | + |
| 11 | + struct LayerSplitRangesResult { |
| 12 | + bool ok = false; |
| 13 | + // Device index per segment (contiguous, non-decreasing). |
| 14 | + std::vector<size_t> device_by_segment; |
| 15 | + // Weight bytes placed on each device. |
| 16 | + std::vector<int64_t> bytes_by_device; |
| 17 | + // On failure: the segment that did not fit, the device it was tried on and the bytes that device would need. |
| 18 | + size_t failed_segment = 0; |
| 19 | + size_t failed_device = 0; |
| 20 | + int64_t failed_need = 0; |
| 21 | + // True when the proportional plan did not fit and the fill-first plan was used instead. |
| 22 | + bool used_fill_first = false; |
| 23 | + }; |
| 24 | + |
| 25 | + // Fill-first: each device takes segments until the next one would exceed its capacity. |
| 26 | + inline LayerSplitRangesResult plan_layer_split_ranges_fill_first(const std::vector<int64_t>& segment_bytes, |
| 27 | + const std::vector<int64_t>& capacities) { |
| 28 | + LayerSplitRangesResult r; |
| 29 | + r.device_by_segment.assign(segment_bytes.size(), 0); |
| 30 | + r.bytes_by_device.assign(capacities.size(), 0); |
| 31 | + if (capacities.empty()) { |
| 32 | + return r; |
| 33 | + } |
| 34 | + size_t dev = 0; |
| 35 | + int64_t used = 0; |
| 36 | + for (size_t s = 0; s < segment_bytes.size(); s++) { |
| 37 | + const int64_t bytes = segment_bytes[s]; |
| 38 | + while (dev + 1 < capacities.size() && bytes > 0 && used + bytes > capacities[dev]) { |
| 39 | + dev++; |
| 40 | + used = 0; |
| 41 | + } |
| 42 | + if (bytes > 0 && used + bytes > capacities[dev]) { |
| 43 | + r.failed_segment = s; |
| 44 | + r.failed_device = dev; |
| 45 | + r.failed_need = used + bytes; |
| 46 | + return r; |
| 47 | + } |
| 48 | + used += bytes; |
| 49 | + r.device_by_segment[s] = dev; |
| 50 | + r.bytes_by_device[dev] += bytes; |
| 51 | + } |
| 52 | + r.ok = true; |
| 53 | + return r; |
| 54 | + } |
| 55 | + |
| 56 | + // Contiguous ranges sized in proportion to each device's capacity, so every device keeps the same fraction of its |
| 57 | + // budget free for the compute buffers of the segments it runs. A fill-first plan packs the main device up to its |
| 58 | + // weight capacity and leaves its compute buffers no room; it is only used when the proportional plan does not fit. |
| 59 | + inline LayerSplitRangesResult plan_layer_split_ranges(const std::vector<int64_t>& segment_bytes, |
| 60 | + const std::vector<int64_t>& capacities) { |
| 61 | + LayerSplitRangesResult r; |
| 62 | + r.device_by_segment.assign(segment_bytes.size(), 0); |
| 63 | + r.bytes_by_device.assign(capacities.size(), 0); |
| 64 | + if (capacities.empty()) { |
| 65 | + return r; |
| 66 | + } |
| 67 | + double total = 0.0; |
| 68 | + for (int64_t b : segment_bytes) { |
| 69 | + total += (double)(b > 0 ? b : 0); |
| 70 | + } |
| 71 | + double capacity_sum = 0.0; |
| 72 | + std::vector<double> caps(capacities.size(), 0.0); |
| 73 | + for (size_t i = 0; i < capacities.size(); i++) { |
| 74 | + // An unknown capacity (very large sentinel) counts as able to hold everything. |
| 75 | + caps[i] = capacities[i] > 0 ? std::min((double)capacities[i], total) : 0.0; |
| 76 | + capacity_sum += caps[i]; |
| 77 | + } |
| 78 | + if (capacities.size() == 1 || total <= 0.0 || capacity_sum <= 0.0) { |
| 79 | + return plan_layer_split_ranges_fill_first(segment_bytes, capacities); |
| 80 | + } |
| 81 | + |
| 82 | + // Cumulative byte boundary at which each device's range ends. |
| 83 | + std::vector<double> boundary(capacities.size(), total); |
| 84 | + double acc = 0.0; |
| 85 | + for (size_t i = 0; i + 1 < capacities.size(); i++) { |
| 86 | + acc += total * caps[i] / capacity_sum; |
| 87 | + boundary[i] = acc; |
| 88 | + } |
| 89 | + |
| 90 | + size_t dev = 0; |
| 91 | + int64_t used = 0; |
| 92 | + double consumed = 0.0; |
| 93 | + bool fits = true; |
| 94 | + for (size_t s = 0; s < segment_bytes.size(); s++) { |
| 95 | + const int64_t bytes = segment_bytes[s]; |
| 96 | + if (bytes > 0) { |
| 97 | + // Move on once this segment's midpoint lies past the current device's share, or it would not fit. |
| 98 | + while (dev + 1 < capacities.size() && |
| 99 | + (consumed + bytes * 0.5 > boundary[dev] || used + bytes > capacities[dev])) { |
| 100 | + dev++; |
| 101 | + used = 0; |
| 102 | + } |
| 103 | + if (used + bytes > capacities[dev]) { |
| 104 | + fits = false; |
| 105 | + break; |
| 106 | + } |
| 107 | + } |
| 108 | + used += bytes; |
| 109 | + consumed += (double)(bytes > 0 ? bytes : 0); |
| 110 | + r.device_by_segment[s] = dev; |
| 111 | + r.bytes_by_device[dev] += bytes; |
| 112 | + } |
| 113 | + if (fits) { |
| 114 | + r.ok = true; |
| 115 | + return r; |
| 116 | + } |
| 117 | + LayerSplitRangesResult fallback = plan_layer_split_ranges_fill_first(segment_bytes, capacities); |
| 118 | + fallback.used_fill_first = true; |
| 119 | + return fallback; |
| 120 | + } |
| 121 | + |
| 122 | +} // namespace sd |
| 123 | + |
| 124 | +#endif // __SD_CORE_LAYER_SPLIT_RANGES_H__ |
0 commit comments