Skip to content

Commit 9fb4ef5

Browse files
committed
Raise bucket weights to the power four in the historical model
Utilizing the results of probes sent once a minute to a random node in the network for a random amount (within a reasonable range), we were able to analyze the accuracy of our resulting success probability estimation with various PDFs across the historical and live-bounds models. For each candidate PDF (as well as other parameters, including the histogram bucket weight), we used the `min_zero_implies_no_successes` fudge factor in `success_probability` as well as a total probability multiple fudge factor to get both the historical success model and the a priori model to be neither too optimistic nor too pessimistic (as measured by the relative log-loss between succeeding and failing hops in our sample data). We then compared the resulting log-loss for the historical success model and selected the candidate PDF with the lowest log-loss, skipping a few candidates with similar resulting log-loss but with more extreme constants (such as a power of 11 with a higher `min_zero_implies_no_successes` penalty). Somewhat surprisingly (to me at least), the (fairly strongly) preferred model was one where the bucket weights in the historical histograms are exponentiated. In the current design, the weights are effectively squared as we multiply the minimum- and maximum- histogram buckets together before adding the weight*probabilities together. Here we multiply the weights yet again before addition. While the simulation runs seemed to prefer a slightly stronger weight than the 4th power we do here, the difference wasn't substantial (log-loss 0.5058 to 0.4941), so we do the simpler single extra multiply here. Note that if we did this naively we'd run out of bits in our arithmetic operations - we have 16-bit buckets, which when raised to the 4th can fully fill a 64-bit int. Additionally, when looking at the 0th min-bucket we occasionally add up to 32 weights together before multiplying by the probability, requiring an additional five bits. Instead, we move to using floats during our histogram walks, which further avoids some float -> int conversions because it allows for retaining the floats we're already using to calculate probability. Across the last handful of commits, the increased pessimism more than makes up for the increased runtime complexity, leading to a 40-45% pathfinding speedup on a Xeon Silver 4116 and a 25-45% speedup on a Xeon E5-2687W v3.
1 parent ba4191b commit 9fb4ef5

File tree

1 file changed

+55
-29
lines changed

1 file changed

+55
-29
lines changed

