Backend Protocol¶
A backend starts the kernels of one device and reads its archive. The driver is written once against this protocol, so a backend that meets it runs correctly.
Two rules make the protocol work. submit_batch returns without waiting, which
lets the driver queue the next chunk before it calls the monitor. A backend
draws its own parent niches, because the host engine draws a fresh parent at
every step and the device engine draws one per walker per pass.
The pattern follows BEAGLE, which solves the same problem in the same field. Ayres et al. (2019).
Backend
¶
Bases: Protocol
The device interface.
A handle is opaque to the driver. A backend returns one from prepare
and gets it back on every later call.
Source code in src/hifuku/backend.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
fit
¶
Settle any parameter left to the engine, and return the parameters.
The driver calls this before it allocates anything or counts a checkpoint, so the shape it plans against and the shape the backend launches are the same numbers.
A backend that can measure what it holds asks for "auto" in its
capability defaults and answers here, sizing the run to the memory of
the machine and to the gene in ctx. A backend that cannot returns
the parameters unchanged. Either way the returned parameters must have
nothing left as "auto".
A parameter a layer named as a number is not fitted. Only the fields
the caller left open may be set here, which is what
ArchiveParams.fitted enforces.
Raise ValueError when the run does not fit at any shape.
Source code in src/hifuku/backend.py
prepare
¶
Allocate the run and return its handle. This does no survey work.
n_restored is the number of elites that resume will place. A
backend with a fixed archive capacity must hold these elites as well as
the niches that the budget can reach. A fresh run gives zero.
Source code in src/hifuku/backend.py
seed
¶
Place the anchor trees and the start trees in the archive.
A seed tree is placed without the log-likelihood gate. Raise
ValueError when no seed tree lands on the chart grid.
resume
¶
Place a saved archive and restore the random state, in place of a seed.
This is what makes a survey continuable. A run is determined by the archive it walks from and the random state it draws with, because every pass draws its parents out of the archive and keeps no walker between passes, so restoring those two continues the survey rather than starting a similar one.
The archive arrives as trees, which is how it is stored, so a device
backend places them the way seed places the start trees. Unlike a
seed they carry their own log-likelihoods and are placed without
recomputing one.
rng_state is whatever rng_state returned, and is opaque to the
driver. gain is the value the elite gain counter had at the
snapshot, and the counter continues from it. moves is the
cumulative move tally at the snapshot, shape
(4, len(MOVE_COUNTERS)). The tally counters continue from it.
When it is None, the counters start at zero.
The driver reads the statistics directly after this call. That read sets the best log-likelihood the move tally compares against, so a resumed run tallies against the best of the restored archive.
Source code in src/hifuku/backend.py
rng_state
¶
Return the random state of this handle, for a later resume.
The contents are the backend's own. A host backend has one generator; a device backend has that and the per-walker state the kernel advances. The value must survive a round trip through storage, so it holds arrays and plain numbers rather than live objects.
Source code in src/hifuku/backend.py
submit_batch
¶
Run n_variations variations, or queue them.
Draw the parents from keys, which holds the niche keys the driver
knows about. Apply logl_floor as the log-likelihood gate: a
variation below it does not make a niche. Place every candidate before
this call returns control to the driver, or queue the placement on the
same stream. A backend that can queue work returns without waiting,
so that the driver can call the monitor while the device works; one
that cannot may finish the work before it returns. Either way, the
driver calls sync before it reads.
Source code in src/hifuku/backend.py
sync
¶
read_stats
¶
read_keys
¶
Return the niche keys added at or after index since.
The driver holds the full list and asks only for the new entries, so a
device backend copies a small slice and not the whole array. The key
values are opaque to the driver, which gives them back to
submit_batch.
Source code in src/hifuku/backend.py
read_map
¶
Return the dense log-likelihood grid, NaN where unoccupied.
This is one bounded transfer. The array is a copy, never a live buffer.
read_elites
¶
Return (ij, logl) for every occupied niche, in ascending (i, j).
ij is an int64 array of shape (n, 2) in the coordinates of the
archive keys; logl is float64 of shape (n,). Call after sync.
The driver builds the landscape record from these at every checkpoint.
Both arrays are copies. The driver uses them after it submits the next
chunk, so a later change to the archive must not change them.
Source code in src/hifuku/backend.py
read_trees
¶
Return {(i, j): HifukuTree} for every occupied niche.
The trees are copies. The CPU archive changes during a run, so a reference to a live tree would show a later state than the checkpoint.
Capabilities
dataclass
¶
What a backend is and what it can do.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Backend name, for example |
unified_memory |
bool
|
True when a state read is pointer arithmetic. On a discrete device this
is False and the driver gives one warning for a |
defaults |
Mapping[str, Any]
|
|
can_read_trees |
bool
|
True when |
precision |
Mapping[str, str]
|
The precision of each quantity the backend stores, by name: |
resumes_from |
tuple of str
|
The other engines whose snapshots this backend continues. A snapshot carries its engine's random state, so a backend continues only a snapshot whose state it restores exactly. The CUDA and WebGPU engines hold the same host generator and the same xoroshiro128+ word pair per walker, so each continues the other's. |
Source code in src/hifuku/backend.py
Stats
dataclass
¶
Survey statistics read at one checkpoint.
Every value is cumulative from the start of the run. The driver takes the difference between two checkpoints to get the coverage and precision rates, which is why a backend must not reset these counters.
Attributes:
| Name | Type | Description |
|---|---|---|
n_keys |
int
|
Occupied niches. |
gain |
float
|
Total elite gain, summed over every replacement of an elite. |
best |
float
|
Best log-likelihood found. |
elite_range |
float
|
|
moves |
ndarray or None
|
Cumulative int64 counters per move type, shape
|
Source code in src/hifuku/backend.py
RunContext
dataclass
¶
The live objects that one survey runs against.
A backend gets this once, in prepare. The context holds trees, an
alignment and a model, so it does not travel between hosts. A
SurveyTask holds labels and indices instead, and a worker rebuilds the
context from the plan.
Attributes:
| Name | Type | Description |
|---|---|---|
anchors |
tuple
|
The three anchor trees that fix the chart. |
alignment, model |
The alignment under survey and its substitution model. |
|
start_trees |
list
|
Extra seed trees, normally the NJ tree of the surveyed gene. |
triangle |
AnchorTriangle
|
Anchor geometry. It carries the chart metric. |
taxon_table |
TaxonTable
|
The shared namespace. |
Source code in src/hifuku/backend.py
CpuBackend
¶
Run the survey on the host.
A state read is a dictionary lookup, so unified_memory is True and a
trees monitor costs nothing beyond building the mapping.
Source code in src/hifuku/cpu_backend.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 | |
fit
¶
Return the parameters unchanged: the host survey has one shape.
This engine holds one walker and one move per pass, which is the sequential survey, and it allocates a tree at a time rather than a whole population. There is nothing here for a memory measurement to size, so its capability defaults name numbers and no field is left open.
Source code in src/hifuku/cpu_backend.py
prepare
¶
Allocate the archive and the generator. This does no survey work.
The host archive grows as it fills, so n_restored changes nothing
here.
Refuses a chart that cannot measure the surveyed gene. A walker holds
the gene's taxa for the whole survey, so an anchor sharing too few of
them yields an undefined distance for every candidate rather than for
some of them. The device backend refuses the same combination in its
own prepare, so neither engine reaches the undefined path.
Source code in src/hifuku/cpu_backend.py
seed
¶
Place the start trees without the gate.
The seeds are trees over the surveyed gene's taxa. An anchor tree carries its own taxa, so its likelihood covers a different number of them, and a likelihood over fewer taxa is larger for that reason alone. The archive ranks a niche by likelihood, so an anchor seed would hold the niche it lands in against every candidate that covers the gene. The anchors fix the chart's coordinates and are not candidates on it.
Source code in src/hifuku/cpu_backend.py
resume
¶
Adopt a saved archive, generator, gain counter and move tally.
This is done in place of seeding. With moves None, the tally
starts at zero.
The elites are placed with the log-likelihoods they were saved with, rather than evaluated again. Re-evaluating would give the same numbers at more cost, and would quietly hide a snapshot written under a different model.
Source code in src/hifuku/cpu_backend.py
rng_state
¶
The generator state, which is all the random state a host run has.
The engine's name travels with it, because a survey continues on the engine that wrote it and the state is what says which that was.
Source code in src/hifuku/cpu_backend.py
submit_batch
¶
Run n_variations variations as whole passes.
A host archive is always current, so this backend draws its parents
from its own key list rather than from the keys the driver holds. A
parent therefore comes from the niches that exist at the moment of the
draw, which is the sampling of the sequential survey.
This method reads best and top_band once, from
handle.last_best at the last checkpoint. Every pass of this call
uses the same two values. The device does the same. Its tally kernel
reads one archive-wide best and one band for a whole queued batch.
Neither engine refreshes them pass by pass. A parent the batch itself
promotes to the summit is not counted as summit-adjacent until the next
checkpoint.
Source code in src/hifuku/cpu_backend.py
sync
¶
read_stats
¶
Read the cumulative survey statistics.
Non-finite elites are skipped, as they are in best_logL and in the
device reduction. max keeps the first item a comparison cannot
beat, so a NaN among the values would set the best and the range by its
position in the dictionary rather than by its magnitude.
moves carries one counter set per move type. See
constants.MOVE_COUNTERS. Its off_chart column, index 1, stays
zero. The host archive's niche_of is unbounded, so no proposal
here is ever off the chart. The device's off_chart column counts
proposals that landed outside its allocation. A parity comparison of
the two tallies must leave that column out.
This also sets handle.last_best, the reference the move tally reads
for the near-the-summit count over every pass up to the next
checkpoint.
Source code in src/hifuku/cpu_backend.py
read_elites
¶
The occupied niches and their log-likelihoods, in key order.
Source code in src/hifuku/cpu_backend.py
read_keys
¶
Return the positions of the keys added at or after since.
The values index archive.keys, which only grows. They are opaque to
the driver.
Source code in src/hifuku/cpu_backend.py
read_map
¶
Return the elite log-likelihood grid, NaN where unoccupied.
read_trees
¶
Return {(i, j): HifukuTree} for every occupied niche.
The mapping is a new dictionary. A move returns a new tree and never changes its parent, so the tree objects of a snapshot stay as they were at the checkpoint.
Source code in src/hifuku/cpu_backend.py
finalize
¶
Return the completed archive.
Its trees are already in canonical form: the archive puts them there as they are placed, so a walk always draws its next parent from a tree numbered the same way a resumed survey would find it. That is what makes carrying a survey on reach the same place as never stopping.
Source code in src/hifuku/cpu_backend.py
Device engines¶
A device engine keeps the walkers, the elite pool and the survey counters on
the device. DeviceBackend holds the host bookkeeping that every device engine
shares, and implements the protocol from a small set of engine methods. An
engine subclasses it, extends DeviceHandle with its own allocations, and
supplies those methods.
The pool is a list of slots, and the slots are claimed in order. The driver
draws parents by niche, because a niche is a function of the run parameters and
a slot is not. DeviceBackend turns each drawn niche into its slot before it
calls the engine.
The host side of a device survey, shared by every device engine.
A device engine holds the walkers, the elite pool and the survey counters on the device, and runs the walk and the placement there. What remains is host bookkeeping, and it is the same for every engine: the checks on the chart and the run shape, the shared leaf order, the niche grid, the seeds, the parent draw, the reads, and the archive that a survey finishes with.
:class:DeviceBackend implements the :class:~hifuku.backend.Backend
protocol from that bookkeeping and from a small set of engine methods. An
engine subclasses it and supplies these methods:
fitsizes a run to the device;_allocatebuilds the handle and the device state of one survey;_placeplaces host-made elites in the pool;_launchqueues the passes of one chunk, andsyncwaits for them;_n_slots,_read_pooland_each_stateread the pool;_counters,_set_counters,_min_occupied,_droppedand_capacityread and set the survey counters;_walker_rngand_set_walker_rngread and set the walker generators.
The pool is a list of slots. A slot holds one niche, as a flat index into the
niche grid, with its fitness, its residual and its flat tree state. Slots are
claimed in order, so the occupied slots are the first _n_slots of them.
DeviceBackend
¶
The survey bookkeeping of a device engine.
A subclass sets caps and supplies the engine methods: fit,
_allocate, _place, _launch, sync, _n_slots,
_read_pool, _each_state, _counters, _set_counters,
_min_occupied, _dropped, _capacity, _walker_rng and
_set_walker_rng. The methods here implement the rest of the
:class:~hifuku.backend.Backend protocol.
Source code in src/hifuku/device_backend.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 | |
prepare
¶
Check the chart and the shape, settle the leaf order, and allocate.
n_restored is the number of elites that resume will place. The
engine's _allocate sizes the pool to hold these elites and the
niches that the budget can reach.
Source code in src/hifuku/device_backend.py
seed
¶
Place the start trees without the gate.
The descriptor and the log-likelihood come from the host, so the seeded archive matches the CPU survey exactly. That includes which trees are seeded: the anchors fix the chart's coordinates and are not candidates on it, so neither engine places them.
Source code in src/hifuku/device_backend.py
resume
¶
Place a saved archive, restore both random states and the counters.
The counters are the gain counter and the move tally. With moves
None, the tally starts at zero.
The same path seed takes, over the saved elites instead of the start
trees. A tree already carries the four per-node arrays the device state
is packed from, so this relabels each to the shared leaf order and packs
it.
The elites keep the log-likelihoods they were saved with. Recomputing them would cost one Felsenstein pass per elite, and would silently rewrite a snapshot taken under a different model rather than showing it.
Source code in src/hifuku/device_backend.py
rng_state
¶
The host generator, and the per-walker states the kernel advances.
The device states come back as an (M, 2) array of unsigned 64-bit
words, s0 then s1, so that the value stores as a plain array.
Source code in src/hifuku/device_backend.py
submit_batch
¶
Draw the parents of whole passes and hand them to the engine.
The assignments for the whole chunk are drawn at once and the gate holds
for the chunk, so no pass waits on the host. The engine's _launch
queues the passes, or runs them and waits when it cannot queue.
Source code in src/hifuku/device_backend.py
read_stats
¶
Read the survey statistics from the device counters.
The elite range is best less the smallest occupied fitness. A rising
relative floor can leave early elites below it, so the smallest elite is
reduced on the device rather than taken as best - floor.
moves is the cumulative move tally. It differs from the host
tally in two cases. A valid candidate that finds no free pool slot
counts as lost, where the host archive places it. A prune-regraft
step that finds no valid prune or graft leaves the tree unchanged.
It still writes a candidate and counts as one proposal, where the
host makes no proposal.
Source code in src/hifuku/device_backend.py
read_keys
¶
Copy the niche keys appended at or after since, in niche order.
Only the new slice moves, so a checkpoint does not copy the whole key array.
The slice is sorted before it is returned. A cell is given its pool slot by whichever thread wins the claim, so the pool holds the niches of one pass in the order the threads finished. The driver hands this list back for parent selection, so an unsorted list would feed the thread schedule into the search and a survey would differ from run to run. The set of niches a pass adds is fixed, so sorting each slice makes the whole key list a function of the run parameters.
The slot each niche lives in is remembered here rather than returned.
Slots are handed out by an atomic counter and are therefore not a
function of the run parameters, so they must not reach the driver;
submit_batch turns the niches it draws back into slots for the
kernel.
Source code in src/hifuku/device_backend.py
read_map
¶
Return the elite log-likelihood grid, NaN where unoccupied.
The grid spans the bounding box of the filled niches, which is the same window the host backend reports.
Source code in src/hifuku/device_backend.py
read_elites
¶
Copy the occupied niches and their fitness to the host.
Source code in src/hifuku/device_backend.py
read_trees
¶
Rebuild {(i, j): HifukuTree} from the device archive state.
Source code in src/hifuku/device_backend.py
finalize
¶
Rebuild the host archive from the device fitness and state.
Source code in src/hifuku/device_backend.py
fit
¶
DeviceHandle
dataclass
¶
The host bookkeeping of one device survey.
An engine extends this with the device state it allocates.
Source code in src/hifuku/device_backend.py
CudaBackend
¶
Bases: DeviceBackend
Run the survey on a CUDA device through the shared driver.
Source code in src/hifuku/gpu_backend.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 | |
fit
¶
Size the run to this device and to this gene.
What a survey allocates has two parts. The niche allocation is fixed: it holds one flat tree state per niche whatever the walker count, and at the default cell and extent over a few thousand taxa it reaches several gibibytes. The walkers are the variable part, and one of them carries the Felsenstein partials twice, so its cost runs with the taxon count and with the alignment width.
Across nine markers of one gene family the alignment width can span more than a decade, which puts the same walker count anywhere from a tenth of the device to several times it. A batch therefore cannot state one count that suits every gene in it, and this measures each one instead.
A count that a layer stated is not measured. It goes to prepare,
which refuses it if the device cannot hold it, so a caller who named a
shape is told rather than quietly given another.
Source code in src/hifuku/gpu_backend.py
WgslBackend
¶
Bases: DeviceBackend
Run the survey on a WebGPU device through the shared driver.
Source code in src/hifuku/wgsl/backend.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | |
fit
¶
Size the walker count to the device's bindings.
WebGPU reports no device memory. The count starts at the largest
whose per-walker buffers stay within one storage binding and whose
walkers stay within one dispatch dimension, capped at
ARCHIVE_WALK_M. The walkers' largest buffers are then allocated,
and the count is halved on each out-of-memory error until they
allocate. A count the caller stated is not measured; prepare
refuses it if the device cannot hold it.
Source code in src/hifuku/wgsl/backend.py
The CUDA and WebGPU engines hold the same random state: the host generator
that draws parents, and one xoroshiro128+ word pair per walker. Each names the
other in Capabilities.resumes_from, so a snapshot that either one writes
continues on the other. A host snapshot does not continue on a device, and a
device snapshot does not continue on the host.
Walk state¶
hifuku.walk_state packs a tree into the flat row that a walker and a pool
slot hold, and lays out the niche grid. It loads no CUDA module, so every
engine uses it.
walk_state
¶
The flat form of a walker's tree, and the niche grid, on the host.
A device survey holds each tree as a flat float32 row: the parent, both
children and the edge length of every node, with the root at node
n_nodes - 1. Every walker and every elite of one survey shares one leaf
order. This module packs and unpacks that row, puts it in canonical numbering,
relabels trees into the shared leaf order, and lays out the niche grid. It
imports no device code, so every engine shares it.
state_dim
¶
Length of the flat float32 state vector for a tree with n_nodes nodes.
The vector holds the parent, both children and the edge length of every node. It is the form a tree takes in an archive niche and in a candidate buffer, so it also fixes what those cost.
Source code in src/hifuku/walk_state.py
flatten_host
¶
Pack one chain's host arrays into the flat float32 state vector.
Source code in src/hifuku/walk_state.py
canonical_flat
¶
Return a flat state renumbered into canonical form.
This is the form of :func:hifuku.tree_io.canonical, for a tree whose
leaves are already in the shared leaf order. The kernel writes every
candidate in this form. A tree that enters the pool by another route, such
as a seed, goes through this function, so every tree in the pool has one
numbering.
Source code in src/hifuku/walk_state.py
canon_leaf_names
¶
The shared leaf order for a survey.
One definition, in :func:~hifuku.tree_io.reference_leaf_order, because
both engines have to number their trees alike for the host survey to be a
reference for the device survey. taxon_table is accepted and unused,
so that a caller that has one need not know which order is used.
Source code in src/hifuku/walk_state.py
relabel_to
¶
Return the same tree with leaf position i bound to canon_names[i].
A pure re-indexing of the leaf nodes: the topology, the taxa, the branch lengths, and the label-based tree distance are unchanged. Every tree in a device survey is relabeled to one shared order, because the walkers share one tip-partials array and one leaf-global-index array, so leaf position must mean the same taxon in every walker.
Source code in src/hifuku/walk_state.py
leaf_layout
¶
(names, global_indices) that every tree of one leaf order shares.
A walker carries the gene's taxa for the whole survey and every elite it leaves behind is written in the same leaf order, so the node names and the global index of each leaf are the same for all of them. Reading them once saves the taxon table a lookup per leaf per tree, which over a full archive is millions of them.
Source code in src/hifuku/walk_state.py
tree_from_state
¶
Rebuild a HifukuTree from a flat state.
Unpacks the topology and branch-length arrays into a HifukuTree, sets
node_order from compute_node_order (which reads the topology, so any
valid index order works), and fills global_leaf_indices from the taxon
table.
layout is what :func:leaf_layout returns, for a caller rebuilding many
trees of one leaf order. The names are shared between them, which is safe
because a tree does not rename its nodes; the index array is copied, so
anything that writes into one tree's cannot reach another's.
Source code in src/hifuku/walk_state.py
device_grid
¶
Dense device allocation for the unbounded niche map.
Returns (n_off, n_side, grid_lo, sink). An archive niche (i, j) maps
to the device cell (i + n_off, j + n_off), a flat index in
[0, n_side**2); sink = n_side**2 receives niches outside the
allocation. The allocation is a memory bound, not a domain: the
log-likelihood gate stops the fill inside it.
Source code in src/hifuku/walk_state.py
bin_frame
¶
(bin_origin, bin_offset) for a device's niche binning.
A point bins as floor((lambda - origin) / cell), the key the host
gives it, and the allocation then shifts the key by n_off. The key
depends on the point, the origin and the cell, and not on the size of the
allocation.