Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 122 additions & 13 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,116 @@

## Unreleased

**A weights config states every choice whose wrong value gives another
number, as the quantile method and the summation already do.** The rule:
a default that fails loudly when it is wrong is fine, and a default that
silently produces a different number is not. So these have no default
any more, and a config that leaves one out is refused naming it:
`normalization`, which was `"per_destination"`; `weights[].fallback`,
which was `"area"`; `weights[].kind`, which was `"point_sum"`;
`backend.coverage`, which was `"exact_fraction"`;
`regions.on_invalid_geometry` and `source.on_invalid_geometry`, which
were `"repair"`; and `grid.offset` and `grid.lon_convention`, which were
`"center"` and `"[-180,180)"`. The last two matter most. A grid at the
wrong offset or longitude origin still covers the regions, so the
weights came out on other cells with nothing said, the error the
BigQuery backend has refused for an edge-offset grid since #34. The area
weight takes neither `fallback` nor `kind`, since its total is the
region's own area and it reads no raster, and it refuses them as it
refuses a raster or table.

The library's own copies of the normalization default follow what the
caller says. `WeightsArtifact.from_table`, `compute_native_weights`,
`apply_fallback`, `Manifest` and `OutputSchema` take it with no default,
and `WeightsArtifact.load` refuses a weights manifest that does not
record it, where it read one as `"per_destination"`. The warning when
the fallback fired often under a policy left unstated goes with that
default, `_IMPLICIT_DEFAULT_FRAC` and `_IMPLICIT_DEFAULT_MIN` too. It
announced that an unstated default had fired, and `weight_share`
measures how much of a target's basis contributed, which is the same
information. The BigQuery backend read a weight named `pop` from the
column `population` of its table and any other weight from the column of
its own name. That was one table's layout and not a rule, so a BigQuery
weight now declares `weights[].column` beside `weights[].table`, and a
local weight refuses one.

Left alone, and why. `read_gmst` and `[stats.warming_levels]` keep
`time_col`, `value_col` and `sep`, because a wrong value there fails to
find a column and stops. `from_table` keeps recognising coordinate
columns by name, because it refuses ambiguity and checks the values
against the grid. And every default that refuses stays,
`on_null_geometry`, `on_unknown_id`, `on_zero_overlap`, `on_unmatched`,
`on_zero_weight` and `on_absent_from_data`, because a default that
refuses chooses nothing in silence.

This breaks every weights config that relied on one of the defaults. The
configs and examples of this repository state the values their defaults
gave them, so none of their numbers moves. The recipes repository,
pinned on v0.5.0, needs the same keys when its pin moves, where it
already changes for the quantile method, the summation, the declared
members and the model weight names. The config hash a weights manifest
records changes for every config, the same values included, since a
weight now holds its column and the area weight no fallback or kind, so
a hash from an earlier run does not match one written now. So the config
an earlier weights manifest came from can no longer be found by hashing
candidate configs with this version, which refuses an old config for its
missing keys and hashes a completed one differently. It takes the
version that wrote the manifest, named in its package_versions, which
the published GADM weights give as segment_weights 0.1.0, the package's
former name.

Regenerating the weights artifacts gives the same weights with new
manifests, since each config states the value its default gave. They are
regenerated before any product, because a product's run manifest records
the SHA-256 of both files of the weights it loaded, the manifest among
them, and a regenerated manifest records another config hash, so a
product made from the old weights would name a manifest that is no
longer published. Regenerating the GADM weights needs the GADM target
layers, which are not distributed and are rebuilt from the GADM sources
with `examples/gadm20/prepare_gadm20_targets.py` and
`examples/gadm41/prepare_gadm41_targets.py`.

**Every block of a reduce is measured and recorded, and only the first
is judged.** Nothing is refused that was not before. A run then shows
how much blocks of one size vary from worker to worker and how far the
first block sits from the heaviest, which is what the tolerance of twice
the estimate is to be set from, where one measured first block could say
neither. Each block's entry under `reduce.block_size.every_block` gives
the readings around it and its own growth, and `over_every_block` the
spread of the measured blocks' bytes a kept row, the heaviest over the
estimate and the first over the heaviest. A block's growth is read off
its process's peak after it, so only a block that took its process to a
new peak has one. On Linux it is taken from the resident memory before
the block, VmRSS of `/proc/self/status`, because the peak before a later
block is that of an earlier one. On macOS, which does not read the
resident memory of the moment, only a worker's first block has one. Each
worker's first block and each block that took its worker higher than
before are measured, and a block lighter than one its worker built
earlier is recorded without a growth, saying why. The log line of each
block gives its bytes a kept row.

