Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/34138. ReviewsSee the guideline for information on the review process. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
4759685 to
ccf5376
Compare
ccf5376 to
919b404
Compare
919b404 to
f651f69
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
f651f69 to
dc09b2e
Compare
|
My thoughts: It should use a metric like this, not time, because it would be weird and undesirable for the behavior to change a lot just based on what system you're on or what you're doing with it. People expect slower computers to have higher cpu utilization and the intended limits are low enough that I think they won't be problematic on even quite slow computers. An operation based metric based on the performance of any specific computer will likely be much more reflective of the real resourced used on another kind of computer than some simple iteration count-- so even if something profiled on a single computer doesn't reflect yours it beats keeping the iteration based approach. One possibility would be to benchmark on several modern architectures like zen4, macpro, and graviton... normalize their coefficients for equal time on some corpus, and then average them. As in the weights for the different measures are just the average of the systems except for some per-system scaling factor of expected average performance difference. (Or in the context of the regression used to fit this, fit a model on the coefficients with Archs-1 additional free scaling factors and then discard the scaling factors)-- e.g. minimize ((A*x+B*y+C*z+..)+Arch1*(A*x+B*y+C*z+..)+Arch2*(A*x+B*y+C*z+..)-(Arch0time+Arch1time+Arch2time+...))^2. This wouldn't reflect the cost on any specific system perfectly but it might give a better estimate overall, across multiple systems. And my argument of "A wrong cost is still better than no cost model" still applies. The downside is that you'd need to measure multiple systems to calibrate it. An alternative might be to just use the coefficients for whatever popular modern hardware you think is most resource constrained-- better to have the unexpectedly show runs impact the fast computers more. And at least this approach only requires the extensive profiling on one system. As far as what transactions calibration should be against, a corpus of historical clusters is probably okay. It would probably make sense to exclude from the corpus easy clusters, e.g. ones with fewer than 10 transactions. You only care if the metric is accurate on times near the limit. I don't think using artificial cases is that helpful, as the metric will only be accurate 'on average' anyways. I don't think the coefficients need to be that well maintained over time except due to future algorithmic optimizations simply because doing anything here is better than just iteration counting. And since it's desirable to behave mostly consistently (so when someone asks why was some suboptimal cluster picked you can just look and see finding it is usually over the limit, etc), you wouldn't want to updated them often -- probably only when advances in typical computer speeds also justify meaningfully increasing the limit overall such that almost all transactions would get as much or more optimization permitted under the new limits. |
dc09b2e to
04b9afa
Compare
Fair enough, but an argument can also be made in favor of using metrics tuned for a relatively high-end system, in that those are likely a better predictor for which machines on the network, in the short to medium term future, will be responsible for actual transaction and block propagation. The numbers are such that any node, even running very low-end hardware, will not have trouble keeping up with mempool/relay, but from a network perspective, it's a well-connected subset of relatively fast systems that is responsible for relay.
Hmm, that does risk situations where adverserially-constructed transactions are significantly (but still probably not more than a small factor, say 2x-3x) more expensive per cost. I think it makes sense to use as wide a variety of clusters, real and synthetic, to calibrate. |
|
Yeah I was trying to capture 'reflective of the future' in the word "modern". Fair point on the relatively fast systems being the ones that count -- while slow systems are just "also runs" and it's sufficient that they just aren't broken. Fair enough.
A concern I had with synthetics is that with artificially hard clusters the costs might end up distorted by 'hard' transactions tending to trigger more of an operation and having that cost inflated in the regression though that operation itself is actually fast. It might make sense to have a starting offset based on the number of dependencies or some costs having a multiplier of the number of dependencies in them. |
FWIW, the current PR (although outdated now)'s weights were determined by benchmarking small pieces of code (functions, sometimes individual loop executions) and running linear regression on those. I also tried regression that tried to relate overall linearization time with various linear factors (number of times each function/loop is executed), but it turned out to be far too noisy. The result ended up containing negative factors etc. So I think that when #34023 is in, I will redo the numbers here with the same approach. For every function or non-trivial branch in the algorithm, benchmark it, and gather a histogram of its runtimes, broken down by loop iterations (e.g. in The exact composition of the training data set shouldn't matter really, except to the extent that it has some variety, and not say all clusters with deps=tx-1 or so. |
18a31ea to
e1c80f6
Compare
e1c80f6 to
fecaa2a
Compare
a568765 to
e8da939
Compare
This small optimization avoids the need to loop over the parents of each transaction when initializing the dependency-counting structures inside GetLinearization().
This is a preparation for the next commit, where chunks will no longer be identified using a representative transaction, but using a set index. Reduce the load of line changes by doing this rename ahead of time. -BEGIN VERIFY SCRIPT- sed --in-place 's/_rep/_idx/g' src/cluster_linearize.h -END VERIFY SCRIPT-
This significantly changes the data structures used in SFL, based on the observation that the DepData::top_setinfo fields are quite wasteful: there is one per dependency (up to n^2/4), but we only ever need one per active dependency (of which there at most n-1). In total, the number of chunks plus the number of active dependencies is always exactly equal to the number of transactions, so it makes sense to have a shared pool of SetInfos, which are used for both chunks and top sets. To that effect, introduce a separate m_set_info variable, which stores a SetInfo per transaction. Some of these are used for chunk sets, and some for active dependencies' top sets. Every activation transforms the parent's chunk into the top set for the new dependency. Every deactivation transforms the top set into the new parent chunk. With that, there is little need for DepData anymore. Use parent/child TxIdxs to refer to dependencies, and find their top set by having a child TxIdx-indexed vector in each TxData, rather than a list of dependencies. This makes code for iterating over dependencies more natural and simpler. With indexes into m_set_data (SetIdx) becoming bounded by the number of transactions, we can use a SetType to represent sets of SetIdxs. Specifically, an m_chunk_idxs is added which contains all SetIdx referring to chunks. This leads to a much more natural way of iterating over chunks. Also use this opportunity to normalize many variable names.
The combined size of TxData::dep_top_idx can be 16 KiB with 64 transactions and SetIdx = uint32_t. Use a smaller type where possible to reduce memory footprint and improve cache locality of m_tx_data. Also switch from an std::vector to an std::array, reducing allocation overhead and indirections.
This is a simple refactor to make the code more readable.
The current process consists of iterating over the transactions of the chunk one by one, and then for each figuring out which of its parents/children are in unprocessed chunks. Simplify this (and speed it up slightly) by splitting this process into two phases: first determine the union of all parents/children, and then find which chunks those belong to.
Instead of computing the set of reachable transactions inside PickMergeCandidate, make the information precomputed, and updated in Activate (by merging the two chucks' reachable sets) and Deactive (by recomputing). This is a small performance gain on itself, but also a preparation for future optimizations that rely on quickly testing whether dependencies between chunks exist.
Future changes will rely on knowing the chunk indexes of the two created chunks after a split. It is natural to return this information from Deactivate, which also simplifies MergeSequence.
After a split, if the top part has a dependency on the bottom part, the first MergeSequence will always perform this merge and then stop. This is referred to as a self-merge. We can special case these by detecting self-merges early, and avoiding the overhead of a full MergeSequence which involves two PickMergeCandidate calls (a succesful and an unsuccesful one).
This means we can iterate over all active dependencies in a cluster/chunk in O(ntx) time rather than O(ndeps) (*), as the number of active dependencies in a set of transactions of size is at most ntx-1. (*) Asymptotically, this is not actually true, as for large transaction counts, iterating over a BitSet still scales with ntx. In practice however, where BitSets are represented by a constant number of integers, it holds.
This avoids adding them a second time to m_suboptimal_chunks when they happen to already be there.
It suffices to initially only attempt one direction of merges in MakeTopological(), and only try both directions on chunks that are the result of other merges.
The two calls to UpdateChunk, in Activate and Deactive each, are subtly different: the top one needs to update the chunk_idx of iterated transactions, while the bottom one leaves it unchanged. To exploit this difference, inline the four function calls, getting rid of UpdateChunks. This is also a preparation for a future improvement that inlines the recomputation of reachable sets in the same loop in Deactivate.
Avoid two full iterations over all of a chunks' transactions to recompute the reachable sets, by inlining them into the dependency-updating loops.
This parametrizes the cost model for the SFL algorithm with another class. Right now, the behavior of that class matches the naive cost model so far, but it will be replaced with a more advanced on in a future commit. The reason for abstracting this out is that it makes benchmarking for creating such cost models easy, by instantiating the cost model class with one that tracks time.
f063dd8 to
f3861d8
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
|
🐙 This pull request conflicts with the target branch and needs rebase. |
|
Thank you for all the benchmark data, @l0rinc. Closing this, to reopen with an uncluttered version, after updating with new numbers and methodology. |
|
Replaced by #34616. |
744d47f clusterlin: adopt trained cost model (feature) (Pieter Wuille) 4eefdfc clusterlin: rescale costs (preparation) (Pieter Wuille) ecc9a84 clusterlin: use 'cost' terminology instead of 'iters' (refactor) (Pieter Wuille) 9e7129d clusterlin: introduce CostModel class (preparation) (Pieter Wuille) Pull request description: Part of #30289, replaces earlier #34138. This introduces a more accurate cost model for SFL, to control how much CPU time is spent inside the algorithm for clusters that cannot be linearized perfectly within a reasonable amount of time. The goal is having a metric for the amount of work performed, so that txmempool can impose limits on that work: a lower bound that is always performed (unless optimality is reached before that point, of course), and an upper bound to limit the latency and total CPU time spent on this. There are conflicting design goals here: * On the one hand, it seems ideal if this metric is closely correlated to actual CPU time, because otherwise the limits become inaccurate. * On the other hand, it seems a nightmare to have the metric be platform/system dependent, as it makes network-wide reasoning nearly impossible. It's expected that slower systems take longer to do the same thing; this holds for everything, and we don't need to compensate for this. There are multiple solutions to this: * One extreme is just measuring the time. This is very accurate, but extremely platform dependent, and also non-deterministic due to random scheduling/cache effects. * The other extreme is using a very abstract metric like counting how many times certain loops/function inside the algorithm run. That is what is implemented in master right now, just counting the sum of the numbers of transactions updated across all `UpdateChunks()` calls. It however necessarily fails to account for significant portions of runtime spent elsewhere, resulting in a rather wide range of "ns per cost" values. * This PR takes a middle ground, counting many function calls / branches / loops, with weights that were determined through benchmarking on an average on a number of systems. Specifically, the cost model was obtained by: * For a variety of machines: * Running a fixed collection of ~385000 clusters found through random generation and fuzzing, optimizing for difficulty of linearization. * Linearize each 1000-5000 times, with different random seeds. Sometimes without input linearization, sometimes with a bad one. * Gather cycle counts for each of the operations included in this cost model, broken down by their parameters. * Correct the data by subtracting the runtime of obtaining the cycle count. * Drop the 5% top and bottom samples from each cycle count dataset, and compute the average of the remaining samples. * For each operation, fit a least-squares linear function approximation through the samples. * Rescale all machine expressions to make their total time match, as we only care about relative cost of each operation. * Take the per-operation average of operation expressions across all machines, to construct expressions for an average machine. * Approximate the result with integer coefficients. The benchmarks were performed by `l0rinc <[email protected]>` and myself, on AMD Ryzen 5950X, AMD Ryzen 7995WX, AMD Ryzen 9980X, Apple M4 Max, Intel Core i5-12500H, Intel Core Ultra 7 155H, Intel N150 (Umbrel), Intel Core i7-7700, Intel Core i9-9900K, Intel Haswell (VPS, virtualized), Intel Xeon E5-2637, ARM Cortex-A76 (Raspberry Pi 5), ARM Cortex-A72 (Raspberry Pi 4). Based on final benchmarking, the "acceptable" iteration count (which is the minimum spent on every cluster) is to 75000 units, which corresponds to roughly 50 μs on Ryzen 5950X and similar modern desktop hardware. ACKs for top commit: instagibbs: ACK 744d47f murchandamus: reACK 744d47f Tree-SHA512: 5cb37a6bdd930389937c435f910410c3581e53ce609b9b594a8dc89601e6fca6e6e26216e961acfe9540581f889c14bf289b6a08438a2d7adafd696fc81ff517

Builds on top of #34023, part of #30289.
This introduces a more accurate cost model for SFL, to control how much CPU time is spent inside the algorithm for clusters that cannot be linearized perfectly within a reasonable amount of time.
The goal is having a metric for the amount of work performed, so that txmempool can impose limits on that work: a lower bound that is always performed (unless optimality is reached before that point, of course), and an upper bound to limit the latency and total CPU time spent on this. There are conflicting design goals here:
There are multiple solutions to this:
UpdateChunks()calls. It however necessarily fails to account for significant portions of runtime spent elsewhere, resulting in a rather wide range of "ns per cost" values.