lightning/src/routing/scoring.rs

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,14 +1218,33 @@ fn nonlinear_success_probability(
12181218
/// Given liquidity bounds, calculates the success probability (in the form of a numerator and
12191219
/// denominator) of an HTLC. This is a key assumption in our scoring models.
12201220
///
1221-
/// Must not return a numerator or denominator greater than 2^31 for arguments less than 2^31.
1222-
///
12231221
/// `total_inflight_amount_msat` includes the amount of the HTLC and any HTLCs in flight over the
12241222
/// channel.
12251223
///
12261224
/// min_zero_implies_no_successes signals that a `min_liquidity_msat` of 0 means we've not
12271225
/// (recently) seen an HTLC successfully complete over this channel.
12281226
#[inline(always)]
1227+
fn success_probability_float(
1228+
total_inflight_amount_msat: u64, min_liquidity_msat: u64, max_liquidity_msat: u64,
1229+
capacity_msat: u64, params: &ProbabilisticScoringFeeParameters,
1230+
min_zero_implies_no_successes: bool,
1231+
) -> (f64, f64) {
1232+
debug_assert!(min_liquidity_msat <= total_inflight_amount_msat);
1233+
debug_assert!(total_inflight_amount_msat < max_liquidity_msat);
1234+
debug_assert!(max_liquidity_msat <= capacity_msat);
1235+
1236+
if params.linear_success_probability {
1237+
let (numerator, denominator) = linear_success_probability(total_inflight_amount_msat, min_liquidity_msat, max_liquidity_msat, min_zero_implies_no_successes);
1238+
(numerator as f64, denominator as f64)
1239+
} else {
1240+
nonlinear_success_probability(total_inflight_amount_msat, min_liquidity_msat, max_liquidity_msat, capacity_msat, min_zero_implies_no_successes)
1241+
}
1242+
}
1243+
1244+
#[inline(always)]
1245+
/// Identical to [`success_probability_float`] but returns integer numerator and denominators.
1246+
///
1247+
/// Must not return a numerator or denominator greater than 2^31 for arguments less than 2^31.
12291248
fn success_probability(
12301249
total_inflight_amount_msat: u64, min_liquidity_msat: u64, max_liquidity_msat: u64,
12311250
capacity_msat: u64, params: &ProbabilisticScoringFeeParameters,
@@ -1798,7 +1817,7 @@ mod bucketed_history {
17981817
// Because the first thing we do is check if `total_valid_points` is sufficient to consider
17991818
// the data here at all, and can return early if it is not, we want this to go first to
18001819
// avoid hitting a second cache line load entirely in that case.
1801-
total_valid_points_tracked: u64,
1820+
total_valid_points_tracked: f64,
18021821
min_liquidity_offset_history: HistoricalBucketRangeTracker,
18031822
max_liquidity_offset_history: HistoricalBucketRangeTracker,
18041823
}
@@ -1808,7 +1827,7 @@ mod bucketed_history {
18081827
HistoricalLiquidityTracker {
18091828
min_liquidity_offset_history: HistoricalBucketRangeTracker::new(),
18101829
max_liquidity_offset_history: HistoricalBucketRangeTracker::new(),
1811-
total_valid_points_tracked: 0,
1830+
total_valid_points_tracked: 0.0,
18121831
}
18131832
}
18141833

@@ -1819,7 +1838,7 @@ mod bucketed_history {
18191838
let mut res = HistoricalLiquidityTracker {
18201839
min_liquidity_offset_history,
18211840
max_liquidity_offset_history,
1822-
total_valid_points_tracked: 0,
1841+
total_valid_points_tracked: 0.0,
18231842
};
18241843
res.recalculate_valid_point_count();
18251844
res
@@ -1842,12 +1861,15 @@ mod bucketed_history {
18421861
}
18431862

18441863
fn recalculate_valid_point_count(&mut self) {
1845-
self.total_valid_points_tracked = 0;
1864+
let mut total_valid_points_tracked = 0;
18461865
for (min_idx, min_bucket) in self.min_liquidity_offset_history.buckets.iter().enumerate() {
18471866
for max_bucket in self.max_liquidity_offset_history.buckets.iter().take(32 - min_idx) {
1848-
self.total_valid_points_tracked += (*min_bucket as u64) * (*max_bucket as u64);
1867+
let mut bucket_weight = (*min_bucket as u64) * (*max_bucket as u64);
1868+
bucket_weight *= bucket_weight;
1869+
total_valid_points_tracked += bucket_weight;
18491870
}
18501871
}
1872+
self.total_valid_points_tracked = total_valid_points_tracked as f64;
18511873
}
18521874

18531875
pub(super) fn writeable_min_offset_history(&self) -> &HistoricalBucketRangeTracker {
@@ -1933,20 +1955,23 @@ mod bucketed_history {
19331955
let mut actual_valid_points_tracked = 0;
19341956
for (min_idx, min_bucket) in min_liquidity_offset_history_buckets.iter().enumerate() {
19351957
for max_bucket in max_liquidity_offset_history_buckets.iter().take(32 - min_idx) {
1936-
actual_valid_points_tracked += (*min_bucket as u64) * (*max_bucket as u64);
1958+
let mut bucket_weight = (*min_bucket as u64) * (*max_bucket as u64);
1959+
bucket_weight *= bucket_weight;
1960+
actual_valid_points_tracked += bucket_weight;
19371961
}
19381962
}
1939-
assert_eq!(total_valid_points_tracked, actual_valid_points_tracked);
1963+
assert_eq!(total_valid_points_tracked, actual_valid_points_tracked as f64);
19401964
}
19411965

19421966
// If the total valid points is smaller than 1.0 (i.e. 32 in our fixed-point scheme),
19431967
// treat it as if we were fully decayed.
1944-
const FULLY_DECAYED: u16 = BUCKET_FIXED_POINT_ONE * BUCKET_FIXED_POINT_ONE;
1968+
const FULLY_DECAYED: f64 = BUCKET_FIXED_POINT_ONE as f64 * BUCKET_FIXED_POINT_ONE as f64 *
1969+
BUCKET_FIXED_POINT_ONE as f64 * BUCKET_FIXED_POINT_ONE as f64;
19451970
if total_valid_points_tracked < FULLY_DECAYED.into() {
19461971
return None;
19471972
}
19481973

1949-
let mut cumulative_success_prob_times_billion = 0;
1974+
let mut cumulative_success_prob = 0.0f64;
19501975
// Special-case the 0th min bucket - it generally means we failed a payment, so only
19511976
// consider the highest (i.e. largest-offset-from-max-capacity) max bucket for all
19521977
// points against the 0th min bucket. This avoids the case where we fail to route
@@ -1959,16 +1984,18 @@ mod bucketed_history {
19591984
// max-bucket with at least BUCKET_FIXED_POINT_ONE.
19601985
let mut highest_max_bucket_with_points = 0;
19611986
let mut highest_max_bucket_with_full_points = None;
1962-
let mut total_max_points = 0; // Total points in max-buckets to consider
1987+
let mut total_weight = 0;
19631988
for (max_idx, max_bucket) in max_liquidity_offset_history_buckets.iter().enumerate() {
19641989
if *max_bucket >= BUCKET_FIXED_POINT_ONE {
19651990
highest_max_bucket_with_full_points = Some(cmp::max(highest_max_bucket_with_full_points.unwrap_or(0), max_idx));
19661991
}
19671992
if *max_bucket != 0 {
19681993
highest_max_bucket_with_points = cmp::max(highest_max_bucket_with_points, max_idx);
19691994
}
1970-
total_max_points += *max_bucket as u64;
1995+
total_weight += (*max_bucket as u64) * (*max_bucket as u64)
1996+
* (min_liquidity_offset_history_buckets[0] as u64) * (min_liquidity_offset_history_buckets[0] as u64);
19711997
}
1998+
debug_assert!(total_weight as f64 <= total_valid_points_tracked);
19721999
// Use the highest max-bucket with at least BUCKET_FIXED_POINT_ONE, but if none is
19732000
// available use the highest max-bucket with any non-zero value. This ensures that
19742001
// if we have substantially decayed data we don't end up thinking the highest
@@ -1977,40 +2004,39 @@ mod bucketed_history {
19772004
let selected_max = highest_max_bucket_with_full_points.unwrap_or(highest_max_bucket_with_points);
19782005
let max_bucket_end_pos = BUCKET_START_POS[32 - selected_max] - 1;
19792006
if payment_pos < max_bucket_end_pos {
1980-
let (numerator, denominator) = success_probability(payment_pos as u64, 0,
2007+
let (numerator, denominator) = success_probability_float(payment_pos as u64, 0,
19812008
max_bucket_end_pos as u64, POSITION_TICKS as u64 - 1, params, true);
1982-
let bucket_prob_times_billion =
1983-
(min_liquidity_offset_history_buckets[0] as u64) * total_max_points
1984-
* 1024 * 1024 * 1024 / total_valid_points_tracked;
1985-
cumulative_success_prob_times_billion += bucket_prob_times_billion *
1986-
numerator / denominator;
2009+
let bucket_prob = total_weight as f64 / total_valid_points_tracked;
2010+
cumulative_success_prob += bucket_prob * numerator / denominator;
19872011
}
19882012
}
19892013

19902014
for (min_idx, min_bucket) in min_liquidity_offset_history_buckets.iter().enumerate().skip(1) {
19912015
let min_bucket_start_pos = BUCKET_START_POS[min_idx];
19922016
for (max_idx, max_bucket) in max_liquidity_offset_history_buckets.iter().enumerate().take(32 - min_idx) {
19932017
let max_bucket_end_pos = BUCKET_START_POS[32 - max_idx] - 1;
1994-
// Note that this multiply can only barely not overflow - two 16 bit ints plus
1995-
// 30 bits is 62 bits.
1996-
let bucket_prob_times_billion = (*min_bucket as u64) * (*max_bucket as u64)
1997-
* 1024 * 1024 * 1024 / total_valid_points_tracked;
2018+
let mut bucket_weight = (*min_bucket as u64) * (*max_bucket as u64);
2019+
bucket_weight *= bucket_weight;
2020+
debug_assert!(bucket_weight as f64 <= total_valid_points_tracked);
2021+
19982022
if payment_pos >= max_bucket_end_pos {
19992023
// Success probability 0, the payment amount may be above the max liquidity
20002024
break;
2001-
} else if payment_pos < min_bucket_start_pos {
2002-
cumulative_success_prob_times_billion += bucket_prob_times_billion;
2025+
}
2026+
2027+
let bucket_prob = bucket_weight as f64 / total_valid_points_tracked;
2028+
if payment_pos < min_bucket_start_pos {
2029+
cumulative_success_prob += bucket_prob;
20032030
} else {
2004-
let (numerator, denominator) = success_probability(payment_pos as u64,
2031+
let (numerator, denominator) = success_probability_float(payment_pos as u64,
20052032
min_bucket_start_pos as u64, max_bucket_end_pos as u64,
20062033
POSITION_TICKS as u64 - 1, params, true);
2007-
cumulative_success_prob_times_billion += bucket_prob_times_billion *
2008-
numerator / denominator;
2034+
cumulative_success_prob += bucket_prob * numerator / denominator;
20092035
}
20102036
}
20112037
}
20122038

2013-
Some(cumulative_success_prob_times_billion)
2039+
Some((cumulative_success_prob * (1024.0 * 1024.0 * 1024.0)) as u64)
20142040
}
20152041
}
20162042
}

0 commit comments

Comments
 (0)