**On Linux a worker's peak resident memory is read as VmHWM of
`/proc/self/status`, where it was `ru_maxrss`.** On Linux `ru_maxrss`
survives an exec, so a worker started by spawn read the peak of the
process that started it, and a block below that peak read nothing. Fork
starts it afresh, so runs on the cluster, which fork, were measured
right already, and what read blind was a test that asked for spawn.
VmHWM is the high-water mark of the process's own address space. On a
Midway3 compute node, RHEL 8 with kernel 4.18 and Python 3.11.12, it
started at the worker's own resident memory under spawn, fork and
forkserver, equal to VmRSS, and grew by the 150 MB a worker touched,
150.0, 150.3 and 150.0 MB. There the spawned worker's `ru_maxrss`
started at the parent's 419.9 MB and grew by nothing. macOS keeps
`ru_maxrss`, which a new worker starts afresh there. A first block whose
peak did not rise is still recorded as unmeasured, and on Linux that
should now be rare. Under fork the peaks a worker records count the
pages it shares with the process that started it, 319.8 MB at the start
of a worker whose parent held 300 MB, so they overstate what the worker
alone holds. That errs on the side of the budget, and the growth is
unaffected, 150.2 MB there. The manifest's measure says which reading
was taken. The aggregate stage reads its workers' peaks the same way,
where it took Linux's kibibytes for thousands of bytes.

**A worker that dies stops the aggregate stage at once, naming the
leaves in flight and how the workers ended.** The leaves ran on a
`multiprocessing.Pool` that renewed each worker after 16 leaves, and a
Expand Down Expand Up @@ -48,19 +158,18 @@ by side under `reduce.block_size`. A first block that keeps 100,000 rows
or more and takes more than twice the estimate stops the run before any
other block, with nothing written, and one that keeps fewer is recorded
and not judged. Twice is a tolerance on a measure with known noise, not
a correction of the estimate. One synthetic first block, measured in one
fresh worker after another, read from a fifth of the estimate to nine
tenths of it, and memory sampled through the block peaked where the
measure did, so the spread is in what the block holds resident and not
in the reading. The manifest records the factor beside the measure. On
one worker the block runs in the process that planned the run, whose
peak cannot be put down to it, and a worker whose peak did not rise, as
a forked one can when the block weighs less than what it inherited, says
nothing of the block either. Neither has a measurement or a judgement,
and the manifest says why, and which start method the workers had. The
peak a worker logs is read in bytes on macOS and kibibytes on Linux,
where Linux's was taken as thousands of bytes. New:
`[reduce.pandas] targets_per_block` declares the size of a block in
a correction of the estimate. One synthetic first block, measured on
macOS in one fresh worker after another, read from a fifth of the
estimate to nine tenths of it, and memory sampled through the block
peaked where the measure did, so the spread is in what the block holds
resident and not in the reading. The manifest records the factor beside
the measure. On one worker the block runs in the process that planned
the run, whose peak cannot be put down to it, and a worker whose peak
did not rise over the block says nothing of it either. Neither has a
measurement or a judgement, and the manifest says why, and which start
method the workers had. The peak a worker logs is read in bytes on macOS
and kibibytes on Linux, where Linux's was taken as thousands of bytes.
New: `[reduce.pandas] targets_per_block` declares the size of a block in
place of `memory_gb`. It warns, and the manifest records the size as
declared.

