Skip to content

Commit adb0afc

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. Thanks to @twood22 for being a sounding board and helping analyze the resulting PDF.
1 parent 85afe25 commit adb0afc

File tree

1 file changed

+71
-29
lines changed

1 file changed

+71
-29
lines changed

lightning/src/routing/scoring.rs

+71-29
Original file line numberDiff line numberDiff line change
@@ -1222,14 +1222,33 @@ fn nonlinear_success_probability(
12221222
/// Given liquidity bounds, calculates the success probability (in the form of a numerator and
12231223
/// denominator) of an HTLC. This is a key assumption in our scoring models.
12241224
///
1225-
/// Must not return a numerator or denominator greater than 2^31 for arguments less than 2^31.
1226-
///
12271225
/// `total_inflight_amount_msat` includes the amount of the HTLC and any HTLCs in flight over the
12281226
/// channel.
12291227
///
12301228
/// min_zero_implies_no_successes signals that a `min_liquidity_msat` of 0 means we've not
12311229
/// (recently) seen an HTLC successfully complete over this channel.
12321230
#[inline(always)]
1231+
fn success_probability_float(
1232+
total_inflight_amount_msat: u64, min_liquidity_msat: u64, max_liquidity_msat: u64,
1233+
capacity_msat: u64, params: &ProbabilisticScoringFeeParameters,
1234+
min_zero_implies_no_successes: bool,
1235+
) -> (f64, f64) {
1236+
debug_assert!(min_liquidity_msat <= total_inflight_amount_msat);
1237+
debug_assert!(total_inflight_amount_msat < max_liquidity_msat);
1238+
debug_assert!(max_liquidity_msat <= capacity_msat);
1239+
1240+
if params.linear_success_probability {
1241+
let (numerator, denominator) = linear_success_probability(total_inflight_amount_msat, min_liquidity_msat, max_liquidity_msat, min_zero_implies_no_successes);
1242+
(numerator as f64, denominator as f64)
1243+
} else {
1244+
nonlinear_success_probability(total_inflight_amount_msat, min_liquidity_msat, max_liquidity_msat, capacity_msat, min_zero_implies_no_successes)
1245+
}
1246+
}
1247+
1248+
#[inline(always)]
1249+
/// Identical to [`success_probability_float`] but returns integer numerator and denominators.
1250+
///
1251+
/// Must not return a numerator or denominator greater than 2^31 for arguments less than 2^31.
12331252
fn success_probability(
12341253
total_inflight_amount_msat: u64, min_liquidity_msat: u64, max_liquidity_msat: u64,
12351254
capacity_msat: u64, params: &ProbabilisticScoringFeeParameters,
@@ -1802,7 +1821,12 @@ mod bucketed_history {
18021821
// Because the first thing we do is check if `total_valid_points` is sufficient to consider
18031822
// the data here at all, and can return early if it is not, we want this to go first to
18041823
// avoid hitting a second cache line load entirely in that case.
1805-
total_valid_points_tracked: u64,
1824+
//
1825+
// Note that we store it as an `f64` rather than a `u64` (potentially losing some
1826+
// precision) because we ultimately need the value as an `f64` when dividing bucket weights
1827+
// by it. Storing it as an `f64` avoids doing the additional int -> float conversion in the
1828+
// hot score-calculation path.
1829+
total_valid_points_tracked: f64,
18061830
min_liquidity_offset_history: HistoricalBucketRangeTracker,
18071831
max_liquidity_offset_history: HistoricalBucketRangeTracker,
18081832
}
@@ -1812,7 +1836,7 @@ mod bucketed_history {
18121836
HistoricalLiquidityTracker {
18131837
min_liquidity_offset_history: HistoricalBucketRangeTracker::new(),
18141838
max_liquidity_offset_history: HistoricalBucketRangeTracker::new(),
1815-
total_valid_points_tracked: 0,
1839+
total_valid_points_tracked: 0.0,
18161840
}
18171841
}
18181842

@@ -1823,7 +1847,7 @@ mod bucketed_history {
18231847
let mut res = HistoricalLiquidityTracker {
18241848
min_liquidity_offset_history,
18251849
max_liquidity_offset_history,
1826-
total_valid_points_tracked: 0,
1850+
total_valid_points_tracked: 0.0,
18271851
};
18281852
res.recalculate_valid_point_count();
18291853
res
@@ -1846,12 +1870,18 @@ mod bucketed_history {
18461870
}
18471871

18481872
fn recalculate_valid_point_count(&mut self) {
1849-
self.total_valid_points_tracked = 0;
1873+
let mut total_valid_points_tracked = 0;
18501874
for (min_idx, min_bucket) in self.min_liquidity_offset_history.buckets.iter().enumerate() {
18511875
for max_bucket in self.max_liquidity_offset_history.buckets.iter().take(32 - min_idx) {
1852-
self.total_valid_points_tracked += (*min_bucket as u64) * (*max_bucket as u64);
1876+
// In testing, raising the weights of buckets to a high power led to better
1877+
// scoring results. Thus, we raise the bucket weights to the 4th power here (by
1878+
// squaring the result of multiplying the weights).
1879+
let mut bucket_weight = (*min_bucket as u64) * (*max_bucket as u64);
1880+
bucket_weight *= bucket_weight;
1881+
total_valid_points_tracked += bucket_weight;
18531882
}
18541883
}
1884+
self.total_valid_points_tracked = total_valid_points_tracked as f64;
18551885
}
18561886

18571887
pub(super) fn writeable_min_offset_history(&self) -> &HistoricalBucketRangeTracker {
@@ -1937,20 +1967,23 @@ mod bucketed_history {
19371967
let mut actual_valid_points_tracked = 0;
19381968
for (min_idx, min_bucket) in min_liquidity_offset_history_buckets.iter().enumerate() {
19391969
for max_bucket in max_liquidity_offset_history_buckets.iter().take(32 - min_idx) {
1940-
actual_valid_points_tracked += (*min_bucket as u64) * (*max_bucket as u64);
1970+
let mut bucket_weight = (*min_bucket as u64) * (*max_bucket as u64);
1971+
bucket_weight *= bucket_weight;
1972+
actual_valid_points_tracked += bucket_weight;
19411973
}
19421974
}
1943-
assert_eq!(total_valid_points_tracked, actual_valid_points_tracked);
1975+
assert_eq!(total_valid_points_tracked, actual_valid_points_tracked as f64);
19441976
}
19451977

19461978
// If the total valid points is smaller than 1.0 (i.e. 32 in our fixed-point scheme),
19471979
// treat it as if we were fully decayed.
1948-
const FULLY_DECAYED: u16 = BUCKET_FIXED_POINT_ONE * BUCKET_FIXED_POINT_ONE;
1980+
const FULLY_DECAYED: f64 = BUCKET_FIXED_POINT_ONE as f64 * BUCKET_FIXED_POINT_ONE as f64 *
1981+
BUCKET_FIXED_POINT_ONE as f64 * BUCKET_FIXED_POINT_ONE as f64;
19491982
if total_valid_points_tracked < FULLY_DECAYED.into() {
19501983
return None;
19511984
}
19521985

1953-
let mut cumulative_success_prob_times_billion = 0;
1986+
let mut cumulative_success_prob = 0.0f64;
19541987
// Special-case the 0th min bucket - it generally means we failed a payment, so only
19551988
// consider the highest (i.e. largest-offset-from-max-capacity) max bucket for all
19561989
// points against the 0th min bucket. This avoids the case where we fail to route
@@ -1963,16 +1996,22 @@ mod bucketed_history {
19631996
// max-bucket with at least BUCKET_FIXED_POINT_ONE.
19641997
let mut highest_max_bucket_with_points = 0;
19651998
let mut highest_max_bucket_with_full_points = None;
1966-
let mut total_max_points = 0; // Total points in max-buckets to consider
1999+
let mut total_weight = 0;
19672000
for (max_idx, max_bucket) in max_liquidity_offset_history_buckets.iter().enumerate() {
19682001
if *max_bucket >= BUCKET_FIXED_POINT_ONE {
19692002
highest_max_bucket_with_full_points = Some(cmp::max(highest_max_bucket_with_full_points.unwrap_or(0), max_idx));
19702003
}
19712004
if *max_bucket != 0 {
19722005
highest_max_bucket_with_points = cmp::max(highest_max_bucket_with_points, max_idx);
19732006
}
1974-
total_max_points += *max_bucket as u64;
2007+
// In testing, raising the weights of buckets to a high power led to better
2008+
// scoring results. Thus, we raise the bucket weights to the 4th power here (by
2009+
// squaring the result of multiplying the weights), matching the logic in
2010+
// `recalculate_valid_point_count`.
2011+
let bucket_weight = (*max_bucket as u64) * (min_liquidity_offset_history_buckets[0] as u64);
2012+
total_weight += bucket_weight * bucket_weight;
19752013
}
2014+
debug_assert!(total_weight as f64 <= total_valid_points_tracked);
19762015
// Use the highest max-bucket with at least BUCKET_FIXED_POINT_ONE, but if none is
19772016
// available use the highest max-bucket with any non-zero value. This ensures that
19782017
// if we have substantially decayed data we don't end up thinking the highest
@@ -1981,40 +2020,43 @@ mod bucketed_history {
19812020
let selected_max = highest_max_bucket_with_full_points.unwrap_or(highest_max_bucket_with_points);
19822021
let max_bucket_end_pos = BUCKET_START_POS[32 - selected_max] - 1;
19832022
if payment_pos < max_bucket_end_pos {
1984-
let (numerator, denominator) = success_probability(payment_pos as u64, 0,
2023+
let (numerator, denominator) = success_probability_float(payment_pos as u64, 0,
19852024
max_bucket_end_pos as u64, POSITION_TICKS as u64 - 1, params, true);
1986-
let bucket_prob_times_billion =
1987-
(min_liquidity_offset_history_buckets[0] as u64) * total_max_points
1988-
* 1024 * 1024 * 1024 / total_valid_points_tracked;
1989-
cumulative_success_prob_times_billion += bucket_prob_times_billion *
1990-
numerator / denominator;
2025+
let bucket_prob = total_weight as f64 / total_valid_points_tracked;
2026+
cumulative_success_prob += bucket_prob * numerator / denominator;
19912027
}
19922028
}
19932029

19942030
for (min_idx, min_bucket) in min_liquidity_offset_history_buckets.iter().enumerate().skip(1) {
19952031
let min_bucket_start_pos = BUCKET_START_POS[min_idx];
19962032
for (max_idx, max_bucket) in max_liquidity_offset_history_buckets.iter().enumerate().take(32 - min_idx) {
19972033
let max_bucket_end_pos = BUCKET_START_POS[32 - max_idx] - 1;
1998-
// Note that this multiply can only barely not overflow - two 16 bit ints plus
1999-
// 30 bits is 62 bits.
2000-
let bucket_prob_times_billion = (*min_bucket as u64) * (*max_bucket as u64)
2001-
* 1024 * 1024 * 1024 / total_valid_points_tracked;
20022034
if payment_pos >= max_bucket_end_pos {
20032035
// Success probability 0, the payment amount may be above the max liquidity
20042036
break;
2005-
} else if payment_pos < min_bucket_start_pos {
2006-
cumulative_success_prob_times_billion += bucket_prob_times_billion;
2037+
}
2038+
2039+
// In testing, raising the weights of buckets to a high power led to better
2040+
// scoring results. Thus, we raise the bucket weights to the 4th power here (by
2041+
// squaring the result of multiplying the weights), matching the logic in
2042+
// `recalculate_valid_point_count`.
2043+
let mut bucket_weight = (*min_bucket as u64) * (*max_bucket as u64);
2044+
bucket_weight *= bucket_weight;
2045+
debug_assert!(bucket_weight as f64 <= total_valid_points_tracked);
2046+
let bucket_prob = bucket_weight as f64 / total_valid_points_tracked;
2047+
2048+
if payment_pos < min_bucket_start_pos {
2049+
cumulative_success_prob += bucket_prob;
20072050
} else {
2008-
let (numerator, denominator) = success_probability(payment_pos as u64,
2051+
let (numerator, denominator) = success_probability_float(payment_pos as u64,
20092052
min_bucket_start_pos as u64, max_bucket_end_pos as u64,
20102053
POSITION_TICKS as u64 - 1, params, true);
2011-
cumulative_success_prob_times_billion += bucket_prob_times_billion *
2012-
numerator / denominator;
2054+
cumulative_success_prob += bucket_prob * numerator / denominator;
20132055
}
20142056
}
20152057
}
20162058

2017-
Some(cumulative_success_prob_times_billion)
2059+
Some((cumulative_success_prob * (1024.0 * 1024.0 * 1024.0)) as u64)
20182060
}
20192061
}
20202062
}

0 commit comments

Comments
 (0)