Expand Down
56 changes: 38 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -622,24 +622,44 @@ memory_gb = 128 # for all the workers together
n_workers = 16 # run.n_workers unless set
```

`--dry-run` prints the blocks and the number of file opens that
budget gives. The blocks are sized from an estimate of the bytes a
kept row takes, calibrated on resident memory. The first block is built
alone in a worker of its own, and measured as the growth of that
worker's peak resident memory over the block, from the peak once the
worker has set itself up. The manifest records the estimate and the
measurement side by side under `reduce.block_size`, and the setup peak,
which every worker holds before its first block. A first block that
keeps 100,000 rows or more and takes more than twice the estimate stops
the run, with nothing written, since its blocks could take more than
the budget. Twice, and not once, because the measure is noisy. The same
block, measured in one fresh worker after another, can read from a
fifth of the estimate to nearly all of it. On one worker the blocks
run in the process that planned the run, whose peak includes what it
did before. A forked worker starts from what it inherited, and a block
lighter than that leaves its peak where it was. In both cases the
manifest says there is no measurement and why, and the run is not
judged.
`--dry-run` prints the blocks and the number of file opens that budget
gives. The blocks are sized from an estimate of the bytes a kept row
takes, calibrated on resident memory. The first block is built alone in
a worker of its own, and measured as the growth of that worker's peak
resident memory over the block, from the peak once the worker has set
itself up. The manifest records the estimate and the measurement side by
side under `reduce.block_size`, and the setup peak, which every worker
holds before its first block. A first block that keeps 100,000 rows or
more and takes more than twice the estimate stops the run, with nothing
written, since its blocks could take more than the budget. Twice, and
not once, because the measure is noisy. The same block, measured on
macOS in one fresh worker after another, can read from a fifth of the
estimate to nearly all of it. On one worker the blocks run in the
process that planned the run, whose peak includes what it did before. A
block that fits in memory the worker already held leaves its peak where
it was. In both cases the manifest says there is no measurement and why,
and the run is not judged. The peak is read as each platform keeps it,
as VmHWM of `/proc/self/status` on Linux and as `ru_maxrss` on macOS. On
Linux `ru_maxrss` survives an exec, so a spawned worker's would start
from the peak of the process that started it. Under fork a worker counts
the pages it shares with that process as its own, so the peaks under
`reduce.block_size` and `max_worker_peak_gb` overstate what a worker
alone holds, which errs on the side of the budget. The growth, the
difference of two peaks that both count those pages, is exact.

Every block is measured and recorded under
`reduce.block_size.every_block`, and only the first is judged. A block's
own growth is read off its process's peak after it, so only a block that
took its process to a new peak has one. On Linux it is taken from the
resident memory before the block, VmRSS of `/proc/self/status`. On
macOS, where that is not read, only a worker's first block has one, from
the peak once the worker had set itself up. So each worker's first block
is measured, and so is each block that took its worker higher than
before, while a block lighter than one its worker built earlier has no
growth and the record says why. `reduce.block_size.over_every_block`
gives the spread of the measured blocks' bytes a kept row, the heaviest
over the estimate, and the first over the heaviest. That is what the
tolerance of twice the estimate is to be set from.

Someone who has measured their own runs can declare the size of a
block instead, in targets, a whole number of the leaves' row groups:
Expand Down
11 changes: 8 additions & 3 deletions cil_regionalization/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,12 @@ def load(cls, output_dir: str | Path, stem: str = "weights") -> "WeightsArtifact

missing = [
k
for k in ("id_fields", "source_key_columns", "weight_names")
for k in (
"id_fields",
"source_key_columns",
"weight_names",
"normalization",
)
if manifest.get(k) is None
]
if missing:
Expand All @@ -151,7 +156,7 @@ def load(cls, output_dir: str | Path, stem: str = "weights") -> "WeightsArtifact
id_fields=tuple(manifest["id_fields"]),
weight_names=tuple(manifest["weight_names"]),
source_units=source_units,
normalization=manifest.get("normalization", "per_destination"),
normalization=manifest["normalization"],
)
return cls(
frame=frame,
Expand All @@ -169,8 +174,8 @@ def from_table(
id_fields: Sequence[str],
weights: Mapping[str, str],
source: str,
normalization: str,
source_keys: Sequence[str] | None = None,
normalization: str = "per_destination",
regions_version: str | None = None,
check: bool = True,
tolerance: float = 1e-6,
Expand Down
13 changes: 11 additions & 2 deletions cil_regionalization/backends/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,15 @@ def _build_main_sql(
return sql

def _weight_value_column(self, weight: WeightSpec) -> str:
return {"pop": "population"}.get(weight.name, weight.name)
"""The column of the weight's table that holds its values, as
``weights[].column`` declares it."""
if weight.column is None:
raise ValueError(
f"weights.{weight.name}.column is required for the bigquery "
f"backend: the column of weights.{weight.name}.table that holds "
f"the weight's values"
)
return weight.column

# ----- BigQuery execution ----------------------------------------------

Expand Down Expand Up @@ -1048,6 +1056,7 @@ def _assemble_result(
id_fields,
"area",
raw_col="raw",
normalization=cfg.normalization,
)

pieces: list[pd.DataFrame] = [area_native]
Expand All @@ -1062,7 +1071,7 @@ def _assemble_result(
spec.name,
spec.fallback,
id_fields,
policy_explicit=spec.fallback_explicit,
normalization=cfg.normalization,
)
pieces.append(piece)
final = self._join_pieces(pieces, id_fields)
Expand Down
4 changes: 2 additions & 2 deletions cil_regionalization/backends/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def _compute_grid(
id_fields,
"area",
raw_col="raw",
normalization=cfg.normalization,
)

per_weight_frames: list[pd.DataFrame] = [area_native]
Expand All @@ -163,7 +164,7 @@ def _compute_grid(
spec.name,
spec.fallback,
id_fields,
policy_explicit=spec.fallback_explicit,
normalization=cfg.normalization,
)
per_weight_frames.append(piece)

Expand Down Expand Up @@ -325,7 +326,6 @@ def _compute_polygons(
spec.name,
spec.fallback,
id_fields,
policy_explicit=spec.fallback_explicit,
source_units=source_units,
normalization=cfg.normalization,
)
Expand Down
Loading