Scikit-learn
human-activity-recognition
wearable
wrist
time-series
cpu
scikit-learn
Zipeng365 commited on
Commit
ac7f7cb
·
verified ·
1 Parent(s): 3a372ea

wisp-har 0.2.0: accelerated implementation with speed (exact/fast) and HMM sequence (subject/recording) options

Browse files

Method code replaced by the accelerated implementation. Default settings (speed=exact, sequence=subject) keep the original behaviour. Retained checkpoints, results and videos unchanged.

This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +1 -0
  2. README.md +42 -1
  3. RELEASE_SUMMARY.json +26 -1
  4. SHA256SUMS +57 -21
  5. SOURCE_PROVENANCE.json +122 -14
  6. dist/wisp_har-0.1.0-py3-none-any.whl +0 -0
  7. dist/wisp_har-0.2.0-cp312-cp312-linux_x86_64.whl +3 -0
  8. docs/CHECKPOINTS.md +1 -1
  9. docs/INPUTS.md +2 -0
  10. docs/METHODS.md +17 -1
  11. environments/Dockerfile.cpu +6 -2
  12. environments/README.md +3 -1
  13. pyproject.toml +4 -1
  14. setup.py +19 -0
  15. src/wisp/search/cascade.py +28 -10
  16. src/wisp/search/controller.py +39 -13
  17. src/wisp/search/grammar.py +38 -18
  18. src/wisp/search/operator_program.py +26 -6
  19. src/wisp/search/posterior.py +19 -15
  20. src/wisp_fast/__init__.py +2 -0
  21. src/wisp_fast/autocorr_lanes.h +54 -0
  22. src/wisp_fast/cache.py +97 -0
  23. src/wisp_fast/cis_conv.c +828 -0
  24. src/wisp_fast/cis_intervals.c +402 -0
  25. src/wisp_fast/decoder.py +241 -0
  26. src/wisp_fast/engine.py +459 -0
  27. src/wisp_fast/family_kernels.c +445 -0
  28. src/wisp_fast/features.py +626 -0
  29. src/wisp_fast/features_kernels.c +88 -0
  30. src/wisp_fast/forest.py +61 -0
  31. src/wisp_fast/forest_kernels.c +40 -0
  32. src/wisp_fast/interval_levels.h +84 -0
  33. src/wisp_fast/moments_tiles.h +72 -0
  34. src/wisp_fast/native.py +16 -0
  35. src/wisp_fast/parallel.py +35 -0
  36. src/wisp_fast/ridge_fast.py +172 -0
  37. src/wisp_fast/search.py +298 -0
  38. src/wisp_fast/search_cache.py +166 -0
  39. src/wisp_fast/search_forest.py +45 -0
  40. src/wisp_fast/search_metrics.py +48 -0
  41. src/wisp_fast/selection.py +26 -0
  42. src/wisp_fast/shapelet_tiles.h +62 -0
  43. src/wisp_fast/viterbi.c +85 -0
  44. src/wisp_release/__init__.py +4 -3
  45. src/wisp_release/cli.py +73 -16
  46. src/wisp_release/methods.py +43 -3
  47. src/wisp_release/selection.py +42 -9
  48. tests/test_cis_features.py +54 -0
  49. tests/test_conv_kernels.py +59 -0
  50. tests/test_engine.py +151 -0
.gitattributes CHANGED
@@ -45,3 +45,4 @@ videos/WISP_zh_mimo_voice.mp4 filter=lfs diff=lfs merge=lfs -text
45
  videos/WISP_zh_mimo_voice.wav filter=lfs diff=lfs merge=lfs -text
46
  videos/WISP_en_mimo_voice.mp4 filter=lfs diff=lfs merge=lfs -text
47
  videos/WISP_en_mimo_voice.wav filter=lfs diff=lfs merge=lfs -text
 
 
45
  videos/WISP_zh_mimo_voice.wav filter=lfs diff=lfs merge=lfs -text
46
  videos/WISP_en_mimo_voice.mp4 filter=lfs diff=lfs merge=lfs -text
47
  videos/WISP_en_mimo_voice.wav filter=lfs diff=lfs merge=lfs -text
48
+ dist/wisp_har-0.2.0-cp312-cp312-linux_x86_64.whl filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -64,7 +64,7 @@ python -m pip install --no-deps .
64
  wisp list
65
  ```
66
 
67
- Only NumPy, SciPy, scikit-learn and its runtime dependencies are installed. No PyTorch/CUDA, aeon, Mamba or baseline source download is needed. [The recorded software gate](validation/software_validation.json) passed 55 tests and verified wheel installation/import from outside the source tree using Python 3.12.13. See [environment notes](environments/README.md) and the release audit for the exact resolved environment and final regression checks.
68
 
69
  ## Export a benchmark fold (optional)
70
 
@@ -152,6 +152,47 @@ Pickle checkpoints are executable serialized objects. Load only trusted releases
152
 
153
  `select-fit` fits candidates on training participants, selects using validation only, then fits a fresh selected model on train+validation once. It does not accept test input. See [method roles and selection rule](docs/METHODS.md).
154
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
  These commands are separate from checkpoint inference. Paper fold checkpoints correspond to a specific dataset view, label vocabulary, fold and seed. They are not a universal cross-dataset recognizer. A new model fitted to all available participants would be a separate deployment artifact, not the original held-out evaluation checkpoint.
156
 
157
  For a paper comparison, retain the frozen dataset view, participant-disjoint splits, window/channel ordering, class axis, validation-based selection rule and sequence-decoding contract. Never choose the advertised “best” fold/seed by its held-out test score and present that selection as unbiased. The source-level saving logic and actual retained checkpoint inventory are separate evidence; [checkpoint notes](docs/CHECKPOINTS.md) explain this distinction.
 
64
  wisp list
65
  ```
66
 
67
+ Only NumPy, SciPy, scikit-learn and its runtime dependencies are installed. No PyTorch/CUDA, aeon, Mamba or baseline source download is needed. Installing from source compiles small optional C kernels when a C compiler is available; without one, the same code runs on slower pure-Python/NumPy paths. A prebuilt wheel for CPython 3.12 on Linux x86_64 is in `dist/`. [The software gate recorded for the first release](validation/software_validation.json) passed 55 tests and verified wheel installation/import from outside the source tree using Python 3.12.13; the current version's test suite passes with its wheel installed the same way. See [environment notes](environments/README.md) and the release audit for the exact resolved environment and final regression checks.
68
 
69
  ## Export a benchmark fold (optional)
70
 
 
152
 
153
  `select-fit` fits candidates on training participants, selects using validation only, then fits a fresh selected model on train+validation once. It does not accept test input. See [method roles and selection rule](docs/METHODS.md).
154
 
155
+ All three training commands accept the two options below. Both default to the original behaviour, so commands without them run as before.
156
+
157
+ ### Speed options
158
+
159
+ - `--speed exact` (default) uses the original Ridge solver. Outputs are identical to the original implementation.
160
+ - `--speed fast` uses a screened Ridge solver: the regularisation strength is chosen on a 512-row subsample of the training data, then the classifier is fitted on all rows. It is faster, and results can change slightly. Only models with a Ridge classifier are affected (WISP-RC, WISP-CIS and the RC/CIS parts of the ensembles and search candidates).
161
+
162
+ On 12 benchmark datasets (single thread), exact mode was 2.6x faster than the previous implementation with identical outputs; fast mode was about 3.6x faster.
163
+
164
+ ### HMM smoothing sequence
165
+
166
+ The optional HMM smoother decodes windows as sequences. There are two ways to form them:
167
+
168
+ - `--sequence subject` (default): all windows of one participant form one chain, ordered by `time_index`. This is the original behaviour and suits continuous, all-day wear.
169
+ - `--sequence recording`: each recording forms its own chain. A new recording starts whenever `time_index` goes backwards within a participant's rows. This suits data collected as separately recorded clips. The confidence scaling of classifier scores is chosen on the validation split, and smoothing is skipped when most training recordings contain a single activity, because there are then no within-recording transitions to learn. With `family-fit`, supply the validation split through `--valid`.
170
+
171
+ ```bash
172
+ wisp family-fit --method wisp_cis \
173
+ --input inputs/adl_wrist_accel_v1/fold0/train.npz \
174
+ --valid inputs/adl_wrist_accel_v1/fold0/valid.npz \
175
+ --output outputs/adl_cis_fold0_seed42.pkl \
176
+ --seed 42 --n-jobs 1 --speed fast --sequence recording --execute
177
+ ```
178
+
179
+ The same choices in Python:
180
+
181
+ ```python
182
+ from wisp_release import build_family
183
+ from wisp_release.data import as_har_data, read_npz
184
+
185
+ train, valid, test = (as_har_data(read_npz(f"inputs/adl_wrist_accel_v1/fold0/{part}.npz", require_y=True))
186
+ for part in ("train", "valid", "test"))
187
+ model = build_family("wisp_cis", seed=42, speed="fast", sequence="recording")
188
+ model.fit(train.X, train.y, groups=train.subject, time_index=train.time_index,
189
+ n_classes=train.n_classes)
190
+ model.fit_confidence_scale(valid.X, valid.y) # only used with sequence="recording"
191
+ labels = model.predict(test.X, groups=test.subject, time_index=test.time_index)
192
+ ```
193
+
194
+ `wisp_release.fit_selector(..., speed=..., sequence=...)` takes the same options. Saved metadata records both choices. Retained checkpoints keep the decoder they were fitted with; these options apply when fitting new models. Details: [method options](docs/METHODS.md#speed-and-hmm-smoothing-sequence).
195
+
196
  These commands are separate from checkpoint inference. Paper fold checkpoints correspond to a specific dataset view, label vocabulary, fold and seed. They are not a universal cross-dataset recognizer. A new model fitted to all available participants would be a separate deployment artifact, not the original held-out evaluation checkpoint.
197
 
198
  For a paper comparison, retain the frozen dataset view, participant-disjoint splits, window/channel ordering, class axis, validation-based selection rule and sequence-decoding contract. Never choose the advertised “best” fold/seed by its held-out test score and present that selection as unbiased. The source-level saving logic and actual retained checkpoint inventory are separate evidence; [checkpoint notes](docs/CHECKPOINTS.md) explain this distinction.
RELEASE_SUMMARY.json CHANGED
@@ -14097,5 +14097,30 @@
14097
  },
14098
  "checkpoint_bytes": 74911659604,
14099
  "remote_uploaded": false,
14100
- "publication_note": "Method-only local delivery. Missing fixed models remain missing; no research reruns."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14101
  }
 
14097
  },
14098
  "checkpoint_bytes": 74911659604,
14099
  "remote_uploaded": false,
14100
+ "publication_note": "Method-only local delivery. Missing fixed models remain missing; no research reruns.",
14101
+ "code_version": {
14102
+ "wisp-har": "0.2.0",
14103
+ "updated_at_utc": "2026-11-07T21:57:57.499881+00:00",
14104
+ "wheel": "dist/wisp_har-0.2.0-cp312-cp312-linux_x86_64.whl",
14105
+ "wheel_sha256": "ef623e646aacc3b8e096caa933782d9ad6d6f35a9fbae832d880d4c34f574268",
14106
+ "wheel_tag": "cp312-cp312-linux_x86_64",
14107
+ "replaces_wheel": "dist/wisp_har-0.1.0-py3-none-any.whl",
14108
+ "options": {
14109
+ "speed": [
14110
+ "exact (default)",
14111
+ "fast"
14112
+ ],
14113
+ "sequence": [
14114
+ "subject (default)",
14115
+ "recording"
14116
+ ]
14117
+ },
14118
+ "software_tests": {
14119
+ "passed": 101,
14120
+ "failed": 0,
14121
+ "python": "3.12.13",
14122
+ "installed_wheel_outside_source_tree": true
14123
+ },
14124
+ "note": "The software_validation and model_validation entries above describe version 0.1.0 and are kept unchanged. Retained checkpoints and their validation records are not affected by the code update."
14125
+ }
14126
  }
SHA256SUMS CHANGED
@@ -1,9 +1,9 @@
1
  22ec17250d6dbf255f5ffe6ce9de5fd7d9752b3e7727f91ea2ccf02cad73b7a1 .dockerignore
2
  3381754b5ce6803518720c82cdc3eaa4855cd6a2e53c0f36954fddc7e07cb448 .gitignore
3
  59e1ab474672b657909afcb346d7c6b2fc0fc627f6518b99925779aa1892b6c4 LICENSE
4
- 875a0d43f4fc89ce3d0b62208d1c799da22beeb1dfd786d42a9ce66662d5d2af README.md
5
- 6ef0910bd45698e3d0cefd9df6b3601ac055af79e3849e9c5af180c6a0e3f0eb RELEASE_SUMMARY.json
6
- 84a9886ef4e8799ee260a4bd7b0def3591f17c4c92852f122522058615337189 SOURCE_PROVENANCE.json
7
  7703e8fc152c823ae34336d8bef1cad90941ba722fa117dcf3f9a6d88cc44f38 configs/data_sources.json
8
  4f16e9b993164120d017dea7087db47dda79515931a65b4429d7c687b3482bbc configs/datasets.json
9
  7c486fb00a2fc0fdb82401ff8c1520d964c565cbb342fe66a0d55be6281a99ab configs/ordering/adl_wrist_accel_v1.csv.gz
@@ -21,14 +21,14 @@ c717390251fa1febf14bc2a2302e16254927e8b87e9d88e9f7f9e4b471b08ebd configs/orderi
21
  a7d4461be0e1f71c4eba257d9748f469cde64c1497ab4e871890884703a93676 configs/ordering_manifest.json
22
  e676e4eb85c96eeeeb926c6982da281955f74c85a0735a96d3ee5ed9071bf623 configs/scope.json
23
  d54c76a3a7333fe2fe3d851b0ba009de0abb247a7bcf2382bdd42f43ee2d8219 configs/selector_order.json
24
- 89c1cccabaf095b66fe8d4b82a007b868316302ca613d6bfe1a9a8b2995876b0 dist/wisp_har-0.1.0-py3-none-any.whl
25
- 97ca4c8ca131fad43ddd31abdf587abc2f118eabbd6b54431975bf4f34a1716e docs/CHECKPOINTS.md
26
  1ac64b1b9ed7034b5e4f17cd2ee6a6c5d8099163a8e99bf33d677536b8531730 docs/EVALUATION.md
27
- c4f01f072b7fad37655beeedaec72dfb93b4969acb3ffea6a8d301bc3e11af32 docs/INPUTS.md
28
- e9b4701987538a165981eec652ee5a1684071b4141ece9a3cf5f2032621b057e docs/METHODS.md
29
  37f564e6d4415a602cb4f320e3c68de7d41a58259e32630b23fa293fba41365c docs/MODEL_USE.md
30
- 39f0b95082c85cf6d78906736959b8735f0335f750653481b328799a731b7f53 environments/Dockerfile.cpu
31
- 15e49eedd14e7f7cfcef615c7a23838265a1029e5fb21bbbf282af7f5e8705fe environments/README.md
32
  4176347d73ec43f141fac10cfec1d214c80c320c3f4329fbf518fec36c2ec12c environments/cpu.lock.txt
33
  9a0752633ddcb1e528eddf119d6aeed86ec80be6b879f84f4375915d14dc27a7 environments/data.lock.txt
34
  6007cf0450afea5cc50accf42b79e0abedb3ff680539583925b594181cadf366 environments/test.lock.txt
@@ -754,11 +754,12 @@ ecbb4e620aef4f605b9a0657077e8589d0c086d0886507bd4d344048bb336d59 models/wisp_ra
754
  57c7e0ea59741b1dc82941501812441efc800bee4b335c72b05ae7bce4cd0c54 models/wisp_random/wisdm_watch_accel_v1/fold4/seed43/model.pkl
755
  19b3fbdc3fd2c757b3ad2c5bdd209650318a60550ee49bb3233f2bceec7a2d66 models/wisp_random/wisdm_watch_accel_v1/fold4/seed44/checkpoint.json
756
  f7822a30c9af1ab31909c8eee97cd6685b0825d809e80ae86a3378ea161df9f8 models/wisp_random/wisdm_watch_accel_v1/fold4/seed44/model.pkl
757
- ceaa134057a0b41f3dfccdeeb9329fe938a6410f512e74e8206fa4661f51ecda pyproject.toml
758
  b9ac248916e29564a4393b8096ecb28f211449c5e35f8dff8d7602220dcb78cd results/cis_and_budget_runs.csv
759
  66dc9ee0e1c20ab5ed2a08f27882d949aed69c47c51fd85d7b87ef6908083eb7 results/run_results.csv
760
  918425f34a826207a18599850f01d4b0728016c7ef6bf7e5555deb49c18fccec results/search_selections.csv
761
  bfa587f5092287d068cfb96040fb3e5a1befd06133126a66addad436a46a00dd scripts/export_benchmark.py
 
762
  01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b src/wisp/__init__.py
763
  d92d2cc5a369ca5d6a3baac156355b0b21b7816778ba9e40eb0c898bafaf8354 src/wisp/ablations.py
764
  c7b9b262d9bceed5f6a31821a26aeb044350b6af759d100876b3967a36a93be5 src/wisp/cis_algorithms.py
@@ -772,26 +773,61 @@ aab2b76f8dbf7d7faf9b4b1785b2596a2596ba4a805a18e0c856b0ea581ef668 src/wisp/cpu/f
772
  1232011f6eac18d45fd13580f96e79b9b8b088f8661a863130fe7084b1e67cc9 src/wisp/cpu/probability.py
773
  a7a96dfa10ca77d95032e4e62e1a526fc53503ee4618fa6194a73a97ae523828 src/wisp/registry.py
774
  01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b src/wisp/search/__init__.py
775
- 2f2d19573a69afe12015657f13a89caf0d28466ffd703a31f2afd951c7d1e245 src/wisp/search/cascade.py
776
- 895240d4ba5b79088766a972a76112d15fa40d4e6fa4f5440bf47b6f2a0ec451 src/wisp/search/controller.py
777
  8e976b35e37534c4fd795d529d63ac01cb0e95b87df4f090791d0d6611fb08ca src/wisp/search/dataset_fingerprint.py
778
- a038e582182aa1bed8b03a173f02b6977730dbcde07f44a3cd721f87e6d08971 src/wisp/search/grammar.py
779
  5d0375afbb4cec325ccb0d476f27c797d0e335e6d4e292a16b9330f3565a7a44 src/wisp/search/motifs.py
780
- 654975111d32566a712dd7ae8e386c577f9fc0196dad8582939ab018f32ff8ec src/wisp/search/operator_program.py
781
- 0fb27135a136f9fc77c0bdc6facb959a85587e629e4d99c40f95ff4262507e1f src/wisp/search/posterior.py
782
  0acb26c966b72721b42f9e90183186ddb1db713a42d451d886103e0ebf7a16a6 src/wisp/search/profiles.py
783
  01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b src/wisp/utils/__init__.py
784
  ab9819981cf6f56436edada19b01cfb11d78a3af40d05504c7b9740384441daa src/wisp/utils/jsonl.py
785
- c4c3a0ae71ab4eb449ac357d5cddaa40b897a6bdc683c5713e019f3fcc5027a2 src/wisp_release/__init__.py
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
786
  261bf3c45c2ef7f5a21ff188533aa19a46a6774e261fb6ee1d0f25cfb794b47f src/wisp_release/checkpoints.py
787
- 4d4f7106bd5eca4c8496c55b0a03c967df589cf1ec2e7f365db0ec6798c980a5 src/wisp_release/cli.py
788
  5dab92de198c7bca28fb620f830168be3bf936bb3c8a8c9ca4dd5762c6cf0a89 src/wisp_release/data.py
789
- 2f1eac83ccb9fe7c8d0e2a7520f746df7f522470af8dab65322a261acc17abcd src/wisp_release/methods.py
790
  aa1e89dd83b7295e0dbe087b441c13bae3247a04129f4710006da9bc49386dc5 src/wisp_release/scoring.py
791
- 7077a0bfef5c9ec3371c64b0bfdef1d285ee9f9902142e2f5676d0f0462d72b6 src/wisp_release/selection.py
792
- 1a300b9ba2ac5df48ab8bf17f960d2f2c8b21e3bdf9f67fd9632901a3901fbc1 tests/test_method_conformance.py
793
- 63f924c3047ab840d14dcd1846ff97ba81ab8ca916f6fdf638945d819206fffd tests/test_release_api.py
 
 
 
 
 
 
 
 
 
 
794
  c235154fbd26befec3d08b6700769487d535d121c6fb9f2e7fd0f7f7269ade9a tests/test_series_end_to_end.py
 
795
  658ccfbb991aad42da596ff6c21ac322cf2cb7b3dffee829338f9ea37525e1ee validation/model_validation.json
796
  26c2159c8c6d9620612b2821e5a7b6752d152475ceff896841e8624e0534b116 validation/software_validation.json
797
  40b444809841d8c452ec78f18cfcd99da706affadeae624ec1efcac2d48163c6 results/external_comparison_by_dataset.csv
 
1
  22ec17250d6dbf255f5ffe6ce9de5fd7d9752b3e7727f91ea2ccf02cad73b7a1 .dockerignore
2
  3381754b5ce6803518720c82cdc3eaa4855cd6a2e53c0f36954fddc7e07cb448 .gitignore
3
  59e1ab474672b657909afcb346d7c6b2fc0fc627f6518b99925779aa1892b6c4 LICENSE
4
+ 2ae742dce8e2f67661b70fe5ff90c740e82e24668316d3af51c141caf5267107 README.md
5
+ 43f14bd6fe7e2a93fc525a26fd95ea9cce48cd11b8ad927f1d4e0fbd2147c01c RELEASE_SUMMARY.json
6
+ 5463d03fb3c8780dc87d585ab51235dde9ee17b9afb1b265dd0fbca63b08dcc3 SOURCE_PROVENANCE.json
7
  7703e8fc152c823ae34336d8bef1cad90941ba722fa117dcf3f9a6d88cc44f38 configs/data_sources.json
8
  4f16e9b993164120d017dea7087db47dda79515931a65b4429d7c687b3482bbc configs/datasets.json
9
  7c486fb00a2fc0fdb82401ff8c1520d964c565cbb342fe66a0d55be6281a99ab configs/ordering/adl_wrist_accel_v1.csv.gz
 
21
  a7d4461be0e1f71c4eba257d9748f469cde64c1497ab4e871890884703a93676 configs/ordering_manifest.json
22
  e676e4eb85c96eeeeb926c6982da281955f74c85a0735a96d3ee5ed9071bf623 configs/scope.json
23
  d54c76a3a7333fe2fe3d851b0ba009de0abb247a7bcf2382bdd42f43ee2d8219 configs/selector_order.json
24
+ ef623e646aacc3b8e096caa933782d9ad6d6f35a9fbae832d880d4c34f574268 dist/wisp_har-0.2.0-cp312-cp312-linux_x86_64.whl
25
+ 43b7f83d38c327a37006659f42037b504714cdd5f50b9a94c839f0ea8afe8fc2 docs/CHECKPOINTS.md
26
  1ac64b1b9ed7034b5e4f17cd2ee6a6c5d8099163a8e99bf33d677536b8531730 docs/EVALUATION.md
27
+ c26d62945108bcb53e7ddd1e204d1a78c279d08e8763b54ccc919225c02e6b49 docs/INPUTS.md
28
+ 4ec5f88c378b240f0c8d78e8614e1dfba821a608cd10e242fd9f28005ed6fd66 docs/METHODS.md
29
  37f564e6d4415a602cb4f320e3c68de7d41a58259e32630b23fa293fba41365c docs/MODEL_USE.md
30
+ 352e2d13ee21e1c469f60abf31ae104b5ffbf2d10d61587bb51611fe9215cb0b environments/Dockerfile.cpu
31
+ b33f43d99ba44daf9a813f2e8ff69f4b5ef3e5ae13875da1d4339ac9340c1833 environments/README.md
32
  4176347d73ec43f141fac10cfec1d214c80c320c3f4329fbf518fec36c2ec12c environments/cpu.lock.txt
33
  9a0752633ddcb1e528eddf119d6aeed86ec80be6b879f84f4375915d14dc27a7 environments/data.lock.txt
34
  6007cf0450afea5cc50accf42b79e0abedb3ff680539583925b594181cadf366 environments/test.lock.txt
 
754
  57c7e0ea59741b1dc82941501812441efc800bee4b335c72b05ae7bce4cd0c54 models/wisp_random/wisdm_watch_accel_v1/fold4/seed43/model.pkl
755
  19b3fbdc3fd2c757b3ad2c5bdd209650318a60550ee49bb3233f2bceec7a2d66 models/wisp_random/wisdm_watch_accel_v1/fold4/seed44/checkpoint.json
756
  f7822a30c9af1ab31909c8eee97cd6685b0825d809e80ae86a3378ea161df9f8 models/wisp_random/wisdm_watch_accel_v1/fold4/seed44/model.pkl
757
+ 38f0d87a78d4e7f9bbb92c6840f52dff9b66ca931899f442bf96491e82a23d76 pyproject.toml
758
  b9ac248916e29564a4393b8096ecb28f211449c5e35f8dff8d7602220dcb78cd results/cis_and_budget_runs.csv
759
  66dc9ee0e1c20ab5ed2a08f27882d949aed69c47c51fd85d7b87ef6908083eb7 results/run_results.csv
760
  918425f34a826207a18599850f01d4b0728016c7ef6bf7e5555deb49c18fccec results/search_selections.csv
761
  bfa587f5092287d068cfb96040fb3e5a1befd06133126a66addad436a46a00dd scripts/export_benchmark.py
762
+ 323f2755c7698fa09b1d1a6f21ddb7b3ce73fb4efe3763539d09becef6dcb032 setup.py
763
  01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b src/wisp/__init__.py
764
  d92d2cc5a369ca5d6a3baac156355b0b21b7816778ba9e40eb0c898bafaf8354 src/wisp/ablations.py
765
  c7b9b262d9bceed5f6a31821a26aeb044350b6af759d100876b3967a36a93be5 src/wisp/cis_algorithms.py
 
773
  1232011f6eac18d45fd13580f96e79b9b8b088f8661a863130fe7084b1e67cc9 src/wisp/cpu/probability.py
774
  a7a96dfa10ca77d95032e4e62e1a526fc53503ee4618fa6194a73a97ae523828 src/wisp/registry.py
775
  01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b src/wisp/search/__init__.py
776
+ cdaf6c03f502ced2d992dc7b6f7166fd49ecd1c30c688fa2301b28f2fc44d046 src/wisp/search/cascade.py
777
+ b244da6b12fd67c9abb3ba3aa4f7a5cef617d8b8a9e880a51ccaaa9d485d74fe src/wisp/search/controller.py
778
  8e976b35e37534c4fd795d529d63ac01cb0e95b87df4f090791d0d6611fb08ca src/wisp/search/dataset_fingerprint.py
779
+ 8506aea80be6d2f59cc6aee1faff68ff281c3710579768e1fe1ce59ca197f310 src/wisp/search/grammar.py
780
  5d0375afbb4cec325ccb0d476f27c797d0e335e6d4e292a16b9330f3565a7a44 src/wisp/search/motifs.py
781
+ eb8c8250bf166d62eb4c53b03253e7c2e1be8504eeb2d72f4d39eccd9e342a84 src/wisp/search/operator_program.py
782
+ c3f77ce27909620bd23e074d5dd22f1cd06a5621271cd529f7b5b417ec9e93be src/wisp/search/posterior.py
783
  0acb26c966b72721b42f9e90183186ddb1db713a42d451d886103e0ebf7a16a6 src/wisp/search/profiles.py
784
  01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b src/wisp/utils/__init__.py
785
  ab9819981cf6f56436edada19b01cfb11d78a3af40d05504c7b9740384441daa src/wisp/utils/jsonl.py
786
+ 6df7c45b5c4fb6578a7b155bd1cb7419e4d62aafb50fbb4c6af92bd6b4aab14b src/wisp_fast/__init__.py
787
+ 6bf85ad45d36fac91f48eb88764cef64e90a9c9a9b733b768357b813dfea2904 src/wisp_fast/autocorr_lanes.h
788
+ 98c62c30c9e2567b0020ed3bb52e4ac5bd6f06343af28cb4cb6d7d6ef1f024b9 src/wisp_fast/cache.py
789
+ 882b278bbb512dc988540f0a17053a528289429428ae8086f0b8c72d3c0b9dd7 src/wisp_fast/cis_conv.c
790
+ 2f9ed4d52f783c84185af5c131d256923fe6f0a305fd10ea77235cdbccbe7f36 src/wisp_fast/cis_intervals.c
791
+ 00428cf66c52dc7b19a63f8802a41aaa0b6cf0c70f414af434e4e19fa331bea4 src/wisp_fast/decoder.py
792
+ b6d3149cee0d0628f3acef7297155b74496ead17fd9dc8a6415eb3d6f475647d src/wisp_fast/engine.py
793
+ e96864aa6831eac552d3c148fc892bddd36d0c6711c2bf6ba935cc6d34a1e7e2 src/wisp_fast/family_kernels.c
794
+ faa5d70e561acb2e667634d7868926ea3f18a671158ee2a1ca085682f2308cbd src/wisp_fast/features.py
795
+ 49e1f0071038f7bcdd68fda0eec9e799a41cec8888d99a2c04681339a226b9db src/wisp_fast/features_kernels.c
796
+ 8a04e5e3a05489f9999cf633f30d55dc06b8f7304fc9b776bcfccddee1a74578 src/wisp_fast/forest.py
797
+ 7f2ee234c7f075b875b51e957afbab62f9033f467ee94058e88ad589f4467cc2 src/wisp_fast/forest_kernels.c
798
+ 06a2c7e0dd3fa3b2ef0751b42f13d17ec3097e61cc54474bbef47159ca30bbdd src/wisp_fast/interval_levels.h
799
+ ba379d5cb1f4de8e0a2f1e2385a4a4edaef56745e814caf74c4f5f36b638d971 src/wisp_fast/moments_tiles.h
800
+ 37273b0b2a5eac371d19bd8be062da0c2a1d198a987c574ce520db26fb38dbea src/wisp_fast/native.py
801
+ d04fab57d812b7d770800ab526e906dd91cfe4c0991dc126c8294f784cd98a56 src/wisp_fast/parallel.py
802
+ ad0eef8901b5a7f3ed79682204c2c9973956ac175ae3d3169bc298f757582962 src/wisp_fast/ridge_fast.py
803
+ f4a28fa322282431209d7236462f02322ecc2e9d5f20819ff97aeb25cb11d188 src/wisp_fast/search.py
804
+ ce4a6ca7cecd193852c6966e8096158f51de55e4c141ab3a43bab4bc42ca5709 src/wisp_fast/search_cache.py
805
+ c7ada30c8a31f29df5e1c4b597689999a25c79ea58b62c48204049a648744b90 src/wisp_fast/search_forest.py
806
+ 7b0cf48b25cbe4dd5d2984b0a4d0748ae1820f62d1d8e8af41a9c3115732cebd src/wisp_fast/search_metrics.py
807
+ 7d7ed2780f4df33a0c7e4a7814ae63b83457c8ae334fc3fd31f917d7c546ad4d src/wisp_fast/selection.py
808
+ b0d823bdd280dc8c953620129942c10dfd727e33ea33ec277e064ffcda8ef595 src/wisp_fast/shapelet_tiles.h
809
+ 2da1b113cab1f80ccd51bdcdc562aa3bde143d9c4955c6c2732c0bede0e95a9b src/wisp_fast/viterbi.c
810
+ bebc1a59a528850af786a04a6d71f681950a523d7b5b0dc54da9b53ded3f3995 src/wisp_release/__init__.py
811
  261bf3c45c2ef7f5a21ff188533aa19a46a6774e261fb6ee1d0f25cfb794b47f src/wisp_release/checkpoints.py
812
+ 35e8207dc015afb65c0047b5482009b321fb42540432c37961f8109ef0a47b7c src/wisp_release/cli.py
813
  5dab92de198c7bca28fb620f830168be3bf936bb3c8a8c9ca4dd5762c6cf0a89 src/wisp_release/data.py
814
+ e5f37c055a6f46019cafdfc923b450daece29583a6cd96e97ab5c1870f66baff src/wisp_release/methods.py
815
  aa1e89dd83b7295e0dbe087b441c13bae3247a04129f4710006da9bc49386dc5 src/wisp_release/scoring.py
816
+ 41cefadd069009e0cd574d1915c01bbcb8dff5f061b40166f155faad414210eb src/wisp_release/selection.py
817
+ 05711d66e52059a7afd5a9234f6d1f01bdbdb0c07fa6e76dd8c4c49f112c06a7 tests/test_cis_features.py
818
+ 6c896f952651453af93a60751712a742f10a1482c44d59a0426aaac68e5d3db5 tests/test_conv_kernels.py
819
+ 0599ed5bc6cead4c7ad0515612576af284082f76bee45e2e1f63e3edab75f60f tests/test_engine.py
820
+ 21713fa476ce1c3249a1598cef53f46771b215661ee3298162f10258d500f190 tests/test_family_kernels.py
821
+ ce27602e7e335952b6e4c90b7a6f792ffe99fceca30905f6c892a544d7988786 tests/test_features_fast.py
822
+ 9700b1bf67e554c25fc51944f93ff13ab8b2eafc00b82de08375000468c8b7f5 tests/test_interval_kernels.py
823
+ a39c8e5a224903779a38523c123211217569adfe33c8cb45b9e103cbaa35fa42 tests/test_method_conformance.py
824
+ 7c0b0f4427d74f200584a7a554dcfedb741f55eff52113d72d758fc4efba08e2 tests/test_native_kernels.py
825
+ e76d6785da76c35d789ef47427a4f29913be7be0a7653b2cbe7dc45e90178f87 tests/test_release_api.py
826
+ 168d8eeac0719a4a41f2e494203866e38cf7446b008da4f8bf319fecf91f8b54 tests/test_ridge_screened.py
827
+ ae38926198725d66a96038297b792d95bd423e4b781b3acbe479198bbf7ceaf3 tests/test_search_cache.py
828
+ 5145bc9e2ccc5e3e73e0e1b9776121e52b3653a9afe1c88a0aee41e0d6677cd7 tests/test_search_shared.py
829
  c235154fbd26befec3d08b6700769487d535d121c6fb9f2e7fd0f7f7269ade9a tests/test_series_end_to_end.py
830
+ d29e2a038d29f7c7d9d58c5301a86e1beb473dbd30e7bbc84fadf8dcb79b6331 tests/test_speed_and_sequence.py
831
  658ccfbb991aad42da596ff6c21ac322cf2cb7b3dffee829338f9ea37525e1ee validation/model_validation.json
832
  26c2159c8c6d9620612b2821e5a7b6752d152475ceff896841e8624e0534b116 validation/software_validation.json
833
  40b444809841d8c452ec78f18cfcd99da706affadeae624ec1efcac2d48163c6 results/external_comparison_by_dataset.csv
SOURCE_PROVENANCE.json CHANGED
@@ -1,7 +1,7 @@
1
  {
2
  "release_name": "WISP",
3
  "author": "ZipengWu",
4
- "method_code_origin": "previous paper-scoped WISP copy, original operations preserved",
5
  "baseline_assets_included": false,
6
  "benchmark_data_duplicated": false,
7
  "result_rows": {
@@ -77,13 +77,15 @@
77
  },
78
  {
79
  "file": "src/wisp/search/cascade.py",
80
- "sha256": "2f2d19573a69afe12015657f13a89caf0d28466ffd703a31f2afd951c7d1e245",
81
- "source_sha256": "2f2d19573a69afe12015657f13a89caf0d28466ffd703a31f2afd951c7d1e245"
 
82
  },
83
  {
84
  "file": "src/wisp/search/controller.py",
85
- "sha256": "895240d4ba5b79088766a972a76112d15fa40d4e6fa4f5440bf47b6f2a0ec451",
86
- "source_sha256": "895240d4ba5b79088766a972a76112d15fa40d4e6fa4f5440bf47b6f2a0ec451"
 
87
  },
88
  {
89
  "file": "src/wisp/search/dataset_fingerprint.py",
@@ -92,8 +94,9 @@
92
  },
93
  {
94
  "file": "src/wisp/search/grammar.py",
95
- "sha256": "a038e582182aa1bed8b03a173f02b6977730dbcde07f44a3cd721f87e6d08971",
96
- "source_sha256": "a038e582182aa1bed8b03a173f02b6977730dbcde07f44a3cd721f87e6d08971"
 
97
  },
98
  {
99
  "file": "src/wisp/search/motifs.py",
@@ -102,13 +105,15 @@
102
  },
103
  {
104
  "file": "src/wisp/search/operator_program.py",
105
- "sha256": "654975111d32566a712dd7ae8e386c577f9fc0196dad8582939ab018f32ff8ec",
106
- "source_sha256": "654975111d32566a712dd7ae8e386c577f9fc0196dad8582939ab018f32ff8ec"
 
107
  },
108
  {
109
  "file": "src/wisp/search/posterior.py",
110
- "sha256": "0fb27135a136f9fc77c0bdc6facb959a85587e629e4d99c40f95ff4262507e1f",
111
- "source_sha256": "0fb27135a136f9fc77c0bdc6facb959a85587e629e4d99c40f95ff4262507e1f"
 
112
  },
113
  {
114
  "file": "src/wisp/search/profiles.py",
@@ -218,8 +223,9 @@
218
  },
219
  {
220
  "file": "tests/test_method_conformance.py",
221
- "sha256": "1a300b9ba2ac5df48ab8bf17f960d2f2c8b21e3bdf9f67fd9632901a3901fbc1",
222
- "source_sha256": "1a300b9ba2ac5df48ab8bf17f960d2f2c8b21e3bdf9f67fd9632901a3901fbc1"
 
223
  },
224
  {
225
  "file": "scripts/export_benchmark.py",
@@ -246,5 +252,107 @@
246
  "operation": "filter WISP/CIS methods only; values unchanged"
247
  }
248
  ],
249
- "remote_changes": "none; local preparation only"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250
  }
 
1
  {
2
  "release_name": "WISP",
3
  "author": "ZipengWu",
4
+ "method_code_origin": "previous paper-scoped WISP copy, original operations preserved; version 0.2.0 replaces five search modules (cascade, controller, grammar, operator_program, posterior) with the accelerated implementation's versions, which accept an injected candidate factory, and adds the accelerated engine in src/wisp_fast",
5
  "baseline_assets_included": false,
6
  "benchmark_data_duplicated": false,
7
  "result_rows": {
 
77
  },
78
  {
79
  "file": "src/wisp/search/cascade.py",
80
+ "sha256": "cdaf6c03f502ced2d992dc7b6f7166fd49ecd1c30c688fa2301b28f2fc44d046",
81
+ "source_sha256": "cdaf6c03f502ced2d992dc7b6f7166fd49ecd1c30c688fa2301b28f2fc44d046",
82
+ "source": "accelerated implementation"
83
  },
84
  {
85
  "file": "src/wisp/search/controller.py",
86
+ "sha256": "b244da6b12fd67c9abb3ba3aa4f7a5cef617d8b8a9e880a51ccaaa9d485d74fe",
87
+ "source_sha256": "b244da6b12fd67c9abb3ba3aa4f7a5cef617d8b8a9e880a51ccaaa9d485d74fe",
88
+ "source": "accelerated implementation"
89
  },
90
  {
91
  "file": "src/wisp/search/dataset_fingerprint.py",
 
94
  },
95
  {
96
  "file": "src/wisp/search/grammar.py",
97
+ "sha256": "8506aea80be6d2f59cc6aee1faff68ff281c3710579768e1fe1ce59ca197f310",
98
+ "source_sha256": "8506aea80be6d2f59cc6aee1faff68ff281c3710579768e1fe1ce59ca197f310",
99
+ "source": "accelerated implementation"
100
  },
101
  {
102
  "file": "src/wisp/search/motifs.py",
 
105
  },
106
  {
107
  "file": "src/wisp/search/operator_program.py",
108
+ "sha256": "eb8c8250bf166d62eb4c53b03253e7c2e1be8504eeb2d72f4d39eccd9e342a84",
109
+ "source_sha256": "eb8c8250bf166d62eb4c53b03253e7c2e1be8504eeb2d72f4d39eccd9e342a84",
110
+ "source": "accelerated implementation"
111
  },
112
  {
113
  "file": "src/wisp/search/posterior.py",
114
+ "sha256": "c3f77ce27909620bd23e074d5dd22f1cd06a5621271cd529f7b5b417ec9e93be",
115
+ "source_sha256": "c3f77ce27909620bd23e074d5dd22f1cd06a5621271cd529f7b5b417ec9e93be",
116
+ "source": "accelerated implementation"
117
  },
118
  {
119
  "file": "src/wisp/search/profiles.py",
 
223
  },
224
  {
225
  "file": "tests/test_method_conformance.py",
226
+ "sha256": "a39c8e5a224903779a38523c123211217569adfe33c8cb45b9e103cbaa35fa42",
227
+ "source_sha256": "1a300b9ba2ac5df48ab8bf17f960d2f2c8b21e3bdf9f67fd9632901a3901fbc1",
228
+ "modified": "internal module labels renamed; test logic unchanged"
229
  },
230
  {
231
  "file": "scripts/export_benchmark.py",
 
252
  "operation": "filter WISP/CIS methods only; values unchanged"
253
  }
254
  ],
255
+ "remote_changes": "none; local preparation only",
256
+ "accelerated_engine": {
257
+ "code_version": "0.2.0",
258
+ "files": [
259
+ {
260
+ "file": "src/wisp_fast/__init__.py",
261
+ "sha256": "6df7c45b5c4fb6578a7b155bd1cb7419e4d62aafb50fbb4c6af92bd6b4aab14b"
262
+ },
263
+ {
264
+ "file": "src/wisp_fast/autocorr_lanes.h",
265
+ "sha256": "6bf85ad45d36fac91f48eb88764cef64e90a9c9a9b733b768357b813dfea2904"
266
+ },
267
+ {
268
+ "file": "src/wisp_fast/cache.py",
269
+ "sha256": "98c62c30c9e2567b0020ed3bb52e4ac5bd6f06343af28cb4cb6d7d6ef1f024b9"
270
+ },
271
+ {
272
+ "file": "src/wisp_fast/cis_conv.c",
273
+ "sha256": "882b278bbb512dc988540f0a17053a528289429428ae8086f0b8c72d3c0b9dd7"
274
+ },
275
+ {
276
+ "file": "src/wisp_fast/cis_intervals.c",
277
+ "sha256": "2f9ed4d52f783c84185af5c131d256923fe6f0a305fd10ea77235cdbccbe7f36"
278
+ },
279
+ {
280
+ "file": "src/wisp_fast/decoder.py",
281
+ "sha256": "00428cf66c52dc7b19a63f8802a41aaa0b6cf0c70f414af434e4e19fa331bea4"
282
+ },
283
+ {
284
+ "file": "src/wisp_fast/engine.py",
285
+ "sha256": "b6d3149cee0d0628f3acef7297155b74496ead17fd9dc8a6415eb3d6f475647d"
286
+ },
287
+ {
288
+ "file": "src/wisp_fast/family_kernels.c",
289
+ "sha256": "e96864aa6831eac552d3c148fc892bddd36d0c6711c2bf6ba935cc6d34a1e7e2"
290
+ },
291
+ {
292
+ "file": "src/wisp_fast/features.py",
293
+ "sha256": "faa5d70e561acb2e667634d7868926ea3f18a671158ee2a1ca085682f2308cbd"
294
+ },
295
+ {
296
+ "file": "src/wisp_fast/features_kernels.c",
297
+ "sha256": "49e1f0071038f7bcdd68fda0eec9e799a41cec8888d99a2c04681339a226b9db"
298
+ },
299
+ {
300
+ "file": "src/wisp_fast/forest.py",
301
+ "sha256": "8a04e5e3a05489f9999cf633f30d55dc06b8f7304fc9b776bcfccddee1a74578"
302
+ },
303
+ {
304
+ "file": "src/wisp_fast/forest_kernels.c",
305
+ "sha256": "7f2ee234c7f075b875b51e957afbab62f9033f467ee94058e88ad589f4467cc2"
306
+ },
307
+ {
308
+ "file": "src/wisp_fast/interval_levels.h",
309
+ "sha256": "06a2c7e0dd3fa3b2ef0751b42f13d17ec3097e61cc54474bbef47159ca30bbdd"
310
+ },
311
+ {
312
+ "file": "src/wisp_fast/moments_tiles.h",
313
+ "sha256": "ba379d5cb1f4de8e0a2f1e2385a4a4edaef56745e814caf74c4f5f36b638d971"
314
+ },
315
+ {
316
+ "file": "src/wisp_fast/native.py",
317
+ "sha256": "37273b0b2a5eac371d19bd8be062da0c2a1d198a987c574ce520db26fb38dbea"
318
+ },
319
+ {
320
+ "file": "src/wisp_fast/parallel.py",
321
+ "sha256": "d04fab57d812b7d770800ab526e906dd91cfe4c0991dc126c8294f784cd98a56"
322
+ },
323
+ {
324
+ "file": "src/wisp_fast/ridge_fast.py",
325
+ "sha256": "ad0eef8901b5a7f3ed79682204c2c9973956ac175ae3d3169bc298f757582962"
326
+ },
327
+ {
328
+ "file": "src/wisp_fast/search.py",
329
+ "sha256": "f4a28fa322282431209d7236462f02322ecc2e9d5f20819ff97aeb25cb11d188"
330
+ },
331
+ {
332
+ "file": "src/wisp_fast/search_cache.py",
333
+ "sha256": "ce4a6ca7cecd193852c6966e8096158f51de55e4c141ab3a43bab4bc42ca5709"
334
+ },
335
+ {
336
+ "file": "src/wisp_fast/search_forest.py",
337
+ "sha256": "c7ada30c8a31f29df5e1c4b597689999a25c79ea58b62c48204049a648744b90"
338
+ },
339
+ {
340
+ "file": "src/wisp_fast/search_metrics.py",
341
+ "sha256": "7b0cf48b25cbe4dd5d2984b0a4d0748ae1820f62d1d8e8af41a9c3115732cebd"
342
+ },
343
+ {
344
+ "file": "src/wisp_fast/selection.py",
345
+ "sha256": "7d7ed2780f4df33a0c7e4a7814ae63b83457c8ae334fc3fd31f917d7c546ad4d"
346
+ },
347
+ {
348
+ "file": "src/wisp_fast/shapelet_tiles.h",
349
+ "sha256": "b0d823bdd280dc8c953620129942c10dfd727e33ea33ec277e064ffcda8ef595"
350
+ },
351
+ {
352
+ "file": "src/wisp_fast/viterbi.c",
353
+ "sha256": "2da1b113cab1f80ccd51bdcdc562aa3bde143d9c4955c6c2732c0bede0e95a9b"
354
+ }
355
+ ],
356
+ "note": "Accelerated engine from the authors' fast implementation, reduced to the modules used by the public API (exact and screened Ridge solvers, subject and recording HMM smoothing sequences, accelerated search)."
357
+ }
358
  }
dist/wisp_har-0.1.0-py3-none-any.whl DELETED
Binary file (64.5 kB)
 
dist/wisp_har-0.2.0-cp312-cp312-linux_x86_64.whl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ef623e646aacc3b8e096caa933782d9ad6d6f35a9fbae832d880d4c34f574268
3
+ size 605026
docs/CHECKPOINTS.md CHANGED
@@ -44,7 +44,7 @@ Use compatible Python/NumPy/SciPy/scikit-learn versions and preserve checkpoint-
44
 
45
  The inference CLI can read an explicitly supplied metadata JSON. If omitted, it looks for a sibling `checkpoint.json`, then `<checkpoint_stem>.metadata.json`. Supported validation fields are `input_shape` (time/channel shape, optionally prefixed by the original window count) and `label_names`. A SHA256 stored in a sidecar is descriptive integrity information; the CLI does not treat that file as an independently trusted source for the `--sha256` check.
46
 
47
- `wisp family-fit` writes `<output_stem>.metadata.json` with the declared method, input shape, global label names, model seed, fitted-data scope, HMM setting, parameters and output checksum. `wisp select-fit` also writes `<output_stem>.selection.json` for its validation choice. `wisp search` writes `selected_model.metadata.json` for the controller's selected model. A generic new fit does not invent a dataset/fold identity that the caller did not provide. These newly fitted artifacts must be kept distinct from retained paper models.
48
 
49
  The manifest inventory is read-only and does not need to deserialize every model. Actual loading/inference verification, hashes, installation and any refit must run on allocated compute nodes. Coverage is reported by the root release audit, not by this explanatory document.
50
 
 
44
 
45
  The inference CLI can read an explicitly supplied metadata JSON. If omitted, it looks for a sibling `checkpoint.json`, then `<checkpoint_stem>.metadata.json`. Supported validation fields are `input_shape` (time/channel shape, optionally prefixed by the original window count) and `label_names`. A SHA256 stored in a sidecar is descriptive integrity information; the CLI does not treat that file as an independently trusted source for the `--sha256` check.
46
 
47
+ `wisp family-fit` writes `<output_stem>.metadata.json` with the declared method, input shape, global label names, model seed, fitted-data scope, HMM setting, speed/Ridge solver, HMM smoothing sequence and confidence scaling (see [method options](METHODS.md#speed-and-hmm-smoothing-sequence)), parameters and output checksum. `wisp select-fit` also writes `<output_stem>.selection.json` for its validation choice. `wisp search` writes `selected_model.metadata.json` for the controller's selected model. Models newly fitted by these commands are `wisp_fast.engine.FastFamily` objects; retained paper checkpoints keep their original classes and are loaded and predicted exactly as before. A generic new fit does not invent a dataset/fold identity that the caller did not provide. These newly fitted artifacts must be kept distinct from retained paper models.
48
 
49
  The manifest inventory is read-only and does not need to deserialize every model. Actual loading/inference verification, hashes, installation and any refit must run on allocated compute nodes. Coverage is reported by the root release audit, not by this explanatory document.
50
 
docs/INPUTS.md CHANGED
@@ -42,6 +42,8 @@ For HMM-enabled models, the checkpoint contains a fitted transition matrix and c
42
 
43
  For new recordings, group identifiers should distinguish independent recording sequences. Do not connect different participants, unrelated sessions or discontinuous recordings into one artificial sequence. Do not shuffle windows, batch a chronological sequence into unrelated chunks or omit metadata and still claim the same HMM prediction protocol. Offline sequence decoding can depend on later windows within a sequence; it is not presented as a causal, streaming recognizer.
44
 
 
 
45
  A direct/argmax model can be run without sequence metadata where supported by its recorded configuration. Disabling HMM or omitting groups is a protocol change, not an equivalent reproduction of an HMM checkpoint.
46
 
47
  ## Example schema
 
42
 
43
  For new recordings, group identifiers should distinguish independent recording sequences. Do not connect different participants, unrelated sessions or discontinuous recordings into one artificial sequence. Do not shuffle windows, batch a chronological sequence into unrelated chunks or omit metadata and still claim the same HMM prediction protocol. Offline sequence decoding can depend on later windows within a sequence; it is not presented as a causal, streaming recognizer.
44
 
45
+ Models fitted with `--sequence recording` split each group into recordings wherever `time_index` goes backwards in the supplied row order, and decode each recording separately; keep the original row order for such models. See [method options](METHODS.md#speed-and-hmm-smoothing-sequence).
46
+
47
  A direct/argmax model can be run without sequence metadata where supported by its recorded configuration. Disabling HMM or omitting groups is a protocol change, not an equivalent reproduction of an HMM checkpoint.
48
 
49
  ## Example schema
docs/METHODS.md CHANGED
@@ -28,6 +28,22 @@ Implementation coverage, retained checkpoint coverage and verified inference cov
28
 
29
  Each candidate is fitted on training only and scored on validation. Selection uses, in order, higher macro-F1, higher full-vocabulary worst-class F1, smaller serialized model size using the preserved `estimate_pickle_size_mb` helper with Python's default pickle protocol (protocol 4 in the pinned Python 3.12 runtime), then the canonical order SS, RC, SO, CSE, GIS, CIS, ESE. A fresh winning model is fitted on train+validation once. Test data are not accepted or used by this command.
30
 
31
- The selector's size measurement and the new fitted output's serialization protocol are separate: newly saved models use the highest protocol, while the historical size tie-break uses the default protocol. Renaming Python namespaces can itself affect serialized byte counts. The wrapper preserves the helper and ordering, but does not claim byte-identical model sizes or guarantee that an exact size-only tie remains unchanged across renamed namespaces/environments.
32
 
33
  With `--execute`, the command saves the fitted output model, `<output_stem>.metadata.json` and `<output_stem>.selection.json`. Without `--execute`, it describes the requested plan only. A newly fitted selector model is a new artifact; it does not fill an original missing paper checkpoint without being labelled as a refit.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  Each candidate is fitted on training only and scored on validation. Selection uses, in order, higher macro-F1, higher full-vocabulary worst-class F1, smaller serialized model size using the preserved `estimate_pickle_size_mb` helper with Python's default pickle protocol (protocol 4 in the pinned Python 3.12 runtime), then the canonical order SS, RC, SO, CSE, GIS, CIS, ESE. A fresh winning model is fitted on train+validation once. Test data are not accepted or used by this command.
30
 
31
+ The selector's size measurement and the new fitted output's serialization protocol are separate: newly saved models use the highest protocol, while the historical size tie-break uses the default protocol. Size is only needed when two candidates tie on both validation scores, so it is measured only for tied candidates (reported as `null` otherwise), on the reference estimator fitted to the training split, as in the original selector. Renaming Python namespaces can itself affect serialized byte counts. The wrapper preserves the helper and ordering, but does not claim byte-identical model sizes or guarantee that an exact size-only tie remains unchanged across renamed namespaces/environments.
32
 
33
  With `--execute`, the command saves the fitted output model, `<output_stem>.metadata.json` and `<output_stem>.selection.json`. Without `--execute`, it describes the requested plan only. A newly fitted selector model is a new artifact; it does not fill an original missing paper checkpoint without being labelled as a refit.
34
+
35
+ ## Speed and HMM smoothing sequence
36
+
37
+ `wisp family-fit`, `wisp select-fit` and `wisp search` (and `wisp_release.build_family` / `fit_selector`) take two options. Both default to the original behaviour.
38
+
39
+ | Option | Value | Meaning |
40
+ | --- | --- | --- |
41
+ | `--speed` | `exact` (default) | Original Ridge solver. Outputs are identical to the original implementation. |
42
+ | | `fast` | Screened Ridge: the regularisation strength is chosen on a 512-row training subsample, then the Ridge classifier is fitted on all rows. Faster; results can change slightly. Only models with a Ridge classifier change: WISP-RC, WISP-CIS, the RC/CIS members of WISP-CSE and WISP-ESE, and RC/CIS candidates in search. |
43
+ | `--sequence` | `subject` (default) | All windows of one participant form one HMM chain, ordered by time index. This is the original behaviour. |
44
+ | | `recording` | Each recording forms its own chain. Within one participant's rows, in the order supplied, a new recording starts whenever the time index goes backwards. The confidence scaling of classifier scores (one factor dividing the log-probabilities) is chosen on the validation split by negative log-likelihood. Smoothing is skipped when more than half of the training recordings contain a single activity, because there are then no within-recording transitions to learn. |
45
+
46
+ The two sequence options suit different data. `subject` fits continuous wear, where all of a participant's windows belong to one timeline. `recording` fits data collected as separately recorded clips, where consecutive clips of one participant are not connected in time.
47
+
48
+ With `--sequence recording`, `family-fit` needs `--valid` to choose the confidence scaling. `select-fit` chooses it on validation for every candidate, and search does the same for every candidate it evaluates. A final train+validation refit reuses the factor chosen for the corresponding train-only model. Saved metadata records `speed`, `sequence` and the chosen `confidence_scale`.
49
+
environments/Dockerfile.cpu CHANGED
@@ -6,9 +6,13 @@ ENV OMP_NUM_THREADS=1 OPENBLAS_NUM_THREADS=1 MKL_NUM_THREADS=1
6
  WORKDIR /opt/wisp
7
  COPY environments/cpu.lock.txt /tmp/wisp-cpu.lock.txt
8
  RUN python -m pip install --no-cache-dir -r /tmp/wisp-cpu.lock.txt
9
- COPY pyproject.toml README.md LICENSE ./
10
  COPY src ./src
11
- RUN python -m pip install --no-cache-dir --no-deps .
 
 
 
 
12
 
13
  # Mount downloaded checkpoints and inputs; do not bake 75 GB of models into an
14
  # environment image. No baseline, CUDA or dataset packages are installed here.
 
6
  WORKDIR /opt/wisp
7
  COPY environments/cpu.lock.txt /tmp/wisp-cpu.lock.txt
8
  RUN python -m pip install --no-cache-dir -r /tmp/wisp-cpu.lock.txt
9
+ COPY pyproject.toml setup.py README.md LICENSE ./
10
  COPY src ./src
11
+ # A C compiler builds the optional native kernels; without it the slower Python paths are used.
12
+ RUN apt-get update && apt-get install -y --no-install-recommends gcc libc6-dev \
13
+ && python -m pip install --no-cache-dir "setuptools==80.9.0" "wheel==0.45.1" \
14
+ && python -m pip install --no-cache-dir --no-deps --no-build-isolation . \
15
+ && apt-get purge -y gcc libc6-dev && apt-get autoremove -y && rm -rf /var/lib/apt/lists/*
16
 
17
  # Mount downloaded checkpoints and inputs; do not bake 75 GB of models into an
18
  # environment image. No baseline, CUDA or dataset packages are installed here.
environments/README.md CHANGED
@@ -28,7 +28,9 @@ python -m pip install -r environments/cpu.lock.txt
28
  python -m pip install --no-deps .
29
  ```
30
 
31
- For release checks, install `environments/test.lock.txt` in the same environment. The recorded software gate passed 55 tests and verified the installed wheel outside the source tree. The model gate loaded and smoke-tested all 360 retained checkpoints in this environment; six ADL-Wrist models also passed full real-test prediction equivalence. See [software validation](../validation/software_validation.json), [model validation](../validation/model_validation.json) and the root release audit for final regression checks and full coverage boundaries.
 
 
32
 
33
  The optional `data.lock.txt` adds pandas, Datasets, Hugging Face Hub and PyArrow for the benchmark NPZ export script. These versions match the existing data/analysis environment pins. It is not needed when the input NPZ files are already available, and it adds no baseline implementation. The small lock files pin their listed distributions; the release audit's resolved package record is needed to identify transitive dependencies installed in a particular validation environment.
34
 
 
28
  python -m pip install --no-deps .
29
  ```
30
 
31
+ Installing from source compiles optional C kernels for the accelerated engine when a C compiler is available (on the cluster, for example after `module load GCC`); without a compiler the build still succeeds and uses slower Python/NumPy paths. Alternatively install the prebuilt CPython 3.12 Linux x86_64 wheel from `dist/` with `python -m pip install --no-deps dist/wisp_har-0.2.0-cp312-cp312-linux_x86_64.whl`.
32
+
33
+ For release checks, install `environments/test.lock.txt` in the same environment. The software gate recorded for the first release passed 55 tests and verified the installed wheel outside the source tree; the current version's suite is run the same way. The model gate loaded and smoke-tested all 360 retained checkpoints in this environment; six ADL-Wrist models also passed full real-test prediction equivalence. See [software validation](../validation/software_validation.json), [model validation](../validation/model_validation.json) and the root release audit for final regression checks and full coverage boundaries.
34
 
35
  The optional `data.lock.txt` adds pandas, Datasets, Hugging Face Hub and PyArrow for the benchmark NPZ export script. These versions match the existing data/analysis environment pins. It is not needed when the input NPZ files are already available, and it adds no baseline implementation. The small lock files pin their listed distributions; the release audit's resolved package record is needed to identify transitive dependencies installed in a particular validation environment.
36
 
pyproject.toml CHANGED
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
 
5
  [project]
6
  name = "wisp-har"
7
- version = "0.1.0"
8
  description = "WISP representations, search strategies and trained wrist activity recognition pipelines"
9
  readme = "README.md"
10
  requires-python = ">=3.12.13,<3.13"
@@ -21,6 +21,9 @@ wisp = "wisp_release.cli:main"
21
  [tool.setuptools.packages.find]
22
  where = ["src"]
23
 
 
 
 
24
  [tool.pytest.ini_options]
25
  testpaths = ["tests"]
26
  addopts = "-ra"
 
4
 
5
  [project]
6
  name = "wisp-har"
7
+ version = "0.2.0"
8
  description = "WISP representations, search strategies and trained wrist activity recognition pipelines"
9
  readme = "README.md"
10
  requires-python = ">=3.12.13,<3.13"
 
21
  [tool.setuptools.packages.find]
22
  where = ["src"]
23
 
24
+ [tool.setuptools.package-data]
25
+ wisp_fast = ["*.c", "*.h"]
26
+
27
  [tool.pytest.ini_options]
28
  testpaths = ["tests"]
29
  addopts = "-ra"
setup.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Optional C kernels of the accelerated engine (pure-Python fallbacks are used when a build fails)."""
2
+ import os
3
+ from setuptools import Extension, setup
4
+
5
+ FLAGS = ["/O2", "/fp:strict"] if os.name == "nt" else ["-O3", "-ffp-contract=off"]
6
+ SRC = "src/wisp_fast/"
7
+
8
+
9
+ def ext(name, sources, depends=()):
10
+ return Extension(f"wisp_fast.{name}", [SRC + s for s in sources], depends=[SRC + d for d in depends],
11
+ extra_compile_args=FLAGS, optional=True)
12
+
13
+
14
+ setup(ext_modules=[
15
+ ext("_kernels", ["features_kernels.c", "viterbi.c", "forest_kernels.c"]),
16
+ ext("_family_kernels", ["family_kernels.c"], ["autocorr_lanes.h", "shapelet_tiles.h", "moments_tiles.h"]),
17
+ ext("_cis_intervals", ["cis_intervals.c"], ["interval_levels.h"]),
18
+ ext("_cis_conv", ["cis_conv.c"]),
19
+ ])
src/wisp/search/cascade.py CHANGED
@@ -8,7 +8,7 @@ from wisp.core.data import HARData
8
  from wisp.core.metrics import classification_report_dict, score_from_metrics
9
  from wisp.cpu.algorithms import build_cpu_estimator, estimate_pickle_size_mb
10
  from wisp.search.grammar import CPU_FAMILIES, CandidateSpec, normalize_spec
11
- from wisp.search.operator_program import TemporalProgram
12
 
13
  @dataclass
14
  class StageResult:
@@ -47,7 +47,7 @@ def validate_candidate_spec(spec: CandidateSpec) -> StageResult:
47
  return StageResult('spec_validity', False, {}, 'n_shapelets outside allowed range')
48
  if 'symbolic_bins' in p and (not 2 <= int(p['symbolic_bins']) <= 32):
49
  return StageResult('spec_validity', False, {}, 'symbolic_bins outside allowed range')
50
- return StageResult('spec_validity', True, TemporalProgram.from_candidate(spec).certificate())
51
 
52
  def _axis_sign_flip(X: np.ndarray) -> np.ndarray:
53
  out = X.copy()
@@ -71,35 +71,53 @@ def _noise(X: np.ndarray) -> np.ndarray:
71
  return X + rng.normal(0.0, 1.0, size=X.shape) * scale
72
  STRESS_TRANSFORMS = {'axis_sign_flip': _axis_sign_flip, 'time_shift': _time_shift, 'channel_dropout': _channel_dropout, 'sensor_noise': _noise}
73
 
74
- def evaluate_candidate_cascade(spec: CandidateSpec, train: HARData, valid: HARData, *, cost_weight: float=0.01, robustness_weight: float=0.1, max_stress_transforms: int=4) -> CascadeResult:
75
  spec = normalize_spec(spec)
76
  stages: list[StageResult] = []
77
  valid_stage = validate_candidate_spec(spec)
78
  stages.append(valid_stage)
79
  if not valid_stage.passed:
80
  return CascadeResult(False, -1000000000.0, stages, {'candidate': spec.to_dict(), 'score': -1000000000.0})
81
- model = build_cpu_estimator(spec.to_dict())
82
  t0 = time.perf_counter()
83
  model.fit(train.X, train.y, groups=train.subject, time_index=train.time_index, n_classes=train.n_classes)
84
  fit_s = time.perf_counter() - t0
85
  t1 = time.perf_counter()
86
  pred = model.predict(valid.X, groups=valid.subject, time_index=valid.time_index)
87
  pred_s = time.perf_counter() - t1
88
- size_mb = estimate_pickle_size_mb(model)
89
- metrics = classification_report_dict(valid.y, pred, fit_seconds=fit_s, predict_seconds=pred_s, model_size_mb=size_mb, label_names=valid.label_names)
90
  complexity = len(spec.operators) + float(spec.params.get('n_kernels', 0)) / 256.0 + float(spec.params.get('n_shapelets', 0)) / 64.0
91
  metrics['score'] = score_from_metrics(metrics, cost_weight=cost_weight, complexity=complexity)
92
  stages.append(StageResult('validation', True, metrics))
 
 
 
 
 
 
 
93
  stress_reports: dict[str, Any] = {}
94
  stress_scores = []
 
95
  for i, (name, fn) in enumerate(STRESS_TRANSFORMS.items()):
96
  if i >= max_stress_transforms:
97
  break
98
- Xs = fn(valid.X)
99
- ps = model.predict(Xs, groups=valid.subject, time_index=valid.time_index)
100
- mr = classification_report_dict(valid.y, ps, label_names=valid.label_names)
 
 
 
101
  stress_reports[name] = {'macro_f1': mr['macro_f1'], 'worst_class_f1': mr['worst_class_f1'], 'balanced_accuracy': mr['balanced_accuracy']}
102
  stress_scores.append(float(mr['macro_f1']))
 
 
 
 
 
 
 
103
  stress_macro = float(np.mean(stress_scores)) if stress_scores else float(metrics['macro_f1'])
104
  stages.append(StageResult('stress', True, {'stress_macro_f1': stress_macro, 'stress_reports': stress_reports}))
105
  pred_unique = int(len(np.unique(pred)))
@@ -110,5 +128,5 @@ def evaluate_candidate_cascade(spec: CandidateSpec, train: HARData, valid: HARDa
110
  gap = float(metrics['macro_f1']) - stress_macro
111
  cascade_score = float(metrics['score'] + robustness_weight * stress_macro - 0.05 * max(gap, 0.0))
112
  result = dict(metrics)
113
- result.update({'candidate': spec.to_dict(), 'operator_certificate': TemporalProgram.from_candidate(spec).certificate(), 'stress_macro_f1': stress_macro, 'validation_to_stress_gap': gap, 'cascade_score': cascade_score})
114
  return CascadeResult(bool(anti.passed), cascade_score, stages, result)
 
8
  from wisp.core.metrics import classification_report_dict, score_from_metrics
9
  from wisp.cpu.algorithms import build_cpu_estimator, estimate_pickle_size_mb
10
  from wisp.search.grammar import CPU_FAMILIES, CandidateSpec, normalize_spec
11
+ from wisp.search.operator_program import candidate_certificate
12
 
13
  @dataclass
14
  class StageResult:
 
47
  return StageResult('spec_validity', False, {}, 'n_shapelets outside allowed range')
48
  if 'symbolic_bins' in p and (not 2 <= int(p['symbolic_bins']) <= 32):
49
  return StageResult('spec_validity', False, {}, 'symbolic_bins outside allowed range')
50
+ return StageResult('spec_validity', True, candidate_certificate(spec))
51
 
52
  def _axis_sign_flip(X: np.ndarray) -> np.ndarray:
53
  out = X.copy()
 
71
  return X + rng.normal(0.0, 1.0, size=X.shape) * scale
72
  STRESS_TRANSFORMS = {'axis_sign_flip': _axis_sign_flip, 'time_shift': _time_shift, 'channel_dropout': _channel_dropout, 'sensor_noise': _noise}
73
 
74
+ def evaluate_candidate_cascade(spec: CandidateSpec, train: HARData, valid: HARData, *, cost_weight: float=0.01, robustness_weight: float=0.1, max_stress_transforms: int=4, estimator_factory=build_cpu_estimator, stress_inputs=None, reporter=classification_report_dict, stress_predictor=None, stress_gate=None) -> CascadeResult:
75
  spec = normalize_spec(spec)
76
  stages: list[StageResult] = []
77
  valid_stage = validate_candidate_spec(spec)
78
  stages.append(valid_stage)
79
  if not valid_stage.passed:
80
  return CascadeResult(False, -1000000000.0, stages, {'candidate': spec.to_dict(), 'score': -1000000000.0})
81
+ model = estimator_factory(spec.to_dict())
82
  t0 = time.perf_counter()
83
  model.fit(train.X, train.y, groups=train.subject, time_index=train.time_index, n_classes=train.n_classes)
84
  fit_s = time.perf_counter() - t0
85
  t1 = time.perf_counter()
86
  pred = model.predict(valid.X, groups=valid.subject, time_index=valid.time_index)
87
  pred_s = time.perf_counter() - t1
88
+ size_mb = estimate_pickle_size_mb(model) if stress_gate is None else None
89
+ metrics = reporter(valid.y, pred, fit_seconds=fit_s, predict_seconds=pred_s, model_size_mb=size_mb, label_names=valid.label_names)
90
  complexity = len(spec.operators) + float(spec.params.get('n_kernels', 0)) / 256.0 + float(spec.params.get('n_shapelets', 0)) / 64.0
91
  metrics['score'] = score_from_metrics(metrics, cost_weight=cost_weight, complexity=complexity)
92
  stages.append(StageResult('validation', True, metrics))
93
+ if stress_gate is not None:
94
+ certificate = stress_gate(metrics, [])
95
+ if certificate is not None:
96
+ stages.append(StageResult('stress_upper_bound', True, certificate))
97
+ return CascadeResult(False, -1e9, stages,
98
+ {**metrics,'candidate':spec.to_dict(),'prune_certificate':certificate})
99
+ metrics['model_size_mb'] = estimate_pickle_size_mb(model)
100
  stress_reports: dict[str, Any] = {}
101
  stress_scores = []
102
+ batched = stress_predictor(model) if stress_predictor is not None else None
103
  for i, (name, fn) in enumerate(STRESS_TRANSFORMS.items()):
104
  if i >= max_stress_transforms:
105
  break
106
+ if batched is None:
107
+ Xs = stress_inputs[name] if stress_inputs is not None and name in stress_inputs else fn(valid.X)
108
+ ps = model.predict(Xs, groups=valid.subject, time_index=valid.time_index)
109
+ else:
110
+ ps = batched[name]
111
+ mr = reporter(valid.y, ps, label_names=valid.label_names)
112
  stress_reports[name] = {'macro_f1': mr['macro_f1'], 'worst_class_f1': mr['worst_class_f1'], 'balanced_accuracy': mr['balanced_accuracy']}
113
  stress_scores.append(float(mr['macro_f1']))
114
+ if stress_gate is not None and batched is None and len(stress_scores)<max_stress_transforms:
115
+ certificate = stress_gate(metrics,stress_scores)
116
+ if certificate is not None:
117
+ stages.append(StageResult('stress_partial', True, {'stress_reports':stress_reports}))
118
+ stages.append(StageResult('stress_upper_bound', True, certificate))
119
+ return CascadeResult(False, -1e9, stages,
120
+ {**metrics,'candidate':spec.to_dict(),'prune_certificate':certificate})
121
  stress_macro = float(np.mean(stress_scores)) if stress_scores else float(metrics['macro_f1'])
122
  stages.append(StageResult('stress', True, {'stress_macro_f1': stress_macro, 'stress_reports': stress_reports}))
123
  pred_unique = int(len(np.unique(pred)))
 
128
  gap = float(metrics['macro_f1']) - stress_macro
129
  cascade_score = float(metrics['score'] + robustness_weight * stress_macro - 0.05 * max(gap, 0.0))
130
  result = dict(metrics)
131
+ result.update({'candidate': spec.to_dict(), 'operator_certificate': candidate_certificate(spec), 'stress_macro_f1': stress_macro, 'validation_to_stress_gap': gap, 'cascade_score': cascade_score})
132
  return CascadeResult(bool(anti.passed), cascade_score, stages, result)
src/wisp/search/controller.py CHANGED
@@ -16,8 +16,8 @@ from wisp.cpu.algorithms import build_cpu_estimator, estimate_pickle_size_mb
16
  from wisp.search.cascade import evaluate_candidate_cascade
17
  from wisp.search.dataset_fingerprint import DatasetFingerprint, compute_dataset_fingerprint
18
  from wisp.search.grammar import CandidateSpec, initial_candidates, mutate_candidate, normalize_spec
19
- from wisp.search.operator_program import TemporalProgram
20
- from wisp.search.posterior import MotifPosterior, island_key, sample_parent
21
  from wisp.search.profiles import SearchProfile, get_profile
22
  from wisp.utils.jsonl import append_jsonl, read_jsonl
23
 
@@ -133,11 +133,13 @@ class WISPSearchController:
133
  return [spec for spec in specs[:count]]
134
 
135
  def _random_spec(self, rng: np.random.Generator, fingerprint: DatasetFingerprint | None) -> CandidateSpec:
136
- bases = initial_candidates(seed=int(rng.integers(0, 2 ** 31 - 1)), include_v2=self.profile.include_v2_families, route='cpu')
137
- spec = CandidateSpec.from_dict(bases[int(rng.integers(0, len(bases)))].to_dict())
 
 
138
  depth = int(rng.integers(1, 5))
139
  for _ in range(depth):
140
- spec = mutate_candidate(spec, rng=rng, mutation_bias=None, motif_weights=None if fingerprint is None else fingerprint.motif_weights())
141
  spec.parent_id = None
142
  spec.mutation = f'random_depth_{depth}'
143
  constrained = self._constrain_spec(spec)
@@ -146,8 +148,11 @@ class WISPSearchController:
146
  return constrained
147
 
148
  def _record(self, *, db_path: Path, candidate_id: str, generation: int, spec: CandidateSpec, status: str, result: dict[str, Any], fingerprint: DatasetFingerprint | None, parent_id: str | None) -> dict[str, Any]:
149
- row = {'id': candidate_id, 'generation': int(generation), 'parent_id': parent_id, 'island': island_key(spec), 'status': status, 'profile': self.profile.to_dict(), 'dataset_fingerprint': None if fingerprint is None else fingerprint.to_dict(), 'motif_weights': None if fingerprint is None else fingerprint.motif_weights(), 'operator_certificate': TemporalProgram.from_candidate(spec).certificate(), 'result': result, 'spec': spec.to_dict()}
150
- append_jsonl(db_path, row)
 
 
 
151
  return row
152
 
153
  def _final_refit(self, best: dict[str, Any], out_dir: Path) -> dict[str, Any] | None:
@@ -170,6 +175,15 @@ class WISPSearchController:
170
  return metrics
171
 
172
  def run(self) -> dict[str, Any]:
 
 
 
 
 
 
 
 
 
173
  out_dir = Path(self.output)
174
  out_dir.mkdir(parents=True, exist_ok=True)
175
  db_path = out_dir / 'program_database.jsonl'
@@ -181,9 +195,14 @@ class WISPSearchController:
181
  return old
182
  if db_path.exists() and db_path.stat().st_size:
183
  raise RuntimeError(f'Refusing to append to non-empty search database {db_path}')
 
184
  rng = np.random.default_rng(self.seed)
185
  fingerprint = compute_dataset_fingerprint(self.train, seed=self.seed) if self.profile.use_fingerprint else None
186
  family_transfer, mutation_transfer = ({}, {})
 
 
 
 
187
  posterior = MotifPosterior()
188
  archive: list[CandidateSpec] = []
189
  population: list[tuple[str, CandidateSpec, dict[str, Any]]] = []
@@ -213,12 +232,15 @@ class WISPSearchController:
213
  while evaluated < budget and generation < int(self.generations):
214
  generation += 1
215
  remaining = budget - evaluated
216
- proposal_count = min(max(self.population - max(2, self.population // 3), 1), remaining)
 
 
217
  proposals: list[tuple[str, str | None, CandidateSpec]] = []
218
  attempts = 0
219
  mutation_bias = posterior.mutation_bias(fingerprint, motif_mode=self.profile.motif_mode, transfer_bias=mutation_transfer) if self.profile.use_posterior else mutation_transfer if self.profile.transfer_prior else {}
220
  for disabled in self.profile.disabled_mutations:
221
  mutation_bias.pop(disabled, None)
 
222
  while len(proposals) < proposal_count:
223
  attempts += 1
224
  if attempts > proposal_count * 100:
@@ -227,10 +249,10 @@ class WISPSearchController:
227
  parent_id = None
228
  child = self._random_spec(rng, fingerprint)
229
  else:
230
- parent_id, parent, parent_result = sample_parent(population, rng, use_islands=self.profile.use_islands, score_key='breeding_score')
231
  child = None
232
  if child is None:
233
- child = mutate_candidate(parent, rng=rng, parent_id=parent_id, mutation_bias=mutation_bias, motif_weights=None if fingerprint is None else fingerprint.motif_weights())
234
  child = self._constrain_spec(child)
235
  if child is None:
236
  continue
@@ -239,14 +261,15 @@ class WISPSearchController:
239
  continue
240
  seen.add(key)
241
  index = evaluated + len(proposals)
242
- proposals.append((_stable_id(seed=self.seed, generation=generation, index=index, spec=child), parent_id, child))
 
243
  evaluated_results = self._evaluate_many([spec for _, _, spec in proposals])
244
  new_items: list[tuple[str, CandidateSpec, dict[str, Any]]] = []
245
- for (cid, parent_id, spec), (status, result) in zip(proposals, evaluated_results):
246
  novelty = posterior.novelty_bonus(spec, archive) if self.profile.use_novelty and status == 'ok' else 0.0
247
  result['novelty_bonus'] = float(novelty)
248
  result['breeding_score'] = float(result.get('predictive_utility', -1000000000.0)) + float(novelty)
249
- row = self._record(db_path=db_path, candidate_id=cid, generation=generation, spec=spec, status=status, result=result, fingerprint=fingerprint, parent_id=parent_id)
250
  rows.append(row)
251
  archive.append(spec)
252
  if status == 'ok':
@@ -262,6 +285,9 @@ class WISPSearchController:
262
  population = (population + new_items)[-int(self.population):]
263
  if not population:
264
  raise RuntimeError(f'No accepted candidate after generation {generation}')
 
 
 
265
  ok_rows = [row for row in rows if row['status'] == 'ok']
266
  if not ok_rows:
267
  raise RuntimeError('Search completed without an accepted candidate')
 
16
  from wisp.search.cascade import evaluate_candidate_cascade
17
  from wisp.search.dataset_fingerprint import DatasetFingerprint, compute_dataset_fingerprint
18
  from wisp.search.grammar import CandidateSpec, initial_candidates, mutate_candidate, normalize_spec
19
+ from wisp.search.operator_program import candidate_certificate
20
+ from wisp.search.posterior import MotifPosterior, island_key, prepare_parents
21
  from wisp.search.profiles import SearchProfile, get_profile
22
  from wisp.utils.jsonl import append_jsonl, read_jsonl
23
 
 
133
  return [spec for spec in specs[:count]]
134
 
135
  def _random_spec(self, rng: np.random.Generator, fingerprint: DatasetFingerprint | None) -> CandidateSpec:
136
+ template_seed=int(rng.integers(0, 2 ** 31 - 1))
137
+ template=self._templates[int(rng.integers(0,len(self._templates)))]
138
+ params=dict(template.params);params['seed']+=template_seed
139
+ spec=CandidateSpec(template.name,params,list(template.operators),template.route,template.parent_id,template.mutation)
140
  depth = int(rng.integers(1, 5))
141
  for _ in range(depth):
142
+ spec = mutate_candidate(spec, rng=rng, mutation_bias=None, motif_weights=self._motif_weights)
143
  spec.parent_id = None
144
  spec.mutation = f'random_depth_{depth}'
145
  constrained = self._constrain_spec(spec)
 
148
  return constrained
149
 
150
  def _record(self, *, db_path: Path, candidate_id: str, generation: int, spec: CandidateSpec, status: str, result: dict[str, Any], fingerprint: DatasetFingerprint | None, parent_id: str | None) -> dict[str, Any]:
151
+ row = {'id': candidate_id, 'generation': int(generation), 'parent_id': parent_id, 'island': island_key(spec), 'status': status, 'profile': self._profile_dict, 'dataset_fingerprint': self._fingerprint_dict, 'motif_weights': self._motif_weights, 'operator_certificate': candidate_certificate(spec), 'result': result, 'spec': spec.to_dict()}
152
+ if self._ledger is None:
153
+ append_jsonl(db_path,row)
154
+ else:
155
+ self._ledger.write(json.dumps(row,ensure_ascii=False,sort_keys=True)+'\n')
156
  return row
157
 
158
  def _final_refit(self, best: dict[str, Any], out_dir: Path) -> dict[str, Any] | None:
 
175
  return metrics
176
 
177
  def run(self) -> dict[str, Any]:
178
+ self._ledger=None
179
+ try:
180
+ return self._run()
181
+ finally:
182
+ if self._ledger is not None:
183
+ self._ledger.close()
184
+ self._ledger=None
185
+
186
+ def _run(self) -> dict[str, Any]:
187
  out_dir = Path(self.output)
188
  out_dir.mkdir(parents=True, exist_ok=True)
189
  db_path = out_dir / 'program_database.jsonl'
 
195
  return old
196
  if db_path.exists() and db_path.stat().st_size:
197
  raise RuntimeError(f'Refusing to append to non-empty search database {db_path}')
198
+ self._ledger=db_path.open('a',encoding='utf-8')
199
  rng = np.random.default_rng(self.seed)
200
  fingerprint = compute_dataset_fingerprint(self.train, seed=self.seed) if self.profile.use_fingerprint else None
201
  family_transfer, mutation_transfer = ({}, {})
202
+ self._templates=initial_candidates(seed=0,include_v2=self.profile.include_v2_families,route='cpu')
203
+ self._motif_weights=None if fingerprint is None else fingerprint.motif_weights()
204
+ self._fingerprint_dict=None if fingerprint is None else fingerprint.to_dict()
205
+ self._profile_dict=self.profile.to_dict()
206
  posterior = MotifPosterior()
207
  archive: list[CandidateSpec] = []
208
  population: list[tuple[str, CandidateSpec, dict[str, Any]]] = []
 
232
  while evaluated < budget and generation < int(self.generations):
233
  generation += 1
234
  remaining = budget - evaluated
235
+ width = max(self.population - max(2, self.population // 3), 1)
236
+ coalesce = getattr(self, "coalesce_random", False)
237
+ proposal_count = min((self.generations-generation+1)*width if coalesce else width, remaining)
238
  proposals: list[tuple[str, str | None, CandidateSpec]] = []
239
  attempts = 0
240
  mutation_bias = posterior.mutation_bias(fingerprint, motif_mode=self.profile.motif_mode, transfer_bias=mutation_transfer) if self.profile.use_posterior else mutation_transfer if self.profile.transfer_prior else {}
241
  for disabled in self.profile.disabled_mutations:
242
  mutation_bias.pop(disabled, None)
243
+ draw=prepare_parents(population,use_islands=self.profile.use_islands) if self.profile.search_strategy=='evolution' else None
244
  while len(proposals) < proposal_count:
245
  attempts += 1
246
  if attempts > proposal_count * 100:
 
249
  parent_id = None
250
  child = self._random_spec(rng, fingerprint)
251
  else:
252
+ parent_id, parent, parent_result = draw(rng)
253
  child = None
254
  if child is None:
255
+ child = mutate_candidate(parent, rng=rng, parent_id=parent_id, mutation_bias=mutation_bias, motif_weights=self._motif_weights)
256
  child = self._constrain_spec(child)
257
  if child is None:
258
  continue
 
261
  continue
262
  seen.add(key)
263
  index = evaluated + len(proposals)
264
+ logical_generation = generation + len(proposals)//width if coalesce else generation
265
+ proposals.append((_stable_id(seed=self.seed, generation=logical_generation, index=index, spec=child), parent_id, child))
266
  evaluated_results = self._evaluate_many([spec for _, _, spec in proposals])
267
  new_items: list[tuple[str, CandidateSpec, dict[str, Any]]] = []
268
+ for position, ((cid, parent_id, spec), (status, result)) in enumerate(zip(proposals, evaluated_results)):
269
  novelty = posterior.novelty_bonus(spec, archive) if self.profile.use_novelty and status == 'ok' else 0.0
270
  result['novelty_bonus'] = float(novelty)
271
  result['breeding_score'] = float(result.get('predictive_utility', -1000000000.0)) + float(novelty)
272
+ row = self._record(db_path=db_path, candidate_id=cid, generation=generation+position//width if coalesce else generation, spec=spec, status=status, result=result, fingerprint=fingerprint, parent_id=parent_id)
273
  rows.append(row)
274
  archive.append(spec)
275
  if status == 'ok':
 
285
  population = (population + new_items)[-int(self.population):]
286
  if not population:
287
  raise RuntimeError(f'No accepted candidate after generation {generation}')
288
+ self._ledger.flush()
289
+ if getattr(self,'retain_rows',False):
290
+ self.evaluated_rows=rows
291
  ok_rows = [row for row in rows if row['status'] == 'ok']
292
  if not ok_rows:
293
  raise RuntimeError('Search completed without an accepted candidate')
src/wisp/search/grammar.py CHANGED
@@ -1,6 +1,8 @@
1
  # Paper-scoped implementation; see SOURCE_PROVENANCE.json.
2
  from __future__ import annotations
3
- from dataclasses import asdict, dataclass, field
 
 
4
  from typing import Any, Literal
5
  import numpy as np
6
  from wisp.search.motifs import FAMILY_MOTIF_WEIGHTS, MOTIF_TO_MUTATION_PRIOR, family_motif_match
@@ -16,7 +18,8 @@ class CandidateSpec:
16
  mutation: str | None = None
17
 
18
  def to_dict(self) -> dict[str, Any]:
19
- return asdict(self)
 
20
 
21
  @staticmethod
22
  def from_dict(d: dict[str, Any]) -> 'CandidateSpec':
@@ -34,6 +37,15 @@ def initial_cpu_candidates(seed: int=42, *, include_v2: bool=True) -> list[Candi
34
  base.extend([CandidateSpec(name='invariant_interval_shape_forest', params={'classifier': 'extratrees', 'use_hmm': True, 'segment_sizes': (4, 8, 16), 'symbolic_bins': 6, 'n_shapelets': 32, 'seed': seed + 6}, operators=['zscore', 'magnitude', 'jerk', 'gram_eig', 'dyadic_intervals', 'quantile_distribution', 'random_shapelet', 'extratrees', 'hmm']), CandidateSpec(name='state_rocket_interval_hmm', params={'n_kernels': 384, 'n_intervals': 24, 'classifier': 'ridge', 'use_hmm': True, 'seed': seed + 7}, operators=['zscore', 'random_conv', 'ppv_pool', 'dyadic_intervals', 'quantile_distribution', 'symbolic_transition', 'ridge', 'hmm']), CandidateSpec(name='posterior_cpu_ensemble', params={'use_hmm': True, 'seed': seed + 8, 'weights': (0.4, 0.35, 0.25)}, operators=['posterior_weighting', 'random_conv', 'invariant_physics', 'symbolic_transition', 'probability_ensemble', 'hmm'])])
35
  return base
36
 
 
 
 
 
 
 
 
 
 
37
  def initial_candidates(seed: int=42, *, include_v2: bool=True, route: Route='cpu', population: int | None=None) -> list[CandidateSpec]:
38
  if route != 'cpu':
39
  raise ValueError('Only CPU search is released')
@@ -61,21 +73,30 @@ def _replace_classifier_ops(ops: list[str], classifier: str) -> list[str]:
61
 
62
  def _weighted_choice(items: list[str], bias: dict[str, float] | None, rng: np.random.Generator) -> str:
63
  if not bias:
64
- return str(rng.choice(items))
65
- weights = np.asarray([max(float(bias.get(item, 0.0)), 0.0) + 1e-06 for item in items], dtype=np.float64)
66
- weights /= weights.sum()
67
- return str(rng.choice(items, p=weights))
 
 
 
 
 
 
 
 
 
 
68
 
69
  def _family_choice(route: Route, motif_weights: dict[str, float] | None, rng: np.random.Generator) -> str:
70
  families = CPU_FAMILIES if route == 'cpu' else GPU_FAMILIES
71
  if not motif_weights:
72
- return str(rng.choice(families))
73
- scores = np.asarray([max(family_motif_match(family, motif_weights).score, 0.0) + 0.1 for family in families], dtype=np.float64)
74
- scores /= scores.sum()
75
- return str(rng.choice(families, p=scores))
76
 
77
  def mutate_cpu_candidate(parent: CandidateSpec, *, rng: np.random.Generator, parent_id: str | None=None, mutation_bias: dict[str, float] | None=None, motif_weights: dict[str, float] | None=None) -> CandidateSpec:
78
- child = normalize_spec(CandidateSpec.from_dict(parent.to_dict()))
79
  ops = list(child.operators)
80
  params = dict(child.params)
81
  mutation = _weighted_choice(list(CPU_MUTATIONS), mutation_bias, rng)
@@ -110,7 +131,7 @@ def mutate_cpu_candidate(parent: CandidateSpec, *, rng: np.random.Generator, par
110
  if child.name == 'state_rocket_interval_hmm':
111
  params['n_intervals'] = int(rng.choice([8, 16, 24, 32, 48]))
112
  else:
113
- child = CandidateSpec.from_dict(initial_cpu_candidates(seed=int(rng.integers(0, 10000)))[7].to_dict())
114
  params, ops = (dict(child.params), list(child.operators))
115
  elif mutation == 'change_shapelets':
116
  if child.name == 'invariant_interval_shape_forest':
@@ -119,23 +140,22 @@ def mutate_cpu_candidate(parent: CandidateSpec, *, rng: np.random.Generator, par
119
  values = [8, 16, 24]
120
  params['n_shapelets'] = int(rng.choice(values))
121
  else:
122
- child = CandidateSpec.from_dict(initial_cpu_candidates(seed=int(rng.integers(0, 10000)))[6].to_dict())
123
  params, ops = (dict(child.params), list(child.operators))
124
  elif mutation == 'reweight_ensemble':
125
  if child.name != 'posterior_cpu_ensemble':
126
- child = CandidateSpec.from_dict(initial_cpu_candidates(seed=int(rng.integers(0, 10000)))[8].to_dict())
127
  params, ops = (dict(child.params), list(child.operators))
128
  params['weights'] = tuple((float(x) for x in rng.dirichlet(np.ones(3)).tolist()))
129
  elif mutation == 'insert_invariance_motif':
130
- child = CandidateSpec.from_dict(initial_cpu_candidates(seed=int(rng.integers(0, 10000)))[6].to_dict())
131
  params, ops = (dict(child.params), list(child.operators))
132
  elif mutation == 'insert_state_motif':
133
- child = CandidateSpec.from_dict(initial_cpu_candidates(seed=int(rng.integers(0, 10000)))[7].to_dict())
134
  params, ops = (dict(child.params), list(child.operators))
135
  elif mutation == 'switch_family':
136
  family = _family_choice('cpu', motif_weights, rng)
137
- templates = [s for s in initial_cpu_candidates(seed=int(rng.integers(0, 10000))) if s.name == family]
138
- child = CandidateSpec.from_dict((templates[0] if templates else initial_cpu_candidates(seed=int(rng.integers(0, 10000)))[0]).to_dict())
139
  params, ops = (dict(child.params), list(child.operators))
140
  params['seed'] = int(params.get('seed', 42)) + int(rng.integers(1, 100000))
141
  return CandidateSpec(child.name, params=params, operators=ops, route='cpu', parent_id=parent_id, mutation=mutation)
 
1
  # Paper-scoped implementation; see SOURCE_PROVENANCE.json.
2
  from __future__ import annotations
3
+ from dataclasses import dataclass, field
4
+ from copy import deepcopy
5
+ from functools import lru_cache
6
  from typing import Any, Literal
7
  import numpy as np
8
  from wisp.search.motifs import FAMILY_MOTIF_WEIGHTS, MOTIF_TO_MUTATION_PRIOR, family_motif_match
 
18
  mutation: str | None = None
19
 
20
  def to_dict(self) -> dict[str, Any]:
21
+ return dict(name=self.name, params=deepcopy(self.params), operators=list(self.operators),
22
+ route=self.route, parent_id=self.parent_id, mutation=self.mutation)
23
 
24
  @staticmethod
25
  def from_dict(d: dict[str, Any]) -> 'CandidateSpec':
 
37
  base.extend([CandidateSpec(name='invariant_interval_shape_forest', params={'classifier': 'extratrees', 'use_hmm': True, 'segment_sizes': (4, 8, 16), 'symbolic_bins': 6, 'n_shapelets': 32, 'seed': seed + 6}, operators=['zscore', 'magnitude', 'jerk', 'gram_eig', 'dyadic_intervals', 'quantile_distribution', 'random_shapelet', 'extratrees', 'hmm']), CandidateSpec(name='state_rocket_interval_hmm', params={'n_kernels': 384, 'n_intervals': 24, 'classifier': 'ridge', 'use_hmm': True, 'seed': seed + 7}, operators=['zscore', 'random_conv', 'ppv_pool', 'dyadic_intervals', 'quantile_distribution', 'symbolic_transition', 'ridge', 'hmm']), CandidateSpec(name='posterior_cpu_ensemble', params={'use_hmm': True, 'seed': seed + 8, 'weights': (0.4, 0.35, 0.25)}, operators=['posterior_weighting', 'random_conv', 'invariant_physics', 'symbolic_transition', 'probability_ensemble', 'hmm'])])
38
  return base
39
 
40
+ # Mutation picks one template. Build its immutable baseline once, then copy its
41
+ # public fields and add the requested seed offset, preserving the RNG stream.
42
+ _TEMPLATES = initial_cpu_candidates(0)
43
+ _FIRST = {t.name:next(i for i,c in enumerate(_TEMPLATES) if c.name==t.name) for t in _TEMPLATES}
44
+
45
+ def _template(index, seed):
46
+ t=_TEMPLATES[index]
47
+ return CandidateSpec(t.name, {**t.params,'seed':t.params['seed']+seed}, list(t.operators))
48
+
49
  def initial_candidates(seed: int=42, *, include_v2: bool=True, route: Route='cpu', population: int | None=None) -> list[CandidateSpec]:
50
  if route != 'cpu':
51
  raise ValueError('Only CPU search is released')
 
73
 
74
  def _weighted_choice(items: list[str], bias: dict[str, float] | None, rng: np.random.Generator) -> str:
75
  if not bias:
76
+ return items[int(rng.integers(len(items)))]
77
+ weights=_mutation_weights(tuple(bias.get(item,0.) for item in items))
78
+ return items[int(rng.choice(len(items),p=weights))]
79
+
80
+ @lru_cache(maxsize=128)
81
+ def _mutation_weights(values):
82
+ weights=np.asarray([max(float(v),0.)+1e-6 for v in values])
83
+ return weights/weights.sum()
84
+
85
+ @lru_cache(maxsize=32)
86
+ def _family_weights(families,weights):
87
+ motif_weights=dict(weights)
88
+ scores=np.asarray([max(family_motif_match(f,motif_weights).score,0.)+.1 for f in families])
89
+ return scores/scores.sum()
90
 
91
  def _family_choice(route: Route, motif_weights: dict[str, float] | None, rng: np.random.Generator) -> str:
92
  families = CPU_FAMILIES if route == 'cpu' else GPU_FAMILIES
93
  if not motif_weights:
94
+ return families[int(rng.integers(len(families)))]
95
+ p=_family_weights(families,tuple(motif_weights.items()))
96
+ return families[int(rng.choice(len(families),p=p))]
 
97
 
98
  def mutate_cpu_candidate(parent: CandidateSpec, *, rng: np.random.Generator, parent_id: str | None=None, mutation_bias: dict[str, float] | None=None, motif_weights: dict[str, float] | None=None) -> CandidateSpec:
99
+ child = normalize_spec(parent)
100
  ops = list(child.operators)
101
  params = dict(child.params)
102
  mutation = _weighted_choice(list(CPU_MUTATIONS), mutation_bias, rng)
 
131
  if child.name == 'state_rocket_interval_hmm':
132
  params['n_intervals'] = int(rng.choice([8, 16, 24, 32, 48]))
133
  else:
134
+ child = _template(7,int(rng.integers(0,10000)))
135
  params, ops = (dict(child.params), list(child.operators))
136
  elif mutation == 'change_shapelets':
137
  if child.name == 'invariant_interval_shape_forest':
 
140
  values = [8, 16, 24]
141
  params['n_shapelets'] = int(rng.choice(values))
142
  else:
143
+ child = _template(6,int(rng.integers(0,10000)))
144
  params, ops = (dict(child.params), list(child.operators))
145
  elif mutation == 'reweight_ensemble':
146
  if child.name != 'posterior_cpu_ensemble':
147
+ child = _template(8,int(rng.integers(0,10000)))
148
  params, ops = (dict(child.params), list(child.operators))
149
  params['weights'] = tuple((float(x) for x in rng.dirichlet(np.ones(3)).tolist()))
150
  elif mutation == 'insert_invariance_motif':
151
+ child = _template(6,int(rng.integers(0,10000)))
152
  params, ops = (dict(child.params), list(child.operators))
153
  elif mutation == 'insert_state_motif':
154
+ child = _template(7,int(rng.integers(0,10000)))
155
  params, ops = (dict(child.params), list(child.operators))
156
  elif mutation == 'switch_family':
157
  family = _family_choice('cpu', motif_weights, rng)
158
+ child = _template(_FIRST[family],int(rng.integers(0,10000)))
 
159
  params, ops = (dict(child.params), list(child.operators))
160
  params['seed'] = int(params.get('seed', 42)) + int(rng.integers(1, 100000))
161
  return CandidateSpec(child.name, params=params, operators=ops, route='cpu', parent_id=parent_id, mutation=mutation)
src/wisp/search/operator_program.py CHANGED
@@ -2,6 +2,7 @@
2
  from __future__ import annotations
3
  from dataclasses import asdict, dataclass, field
4
  from typing import Any
 
5
  import numpy as np
6
  from wisp.search.grammar import CandidateSpec
7
  from wisp.search.motifs import canonicalize_tags, family_motif_match
@@ -34,7 +35,7 @@ class TemporalProgram:
34
 
35
  @classmethod
36
  def from_candidate(cls, spec: CandidateSpec) -> 'TemporalProgram':
37
- nodes = [OPERATOR_LIBRARY.get(op, OperatorNode(op, 'unknown', cost=1.0)) for op in spec.operators]
38
  return cls(name=spec.name, route=spec.route, nodes=nodes, params=dict(spec.params), parent_id=spec.parent_id, mutation=spec.mutation)
39
 
40
  def to_candidate(self) -> CandidateSpec:
@@ -89,8 +90,27 @@ class TemporalProgram:
89
  return {k: float(min(v, 1.0)) for k, v in score.items()}
90
 
91
  def jaccard_novelty(a: CandidateSpec, b: CandidateSpec) -> float:
92
- A = set(a.operators)
93
- B = set(b.operators)
94
- if not A and (not B):
95
- return 0.0
96
- return 1.0 - len(A & B) / max(len(A | B), 1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from __future__ import annotations
3
  from dataclasses import asdict, dataclass, field
4
  from typing import Any
5
+ from functools import lru_cache
6
  import numpy as np
7
  from wisp.search.grammar import CandidateSpec
8
  from wisp.search.motifs import canonicalize_tags, family_motif_match
 
35
 
36
  @classmethod
37
  def from_candidate(cls, spec: CandidateSpec) -> 'TemporalProgram':
38
+ nodes = [OPERATOR_LIBRARY[op] if op in OPERATOR_LIBRARY else OperatorNode(op, 'unknown', cost=1.0) for op in spec.operators]
39
  return cls(name=spec.name, route=spec.route, nodes=nodes, params=dict(spec.params), parent_id=spec.parent_id, mutation=spec.mutation)
40
 
41
  def to_candidate(self) -> CandidateSpec:
 
90
  return {k: float(min(v, 1.0)) for k, v in score.items()}
91
 
92
  def jaccard_novelty(a: CandidateSpec, b: CandidateSpec) -> float:
93
+ return _distance(tuple(a.operators),tuple(b.operators))
94
+
95
+
96
+ @lru_cache(maxsize=4096)
97
+ def _certificate(name, route, nodes):
98
+ return TemporalProgram(name, route, list(nodes)).certificate()
99
+
100
+
101
+ def candidate_certificate(spec):
102
+ """Reuse statistics of immutable nodes; returned nested containers are private."""
103
+ nodes=tuple(OPERATOR_LIBRARY.get(op) or OperatorNode(op,'unknown') for op in spec.operators)
104
+ cached=_certificate(spec.name,spec.route,nodes)
105
+ return {k:(v.copy() if isinstance(v,(dict,list)) else v) for k,v in cached.items()}
106
+
107
+
108
+ @lru_cache(maxsize=2048)
109
+ def _opset(ops):
110
+ return frozenset(ops)
111
+
112
+
113
+ @lru_cache(maxsize=8192)
114
+ def _distance(a,b):
115
+ A,B=_opset(a),_opset(b)
116
+ return 1.0-len(A & B)/len(A | B) if A or B else 0.
src/wisp/search/posterior.py CHANGED
@@ -8,7 +8,7 @@ import numpy as np
8
  from wisp.search.dataset_fingerprint import DatasetFingerprint
9
  from wisp.search.grammar import CandidateSpec, initial_candidates
10
  from wisp.search.motifs import family_motif_match, mutation_prior_from_motifs
11
- from wisp.search.operator_program import TemporalProgram, jaccard_novelty
12
  from wisp.utils.jsonl import read_jsonl
13
 
14
  @dataclass
@@ -26,7 +26,7 @@ class MotifPosterior:
26
  self.operator_scores[op].append(score)
27
  if spec.mutation:
28
  self.mutation_scores[str(spec.mutation)].append(score)
29
- cert = TemporalProgram.from_candidate(spec).certificate()
30
  for motif in cert.get('canonical_motifs', []):
31
  self.motif_scores[str(motif)].append(score)
32
  self.n_records += 1
@@ -91,7 +91,7 @@ class MotifPosterior:
91
  return float(weight * np.mean([jaccard_novelty(spec, other) for other in archive[-50:]]))
92
 
93
  def island_key(spec: CandidateSpec) -> str:
94
- cert = TemporalProgram.from_candidate(spec).certificate()
95
  families = cert.get('family_trace', [])
96
  counts = Counter(families)
97
  top_family = counts.most_common(1)[0][0] if counts else spec.name
@@ -101,18 +101,22 @@ def island_key(spec: CandidateSpec) -> str:
101
  motif = motifs[0] if motifs else 'untagged'
102
  return f'{spec.route}:{top_family}:{cost_bin}:{motif}'
103
 
104
- def sample_parent(population: list[tuple[str, CandidateSpec, dict[str, Any]]], rng: np.random.Generator, *, use_islands: bool=True, score_key: str='breeding_score') -> tuple[str, CandidateSpec, dict[str, Any]]:
 
105
  if not population:
106
  raise ValueError('population must be non-empty')
107
- if not use_islands:
108
- members = sorted(population, key=lambda x: float(x[2].get(score_key, -1000000000.0)), reverse=True)
109
- rank = int(min(len(members) - 1, rng.geometric(0.55) - 1))
110
- return members[rank]
111
- islands: dict[str, list[tuple[str, CandidateSpec, dict[str, Any]]]] = defaultdict(list)
112
  for item in population:
113
- islands[island_key(item[1])].append(item)
114
- keys = list(islands)
115
- key = min(keys, key=lambda k: len(islands[k])) if rng.random() < 0.25 else str(rng.choice(keys))
116
- members = sorted(islands[key], key=lambda x: float(x[2].get(score_key, -1000000000.0)), reverse=True)
117
- rank = int(min(len(members) - 1, rng.geometric(0.55) - 1))
118
- return members[rank]
 
 
 
 
 
 
 
 
8
  from wisp.search.dataset_fingerprint import DatasetFingerprint
9
  from wisp.search.grammar import CandidateSpec, initial_candidates
10
  from wisp.search.motifs import family_motif_match, mutation_prior_from_motifs
11
+ from wisp.search.operator_program import candidate_certificate, jaccard_novelty
12
  from wisp.utils.jsonl import read_jsonl
13
 
14
  @dataclass
 
26
  self.operator_scores[op].append(score)
27
  if spec.mutation:
28
  self.mutation_scores[str(spec.mutation)].append(score)
29
+ cert = candidate_certificate(spec)
30
  for motif in cert.get('canonical_motifs', []):
31
  self.motif_scores[str(motif)].append(score)
32
  self.n_records += 1
 
91
  return float(weight * np.mean([jaccard_novelty(spec, other) for other in archive[-50:]]))
92
 
93
  def island_key(spec: CandidateSpec) -> str:
94
+ cert = candidate_certificate(spec)
95
  families = cert.get('family_trace', [])
96
  counts = Counter(families)
97
  top_family = counts.most_common(1)[0][0] if counts else spec.name
 
101
  motif = motifs[0] if motifs else 'untagged'
102
  return f'{spec.route}:{top_family}:{cost_bin}:{motif}'
103
 
104
+ def prepare_parents(population, *, use_islands=True, score_key='breeding_score'):
105
+ """Build the rank lists once for the lifetime of one fixed generation."""
106
  if not population:
107
  raise ValueError('population must be non-empty')
108
+ islands=defaultdict(list)
 
 
 
 
109
  for item in population:
110
+ islands[island_key(item[1]) if use_islands else None].append(item)
111
+ for members in islands.values():
112
+ members.sort(key=lambda x: float(x[2].get(score_key,-1000000000.0)),reverse=True)
113
+ keys=list(islands)
114
+ smallest=min(keys,key=lambda k:len(islands[k]))
115
+ def draw(rng):
116
+ key=(smallest if rng.random()<0.25 else str(rng.choice(keys))) if use_islands else None
117
+ members=islands[key]
118
+ return members[min(len(members)-1,int(rng.geometric(0.55))-1)]
119
+ return draw
120
+
121
+ def sample_parent(population,rng,*,use_islands=True,score_key='breeding_score'):
122
+ return prepare_parents(population,use_islands=use_islands,score_key=score_key)(rng)
src/wisp_fast/__init__.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ """Accelerated CPU engine for the WISP method family (fixed families, selection and search)."""
2
+ __version__ = "0.2.0"
src/wisp_fast/autocorr_lanes.h ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Each lane owns one window. The single-channel reduction follows the
2
+ contiguous NumPy tree, while strided multi-channel sums stay serial. */
3
+ #if defined(__x86_64__) && (defined(__GNUC__) || defined(__clang__)) && !defined(_WIN32)
4
+ #include <immintrin.h>
5
+ #define WISP_ACF_LANES 1
6
+ __attribute__((target("avx512f"),always_inline))
7
+ static inline __m512d acf_term(const double *a,const double *b,int j){
8
+ __m512d u=_mm512_loadu_pd(a+j*8);
9
+ return b?_mm512_mul_pd(u,_mm512_loadu_pd(b+j*8)):u;
10
+ }
11
+ __attribute__((target("avx512f")))
12
+ static __m512d acf_sum8(const double *a,const double *b,int n,int paired){
13
+ __m512d sum=_mm512_setzero_pd();
14
+ if(!paired || n<8){
15
+ if(paired)sum=_mm512_set1_pd(-0.);
16
+ for(int j=0;j<n;j++)sum=_mm512_add_pd(sum,acf_term(a,b,j));
17
+ return sum;
18
+ }
19
+ if(n>128){
20
+ int cut=n/16*8;
21
+ return _mm512_add_pd(acf_sum8(a,b,cut,1),acf_sum8(a+8*cut,b?b+8*cut:NULL,n-cut,1));
22
+ }
23
+ __m512d s[8];
24
+ for(int q=0;q<8;q++)s[q]=acf_term(a,b,q);
25
+ int j=8;
26
+ for(;j+8<=n;j+=8)for(int q=0;q<8;q++)s[q]=_mm512_add_pd(s[q],acf_term(a,b,j+q));
27
+ sum=_mm512_add_pd(_mm512_add_pd(_mm512_add_pd(s[0],s[1]),_mm512_add_pd(s[2],s[3])),
28
+ _mm512_add_pd(_mm512_add_pd(s[4],s[5]),_mm512_add_pd(s[6],s[7])));
29
+ for(;j<n;j++)sum=_mm512_add_pd(sum,acf_term(a,b,j));
30
+ return sum;
31
+ }
32
+ __attribute__((target("avx512f")))
33
+ static void acf_tile(const double *x,int t,int c,const long *lags,int k,
34
+ double *tile,double *z,double *out){
35
+ for(int ch=0;ch<c;ch++){
36
+ for(int j=0;j<t;j++)for(int i=0;i<8;i++)tile[8*j+i]=x[(i*t+j)*c+ch];
37
+ __m512d m=_mm512_div_pd(acf_sum8(tile,NULL,t,c==1),_mm512_set1_pd(t));
38
+ m=_mm512_add_pd(m,_mm512_setzero_pd());
39
+ for(int j=0;j<t;j++)_mm512_storeu_pd(tile+8*j,_mm512_sub_pd(_mm512_loadu_pd(tile+8*j),m));
40
+ if(z)for(int i=0;i<8;i++)for(int j=0;j<t;j++)z[(i*t+j)*c+ch]=tile[8*j+i];
41
+ __m512d den=_mm512_add_pd(acf_sum8(tile,tile,t,c==1),_mm512_set1_pd(1.e-12));
42
+ for(int l=0;l<k;l++){
43
+ long lag=lags[l];
44
+ __m512d result=_mm512_setzero_pd();
45
+ if(lag>0 && lag<t){
46
+ __m512d sum=acf_sum8(tile,tile+8*lag,t-lag,c==1);
47
+ result=_mm512_div_pd(_mm512_add_pd(sum,_mm512_setzero_pd()),den);
48
+ }
49
+ double vals[8];_mm512_storeu_pd(vals,result);
50
+ for(int i=0;i<8;i++)out[(i*k+l)*c+ch]=vals[i];
51
+ }
52
+ }
53
+ }
54
+ #endif
src/wisp_fast/cache.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Opt-in numeric feature cache. Keys include actual signals and fitted block state."""
2
+ from contextlib import contextmanager
3
+ from contextvars import ContextVar
4
+ from functools import lru_cache
5
+ import hashlib
6
+ import os
7
+ from pathlib import Path
8
+ import platform
9
+ import tempfile
10
+
11
+ import joblib
12
+ import numpy as np
13
+ import scipy
14
+
15
+ _SEARCH = ContextVar("wisp_search_memory", default=None)
16
+ _ACTIVE = ContextVar("wisp_feature_store", default=None)
17
+
18
+
19
+ @lru_cache(maxsize=1)
20
+ def _backend():
21
+ from wisp.cpu import features as original
22
+ from . import features as fast
23
+ digest = hashlib.sha256()
24
+ from importlib.util import find_spec
25
+ native = [find_spec("wisp_fast."+key) for key in ("_kernels","_cis_conv","_cis_intervals","_family_kernels")]
26
+ for path in [original.__file__, fast.__file__, __file__]+[s.origin for s in native if s is not None]:
27
+ if path:
28
+ digest.update(Path(path).read_bytes())
29
+ digest.update(repr((np.__version__,scipy.__version__,joblib.__version__, platform.machine(),
30
+ platform.processor())).encode())
31
+ return digest.hexdigest()
32
+
33
+
34
+ @contextmanager
35
+ def feature_cache(directory, *, max_entry_mb=512):
36
+ """Reuse exact NPY feature matrices. Use a private, same-host cache directory.
37
+
38
+ Caller controls the directory lifetime. Bound any single entry with max_entry_mb.
39
+ Atomic replacement supports racing workers. There is no cross-filesystem lock.
40
+ """
41
+ if directory is None:
42
+ yield
43
+ return
44
+ if max_entry_mb <= 0:
45
+ raise ValueError("max_entry_mb must be positive")
46
+ path = Path(directory) / _backend()
47
+ path.mkdir(parents=True, exist_ok=True)
48
+ token = _ACTIVE.set((path, max_entry_mb*1024**2))
49
+ try:
50
+ yield
51
+ finally:
52
+ _ACTIVE.reset(token)
53
+
54
+
55
+ def transform_blocks(blocks, X):
56
+ config = _ACTIVE.get()
57
+ shared_search = _SEARCH.get()
58
+ if shared_search is not None and config is None:
59
+ return shared_search.transform(blocks,X)
60
+ if config is None:
61
+ shared = (len(blocks) >= 2 and X.flags.c_contiguous and
62
+ all(getattr(b,"zscore",False) and hasattr(b,"transform_normalized") for b in blocks))
63
+ if shared:
64
+ from wisp.cpu.features import _as_3d
65
+ from .features import _normalize
66
+ z = _normalize(_as_3d(X))
67
+ return [b.transform_normalized(z) for b in blocks]
68
+ return [block.transform(X) for block in blocks]
69
+ directory, limit = config
70
+ signal = hashlib.sha256(np.ascontiguousarray(X).view(np.uint8)).digest()
71
+ features = []
72
+ for block in blocks:
73
+ # feature_names_ is display metadata produced during transform. Every
74
+ # fit-dependent quantity, including shapelet values, remains in the key.
75
+ state = {k:v for k,v in vars(block).items() if k not in ("feature_names_","_conv_plan","_quant_plan")}
76
+ key = joblib.hash((type(block).__module__, type(block).__qualname__, state,
77
+ X.dtype.str,X.shape,X.strides,signal),hash_name="sha1")
78
+ path = directory / (key+".npy")
79
+ try:
80
+ arr = np.load(path, allow_pickle=False, mmap_mode="r")
81
+ except FileNotFoundError:
82
+ arr = block.transform(X)
83
+ if arr.nbytes <= limit:
84
+ tmp = None
85
+ try:
86
+ with tempfile.NamedTemporaryFile(dir=directory, delete=False) as stream:
87
+ tmp = stream.name
88
+ np.save(stream, arr, allow_pickle=False)
89
+ os.replace(tmp,path)
90
+ finally:
91
+ if tmp and os.path.exists(tmp):
92
+ os.unlink(tmp)
93
+ else:
94
+ if hasattr(block, "feature_names_") and not block.feature_names_:
95
+ block.transform(X[:1])
96
+ features.append(arr)
97
+ return features
src/wisp_fast/cis_conv.c ADDED
@@ -0,0 +1,828 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Packed random convolution, preserving the tap accumulation order. The
2
+ multichannel exact path reuses the BLAS already linked into NumPy's
3
+ core, including its choice of fused operations. */
4
+ #define _GNU_SOURCE
5
+ #define PY_SSIZE_T_CLEAN
6
+ #include <Python.h>
7
+ #include <math.h>
8
+ #include <float.h>
9
+ #include <stdint.h>
10
+ #include <stdlib.h>
11
+ #include <string.h>
12
+ #ifndef _WIN32
13
+ #include <dlfcn.h>
14
+ #endif
15
+
16
+ typedef void (*gemv64_t)(int,int,int64_t,int64_t,double,const double*,int64_t,
17
+ const double*,int64_t,double,double*,int64_t);
18
+ static gemv64_t np_gemv;
19
+ static int attempted;
20
+ static int bind_numpy(void) {
21
+ if (!attempted) {
22
+ attempted = 1;
23
+ #ifndef _WIN32
24
+ PyObject *core = PyImport_ImportModule("numpy._core._multiarray_umath");
25
+ if (!core) { PyErr_Clear(); return 0; }
26
+ PyObject *file = PyModule_GetFilenameObject(core);
27
+ Py_DECREF(core);
28
+ if (file) {
29
+ const char *path = PyUnicode_AsUTF8(file);
30
+ void *h = path ? dlopen(path, RTLD_LAZY | RTLD_LOCAL) : NULL;
31
+ if (h) {
32
+ void *symbol = dlsym(h, "scipy_cblas_dgemv64_");
33
+ if (symbol) memcpy(&np_gemv, &symbol, sizeof np_gemv);
34
+ /* The NumPy module keeps this dependency loaded. */
35
+ dlclose(h);
36
+ }
37
+ Py_DECREF(file);
38
+ }
39
+ if (PyErr_Occurred()) PyErr_Clear();
40
+ #endif
41
+ }
42
+ return np_gemv != NULL;
43
+ }
44
+ static PyObject *has_blas(PyObject *self,PyObject *args) {
45
+ return PyBool_FromLong(bind_numpy());
46
+ }
47
+ /* This order is specific to the inspected NumPy OpenBLAS SkylakeX kernel
48
+ shipped with the pinned Linux x86-64 wheel. Python additionally verifies
49
+ the library's full SHA-256. Other builds use actual calls to GEMV. */
50
+ #if defined(__x86_64__) && (defined(__GNUC__) || defined(__clang__)) && !defined(_WIN32)
51
+ #include <immintrin.h>
52
+ #define HAVE_FAST_X86 1
53
+ /* Four vectors share weight broadcasts. Keep pertap contributions in four
54
+ registers and only materialize responses once, after the last tap. */
55
+ __attribute__((target("avx2,fma")))
56
+ static void skylakex_project(double *r,const double *base,Py_ssize_t lda,Py_ssize_t frames,
57
+ const double *weights,int c,int64_t len,int64_t dilation) {
58
+ Py_ssize_t j=0;
59
+ for (;j+16<=frames;j+=16) {
60
+ __m256d acc[4]={_mm256_setzero_pd(),_mm256_setzero_pd(),_mm256_setzero_pd(),_mm256_setzero_pd()};
61
+ for(int h=0;h<len;h++){
62
+ const double *v=weights+3*h,*a=base+j+h*dilation;
63
+ __m256d w0=_mm256_set1_pd(v[0]),w1=_mm256_set1_pd(v[1]),w2=_mm256_set1_pd(v[2]);
64
+ for(int q=0;q<4;q++) {
65
+ __m256d dot=_mm256_mul_pd(_mm256_loadu_pd(a+4*q+lda),w1);
66
+ dot=_mm256_fmadd_pd(_mm256_loadu_pd(a+4*q),w0,dot);
67
+ if(c==3) dot=_mm256_add_pd(dot,_mm256_mul_pd(_mm256_loadu_pd(a+4*q+2*lda),w2));
68
+ acc[q]=_mm256_add_pd(acc[q],dot);
69
+ }
70
+ }
71
+ for(int q=0;q<4;q++) _mm256_storeu_pd(r+j+4*q,acc[q]);
72
+ }
73
+ for(;j+4<=frames;j+=4){
74
+ __m256d acc=_mm256_setzero_pd();
75
+ for(int h=0;h<len;h++){
76
+ const double *v=weights+3*h,*a=base+j+h*dilation;
77
+ __m256d dot=_mm256_mul_pd(_mm256_loadu_pd(a+lda),_mm256_set1_pd(v[1]));
78
+ dot=_mm256_fmadd_pd(_mm256_loadu_pd(a),_mm256_set1_pd(v[0]),dot);
79
+ if(c==3) dot=_mm256_add_pd(dot,_mm256_mul_pd(_mm256_loadu_pd(a+2*lda),_mm256_set1_pd(v[2])));
80
+ acc=_mm256_add_pd(acc,dot);
81
+ }
82
+ _mm256_storeu_pd(r+j,acc);
83
+ }
84
+ for(;j<frames;j++){
85
+ double acc=0;
86
+ for(int h=0;h<len;h++){
87
+ const double *v=weights+3*h,*a=base+j+h*dilation;
88
+ double dot=fma(a[lda],v[1],a[0]*v[0]);
89
+ if(c==3)dot=fma(a[2*lda],v[2],dot);
90
+ acc+=dot;
91
+ }
92
+ r[j]=acc;
93
+ }
94
+ }
95
+ /* Specialization by channel count hoists branches out of the hot loop.
96
+ Each coefficient broadcast is shared by four eight-position tiles. */
97
+ __attribute__((target("avx512f,fma"),always_inline))
98
+ static inline void tile_project(double *r,const double *base,Py_ssize_t lda,Py_ssize_t frames,
99
+ const double *weights,int c,int64_t len,int64_t dilation){
100
+ Py_ssize_t j=0;
101
+ for(;j+32<=frames;j+=32){
102
+ __m512d x[4]={_mm512_setzero_pd(),_mm512_setzero_pd(),_mm512_setzero_pd(),_mm512_setzero_pd()};
103
+ for(int h=0;h<len;h++){
104
+ const double *v=weights+3*h,*s=base+j+h*dilation;
105
+ __m512d w0=_mm512_set1_pd(v[0]),w1=_mm512_set1_pd(v[1]),w2=_mm512_set1_pd(v[2]);
106
+ for(int q=0;q<4;q++){
107
+ const double *a=s+8*q;
108
+ __m512d av=_mm512_loadu_pd(a),dot;
109
+ if(c==1)dot=_mm512_mul_pd(av,w0);
110
+ else{
111
+ dot=_mm512_fmadd_pd(av,w0,_mm512_mul_pd(_mm512_loadu_pd(a+lda),w1));
112
+ if(c==3)dot=_mm512_add_pd(dot,_mm512_mul_pd(_mm512_loadu_pd(a+2*lda),w2));
113
+ }
114
+ x[q]=_mm512_add_pd(x[q],dot);
115
+ }
116
+ }
117
+ for(int q=0;q<4;q++)_mm512_storeu_pd(r+j+8*q,x[q]);
118
+ }
119
+ for(;j+8<=frames;j+=8){
120
+ __m512d x=_mm512_setzero_pd();
121
+ for(int h=0;h<len;h++){
122
+ const double *v=weights+3*h,*a=base+j+h*dilation;
123
+ __m512d av=_mm512_loadu_pd(a),w0=_mm512_set1_pd(v[0]),dot;
124
+ if(c==1)dot=_mm512_mul_pd(av,w0);
125
+ else{
126
+ dot=_mm512_fmadd_pd(av,w0,_mm512_mul_pd(_mm512_loadu_pd(a+lda),_mm512_set1_pd(v[1])));
127
+ if(c==3)dot=_mm512_add_pd(dot,_mm512_mul_pd(_mm512_loadu_pd(a+2*lda),_mm512_set1_pd(v[2])));
128
+ }
129
+ x=_mm512_add_pd(x,dot);
130
+ }
131
+ _mm512_storeu_pd(r+j,x);
132
+ }
133
+ if(c>1)skylakex_project(r+j,base+j,lda,frames-j,weights,c,len,dilation);
134
+ else for(;j<frames;j++){
135
+ double a=0.;
136
+ for(int h=0;h<len;h++)a+=base[j+h*dilation]*weights[3*h];
137
+ r[j]=a;
138
+ }
139
+ }
140
+ __attribute__((target("avx512f,fma")))
141
+ static void wide_project(double *r,const double *base,Py_ssize_t lda,Py_ssize_t frames,
142
+ const double *weights,int c,int64_t len,int64_t dilation){
143
+ if(c==1)tile_project(r,base,lda,frames,weights,1,len,dilation);
144
+ else if(c==2)tile_project(r,base,lda,frames,weights,2,len,dilation);
145
+ else tile_project(r,base,lda,frames,weights,3,len,dilation);
146
+ }
147
+
148
+
149
+ /* Fixed-size response tile: a whole eight-vector strip shares each tap.
150
+ Mask the short suffix and explicitly retain the special 1--3 frame BLAS
151
+ contraction at the very end of a multi-channel projection. */
152
+ __attribute__((target("avx512f,fma"),always_inline))
153
+ static inline void project_strip(double *r,const double *base,Py_ssize_t lda,int frames,
154
+ const double *weights,int c,int len,int dilation) {
155
+ int j=0;
156
+ for(;j+64<=frames;j+=64){
157
+ __m512d acc[8];for(int q=0;q<8;q++) acc[q]=_mm512_setzero_pd();
158
+ for(int h=0;h<len;h++){
159
+ const double *a=base+j+h*dilation,*v=weights+3*h;
160
+ __m512d w0=_mm512_set1_pd(v[0]),w1=_mm512_set1_pd(v[1]),w2=_mm512_set1_pd(v[2]);
161
+ for(int q=0;q<8;q++){
162
+ __m512d x=_mm512_loadu_pd(a+8*q);
163
+ __m512d dot=c==1?_mm512_mul_pd(x,w0):
164
+ _mm512_fmadd_pd(x,w0,_mm512_mul_pd(_mm512_loadu_pd(a+lda+8*q),w1));
165
+ if(c==3)dot=_mm512_add_pd(dot,_mm512_mul_pd(_mm512_loadu_pd(a+2*lda+8*q),w2));
166
+ acc[q]=_mm512_add_pd(acc[q],dot);
167
+ }
168
+ }
169
+ for(int q=0;q<8;q++)_mm512_storeu_pd(r+j+8*q,acc[q]);
170
+ }
171
+ tile_project(r+j,base+j,lda,frames-j,weights,c,len,dilation);
172
+ }
173
+
174
+ #define STRIP_FUNC(C,L) \
175
+ __attribute__((target("avx512f,fma"))) static void strip_##C##_##L( \
176
+ double *r,const double *x,Py_ssize_t ld,int n,const double *w,int d) { \
177
+ project_strip(r,x,ld,n,w,C,L,d); }
178
+ #define STRIP_L(C) STRIP_FUNC(C,7) STRIP_FUNC(C,9) STRIP_FUNC(C,11) STRIP_FUNC(C,15)
179
+ STRIP_L(1) STRIP_L(2) STRIP_L(3)
180
+ typedef void (*strip_fn)(double*,const double*,Py_ssize_t,int,const double*,int);
181
+ #define STRIP_ROW(C) {strip_##C##_7,strip_##C##_9,strip_##C##_11,strip_##C##_15}
182
+ static strip_fn strips[3][4]={STRIP_ROW(1),STRIP_ROW(2),STRIP_ROW(3)};
183
+ /* NumPy sums each <=128-value leaf in eight lanes, then combines pairs.
184
+ Fuse both traversals: bias + max + PPV + mean; squared deviations + sum.
185
+ Larger inputs recurse at the same eight-aligned split as pair_sum. */
186
+ __attribute__((target("avx512f,popcnt"),always_inline))
187
+ static inline double leaf_reduce(double *r,Py_ssize_t n,double shift,int central,
188
+ Py_ssize_t *pos,double *maximum,int *special) {
189
+ __m512d sum=_mm512_setzero_pd(),hi=_mm512_set1_pd(-INFINITY);
190
+ const __m512d s=_mm512_set1_pd(shift);
191
+ Py_ssize_t j=0;
192
+ for(;j+8<=n;j+=8) {
193
+ __m512d v=_mm512_loadu_pd(r+j);
194
+ if(central){
195
+ v=_mm512_sub_pd(v,s);v=_mm512_mul_pd(v,v);
196
+ }else{
197
+ v=_mm512_add_pd(v,s);_mm512_storeu_pd(r+j,v);
198
+ *pos+=__builtin_popcount((unsigned)_mm512_cmp_pd_mask(v,_mm512_setzero_pd(),_CMP_GT_OQ));
199
+ *special|=_mm512_cmp_pd_mask(v,v,_CMP_UNORD_Q);
200
+ hi=_mm512_max_pd(hi,v);
201
+ }
202
+ sum=j?_mm512_add_pd(sum,v):v;
203
+ }
204
+ double total=-0.;
205
+ if(j){
206
+ double a[8];_mm512_storeu_pd(a,sum);
207
+ total=((a[0]+a[1])+(a[2]+a[3]))+((a[4]+a[5])+(a[6]+a[7]));
208
+ if(!central){
209
+ double peak=_mm512_reduce_max_pd(hi);
210
+ if(peak>=*maximum)*maximum=peak;
211
+ }
212
+ }
213
+ for(;j<n;j++){
214
+ double v=r[j];
215
+ if(central){v-=shift;v*=v;}
216
+ else{
217
+ v+=shift;r[j]=v;*pos+=v>0.;*special|=isnan(v);
218
+ if(v>=*maximum)*maximum=v;
219
+ }
220
+ total+=v;
221
+ }
222
+ return total;
223
+ }
224
+ __attribute__((target("avx512f,popcnt")))
225
+ static double reduce_window(double *r,Py_ssize_t n,double shift,int central,
226
+ Py_ssize_t *pos,double *maximum,int *special) {
227
+ if(n<=128)return leaf_reduce(r,n,shift,central,pos,maximum,special);
228
+ Py_ssize_t m=(n/2)/8*8;
229
+ double lo=reduce_window(r,m,shift,central,pos,maximum,special);
230
+ return lo+reduce_window(r+m,n-m,shift,central,pos,maximum,special);
231
+ }
232
+
233
+ /* A SIMD lane owns an independent window. This allows reductions to follow
234
+ the reference's exact time order without horizontal-vector reductions. */
235
+ __attribute__((target("avx512f"),always_inline))
236
+ static inline __m512d batch_sum(const double *r,int n){
237
+ if(n<8){
238
+ __m512d acc=_mm512_set1_pd(-0.);
239
+ for(int j=0;j<n;j++)acc=_mm512_add_pd(acc,_mm512_loadu_pd(r+8*j));
240
+ return acc;
241
+ }
242
+ __m512d lane[8];
243
+ for(int q=0;q<8;q++)lane[q]=_mm512_loadu_pd(r+8*q);
244
+ int j=8;
245
+ for(;j+8<=n;j+=8)for(int q=0;q<8;q++)lane[q]=_mm512_add_pd(lane[q],_mm512_loadu_pd(r+8*(j+q)));
246
+ __m512d x=_mm512_add_pd(_mm512_add_pd(_mm512_add_pd(lane[0],lane[1]),_mm512_add_pd(lane[2],lane[3])),
247
+ _mm512_add_pd(_mm512_add_pd(lane[4],lane[5]),_mm512_add_pd(lane[6],lane[7])));
248
+ for(;j<n;j++)x=_mm512_add_pd(x,_mm512_loadu_pd(r+j*8));
249
+ return x;
250
+ }
251
+ /* At lengths 249..255 the right leaf itself exceeds 128. */
252
+ __attribute__((target("avx512f")))
253
+ static __m512d batch_reduce(const double *r,int n){
254
+ if(n<=128)return batch_sum(r,n);
255
+ int cut=n/16*8;
256
+ return _mm512_add_pd(batch_reduce(r,cut),batch_reduce(r+cut*8,n-cut));
257
+ }
258
+ __attribute__((target("avx512f"))) static void finish_projection(double *r,int nf,double *out,Py_ssize_t outjump){
259
+ __m512d m=batch_reduce(r,nf);
260
+ m=_mm512_div_pd(m,_mm512_set1_pd(nf));
261
+ __m512d mx=_mm512_set1_pd(-INFINITY),pos=_mm512_setzero_pd(),one=_mm512_set1_pd(1.);
262
+ __mmask8 nan=0;
263
+ for(int j=0;j<nf;j++){
264
+ __m512d v=_mm512_loadu_pd(r+j*8);nan|=_mm512_cmp_pd_mask(v,v,_CMP_UNORD_Q);
265
+ mx=_mm512_max_pd(mx,v);pos=_mm512_mask_add_pd(pos,_mm512_cmp_pd_mask(v,_mm512_setzero_pd(),_CMP_GT_OQ),pos,one);
266
+ v=_mm512_sub_pd(v,m);_mm512_storeu_pd(r+j*8,_mm512_mul_pd(v,v));
267
+ }
268
+ __m512d sum=batch_reduce(r,nf);
269
+ double d[4][8];
270
+ _mm512_storeu_pd(d[0],_mm512_div_pd(pos,_mm512_set1_pd(nf)));_mm512_storeu_pd(d[1],_mm512_mask_mov_pd(mx,nan,_mm512_set1_pd(NAN)));
271
+ _mm512_storeu_pd(d[2],m);_mm512_storeu_pd(d[3],_mm512_sqrt_pd(_mm512_div_pd(sum,_mm512_set1_pd(nf))));
272
+ for(int lane=0;lane<8;lane++)for(int q=0;q<4;q++)out[lane*outjump+q]=d[q][lane];
273
+ }
274
+ /* Sort a transient index of the fitted bank by shape, without reordering
275
+ the output columns. Paired kernels share input vectors only. */
276
+ typedef struct {int64_t geom[6], idx;} BankItem;
277
+ static int cmp_bank(const void *x,const void *y){
278
+ const BankItem *a=x,*b=y;return memcmp(a->geom,b->geom,sizeof a->geom);
279
+ }
280
+ __attribute__((target("avx512f,fma"),always_inline))
281
+ static inline void pair_kernel(const double *s0,const double *s1,const double *s2,const double *w,const double *w2,
282
+ double bias,double bias2,int c,int len,int dil,int nf,double *r,double *r2,double *out,double *out2,Py_ssize_t jump){
283
+ int main=nf/8*8,j=0;
284
+ for(;j<main;j+=8){
285
+ __m512d v[8],u[8];for(int q=0;q<8;++q)v[q]=u[q]=_mm512_setzero_pd();
286
+ for(int h=0;h<len;++h){
287
+ int off=(j+h*dil)*8;
288
+ __m512d wa=_mm512_set1_pd(w[3*h]),wb=_mm512_set1_pd(w[3*h+1]),wc=_mm512_set1_pd(w[3*h+2]);
289
+ __m512d xa=_mm512_set1_pd(w2[3*h]),xb=_mm512_set1_pd(w2[3*h+1]),xc=_mm512_set1_pd(w2[3*h+2]);
290
+ for(int q=0;q<8;++q,off+=8){
291
+ __m512d a=_mm512_loadu_pd(s0+off), b=_mm512_loadu_pd(s1+off),e=_mm512_loadu_pd(s2+off);
292
+ __m512d p=a*wa,z=a*xa; if(c>1){p=_mm512_fmadd_pd(a,wa,b*wb);z=_mm512_fmadd_pd(a,xa,b*xb);}
293
+ if(c==3){p+=e*wc;z+=e*xc;}v[q]+=p;u[q]+=z;
294
+ }
295
+ }
296
+ for(int q=0;q<8;++q){_mm512_storeu_pd(r+8*(j+q),v[q]+bias);_mm512_storeu_pd(r2+8*(j+q),u[q]+bias2);}
297
+ }
298
+ for(;j<nf;++j){
299
+ __m512d v=_mm512_setzero_pd(),u=v;
300
+ for(int h=0;h<len;++h){int off=(j+h*dil)*8;
301
+ __m512d a=_mm512_loadu_pd(s0+off),b=_mm512_loadu_pd(s1+off),e=_mm512_loadu_pd(s2+off);
302
+ __m512d p=a*w[3*h], z=a*w2[3*h];
303
+ if(c>1){
304
+ if(j<((nf/4)*4)){p=_mm512_fmadd_pd(a,_mm512_set1_pd(w[3*h]),b*w[3*h+1]);z=_mm512_fmadd_pd(a,_mm512_set1_pd(w2[3*h]),b*w2[3*h+1]);}
305
+ else {p=_mm512_fmadd_pd(b,_mm512_set1_pd(w[3*h+1]),p);z=_mm512_fmadd_pd(b,_mm512_set1_pd(w2[3*h+1]),z);}
306
+ }
307
+ if(c>2){
308
+ if(j<((nf/4)*4)){p+=e*w[3*h+2];z+=e*w2[3*h+2];}
309
+ else{p=_mm512_fmadd_pd(e,_mm512_set1_pd(w[3*h+2]),p);z=_mm512_fmadd_pd(e,_mm512_set1_pd(w2[3*h+2]),z);}
310
+ }
311
+ v+=p;u+=z;
312
+ }
313
+ _mm512_storeu_pd(r+j*8,v+bias);_mm512_storeu_pd(r2+j*8,u+bias2);
314
+ }
315
+ finish_projection(r,nf,out,jump);finish_projection(r2,nf,out2,jump);
316
+ }
317
+
318
+ __attribute__((target("avx512f,fma"),always_inline))
319
+ static inline void batch_kernel(const double *s0,const double *s1,const double *s2,
320
+ const double *w,double bias,int c,int len,int dil,int nf,
321
+ double *r,double *out,Py_ssize_t outjump){
322
+ int main=nf/8*8,j=0;
323
+ for(;j<main;j+=8){
324
+ __m512d v[8];for(int q=0;q<8;q++)v[q]=_mm512_setzero_pd();
325
+ for(int h=0;h<len;h++){
326
+ int off=(j+h*dil)*8;
327
+ __m512d w0=_mm512_set1_pd(w[3*h]),w1=_mm512_set1_pd(w[3*h+1]),w2=_mm512_set1_pd(w[3*h+2]);
328
+ for(int q=0;q<8;q++,off+=8){
329
+ __m512d a=_mm512_loadu_pd(s0+off);
330
+ __m512d p=c==1?_mm512_mul_pd(a,w0):_mm512_fmadd_pd(a,w0,_mm512_mul_pd(_mm512_loadu_pd(s1+off),w1));
331
+ if(c==3)p=_mm512_add_pd(p,_mm512_mul_pd(_mm512_loadu_pd(s2+off),w2));
332
+ v[q]=_mm512_add_pd(v[q],p);
333
+ }
334
+ }
335
+ for(int q=0;q<8;q++)_mm512_storeu_pd(r+8*(j+q),_mm512_add_pd(v[q],_mm512_set1_pd(bias)));
336
+ }
337
+ if(j+4<=nf){
338
+ __m512d acc[4];for(int q=0;q<4;q++)acc[q]=_mm512_setzero_pd();
339
+ for(int h=0;h<len;h++){
340
+ int off=(j+h*dil)*8;__m512d w0=_mm512_set1_pd(w[3*h]),w1=_mm512_set1_pd(w[3*h+1]),w2=_mm512_set1_pd(w[3*h+2]);
341
+ for(int q=0;q<4;q++,off+=8){
342
+ __m512d u=_mm512_loadu_pd(s0+off),z=c==1?_mm512_mul_pd(u,w0):_mm512_fmadd_pd(u,w0,_mm512_mul_pd(_mm512_loadu_pd(s1+off),w1));
343
+ if(c==3)z=_mm512_add_pd(z,_mm512_mul_pd(_mm512_loadu_pd(s2+off),w2));
344
+ acc[q]=_mm512_add_pd(acc[q],z);
345
+ }
346
+ }
347
+ for(int q=0;q<4;q++)_mm512_storeu_pd(r+8*(j+q),_mm512_add_pd(acc[q],_mm512_set1_pd(bias)));
348
+ j+=4;
349
+ }
350
+ for(;j<nf;j++){
351
+ __m512d v=_mm512_setzero_pd();
352
+ for(int h=0;h<len;h++){
353
+ int off=(j+h*dil)*8;
354
+ __m512d p=_mm512_mul_pd(_mm512_loadu_pd(s0+off),_mm512_set1_pd(w[3*h]));
355
+ if(c>1)p=_mm512_fmadd_pd(_mm512_loadu_pd(s1+off),_mm512_set1_pd(w[3*h+1]),p);
356
+ if(c>2)p=_mm512_fmadd_pd(_mm512_loadu_pd(s2+off),_mm512_set1_pd(w[3*h+2]),p);
357
+ v=_mm512_add_pd(v,p);
358
+ }
359
+ _mm512_storeu_pd(r+j*8,_mm512_add_pd(v,_mm512_set1_pd(bias)));
360
+ }
361
+ finish_projection(r,nf,out,outjump);
362
+ }
363
+ __attribute__((target("avx512f,fma")))
364
+ static void batch_bank(const double *x,Py_ssize_t ch,int t,const int64_t *rows,const double *weights,
365
+ const double *bias,Py_ssize_t k,int maxlen,double *out,double *packed,double *r,const BankItem *items){
366
+ for(Py_ssize_t c=0;c<ch;c++)for(int j=0;j<t;j++)for(int a=0;a<8;a++)
367
+ packed[(c*t+j)*8+a]=x[(a*ch+c)*t+j];
368
+ for(Py_ssize_t item=0;item<k;item++){
369
+ Py_ssize_t z=items[item].idx;
370
+ const int64_t *s=rows+6*z;int len=s[0],d=s[1],c=s[2],nf=t-(len-1)*d;
371
+ const double *a=packed+8*t*s[3],*b=c>1?packed+8*t*s[4]:a,*e=c>2?packed+8*t*s[5]:a;
372
+ if(item+1<k && !memcmp(items[item].geom,items[item+1].geom,6*sizeof(int64_t))){
373
+ Py_ssize_t zz=items[++item].idx;
374
+ #define PK(C) pair_kernel(a,b,e,weights+z*maxlen*3,weights+zz*maxlen*3,bias[z],bias[zz],C,len,d,nf,r,r+8*t,out+z*4,out+zz*4,4*k)
375
+ if(c==1)PK(1);else if(c==2)PK(2);else PK(3);
376
+ #undef PK
377
+ continue;
378
+ }
379
+ #define BK(C) batch_kernel(a,b,e,weights+z*maxlen*3,bias[z],C,len,d,nf,r,out+z*4,4*k)
380
+ if(c==1)BK(1);else if(c==2)BK(2);else BK(3);
381
+ #undef BK
382
+ }
383
+ }
384
+ /* Optional FP32 projections for already fitted FP64 banks. Sixteen retained
385
+ time positions share a vector. The two-pass centered variance avoids the
386
+ cancellation of E[X^2]-E[X]^2 near a constant. */
387
+ __attribute__((target("avx512f,fma,popcnt"),always_inline))
388
+ static inline void small_kernel(const float *x,const float *y,const float *z,
389
+ const float *w,int c,int len,int d,int nf,float bias,
390
+ float *scratch,double *out){
391
+ int count=0;__m512 mx=_mm512_set1_ps(-INFINITY);
392
+ for(int j=0;j<nf;j+=64){
393
+ int left=nf-j;
394
+ __m512 acc[4];__mmask16 masks[4];
395
+ for(int q=0;q<4;q++){
396
+ int take=left-16*q;
397
+ masks[q]=take<=0?0:take>=16?0xffff:(1u<<take)-1u;
398
+ acc[q]=_mm512_setzero_ps();
399
+ }
400
+ for(int h=0;h<len;h++){
401
+ __m512 w0=_mm512_set1_ps(w[3*h]),w1=_mm512_set1_ps(w[3*h+1]),w2=_mm512_set1_ps(w[3*h+2]);
402
+ for(int q=0;q<4;q++){
403
+ int off=j+d*h+q*16;
404
+ /* Input rows are padded by 64 floats, allowing these full
405
+ loads even on the last partial tile. */
406
+ __m512 u=_mm512_loadu_ps(x+off);
407
+ if(c==1)acc[q]=_mm512_fmadd_ps(u,w0,acc[q]);
408
+ else {
409
+ __m512 v=_mm512_fmadd_ps(u,w0,_mm512_mul_ps(_mm512_loadu_ps(y+off),w1));
410
+ if(c>2)v=_mm512_fmadd_ps(_mm512_loadu_ps(z+off),w2,v);
411
+ acc[q]=_mm512_add_ps(acc[q],v);
412
+ }
413
+ }
414
+ }
415
+ for(int q=0;q<4;q++){
416
+ __m512 v=_mm512_add_ps(acc[q],_mm512_set1_ps(bias));
417
+ _mm512_storeu_ps(scratch+j+16*q,v);
418
+ __mmask16 mask=masks[q];
419
+ mx=_mm512_mask_max_ps(mx,mask,mx,v);
420
+ count+=__builtin_popcount((unsigned)(mask&_mm512_cmp_ps_mask(v,_mm512_setzero_ps(),_CMP_GT_OQ)));
421
+ }
422
+ }
423
+ /* The sixteen-window path sums in time order. Preserve that order for
424
+ residual windows to make predictions independent of input chunking. */
425
+ float sum=0.f,ss=0.f;
426
+ for(int j=0;j<nf;j++)sum+=scratch[j];
427
+ float mu=sum/nf;
428
+ for(int j=0;j<nf;j++){float u=scratch[j]-mu;ss=fmaf(u,u,ss);}
429
+ out[0]=(double)count/nf;out[1]=_mm512_reduce_max_ps(mx);
430
+ out[2]=mu;out[3]=sqrtf(ss/nf);
431
+ }
432
+ __attribute__((target("avx512f,fma,popcnt")))
433
+ static void small_bank(const double *x,Py_ssize_t ch,int t,const int64_t *rows,const float *w,
434
+ const double *bias,Py_ssize_t k,int maxlen,float *buf,double *out){
435
+ int pitch=t+64;
436
+ float *scratch=buf+ch*pitch;
437
+ for(Py_ssize_t c=0;c<ch;c++)
438
+ for(int j=0;j<t;j++)buf[c*pitch+j]=(float)x[c*t+j];
439
+ for(Py_ssize_t i=0;i<k;i++){
440
+ const int64_t *s=rows+6*i;int len=s[0],d=s[1],c=s[2],nf=t-(len-1)*d;
441
+ const float *a=buf+s[3]*pitch,*b=c>1?buf+s[4]*pitch:a,*e=c>2?buf+s[5]*pitch:a;
442
+ #define WK(C) small_kernel(a,b,e,w+i*3*maxlen,C,len,d,nf,(float)bias[i],scratch,out+4*i)
443
+ if(c==1)WK(1);else if(c==2)WK(2);else WK(3);
444
+ #undef WK
445
+ }
446
+ }
447
+ /* Window lanes reuse each coefficient over 16 windows by 8 positions.
448
+ Pool each finished tile directly, retaining only the response scratch
449
+ required for centered variance. */
450
+ __attribute__((target("avx512f,fma"),always_inline))
451
+ static inline void small_lanes(const float *a,const float *b,const float *e,
452
+ const float *w,int c,int len,int d,int nf,float bias,float *work,double *out,int stride){
453
+ __m512 hi=_mm512_set1_ps(-INFINITY),sum=_mm512_setzero_ps(),pos=sum;
454
+ __m512 one=_mm512_set1_ps(1.),zero=_mm512_setzero_ps(),v_bias=_mm512_set1_ps(bias);
455
+ for(int j=0;j<nf;j+=8){
456
+ int take=nf-j<8?nf-j:8;__m512 v[8];
457
+ for(int q=0;q<8;q++)v[q]=zero;
458
+ for(int h=0;h<len;h++){
459
+ int start=(j+h*d)*16;
460
+ __m512 w0=_mm512_set1_ps(w[3*h]),w1=_mm512_set1_ps(w[3*h+1]),w2=_mm512_set1_ps(w[3*h+2]);
461
+ for(int q=0;q<8;q++){
462
+ int off=start+16*q;__m512 x=_mm512_loadu_ps(a+off);
463
+ if(c==1)v[q]=_mm512_fmadd_ps(x,w0,v[q]);
464
+ else{
465
+ __m512 dot=_mm512_fmadd_ps(x,w0,_mm512_mul_ps(_mm512_loadu_ps(b+off),w1));
466
+ if(c==3)dot=_mm512_fmadd_ps(_mm512_loadu_ps(e+off),w2,dot);
467
+ v[q]=_mm512_add_ps(v[q],dot);
468
+ }
469
+ }
470
+ }
471
+ for(int q=0;q<take;q++){
472
+ __m512 u=_mm512_add_ps(v[q],v_bias);
473
+ _mm512_storeu_ps(work+(j+q)*16,u);
474
+ hi=_mm512_max_ps(hi,u);sum=_mm512_add_ps(sum,u);
475
+ pos=_mm512_mask_add_ps(pos,_mm512_cmp_ps_mask(u,zero,_CMP_GT_OQ),pos,one);
476
+ }
477
+ }
478
+ __m512 avg=_mm512_div_ps(sum,_mm512_set1_ps(nf)),ss=zero;
479
+ for(int j=0;j<nf;j++){
480
+ __m512 u=_mm512_sub_ps(_mm512_loadu_ps(work+j*16),avg);
481
+ ss=_mm512_fmadd_ps(u,u,ss);
482
+ }
483
+ float pooled[4][16];
484
+ _mm512_storeu_ps(pooled[0],pos);_mm512_storeu_ps(pooled[1],hi);
485
+ _mm512_storeu_ps(pooled[2],avg);_mm512_storeu_ps(pooled[3],_mm512_sqrt_ps(_mm512_div_ps(ss,_mm512_set1_ps(nf))));
486
+ for(int j=0;j<16;j++){
487
+ double *dst=out+j*stride;dst[0]=(double)pooled[0][j]/nf;
488
+ for(int h=1;h<4;h++)dst[h]=pooled[h][j];
489
+ }
490
+ }
491
+ __attribute__((target("avx512f,fma")))
492
+ static void small_batch(const double *x,int ch,int t,const int64_t *rows,const float *w,
493
+ const double *bias,int k,int maxlen,float *buf,double *out){
494
+ int pitch=(t+8)*16;
495
+ float *scratch=buf+ch*pitch;
496
+ for(int cc=0;cc<ch;cc++)for(int j=0;j<t;j++)for(int h=0;h<16;h++)
497
+ buf[cc*pitch+j*16+h]=(float)x[(h*ch+cc)*t+j];
498
+ for(int i=0;i<k;i++){
499
+ const int64_t *s=rows+6*i;
500
+ int c=s[2],len=s[0],d=s[1],nf=t-(len-1)*d;
501
+ const float *a=buf+pitch*s[3],*b=c>1?buf+pitch*s[4]:a,*e=c>2?buf+pitch*s[5]:a;
502
+ #define SB(C) small_lanes(a,b,e,w+i*3*maxlen,C,len,d,nf,(float)bias[i],scratch,out+4*i,4*k)
503
+ if(c==1)SB(1);else if(c==2)SB(2);else SB(3);
504
+ #undef SB
505
+ }
506
+ }
507
+ static int can_fuse(void){
508
+ if(!bind_numpy() || !__builtin_cpu_supports("avx2") || !__builtin_cpu_supports("fma"))return 0;
509
+ Dl_info info;
510
+ if(!dladdr((void*)np_gemv,&info))return 0;
511
+ void *handle=dlopen(info.dli_fname,RTLD_LAZY|RTLD_LOCAL);
512
+ if(!handle)return 0;
513
+ const char *(*core)(void)=NULL;
514
+ void *fn=dlsym(handle,"scipy_openblas_get_corename64_");
515
+ if(fn)memcpy(&core,&fn,sizeof core);
516
+ int ok=core && !strcmp(core(),"SkylakeX");
517
+ dlclose(handle);
518
+ return ok;
519
+ }
520
+ #else
521
+ #define HAVE_FAST_X86 0
522
+ #endif
523
+ static PyObject *fused_library(PyObject *self,PyObject *args) {
524
+ #if HAVE_FAST_X86
525
+ Dl_info info;
526
+ if(can_fuse() && dladdr((void*)np_gemv,&info))
527
+ return PyUnicode_DecodeFSDefault(info.dli_fname);
528
+ #endif
529
+ Py_RETURN_NONE;
530
+ }
531
+ static int view(PyObject *obj, Py_buffer *v, int rank, int writable, int integer) {
532
+ int flags=PyBUF_FORMAT|PyBUF_C_CONTIGUOUS|(writable?PyBUF_WRITABLE:0);
533
+ if (PyObject_GetBuffer(obj,v,flags)<0) return -1;
534
+ int fmt=integer ? (!strcmp(v->format,"q") || !strcmp(v->format,"l"))
535
+ : !strcmp(v->format,"d");
536
+ if (!fmt || v->ndim!=rank || v->itemsize!=8 || (uintptr_t)v->buf%8) {
537
+ PyErr_SetString(PyExc_ValueError,"expected aligned contiguous int64 or float64 array of specified rank");
538
+ return -1;
539
+ }
540
+ return 0;
541
+ }
542
+ /* Eight independent partial sums and a balanced combination reproduce the
543
+ NumPy pairwise reduction tree used by contiguous float64 sums. */
544
+ static double pair_sum(const double *x, Py_ssize_t n) {
545
+ if(n<8) {
546
+ double s=-0.;
547
+ for(Py_ssize_t i=0;i<n;i++) s+=x[i];
548
+ return s;
549
+ }
550
+ if(n>128) {
551
+ Py_ssize_t m=(n/2)/8*8;
552
+ return pair_sum(x,m)+pair_sum(x+m,n-m);
553
+ }
554
+ double a[8];
555
+ memcpy(a,x,sizeof a);
556
+ Py_ssize_t i=8;
557
+ for(;i+8<=n;i+=8) for(int j=0;j<8;j++) a[j]+=x[i+j];
558
+ double s=((a[0]+a[1])+(a[2]+a[3]))+((a[4]+a[5])+(a[6]+a[7]));
559
+ for(;i<n;i++) s+=x[i];
560
+ return s;
561
+ }
562
+
563
+ #if HAVE_FAST_X86 && !defined(WISP_SCALAR_PHASE)
564
+ /* Each SIMD lane is an independent retained response. Keep tap order and the
565
+ unfused per-channel arithmetic of the scalar approximation. Mask the final
566
+ load as well as its store: a phase row need not contain eight extra values. */
567
+ __attribute__((target("avx512f")))
568
+ static inline __m512d phase_dot(const double *a,Py_ssize_t b,Py_ssize_t g,
569
+ __m512d w0,__m512d w1,__m512d w2,int c,__mmask8 mask){
570
+ __m512d v=_mm512_mul_pd(_mm512_maskz_loadu_pd(mask,a),w0);
571
+ if(c>1)v=_mm512_add_pd(v,_mm512_mul_pd(_mm512_maskz_loadu_pd(mask,a+b),w1));
572
+ if(c>2)v=_mm512_add_pd(v,_mm512_mul_pd(_mm512_maskz_loadu_pd(mask,a+g),w2));
573
+ return v;
574
+ }
575
+
576
+ __attribute__((target("avx512f")))
577
+ static void wide_phase_project(double *r,const double *phase,Py_ssize_t pitch,
578
+ Py_ssize_t stride,Py_ssize_t nf,const int64_t *s,
579
+ const double *weights){
580
+ const int len=(int)s[0],d=(int)s[1],c=(int)s[2];
581
+ const double *x=phase+s[3]*stride*pitch;
582
+ const Py_ssize_t b=c>1?(s[4]-s[3])*stride*pitch:0;
583
+ const Py_ssize_t g=c>2?(s[5]-s[3])*stride*pitch:0;
584
+ const double *tap[32];
585
+ /* Divide once for the entire filter, including non-power-of-two strides. */
586
+ const Py_ssize_t delta=d%stride, jump=delta*pitch+d/stride;
587
+ Py_ssize_t residue=0,off=0,j=0;
588
+ for(int h=0;h<len;h++){
589
+ tap[h]=x+off;
590
+ off+=jump;residue+=delta;
591
+ if(residue>=stride){off+=1-stride*pitch;residue-=stride;}
592
+ }
593
+ for(;j+32<=nf;j+=32){
594
+ __m512d a0=_mm512_setzero_pd(),a1=a0,a2=a0,a3=a0;
595
+ for(int h=0;h<len;h++){
596
+ const double *v=weights+3*h,*a=tap[h]+j;
597
+ __m512d w0=_mm512_set1_pd(v[0]),w1=_mm512_set1_pd(v[1]),w2=_mm512_set1_pd(v[2]);
598
+ a0=_mm512_add_pd(a0,phase_dot(a,b,g,w0,w1,w2,c,255));
599
+ a1=_mm512_add_pd(a1,phase_dot(a+8,b,g,w0,w1,w2,c,255));
600
+ a2=_mm512_add_pd(a2,phase_dot(a+16,b,g,w0,w1,w2,c,255));
601
+ a3=_mm512_add_pd(a3,phase_dot(a+24,b,g,w0,w1,w2,c,255));
602
+ }
603
+ _mm512_storeu_pd(r+j,a0);_mm512_storeu_pd(r+j+8,a1);
604
+ _mm512_storeu_pd(r+j+16,a2);_mm512_storeu_pd(r+j+24,a3);
605
+ }
606
+ for(;j<nf;j+=8){
607
+ const __mmask8 mask=(nf-j>=8)?255:(__mmask8)((1u<<(nf-j))-1u);
608
+ __m512d sum=_mm512_setzero_pd();
609
+ for(int h=0;h<len;h++){
610
+ const double *v=weights+3*h;
611
+ sum=_mm512_add_pd(sum,phase_dot(tap[h]+j,b,g,_mm512_set1_pd(v[0]),
612
+ _mm512_set1_pd(v[1]),_mm512_set1_pd(v[2]),c,mask));
613
+ }
614
+ _mm512_mask_storeu_pd(r+j,mask,sum);
615
+ }
616
+ }
617
+ #endif
618
+
619
+ /* Subsampled response starts are adjacent after grouping equal time-index
620
+ residues. Repack once per window and reuse this layout for every filter. */
621
+ static void phase_project(double *r,const double *phase,Py_ssize_t pitch,Py_ssize_t stride,
622
+ Py_ssize_t nf,const int64_t *s,const double *weights) {
623
+ #if HAVE_FAST_X86 && !defined(WISP_SCALAR_PHASE)
624
+ if(s[0]<=32 && __builtin_cpu_supports("avx512f")){
625
+ wide_phase_project(r,phase,pitch,stride,nf,s,weights);
626
+ return;
627
+ }
628
+ #endif
629
+ const int len=(int)s[0],d=(int)s[1],c=(int)s[2];
630
+ const double *x=phase+s[3]*stride*pitch;
631
+ const Py_ssize_t b=c>1?(s[4]-s[3])*stride*pitch:0;
632
+ const Py_ssize_t g=c>2?(s[5]-s[3])*stride*pitch:0;
633
+ memset(r,0,nf*sizeof(double));
634
+ for(int h=0;h<len;h++){
635
+ const int off=h*d;
636
+ const double *a=x+(off%stride)*pitch+off/stride, *v=weights+3*h;
637
+ if(c==1) for(Py_ssize_t j=0;j<nf;j++)r[j]+=a[j]*v[0];
638
+ else if(c==2) for(Py_ssize_t j=0;j<nf;j++)r[j]+=a[j]*v[0]+a[j+b]*v[1];
639
+ else for(Py_ssize_t j=0;j<nf;j++)r[j]+=(a[j]*v[0]+a[j+b]*v[1])+a[j+g]*v[2];
640
+ }
641
+ }
642
+
643
+ static PyObject *transform(PyObject *self,PyObject *args) {
644
+ PyObject *xo,*mo,*wo,*bo,*yo;
645
+ Py_ssize_t stride;
646
+ int approximate, fused, polyphase=0, low=0;
647
+ float *small=NULL,*smallw=NULL;
648
+ Py_buffer x={0},m={0},w={0},b={0},y={0};
649
+ double *work=NULL,*phase=NULL,*bt=NULL;
650
+ if (!PyArg_ParseTuple(args,"OOOOnppO|pp",&xo,&mo,&wo,&bo,&stride,&approximate,&fused,&yo,&polyphase,&low)) return NULL;
651
+ if (view(xo,&x,3,0,0)||view(mo,&m,2,0,1)||view(wo,&w,3,0,0)||
652
+ view(bo,&b,1,0,0)||view(yo,&y,3,1,0)) goto finish;
653
+ Py_ssize_t n=x.shape[0],ch=x.shape[1],t=x.shape[2],k=m.shape[0],maxlen=w.shape[1];
654
+ const int64_t *rows=m.buf;
655
+ if (t<1 || t>8192 || n<1 || ch<1 || k<1 || m.shape[1]!=6 ||
656
+ w.shape[0]!=k || w.shape[2]!=3 || maxlen<1 || b.shape[0]!=k ||
657
+ y.shape[0]!=n || y.shape[1]!=k || y.shape[2]!=4 || stride<1 || stride>t ||
658
+ (!approximate && stride!=1) || (low && (approximate || stride!=1))) {
659
+ PyErr_SetString(PyExc_ValueError,"invalid bank dimensions or stride"); goto finish;
660
+ }
661
+ Py_buffer *inputs[]={&x,&m,&w,&b};
662
+ for(int i=0;i<4;i++) {
663
+ uintptr_t p=(uintptr_t)inputs[i]->buf,q=(uintptr_t)y.buf;
664
+ if(p<q+y.len && q<p+inputs[i]->len) {
665
+ PyErr_SetString(PyExc_ValueError,"output overlaps input"); goto finish;
666
+ }
667
+ }
668
+ for(Py_ssize_t z=0;z<k;z++) {
669
+ const int64_t *meta=rows+6*z;
670
+ int64_t len=meta[0],d=meta[1],c=meta[2];
671
+ if(len<1 || len>maxlen || d<1 || len-1>(t-1)/d || c<1 || c>3) {
672
+ PyErr_SetString(PyExc_ValueError,"invalid kernel geometry"); goto finish;
673
+ }
674
+ if(!approximate && c>1 && (!bind_numpy() || t-(len-1)*d<=1)) {
675
+ PyErr_SetString(PyExc_ValueError,"unsupported exact BLAS geometry"); goto finish;
676
+ }
677
+ for(int j=0;j<c;j++) if(meta[3+j]<0 || meta[3+j]>=ch || (j && meta[3+j]<=meta[2+j])) {
678
+ PyErr_SetString(PyExc_ValueError,"invalid kernel channels"); goto finish;
679
+ }
680
+ }
681
+ #if HAVE_FAST_X86
682
+ int wide = __builtin_cpu_supports("avx512f") && __builtin_cpu_supports("popcnt");
683
+ fused = fused && !approximate && (__builtin_cpu_supports("avx2") && __builtin_cpu_supports("fma")) && t>=16;
684
+ #endif
685
+ work=PyMem_Malloc(5*t*sizeof(double));
686
+ if (!work) { PyErr_NoMemory(); goto finish; }
687
+ if(low){
688
+ /* Unusual custom coefficients or unnormalized signals can exceed FP32.
689
+ The bound covers convolution, reduction and centered squared sums. */
690
+ double xmax=0.,wmax=0.,bmax=0.;
691
+ const double *values[3]={x.buf,w.buf,b.buf};
692
+ Py_ssize_t sizes[3]={x.len/8,w.len/8,b.len/8};
693
+ double *maxima[3]={&xmax,&wmax,&bmax};
694
+ for(int j=0;j<3;j++)for(Py_ssize_t i=0;i<sizes[j];i++){
695
+ double v=fabs(values[j][i]);if(!isfinite(v)){low=0;break;}
696
+ if(v>*maxima[j])*maxima[j]=v;
697
+ }
698
+ if(xmax>FLT_MAX || wmax>FLT_MAX || bmax>FLT_MAX ||
699
+ 3.*maxlen*xmax*wmax+bmax>sqrt((double)FLT_MAX/(16.*t)))low=0;
700
+ }
701
+ if(low){
702
+ #if HAVE_FAST_X86
703
+ if(!wide || !__builtin_cpu_supports("fma")){
704
+ #endif
705
+ PyErr_SetString(PyExc_ValueError,"conv32 requires an AVX-512F/FMA/POPCNT CPU");goto finish;
706
+ #if HAVE_FAST_X86
707
+ }
708
+ if((size_t)(ch+1)>SIZE_MAX/((size_t)t+64)/(16*sizeof(float))){
709
+ PyErr_NoMemory();goto finish;
710
+ }
711
+ small=PyMem_Calloc(((size_t)ch+1)*(t+64)*(n>=16?16:1),sizeof(float));smallw=PyMem_Malloc(w.len/2);
712
+ if(!small||!smallw){PyErr_NoMemory();goto finish;}
713
+ for(Py_ssize_t i=0;i<w.len/8;i++)smallw[i]=(float)((double*)w.buf)[i];
714
+ #endif
715
+ }
716
+ double *r=work,*tmp=work+t,*gather=work+2*t;
717
+ const double *X=x.buf,*W=w.buf,*bias=b.buf;
718
+ double *Y=y.buf;
719
+ Py_ssize_t pitch=(t+stride-1)/stride;
720
+ if(polyphase && approximate && stride>1 && ch<=64){
721
+ phase=PyMem_Malloc(ch*stride*pitch*sizeof(double));
722
+ if(!phase){PyErr_NoMemory();goto finish;}
723
+ }
724
+ Py_ssize_t first=0,block=phase?1:12;
725
+ #if HAVE_FAST_X86
726
+ BankItem *items=NULL;
727
+ if(!low && wide && fused && n>=8 && ch<=8 && t<=256){
728
+ bt=PyMem_Malloc((ch+2)*t*8*sizeof(double));if(!bt){PyErr_NoMemory();goto finish;}
729
+ items=PyMem_Malloc(k*sizeof(*items));if(!items){PyErr_NoMemory();goto finish;}
730
+ for(Py_ssize_t z=0;z<k;++z){memcpy(items[z].geom,rows+6*z,6*sizeof(int64_t));items[z].idx=z;}
731
+ qsort(items,k,sizeof(*items),cmp_bank);
732
+ }
733
+ #endif
734
+ Py_BEGIN_ALLOW_THREADS
735
+ #if HAVE_FAST_X86
736
+ if(low)for(;first+16<=n;first+=16)small_batch(X+first*ch*t,ch,t,rows,smallw,bias,k,maxlen,small,Y+first*k*4);
737
+ if(low)for(;first<n;first++)small_bank(X+first*ch*t,ch,t,rows,smallw,bias,k,maxlen,small,Y+first*k*4);
738
+ if(bt) for(;first+8<=n;first+=8)batch_bank(X+first*ch*t,ch,t,rows,W,bias,k,maxlen,Y+first*k*4,bt,bt+ch*t*8,items);
739
+ #endif
740
+ for(Py_ssize_t tile=first;tile<n;tile+=block){
741
+ Py_ssize_t stop=tile+block<n?tile+block:n;
742
+ if(phase) for(Py_ssize_t h=0;h<ch;h++) for(Py_ssize_t q=0;q<t;q++)
743
+ phase[(h*stride+q%stride)*pitch+q/stride]=X[(tile*ch+h)*t+q];
744
+ for(Py_ssize_t z=0;z<k;z++){
745
+ for(Py_ssize_t i=tile;i<stop;i++){
746
+ const int64_t *s=rows+6*z;
747
+ int64_t len=s[0],d=s[1],c=s[2],a0=s[3],lda=c>1?t*(s[4]-a0):t;
748
+ Py_ssize_t nf=1+(t-1-(len-1)*d)/stride;
749
+ const double *base=X+(i*ch+a0)*t;
750
+ if (!phase && c>2 && s[5]-s[4]!=s[4]-a0) {
751
+ for(int j=0;j<c;j++) memcpy(gather+j*t,X+(i*ch+s[3+j])*t,t*sizeof(double));
752
+ base=gather; lda=t;
753
+ }
754
+ if(phase) phase_project(r,phase,pitch,stride,nf,s,W+z*maxlen*3); else
755
+ #if HAVE_FAST_X86
756
+ if(wide && !approximate && (fused || c==1) && nf>=16) {
757
+ int ix=len==7?0:len==9?1:len==11?2:len==15?3:-1;
758
+ if(ix>=0)strips[c-1][ix](r,base,lda,(int)nf,W+z*maxlen*3,(int)d);
759
+ else wide_project(r,base,lda,nf,W+z*maxlen*3,c,len,d);
760
+ } else if(fused && c>1) {
761
+ skylakex_project(r,base,lda,nf,W+z*maxlen*3,c,len,d);
762
+ } else
763
+ #endif
764
+ {
765
+ memset(r,0,nf*sizeof(double));
766
+ for(int64_t h=0;h<len;h++){
767
+ const double *v=W+(z*maxlen+h)*3,*src=base+h*d;
768
+ if(c==1){
769
+ if(stride==1) for(Py_ssize_t j=0;j<nf;j++) r[j]+=src[j]*v[0];
770
+ else for(Py_ssize_t j=0;j<nf;j++) r[j]+=src[j*stride]*v[0];
771
+ } else if(!approximate) {
772
+ np_gemv(102,111,nf,c,1.,src,lda,v,1,0.,tmp,1);
773
+ for(Py_ssize_t j=0;j<nf;j++) r[j]+=tmp[j];
774
+ } else if(c==2) {
775
+ for(Py_ssize_t j=0;j<nf;j++) r[j]+=src[j*stride]*v[0]+src[lda+j*stride]*v[1];
776
+ } else {
777
+ for(Py_ssize_t j=0;j<nf;j++) r[j]+=(src[j*stride]*v[0]+src[lda+j*stride]*v[1])+src[2*lda+j*stride]*v[2];
778
+ }
779
+ }
780
+ }
781
+ Py_ssize_t pos=0;
782
+ double mx=-INFINITY;
783
+ #if HAVE_FAST_X86
784
+ if(wide && nf>=16) {
785
+ int special=0;
786
+ double mu=reduce_window(r,nf,bias[z],0,&pos,&mx,&special)/nf;
787
+ if(special || mx==0.){
788
+ mx=-INFINITY;
789
+ for(Py_ssize_t j=0;j<nf;j++)if(r[j]>=mx || isnan(r[j]))mx=r[j];
790
+ }
791
+ double *dst=Y+(i*k+z)*4;
792
+ dst[0]=(double)pos/nf;dst[1]=mx;dst[2]=mu;
793
+ dst[3]=sqrt(reduce_window(r,nf,mu,1,&pos,&mx,&special)/nf);
794
+ continue;
795
+ }
796
+ #endif
797
+ for(Py_ssize_t j=0;j<nf;j++) {
798
+ r[j]+=bias[z]; pos+=r[j]>0;
799
+ if(r[j]>=mx || isnan(r[j])) mx=r[j];
800
+ }
801
+ double mu=pair_sum(r,nf)/nf;
802
+ for(Py_ssize_t j=0;j<nf;j++) { double v=r[j]-mu;tmp[j]=v*v; }
803
+ double *dst=Y+(i*k+z)*4;
804
+ dst[0]=(double)pos/nf;dst[1]=mx;dst[2]=mu;dst[3]=sqrt(pair_sum(tmp,nf)/nf);
805
+ }
806
+ }
807
+ }
808
+ Py_END_ALLOW_THREADS
809
+ #if HAVE_FAST_X86
810
+ PyMem_Free(items);
811
+ #endif
812
+ finish:
813
+ PyMem_Free(small);PyMem_Free(smallw);
814
+ PyMem_Free(bt);PyMem_Free(phase);
815
+ PyMem_Free(work);
816
+ if(x.obj)PyBuffer_Release(&x);
817
+ if(m.obj)PyBuffer_Release(&m);
818
+ if(w.obj)PyBuffer_Release(&w);
819
+ if(b.obj)PyBuffer_Release(&b);
820
+ if(y.obj)PyBuffer_Release(&y);
821
+ if(PyErr_Occurred())return NULL;
822
+ Py_RETURN_NONE;
823
+ }
824
+ static PyMethodDef methods[]={{"has_blas",has_blas,METH_NOARGS,"Whether the exact multichannel NumPy BLAS is accessible."},
825
+ {"fused_library",fused_library,METH_NOARGS,"Candidate library path for the verified SkylakeX arithmetic profile."},
826
+ {"transform",transform,METH_VARARGS,"Apply a packed bank on N,C,T inputs."},{NULL,NULL,0,NULL}};
827
+ static struct PyModuleDef mod={PyModuleDef_HEAD_INIT,"_cis_conv",NULL,-1,methods};
828
+ PyMODINIT_FUNC PyInit__cis_conv(void){return PyModule_Create(&mod);}
src/wisp_fast/cis_intervals.c ADDED
@@ -0,0 +1,402 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #define PY_SSIZE_T_CLEAN
2
+ #include <Python.h>
3
+ #include <stdint.h>
4
+ #include <string.h>
5
+ #include <math.h>
6
+
7
+ /* All arrays originate inside our Python wrapper. Enforce contiguous native
8
+ buffers, dtype, rank, extents and index ranges before native indexing. */
9
+ static int view(PyObject *obj, Py_buffer *v, int rank, char type, int write) {
10
+ if (PyObject_GetBuffer(obj, v, PyBUF_C_CONTIGUOUS|PyBUF_FORMAT|(write?PyBUF_WRITABLE:0)) < 0) return 0;
11
+ int integer = type=='q';
12
+ if (v->ndim!=rank || v->itemsize!=8 || !v->format ||
13
+ (integer ? strcmp(v->format,"q") && strcmp(v->format,"l") : strcmp(v->format,"d")) ||
14
+ (uintptr_t)v->buf % 8) {
15
+ PyErr_SetString(PyExc_ValueError,"invalid contiguous buffer type or rank");return 0;
16
+ }
17
+ return 1;
18
+ }
19
+ static int separate_output(const Py_buffer *views,int output){
20
+ uintptr_t dst=(uintptr_t)views[output].buf;
21
+ for(int i=0;i<output;i++){
22
+ uintptr_t src=(uintptr_t)views[i].buf;
23
+ if(views[i].len && views[output].len &&
24
+ (dst>=src ? dst-src<(uintptr_t)views[i].len : src-dst<(uintptr_t)views[output].len)){
25
+ PyErr_SetString(PyExc_ValueError,"output overlaps an input");
26
+ return 0;
27
+ }
28
+ }
29
+ return 1;
30
+ }
31
+ /* ord is the sorted list of time positions. Select interval membership in
32
+ rank order; one packed value per rank, no per-interval indirect gathers. */
33
+ #if defined(__x86_64__) && (defined(__GNUC__) || defined(__clang__)) && !defined(_WIN32)
34
+ #include <immintrin.h>
35
+ __attribute__((target("avx512f,popcnt")))
36
+ static Py_ssize_t filter_wide(const int64_t *ord,const double *rankval,double *out,
37
+ Py_ssize_t n,Py_ssize_t a,Py_ssize_t b) {
38
+ Py_ssize_t j=0,count=0;
39
+ __m512i lo=_mm512_set1_epi64(a),hi=_mm512_set1_epi64(b);
40
+ for(;j+8<=n;j+=8){
41
+ __m512i ix=_mm512_loadu_si512(ord+j);
42
+ __mmask8 mask=_mm512_cmpge_epi64_mask(ix,lo)&_mm512_cmplt_epi64_mask(ix,hi);
43
+ _mm512_mask_compressstoreu_pd(out+count,mask,_mm512_loadu_pd(rankval+j));
44
+ count+=__builtin_popcount((unsigned)mask);
45
+ }
46
+ for(;j<n;j++)if(ord[j]>=a && ord[j]<b)out[count++]=rankval[j];
47
+ return count;
48
+ }
49
+ /* A prefix at timestamp u stores the ranks seen before that timestamp. A
50
+ bounded range query needs at most four words for common wearable windows. */
51
+ __attribute__((target("bmi2,popcnt")))
52
+ static int masked_quantiles(const uint64_t *prefix,int words,Py_ssize_t a,Py_ssize_t b,
53
+ const double *rankval,const double *q,int nq,int turbo,
54
+ double *out,double *range) {
55
+ uint64_t bits[4];int cumulative[5]={0};
56
+ for(int w=0;w<words;w++){
57
+ bits[w]=prefix[b*words+w]^prefix[a*words+w];
58
+ cumulative[w+1]=cumulative[w]+__builtin_popcountll(bits[w]);
59
+ }
60
+ int len=(int)(b-a);
61
+ if(cumulative[words]!=len)return 0;
62
+ int first=0,last=words-1;
63
+ while(!bits[first])first++;
64
+ while(!bits[last])last--;
65
+ double low=rankval[first*64+__builtin_ctzll(bits[first])];
66
+ double high=rankval[last*64+63-__builtin_clzll(bits[last])];
67
+ *range=high==low?0.:high-low;
68
+ int w=first;
69
+ for(int k=0;k<nq;k++){
70
+ double loc=(len-1)*q[k];int lo=(int)loc;double f=loc-lo;
71
+ while(lo>=cumulative[w+1])w++;
72
+ uint64_t flag=_pdep_u64((uint64_t)1<<(lo-cumulative[w]),bits[w]);
73
+ double x=rankval[64*w+__builtin_ctzll(flag)],y=x;
74
+ if(lo+1<len){
75
+ uint64_t next=bits[w] & ~(flag | (flag-1));
76
+ int ww=w;
77
+ while(!next)next=bits[++ww];
78
+ y=rankval[64*ww+__builtin_ctzll(next)];
79
+ }
80
+ out[k]=turbo?((len&1)?x:(x+y)*.5):(f>=.5?y-(y-x)*(1.-f):x+(y-x)*f);
81
+ }
82
+ return 1;
83
+ }
84
+
85
+
86
+ /* Across-window SIMD leaves every within-interval sum strictly serial.
87
+ A small [time][8 windows] tile avoids a gather per scalar visit. */
88
+ __attribute__((target("avx512f")))
89
+ static void numeric_eight(const double *x,const int64_t *bounds,double *out,double *tile,
90
+ Py_ssize_t t,Py_ssize_t c,Py_ssize_t m,int width,int turbo){
91
+ for(Py_ssize_t ch=0;ch<c;ch++){
92
+ for(Py_ssize_t j=0;j<t;j++)for(int k=0;k<8;k++)tile[j*8+k]=x[(k*t+j)*c+ch];
93
+ for(Py_ssize_t h=0;h<m;h++){
94
+ int a=(int)bounds[2*h],b=(int)bounds[2*h+1],len=b-a;
95
+ __m512d sum=_mm512_setzero_pd(),p=sum,ss=sum,sl=sum,length=_mm512_set1_pd(len);
96
+ double mid=(len-1)*.5,den=(double)len*((double)len*len-1.)/12.;
97
+ for(int j=a;j<b;j++){
98
+ __m512d v=_mm512_loadu_pd(tile+8*j);
99
+ sum=_mm512_add_pd(sum,v);
100
+ if(!turbo)p=_mm512_add_pd(p,_mm512_mul_pd(v,v));
101
+ }
102
+ __m512d mu=_mm512_div_pd(sum,length);
103
+ for(int j=a;j<b;j++){
104
+ __m512d v=_mm512_sub_pd(_mm512_loadu_pd(tile+8*j),mu);
105
+ ss=_mm512_add_pd(ss,_mm512_mul_pd(v,v));
106
+ if(!turbo)sl=_mm512_add_pd(sl,_mm512_mul_pd(_mm512_set1_pd((j-a)-mid),v));
107
+ }
108
+ double dst[4][8];_mm512_storeu_pd(dst[0],mu);
109
+ _mm512_storeu_pd(dst[1],_mm512_sqrt_pd(_mm512_div_pd(ss,length)));
110
+ if(!turbo){
111
+ _mm512_storeu_pd(dst[2],_mm512_div_pd(p,length));
112
+ _mm512_storeu_pd(dst[3],den?_mm512_div_pd(sl,_mm512_set1_pd(den)):_mm512_setzero_pd());
113
+ }
114
+ for(int k=0;k<8;k++){
115
+ double *o=out+(k*m+h)*width*c+ch;
116
+ o[0]=dst[0][k];o[c]=dst[1][k];
117
+ if(!turbo){o[3*c]=dst[2][k];o[4*c]=dst[3][k];}
118
+ }
119
+ }
120
+ }
121
+ }
122
+
123
+ __attribute__((target("avx512f")))
124
+ static void state_fused(const double *u,const double *th,double *hist,int t,int k){
125
+ int64_t states[8];int prev=0,j=0;
126
+ for(;j+8<=t;j+=8){
127
+ __m512d x=_mm512_loadu_pd(u+j);__m512i s=_mm512_setzero_si512(),one=_mm512_set1_epi64(1);
128
+ for(int l=0;l<k-1;l++)
129
+ s=_mm512_mask_add_epi64(s,_mm512_cmp_pd_mask(x,_mm512_set1_pd(th[l]),_CMP_GT_OQ),s,one);
130
+ _mm512_storeu_si512((void*)states,s);
131
+ for(int i=0;i<8;i++){int a=(int)states[i];hist[a]++;if(j||i)hist[k+prev*k+a]++;prev=a;}
132
+ }
133
+ for(;j<t;j++){
134
+ int a=0;for(int l=0;l<k-1;l++)a+=u[j]>th[l];
135
+ hist[a]++;if(j)hist[k+prev*k+a]++;prev=a;
136
+ }
137
+ }
138
+ #define CAN_FILTER (__builtin_cpu_supports("avx512f") && __builtin_cpu_supports("popcnt"))
139
+ #endif
140
+
141
+ /* Bucket by value then insertion-sort locally. Equal finite values may be
142
+ permuted, but ordered value statistics do not depend on tied time positions.
143
+ Defer signed zero / extreme exponents to the NumPy ordering implementation. */
144
+ static PyObject *rank_short(PyObject *self,PyObject *args) {
145
+ PyObject *xo,*oo;Py_buffer v[2]={{0}};int ok=1;
146
+ if(!PyArg_ParseTuple(args,"OO",&xo,&oo))return NULL;
147
+ if(!view(xo,v,3,'d',0)||!view(oo,v+1,3,'q',1)||!separate_output(v,1))goto done;
148
+ Py_ssize_t n=v[0].shape[0],t=v[0].shape[1],c=v[0].shape[2];
149
+ if(t<1||t>256||c<1||v[1].shape[0]!=n||v[1].shape[1]!=c||v[1].shape[2]!=t){
150
+ PyErr_SetString(PyExc_ValueError,"invalid short-rank geometry");goto done;
151
+ }
152
+ const double *x=v[0].buf;int64_t *out=v[1].buf;
153
+ Py_BEGIN_ALLOW_THREADS
154
+ for(Py_ssize_t i=0;ok && i<n;++i)for(Py_ssize_t ch=0;ok && ch<c;++ch){
155
+ int64_t *idx=out+(i*c+ch)*t;double vals[256],lo=INFINITY,hi=-INFINITY;
156
+ uint8_t key[256];uint16_t edges[257]={0};
157
+ for(int j=0;j<t;++j){
158
+ double z=x[(i*t+j)*c+ch];
159
+ if(!isfinite(z) || (z==0. && signbit(z)))ok=0;
160
+ if(z<lo)lo=z;if(z>hi)hi=z;vals[j]=z;
161
+ }
162
+ if(!ok)break;
163
+ if(hi==lo){for(int j=0;j<t;++j)idx[j]=j;continue;}
164
+ double span=hi-lo,s=(t-1)/span;
165
+ if(!isfinite(s)||!isfinite(span)){ok=0;break;}
166
+ for(int j=0;j<t;++j){
167
+ int k=(int)((vals[j]-lo)*s);if(k>=t)k=(int)t-1;
168
+ key[j]=(uint8_t)k;edges[k+1]++;
169
+ }
170
+ for(int j=1;j<=t;++j)edges[j]+=edges[j-1];
171
+ for(int j=0;j<t;++j)idx[edges[key[j]]++]=j;
172
+ int begin=0;
173
+ for(int j=0;j<t;++j){
174
+ int end=edges[j];
175
+ for(int a=begin+1;a<end;++a){
176
+ int h=a;int64_t id=idx[a];
177
+ while(h>begin && vals[idx[h-1]]>vals[id]){idx[h]=idx[h-1];--h;}idx[h]=id;
178
+ }
179
+ begin=end;
180
+ }
181
+ }
182
+ Py_END_ALLOW_THREADS
183
+ done:
184
+ for(int j=0;j<2;++j)if(v[j].obj)PyBuffer_Release(v+j);
185
+ if(PyErr_Occurred())return NULL;
186
+ return PyBool_FromLong(ok);
187
+ }
188
+
189
+ #include "interval_levels.h"
190
+ static PyObject *intervals(PyObject *self,PyObject *args) {
191
+ PyObject *obj[5]; int turbo;
192
+ Py_buffer v[5]={{0}};
193
+ if(!PyArg_ParseTuple(args,"OOOOOi",&obj[0],&obj[1],&obj[2],&obj[3],&obj[4],&turbo)) return NULL;
194
+ if(!view(obj[0],v+0,3,'d',0)||!view(obj[1],v+1,2,'q',0)||
195
+ !view(obj[2],v+2,3,'q',0)||!view(obj[3],v+3,1,'d',0)||!view(obj[4],v+4,3,'d',1)) goto finish;
196
+ if(!separate_output(v,4))goto finish;
197
+ Py_ssize_t n=v[0].shape[0],t=v[0].shape[1],c=v[0].shape[2],m=v[1].shape[0],nq=v[3].shape[0];
198
+ const int width=(turbo?3:5)+(int)nq;
199
+ if((turbo && (nq!=1 || ((const double*)v[3].buf)[0]!=0.5)) || t<1 || t>1048576 || c<2 || c>256 || nq<1 || nq>16 || v[1].shape[1]!=2 ||
200
+ v[2].shape[0]!=n || v[2].shape[1]!=c || v[2].shape[2]!=t ||
201
+ v[4].shape[0]!=n || v[4].shape[1]!=m || v[4].shape[2]!=width*c) {
202
+ PyErr_SetString(PyExc_ValueError,"incompatible interval arguments");goto finish;
203
+ }
204
+ const double *x=v[0].buf,*q=v[3].buf; double *out=v[4].buf;
205
+ const int64_t *bounds=v[1].buf,*order=v[2].buf;
206
+ for(Py_ssize_t j=0;j<nq;j++) if(!isfinite(q[j]) || q[j]<0 || q[j]>1 || (j && q[j]<q[j-1])) {
207
+ PyErr_SetString(PyExc_ValueError,"invalid ascending quantiles");goto finish;
208
+ }
209
+ for(Py_ssize_t j=0;j<m;j++) if(bounds[2*j]<0 || bounds[2*j+1]>t || bounds[2*j]>=bounds[2*j+1]){
210
+ PyErr_SetString(PyExc_ValueError,"empty or invalid interval");goto finish;
211
+ }
212
+ for(Py_ssize_t j=0;j<v[2].len/8;j++) if(order[j]<0||order[j]>=t){
213
+ PyErr_SetString(PyExc_ValueError,"invalid ordering");goto finish;
214
+ }
215
+ double *sorted=PyMem_Malloc(3*(size_t)t*sizeof(double));
216
+ if(!sorted){PyErr_NoMemory();goto finish;}
217
+ uint64_t *prefix=NULL;
218
+ #ifdef CAN_FILTER
219
+ int words=(int)((t+63)/64);
220
+ if(t<=256 && m>=8 && CAN_FILTER && __builtin_cpu_supports("bmi2")){
221
+ prefix=PyMem_Malloc((t+1)*words*sizeof(uint64_t));
222
+ if(!prefix){PyMem_Free(sorted);PyErr_NoMemory();goto finish;}
223
+ }
224
+ #endif
225
+ double *tile=NULL;Py_ssize_t packed=0;
226
+ #ifdef CAN_FILTER
227
+ if(n>=8 && t<=1024 && m>=4 && CAN_FILTER){
228
+ tile=PyMem_Malloc(8*t*sizeof(double));
229
+ if(tile)packed=n/8*8;
230
+ }
231
+ #endif
232
+ Py_BEGIN_ALLOW_THREADS
233
+ #ifdef CAN_FILTER
234
+ for(Py_ssize_t i=0;i<packed;i+=8)numeric_eight(x+i*t*c,bounds,out+i*m*width*c,tile,t,c,m,width,turbo);
235
+ #endif
236
+ double *rankval=sorted+t,*signal=rankval+t;
237
+ #ifdef CAN_FILTER
238
+ int wide=CAN_FILTER;
239
+ #endif
240
+ for(Py_ssize_t i=0;i<n;i++)for(Py_ssize_t ch=0;ch<c;ch++){
241
+ const int64_t *ord=order+(i*c+ch)*t;
242
+ const double *src=x+i*t*c+ch;
243
+ for(Py_ssize_t j=0;j<t;j++)signal[j]=src[j*c];
244
+ int monotonic=1;
245
+ for(Py_ssize_t j=0;j<t;j++){
246
+ rankval[j]=signal[ord[j]];
247
+ monotonic &= isfinite(rankval[j]) && (!j || rankval[j]>=rankval[j-1]);
248
+ }
249
+ #ifdef CAN_FILTER
250
+ if(monotonic && prefix){
251
+ memset(prefix,0,(t+1)*words*sizeof(uint64_t));
252
+ for(Py_ssize_t rank=0;rank<t;rank++)
253
+ prefix[(ord[rank]+1)*words+rank/64] |= (uint64_t)1<<(rank%64);
254
+ for(Py_ssize_t j=0;j<t;j++)
255
+ for(int w=0;w<words;w++)prefix[(j+1)*words+w] |= prefix[j*words+w];
256
+ }
257
+ #endif
258
+ for(Py_ssize_t h=0;h<m;h++){
259
+ const Py_ssize_t a=bounds[2*h],b=bounds[2*h+1],len=b-a;
260
+ const double mid=(len-1)*0.5, den=(double)len*(len* (double)len-1.0)/12.0;
261
+ double *dst=out+(i*m+h)*width*c;
262
+ const double *part=signal+a;
263
+ if(i>=packed){
264
+ double sum=0,power=0;
265
+ for(Py_ssize_t j=0;j<len;j++){
266
+ double u=part[j];sum+=u;if(!turbo)power+=u*u;
267
+ }
268
+ double mu=sum/len,ss=0,sl=0;
269
+ for(Py_ssize_t j=0;j<len;j++){
270
+ double y=part[j]-mu; ss+=y*y;if(!turbo)sl+=(j-mid)*y;
271
+ }
272
+ dst[ch]=mu;dst[c+ch]=sqrt(ss/len);
273
+ if(!turbo){dst[3*c+ch]=power/len; dst[4*c+ch]=den?sl/den:0;}
274
+ }
275
+ Py_ssize_t count=0;
276
+ #ifdef CAN_FILTER
277
+ if(monotonic && prefix && masked_quantiles(prefix,words,a,b,rankval,q,(int)nq,turbo,
278
+ dst+(width-nq)*c+ch*nq,dst+2*c+ch))continue;
279
+ if(wide && t>=16)count=filter_wide(ord,rankval,sorted,t,a,b);else
280
+ #endif
281
+ for(Py_ssize_t j=0;j<t;j++){
282
+ int64_t ix=ord[j];if(ix>=a && ix<b)sorted[count++]=rankval[j];
283
+ }
284
+ /* Wrapper supplies an argsort permutation. Guard duplicate indices
285
+ even if an external extension caller supplies its own buffer. */
286
+ if(monotonic && count==len){
287
+ dst[2*c+ch]=sorted[len-1]==sorted[0]?0.:sorted[len-1]-sorted[0];
288
+ }else{
289
+ double min=part[0],max=min;
290
+ for(Py_ssize_t j=1;j<len;j++){min=fmin(min,part[j]);max=fmax(max,part[j]);}
291
+ dst[2*c+ch]=max-min;
292
+ }
293
+ if(count!=len){ for(Py_ssize_t k=0;k<nq;k++)dst[(width-nq)*c+ch*nq+k]=NAN;continue; }
294
+ for(Py_ssize_t k=0;k<nq;k++){
295
+ double loc=(len-1)*q[k];Py_ssize_t lo=(Py_ssize_t)loc,hi=lo+1<len?lo+1:lo;
296
+ double f=loc-lo,low=sorted[lo],high=sorted[hi],delta=high-low;
297
+ dst[(width-nq)*c+ch*nq+k]=turbo ? ((len&1)?low:(low+high)*0.5) :
298
+ (f>=0.5?high-delta*(1.0-f):low+delta*f);
299
+ }
300
+ }
301
+ }
302
+ Py_END_ALLOW_THREADS
303
+ PyMem_Free(tile);PyMem_Free(prefix);PyMem_Free(sorted);
304
+ finish:
305
+ for(int i=0;i<5;i++)if(v[i].obj)PyBuffer_Release(&v[i]);
306
+ if(PyErr_Occurred())return NULL;
307
+ Py_RETURN_NONE;
308
+ }
309
+ static PyObject *state_counts(PyObject *self,PyObject *args){
310
+ PyObject *xx,*qq,*oo;Py_buffer v[3]={{0}};
311
+ if(!PyArg_ParseTuple(args,"OOO",&xx,&qq,&oo))return NULL;
312
+ if(!view(xx,&v[0],3,'d',0)||!view(qq,&v[1],3,'d',0)||!view(oo,&v[2],3,'d',1))goto done;
313
+ if(!separate_output(v,2))goto done;
314
+ Py_ssize_t n=v[0].shape[0],c=v[0].shape[1],t=v[0].shape[2],k=v[1].shape[2]+1;
315
+ if(t<1||k<2||k>32||v[1].shape[0]!=n||v[1].shape[1]!=c||v[2].shape[0]!=n||
316
+ v[2].shape[1]!=c||v[2].shape[2]!=k*(1+k)){
317
+ PyErr_SetString(PyExc_ValueError,"incompatible count buffers");goto done;
318
+ }
319
+ const double *x=v[0].buf,*q=v[1].buf;double *out=v[2].buf;
320
+ Py_BEGIN_ALLOW_THREADS
321
+ memset(out,0,v[2].len);
322
+ for(Py_ssize_t z=0;z<n*c;z++){
323
+ const double *u=x+z*t,*th=q+z*(k-1);
324
+ double *hist=out+z*k*(k+1);
325
+ #ifdef CAN_FILTER
326
+ if(t>=16 && t<=8192 && CAN_FILTER){state_fused(u,th,hist,(int)t,(int)k);continue;}
327
+ #endif
328
+ int prev=0;
329
+ for(Py_ssize_t j=0;j<t;j++){
330
+ int state=0;
331
+ for(int l=0;l<k-1;l++)state+=u[j]>th[l];
332
+ hist[state]++;
333
+ if(j)hist[k+prev*k+state]++;
334
+ prev=state;
335
+ }
336
+ }
337
+ Py_END_ALLOW_THREADS
338
+ done:
339
+ for(int i=0;i<3;i++)if(v[i].obj)PyBuffer_Release(&v[i]);
340
+ if(PyErr_Occurred())return NULL;
341
+ Py_RETURN_NONE;
342
+ }
343
+ /* Pool normalized histogram, transition probabilities and Shannon terms in
344
+ one pass. A small table prepared by NumPy retains the original log and
345
+ division for all attainable (numerator, denominator) pairs. */
346
+ static double symbolic_sum(const double *a,int m){
347
+ if(m<8){ double s=-0.;for(int i=0;i<m;i++)s+=a[i];return s; }
348
+ if(m>128){int cut=m/16*8;return symbolic_sum(a,cut)+symbolic_sum(a+cut,m-cut);}
349
+ double sums[8];memcpy(sums,a,sizeof sums);
350
+ int j=8;
351
+ for(;j+8<=m;j+=8)for(int i=0;i<8;i++)sums[i]+=a[j+i];
352
+ double res=((sums[0]+sums[1])+(sums[2]+sums[3]))+((sums[4]+sums[5])+(sums[6]+sums[7]));
353
+ for(;j<m;j++)res+=a[j];return res;
354
+ }
355
+ static PyObject *state_pool(PyObject *self,PyObject *args){
356
+ PyObject *ob[4];Py_buffer v[4]={{0}};
357
+ if(!PyArg_ParseTuple(args,"OOOO",&ob[0],&ob[1],&ob[2],&ob[3]))return NULL;
358
+ for(int i=0;i<4;i++)if(!view(ob[i],v+i,3,'d',i==3))goto done;
359
+ if(!separate_output(v,3))goto done;
360
+ Py_ssize_t n=v[0].shape[0],c=v[0].shape[1],t=v[0].shape[2],k=v[1].shape[2]+1;
361
+ if(t<1 || t>512 || k<2 || k>16 || v[1].shape[0]!=n || v[1].shape[1]!=c ||
362
+ v[2].shape[0]!=t+1 || v[2].shape[1]!=t+1 || v[2].shape[2]!=2 ||
363
+ v[3].shape[0]!=n || v[3].shape[1]!=c || v[3].shape[2]!=k+k*k+1){
364
+ PyErr_SetString(PyExc_ValueError,"invalid bounded symbolic pooling dimensions");goto done;
365
+ }
366
+ const double *x=v[0].buf,*b=v[1].buf,*tab=v[2].buf;double *out=v[3].buf;
367
+ Py_BEGIN_ALLOW_THREADS
368
+ for(Py_ssize_t row=0;row<n*c;row++){
369
+ const double *src=x+row*t,*th=b+row*(k-1);
370
+ double counts[16+256]={0.},terms[256],*dest=out+row*(k+k*k+1);
371
+ #ifdef CAN_FILTER
372
+ if(t>=16 && CAN_FILTER)state_fused(src,th,counts,t,k);
373
+ else
374
+ #endif
375
+ {
376
+ int prev=0;
377
+ for(int j=0;j<t;j++){
378
+ int a=0;for(int l=0;l<k-1;l++)a+=src[j]>th[l];
379
+ counts[a]++;if(j)counts[k+prev*k+a]++;prev=a;
380
+ }
381
+ }
382
+ for(int j=0;j<k;j++){
383
+ int den=0;dest[j]=counts[j]/t;
384
+ for(int l=0;l<k;l++)den+=(int)counts[k+j*k+l];
385
+ const double *lookup=tab+2*(den?den:1)*(t+1);
386
+ for(int l=0;l<k;l++){
387
+ int ix=j*k+l,count=(int)counts[k+ix];
388
+ dest[k+ix]=lookup[2*count];terms[ix]=lookup[2*count+1];
389
+ }
390
+ }
391
+ dest[k+k*k]=-(symbolic_sum(terms,k*k)+0.);
392
+ }
393
+ Py_END_ALLOW_THREADS
394
+ done:
395
+ for(int i=0;i<4;i++)if(v[i].obj)PyBuffer_Release(v+i);
396
+ if(PyErr_Occurred())return NULL;Py_RETURN_NONE;
397
+ }
398
+ static PyMethodDef methods[]={
399
+ {"levels",interval_levels,METH_VARARGS,"Exact low-cardinality interval quantiles."},
400
+ {"rank_short",rank_short,METH_VARARGS,"Build short-window rank permutations for finite statistics."},{"state_pool",state_pool,METH_VARARGS,"Normalized state, transition and entropy features."},{"intervals",intervals,METH_VARARGS,"Pool interval statistics."},{"state_counts",state_counts,METH_VARARGS,"Count occupancy and successive states."},{NULL,NULL,0,NULL}};
401
+ static struct PyModuleDef mod={PyModuleDef_HEAD_INIT,"_cis_intervals",NULL,-1,methods};
402
+ PyMODINIT_FUNC PyInit__cis_intervals(void){return PyModule_Create(&mod);}
src/wisp_fast/decoder.py ADDED
@@ -0,0 +1,241 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Grouped transition counting and Viterbi decoding on the release class axis.
2
+
3
+ Two ways to form the sequences that the HMM smoother decodes:
4
+
5
+ ``subject``
6
+ All windows of one participant (the ``subject``/``groups`` value) form one
7
+ chain, ordered by ``time_index``. This is the original WISP behaviour.
8
+ ``recording``
9
+ Each recording forms its own chain. Within one participant's rows, in the
10
+ order they are supplied, a new recording starts whenever ``time_index``
11
+ goes backwards. Classifier scores are rescaled by one confidence factor
12
+ chosen on validation data (``fit_confidence_scale``), and smoothing is
13
+ skipped when most training recordings contain a single activity, because
14
+ then there are no within-recording transitions to learn.
15
+ """
16
+ from functools import lru_cache
17
+ import ctypes
18
+ from .native import lookup
19
+
20
+ import numpy as np
21
+
22
+ SEQUENCES = ("subject", "recording")
23
+ # Smoothing is skipped when more than this fraction of training recordings has one label.
24
+ SINGLE_LABEL_RECORDING_LIMIT = 0.5
25
+ # Candidate confidence-scaling factors, searched on validation negative log-likelihood.
26
+ CONFIDENCE_SCALE_GRID = np.exp(np.linspace(np.log(0.01), np.log(10.0), 121))
27
+
28
+
29
+ def check_sequence(sequence):
30
+ if sequence not in SEQUENCES:
31
+ raise ValueError(f"sequence must be one of {', '.join(SEQUENCES)}; got {sequence!r}")
32
+ return sequence
33
+
34
+
35
+ def recording_ids(groups, time_index):
36
+ """Split each participant into recordings at every backward step of time_index.
37
+
38
+ Rows keep the order in which they are supplied; the k-th backward step within
39
+ a participant starts recording k. Returns string IDs ``"<participant>|<k>"``.
40
+ """
41
+ g = np.asarray(groups).astype(str)
42
+ if time_index is None:
43
+ return g
44
+ t = np.asarray(time_index, dtype=float)
45
+ if t.shape != g.shape:
46
+ raise ValueError("time_index must have one entry per window")
47
+ part = np.zeros(len(t), dtype=np.int64)
48
+ for subject in np.unique(g):
49
+ rows = np.flatnonzero(g == subject)
50
+ part[rows] = np.concatenate([[0], np.cumsum(np.diff(t[rows]) < 0)])
51
+ return np.char.add(np.char.add(g, "|"), part.astype(str))
52
+
53
+
54
+ def single_label_fraction(y, recordings):
55
+ """Fraction of recordings with at least two windows whose windows all share one label."""
56
+ _, inverse, counts = np.unique(recordings, return_inverse=True, return_counts=True)
57
+ pairs = np.unique(np.stack([inverse.ravel(), np.asarray(y, dtype=np.int64)]), axis=1)
58
+ labels_per_recording = np.bincount(pairs[0], minlength=len(counts))
59
+ eligible = counts > 1
60
+ if not eligible.any():
61
+ return 1.0
62
+ return float(1 - np.mean(labels_per_recording[eligible] > 1))
63
+
64
+
65
+ def scale_confidence(proba, scale):
66
+ """Divide log-probabilities by ``scale`` and renormalise (scale 1 leaves them unchanged)."""
67
+ P = np.asarray(proba, dtype=np.float64)
68
+ if scale == 1.0:
69
+ return P
70
+ Z = np.log(np.clip(P, 1e-12, 1.0)) / scale
71
+ Z -= Z.max(axis=1, keepdims=True)
72
+ Q = np.exp(Z)
73
+ return Q / Q.sum(axis=1, keepdims=True)
74
+
75
+
76
+ def fit_confidence_scale(proba, y):
77
+ """Choose the factor on CONFIDENCE_SCALE_GRID with the lowest validation NLL."""
78
+ L = np.log(np.clip(np.asarray(proba, dtype=np.float64), 1e-12, 1.0))
79
+ y = np.asarray(y, dtype=int)
80
+ best = (np.inf, 1.0)
81
+ for scale in CONFIDENCE_SCALE_GRID:
82
+ Z = L / scale
83
+ Z -= Z.max(1, keepdims=True)
84
+ Q = np.exp(Z)
85
+ Q /= Q.sum(1, keepdims=True)
86
+ nll = -float(np.mean(np.log(np.clip(Q[np.arange(len(y)), y], 1e-12, 1.0))))
87
+ if nll < best[0]:
88
+ best = (nll, float(scale))
89
+ return best[1]
90
+
91
+
92
+ @lru_cache(maxsize=1)
93
+ def _native():
94
+ fn = lookup("wisp_viterbi")
95
+ if fn is None:
96
+ return None
97
+ arr = np.ctypeslib.ndpointer
98
+ f64 = arr(dtype=np.float64, flags="C_CONTIGUOUS")
99
+ i32 = arr(dtype=np.int32, flags="C_CONTIGUOUS")
100
+ i64 = arr(dtype=np.int64, flags="C_CONTIGUOUS")
101
+ fn.argtypes = [f64, f64, f64, ctypes.c_int64, ctypes.c_int64, i32, f64, f64, i64]
102
+ fn.restype = None
103
+ return fn
104
+
105
+
106
+ @lru_cache(maxsize=1)
107
+ def _native_groups():
108
+ fn = lookup("wisp_viterbi_groups")
109
+ if fn is not None:
110
+ arr = np.ctypeslib.ndpointer
111
+ f64 = arr(dtype=np.float64, flags="C_CONTIGUOUS")
112
+ i32 = arr(dtype=np.int32, flags="C_CONTIGUOUS")
113
+ i64 = arr(dtype=np.int64, flags="C_CONTIGUOUS")
114
+ fn.argtypes = [f64, f64, f64, i64, ctypes.c_int64, ctypes.c_int64, i32, f64, f64, i64]
115
+ fn.restype = None
116
+ return fn
117
+
118
+
119
+ def _order(n, groups, time_index):
120
+ """Preserve original order within a group when timestamps are absent or tied."""
121
+ if groups is None:
122
+ return np.arange(n), np.array([0, n], dtype=np.intp)
123
+ g = np.asarray(groups)
124
+ if g.shape != (n,):
125
+ raise ValueError("groups must have one entry per window")
126
+ if np.any(g != g):
127
+ raise ValueError("Missing group ID")
128
+ if time_index is None:
129
+ order = np.argsort(g, kind="stable")
130
+ else:
131
+ time = np.asarray(time_index)
132
+ if time.shape != (n,):
133
+ raise ValueError("time_index must have one entry per window")
134
+ order = np.lexsort((time, g))
135
+ sorted_groups = g[order]
136
+ bounds = np.r_[0, np.flatnonzero(sorted_groups[1:] != sorted_groups[:-1]) + 1, n]
137
+ return order, bounds
138
+
139
+
140
+ class FastSmoother:
141
+ """HMM smoother. ``sequence`` selects how windows are grouped into chains (see module docs)."""
142
+
143
+ def __init__(self, sequence="subject"):
144
+ self.sequence = check_sequence(sequence)
145
+
146
+ @property
147
+ def active(self):
148
+ """False when recording-level smoothing was skipped at fit time."""
149
+ return getattr(self, "active_", True)
150
+
151
+ @property
152
+ def confidence_scale(self):
153
+ return getattr(self, "confidence_scale_", 1.0)
154
+
155
+ def set_confidence_scale(self, scale):
156
+ scale = float(scale)
157
+ if not np.isfinite(scale) or scale <= 0:
158
+ raise ValueError("confidence scale must be finite and positive")
159
+ self.confidence_scale_ = scale
160
+ return self
161
+
162
+ def fit(self, y, *, groups=None, time_index=None, n_classes=None):
163
+ y = np.asarray(y, dtype=np.int64)
164
+ k = int(n_classes) if n_classes is not None else int(y.max()) + 1
165
+ if y.ndim != 1 or y.size == 0 or np.any((y < 0) | (y >= k)):
166
+ raise ValueError("Invalid y or n_classes")
167
+ if getattr(self, "sequence", "subject") == "recording":
168
+ self.confidence_scale_ = 1.0
169
+ self.active_ = True
170
+ self.single_label_fraction_ = None
171
+ if groups is not None:
172
+ rec = recording_ids(groups, time_index)
173
+ self.single_label_fraction_ = single_label_fraction(y, rec)
174
+ self.active_ = self.single_label_fraction_ <= SINGLE_LABEL_RECORDING_LIMIT
175
+ groups = rec
176
+ A = np.ones((k, k), dtype=np.float64)
177
+ if groups is not None:
178
+ order, bounds = _order(len(y), groups, time_index)
179
+ labels = y[order]
180
+ edges = labels[:-1] * k + labels[1:]
181
+ valid = np.ones(edges.size, dtype=bool)
182
+ valid[bounds[1:-1] - 1] = False
183
+ A += np.bincount(edges[valid], minlength=k*k).reshape(k, k)
184
+ # Upstream adds persistence twice for ungrouped training and once otherwise.
185
+ A.flat[::k + 1] += 4.0 if groups is None else 2.0
186
+ p = np.bincount(y, minlength=k).astype(np.float64) + 1.0
187
+ self.transition_ = A / A.sum(axis=1, keepdims=True)
188
+ self.prior_ = p / p.sum()
189
+ self.log_A_ = np.log(np.clip(self.transition_, 1e-12, 1.0))
190
+ self.log_pi_ = np.log(np.clip(self.prior_, 1e-12, 1.0))
191
+ return self
192
+
193
+ def predict_from_proba(self, proba, *, groups=None, time_index=None):
194
+ P = np.asarray(proba, dtype=np.float64)
195
+ if P.ndim != 2 or P.shape[1] != len(self.prior_) or not np.isfinite(P).all():
196
+ raise ValueError("Invalid emissions")
197
+ n, k = P.shape
198
+ if n == 0:
199
+ return np.empty(0, dtype=np.int64)
200
+ if getattr(self, "sequence", "subject") == "recording":
201
+ if not self.active:
202
+ return P.argmax(1).astype(np.int64)
203
+ P = scale_confidence(P, self.confidence_scale)
204
+ if groups is not None:
205
+ groups = recording_ids(groups, time_index)
206
+ order, bounds = _order(n, groups, time_index)
207
+ return self._decode_ordered(P,order,bounds)
208
+
209
+ def _decode_ordered(self,P,order,bounds):
210
+ n,k=P.shape
211
+ P = np.clip(P[order], 1e-12, 1.0)
212
+ P /= P.sum(axis=1, keepdims=True)
213
+ E = np.ascontiguousarray(np.log(np.clip(P, 1e-12, 1.0)))
214
+ back = np.empty((np.diff(bounds).max(), k), dtype=np.int32)
215
+ prev, nxt = np.empty(k), np.empty(k)
216
+ labels = np.empty(n, dtype=np.int64)
217
+ grouped = _native_groups() if _native() is not None else None
218
+ if grouped is not None:
219
+ grouped(E, self.log_A_, self.log_pi_, np.ascontiguousarray(bounds, dtype=np.int64),
220
+ len(bounds)-1, k, back, prev, nxt, labels)
221
+ out = np.empty_like(labels)
222
+ out[order] = labels
223
+ return out
224
+ fn = _native()
225
+ for lo, hi in zip(bounds[:-1], bounds[1:]):
226
+ if fn is not None:
227
+ fn(E[lo:hi], self.log_A_, self.log_pi_, hi-lo, k, back, prev, nxt, labels[lo:hi])
228
+ else:
229
+ prev[:] = self.log_pi_ + E[lo]
230
+ cols = np.arange(k)
231
+ for i in range(lo + 1, hi):
232
+ scores = prev[:, None] + self.log_A_
233
+ pred = np.argmax(scores, axis=0)
234
+ back[i-lo] = pred
235
+ prev[:] = scores[pred, cols] + E[i]
236
+ labels[hi-1] = np.argmax(prev)
237
+ for i in range(hi-2, lo-1, -1):
238
+ labels[i] = back[i-lo+1, labels[i+1]]
239
+ out = np.empty_like(labels)
240
+ out[order] = labels
241
+ return out
src/wisp_fast/engine.py ADDED
@@ -0,0 +1,459 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Accelerated CPU execution of the seven fixed WISP families and search recipes."""
2
+ from copy import deepcopy
3
+ from dataclasses import dataclass
4
+ import time
5
+
6
+ import numpy as np
7
+ from sklearn.ensemble import ExtraTreesClassifier, RandomForestClassifier
8
+ from sklearn.linear_model import RidgeClassifier
9
+ from sklearn.pipeline import Pipeline
10
+ from sklearn.preprocessing import StandardScaler
11
+ from sklearn.utils.class_weight import compute_sample_weight
12
+
13
+ from wisp.cpu.algorithms import _classifier, build_cpu_estimator
14
+ from wisp.cpu.probability import align_proba, estimator_proba, global_classes, labels_from_proba
15
+ from wisp.registry import build_method, list_specs
16
+ from wisp_release.methods import build_reference_family as reference_family
17
+ from . import features as fx
18
+ from .cache import _ACTIVE, _SEARCH, transform_blocks
19
+ from .decoder import FastSmoother, check_sequence, fit_confidence_scale
20
+ from .forest import ForestPlan, local_proba
21
+
22
+ CLASS_TO_ID = {spec.code_class: spec.method_id for spec in list_specs()}
23
+ # "original": the paper's RidgeClassifierCV. "screened": Ridge strength chosen on a
24
+ # 512-row training subsample, then one Ridge fit on all rows (approximate, faster).
25
+ RIDGE_SOLVERS = ("original", "screened")
26
+
27
+
28
+ @dataclass(frozen=True)
29
+ class TurboBudget:
30
+ """Upper limits. The canonical HMM sees all training labels and timestamps."""
31
+ kernels: int = 32
32
+ intervals: int = 4
33
+ shapelets: int = 6
34
+ trees: int = 24
35
+ max_steps: int = 96
36
+ max_train: int = 4096
37
+ ridge_alpha: float = 1.0
38
+
39
+ def __post_init__(self):
40
+ for field in ("kernels", "intervals", "shapelets", "trees", "max_steps", "max_train"):
41
+ val = getattr(self, field)
42
+ if not isinstance(val, (int, np.integer)) or val < 1:
43
+ raise ValueError(f"{field} must be a positive integer")
44
+ if self.max_steps < 15:
45
+ raise ValueError("max_steps must allow the longest standard convolution (15)")
46
+ if self.ridge_alpha <= 0 or not np.isfinite(self.ridge_alpha):
47
+ raise ValueError("ridge_alpha must be finite and positive")
48
+
49
+
50
+ def uses_ridge_head(template):
51
+ """True for RC/CIS templates whose classifier is the Ridge head the solver option changes."""
52
+ return (CLASS_TO_ID.get(type(template).__name__) in ("wisp_rc", "wisp_cis")
53
+ and getattr(template, "classifier", None) == "ridge")
54
+
55
+
56
+ def with_ridge_solver(template, mode="exact", budget=None, ridge_solver="original", sequence="subject",
57
+ feature_workers=1):
58
+ """Wrap a reference template; apply ``ridge_solver`` wherever a Ridge head exists.
59
+
60
+ RC/CIS Ridge models use the solver directly, the CSE/ESE ensembles pass it to their
61
+ RC/CIS members, and families without a Ridge head are unaffected by the choice.
62
+ """
63
+ if ridge_solver not in RIDGE_SOLVERS:
64
+ raise ValueError(f"ridge_solver must be one of {', '.join(RIDGE_SOLVERS)}")
65
+ direct = ridge_solver if mode == "exact" and uses_ridge_head(template) else "original"
66
+ model = FastFamily(template, mode, budget, ridge_solver=direct, feature_workers=feature_workers,
67
+ sequence=sequence)
68
+ if mode == "exact" and model.method_id in ("wisp_cse", "wisp_ese"):
69
+ model.member_ridge_solver = ridge_solver
70
+ return model
71
+
72
+
73
+ def build_family(method_id, *, seed=42, n_jobs=1, direct=False, overrides=None,
74
+ mode="exact", budget=None, ridge_solver="original", feature_workers=1,
75
+ sequence="subject"):
76
+ model = reference_family(method_id, seed=seed, n_jobs=n_jobs, direct=direct,
77
+ overrides=overrides)
78
+ return with_ridge_solver(model, mode, budget, ridge_solver, sequence, feature_workers)
79
+
80
+
81
+ def build_recipe(recipe_id, *, seed=42, n_jobs=1, mode="exact", budget=None, ridge_solver="original",
82
+ feature_workers=1, sequence="subject"):
83
+ """Seven non-empty controlled R/I/S combinations crossed with direct/Viterbi."""
84
+ from wisp.ablations import build_recipe as original_recipe
85
+ if n_jobs < 1:
86
+ raise ValueError("n_jobs must be positive")
87
+ model = FastFamily(original_recipe(recipe_id, seed=seed, n_jobs=n_jobs), mode, budget,
88
+ ridge_solver=ridge_solver, feature_workers=feature_workers, sequence=sequence)
89
+ model.recipe_id = recipe_id
90
+ return model
91
+
92
+
93
+ def build_candidate(spec_dict, *, mode="exact", budget=None, ridge_solver="original", sequence="subject"):
94
+ if isinstance(spec_dict, dict) and spec_dict.get("route", "cpu") != "cpu":
95
+ raise ValueError("Only CPU WISP candidates are released")
96
+ return with_ridge_solver(build_cpu_estimator(spec_dict), mode, budget, ridge_solver, sequence)
97
+
98
+
99
+ def _training_rows(y, cap, seed):
100
+ """Roughly proportional stratified random sample, retaining every observed class."""
101
+ labels, inverse, counts = np.unique(y, return_counts=True, return_inverse=True)
102
+ if cap < len(labels):
103
+ raise ValueError("max_train is smaller than the number of observed classes")
104
+ # Reserve one per class, then allocate remaining slots proportionally to capacity.
105
+ quota = (counts - 1) * ((cap - len(labels)) / (len(y) - len(labels)))
106
+ floor = np.floor(quota).astype(int)
107
+ sizes = floor + 1
108
+ order = np.argsort(-(quota - floor), kind="stable")
109
+ sizes[order[:cap - sizes.sum()]] += 1
110
+ rng = np.random.default_rng(seed)
111
+ return np.sort(np.concatenate([
112
+ rng.choice(np.flatnonzero(inverse == j), size=size, replace=False)
113
+ for j, size in enumerate(sizes)]))
114
+
115
+
116
+ class FastFamily:
117
+ """fit/predict API of the release. Inference batching never splits a sequence decoder."""
118
+ def __init__(self, template, mode="exact", budget=None, ridge_solver="original", feature_workers=1,
119
+ sequence="subject"):
120
+ if not isinstance(feature_workers,(int,np.integer)) or feature_workers<1:
121
+ raise ValueError("feature_workers must be a positive integer")
122
+ self.feature_workers = feature_workers
123
+ self.sequence = check_sequence(sequence)
124
+ if ridge_solver not in RIDGE_SOLVERS:
125
+ raise ValueError("Unknown Ridge solver")
126
+ if ridge_solver!="original" and (mode!="exact" or CLASS_TO_ID[type(template).__name__]
127
+ not in ("wisp_rc","wisp_cis") or template.classifier!="ridge"):
128
+ raise ValueError("Custom solvers are supported for exact RC/CIS Ridge models and CIS recipes")
129
+ self.ridge_solver = ridge_solver
130
+ if mode not in {"exact", "turbo"}:
131
+ raise ValueError("mode must be exact or turbo")
132
+ if budget is not None and mode != "turbo":
133
+ raise ValueError("budget is only meaningful in turbo mode")
134
+ if budget is not None and not isinstance(budget, TurboBudget):
135
+ raise TypeError("budget must be a TurboBudget")
136
+ self.template = template
137
+ self.method_id = CLASS_TO_ID[type(template).__name__]
138
+ self.mode = mode
139
+ self.budget = (budget or TurboBudget()) if mode == "turbo" else None
140
+ self.n_jobs = template.n_jobs
141
+ self.smoother_ = None
142
+ self.members_ = []
143
+ self.blocks_ = []
144
+ self.model_ = None
145
+ self.classes_ = None
146
+ self.input_shape_ = None
147
+ self.checkpoint_only_ = False
148
+
149
+ def __getstate__(self):
150
+ # Process-local pointers cannot be serialized or included in a size tiebreak.
151
+ state = self.__dict__.copy()
152
+ state.pop("_forest_plan", None)
153
+ return state
154
+
155
+ def design_profile(self):
156
+ return self.template.design_profile()
157
+
158
+ def _blocks(self):
159
+ m, family, b = self.template, self.method_id, self.budget
160
+ fast = {"fast_profile": self.mode == "turbo"}
161
+ if family == "wisp_ss":
162
+ return [fx.WearableFeatureExtractor(segment_sizes=m.segment_sizes,
163
+ symbolic_bins=m.symbolic_bins, **fast)]
164
+ if family == "wisp_so":
165
+ return [fx.WearableFeatureExtractor(
166
+ include_raw_stats=True, include_magnitude=True, include_jerk=False,
167
+ include_multiscale=True, segment_sizes=(5, 10, 20), include_spectral=False,
168
+ include_autocorr=False, include_cross_channel=True, include_symbolic=True,
169
+ symbolic_bins=8, **fast)]
170
+ if family == "wisp_gis":
171
+ return [fx.InvariantPhysicsFeatureExtractor(
172
+ segment_sizes=m.segment_sizes, symbolic_bins=m.symbolic_bins,
173
+ random_intervals=min(24, b.intervals) if b else 24,
174
+ random_state=m.seed, include_axis_features=True, **fast),
175
+ fx.RandomShapeletSketch(
176
+ n_shapelets=min(m.n_shapelets, b.shapelets) if b else m.n_shapelets,
177
+ random_state=m.seed+17, **fast)]
178
+ if family in {"wisp_cis", "wisp_rc"}:
179
+ kernels = min(m.n_kernels, b.kernels) if b else m.n_kernels
180
+ blocks = []
181
+ if getattr(m, "use_rocket", True):
182
+ blocks.append(fx.RandomConvSketch(n_kernels=kernels, random_state=m.seed, **fast))
183
+ if family == "wisp_cis" and getattr(m, "use_intervals", True):
184
+ blocks.append(fx.IntervalDistributionSketch(
185
+ n_random_intervals=min(m.n_intervals, b.intervals) if b else m.n_intervals,
186
+ random_state=m.seed+29, **fast))
187
+ if family == "wisp_cis" and getattr(m, "use_state", True):
188
+ blocks.append(fx.WearableFeatureExtractor(
189
+ include_raw_stats=False, include_magnitude=True, include_jerk=True,
190
+ include_multiscale=False, include_spectral=True, include_autocorr=True,
191
+ include_cross_channel=True, include_symbolic=True, symbolic_bins=6, **fast))
192
+ return blocks
193
+ raise ValueError(f"{family} has no base feature blocks")
194
+
195
+ def _inputs(self, X):
196
+ X = np.asarray(X)
197
+ if X.ndim == 2:
198
+ X = X[..., None]
199
+ if X.ndim != 3 or min(X.shape) < 1:
200
+ raise ValueError("X must have nonempty shape (N,T,C) or (N,T)")
201
+ return X
202
+
203
+ def _times(self, X):
204
+ if self.budget and X.shape[1] > self.budget.max_steps:
205
+ def sample():
206
+ take = np.linspace(0, X.shape[1]-1, self.budget.max_steps, dtype=np.intp)
207
+ return np.ascontiguousarray(X[:, take, :])
208
+ memory = _SEARCH.get()
209
+ return sample() if memory is None else memory.sampled(X, self.budget.max_steps, sample)
210
+ return X
211
+
212
+ def _fit_base(self, X, y):
213
+ self.blocks_ = self._blocks()
214
+ for block in self.blocks_:
215
+ block.fit(X, y)
216
+ self._fit_head(self._features(X), y)
217
+
218
+ def _fit_head(self, F, y):
219
+ m, b = self.template, self.budget
220
+ kind = getattr(m, "classifier", "extratrees")
221
+ if self.method_id == "wisp_so":
222
+ head = ExtraTreesClassifier(n_estimators=160, max_features="sqrt",
223
+ class_weight="balanced", random_state=m.seed, n_jobs=self.n_jobs)
224
+ elif b and kind == "ridge":
225
+ head = RidgeClassifier(alpha=b.ridge_alpha, solver="cholesky")
226
+ elif getattr(self,"ridge_solver","original") == "screened":
227
+ from .ridge_fast import ScreenedRidgeClassifierCV
228
+ head = ScreenedRidgeClassifierCV(alphas=np.logspace(-3,3,9))
229
+ elif hasattr(self, "recipe_id"):
230
+ from wisp.cis_algorithms import _classifier as recipe_classifier
231
+ head = recipe_classifier(kind, seed=m.seed, n_jobs=self.n_jobs,
232
+ class_balance=m.class_balance)
233
+ else:
234
+ head = _classifier(kind, seed=m.seed, n_jobs=self.n_jobs)
235
+ if b:
236
+ if kind in {"rf", "extratrees"}:
237
+ head.set_params(n_estimators=min(head.n_estimators, b.trees))
238
+ elif kind == "logreg":
239
+ head.set_params(max_iter=250, tol=1e-3)
240
+ weighted = kind == "ridge" and getattr(m, "class_balance", "balanced") == "balanced"
241
+ kwargs = {"clf__sample_weight": compute_sample_weight("balanced", y)} if weighted else {}
242
+ if self.method_id in {"wisp_cis", "wisp_rc"} or kind == "logreg":
243
+ head = Pipeline([("scale", StandardScaler()), ("clf", head)])
244
+ # Upstream only uses ridge via the RC/CIS scaled pipeline in its search grammar.
245
+ if kind == "ridge" and not isinstance(head, Pipeline):
246
+ kwargs = {}
247
+ forest = head.steps[-1][1] if isinstance(head, Pipeline) else head
248
+ if _SEARCH.get() is not None and type(forest) in (ExtraTreesClassifier,RandomForestClassifier):
249
+ from .search_forest import forest_fit_plan
250
+ with forest_fit_plan(forest):
251
+ self.model_ = head.fit(F, y, **kwargs)
252
+ else:
253
+ self.model_ = head.fit(F, y, **kwargs)
254
+ self.__dict__.pop("_forest_plan", None)
255
+
256
+ def _features(self, X):
257
+ if getattr(self,"feature_workers",1)>1:
258
+ from .parallel import parallel_rows
259
+ return parallel_rows(self._feature_batch,X,self.feature_workers)
260
+ return self._feature_batch(X)
261
+
262
+ def _feature_batch(self, X):
263
+ blocks = transform_blocks(self.blocks_, X)
264
+ return blocks[0] if len(blocks) == 1 else np.concatenate(blocks, axis=1)
265
+
266
+ def _member_models(self):
267
+ seed = self.template.seed
268
+ if self.method_id == "wisp_cse":
269
+ configs = [("wisp_ss", {}), ("wisp_rc", {"n_kernels": 256}), ("wisp_so", {})]
270
+ else:
271
+ configs = [("wisp_cis", {"n_kernels": 256, "n_intervals": 16}),
272
+ ("wisp_gis", {"n_shapelets": 24}), ("wisp_so", {})]
273
+ members = []
274
+ for j, (name, params) in enumerate(configs):
275
+ template = build_method(name, seed=seed+j, n_jobs=self.n_jobs,
276
+ overrides={"use_hmm": False, **params})
277
+ member = FastFamily(template, self.mode, self.budget)
278
+ solver = getattr(self, "member_ridge_solver", "original")
279
+ if solver != "original" and uses_ridge_head(template):
280
+ member.ridge_solver = solver
281
+ members.append(member)
282
+ return members
283
+
284
+ def fit(self, X, y, *, groups=None, time_index=None, n_classes=None):
285
+ if self.checkpoint_only_:
286
+ raise ValueError("Construct a new family for refitting an imported checkpoint")
287
+ start = time.perf_counter()
288
+ X = self._inputs(X)
289
+ y = np.asarray(y, dtype=np.int64)
290
+ if y.shape != (len(X),):
291
+ raise ValueError("y must have one label per window")
292
+ self.classes_ = global_classes(y, n_classes)
293
+ self.trained_classes_ = np.unique(y)
294
+ self.input_shape_ = X.shape[1:]
295
+ k = len(self.classes_)
296
+ self.smoother_ = (FastSmoother(getattr(self, "sequence", "subject")).fit(
297
+ y, groups=groups, time_index=time_index, n_classes=k)
298
+ if self.template.use_hmm else None)
299
+ if self.budget and len(y) > self.budget.max_train:
300
+ rows = _training_rows(y, self.budget.max_train, self.template.seed)
301
+ X, y = X[rows], y[rows]
302
+ self.fit_windows_ = len(y)
303
+ if self.method_id in {"wisp_cse", "wisp_ese"}:
304
+ self.members_ = self._member_models()
305
+ for member in self.members_:
306
+ member.fit(X, y, n_classes=k)
307
+ else:
308
+ self._fit_base(self._times(X), y)
309
+ self.fit_seconds_ = time.perf_counter() - start
310
+ calibration = self.__dict__.pop("calibration_data", None)
311
+ if calibration is not None and groups is not None:
312
+ self.fit_confidence_scale(*calibration)
313
+ return self
314
+
315
+ # ---- recording-level smoothing: confidence scaling chosen on validation data ----
316
+ @property
317
+ def use_hmm(self):
318
+ return bool(self.template.use_hmm)
319
+
320
+ def fit_confidence_scale(self, X_valid, y_valid):
321
+ """Choose the confidence-scaling factor on validation windows (``sequence="recording"``).
322
+
323
+ Has no effect for ``sequence="subject"``, for models without HMM smoothing, or
324
+ when recording-level smoothing was skipped at fit time.
325
+ """
326
+ sm = self.smoother_
327
+ if getattr(self, "sequence", "subject") != "recording" or sm is None or not sm.active:
328
+ return self
329
+ sm.set_confidence_scale(fit_confidence_scale(self.predict_proba(X_valid), y_valid))
330
+ return self
331
+
332
+ @property
333
+ def confidence_scale(self):
334
+ sm = self.smoother_
335
+ if getattr(self, "sequence", "subject") != "recording" or sm is None or not sm.active:
336
+ return None
337
+ return sm.confidence_scale
338
+
339
+ def set_confidence_scale(self, scale):
340
+ """Reuse a factor chosen for a train-only fit (e.g. after refitting on train+validation)."""
341
+ sm = self.smoother_
342
+ if scale is not None and sm is not None and sm.active and getattr(self, "sequence", "subject") == "recording":
343
+ sm.set_confidence_scale(scale)
344
+ return self
345
+
346
+ def _head_proba(self,features,*,private=False):
347
+ head=self.model_
348
+ # Standalone fitted model only. Shared panels, disk cache mappings,
349
+ # and externally supplied feature arrays take the read-only path.
350
+ if (private and type(head) is Pipeline and len(head.steps)==2
351
+ and type(head.steps[0][1]) is StandardScaler
352
+ and features.dtype==np.float64 and features.flags.writeable and features.flags.owndata):
353
+ scaled=head.steps[0][1].transform(features,copy=False)
354
+ return estimator_proba(head.steps[1][1],scaled,self.classes_)
355
+
356
+ if isinstance(head,(ExtraTreesClassifier,RandomForestClassifier)) and head.n_jobs==1:
357
+ plan=getattr(self,"_forest_plan",None)
358
+ if plan is None:plan=self._forest_plan=ForestPlan(head)
359
+ return align_proba(local_proba(head,features,plan),head.classes_,self.classes_)
360
+ return estimator_proba(head,features,self.classes_)
361
+
362
+ def _proba(self, X):
363
+ if not self.members_:
364
+ private=(self.method_id in ("wisp_cis","wisp_rc") and len(self.blocks_)>=2
365
+ and _SEARCH.get() is None and _ACTIVE.get() is None)
366
+ return self._head_proba(self._features(self._times(X)),private=private)
367
+ return self._combine(lambda member:member._proba(X),len(X))
368
+
369
+ def _combine(self,score,n):
370
+ # Accumulate in canonical order without retaining all member scores.
371
+ if self.method_id == "wisp_cse":
372
+ out = score(self.members_[0]).copy()
373
+ for member in self.members_[1:]:
374
+ out += score(member)
375
+ out /= len(self.members_)
376
+ else:
377
+ w = np.asarray(self.template.weights, dtype=np.float64)
378
+ if w.size != len(self.members_):
379
+ w = np.ones(len(self.members_))
380
+ w = w / np.maximum(w.sum(), 1e-12)
381
+ out = np.zeros((n, len(self.classes_)))
382
+ for wi, member in zip(w, self.members_):
383
+ if wi != 0:
384
+ out += wi * score(member)
385
+ return out / np.maximum(out.sum(axis=1, keepdims=True), 1e-12)
386
+
387
+ def predict_proba(self, X, *, batch_size=None):
388
+ if self.classes_ is None:
389
+ raise RuntimeError("Unfitted estimator")
390
+ X = self._inputs(X)
391
+ if self.input_shape_ is not None and X.shape[1:] != self.input_shape_:
392
+ raise ValueError(f"Fitted T,C={self.input_shape_}; got {X.shape[1:]}")
393
+ if batch_size is None:
394
+ return self._proba(X)
395
+ if not isinstance(batch_size, (int, np.integer)) or batch_size < 1:
396
+ raise ValueError("batch_size must be a positive integer")
397
+ out = np.empty((len(X), len(self.classes_)), dtype=np.float64)
398
+ for start in range(0, len(X), batch_size):
399
+ stop = min(len(X), start + batch_size)
400
+ out[start:stop] = self._proba(X[start:stop])
401
+ return out
402
+
403
+ def decode_proba(self, proba, *, groups=None, time_index=None):
404
+ if self.smoother_ is not None and groups is not None:
405
+ return self.smoother_.predict_from_proba(proba, groups=groups, time_index=time_index)
406
+ return labels_from_proba(proba, self.classes_)
407
+
408
+ def predict(self, X, *, groups=None, time_index=None, batch_size=None):
409
+ return self.decode_proba(self.predict_proba(X, batch_size=batch_size),
410
+ groups=groups, time_index=time_index)
411
+
412
+
413
+ def _convert_block(block):
414
+ cls = getattr(fx, type(block).__name__)
415
+ obj = cls(**block.get_params(deep=False))
416
+ for name, value in vars(block).items():
417
+ if name.endswith("_"):
418
+ setattr(obj, name, _convert_block(value) if name == "interval_" and value is not None
419
+ else deepcopy(value))
420
+ return obj
421
+
422
+
423
+ def accelerate_checkpoint(model):
424
+ """Convert a trusted, already-loaded release estimator. Does not refit or unpickle.
425
+
426
+ Returns an independent inference-only copy, including fitted head and transition
427
+ probabilities. For guarded file loading use wisp_release.checkpoints.load_checkpoint.
428
+ """
429
+ if isinstance(model, FastFamily):
430
+ return deepcopy(model)
431
+ if type(model).__name__ not in CLASS_TO_ID or getattr(model, "classes_", None) is None:
432
+ raise ValueError("Expected a fitted WISP CPU family from the release")
433
+ out = FastFamily(type(model)(**model.get_params(deep=False)))
434
+ out.classes_ = model.classes_.copy()
435
+ out.trained_classes_ = deepcopy(getattr(model, "trained_classes_", None))
436
+ out.fit_seconds_ = getattr(model, "fit_seconds_", None)
437
+ out.checkpoint_only_ = True
438
+ if getattr(model, "members_", None):
439
+ out.members_ = [accelerate_checkpoint(member) for member in model.members_]
440
+ else:
441
+ if getattr(model, "model_", None) is None or getattr(model, "use_anchor_stats", False):
442
+ raise ValueError("Unsupported or incomplete WISP checkpoint")
443
+ out.model_ = deepcopy(model.model_)
444
+ names = {"wisp_ss": ("feature_extractor_",), "wisp_so": ("feature_extractor_",),
445
+ "wisp_rc": ("sketch_",), "wisp_gis": ("physics_", "shapelets_"),
446
+ "wisp_cis": ("rocket_", "interval_", "state_")}
447
+ out.blocks_ = [_convert_block(getattr(model, name)) for name in names[out.method_id]
448
+ if getattr(model, name, None) is not None]
449
+ if not out.blocks_:
450
+ raise ValueError("No fitted feature blocks found")
451
+ smoother = getattr(model, "smoother_", None)
452
+ if smoother is not None:
453
+ s = FastSmoother()
454
+ s.transition_ = smoother.transition_.copy()
455
+ s.prior_ = smoother.prior_.copy()
456
+ s.log_A_ = np.ascontiguousarray(np.log(np.clip(s.transition_, 1e-12, 1.0)))
457
+ s.log_pi_ = np.ascontiguousarray(np.log(np.clip(s.prior_, 1e-12, 1.0)))
458
+ out.smoother_ = s
459
+ return out
src/wisp_fast/family_kernels.c ADDED
@@ -0,0 +1,445 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #define PY_SSIZE_T_CLEAN
2
+ #include <Python.h>
3
+ #include <math.h>
4
+ #include <stdint.h>
5
+ #include <string.h>
6
+
7
+ /* Repeat NumPy's double pairwise reduction on a contiguous axis. An
8
+ * interleaved channel's stride prevents pairwise summation on the time axis. */
9
+ static double pairwise_sum(const double *x, Py_ssize_t n) {
10
+ if (n < 8) {
11
+ double s = -0.;
12
+ for (Py_ssize_t j = 0; j < n; j++) s += x[j];
13
+ return s;
14
+ }
15
+ if (n > 128) {
16
+ Py_ssize_t mid = (n / 2) / 8 * 8;
17
+ return pairwise_sum(x, mid) + pairwise_sum(x + mid, n - mid);
18
+ }
19
+ double lane[8];
20
+ memcpy(lane, x, sizeof lane);
21
+ Py_ssize_t j = 8;
22
+ for (; j + 8 <= n; j += 8)
23
+ for (int k = 0; k < 8; k++) lane[k] += x[j+k];
24
+ double s = ((lane[0]+lane[1])+(lane[2]+lane[3])) +
25
+ ((lane[4]+lane[5])+(lane[6]+lane[7]));
26
+ for (; j < n; j++) s += x[j];
27
+ return s;
28
+ }
29
+
30
+ static double ordered_sum(const double *x, Py_ssize_t n, int contiguous_axis) {
31
+ if (contiguous_axis) return pairwise_sum(x, n);
32
+ double s = 0.;
33
+ for (Py_ssize_t j = 0; j < n; j++) s += x[j];
34
+ return s;
35
+ }
36
+
37
+ static int get_view(PyObject *obj, Py_buffer *v, int rank, int writable) {
38
+ int flags = PyBUF_C_CONTIGUOUS | PyBUF_FORMAT | (writable ? PyBUF_WRITABLE : 0);
39
+ if (PyObject_GetBuffer(obj, v, flags) < 0) return 0;
40
+ if (v->ndim != rank || v->itemsize != 8 || !v->format || strcmp(v->format, "d") ||
41
+ (uintptr_t)v->buf % 8) {
42
+ PyErr_SetString(PyExc_ValueError, "expected aligned contiguous native float64 array of given rank");
43
+ return 0;
44
+ }
45
+ return 1;
46
+ }
47
+
48
+ static int overlaps(const Py_buffer *x, const Py_buffer *y) {
49
+ uintptr_t a = (uintptr_t)x->buf, b = (uintptr_t)y->buf;
50
+ return x->len && y->len && (a <= b ? b-a < (uintptr_t)x->len : a-b < (uintptr_t)y->len);
51
+ }
52
+
53
+ static void release_views(Py_buffer *v, int count) {
54
+ for (int i = 0; i < count; i++)
55
+ if (v[i].obj) PyBuffer_Release(v+i);
56
+ }
57
+
58
+ #include "moments_tiles.h"
59
+
60
+ static PyObject *stats(PyObject *self, PyObject *args) {
61
+ PyObject *xo, *yo;
62
+ Py_buffer v[2] = {{0}};
63
+ if (!PyArg_ParseTuple(args, "OO", &xo, &yo)) return NULL;
64
+ if (!get_view(xo, v, 3, 0) || !get_view(yo, v+1, 3, 1)) goto done;
65
+ Py_ssize_t n = v[0].shape[0], t = v[0].shape[1], c = v[0].shape[2];
66
+ if (t < 1 || t > 8192 || c < 1 || v[1].shape[0] != n ||
67
+ v[1].shape[1] != 11 || v[1].shape[2] != c || overlaps(v, v+1)) {
68
+ PyErr_SetString(PyExc_ValueError, "invalid stats geometry");
69
+ goto done;
70
+ }
71
+ double *signal = PyMem_Malloc(6*t*sizeof(double));
72
+ if (!signal) { PyErr_NoMemory(); goto done; }
73
+ double *sq = signal+t, *cube = sq+t, *fourth = cube+t;
74
+ double *diff = fourth+t, *diff_sq = diff+t;
75
+ const double *x = v[0].buf;
76
+ double *out = v[1].buf;
77
+ int contiguous = c == 1;
78
+ Py_BEGIN_ALLOW_THREADS
79
+ Py_ssize_t i=0;
80
+ #ifdef MOM512
81
+ if(t<=256 && c<=64 && __builtin_cpu_supports("avx512f"))
82
+ for(;i+8<=n;i+=8)stats_tile8(x+i*t*c,(int)t,(int)c,out+i*11*c);
83
+ #endif
84
+ for (; i < n; i++) for (Py_ssize_t ch = 0; ch < c; ch++) {
85
+ const double *src = x + i*t*c + ch;
86
+ double *dst = out + i*11*c + ch;
87
+ double high = src[0], low = src[0];
88
+ int crossings = 0;
89
+ for (Py_ssize_t j = 0; j < t; j++) {
90
+ double value = src[c*j];
91
+ signal[j] = value;
92
+ sq[j] = value*value;
93
+ diff[j] = j ? value-signal[j-1] : 0.;
94
+ if (value > high) high = value;
95
+ if (value < low) low = value;
96
+ if (j) crossings += signbit(value) != signbit(signal[j-1]);
97
+ }
98
+ double mean = ordered_sum(signal, t, contiguous)/t;
99
+ double dmean = ordered_sum(diff, t, contiguous)/t;
100
+ double power = ordered_sum(sq, t, contiguous)/t;
101
+ double slope = 0., mid = (t-1)*.5;
102
+ for (Py_ssize_t j = 0; j < t; j++) {
103
+ double centered = signal[j]-mean, delta = diff[j]-dmean;
104
+ diff_sq[j] = delta*delta;
105
+ sq[j] = centered*centered;
106
+ cube[j] = sq[j]*centered;
107
+ fourth[j] = sq[j]*sq[j];
108
+ slope += (j-mid)*centered;
109
+ }
110
+ dst[0] = mean;
111
+ dst[c] = ordered_sum(sq, t, contiguous)/t;
112
+ dst[2*c] = low;
113
+ dst[3*c] = high;
114
+ dst[4*c] = power;
115
+ dst[5*c] = dmean;
116
+ dst[6*c] = sqrt(ordered_sum(diff_sq, t, contiguous)/t);
117
+ dst[7*c] = t > 1 ? (double)crossings/(t-1) : 0.;
118
+ dst[8*c] = t > 1 ? slope/((double)t*((double)t*t-1)/12.) : 0.;
119
+ dst[9*c] = ordered_sum(cube, t, contiguous)/t;
120
+ dst[10*c] = ordered_sum(fourth, t, contiguous)/t;
121
+ }
122
+ Py_END_ALLOW_THREADS
123
+ PyMem_Free(signal);
124
+ done:
125
+ release_views(v, 2);
126
+ if (PyErr_Occurred()) return NULL;
127
+ Py_RETURN_NONE;
128
+ }
129
+
130
+ static PyObject *segments(PyObject *self, PyObject *args) {
131
+ PyObject *xo, *yo;
132
+ Py_buffer v[2] = {{0}};
133
+ int pieces, fast;
134
+ if (!PyArg_ParseTuple(args, "OOii", &xo, &yo, &pieces, &fast)) return NULL;
135
+ if (!get_view(xo, v, 3, 0) || !get_view(yo, v+1, 4, 1)) goto done;
136
+ Py_ssize_t n = v[0].shape[0], t = v[0].shape[1], c = v[0].shape[2], fields = fast ? 3 : 4;
137
+ if (t < 1 || t > 8192 || c < 1 || pieces < 1 || pieces > t || v[1].shape[0] != n ||
138
+ v[1].shape[1] != fields || v[1].shape[2] != pieces || v[1].shape[3] != c || overlaps(v, v+1)) {
139
+ PyErr_SetString(PyExc_ValueError, "invalid segment geometry");
140
+ goto done;
141
+ }
142
+ Py_ssize_t length = (t+pieces-1)/pieces;
143
+ double *signal = PyMem_Malloc(2*length*sizeof(double));
144
+ if (!signal) { PyErr_NoMemory(); goto done; }
145
+ double *sq = signal+length, *out = v[1].buf;
146
+ const double *x = v[0].buf;
147
+ Py_BEGIN_ALLOW_THREADS
148
+ Py_ssize_t i=0;
149
+ #ifdef MOM512
150
+ if(t<=256 && c<=64 && __builtin_cpu_supports("avx512f"))
151
+ for(;i+8<=n;i+=8)seg_tile8(x+i*t*c,(int)t,(int)c,pieces,(int)fields,out+i*fields*pieces*c);
152
+ #endif
153
+ for (; i < n; i++) for (int s = 0; s < pieces; s++) for (Py_ssize_t k = 0; k < c; k++) {
154
+ const double *src = x+i*t*c+k;
155
+ double *dst = out+(i*fields*pieces+s)*c+k;
156
+ Py_ssize_t offset = (Py_ssize_t)s*length, field_stride = pieces*c;
157
+ /* The final observed value fills padded segment positions. */
158
+ for (Py_ssize_t j = 0; j < length; j++)
159
+ signal[j] = src[(offset+j < t ? offset+j : t-1)*c];
160
+ double mean = ordered_sum(signal, length, c == 1)/length;
161
+ double high = signal[0], low = high, slope = 0., mid = (length-1)*.5;
162
+ for (Py_ssize_t j = 0; j < length; j++) {
163
+ double value = signal[j], centered = value-mean;
164
+ if (value > high) high = value;
165
+ if (value < low) low = value;
166
+ sq[j] = centered*centered;
167
+ if (!fast) slope += (j-mid)*centered;
168
+ }
169
+ dst[0] = mean;
170
+ dst[field_stride] = sqrt(ordered_sum(sq, length, c == 1)/length);
171
+ dst[2*field_stride] = high-low;
172
+ if (!fast)
173
+ dst[3*field_stride] = length > 1 ? slope/((double)length*((double)length*length-1)/12.) : 0.;
174
+ }
175
+ Py_END_ALLOW_THREADS
176
+ PyMem_Free(signal);
177
+ done:
178
+ release_views(v, 2);
179
+ if (PyErr_Occurred()) return NULL;
180
+ Py_RETURN_NONE;
181
+ }
182
+
183
+ #include "shapelet_tiles.h"
184
+
185
+ static PyObject *shapelets(PyObject *self, PyObject *args) {
186
+ PyObject *xo, *wo, *yo;
187
+ Py_buffer v[3] = {{0}};
188
+ int dilation, stride;
189
+ if (!PyArg_ParseTuple(args, "OOOii", &xo, &wo, &yo, &dilation, &stride)) return NULL;
190
+ if (!get_view(xo, v, 2, 0) || !get_view(wo, v+1, 2, 0) || !get_view(yo, v+2, 3, 1)) goto done;
191
+ Py_ssize_t n = v[0].shape[0], t = v[0].shape[1], k = v[1].shape[0], length = v[1].shape[1];
192
+ if (t < 1 || length < 1 || length > 256 || k < 1 || stride < 1 || dilation < 1 ||
193
+ length-1 > (t-1)/dilation || v[2].shape[0] != n || v[2].shape[1] != k ||
194
+ v[2].shape[2] != 2 || overlaps(v,v+2) || overlaps(v+1,v+2)) {
195
+ PyErr_SetString(PyExc_ValueError, "invalid shapelet geometry");
196
+ goto done;
197
+ }
198
+ double *signal = PyMem_Malloc(2*length*sizeof(double));
199
+ if (!signal) { PyErr_NoMemory(); goto done; }
200
+ const double *x = v[0].buf, *weights = v[1].buf;
201
+ double *out = v[2].buf, *sq = signal+length;
202
+ const Py_ssize_t stop = t - (length-1)*dilation;
203
+ Py_BEGIN_ALLOW_THREADS
204
+ for (Py_ssize_t row = 0; row < n; row++) {
205
+ double *dst = out+row*k*2;
206
+ for (Py_ssize_t h = 0; h < k; h++) {
207
+ dst[2*h] = INFINITY;
208
+ dst[2*h+1] = 0.;
209
+ }
210
+ #ifdef SHAPE512
211
+ if (length <= 64 && (stop-1)/stride >= 7 && __builtin_cpu_supports("avx512f")) {
212
+ shapelet_row(x+row*t,weights,dst,stop,(int)length,dilation,stride,k);
213
+ continue;
214
+ }
215
+ #endif
216
+ for (Py_ssize_t a = 0; a < stop; a += stride) {
217
+ for (Py_ssize_t j = 0; j < length; j++) signal[j] = x[row*t+a+j*dilation];
218
+ double mean = pairwise_sum(signal, length)/length;
219
+ for (Py_ssize_t j = 0; j < length; j++) signal[j] -= mean;
220
+ /* NumPy std recomputes the mean of the already centered window. */
221
+ double centered_mean = pairwise_sum(signal, length)/length;
222
+ for (Py_ssize_t j = 0; j < length; j++) {
223
+ double value = signal[j]-centered_mean;
224
+ sq[j] = value*value;
225
+ }
226
+ double sd = sqrt(pairwise_sum(sq, length)/length);
227
+ if (sd < 1e-6) sd = 1.;
228
+ for (Py_ssize_t j = 0; j < length; j++) signal[j] /= sd;
229
+ /* Normalize once, compare every prototype with this geometry. */
230
+ for (Py_ssize_t h = 0; h < k; h++) {
231
+ for (Py_ssize_t j = 0; j < length; j++) {
232
+ double d = signal[j]-weights[h*length+j];
233
+ sq[j] = d*d;
234
+ }
235
+ double value = sqrt(pairwise_sum(sq, length)/length);
236
+ if (value < dst[2*h]) {
237
+ dst[2*h] = value;
238
+ dst[2*h+1] = (double)a/(stop > 1 ? stop-1 : 1);
239
+ }
240
+ }
241
+ }
242
+ }
243
+ Py_END_ALLOW_THREADS
244
+ PyMem_Free(signal);
245
+ done:
246
+ release_views(v, 3);
247
+ if (PyErr_Occurred()) return NULL;
248
+ Py_RETURN_NONE;
249
+ }
250
+
251
+ /* Bounded common geometry. Each scalar uses NumPy's channel-dependent
252
+ reduction tree. Return false to the wrapper for non-finite normalization. */
253
+ static PyObject *normalize(PyObject *self, PyObject *args) {
254
+ PyObject *xo,*yo;Py_buffer v[2]={{0}};
255
+ int finite=1;
256
+ if(!PyArg_ParseTuple(args,"OO",&xo,&yo))return NULL;
257
+ if(!get_view(xo,v,3,0)||!get_view(yo,v+1,3,1))goto done;
258
+ Py_ssize_t n=v[0].shape[0],t=v[0].shape[1],c=v[0].shape[2];
259
+ if(t<1||t>8192||c<1||c>64||memcmp(v[0].shape,v[1].shape,3*sizeof(Py_ssize_t))||overlaps(v,v+1)){
260
+ PyErr_SetString(PyExc_ValueError,"invalid normalization buffers");goto done;
261
+ }
262
+ double *tmp=PyMem_Malloc(2*t*sizeof(double));
263
+ if(!tmp){PyErr_NoMemory();goto done;}
264
+ double *sq=tmp+t;
265
+ const double *x=v[0].buf;double *y=v[1].buf;
266
+ Py_BEGIN_ALLOW_THREADS
267
+ for(Py_ssize_t i=0;finite && i<n;i++)for(Py_ssize_t ch=0;ch<c;ch++){
268
+ const double *s=x+i*t*c+ch;double *o=y+i*t*c+ch;
269
+ for(Py_ssize_t j=0;j<t;j++)tmp[j]=s[j*c];
270
+ double mu=ordered_sum(tmp,t,c==1)/t;
271
+ if(mu==0.)mu=0.; /* NumPy adds its positive-zero reduction identity. */
272
+ for(Py_ssize_t j=0;j<t;j++){tmp[j]-=mu;sq[j]=tmp[j]*tmp[j];}
273
+ double sd=sqrt(ordered_sum(sq,t,c==1)/t);
274
+ if(!isfinite(sd)){finite=0;break;}
275
+ if(sd<1e-6)sd=1.;
276
+ for(Py_ssize_t j=0;j<t;j++)o[j*c]=tmp[j]/sd;
277
+ }
278
+ Py_END_ALLOW_THREADS
279
+ PyMem_Free(tmp);
280
+ done:
281
+ release_views(v,2);
282
+ if(PyErr_Occurred())return NULL;
283
+ return PyBool_FromLong(finite);
284
+ }
285
+
286
+ /* Avoid allocating products and performing a separate NumPy reduction per
287
+ lag. Scratch is bounded by the window length, independent of batch size. */
288
+ #include "autocorr_lanes.h"
289
+ static PyObject *autocorr(PyObject *self,PyObject *args){
290
+ PyObject *xo,*yo,*lags_obj,*co=NULL;Py_buffer v[3]={{0}};
291
+ if(!PyArg_ParseTuple(args,"OOO|O",&xo,&lags_obj,&yo,&co))return NULL;
292
+ if(!get_view(xo,v,3,0)||!get_view(yo,v+1,3,1))goto done;
293
+ if(co && (!get_view(co,v+2,3,1)||memcmp(v[2].shape,v[0].shape,3*sizeof(Py_ssize_t))||overlaps(v+2,v)||overlaps(v+2,v+1))){
294
+ if(!PyErr_Occurred())PyErr_SetString(PyExc_ValueError,"invalid centered buffer");
295
+ goto done;
296
+ }
297
+ Py_ssize_t n=v[0].shape[0],t=v[0].shape[1],c=v[0].shape[2],k=v[1].shape[1];
298
+ if(t<1||t>8192||c<1||k>64||v[1].shape[0]!=n||v[1].shape[2]!=c||
299
+ !PyTuple_Check(lags_obj)||PyTuple_Size(lags_obj)!=k||overlaps(v,v+1)){
300
+ PyErr_SetString(PyExc_ValueError,"invalid autocorrelation buffers");goto done;
301
+ }
302
+ long lags[64];
303
+ for(Py_ssize_t l=0;l<k;l++){
304
+ lags[l]=PyLong_AsLong(PyTuple_GET_ITEM(lags_obj,l));
305
+ if(PyErr_Occurred())goto done;
306
+ }
307
+ double *tmp=PyMem_Malloc(2*t*sizeof(double));if(!tmp){PyErr_NoMemory();goto done;}
308
+ double *prod=tmp+t;const double *x=v[0].buf;double *y=v[1].buf;
309
+ Py_ssize_t first=0;
310
+ double *tile=NULL;
311
+ #ifdef WISP_ACF_LANES
312
+ if(n>=8 && t<=1024 && c<=16 && __builtin_cpu_supports("avx512f")){
313
+ tile=PyMem_Malloc(8*t*sizeof(double));
314
+ if(!tile){PyMem_Free(tmp);PyErr_NoMemory();goto done;}
315
+ }
316
+ #endif
317
+ Py_BEGIN_ALLOW_THREADS
318
+ #ifdef WISP_ACF_LANES
319
+ if(tile)for(;first+8<=n;first+=8)
320
+ acf_tile(x+first*t*c,t,c,lags,k,tile,co?(double*)v[2].buf+first*t*c:NULL,y+first*k*c);
321
+ #endif
322
+ for(Py_ssize_t i=first;i<n;i++)for(Py_ssize_t ch=0;ch<c;ch++){
323
+ const double *s=x+i*t*c+ch;
324
+ for(Py_ssize_t j=0;j<t;j++)tmp[j]=s[j*c];
325
+ double mu=ordered_sum(tmp,t,c==1)/t;
326
+ if(mu==0.)mu=0.; /* NumPy adds its positive-zero reduction identity. */
327
+ for(Py_ssize_t j=0;j<t;j++){tmp[j]-=mu;prod[j]=tmp[j]*tmp[j];}
328
+ if(co){double *o=(double*)v[2].buf+i*t*c+ch;for(Py_ssize_t j=0;j<t;j++)o[j*c]=tmp[j];}
329
+ double den=ordered_sum(prod,t,c==1)+1.e-12;
330
+ for(Py_ssize_t l=0;l<k;l++){
331
+ long lag=lags[l];double sum=0;
332
+ if(lag>0 && lag<t){
333
+ Py_ssize_t len=t-lag;
334
+ if(c==1){
335
+ for(Py_ssize_t j=0;j<len;j++)prod[j]=tmp[j]*tmp[j+lag];
336
+ sum=pairwise_sum(prod,len);
337
+ }else{
338
+ for(Py_ssize_t j=0;j<len;j++)sum+=tmp[j]*tmp[j+lag];
339
+ }
340
+ }
341
+ y[(i*k+l)*c+ch]=lag>0 && lag<t ? (sum+0.)/den : 0.;
342
+ }
343
+ }
344
+ Py_END_ALLOW_THREADS
345
+ PyMem_Free(tile);PyMem_Free(tmp);
346
+ done:
347
+ release_views(v,3);if(PyErr_Occurred())return NULL;Py_RETURN_NONE;
348
+ }
349
+
350
+ /* Scalars match NumPy's contiguous-axis pairwise sums or its strided
351
+ linear sums. Prepare FFT and ACF from the same centered waveform. */
352
+ static PyObject *derived(PyObject *self,PyObject *args){
353
+ PyObject *objs[4];Py_buffer v[4]={{0}};
354
+ if(!PyArg_ParseTuple(args,"OOOO",objs,objs+1,objs+2,objs+3))return NULL;
355
+ for(int j=0;j<4;j++)if(!get_view(objs[j],v+j,3,j!=0))goto finish;
356
+ Py_ssize_t n=v[0].shape[0],t=v[0].shape[1],c=v[0].shape[2],axes=c>3?3:c;
357
+ if(t<1||c<2||v[1].shape[0]!=n||v[1].shape[1]!=t||v[1].shape[2]!=1||
358
+ memcmp(v[1].shape,v[3].shape,3*sizeof(Py_ssize_t))||
359
+ memcmp(v[0].shape,v[2].shape,3*sizeof(Py_ssize_t))){
360
+ PyErr_SetString(PyExc_ValueError,"invalid derived-signal buffers");goto finish;
361
+ }
362
+ for(int j=0;j<4;j++)for(int h=j+1;h<4;h++)if(overlaps(v+j,v+h)){
363
+ PyErr_SetString(PyExc_ValueError,"overlapping derived-signal buffers");goto finish;
364
+ }
365
+ const double *x=v[0].buf;double *mag=v[1].buf,*jerk=v[2].buf,*jm=v[3].buf;
366
+ Py_BEGIN_ALLOW_THREADS
367
+ for(Py_ssize_t i=0;i<n;i++)for(Py_ssize_t j=0;j<t;j++){
368
+ Py_ssize_t off=(i*t+j)*c;double a=0.,b=0.;
369
+ for(Py_ssize_t h=0;h<c;h++){
370
+ double z=x[off+h],d=z-x[off+h-(j?c:0)];jerk[off+h]=d;
371
+ if(h<axes){a+=z*z;b+=d*d;}
372
+ }
373
+ mag[i*t+j]=sqrt(a);jm[i*t+j]=sqrt(b);
374
+ }
375
+ Py_END_ALLOW_THREADS
376
+ finish:
377
+ release_views(v,4);if(PyErr_Occurred())return NULL;Py_RETURN_NONE;
378
+ }
379
+ static PyObject *spectral(PyObject *self,PyObject *args){
380
+ PyObject *objs[5];Py_buffer v[5]={{0}};
381
+ if(!PyArg_ParseTuple(args,"OOOOO",objs,objs+1,objs+2,objs+3,objs+4))return NULL;
382
+ const int ranks[5]={3,1,2,3,3};
383
+ for(int j=0;j<5;j++)if(!get_view(objs[j],v+j,ranks[j],j>=3))goto finish;
384
+ Py_ssize_t n=v[0].shape[0],f=v[0].shape[1],c=v[0].shape[2],b=v[2].shape[0];
385
+ if(f<2||f>4097||c<1||v[1].shape[0]!=f||v[2].shape[1]!=2||
386
+ v[3].shape[0]!=n||v[3].shape[1]!=f-1||v[3].shape[2]!=c||
387
+ v[4].shape[0]!=n||v[4].shape[1]!=b+3||v[4].shape[2]!=c){
388
+ PyErr_SetString(PyExc_ValueError,"invalid spectral buffers");goto finish;
389
+ }
390
+ for(int j=3;j<5;j++)for(int h=0;h<j;h++)if(overlaps(v+j,v+h)){
391
+ PyErr_SetString(PyExc_ValueError,"overlapping spectral buffers");goto finish;
392
+ }
393
+ const double *power=v[0].buf,*freq=v[1].buf,*bounds=v[2].buf;
394
+ for(Py_ssize_t j=0;j<b;j++)if(!(bounds[2*j]>=0 && bounds[2*j]<=bounds[2*j+1] &&
395
+ bounds[2*j+1]<=f && bounds[2*j]==floor(bounds[2*j])&&bounds[2*j+1]==floor(bounds[2*j+1]))){
396
+ PyErr_SetString(PyExc_ValueError,"invalid band bounds");goto finish;
397
+ }
398
+ double *tmp=PyMem_Malloc(2*f*sizeof(double));if(!tmp){PyErr_NoMemory();goto finish;}
399
+ double *prob=v[3].buf,*out=v[4].buf,*weighted=tmp+f;
400
+ Py_BEGIN_ALLOW_THREADS
401
+ for(Py_ssize_t i=0;i<n;i++)for(Py_ssize_t h=0;h<c;h++){
402
+ const double *x=power+i*f*c+h;double *q=prob+i*(f-1)*c+h,*z=out+i*(b+3)*c+h;
403
+ for(Py_ssize_t j=0;j<f;j++){tmp[j]=x[j*c];weighted[j]=freq[j]*tmp[j];}
404
+ double total=ordered_sum(tmp+1,f-1,c==1)+1.e-12;
405
+ for(Py_ssize_t j=0;j<b;j++){
406
+ int lo=(int)bounds[2*j],hi=(int)bounds[2*j+1];
407
+ z[j*c]=lo==hi?0.:ordered_sum(tmp+lo,hi-lo,n==1&&c==1)/total;
408
+ }
409
+ int peak=1;
410
+ for(int j=1;j<f;j++){
411
+ q[(j-1)*c]=tmp[j]/total;
412
+ if(tmp[j]>tmp[peak] || (isnan(tmp[j])&&!isnan(tmp[peak])))peak=j;
413
+ }
414
+ z[(b+1)*c]=ordered_sum(weighted+1,f-1,c==1)/total;
415
+ z[(b+2)*c]=freq[peak];
416
+ }
417
+ Py_END_ALLOW_THREADS
418
+ PyMem_Free(tmp);
419
+ finish:
420
+ release_views(v,5);if(PyErr_Occurred())return NULL;Py_RETURN_NONE;
421
+ }
422
+ static PyObject *clean(PyObject *self,PyObject *args){
423
+ PyObject *o;Py_buffer v={0};
424
+ if(!PyArg_ParseTuple(args,"O",&o))return NULL;
425
+ if(!get_view(o,&v,2,1)){if(v.obj)PyBuffer_Release(&v);return NULL;}
426
+ double *a=v.buf;Py_ssize_t n=v.len/sizeof(double);
427
+ Py_BEGIN_ALLOW_THREADS
428
+ for(Py_ssize_t i=0;i<n;i++)a[i]=isfinite(a[i])?a[i]:0.;
429
+ Py_END_ALLOW_THREADS
430
+ PyBuffer_Release(&v);Py_RETURN_NONE;
431
+ }
432
+
433
+ static PyMethodDef methods[] = {
434
+ {"spectral",spectral,METH_VARARGS,"Spectral band, frequency, peak and normalized power."},
435
+ {"derived",derived,METH_VARARGS,"Magnitude, jerk and jerk magnitude in a single traversal."},
436
+ {"clean",clean,METH_VARARGS,"Replace nonfinite entries by positive zero."},
437
+ {"normalize", normalize, METH_VARARGS, "Finite per-window z score."},
438
+ {"autocorr", autocorr, METH_VARARGS, "Within-window lag correlations."},
439
+ {"stats", stats, METH_VARARGS, "Compute centered raw statistics."},
440
+ {"segments", segments, METH_VARARGS, "Pool padded segments."},
441
+ {"shapelets", shapelets, METH_VARARGS, "Match normalized subsequences."},
442
+ {NULL, NULL, 0, NULL}
443
+ };
444
+ static struct PyModuleDef mod = {PyModuleDef_HEAD_INIT, "_family_kernels", NULL, -1, methods};
445
+ PyMODINIT_FUNC PyInit__family_kernels(void) { return PyModule_Create(&mod); }
src/wisp_fast/features.py ADDED
@@ -0,0 +1,626 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Accelerated transforms for the pinned WISP release.
2
+
3
+ Faithful mode retains NumPy's arithmetic on scale-sensitive features. The
4
+ fast_profile parameter is a training-time choice and changes the features.
5
+ """
6
+ from collections import defaultdict
7
+ from dataclasses import dataclass
8
+ from functools import lru_cache
9
+
10
+ import numpy as np
11
+ from numpy.lib.stride_tricks import sliding_window_view
12
+ from scipy.stats import kurtosis, skew
13
+
14
+ from wisp.cpu import features as ref
15
+
16
+ try:
17
+ from . import _kernels
18
+ except ImportError:
19
+ _kernels = None
20
+
21
+
22
+ try:
23
+ from . import _cis_intervals
24
+ except ImportError:
25
+ _cis_intervals = None
26
+
27
+
28
+ def _state_counts(x, n_bins):
29
+ n, length, channels = x.shape
30
+ blocks = []
31
+ levels = np.linspace(0, 1, n_bins + 1)[1:-1]
32
+ offset = np.arange(n)[:, None]
33
+ for c in range(channels):
34
+ xc = x[:, :, c]
35
+ q = np.quantile(xc, levels, axis=1).T
36
+ states = np.zeros((n, length), dtype=np.intp)
37
+ for border in q.T:
38
+ states += xc > border[:, None]
39
+ h = np.bincount((states + offset * n_bins).ravel(), minlength=n * n_bins)
40
+ trans = (offset * n_bins + states[:, :-1]) * n_bins + states[:, 1:]
41
+ mat = np.bincount(trans.ravel(), minlength=n * n_bins * n_bins)
42
+ mat = mat.reshape(n, n_bins, n_bins)
43
+ flat = (mat / np.maximum(mat.sum(axis=2, keepdims=True), 1)).reshape(n, -1)
44
+ ent = -(flat * np.log(flat + 1e-12)).sum(axis=1, keepdims=True)
45
+ blocks.extend((h.reshape(n, n_bins) / length, flat, ent))
46
+ return np.concatenate(blocks, axis=1)
47
+
48
+
49
+ @lru_cache(maxsize=4)
50
+ def _state_table(length):
51
+ den = np.maximum(np.arange(length + 1), 1)[:, None]
52
+ p = np.arange(length + 1, dtype=np.float64)[None, :] / den
53
+ return np.stack((p, p * np.log(p + 1e-12)), axis=-1)
54
+
55
+
56
+ @lru_cache(maxsize=128)
57
+ def _state_levels(length,bins):
58
+ pos = (length-1)*np.linspace(0,1,bins+1)[1:-1]
59
+ lo = pos.astype(np.intp)
60
+ return lo, np.minimum(lo+1,length-1), pos-lo
61
+
62
+
63
+ @lru_cache(maxsize=256)
64
+ def _column_names(prefix,width):
65
+ return tuple(f"{prefix}_{j}" for j in range(width))
66
+
67
+
68
+ def _symbolic_bundle(variants, bins):
69
+ """Quantiles and transition counts for every signal view in the CIS state block."""
70
+ slices = [v[:,:,:3].transpose(0,2,1) for _, v in variants]
71
+ n, _, length = slices[0].shape
72
+ x = np.empty((n, sum(v.shape[1] for v in slices), length))
73
+ offset = 0
74
+ for v in slices:
75
+ x[:,offset:offset+v.shape[1]] = v
76
+ offset += v.shape[1]
77
+ n, width, length = x.shape
78
+ lo, hi, frac = _state_levels(length, bins)
79
+ part = (np.sort(x,axis=2) if length <= 512 else
80
+ np.partition(x, np.unique(np.r_[lo,hi]), axis=2))
81
+ a, b = np.take(part,lo,axis=2), np.take(part,hi,axis=2)
82
+ q = np.ascontiguousarray(np.where(frac>=.5,b-(b-a)*(1-frac),a+(b-a)*frac))
83
+ if length<=512 and bins<=16 and hasattr(_cis_intervals, 'state_pool'):
84
+ out = np.empty((n,width,bins+bins*bins+1))
85
+ _cis_intervals.state_pool(x,q,_state_table(length),out)
86
+ else:
87
+ counts = np.empty((n,width,bins+bins*bins))
88
+ _cis_intervals.state_counts(x,q,counts)
89
+ h = counts[:,:,:bins] / length
90
+ mat = counts[:,:,bins:].reshape(n,width,bins,bins)
91
+ flat = (mat / np.maximum(mat.sum(axis=3,keepdims=True),1)).reshape(n,width,-1)
92
+ e = -(flat*np.log(flat+1e-12)).sum(axis=2,keepdims=True)
93
+ out = np.concatenate((h,flat,e),axis=2)
94
+ boundaries = np.cumsum([v.shape[1] for v in slices])[:-1]
95
+ return {name:block.reshape(n,-1) for (name,_),block in
96
+ zip(variants,np.split(out,boundaries,axis=1))}
97
+
98
+
99
+ try:
100
+ from . import _family_kernels
101
+ except ImportError:
102
+ _family_kernels = None
103
+
104
+
105
+ def _normalize(x):
106
+ if (_family_kernels is not None and x.dtype == np.float64 and x.flags.c_contiguous
107
+ and x.flags.aligned and 0 < x.shape[1] <= 8192 and 0 < x.shape[2] <= 64):
108
+ out = np.empty_like(x)
109
+ if _family_kernels.normalize(x, out):
110
+ return out
111
+ return ref._safe_zscore_per_window(x)
112
+
113
+
114
+ def _autocorr(v, lags):
115
+ if (_family_kernels is not None and v.dtype == np.float64 and v.flags.c_contiguous
116
+ and v.flags.aligned and 0 < v.shape[1] <= 8192 and len(lags) <= 64):
117
+ out = np.empty((len(v),len(lags),v.shape[2]))
118
+ _family_kernels.autocorr(v, tuple(int(lag) for lag in lags), out)
119
+ return out.reshape(len(v),len(lags)*v.shape[2])
120
+ return ref._autocorr_features(v, lags)
121
+
122
+
123
+ def _clean(out):
124
+ if _family_kernels is not None and out.flags.c_contiguous and out.flags.aligned and out.dtype==np.float64 and out.ndim==2:
125
+ _family_kernels.clean(out)
126
+ return out
127
+ return np.nan_to_num(out,copy=False,nan=0.,posinf=0.,neginf=0.)
128
+
129
+
130
+ @lru_cache(maxsize=64)
131
+ def _frequency_plan(time,bands):
132
+ freq=np.fft.rfftfreq(time)
133
+ pairs=[(0,0) if np.isnan(lo) or np.isnan(hi) else
134
+ (int(freq.searchsorted(lo)),int(freq.searchsorted(hi))) for lo,hi in bands]
135
+ return freq,np.array([(l,max(l,r)) for l,r in pairs],dtype=np.float64).reshape(-1,2)
136
+
137
+
138
+ def _spectrum_centered(center,bands):
139
+ n,t,c=center.shape
140
+ freq,bounds=_frequency_plan(t,tuple(map(tuple,bands)))
141
+ power=np.abs(np.fft.rfft(center,axis=1))
142
+ np.square(power,out=power);power/=t
143
+ prob=np.empty((n,len(freq)-1,c))
144
+ out=np.empty((n,len(bounds)+3,c))
145
+ _family_kernels.spectral(power,freq,bounds,prob,out)
146
+ work=np.log(prob+1.e-12);work*=prob
147
+ np.copyto(work,0.,where=~(prob>0))
148
+ out[:,len(bounds)]=-work.sum(axis=1)/np.log(max(len(freq)-1,2))
149
+ return out.reshape(n,-1)
150
+
151
+
152
+ def _raw_quantiles(variants, bins, need_symbols):
153
+ """One time-axis partition supplies the median, quartiles and state boundaries."""
154
+ x = np.ascontiguousarray(np.concatenate([v.transpose(0, 2, 1) for _, v in variants], axis=1))
155
+ n, width, time = x.shape
156
+ levels = np.array([.25, .5, .75])
157
+ if need_symbols:
158
+ levels = np.r_[levels, np.linspace(0, 1, bins + 1)[1:-1]]
159
+ pos = (time - 1) * levels
160
+ lower = pos.astype(np.intp)
161
+ upper = np.minimum(lower + 1, time - 1)
162
+ # Short multi-quantile axes are cheaper to sort. +0 and -0 compare
163
+ # equal but may have different median bytes after sort vs partition.
164
+ if time<=512 and not np.any(x.view(np.uint64)==np.uint64(1)<<np.uint64(63)):
165
+ part=np.sort(x,axis=-1)
166
+ else:
167
+ part=np.partition(x,np.unique(np.r_[lower,upper]),axis=-1)
168
+ a, b, frac = part[:, :, lower], part[:, :, upper], pos - lower
169
+ # NumPy's linear interpolation evaluates from the closer endpoint.
170
+ q = np.where(frac >= .5, b - (b-a)*(1-frac), a + (b-a)*frac)
171
+ median = a[:, :, 1] if time % 2 else (a[:, :, 1] + b[:, :, 1]) * .5
172
+
173
+ pooled = None
174
+ if need_symbols:
175
+ state_x, thresholds = x, q[:, :, 3:]
176
+ if any(v.shape[2] > 3 for _, v in variants):
177
+ starts = np.cumsum([0] + [v.shape[2] for _, v in variants[:-1]])
178
+ indices = np.concatenate([np.arange(start, start + min(3, v.shape[2]))
179
+ for start, (_, v) in zip(starts, variants)])
180
+ state_x = np.ascontiguousarray(x[:, indices])
181
+ thresholds = thresholds[:, indices]
182
+ state_width = state_x.shape[1]
183
+ counts = np.empty((n, state_width, bins * (bins+1)))
184
+ _cis_intervals.state_counts(state_x, np.ascontiguousarray(thresholds), counts)
185
+ hist = counts[:, :, :bins] / time
186
+ mat = counts[:, :, bins:].reshape(n, state_width, bins, bins)
187
+ flat = (mat / np.maximum(mat.sum(axis=3, keepdims=True), 1)).reshape(n, state_width, -1)
188
+ entropy = -(flat * np.log(flat + 1e-12)).sum(axis=-1, keepdims=True)
189
+ pooled = np.concatenate((hist, flat, entropy), axis=2)
190
+
191
+ quantiles, symbols = {}, {}
192
+ start, symbol_start = 0, 0
193
+ for name, v in variants:
194
+ end = start + v.shape[2]
195
+ quantiles[name] = (median[:, start:end], q[:, start:end, 0], q[:, start:end, 2])
196
+ if pooled is not None:
197
+ symbol_end = symbol_start + min(3, v.shape[2])
198
+ symbols[name] = pooled[:, symbol_start:symbol_end].reshape(n, -1)
199
+ symbol_start = symbol_end
200
+ start = end
201
+ return quantiles, symbols
202
+
203
+
204
+ def _family_stats(v, quantiles):
205
+ n, time, channels = v.shape
206
+ out = np.empty((n, 11, channels))
207
+ _family_kernels.stats(v, out)
208
+ mean, var, low, high, power, dmean, dstd, zcr, slope, third, fourth = out.transpose(1, 0, 2)
209
+ small = var <= (np.finfo(float).eps * mean)**2
210
+ with np.errstate(divide="ignore", invalid="ignore"):
211
+ skewness = np.where(small, 0, third / var**1.5)
212
+ excess = np.where(small, 0, fourth / var**2 - 3)
213
+ if channels == 1:
214
+ # NumPy's einsum uses a contiguous dot path on univariate windows.
215
+ slope = ref._slope(v)
216
+ stats = np.concatenate((mean, np.sqrt(var), low, high, *quantiles, power, dmean,
217
+ dstd, zcr, slope, skewness, excess), axis=1)
218
+ return np.nan_to_num(stats, copy=False)
219
+
220
+
221
+ @dataclass
222
+ class WearableFeatureExtractor(ref.WearableFeatureExtractor):
223
+ fast_profile: bool = False
224
+
225
+ def fit(self, X, y=None):
226
+ # No fitted statistics: the first real transform supplies feature names.
227
+ return self
228
+
229
+ def transform(self, X):
230
+ x = ref._as_3d(X)
231
+ if not self.fast_profile and not x.flags.c_contiguous:
232
+ return super().transform(X)
233
+ x = _normalize(x) if self.zscore else np.nan_to_num(
234
+ x, nan=0.0, posinf=0.0, neginf=0.0)
235
+ return self.transform_normalized(x)
236
+
237
+ def transform_normalized(self, x):
238
+ n, length, channels = x.shape
239
+ variants = [('ch', x)]
240
+ if (self.include_jerk and self.include_magnitude and channels>=2 and _family_kernels is not None
241
+ and x.dtype==np.float64 and x.flags.c_contiguous and x.flags.aligned):
242
+ mag=np.empty((n,length,1));jerk=np.empty_like(x);jm=np.empty_like(mag)
243
+ _family_kernels.derived(x,mag,jerk,jm)
244
+ variants.extend((('mag',mag),('jerk',jerk),('jerk_mag',jm)))
245
+ else:
246
+ if self.include_magnitude and channels >= 2:
247
+ variants.append(('mag', np.linalg.norm(x[:, :, :3], axis=2, keepdims=True)))
248
+ if self.include_jerk:
249
+ jerk = np.diff(x, axis=1, prepend=x[:, :1])
250
+ variants.append(('jerk', jerk))
251
+ if channels >= 2:
252
+ variants.append(('jerk_mag', np.linalg.norm(jerk[:, :, :3], axis=2, keepdims=True)))
253
+ blocks, names = [], []
254
+ bins = min(4, self.symbolic_bins) if self.fast_profile else self.symbolic_bins
255
+ raw_native = (self.include_raw_stats and not self.fast_profile and
256
+ _family_kernels is not None and 1 <= length <= 8192 and
257
+ all(v.flags.c_contiguous and v.flags.aligned for _, v in variants))
258
+ fused_symbols = (self.include_symbolic and length >= 8 and 2 <= bins <= 32 and
259
+ _cis_intervals is not None)
260
+ symbols = None
261
+ if raw_native:
262
+ raw_q, sym_q = _raw_quantiles(variants, bins, fused_symbols)
263
+ if fused_symbols:
264
+ symbols = sym_q
265
+ elif fused_symbols:
266
+ symbols = _symbolic_bundle(variants, bins)
267
+
268
+ def emit(name, block):
269
+ blocks.append(block.reshape(n, -1))
270
+ names.extend(_column_names(name, blocks[-1].shape[1]))
271
+
272
+ for prefix, v in variants:
273
+ if raw_native:
274
+ emit(f'{prefix}_stat', _family_stats(v, raw_q[prefix]))
275
+ elif self.include_raw_stats:
276
+ stats = [v.mean(axis=1), v.std(axis=1), v.min(axis=1), v.max(axis=1)]
277
+ if not self.fast_profile:
278
+ quartiles = np.quantile(v, [.25, .75], axis=1)
279
+ diff = np.diff(v, axis=1, prepend=v[:, :1])
280
+ stats += [np.median(v, axis=1), *quartiles, np.mean(v*v, axis=1),
281
+ diff.mean(axis=1), diff.std(axis=1), ref._zero_crossing_rate(v),
282
+ ref._slope(v), skew(v, axis=1, nan_policy='omit'),
283
+ kurtosis(v, axis=1, nan_policy='omit')]
284
+ emit(f'{prefix}_stat', np.concatenate([np.nan_to_num(s) for s in stats], axis=1))
285
+ if self.include_multiscale:
286
+ sizes = self.segment_sizes[:1] if self.fast_profile else self.segment_sizes
287
+ for w in sizes:
288
+ if w > length:
289
+ continue
290
+ if (_family_kernels is not None and v.flags.c_contiguous and v.flags.aligned
291
+ and 1 <= length <= 8192):
292
+ nf = 3 if self.fast_profile else 4
293
+ packed = np.empty((n, nf, w, v.shape[2]))
294
+ _family_kernels.segments(v, packed, int(w), int(self.fast_profile))
295
+ if nf == 4 and v.shape[2] == 1:
296
+ sv = ref._segment_view(v, int(w))
297
+ packed[:, 3] = ref._slope(sv.reshape(n*w, sv.shape[2], 1)).reshape(n, w, 1)
298
+ emit(f'{prefix}_seg{w}', packed)
299
+ continue
300
+ seg = ref._segment_view(v, int(w))
301
+ means = seg.mean(axis=2)
302
+ stats = [means, seg.std(axis=2), seg.max(axis=2) - seg.min(axis=2)]
303
+ if not self.fast_profile:
304
+ # Match _slope's per-segment reduction order. The flattened
305
+ # sample+segment axes are independent.
306
+ seg_flat = seg.reshape(n*w, seg.shape[2], seg.shape[3])
307
+ stats.append(ref._slope(seg_flat).reshape(means.shape))
308
+ emit(f'{prefix}_seg{w}', np.concatenate([s.reshape(n,-1) for s in stats], axis=1))
309
+ lags=tuple(l for l in self.autocorr_lags if l<length) if self.include_autocorr else ()
310
+ if self.fast_profile:lags=lags[:2]
311
+ if (self.include_spectral and self.include_autocorr and _family_kernels is not None
312
+ and v.dtype==np.float64 and v.flags.c_contiguous and v.flags.aligned
313
+ and 2<=length<=8192 and len(lags)<=64):
314
+ centered=np.empty_like(v);acf=np.empty((n,len(lags),v.shape[2]))
315
+ _family_kernels.autocorr(v,lags,acf,centered)
316
+ emit(f'{prefix}_fft',_spectrum_centered(centered,self.spectral_bands))
317
+ emit(f'{prefix}_acf',acf)
318
+ else:
319
+ if self.include_spectral:emit(f'{prefix}_fft',ref._spectral_features(v,self.spectral_bands))
320
+ if self.include_autocorr:emit(f'{prefix}_acf',_autocorr(v,lags))
321
+ if self.include_symbolic and length >= 8:
322
+ counts = symbols[prefix] if symbols is not None else _state_counts(v[:, :, :3], int(bins))
323
+ emit(f'{prefix}_sym', counts)
324
+ if self.include_cross_channel and channels > 1:
325
+ cols = [x[:, :, c] - x[:, :, c].mean(axis=1, keepdims=True) for c in range(channels)]
326
+ powers = [(col * col).sum(axis=1) for col in cols]
327
+ corr = [(cols[i]*cols[j]).sum(axis=1)/(np.sqrt(powers[i]*powers[j])+1e-12)
328
+ for i in range(channels) for j in range(i+1, channels)]
329
+ emit('corr', np.column_stack(corr))
330
+ if not blocks:
331
+ raise ValueError('No feature blocks enabled')
332
+ self.feature_names_ = names
333
+ return _clean(np.concatenate(blocks, axis=1))
334
+
335
+
336
+ def _pool_response(out, indexes, resp):
337
+ # (kernel, sample, time) with contiguous time, as in the original.
338
+ out[:, indexes, 0] = (resp > 0.).mean(axis=2).T
339
+ out[:, indexes, 1] = resp.max(axis=2).T
340
+ out[:, indexes, 2] = resp.mean(axis=2).T
341
+ out[:, indexes, 3] = resp.std(axis=2).T
342
+
343
+
344
+ try:
345
+ from . import _cis_conv
346
+ except ImportError:
347
+ _cis_conv = None
348
+
349
+
350
+ @lru_cache(maxsize=1)
351
+ def _verified_conv_blas():
352
+ import hashlib
353
+ library = _cis_conv.fused_library() if _cis_conv is not None else None
354
+ if not library or np.__version__ != "2.3.5":
355
+ return False
356
+ try:
357
+ with open(library, "rb") as f:
358
+ actual = hashlib.file_digest(f, "sha256").hexdigest()
359
+ except OSError:
360
+ return False
361
+ return actual == "ee2039cf13a45e1e89f04d0cea9585657d93ffaa425594e1935161013fdf4e12"
362
+
363
+
364
+ def _pack_kernels(kernels):
365
+ count = len(kernels)
366
+ max_len = max(spec["length"] for spec in kernels)
367
+ layout = np.zeros((count, 6), dtype=np.int64)
368
+ weights = np.zeros((count, max_len, 3))
369
+ biases = np.empty(count)
370
+ for j, spec in enumerate(kernels):
371
+ chan = spec["channels"]
372
+ length = spec["length"]
373
+ layout[j, :3] = (length, spec["dilation"], len(chan))
374
+ layout[j, 3:3+len(chan)] = chan
375
+ weights[j, :length, :len(chan)] = spec["weights"]
376
+ biases[j] = spec["bias"]
377
+ max_offset = int(np.max((layout[:, 0]-1)*layout[:, 1]))
378
+ return layout, weights, biases, max_offset, bool(np.any(layout[:, 2] > 1))
379
+
380
+
381
+ @dataclass
382
+ class RandomConvSketch(ref.RandomConvSketch):
383
+ fast_profile: bool = False
384
+
385
+ def fit(self, X, y=None):
386
+ self.invalidate_plan()
387
+ return super().fit(X, y)
388
+
389
+ def invalidate_plan(self):
390
+ """Call after custom in-place edits to learned kernel specifications."""
391
+ self.__dict__.pop("_conv_plan", None)
392
+ self.__dict__.pop("_quant_plan", None)
393
+
394
+ def __getstate__(self):
395
+ state = super().__getstate__().copy()
396
+ state.pop("_conv_plan", None)
397
+ state.pop("_quant_plan", None)
398
+ return state
399
+
400
+ def transform(self, X):
401
+ x = ref._as_3d(X)
402
+ if not self.fast_profile and not getattr(self,"response_stride_",None) and not x.flags.c_contiguous:
403
+ return super().transform(X)
404
+ if self.zscore:
405
+ x = _normalize(x)
406
+ return self.transform_normalized(x)
407
+
408
+ def transform_normalized(self, x):
409
+ """Apply the bank to an already prepared float64 time-major window tensor."""
410
+ n, t, _ = x.shape
411
+ f = np.empty((n, len(self.kernels_), 4))
412
+ stride = getattr(self,"response_stride_",4 if self.fast_profile else 1)
413
+ if (_cis_conv is not None and _kernels is not None and
414
+ self.kernels_ and n > 0 and stride <= t <= 8192):
415
+ plan = self.__dict__.get("_conv_plan")
416
+ if plan is None or plan[0] is not self.kernels_:
417
+ plan = (self.kernels_, _pack_kernels(self.kernels_))
418
+ self._conv_plan = plan
419
+ layout, weights, biases, max_offset, multi = plan[1]
420
+ if t > max_offset and (stride>1 or self.fast_profile or not multi or
421
+ (t-max_offset > 1 and _cis_conv.has_blas())):
422
+ # A single N,C,T copy is shared by all kernels. Projection and
423
+ # pooling scratch storage is per window, independent of bank size.
424
+ data = np.ascontiguousarray(x.transpose(0,2,1), dtype=np.float64)
425
+ args = (data, layout, weights, biases, stride, self.fast_profile or stride>1,
426
+ stride==1 and not self.fast_profile and _verified_conv_blas(), f,
427
+ bool(getattr(self, "response_stride_", None)))
428
+ if getattr(self, 'projection_precision_', None) == 'float32':
429
+ _cis_conv.transform(*args, True)
430
+ else:
431
+ _cis_conv.transform(*args)
432
+ return _clean(f.reshape(n,-1))
433
+ groups = defaultdict(list)
434
+ for k, spec in enumerate(self.kernels_):
435
+ groups[tuple(spec['channels']), spec['length'], spec['dilation']].append((k, spec))
436
+ for (channels, length, dilation), group in groups.items():
437
+ valid = t - (length - 1)*dilation
438
+ if valid <= 0:
439
+ raise ValueError('Random convolution kernel exceeds window length')
440
+ frames = (valid + stride - 1)//stride
441
+ native = _kernels is not None and (stride>1 or self.fast_profile or len(channels) == 1)
442
+ if native:
443
+ xch = np.ascontiguousarray(x[:, :, channels].transpose(2, 0, 1))
444
+ else:
445
+ # Same channel-major advanced-index layout as upstream matmul.
446
+ dat = x[:, :, channels]
447
+ # Bound simultaneous kernel response storage to about 8 MiB.
448
+ block_size = max(1, (1 << 20)//max(n*frames, 1))
449
+ for start in range(0, len(group), block_size):
450
+ parts = group[start:start+block_size]
451
+ idx = [k for k, _ in parts]
452
+ weights = np.stack([spec['weights'] for _, spec in parts])
453
+ biases = np.asarray([spec['bias'] for _, spec in parts])
454
+ resp = np.zeros((len(parts), n, frames))
455
+ if native:
456
+ _kernels.project(xch, weights, biases, dilation, stride, resp)
457
+ else:
458
+ for i in range(length):
459
+ sub = dat[None, :, i*dilation:i*dilation+valid:stride, :]
460
+ resp += (sub @ weights[:, None, i, :, None])[..., 0]
461
+ resp += biases[:, None, None]
462
+ _pool_response(f, idx, resp)
463
+ return np.nan_to_num(f.reshape(n, -1), copy=False, nan=0., posinf=0., neginf=0.)
464
+
465
+
466
+ @dataclass
467
+ class IntervalDistributionSketch(ref.IntervalDistributionSketch):
468
+ fast_profile: bool = False
469
+
470
+ def transform(self, X):
471
+ x = ref._as_3d(X)
472
+ if not self.fast_profile and not x.flags.c_contiguous:
473
+ return super().transform(X)
474
+ if self.zscore:
475
+ x = _normalize(x)
476
+ return self.transform_normalized(x)
477
+
478
+ def transform_normalized(self, x, *, native_plan=None):
479
+ n, time, channels = x.shape
480
+ width = channels * (4 if self.fast_profile else (5 + len(self.quantiles)))
481
+ eligible=(_cis_intervals is not None and x.flags.c_contiguous and 2 <= channels <= 256
482
+ and time <= 2048 and 0 < len(self.quantiles) <= 16
483
+ and all(0 <= q <= 1 for q in self.quantiles)
484
+ and tuple(sorted(self.quantiles)) == tuple(self.quantiles))
485
+ if eligible:
486
+ qs = np.asarray((0.5,) if self.fast_profile else self.quantiles, dtype=np.float64)
487
+ if (native_plan is None and n>=8 and time<=255 and 4<=len(self.intervals_)<=65536
488
+ and hasattr(_cis_intervals,"levels")):
489
+ bounds=np.ascontiguousarray(self.intervals_,np.int64)
490
+ out=np.empty((n,len(bounds),width))
491
+ if _cis_intervals.levels(x,bounds,qs,out,int(self.fast_profile)):
492
+ return out.reshape(n,-1)
493
+ if eligible and np.isfinite(x).all():
494
+ def rank():
495
+ if time<=256 and n>=8 and hasattr(_cis_intervals,"rank_short"):
496
+ ordered=np.empty((n,channels,time),dtype=np.int64)
497
+ if _cis_intervals.rank_short(x,ordered):
498
+ return ordered
499
+ return np.argsort(x.swapaxes(1,2), axis=-1).astype(np.int64,copy=False)
500
+ def evaluate(intervals, order):
501
+ bounds = np.asarray(intervals, dtype=np.int64).reshape(-1,2)
502
+ out = np.empty((n,len(bounds),width))
503
+ _cis_intervals.intervals(x,bounds,order,qs,out,int(self.fast_profile))
504
+ return _clean(out.reshape(n,-1))
505
+ if native_plan is not None:
506
+ return native_plan(self, rank, evaluate)
507
+ return evaluate(self.intervals_, rank())
508
+ out = np.empty((n, len(self.intervals_), width))
509
+ intervals = defaultdict(list)
510
+ for idx, (lo, hi) in enumerate(self.intervals_):
511
+ intervals[hi-lo].append((idx, lo, hi))
512
+ for length, group in intervals.items():
513
+ group_size = max(1, (1 << 18) // max(1, n*length*channels))
514
+ for start in range(0, len(group), group_size):
515
+ parts = group[start:start + group_size]
516
+ if len(parts) == 1:
517
+ v = x[:, parts[0][1]:parts[0][2]]
518
+ else:
519
+ v = np.concatenate([x[:, lo:hi] for _, lo, hi in parts], axis=0)
520
+ stats = [v.mean(axis=1), v.std(axis=1), np.ptp(v,axis=1)]
521
+ if self.fast_profile:
522
+ stats.append(np.median(v, axis=1))
523
+ else:
524
+ stats.extend([(v*v).mean(axis=1), ref._slope(v), ref._quantile_block(v, self.quantiles)])
525
+ ix = [idx for idx, _, _ in parts]
526
+ out[:,ix] = np.concatenate(stats, axis=1).reshape(len(ix), n, width).swapaxes(0,1)
527
+ return np.nan_to_num(out.reshape(n, -1), copy=False, nan=0., posinf=0., neginf=0.)
528
+
529
+
530
+ @dataclass
531
+ class InvariantPhysicsFeatureExtractor(ref.InvariantPhysicsFeatureExtractor):
532
+ fast_profile: bool = False
533
+
534
+ def fit(self, X, y=None):
535
+ self.interval_ = IntervalDistributionSketch(n_random_intervals=self.random_intervals,
536
+ random_state=self.random_state, zscore=self.zscore, fast_profile=self.fast_profile).fit(X, y)
537
+ return self
538
+
539
+ def transform(self, X):
540
+ x = ref._as_3d(X)
541
+ if not self.fast_profile and not x.flags.c_contiguous:
542
+ return super().transform(X)
543
+ x = _normalize(x) if self.zscore else np.nan_to_num(x)
544
+ blocks = []
545
+ common = dict(zscore=False, include_magnitude=False, include_jerk=False,
546
+ segment_sizes=self.segment_sizes, fast_profile=self.fast_profile)
547
+ if self.include_axis_features:
548
+ axis = WearableFeatureExtractor(**common, include_spectral=False,
549
+ include_autocorr=False, include_symbolic=False)
550
+ blocks.append(axis.transform(x))
551
+ base = x[:, :, :3]
552
+ mag = np.linalg.norm(base, axis=2, keepdims=True)
553
+ jerk = np.diff(base, axis=1, prepend=base[:, :1])
554
+ jmag = np.linalg.norm(jerk, axis=2, keepdims=True)
555
+ inv = WearableFeatureExtractor(**common, include_cross_channel=False, symbolic_bins=self.symbolic_bins)
556
+ blocks.append(inv.transform(np.concatenate([mag, jmag], axis=2)))
557
+ centered = base - base.mean(axis=1,keepdims=True)
558
+ gram = centered.swapaxes(1,2) @ centered / max(1, x.shape[1]-1)
559
+ vals = np.maximum(np.linalg.eigvalsh(gram)[:, ::-1], 0)
560
+ ratios = vals / (vals.sum(axis=1, keepdims=True)+1e-12)
561
+ blocks.extend([vals, ratios, np.zeros((len(x), max(6-2*base.shape[2], 0)))])
562
+ blocks.append((jmag*jmag).mean(axis=1)/((mag*mag).mean(axis=1)+1e-12))
563
+ if self.interval_ is not None:
564
+ # The upstream interval branch z-scores its already normalized input.
565
+ blocks.append(self.interval_.transform(x))
566
+ return _clean(np.concatenate(blocks, axis=1))
567
+
568
+
569
+ @dataclass
570
+ class RandomShapeletSketch(ref.RandomShapeletSketch):
571
+ fast_profile: bool = False
572
+
573
+ def transform(self, X):
574
+ x = ref._as_3d(X)
575
+ if not self.fast_profile and not x.flags.c_contiguous:
576
+ return super().transform(X)
577
+ if self.zscore:
578
+ x = _normalize(x)
579
+ return self.transform_normalized(x)
580
+
581
+ def transform_normalized(self, x, *, window_tile=None):
582
+ n, time, _ = x.shape
583
+ groups = defaultdict(list)
584
+ for k, s in enumerate(self.shapelets_):
585
+ groups[s['channel'], s['dilation'], len(s['values'])].append((k, s['values']))
586
+ f = np.empty((n, len(self.shapelets_)*2))
587
+ stride = 4 if self.fast_profile else 1
588
+ channels = {}
589
+ for (c, d, length), group in groups.items():
590
+ span = (length-1)*d+1
591
+ valid = time-span+1
592
+ if valid < 1:
593
+ raise ValueError('Fitted shapelet exceeds input window length')
594
+ if _family_kernels is not None and length <= 256 and self.zscore:
595
+ if c not in channels:
596
+ channels[c] = np.require(x[:, :, c], dtype=np.float64, requirements=("C", "A"))
597
+ weights = np.stack([v for _, v in group])
598
+ pooled = np.empty((n, len(group), 2))
599
+ _family_kernels.shapelets(channels[c], weights, pooled, int(d), stride)
600
+ for j, (k, _) in enumerate(group):
601
+ f[:, 2*k:2*k+2] = pooled[:, j]
602
+ continue
603
+ grid = sliding_window_view(x[:, :, c], span, axis=1)[:, ::stride, ::d]
604
+ # Limit materialized normalized windows; sliding_window_view itself
605
+ # does not copy any data. Each row can be discarded after pooling.
606
+ chunk = max(1, (1 << 18)//(grid.shape[1]*length))
607
+ for lo in range(0, n, chunk):
608
+ def normalize():
609
+ part = np.ascontiguousarray(grid[lo:lo+chunk])
610
+ block = part - part.mean(axis=2, keepdims=True)
611
+ sd = block.std(axis=2, keepdims=True)
612
+ return block / np.where(sd < 1e-6, 1., sd)
613
+ block = (normalize() if window_tile is None else
614
+ window_tile((c,d,length,stride,lo,chunk), normalize))
615
+ for k, values in group:
616
+ delta = block - values
617
+ np.square(delta, out=delta)
618
+ dist = np.sqrt(delta.mean(axis=2))
619
+ # NaN distances are skipped by upstream's strict '<' scan.
620
+ if not self.zscore:
621
+ dist[np.isnan(dist)] = np.inf
622
+ positions = dist.argmin(axis=1)
623
+ rows = slice(lo, lo+len(block))
624
+ f[rows, 2*k] = dist[np.arange(len(block)), positions]
625
+ f[rows, 2*k+1] = positions*stride / max(valid-1, 1)
626
+ return np.nan_to_num(f, copy=False, nan=0., posinf=0., neginf=0.)
src/wisp_fast/features_kernels.c ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #define PY_SSIZE_T_CLEAN
2
+ #include <Python.h>
3
+ #include <string.h>
4
+
5
+ static int get_doubles(PyObject *obj, Py_buffer *view, int ndim, int writable) {
6
+ int flags = PyBUF_FORMAT | PyBUF_C_CONTIGUOUS | (writable ? PyBUF_WRITABLE : 0);
7
+ if (PyObject_GetBuffer(obj, view, flags) < 0)
8
+ return -1;
9
+ if (view->ndim != ndim || view->itemsize != sizeof(double) ||
10
+ strcmp(view->format, "d") != 0 ||
11
+ ((uintptr_t)view->buf % _Alignof(double)) != 0) {
12
+ PyErr_SetString(PyExc_ValueError, "expected aligned, C-contiguous native doubles with the specified rank");
13
+ return -1;
14
+ }
15
+ return 0;
16
+ }
17
+
18
+ /* Project kernels of one geometry. Time-contiguous buffers permit ordinary
19
+ compiler vectorization of the inner loop. Pooling stays in NumPy to retain
20
+ its exact summation policy. resp is an output (it is cleared below). */
21
+ static PyObject *project(PyObject *self, PyObject *args) {
22
+ PyObject *x_obj, *w_obj, *b_obj, *out_obj;
23
+ Py_ssize_t dilation, stride;
24
+ Py_buffer x = {0}, w = {0}, b = {0}, out = {0};
25
+ if (!PyArg_ParseTuple(args, "OOOnnO", &x_obj, &w_obj, &b_obj, &dilation, &stride, &out_obj))
26
+ return NULL;
27
+ if (get_doubles(x_obj, &x, 3, 0) < 0 || get_doubles(w_obj, &w, 3, 0) < 0 ||
28
+ get_doubles(b_obj, &b, 1, 0) < 0 || get_doubles(out_obj, &out, 3, 1) < 0)
29
+ goto done;
30
+
31
+ const Py_ssize_t c = x.shape[0], n = x.shape[1], t = x.shape[2];
32
+ const Py_ssize_t k = w.shape[0], length = w.shape[1];
33
+ if (c < 1 || c > 3 || t < 1 || length < 1 || dilation < 1 || stride < 1 ||
34
+ length-1 > (t-1)/dilation || c != w.shape[2] || b.shape[0] != k ||
35
+ out.shape[0] != k || out.shape[1] != n) {
36
+ PyErr_SetString(PyExc_ValueError, "incompatible projection dimensions or dilation/stride");
37
+ goto done;
38
+ }
39
+ const Py_ssize_t frames = 1 + (t-1-(length-1)*dilation)/stride;
40
+ if (out.shape[2] != frames) {
41
+ PyErr_SetString(PyExc_ValueError, "incompatible output frame count");
42
+ goto done;
43
+ }
44
+ const double *signals = (const double *)x.buf;
45
+ const double *weights = (const double *)w.buf;
46
+ const double *bias = (const double *)b.buf;
47
+ double *result = (double *)out.buf;
48
+ Py_BEGIN_ALLOW_THREADS
49
+ memset(result, 0, (size_t)out.len);
50
+ for (Py_ssize_t kernel = 0; kernel < k; ++kernel) {
51
+ for (Py_ssize_t sample = 0; sample < n; ++sample) {
52
+ double *r = result + (kernel*n + sample)*frames;
53
+ for (Py_ssize_t tap = 0; tap < length; ++tap) {
54
+ const double *v = weights + (kernel*length + tap)*c;
55
+ const double *a = signals + sample*t + tap*dilation;
56
+ if (c == 1) {
57
+ if (stride == 1) {
58
+ for (Py_ssize_t j=0; j<frames; ++j) r[j] += a[j]*v[0];
59
+ } else {
60
+ for (Py_ssize_t j=0; j<frames; ++j) r[j] += a[j*stride]*v[0];
61
+ }
62
+ } else if (c == 2) {
63
+ const double *h = a+n*t;
64
+ for (Py_ssize_t j=0; j<frames; ++j)
65
+ r[j] += a[j*stride]*v[0] + h[j*stride]*v[1];
66
+ } else {
67
+ const double *h = a+n*t, *z = h+n*t;
68
+ for (Py_ssize_t j=0; j<frames; ++j)
69
+ r[j] += a[j*stride]*v[0] + h[j*stride]*v[1] + z[j*stride]*v[2];
70
+ }
71
+ }
72
+ for (Py_ssize_t j=0; j<frames; ++j) r[j] += bias[kernel];
73
+ }
74
+ }
75
+ Py_END_ALLOW_THREADS
76
+ done:
77
+ if (x.obj != NULL) PyBuffer_Release(&x);
78
+ if (w.obj != NULL) PyBuffer_Release(&w);
79
+ if (b.obj != NULL) PyBuffer_Release(&b);
80
+ if (out.obj != NULL) PyBuffer_Release(&out);
81
+ if (PyErr_Occurred()) return NULL;
82
+ Py_RETURN_NONE;
83
+ }
84
+
85
+ static PyMethodDef methods[] = {{"project", project, METH_VARARGS,
86
+ "Project a group of kernels over aligned double buffers."}, {NULL,NULL,0,NULL}};
87
+ static struct PyModuleDef module = {PyModuleDef_HEAD_INIT, "_kernels", NULL, -1, methods};
88
+ PyMODINIT_FUNC PyInit__kernels(void) { return PyModule_Create(&module); }
src/wisp_fast/forest.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Optional zero-copy C traversal over sklearn's fitted single-output forests."""
2
+ import ctypes
3
+ from functools import lru_cache
4
+ from .native import lookup
5
+
6
+ import numpy as np
7
+
8
+
9
+ @lru_cache(maxsize=1)
10
+ def _native():
11
+ fn = lookup("wisp_forest")
12
+ if fn is None:
13
+ return None
14
+ arr = np.ctypeslib.ndpointer
15
+ size = ctypes.c_ssize_t
16
+ fn.argtypes = [arr(dtype=np.float32, ndim=2, flags="C_CONTIGUOUS"), size, size,
17
+ arr(dtype=np.uintp, ndim=2, flags="C_CONTIGUOUS"), size, size,
18
+ arr(dtype=np.float64, ndim=2, flags="C_CONTIGUOUS")]
19
+ fn.restype = None
20
+ return fn
21
+
22
+
23
+ class ForestPlan:
24
+ """Transient table of eight machine words per tree. The tree objects own the buffers."""
25
+ def __init__(self, head):
26
+ self.trees = [e.tree_ for e in head.estimators_]
27
+ self.table = None
28
+ self._views = []
29
+ if _native() is None or head.n_outputs_ != 1 or len(self.trees) > 32768:
30
+ return
31
+ width = len(head.classes_)
32
+ rows = []
33
+ for t in self.trees:
34
+ fields = [t.children_left, t.children_right, t.feature, t.threshold,
35
+ t.value, getattr(t, "missing_go_to_left", None)]
36
+ left, right, feat, thresh, value, missing = fields
37
+ if (missing is None or missing.dtype != np.uint8 or thresh.dtype != np.float64
38
+ or any(a.dtype != np.intp for a in [left, right, feat])
39
+ or value.dtype != np.float64 or value.shape != (t.node_count, 1, width)
40
+ or value.strides[0] < width * 8 or value.strides[-1] != 8
41
+ or not value.flags.aligned or left.strides[0] <= 0
42
+ or any(a.shape != (t.node_count,) or a.strides != left.strides
43
+ for a in (right, feat, thresh, missing))):
44
+ return
45
+ self._views.append(fields) # keep every buffer alive for the plan's lifetime
46
+ rows.append([a.ctypes.data for a in fields] + [left.strides[0], value.strides[0]])
47
+ self.table = np.asarray(rows, dtype=np.uintp)
48
+
49
+
50
+ def local_proba(head, features, plan):
51
+ # sklearn performs its normal feature-count/dtype and finite input checks.
52
+ X = head._validate_X_predict(features)
53
+ k = len(head.classes_)
54
+ out = np.zeros((len(X), k), dtype=np.float64)
55
+ if plan.table is not None:
56
+ _native()(np.ascontiguousarray(X), len(X), X.shape[1], plan.table, len(plan.trees), k, out)
57
+ else:
58
+ for tree in plan.trees:
59
+ out += tree.predict(X)[:, :k]
60
+ out /= len(plan.trees)
61
+ return out
src/wisp_fast/forest_kernels.c ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <math.h>
2
+ #include <stdint.h>
3
+ #include <string.h>
4
+
5
+ /* Read the dtype/strides verified by forest.py; never assume an sklearn C struct layout.
6
+ The table contains pointers to non-owning array views pinned by the Python plan.
7
+ f32 inputs, f64 comparisons and tree-order f64 sums match sklearn's serial path. */
8
+ static intptr_t read_index(uintptr_t p) {
9
+ intptr_t out;
10
+ memcpy(&out, (const void *)p, sizeof out);
11
+ return out;
12
+ }
13
+ static double read_double(uintptr_t p) {
14
+ double out;
15
+ memcpy(&out, (const void *)p, sizeof out);
16
+ return out;
17
+ }
18
+ #ifdef _WIN32
19
+ __declspec(dllexport)
20
+ #endif
21
+ void wisp_forest(const float *x, intptr_t n, intptr_t d,
22
+ const uintptr_t *specs, intptr_t nt, intptr_t k, double *out) {
23
+ for (intptr_t t = 0; t < nt; ++t) {
24
+ const uintptr_t *tree = specs + 8 * t;
25
+ const uintptr_t stride = tree[6];
26
+ for (intptr_t i = 0; i < n; ++i) {
27
+ intptr_t node = 0, left;
28
+ uintptr_t off = 0;
29
+ while ((left = read_index(tree[0] + off)) != -1) {
30
+ const float val = x[i*d + read_index(tree[2]+off)];
31
+ const int go_left = isnan(val) ? *(const uint8_t *)(tree[5]+off)
32
+ : (double)val <= read_double(tree[3]+off);
33
+ node = go_left ? left : read_index(tree[1]+off);
34
+ off = (uintptr_t)node * stride;
35
+ }
36
+ const double *leaf = (const double *)(tree[4] + (uintptr_t)node * tree[7]);
37
+ for (intptr_t j = 0; j < k; ++j) out[i*k+j] += leaf[j];
38
+ }
39
+ }
40
+ }
src/wisp_fast/interval_levels.h ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Each bin may represent exactly one observed double. Detect collisions and
2
+ * let the original ranking routine take over. In each time-prefix vector,
3
+ * byte b counts samples with value <= bin b (T<=255). */
4
+ #if defined(CAN_FILTER)
5
+ typedef struct {unsigned char lo,hi;double frac;} LevelCut;
6
+ __attribute__((target("avx512bw,avx512f")))
7
+ static int level_cdf(const double *src,int t,int c,const int64_t *bounds,int m,
8
+ const LevelCut *cuts,int nq,int width,int turbo,double *out,int ch){
9
+ double min=src[0],max=src[0],values[64];uint64_t used=0;
10
+ for(int j=0;j<t;++j){double v=src[j*c];if(!isfinite(v)||(v==0. && signbit(v)))return 0;
11
+ if(v<min)min=v;if(v>max)max=v;}
12
+ /* Accepted values also bound every numeric interval moment far below DBL_MAX. */
13
+ if(min < -1.e140 || max > 1.e140)return 0;
14
+ double span=max-min,scale=span?63./span:0.;
15
+ if(!isfinite(scale)||!isfinite(span))return 0;
16
+ unsigned char key[256];
17
+ for(int j=0;j<t;++j){double v=src[j*c];int k=(int)((v-min)*scale+.5);if(k>63)k=63;
18
+ uint64_t bit=UINT64_C(1)<<k;
19
+ if((used&bit) && values[k]!=v)return 0;
20
+ values[k]=v;used|=bit;key[j]=(unsigned char)k;
21
+ }
22
+ static const unsigned char index[64]={
23
+ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
24
+ 32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63};
25
+ __m512i prefix[256],cdf=_mm512_setzero_si512(),one=_mm512_set1_epi8(1),bins=_mm512_loadu_si512(index);
26
+ prefix[0]=cdf;
27
+ for(int j=0;j<t;++j){
28
+ __mmask64 update=_mm512_cmpge_epu8_mask(bins,_mm512_set1_epi8(key[j]));
29
+ cdf=_mm512_mask_add_epi8(cdf,update,cdf,one);prefix[j+1]=cdf;
30
+ }
31
+ for(int j=0;j<m;++j){
32
+ int a=bounds[2*j],b=bounds[2*j+1],n=b-a;
33
+ __m512i f=_mm512_sub_epi8(prefix[b],prefix[a]);
34
+ int low=__builtin_ctzll(_mm512_cmpneq_epi8_mask(f,_mm512_setzero_si512()));
35
+ int high=__builtin_ctzll(_mm512_cmpeq_epi8_mask(f,_mm512_set1_epi8((char)n)));
36
+ double *dst=out+j*width*c;dst[2*c+ch]=values[high]-values[low];
37
+ for(int h=0;h<nq;++h){const LevelCut *s=cuts+j*nq+h;
38
+ int lo=__builtin_ctzll(_mm512_cmpgt_epu8_mask(f,_mm512_set1_epi8((char)s->lo)));
39
+ int hi=__builtin_ctzll(_mm512_cmpgt_epu8_mask(f,_mm512_set1_epi8((char)s->hi)));
40
+ double x=values[lo],y=values[hi],q=s->frac;
41
+ dst[(width-nq)*c+ch*nq+h]=turbo?((n&1)?x:(x+y)*.5):(q>=.5?y-(y-x)*(1.-q):x+(y-x)*q);
42
+ }
43
+ }
44
+ return 1;
45
+ }
46
+ #endif
47
+ static PyObject *interval_levels(PyObject *self,PyObject *args){
48
+ PyObject *obj[4];int turbo;Py_buffer v[4]={{0}};int ok=0;
49
+ if(!PyArg_ParseTuple(args,"OOOOi",&obj[0],&obj[1],&obj[2],&obj[3],&turbo))return NULL;
50
+ if(!view(obj[0],v,3,'d',0)||!view(obj[1],v+1,2,'q',0)||!view(obj[2],v+2,1,'d',0)||
51
+ !view(obj[3],v+3,3,'d',1)||!separate_output(v,3))goto done;
52
+ Py_ssize_t n=v[0].shape[0],t=v[0].shape[1],c=v[0].shape[2],m=v[1].shape[0],nq=v[2].shape[0];
53
+ int width=(turbo?3:5)+(int)nq;const double *q=v[2].buf;const int64_t *bounds=v[1].buf;
54
+ if(n<8||t<1||t>255||c<2||c>256||m<1||m>65536||nq<1||nq>16||v[1].shape[1]!=2||
55
+ v[3].shape[0]!=n||v[3].shape[1]!=m||v[3].shape[2]!=c*width||
56
+ (turbo && (nq!=1||q[0]!=.5))){
57
+ PyErr_SetString(PyExc_ValueError,"unsupported bounded CDF geometry");goto done;
58
+ }
59
+ for(int j=0;j<nq;++j)if(!isfinite(q[j])||q[j]<0||q[j]>1){PyErr_SetString(PyExc_ValueError,"invalid quantile");goto done;}
60
+ for(int j=0;j<m;++j)if(bounds[2*j]<0||bounds[2*j+1]>t||bounds[2*j]>=bounds[2*j+1]){
61
+ PyErr_SetString(PyExc_ValueError,"invalid interval");goto done;}
62
+ #ifdef CAN_FILTER
63
+ if(__builtin_cpu_supports("avx512bw") && CAN_FILTER){
64
+ LevelCut *cuts=PyMem_Malloc(m*nq*sizeof(*cuts));
65
+ double *tile=PyMem_Malloc(8*t*sizeof(double)),*out=v[3].buf;const double *x=v[0].buf;
66
+ if(!cuts || !tile){PyMem_Free(cuts);PyMem_Free(tile);PyErr_NoMemory();goto done;}
67
+ for(int j=0;j<m;++j)for(int h=0;h<nq;++h){int last=(int)(bounds[2*j+1]-bounds[2*j]-1);
68
+ double loc=last*q[h];int k=(int)loc;cuts[j*nq+h]=(LevelCut){k,k<last?k+1:k,loc-k};}
69
+ ok=1;
70
+ Py_BEGIN_ALLOW_THREADS
71
+ for(Py_ssize_t i=0;ok && i<n;++i)for(int h=0;ok&&h<c;++h)
72
+ ok=level_cdf(x+i*t*c+h,(int)t,(int)c,bounds,(int)m,cuts,(int)nq,width,turbo,out+i*m*width*c,h);
73
+ if(ok){
74
+ for(Py_ssize_t i=0;i+8<=n;i+=8)numeric_eight(x+i*t*c,bounds,out+i*m*width*c,tile,t,c,m,width,turbo);
75
+ if(n%8){Py_ssize_t i=n-8;numeric_eight(x+i*t*c,bounds,out+i*m*width*c,tile,t,c,m,width,turbo);}
76
+ }
77
+ Py_END_ALLOW_THREADS
78
+ PyMem_Free(cuts);PyMem_Free(tile);
79
+ }
80
+ #endif
81
+ done:
82
+ for(int j=0;j<4;++j)if(v[j].obj)PyBuffer_Release(v+j);
83
+ if(PyErr_Occurred())return NULL;return PyBool_FromLong(ok);
84
+ }
src/wisp_fast/moments_tiles.h ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Process eight independent windows; each lane follows the scalar/numpy
2
+ reduction order. The short-axis path bounds stack scratch space. */
3
+ #if defined(__x86_64__) && (defined(__GNUC__) || defined(__clang__)) && !defined(_WIN32)
4
+ #include <immintrin.h>
5
+ #define MOM512 __attribute__((target("avx512f")))
6
+ typedef __m512d MomTile;
7
+ static MOM512 inline MomTile gather_rows(const double *x,Py_ssize_t pitch) {
8
+ return _mm512_set_pd(x[7*pitch],x[6*pitch],x[5*pitch],x[4*pitch],x[3*pitch],x[2*pitch],x[pitch],x[0]);
9
+ }
10
+ static MOM512 inline void put_rows(double *out,Py_ssize_t pitch,MomTile value) {
11
+ double lane[8];_mm512_storeu_pd(lane,value);for(int i=0;i<8;++i)out[i*pitch]=lane[i];
12
+ }
13
+ static MOM512 MomTile sum_rows(const MomTile *x,int n,int paired) {
14
+ if(!paired||n<8){
15
+ MomTile s=_mm512_set1_pd(paired?-0.:0.);for(int j=0;j<n;++j)s+=x[j];return s;
16
+ }
17
+ if(n>128){int mid=n/2/8*8;return sum_rows(x,mid,1)+sum_rows(x+mid,n-mid,1);}
18
+ MomTile a=x[0],b=x[1],c=x[2],d=x[3],e=x[4],f=x[5],g=x[6],h=x[7];
19
+ int j=8;
20
+ for(;j+8<=n;j+=8){a+=x[j];b+=x[j+1];c+=x[j+2];d+=x[j+3];e+=x[j+4];f+=x[j+5];g+=x[j+6];h+=x[j+7];}
21
+ MomTile s=((a+b)+(c+d))+((e+f)+(g+h));for(;j<n;++j)s+=x[j];return s;
22
+ }
23
+ static MOM512 void stats_tile8(const double *x,int t,int c,double *out) {
24
+ MomTile v[256],dv[256],sq[256],cub[256],four[256],dsq[256];
25
+ Py_ssize_t pitch=(Py_ssize_t)t*c;int p=11*c;
26
+ for(int ch=0;ch<c;++ch){
27
+ MomTile lo=gather_rows(x+ch,pitch),hi=lo,zero=_mm512_setzero_pd(),cross=zero,slope=zero;
28
+ for(int j=0;j<t;++j){
29
+ v[j]=gather_rows(x+(Py_ssize_t)j*c+ch,pitch);sq[j]=v[j]*v[j];
30
+ dv[j]=zero;if(j)dv[j]=v[j]-v[j-1];
31
+ lo=_mm512_mask_mov_pd(lo,_mm512_cmp_pd_mask(v[j],lo,_CMP_LT_OQ),v[j]);
32
+ hi=_mm512_mask_mov_pd(hi,_mm512_cmp_pd_mask(v[j],hi,_CMP_GT_OQ),v[j]);
33
+ if(j){__mmask8 flip=_mm512_test_epi64_mask(_mm512_xor_si512(_mm512_castpd_si512(v[j]),_mm512_castpd_si512(v[j-1])),_mm512_set1_epi64(INT64_MIN));
34
+ cross=_mm512_mask_add_pd(cross,flip,cross,_mm512_set1_pd(1.));}
35
+ }
36
+ int paired=(c==1);MomTile mu=sum_rows(v,t,paired)/(double)t,dm=sum_rows(dv,t,paired)/(double)t;
37
+ double *dst=out+ch;
38
+ put_rows(dst,p,mu);put_rows(dst+2*c,p,lo);put_rows(dst+3*c,p,hi);
39
+ put_rows(dst+4*c,p,sum_rows(sq,t,paired)/(double)t);put_rows(dst+5*c,p,dm);
40
+ for(int j=0;j<t;++j){
41
+ MomTile m=v[j]-mu,d=dv[j]-dm;sq[j]=m*m;dsq[j]=d*d;
42
+ cub[j]=sq[j]*m;four[j]=sq[j]*sq[j];slope+=((double)j-(t-1)*.5)*m;
43
+ }
44
+ put_rows(dst+c,p,sum_rows(sq,t,paired)/(double)t);
45
+ put_rows(dst+6*c,p,_mm512_sqrt_pd(sum_rows(dsq,t,paired)/(double)t));
46
+ if(t>1){cross/=(double)(t-1);slope/=((double)t*((double)t*t-1)/12.);}
47
+ else{cross=zero;slope=zero;}
48
+ put_rows(dst+7*c,p,cross);put_rows(dst+8*c,p,slope);
49
+ put_rows(dst+9*c,p,sum_rows(cub,t,paired)/(double)t);
50
+ put_rows(dst+10*c,p,sum_rows(four,t,paired)/(double)t);
51
+ }
52
+ }
53
+ static MOM512 void seg_tile8(const double *x,int t,int c,int pieces,int fields,double *out) {
54
+ int len=(t+pieces-1)/pieces;Py_ssize_t pitch=(Py_ssize_t)t*c,p=fields*pieces*c;
55
+ MomTile v[256],sq[256];
56
+ for(int s=0;s<pieces;++s)for(int ch=0;ch<c;++ch){
57
+ for(int j=0;j<len;++j){int q=s*len+j;v[j]=gather_rows(x+(Py_ssize_t)(q<t?q:t-1)*c+ch,pitch);}
58
+ MomTile mu=sum_rows(v,len,c==1)/(double)len,lo=v[0],hi=v[0],slope=_mm512_setzero_pd();
59
+ for(int j=0;j<len;++j){
60
+ MomTile z=v[j]-mu;sq[j]=z*z;
61
+ lo=_mm512_mask_mov_pd(lo,_mm512_cmp_pd_mask(v[j],lo,_CMP_LT_OQ),v[j]);
62
+ hi=_mm512_mask_mov_pd(hi,_mm512_cmp_pd_mask(v[j],hi,_CMP_GT_OQ),v[j]);
63
+ if(fields==4)slope+=((double)j-(len-1)*.5)*z;
64
+ }
65
+ Py_ssize_t w=(Py_ssize_t)pieces*c;
66
+ double *dst=out+(Py_ssize_t)s*c+ch;
67
+ put_rows(dst,p,mu);put_rows(dst+w,p,_mm512_sqrt_pd(sum_rows(sq,len,c==1)/(double)len));
68
+ put_rows(dst+2*w,p,hi-lo);
69
+ if(fields==4){if(len>1)slope/=((double)len*((double)len*len-1)/12.);else slope=_mm512_setzero_pd();put_rows(dst+3*w,p,slope);}
70
+ }
71
+ }
72
+ #endif
src/wisp_fast/native.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Look up optional CPU symbols from the installed extension or standalone C build."""
2
+ import ctypes
3
+ from importlib.util import find_spec
4
+ from pathlib import Path
5
+
6
+
7
+ def lookup(symbol):
8
+ ext = find_spec("wisp_fast._kernels")
9
+ paths = [ext.origin] if ext is not None and ext.origin else []
10
+ paths.append(Path(__file__).with_name("_wisp_viterbi.so"))
11
+ for path in paths:
12
+ try:
13
+ return getattr(ctypes.CDLL(str(path)), symbol)
14
+ except (OSError, AttributeError):
15
+ continue
16
+ return None
src/wisp_fast/parallel.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Threaded row-local work; sequence decoding is performed once on complete emissions."""
2
+ from concurrent.futures import ThreadPoolExecutor
3
+ from contextvars import copy_context
4
+
5
+ import numpy as np
6
+ from threadpoolctl import threadpool_limits
7
+ from wisp.cpu.probability import labels_from_proba
8
+ from wisp_release.checkpoints import require_sequence_metadata
9
+
10
+
11
+ def parallel_rows(fn, X, workers):
12
+ if not isinstance(workers,(int,np.integer)) or workers < 1:
13
+ raise ValueError("workers must be a positive integer")
14
+ workers=min(workers,max(1,len(X)//32))
15
+ if workers==1:
16
+ return fn(X)
17
+ bounds=np.linspace(0,len(X),workers+1,dtype=np.intp)
18
+ with threadpool_limits(limits=1, user_api="blas"), ThreadPoolExecutor(max_workers=workers) as pool:
19
+ jobs=[pool.submit(copy_context().run,fn,X[a:b]) for a,b in zip(bounds[:-1],bounds[1:])]
20
+ first=jobs[0].result()
21
+ out=np.empty((len(X),*first.shape[1:]),dtype=first.dtype)
22
+ out[:bounds[1]]=first
23
+ for a,b,job in zip(bounds[1:-1],bounds[2:],jobs[1:]):
24
+ out[a:b]=job.result()
25
+ return out
26
+
27
+
28
+ def predict_parallel(model,X,*,groups=None,time_index=None,workers=4):
29
+ if getattr(model,"feature_workers",1)>1:
30
+ raise ValueError("Use either model feature workers or outer prediction workers")
31
+ groups,time_index=require_sequence_metadata(model,groups,time_index,n=len(X))
32
+ P=parallel_rows(model.predict_proba,np.asarray(X),workers)
33
+ if model.smoother_ is None:
34
+ return labels_from_proba(P,model.classes_)
35
+ return model.smoother_.predict_from_proba(P,groups=groups,time_index=time_index)
src/wisp_fast/ridge_fast.py ADDED
@@ -0,0 +1,172 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Dense weighted leave-one-out Ridge classification for WISP RC/CIS.
2
+
3
+ Implements the mean squared, binary-indicator LOO objective of
4
+ RidgeClassifierCV(cv=None, scoring=None, fit_intercept=True). Uses the primal
5
+ covariance for tall matrices and a centered sample Gram matrix otherwise.
6
+ Spectral operations reorder floating-point arithmetic relative to sklearn.
7
+ """
8
+ import numpy as np
9
+ from scipy.linalg import eigh
10
+ from scipy.linalg.blas import dsyrk
11
+ from sklearn.base import BaseEstimator, ClassifierMixin
12
+ from sklearn.linear_model import RidgeClassifier, RidgeClassifierCV
13
+ from sklearn.preprocessing import LabelBinarizer
14
+ from sklearn.utils.validation import check_X_y, check_array, check_is_fitted
15
+
16
+
17
+ class SpectralRidgeClassifierCV(ClassifierMixin, BaseEstimator):
18
+ def __init__(self, *, alphas=(0.1, 1., 10.)):
19
+ self.alphas = alphas
20
+
21
+ def fit(self, X, y, sample_weight=None):
22
+ X, y = check_X_y(X, y, dtype=np.float64, ensure_min_samples=2)
23
+ alpha = np.atleast_1d(np.asarray(self.alphas, dtype=np.float64))
24
+ if alpha.ndim != 1 or not len(alpha) or np.any(~np.isfinite(alpha)) or np.any(alpha <= 0):
25
+ raise ValueError("alphas must be finite, positive and one-dimensional")
26
+ n, d = X.shape
27
+ sw = np.ones(n) if sample_weight is None else np.asarray(sample_weight, dtype=np.float64)
28
+ if sw.shape != (n,) or not np.isfinite(sw).all() or np.any(sw < 0) or sw.sum() <= 0:
29
+ raise ValueError("sample_weight must be nonnegative, finite, positive in total and length N")
30
+ self.n_features_in_ = d
31
+ encoder = LabelBinarizer(neg_label=-1, pos_label=1)
32
+ targets = encoder.fit_transform(y).astype(np.float64)
33
+ self.classes_ = encoder.classes_
34
+ def fallback(reason):
35
+ # Exact rounding and first-alpha tie policy of the installed reference.
36
+ model = RidgeClassifierCV(alphas=alpha).fit(X, y, sample_weight=sw)
37
+ self.coef_, self.intercept_ = model.coef_, model.intercept_
38
+ self.alpha_, self.best_score_ = model.alpha_, model.best_score_
39
+ self.solver_used_ = "sklearn_" + reason
40
+ self.loo_scores_ = []
41
+ return self
42
+ if len(self.classes_) < 2 or np.any(sw == 0):
43
+ return fallback("degenerate")
44
+ root = np.sqrt(sw)
45
+ total = sw.sum()
46
+ offset = np.average(X, axis=0, weights=sw)
47
+ bias = np.average(targets, axis=0, weights=sw)
48
+ A = np.empty((n, d), dtype=np.float64, order="F")
49
+ np.subtract(X, offset, out=A)
50
+ A *= root[:, None]
51
+ B = (targets-bias)*root[:, None]
52
+
53
+ # For nearly square designs the sample system avoids multiplying a
54
+ # second large eigenvector matrix into every feature row.
55
+ primal = 2*d < n
56
+ if not primal:
57
+ # Orthogonally remove the intercept before building the Gram matrix.
58
+ # H = I - 2uu^T maps sqrt(weight) to -||sqrt(weight)|| e0.
59
+ # Only rows 1..N-1 of H A and H B are needed for the solve.
60
+ u = root.copy()
61
+ u[0] += np.sqrt(total)
62
+ u /= np.linalg.norm(u)
63
+ A[1:] -= (2 * u[1:, None]) * (u @ A)
64
+ A = np.asfortranarray(A[1:])
65
+ B = B[1:] - (2 * u[1:, None]) * (u @ B)
66
+ gram = dsyrk(1., A, trans=int(primal), lower=1)
67
+ try:
68
+ vals, vec = eigh(gram, lower=True, overwrite_a=True, check_finite=False, driver="evd")
69
+ except np.linalg.LinAlgError:
70
+ return fallback("eigensolver")
71
+ del gram
72
+ if not primal:
73
+ reflect_row = (2*u[1:]) @ vec
74
+ if np.min(vals) < -1e-4 * np.min(alpha):
75
+ return fallback("conditioning")
76
+ weights = 1. / (np.maximum(vals, 0.)[:, None] + alpha)
77
+ rhs = vec.T @ ((A.T @ B) if primal else B)
78
+ projections = (weights[:, :, None] * rhs[:, None, :]).reshape(len(vals), -1)
79
+ loss = np.zeros(len(alpha), dtype=float)
80
+ for lo in range(0, n, 192):
81
+ sl = slice(lo, min(n, lo+192))
82
+ if primal:
83
+ Z = A[sl] @ vec
84
+ else:
85
+ # Selected rows of H [0; Q], expressed in the original sample order.
86
+ Z = -u[sl, None] * reflect_row
87
+ if lo == 0:
88
+ Z[1:] += vec[:len(Z)-1]
89
+ else:
90
+ Z += vec[lo-1:sl.stop-1]
91
+ diagonal = (Z*Z) @ weights
92
+ numerator = (Z @ projections).reshape(len(Z), len(alpha), B.shape[1])
93
+ if primal:
94
+ diagonal = 1 - sw[sl, None]/total - diagonal
95
+ numerator = B[sl, None, :] - numerator
96
+ if not np.isfinite(diagonal).all() or np.any(diagonal <= np.finfo(float).eps*32):
97
+ return fallback("leverage")
98
+ relative = numerator / diagonal[:, :, None]
99
+ loss += np.sum(relative*relative, axis=(0, 2))
100
+ loss /= (n * targets.shape[1])
101
+ if not np.isfinite(loss).all():
102
+ return fallback("nonfinite")
103
+ winner = int(np.argmin(loss))
104
+ if len(alpha) > 1 and np.partition(loss, 1)[1]-loss[winner] <= 1e-10*(1.+loss[winner]):
105
+ return fallback("score_tie")
106
+ scaled = weights[:, winner, None] * rhs
107
+ coef = vec @ scaled
108
+ if not primal:
109
+ coef = A.T @ coef
110
+ self.coef_ = coef.T.copy()
111
+ self.intercept_ = bias - offset @ coef
112
+ self.alpha_, self.best_score_ = float(alpha[winner]), float(-loss[winner])
113
+ self.loo_scores_ = -loss
114
+ self.solver_used_ = "primal_eigh" if primal else "dual_eigh"
115
+ return self
116
+
117
+ def decision_function(self, X):
118
+ check_is_fitted(self, "coef_")
119
+ X = check_array(X, dtype=np.float64)
120
+ if X.shape[1] != self.n_features_in_:
121
+ raise ValueError("Wrong number of input features")
122
+ score = X @ self.coef_.T + self.intercept_
123
+ return score.ravel() if score.ndim == 1 or score.shape[1] == 1 else score
124
+
125
+ def predict(self, X):
126
+ dec = self.decision_function(X)
127
+ if len(self.classes_) == 1:
128
+ return np.full(dec.shape[0], self.classes_[0])
129
+ return self.classes_[(dec > 0).astype(int) if dec.ndim == 1 else dec.argmax(axis=1)]
130
+
131
+
132
+ class ScreenedRidgeClassifierCV(SpectralRidgeClassifierCV):
133
+ """Estimate the alpha on a training subsample, then refit on every row.
134
+
135
+ Inclusion weights for each class preserve its total training mass in the
136
+ screening step. Alpha selection is approximate when screening_rows < N.
137
+ """
138
+ def __init__(self, *, alphas=(0.1, 1., 10.), screening_rows=512, random_state=42):
139
+ super().__init__(alphas=alphas)
140
+ self.screening_rows = screening_rows
141
+ self.random_state = random_state
142
+
143
+ def fit(self, X, y, sample_weight=None):
144
+ if not isinstance(self.screening_rows, (int, np.integer)) or self.screening_rows < 2:
145
+ raise ValueError("screening_rows must be an integer >=2")
146
+ X, y = check_X_y(X, y, dtype=np.float64, ensure_min_samples=2)
147
+ if len(y) <= self.screening_rows:
148
+ self.screening_rows_ = len(y)
149
+ return super().fit(X, y, sample_weight=sample_weight)
150
+ from .engine import _training_rows
151
+ sw = np.ones(len(y)) if sample_weight is None else np.asarray(sample_weight, dtype=np.float64)
152
+ if sw.shape != (len(y),) or not np.isfinite(sw).all() or np.any(sw < 0) or sw.sum() <= 0:
153
+ raise ValueError("Invalid sample weights")
154
+ ids = _training_rows(y, self.screening_rows, self.random_state)
155
+ _, encoded = np.unique(y, return_inverse=True)
156
+ mass = np.bincount(encoded, weights=sw)
157
+ submass = np.bincount(encoded[ids], weights=sw[ids], minlength=len(mass))
158
+ if np.any(submass <= 0):
159
+ self.screening_rows_ = len(y)
160
+ return super().fit(X, y, sample_weight=sw)
161
+ weights = sw[ids] * (mass/submass)[encoded[ids]]
162
+ screen = SpectralRidgeClassifierCV(alphas=self.alphas).fit(X[ids], y[ids], sample_weight=weights)
163
+ self.alpha_, self.best_score_, self.loo_scores_ = screen.alpha_, screen.best_score_, screen.loo_scores_
164
+ self.screening_rows_ = len(ids)
165
+ self.n_features_in_ = X.shape[1]
166
+ return self._refit_at_alpha(X, y, sw)
167
+
168
+ def _refit_at_alpha(self, X, y, sw):
169
+ model = RidgeClassifier(alpha=self.alpha_, solver="cholesky").fit(X, y, sample_weight=sw)
170
+ self.coef_, self.intercept_, self.classes_ = model.coef_, model.intercept_, model.classes_
171
+ self.solver_used_ = "screened_loo_full_cholesky"
172
+ return self
src/wisp_fast/search.py ADDED
@@ -0,0 +1,298 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """WISP-Random / WISP-Evolution search with split-local feature sharing.
2
+
3
+ The proposal grammar, cascade, stress views and utility are those of
4
+ ``wisp.search.controller``; candidates are fitted with the accelerated engine.
5
+ """
6
+ from concurrent.futures import ThreadPoolExecutor
7
+ from contextvars import copy_context
8
+ from dataclasses import replace
9
+ from functools import partial
10
+ import json
11
+ import pickle
12
+ from pathlib import Path
13
+ import time
14
+
15
+ import numpy as np
16
+ from wisp.core.data import concatenate_har
17
+ from wisp.core.metrics import classification_report_dict
18
+ from wisp.cpu.algorithms import estimate_pickle_size_mb
19
+ from wisp.search.cascade import STRESS_TRANSFORMS, evaluate_candidate_cascade
20
+ from wisp.search.controller import WISPSearchController
21
+ from wisp.search.grammar import CandidateSpec
22
+ from wisp.search.profiles import get_profile
23
+ from .cache import _SEARCH
24
+ from .decoder import check_sequence
25
+ from .engine import RIDGE_SOLVERS, build_candidate
26
+ from .selection import check_splits, fit_on
27
+ from .search_cache import SearchFeatures
28
+ from .search_metrics import search_report,SearchConfusion
29
+
30
+
31
+ def make_candidate(spec_dict, *, mode="exact", budget=None, ridge_solver="original", sequence="subject",
32
+ calibration=None):
33
+ """Accelerated estimator for one search candidate.
34
+
35
+ ``calibration`` (a validation HARData) is used only with ``sequence="recording"``:
36
+ right after fitting, the confidence scaling is chosen on those windows.
37
+ """
38
+ if ridge_solver not in RIDGE_SOLVERS or (mode!="exact" and ridge_solver!="original"):
39
+ raise ValueError("The screened Ridge solver requires exact full-size features")
40
+ model=build_candidate(spec_dict,mode=mode,budget=budget,ridge_solver=ridge_solver,sequence=sequence)
41
+ if sequence=="recording" and calibration is not None:
42
+ model.calibration_data=(calibration.X,calibration.y)
43
+ return model
44
+
45
+
46
+ class _AcceleratedSearch(WISPSearchController):
47
+ def _evaluate_many(self, specs):
48
+ # Random proposals are score-independent. Evolution may prune only after
49
+ # generating its terminal batch; every breeding decision is then complete.
50
+ terminal = self.rows_seen+len(specs) == self.profile.max_evaluations
51
+ eligible = self.profile.search_strategy == "random" or terminal
52
+ self.batch_bound = self.incumbent if self.stress_policy=="certified" and eligible else None
53
+ # Update only between completed waves. Certificates always reference
54
+ # earlier canonical ledger rows, independent of worker completion order.
55
+ if self.stress_policy=="frontier" and eligible:
56
+ rows=[]
57
+ width=max(self._worker_count(),1)
58
+ for start in range(0,len(specs),width):
59
+ self.batch_bound=self.incumbent
60
+ chunk=self._evaluate_batch(specs[start:start+width])
61
+ self._observe(chunk,self.rows_seen+start)
62
+ rows.extend(chunk)
63
+ else:
64
+ rows = self._evaluate_batch(specs)
65
+ self._observe(rows,self.rows_seen)
66
+ self.rows_seen += len(rows)
67
+ return rows
68
+
69
+ def _observe(self,rows,start):
70
+ for index,(status,result) in enumerate(rows,start):
71
+ if status == "ok":
72
+ score = result["predictive_utility"]
73
+ if self.incumbent is None or score > self.incumbent[0]:
74
+ self.incumbent = (score,index)
75
+ elif status == "pruned":
76
+ self.pruned_count += 1
77
+ self.skipped_views += result["prune_certificate"]["skipped_views"]
78
+
79
+ def _stress_gate(self, metrics, observed):
80
+ # Macro-F1 <= 1. The stress contribution w*s - .05*max(clean-s,0)
81
+ # is monotone on [0,1] for w>=0. No equality pruning, so ties survive.
82
+ score,index = self.batch_bound
83
+ remaining = self.stress_count-len(observed)
84
+ mean_upper = float(np.mean(observed+[1.0]*remaining)) if observed else 1.0
85
+ gap_lower = max(float(metrics["macro_f1"])-mean_upper,0.0)
86
+ upper = float(metrics["score"] + self.robustness_weight*mean_upper - .05*gap_lower)
87
+ if np.isfinite(upper) and upper < score:
88
+ return dict(upper_bound=upper,incumbent_utility=score,
89
+ incumbent_ledger_index=index,stress_f1_upper=mean_upper,
90
+ observed_stress_f1=list(observed),skipped_views=remaining)
91
+ return None
92
+
93
+ def _evaluate_batch(self,specs):
94
+ if self._worker_count() == 1 or len(specs)<2:
95
+ return super()._evaluate_many(specs)
96
+ with ThreadPoolExecutor(max_workers=min(self._worker_count(),len(specs))) as pool:
97
+ jobs=[pool.submit(copy_context().run,self._evaluate_one,s) for s in specs]
98
+ return [job.result() for job in jobs]
99
+
100
+ def _batched_stress(self, model):
101
+ # Each stress view is a distinct sequence panel. Never Viterbi-decode
102
+ # a concatenated panel, even if the same subject IDs appear in each.
103
+ P=model.predict_proba(self.stress_bundle)
104
+ chunks=np.split(P,self.stress_count)
105
+ return {name:model.decode_proba(p,groups=self.valid.subject,time_index=self.valid.time_index)
106
+ for name,p in zip(self.stress_names,chunks)}
107
+
108
+ def _evaluate_one(self, spec):
109
+ token=_SEARCH.set(self.memory if self.memory.limit else None)
110
+ built=[]
111
+
112
+ def factory(spec_dict):
113
+ built.append(self.create_candidate(spec_dict))
114
+ return built[-1]
115
+ try:
116
+ cascade=evaluate_candidate_cascade(
117
+ spec,self.train,self.valid,estimator_factory=factory,
118
+ cost_weight=self.cost_weight,robustness_weight=self.robustness_weight,
119
+ max_stress_transforms=self.stress_count,stress_inputs=self.stress_inputs,
120
+ reporter=self.reporter,stress_gate=self._stress_gate if self.batch_bound is not None else None,
121
+ stress_predictor=self._batched_stress if self.stress_bundle is not None else None)
122
+ result=cascade.to_dict()
123
+ if self.sequence=="recording" and built:
124
+ smoother=getattr(built[-1],"smoother_",None)
125
+ result["sequence"]="recording"
126
+ result["sequence_smoothing"]=bool(smoother is not None and smoother.active)
127
+ result["confidence_scale"]=built[-1].confidence_scale
128
+ if "prune_certificate" in result:
129
+ del result["cascade_score"]
130
+ del result["accepted"] # stress and final eligibility were not evaluated
131
+ return "pruned",result
132
+ result["predictive_utility"]=float(result["cascade_score"])
133
+ return ("ok" if cascade.accepted else "rejected",result)
134
+ except Exception as exc:
135
+ return ("failed",{"predictive_utility":-1e9,"breeding_score":-1e9,
136
+ "error":repr(exc),"candidate":spec.to_dict()})
137
+ finally:
138
+ _SEARCH.reset(token)
139
+
140
+ def _final_refit(self, best, out_dir):
141
+ """Refit the validation winner on train+validation with the accelerated engine."""
142
+ if self.test is None:
143
+ return None
144
+ model = self.create_candidate(best["spec"])
145
+ model.__dict__.pop("calibration_data", None)
146
+ refit = concatenate_har(self.train, self.valid, source="train+validation")
147
+ t0 = time.perf_counter()
148
+ fit_on(model, refit)
149
+ fit_s = time.perf_counter() - t0
150
+ # A train+validation refit reuses the scaling chosen for the train-only candidate.
151
+ model.set_confidence_scale(best["result"].get("confidence_scale"))
152
+ t1 = time.perf_counter()
153
+ pred = model.predict(self.test.X, groups=self.test.subject, time_index=self.test.time_index)
154
+ pred_s = time.perf_counter() - t1
155
+ metrics = classification_report_dict(self.test.y, pred, fit_seconds=fit_s, predict_seconds=pred_s,
156
+ model_size_mb=estimate_pickle_size_mb(model),
157
+ label_names=self.test.label_names)
158
+ np.savez_compressed(out_dir / "locked_test_predictions.npz", y_true=self.test.y, y_pred=pred,
159
+ subject=self.test.subject, time_index=self.test.time_index)
160
+ with (out_dir / "selected_model.pkl").open("wb") as f:
161
+ pickle.dump(model, f)
162
+ (out_dir / "locked_test_metrics.json").write_text(
163
+ json.dumps(metrics, indent=2, ensure_ascii=False, default=str), encoding="utf-8")
164
+ return metrics
165
+
166
+
167
+ def _configure(search, mode, budget, ridge_solver, cache_mb, stress_cache_mb, stress_count,
168
+ stress_policy="complete", sequence="subject"):
169
+ if stress_policy not in {"complete","certified","frontier"}:
170
+ raise ValueError("stress_policy must be complete, certified or frontier")
171
+ if stress_policy in ("certified","frontier") and (stress_count==0 or not np.isfinite(search.robustness_weight)
172
+ or search.robustness_weight<0):
173
+ raise ValueError("Certified bounds require stress_count>0 and finite nonnegative robustness_weight")
174
+ search.stress_policy = stress_policy
175
+ search.reporter=SearchConfusion(search.valid.y,search.train.n_classes) if stress_policy=="frontier" else search_report
176
+ search.incumbent = search.batch_bound = None
177
+ search.pruned_count = search.skipped_views = search.rows_seen = 0
178
+ if mode not in ("exact","turbo") or ridge_solver not in RIDGE_SOLVERS:
179
+ raise ValueError("Unknown search execution mode or Ridge solver")
180
+ if mode=="turbo" and ridge_solver!="original":
181
+ raise ValueError("Turbo uses its fixed Ridge alpha")
182
+ if cache_mb<0 or stress_cache_mb<0 or not isinstance(stress_count,int) or not 0<=stress_count<=4:
183
+ raise ValueError("Invalid cache size or stress count")
184
+ search.sequence=check_sequence(sequence)
185
+ search.create_candidate=partial(make_candidate,mode=mode,budget=budget,ridge_solver=ridge_solver,
186
+ sequence=sequence,calibration=search.valid)
187
+ X=search.valid.X
188
+ search.stress_count=stress_count
189
+ search.stress_names=list(STRESS_TRANSFORMS)[:stress_count]
190
+ search.stress_bundle=None
191
+ search.stress_inputs={}
192
+ remaining=stress_cache_mb*1024**2
193
+ if stress_count>1 and stress_count*len(X)<=1024 and stress_count*X.nbytes<=min(remaining,16*1024**2):
194
+ arr=np.empty((stress_count*len(X),*X.shape[1:]),dtype=X.dtype)
195
+ for i,name in enumerate(search.stress_names):
196
+ arr[i*len(X):(i+1)*len(X)]=STRESS_TRANSFORMS[name](X)
197
+ arr.flags.writeable=False
198
+ search.stress_bundle=arr
199
+ else:
200
+ for name in search.stress_names:
201
+ if X.nbytes<=remaining:
202
+ transformed=STRESS_TRANSFORMS[name](X)
203
+ transformed.flags.writeable=False
204
+ search.stress_inputs[name]=transformed
205
+ remaining-=transformed.nbytes
206
+ arrays=[search.train.X,X,*search.stress_inputs.values()]
207
+ if search.stress_bundle is not None:arrays.append(search.stress_bundle)
208
+ search.memory=SearchFeatures(cache_mb,arrays)
209
+ return search
210
+
211
+
212
+ def evaluate_recipes(specs,train,valid,*,mode="exact",budget=None,workers=1,n_jobs=1,seed=42,
213
+ cost_weight=.01,robustness_weight=.1,cache_mb=192,stress_cache_mb=128,
214
+ ridge_solver="original",stress_count=4,sequence="subject"):
215
+ """Evaluate an explicit ordered sequence with fresh split-local caches."""
216
+ check_splits(train,valid)
217
+ if min(workers,n_jobs)<1:raise ValueError("Worker counts must be positive")
218
+ controller=_AcceleratedSearch(train,valid,".",seed=seed,candidate_workers=workers,
219
+ n_jobs_per_candidate=n_jobs,cost_weight=cost_weight,robustness_weight=robustness_weight)
220
+ _configure(controller,mode,budget,ridge_solver,cache_mb,stress_cache_mb,stress_count,sequence=sequence)
221
+ specs=[s if isinstance(s,CandidateSpec) else CandidateSpec.from_dict(s) for s in specs]
222
+ # Preserve recorded effective candidate parameters; supply missing job settings.
223
+ specs=[controller._runtime_spec(s) if "n_jobs" not in s.params else s for s in specs]
224
+ rows=controller._evaluate_many(specs)
225
+ stats=controller.memory.stats()
226
+ controller.memory.clear()
227
+ return rows, stats
228
+
229
+
230
+ def run_search(method, train, valid, test=None, *, output, population, generations, max_evaluations,
231
+ seed=42, candidate_workers=1, n_jobs_per_candidate=1, ridge_solver="original",
232
+ sequence="subject", cost_weight=0.01, robustness_weight=0.1, cache_mb=192,
233
+ stress_cache_mb=128):
234
+ """Run WISP-Random or WISP-Evolution with explicit population/generations/budget.
235
+
236
+ Selection uses validation only. With ``test``, the winner is refitted once on
237
+ train+validation and scored on test (``selected_model.pkl``, ``locked_test_*``).
238
+ """
239
+ check_splits(*[part for part in (train, valid, test) if part is not None])
240
+ profile = replace(get_profile(method), max_evaluations=int(max_evaluations))
241
+ search = _AcceleratedSearch(train=train, valid=valid, output=output, profile=profile, test=test,
242
+ population=population, generations=generations, seed=seed,
243
+ cost_weight=cost_weight, robustness_weight=robustness_weight,
244
+ candidate_workers=candidate_workers, n_jobs_per_candidate=n_jobs_per_candidate)
245
+ _configure(search, "exact", None, ridge_solver, cache_mb, stress_cache_mb,
246
+ 4 if profile.use_stress else 0, "complete", sequence=sequence)
247
+ try:
248
+ summary = search.run()
249
+ finally:
250
+ search.memory.clear()
251
+ return summary
252
+
253
+
254
+ def fit_search(method,train,valid,*,output,mode="exact",seed=42,evaluations=None,workers=1,
255
+ n_jobs=1,population=24,cost_weight=0.01,robustness_weight=0.1,
256
+ stress_cache_mb=128,budget=None,cache_mb=192,ridge_solver="original",
257
+ refit=True,stress_count=4,family_scope="all",stress_policy="complete",sequence="subject"):
258
+ """Search with the paper's default budget (329 proposals) and population, then refit.
259
+
260
+ Same proposal grammar and default stress/utility as the search controller. Training and
261
+ clean prediction times include actual shared-cache costs, so rankings may change."""
262
+ check_splits(train,valid)
263
+ if min(workers,n_jobs)<1 or population<3:raise ValueError("Invalid parallelism or population")
264
+ scopes={"all":None, "cis":("state_rocket_interval_hmm",),
265
+ "cis-related":("state_rocket_interval_hmm","rocket_sketch_ridge_hmm","posterior_cpu_ensemble")}
266
+ if family_scope not in scopes:raise ValueError("family_scope must be all, cis or cis-related")
267
+ profile=replace(get_profile(method),allowed_cpu_families=scopes[family_scope])
268
+ limit=(profile.max_evaluations if mode=="exact" else 25) if evaluations is None else evaluations
269
+ if not isinstance(limit,int) or limit<1:raise ValueError("evaluations must be a positive integer")
270
+ out=Path(output)
271
+ out.mkdir(parents=True,exist_ok=True)
272
+ if any(out.iterdir()):raise ValueError("Search requires a fresh empty output directory per run")
273
+ per_generation=max(population-max(2,population//3),1)
274
+ generations=max(1,(limit-min(9,limit)+per_generation-1)//per_generation)
275
+ search=_AcceleratedSearch(train,valid,out,profile=replace(profile,max_evaluations=limit),
276
+ population=population,generations=generations,seed=seed,candidate_workers=workers,
277
+ n_jobs_per_candidate=n_jobs,cost_weight=cost_weight,robustness_weight=robustness_weight)
278
+ _configure(search,mode,budget,ridge_solver,cache_mb,stress_cache_mb,stress_count,stress_policy,sequence)
279
+ summary=search.run()
280
+ if summary["n_evaluated"]!=limit:raise RuntimeError("Controller did not complete evaluation budget")
281
+ stats=search.memory.stats()
282
+ search.memory.clear()
283
+ model=None
284
+ if refit:
285
+ model=search.create_candidate(summary["best"]["spec"])
286
+ model.__dict__.pop("calibration_data",None)
287
+ fit_on(model,concatenate_har(train,valid,source="train+validation"))
288
+ model.set_confidence_scale(summary["best"]["result"].get("confidence_scale"))
289
+ summary.update(backend="wisp-fast-shared",mode=mode,ridge_solver=ridge_solver,sequence=sequence,
290
+ family_scope=family_scope,
291
+ fit_scope="train+validation" if refit else "candidate selection only",selection_uses_test=False,
292
+ cached_stress=search.stress_names if search.stress_bundle is not None else list(search.stress_inputs),
293
+ stress_count=stress_count,stress_policy=stress_policy,pruned_candidates=search.pruned_count,
294
+ skipped_stress_views=search.skipped_views,batched_stress=search.stress_bundle is not None,feature_cache=stats,
295
+ evaluations=limit,winner_recipe=summary["best"]["spec"],
296
+ effective_budget=(vars(budget) if budget is not None else "mode defaults"))
297
+ (out/"summary.json").write_text(json.dumps(summary,indent=2,default=str))
298
+ return model, summary
src/wisp_fast/search_cache.py ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Search-scoped reuse of deterministic representation components on immutable splits."""
2
+ from collections import OrderedDict
3
+ from copy import copy
4
+ import hashlib
5
+ from threading import RLock
6
+ import weakref
7
+ from functools import lru_cache
8
+
9
+ import numpy as np
10
+
11
+ from wisp.cpu import features as ref
12
+ from . import features as fx
13
+
14
+
15
+ @lru_cache(maxsize=128)
16
+ def _dyadic(time):
17
+ # Mirrors the release order, including deduplication for short windows.
18
+ bounds = []
19
+ for parts in (2, 4, 8):
20
+ width = max(2, (time+parts-1)//parts)
21
+ bounds.extend((lo,min(lo+width,time)) for lo in range(0,time,width)
22
+ if min(lo+width,time)-lo >= 2)
23
+ return tuple(dict.fromkeys(bounds))
24
+
25
+
26
+ class SearchFeatures:
27
+ """A private per-run LRU; share only deterministic blocks, never train labels or heads."""
28
+ def __init__(self, limit_mb, arrays):
29
+ self.limit = int(limit_mb * 1024**2)
30
+ self.known = {id(arr): (arr, i) for i, arr in enumerate(arrays)}
31
+ self.derived = {}
32
+ self.store = OrderedDict()
33
+ # Tile data have much greater cardinality than the stable feature
34
+ # blocks. Keep a separate bounded LRU to avoid flushing the hot set.
35
+ self.tiles = OrderedDict()
36
+ self.tile_limit = self.limit // 8
37
+ self.tile_bytes = self.tile_hits = self.rank_hits = self.dyad_hits = 0
38
+ self.bytes = self.peak = self.hits = self.misses = self.evictions = 0
39
+ self.lock = RLock()
40
+
41
+ def clear(self):
42
+ self.store.clear()
43
+ self.known.clear()
44
+ self.derived.clear()
45
+ self.tiles.clear()
46
+ self.tile_bytes = 0
47
+ self.bytes = 0
48
+
49
+ def stats(self):
50
+ return dict(limit_bytes=self.limit, peak_bytes=self.peak, hits=self.hits,
51
+ misses=self.misses, evictions=self.evictions, shapelet_tile_hits=self.tile_hits,
52
+ interval_rank_hits=self.rank_hits, dyadic_block_hits=self.dyad_hits)
53
+
54
+ def token(self, x):
55
+ val = self.known.get(id(x))
56
+ if val is not None and x is val[0]:
57
+ return val[1]
58
+ with self.lock:
59
+ entry = self.derived.get(id(x))
60
+ if entry is not None and entry[0]() is x:
61
+ return entry[1]
62
+ # Derived arrays (e.g. train-row cap and temporal downsampling) must
63
+ # never be keyed by shape alone. Their allocation may change each fit.
64
+ return (x.shape, x.dtype.str, x.strides,
65
+ hashlib.blake2b(np.ascontiguousarray(x).view(np.uint8), digest_size=16).digest())
66
+
67
+ def sampled(self, x, steps, sample):
68
+ key = (self.token(x),"time_sample",steps)
69
+ arr = self.get(key,sample)
70
+ # Weak entries tie identity to live allocations, even after eviction.
71
+ idx = id(arr)
72
+ def expired(ref):
73
+ with self.lock:
74
+ item = self.derived.get(idx)
75
+ if item is not None and item[0] is ref:
76
+ self.derived.pop(idx)
77
+ with self.lock:
78
+ self.derived[idx] = (weakref.ref(arr,expired),key)
79
+ return arr
80
+
81
+ def get(self, key, fn, *, tile=False):
82
+ store = self.tiles if tile else self.store
83
+ limit = self.tile_limit if tile else self.limit - self.tile_limit
84
+ with self.lock:
85
+ if key in store:
86
+ self.hits += 1
87
+ if tile:self.tile_hits += 1
88
+ if key[1]=="interval_rank":self.rank_hits += 1
89
+ if key[1]=="dyad":self.dyad_hits += 1
90
+ store.move_to_end(key)
91
+ return store[key]
92
+ self.misses += 1
93
+ arr = fn()
94
+ if arr.nbytes <= limit:
95
+ with self.lock:
96
+ if key not in store:
97
+ used = self.tile_bytes if tile else self.bytes
98
+ while used + arr.nbytes > limit:
99
+ _, old = store.popitem(last=False)
100
+ used -= old.nbytes
101
+ self.evictions += 1
102
+ store[key] = arr
103
+ used += arr.nbytes
104
+ if tile:self.tile_bytes = used
105
+ else:self.bytes = used
106
+ self.peak = max(self.peak, self.bytes+self.tile_bytes)
107
+ return arr
108
+
109
+ def transform(self, blocks, x):
110
+ token = self.token(x)
111
+ def zscore():
112
+ return self.get((token, "z"), lambda: fx._normalize(ref._as_3d(x)))
113
+ def interval(block, data, norm):
114
+ def native_plan(b, rank, evaluate):
115
+ order = self.get((token,"interval_rank",norm),rank)
116
+ fixed = _dyadic(data.shape[1]) if b.include_dyadic else ()
117
+ if not fixed or tuple(b.intervals_[:len(fixed)]) != fixed:
118
+ return evaluate(b.intervals_,order)
119
+ params = (norm,b.fast_profile,tuple(b.quantiles),fixed)
120
+ common = self.get((token,"dyad",params),lambda:evaluate(fixed,order))
121
+ suffix = b.intervals_[len(fixed):]
122
+ return np.concatenate((common,evaluate(suffix,order)),axis=1) if suffix else common
123
+ return block.transform_normalized(data,native_plan=native_plan)
124
+ features=[]
125
+ for b in blocks:
126
+ if type(b) is fx.WearableFeatureExtractor:
127
+ # Stateless window-local features. Constructor parameters fully
128
+ # specify columns, including turbo choices. Fitted names are
129
+ # cheap to copy and restored on warm hits for pickle-size parity.
130
+ params=tuple(b.get_params(deep=False).items())
131
+ key=(token, "wear", params)
132
+ def calculate():
133
+ return b.transform_normalized(zscore()) if x.flags.c_contiguous and b.zscore else b.transform(x)
134
+ value=self.get(key,calculate)
135
+ if not b.feature_names_:
136
+ b.transform(x[:1])
137
+ elif type(b) is fx.InvariantPhysicsFeatureExtractor and x.flags.c_contiguous and b.interval_ is not None:
138
+ # Upstream concatenates deterministic physics with a separate
139
+ # seeded random-interval branch. Share the former across seeds.
140
+ key=(token,"physics",b.segment_sizes,b.symbolic_bins,b.zscore,b.fast_profile,b.include_axis_features)
141
+ def fixed():
142
+ base=copy(b)
143
+ base.interval_=None
144
+ return base.transform(x)
145
+ common=self.get(key,fixed)
146
+ xn=zscore() if b.zscore else np.nan_to_num(ref._as_3d(x))
147
+ if b.interval_.zscore:
148
+ xn=self.get((token,"physics_second_z",b.zscore),lambda:fx._normalize(xn))
149
+ intervals=interval(b.interval_,xn,("physics",b.zscore,b.interval_.zscore))
150
+ value=np.concatenate((common,intervals),axis=1)
151
+ elif type(b) is fx.IntervalDistributionSketch and x.flags.c_contiguous and b.zscore:
152
+ value=interval(b,zscore(),"z")
153
+ elif type(b) is fx.RandomShapeletSketch and x.flags.c_contiguous and b.zscore:
154
+ def tile(key, fn):
155
+ return self.get((token,"shapelet_windows",key),fn,tile=True)
156
+ value=b.transform_normalized(zscore(),window_tile=tile)
157
+ elif type(b) is fx.RandomConvSketch and x.flags.c_contiguous and b.zscore:
158
+ value=b.transform_normalized(zscore())
159
+ else:
160
+ # Fitted shapelets depend on actual training examples; they
161
+ # are deliberately evaluated with the candidate's own object.
162
+ value=b.transform(x)
163
+ features.append(value)
164
+ capture=getattr(self,"capture",None)
165
+ if capture is not None:capture(x,blocks,features)
166
+ return features
src/wisp_fast/search_forest.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Avoid repeated sklearn parameter introspection for built-in WISP forest trees."""
2
+ from contextlib import contextmanager
3
+
4
+ import numpy as np
5
+ from sklearn.ensemble import ExtraTreesClassifier, RandomForestClassifier
6
+ from sklearn.tree import DecisionTreeClassifier, ExtraTreeClassifier
7
+ from sklearn.utils import check_random_state
8
+
9
+
10
+ @contextmanager
11
+ def forest_fit_plan(forest):
12
+ """Use the same per-tree constructor parameters and sklearn seed stream.
13
+
14
+ The hook is local to this one estimator and removed before it is serialized.
15
+ Tree growth, bootstrap samples, sample/class weights and parallel schedules
16
+ remain sklearn's own. Only the two built-in release forest types qualify.
17
+ """
18
+ if type(forest) not in (RandomForestClassifier, ExtraTreesClassifier):
19
+ yield
20
+ return
21
+ prototype = forest.estimator
22
+ if type(prototype) not in (DecisionTreeClassifier,ExtraTreeClassifier):
23
+ yield
24
+ return
25
+ constructor = type(prototype)
26
+ params = prototype.get_params(deep=False)
27
+ params.update({name:getattr(forest,name) for name in forest.estimator_params})
28
+ max_seed = np.iinfo(np.int32).max
29
+
30
+ def make_estimator(append=True,random_state=None):
31
+ # sklearn._set_random_states draws one int for these non-nested trees.
32
+ if random_state is not None:
33
+ params["random_state"] = check_random_state(random_state).randint(max_seed)
34
+ else:
35
+ params["random_state"] = forest.random_state
36
+ tree = constructor(**params)
37
+ if append:
38
+ forest.estimators_.append(tree)
39
+ return tree
40
+
41
+ forest._make_estimator = make_estimator
42
+ try:
43
+ yield
44
+ finally:
45
+ del forest._make_estimator
src/wisp_fast/search_metrics.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Single-confusion-matrix form of the *search* scorer, including observed-axis worst F1."""
2
+ import numpy as np
3
+
4
+
5
+ def search_report(y_true, y_pred, *, label_names=None, **timing):
6
+ truth=np.asarray(y_true,dtype=np.int64)
7
+ pred=np.asarray(y_pred,dtype=np.int64)
8
+ labels, encoded=np.unique(np.concatenate((truth,pred)),return_inverse=True)
9
+ k=len(labels)
10
+ if not k:
11
+ from wisp.core.metrics import classification_report_dict
12
+ return classification_report_dict(truth,pred,label_names=label_names,**timing)
13
+ t,p=np.split(encoded,2)
14
+ cm=np.bincount(t*k+p,minlength=k*k).reshape(k,k)
15
+ return _matrix_report(cm,labels,len(truth),label_names,timing)
16
+
17
+
18
+ def _matrix_report(cm,labels,n,label_names,timing):
19
+ gt=cm.sum(axis=1); predicted=cm.sum(axis=0); tp=cm.diagonal()
20
+ f1=(2.*tp)/(gt+predicted)
21
+ present=gt>0
22
+ r=dict(accuracy=float(tp.sum()/n),balanced_accuracy=float(np.mean(tp[present]/gt[present])),
23
+ macro_f1=float(f1.mean()),weighted_f1=float(np.average(f1,weights=gt)),
24
+ worst_class_f1=float(f1.min()),n=n,n_classes_seen=len(labels),
25
+ per_class_f1={str(int(i)):float(f) for i,f in zip(labels,f1)},confusion_matrix=cm.tolist())
26
+ if label_names is not None:r["label_names"]=label_names
27
+ r.update({k:float(v) for k,v in timing.items() if v is not None})
28
+ return r
29
+
30
+
31
+ class SearchConfusion:
32
+ """Reuse the encoded truth offsets for clean and perturbed predictions."""
33
+ def __init__(self,truth,k):
34
+ self.truth=truth
35
+ self.k=k
36
+ self.offset=None
37
+ arr=np.asarray(truth)
38
+ if 0<k<=512 and len(arr) and np.issubdtype(arr.dtype,np.integer) and arr.min()>=0 and arr.max()<k:
39
+ self.offset=arr.astype(np.int64)*k
40
+
41
+ def __call__(self,truth,pred,*,label_names=None,**timing):
42
+ pred=np.asarray(pred)
43
+ if (truth is not self.truth or self.offset is None or pred.shape!=self.offset.shape or
44
+ not np.issubdtype(pred.dtype,np.integer) or pred.min()<0 or pred.max()>=self.k):
45
+ return search_report(truth,pred,label_names=label_names,**timing)
46
+ cm=np.bincount(self.offset+pred,minlength=self.k*self.k).reshape(self.k,self.k)
47
+ labels=np.flatnonzero(cm.sum(0)+cm.sum(1))
48
+ return _matrix_report(cm[np.ix_(labels,labels)],labels,len(pred),label_names,timing)
src/wisp_fast/selection.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Split checks and the fit call shared by the accelerated search."""
2
+ import numpy as np
3
+
4
+
5
+ def check_splits(*parts):
6
+ """Require sequence metadata, one global vocabulary and disjoint group IDs."""
7
+ first = parts[0]
8
+ if not first.label_names:
9
+ raise ValueError("Supply the ordered global label_names")
10
+ seen = set()
11
+ for data in parts:
12
+ if data.label_names != first.label_names or data.X.shape[1:] != first.X.shape[1:]:
13
+ raise ValueError("Split vocabularies and time/channel shapes must match")
14
+ if data.subject is None or data.time_index is None or not len(data.y):
15
+ raise ValueError("Every split needs nonempty windows, subject and time_index")
16
+ if np.min(data.y) < 0 or np.max(data.y) >= len(first.label_names):
17
+ raise ValueError("Class ID outside supplied global vocabulary")
18
+ groups = set(np.asarray(data.subject).astype(str))
19
+ if groups & seen:
20
+ raise ValueError("Participant/sequence groups overlap between splits")
21
+ seen |= groups
22
+
23
+
24
+ def fit_on(model, data):
25
+ return model.fit(data.X, data.y, groups=data.subject, time_index=data.time_index,
26
+ n_classes=data.n_classes)
src/wisp_fast/shapelet_tiles.h ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Compare eight consecutive candidate start positions of the same shapelet.
2
+ Each lane retains NumPy's internal 8-way double reduction and its second
3
+ centering pass in std. Strict chronological comparisons preserve first ties. */
4
+ #if defined(__x86_64__) && (defined(__GNUC__) || defined(__clang__)) && !defined(_WIN32)
5
+ #include <immintrin.h>
6
+ #define SHAPE512 __attribute__((target("avx512f")))
7
+ typedef __m512d PositionTile;
8
+ static SHAPE512 inline PositionTile tile_sum(const PositionTile *x, int len) {
9
+ if(len<8) {
10
+ PositionTile s=_mm512_set1_pd(-0.);
11
+ for(int j=0;j<len;++j)s+=x[j];
12
+ return s;
13
+ }
14
+ PositionTile a=x[0],b=x[1],c=x[2],d=x[3],e=x[4],f=x[5],g=x[6],h=x[7];
15
+ int j=8;
16
+ for(;j+8<=len;j+=8) {
17
+ a+=x[j]; b+=x[j+1]; c+=x[j+2]; d+=x[j+3];
18
+ e+=x[j+4]; f+=x[j+5]; g+=x[j+6]; h+=x[j+7];
19
+ }
20
+ PositionTile s=((a+b)+(c+d))+((e+f)+(g+h));
21
+ for(;j<len;++j)s+=x[j];
22
+ return s;
23
+ }
24
+ static SHAPE512 void shapelet_row(const double *x,const double *weights,double *dst,
25
+ Py_ssize_t stop,int len,int dilation,int stride,Py_ssize_t k) {
26
+ const __m512i offsets=_mm512_set_epi64(7LL*stride,6LL*stride,5LL*stride,4LL*stride,
27
+ 3LL*stride,2LL*stride,stride,0);
28
+ const PositionTile one=_mm512_set1_pd(1.), floor=_mm512_set1_pd(1e-6);
29
+ PositionTile z[64],sq[64];
30
+ for(Py_ssize_t a=0;a<stop;a+=8LL*stride) {
31
+ int lanes=(int)((stop-1-a)/stride+1);
32
+ if(lanes>8)lanes=8;
33
+ __mmask8 active=(__mmask8)((1U<<lanes)-1);
34
+ for(int j=0;j<len;++j) {
35
+ const double *base=x+a+(Py_ssize_t)j*dilation;
36
+ z[j]=(stride==1)?_mm512_maskz_loadu_pd(active,base):
37
+ _mm512_mask_i64gather_pd(_mm512_setzero_pd(),active,offsets,base,8);
38
+ }
39
+ PositionTile mu=tile_sum(z,len)/(double)len;
40
+ for(int j=0;j<len;++j)z[j]-=mu;
41
+ PositionTile centered_mean=tile_sum(z,len)/(double)len;
42
+ for(int j=0;j<len;++j){PositionTile delta=z[j]-centered_mean;sq[j]=delta*delta;}
43
+ PositionTile sd=_mm512_sqrt_pd(tile_sum(sq,len)/(double)len);
44
+ sd=_mm512_mask_mov_pd(sd,_mm512_cmp_pd_mask(sd,floor,_CMP_LT_OQ),one);
45
+ for(int j=0;j<len;++j)z[j]/=sd;
46
+ for(Py_ssize_t h=0;h<k;++h) {
47
+ for(int j=0;j<len;++j){PositionTile delta=z[j]-weights[h*len+j];sq[j]=delta*delta;}
48
+ PositionTile value=_mm512_sqrt_pd(tile_sum(sq,len)/(double)len);
49
+ unsigned improved=active & _mm512_cmp_pd_mask(value,_mm512_set1_pd(dst[2*h]),_CMP_LT_OQ);
50
+ if(improved) {
51
+ double scores[8];_mm512_storeu_pd(scores,value);
52
+ while(improved) {
53
+ int j=__builtin_ctz(improved); improved&=improved-1;
54
+ if(scores[j]<dst[2*h]) {
55
+ dst[2*h]=scores[j];dst[2*h+1]=(double)(a+(Py_ssize_t)j*stride)/(stop>1?stop-1:1);
56
+ }
57
+ }
58
+ }
59
+ }
60
+ }
61
+ }
62
+ #endif
src/wisp_fast/viterbi.c ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <stddef.h>
2
+ #include <stdint.h>
3
+
4
+ /* Inputs are contiguous with predecessor-major A; first index wins ties.
5
+ Workspace is sized by the Python caller and reused for every recording. */
6
+ #ifdef _WIN32
7
+ __declspec(dllexport)
8
+ #endif
9
+ void wisp_viterbi(const double *e, const double *a, const double *pi,
10
+ int64_t t_count, int64_t k_count, int32_t *back,
11
+ double *prev, double *next, int64_t *path) {
12
+ for (int64_t j = 0; j < k_count; ++j) prev[j] = pi[j] + e[j];
13
+ for (int64_t t = 1; t < t_count; ++t) {
14
+ int32_t *row = back + t * k_count;
15
+ for (int64_t j = 0; j < k_count; ++j) {
16
+ next[j] = prev[0] + a[j];
17
+ row[j] = 0;
18
+ }
19
+ for (int64_t i = 1; i < k_count; ++i) {
20
+ const double *arow = a + i * k_count;
21
+ for (int64_t j = 0; j < k_count; ++j) {
22
+ const double v = prev[i] + arow[j];
23
+ if (v > next[j]) { next[j] = v; row[j] = (int32_t)i; }
24
+ }
25
+ }
26
+ for (int64_t j = 0; j < k_count; ++j) next[j] += e[t * k_count + j];
27
+ double *swap = prev; prev = next; next = swap;
28
+ }
29
+ int64_t last = 0;
30
+ for (int64_t j = 1; j < k_count; ++j) if (prev[j] > prev[last]) last = j;
31
+ path[t_count - 1] = last;
32
+ for (int64_t t = t_count - 1; t > 0; --t) {
33
+ last = back[t * k_count + last];
34
+ path[t - 1] = last;
35
+ }
36
+ }
37
+
38
+ #if defined(__x86_64__) && (defined(__GNUC__) || defined(__clang__)) && !defined(_WIN32)
39
+ #include <immintrin.h>
40
+ /* Hold the winning score and its earliest predecessor in registers while
41
+ scanning a transition column tile. No padding or sentinel class needed. */
42
+ __attribute__((target("avx512f")))
43
+ static void viterbi_tile(const double *e,const double *a,const double *pi,
44
+ int64_t n,int64_t k,int32_t *back,double *p,double *q,int64_t *path) {
45
+ for(int64_t j=0;j<k;++j)p[j]=pi[j]+e[j];
46
+ for(int64_t t=1;t<n;++t) {
47
+ for(int64_t j=0;j<k;j+=8) {
48
+ int w=(int)(k-j<8?k-j:8);
49
+ __mmask8 active=(__mmask8)((1u<<w)-1);
50
+ __m512d best=_mm512_add_pd(_mm512_set1_pd(p[0]),_mm512_maskz_loadu_pd(active,a+j));
51
+ __m512i id=_mm512_setzero_si512();
52
+ for(int64_t i=1;i<k;++i) {
53
+ __m512d s=_mm512_add_pd(_mm512_set1_pd(p[i]),_mm512_maskz_loadu_pd(active,a+i*k+j));
54
+ __mmask8 better=_mm512_cmp_pd_mask(s,best,_CMP_GT_OQ);
55
+ best=_mm512_mask_mov_pd(best,better,s);
56
+ id=_mm512_mask_set1_epi64(id,better,i);
57
+ }
58
+ _mm512_mask_storeu_pd(q+j,active,_mm512_add_pd(best,_mm512_maskz_loadu_pd(active,e+t*k+j)));
59
+ _mm512_mask_cvtepi64_storeu_epi32(back+t*k+j,active,id);
60
+ }
61
+ double *swap=p;p=q;q=swap;
62
+ }
63
+ int64_t last=0;
64
+ for(int64_t j=1;j<k;++j)if(p[j]>p[last])last=j;
65
+ path[n-1]=last;
66
+ for(int64_t t=n-1;t>0;--t){last=back[t*k+last];path[t-1]=last;}
67
+ }
68
+ #endif
69
+
70
+ #ifdef _WIN32
71
+ __declspec(dllexport)
72
+ #endif
73
+ void wisp_viterbi_groups(const double *e,const double *a,const double *pi,
74
+ const int64_t *bounds,int64_t ng,int64_t k,
75
+ int32_t *back,double *prev,double *next,int64_t *path) {
76
+ for(int64_t g=0;g<ng;++g) {
77
+ int64_t start=bounds[g],n=bounds[g+1]-start;
78
+ #if defined(__x86_64__) && (defined(__GNUC__) || defined(__clang__)) && !defined(_WIN32)
79
+ if(k>=5 && __builtin_cpu_supports("avx512f"))
80
+ viterbi_tile(e+start*k,a,pi,n,k,back,prev,next,path+start);
81
+ else
82
+ #endif
83
+ wisp_viterbi(e+start*k,a,pi,n,k,back,prev,next,path+start);
84
+ }
85
+ }
src/wisp_release/__init__.py CHANGED
@@ -1,8 +1,9 @@
1
  """Small release interfaces for WISP; importing this package performs no work."""
2
 
3
  from .checkpoints import load_checkpoint, predict_checkpoint
4
- from .methods import list_methods
5
  from .selection import fit_selector
6
 
7
- __all__ = ["load_checkpoint", "predict_checkpoint", "list_methods", "fit_selector"]
8
- __version__ = "0.1.0"
 
 
1
  """Small release interfaces for WISP; importing this package performs no work."""
2
 
3
  from .checkpoints import load_checkpoint, predict_checkpoint
4
+ from .methods import SEQUENCES, SPEEDS, build_family, list_methods
5
  from .selection import fit_selector
6
 
7
+ __all__ = ["load_checkpoint", "predict_checkpoint", "list_methods", "build_family", "fit_selector",
8
+ "SPEEDS", "SEQUENCES"]
9
+ __version__ = "0.2.0"
src/wisp_release/cli.py CHANGED
@@ -13,7 +13,24 @@ from typing import Any
13
 
14
  from .checkpoints import load_checkpoint, predict_checkpoint, sha256_file
15
  from .data import as_har_data, read_npz
16
- from .methods import FIXED_METHODS, SEARCH_METHODS, SELECTION_METHODS, build_family, list_methods
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
 
19
  def _json(payload: Any) -> str:
@@ -87,7 +104,7 @@ def _predict(args: argparse.Namespace, *, evaluate: bool = False) -> dict[str, A
87
  else int(max(np.max(data["y"]), np.max(prediction))) + 1)
88
  result.update(score(data["y"], prediction, n_classes))
89
  result.update({"checkpoint": str(args.checkpoint), "input": str(args.input),
90
- "decoder": "sequence" if getattr(model, "smoother_", None) is not None else "direct",
91
  "metric_contract": "paper: macro_F1 on observed truth/prediction classes; worst_F1 on full global vocabulary",
92
  "test_usage": "This command evaluates; it does not select or refit a model."})
93
  _write_new(args.output, _json(result))
@@ -104,7 +121,7 @@ def _predict(args: argparse.Namespace, *, evaluate: bool = False) -> dict[str, A
104
  with args.output.open("xb") as stream:
105
  np.savez_compressed(stream, **arrays)
106
  return {"output": str(args.output), "n_predictions": len(prediction),
107
- "decoder": "sequence" if getattr(model, "smoother_", None) is not None else "direct"}
108
 
109
 
110
  def _training_plan(args: argparse.Namespace) -> dict[str, Any]:
@@ -128,12 +145,25 @@ def _fit(args: argparse.Namespace) -> dict[str, Any]:
128
  if not isinstance(overrides, dict):
129
  raise ValueError("--overrides must be a JSON object")
130
  model = build_family(args.method, seed=args.seed, n_jobs=args.n_jobs,
131
- direct=args.direct, overrides=overrides)
132
- if getattr(model, "use_hmm", False) and (dataset.subject is None or dataset.time_index is None):
133
  raise ValueError("Sequence-decoder fitting requires both subject/session IDs and time_index; "
134
  "use --direct only if intentionally fitting a different direct-decoder variant")
 
 
 
 
 
 
 
 
 
 
 
135
  model.fit(dataset.X, dataset.y, groups=dataset.subject, time_index=dataset.time_index,
136
  n_classes=dataset.n_classes)
 
 
137
  args.output.parent.mkdir(parents=True, exist_ok=True)
138
  with args.output.open("xb") as stream:
139
  pickle.dump(model, stream, protocol=pickle.HIGHEST_PROTOCOL)
@@ -141,8 +171,11 @@ def _fit(args: argparse.Namespace) -> dict[str, Any]:
141
  "label_names": dataset.label_names, "model_seed": args.seed,
142
  "fit_scope": "explicit input NPZ; no automatic benchmark split or refit",
143
  "use_hmm": getattr(model, "smoother_", None) is not None,
 
 
 
144
  "source_status": "newly_fitted", "sha256": sha256_file(args.output),
145
- "params": model.get_params(deep=False)}
146
  _write_new(metadata_path, _json(metadata))
147
  return {"checkpoint": str(args.output), "metadata": str(metadata_path),
148
  "sha256": metadata["sha256"], "fit_seconds": getattr(model, "fit_seconds_", None)}
@@ -160,7 +193,8 @@ def _select_fit(args: argparse.Namespace) -> dict[str, Any]:
160
  raise FileExistsError(f"Refusing to overwrite {path}")
161
  train, valid = [as_har_data(read_npz(path, require_y=True), n_classes=args.n_classes,
162
  source=str(path)) for path in (args.train, args.valid)]
163
- model, report = fit_selector(args.method, train, valid, seed=args.seed, n_jobs=args.n_jobs)
 
164
  args.output.parent.mkdir(parents=True, exist_ok=True)
165
  with args.output.open("xb") as stream:
166
  pickle.dump(model, stream, protocol=pickle.HIGHEST_PROTOCOL)
@@ -168,6 +202,7 @@ def _select_fit(args: argparse.Namespace) -> dict[str, Any]:
168
  "input_shape": list(train.X.shape[1:]), "label_names": train.label_names,
169
  "model_seed": args.seed, "fit_scope": "train+validation",
170
  "use_hmm": getattr(model, "smoother_", None) is not None,
 
171
  "source_status": "newly_fitted", "sha256": sha256_file(args.output),
172
  "selection_uses_test": False}
173
  _write_new(metadata_path, _json(metadata))
@@ -180,8 +215,8 @@ def _select_fit(args: argparse.Namespace) -> dict[str, Any]:
180
  def _search(args: argparse.Namespace) -> dict[str, Any]:
181
  if not args.execute:
182
  return _training_plan(args)
183
- from wisp.search.controller import WISPSearchController
184
  from wisp.search.profiles import get_profile
 
185
 
186
  if args.candidate_workers > args.n_jobs:
187
  raise ValueError("candidate_workers cannot exceed the explicit total n_jobs budget")
@@ -202,13 +237,14 @@ def _search(args: argparse.Namespace) -> dict[str, Any]:
202
  # Users supply locked partitions; this command never invents a split, and
203
  # selection stays on validation inside the preserved search controller.
204
  profile = replace(get_profile(args.method), max_evaluations=args.max_evaluations)
205
- controller = WISPSearchController(
206
- train=parts[0], valid=parts[1], test=parts[2], output=args.output,
207
- profile=profile, population=args.population, generations=args.generations,
208
- seed=args.seed, candidate_workers=args.candidate_workers,
 
209
  n_jobs_per_candidate=max(1, args.n_jobs // args.candidate_workers),
 
210
  )
211
- result = controller.run()
212
  model_path = args.output / "selected_model.pkl"
213
  if model_path.is_file():
214
  sidecar = args.output / "selected_model.metadata.json"
@@ -216,7 +252,8 @@ def _search(args: argparse.Namespace) -> dict[str, Any]:
216
  "label_names": parts[0].label_names, "model_seed": args.seed,
217
  "fit_scope": "train+validation; selection uses validation only",
218
  "sha256": sha256_file(model_path), "source_status": "newly_fitted",
219
- "search_profile": profile.to_dict(), "selection_uses_test": False}
 
220
  if not sidecar.exists():
221
  _write_new(sidecar, _json(metadata))
222
  return result
@@ -229,9 +266,24 @@ def _positive(value: str) -> int:
229
  return result
230
 
231
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
  def make_parser() -> argparse.ArgumentParser:
233
  parser = argparse.ArgumentParser(prog="wisp", description="WISP method checkpoints and CPU workflows")
234
- parser.add_argument("--version", action="version", version="WISP release 0.1.0")
235
  sub = parser.add_subparsers(dest="command", required=True)
236
  sub.add_parser("list", help="List the full paper method series; no model loads")
237
  inv = sub.add_parser("inventory", help="Read inventory/file presence; no model loads")
@@ -255,6 +307,9 @@ def make_parser() -> argparse.ArgumentParser:
255
  fit.add_argument("--n-classes", type=_positive)
256
  fit.add_argument("--direct", action="store_true", help="Intentionally fit without the sequence decoder")
257
  fit.add_argument("--overrides", help="Explicit JSON parameter overrides (changes the trained variant)")
 
 
 
258
  fit.add_argument("--execute", action="store_true", help="Actually fit; otherwise print a plan only")
259
  select = sub.add_parser("select-fit", help="Plan or explicitly select a fixed family on validation and refit once")
260
  select.add_argument("--method", choices=tuple(SELECTION_METHODS), required=True)
@@ -264,8 +319,9 @@ def make_parser() -> argparse.ArgumentParser:
264
  select.add_argument("--seed", type=int, required=True)
265
  select.add_argument("--n-jobs", type=_positive, required=True)
266
  select.add_argument("--n-classes", type=_positive)
 
267
  select.add_argument("--execute", action="store_true", help="Actually select/refit; otherwise print a plan only")
268
- search = sub.add_parser("search", help="Plan or explicitly run the preserved CPU search controller")
269
  search.add_argument("--method", choices=tuple(SEARCH_METHODS), required=True)
270
  for part in ("train", "valid", "test"):
271
  search.add_argument(f"--{part}", type=Path, required=True)
@@ -277,6 +333,7 @@ def make_parser() -> argparse.ArgumentParser:
277
  search.add_argument("--n-jobs", type=_positive, required=True, help="Total CPU thread budget")
278
  search.add_argument("--candidate-workers", type=_positive, default=1)
279
  search.add_argument("--n-classes", type=_positive)
 
280
  search.add_argument("--execute", action="store_true", help="Actually search; otherwise print a plan only")
281
  return parser
282
 
 
13
 
14
  from .checkpoints import load_checkpoint, predict_checkpoint, sha256_file
15
  from .data import as_har_data, read_npz
16
+ from .methods import (FIXED_METHODS, SEARCH_METHODS, SELECTION_METHODS, SEQUENCES, SPEEDS,
17
+ RIDGE_SOLVER_FOR_SPEED, build_family, list_methods)
18
+
19
+ __version__ = "0.2.0"
20
+
21
+
22
+ def _decoder(model: Any) -> str:
23
+ smoother = getattr(model, "smoother_", None)
24
+ if smoother is None:
25
+ return "direct"
26
+ if getattr(smoother, "sequence", "subject") == "recording" and not getattr(smoother, "active", True):
27
+ return "direct (recording-level smoothing skipped)"
28
+ return "sequence"
29
+
30
+
31
+ def _sequence(model: Any) -> str | None:
32
+ smoother = getattr(model, "smoother_", None)
33
+ return None if smoother is None else getattr(smoother, "sequence", "subject")
34
 
35
 
36
  def _json(payload: Any) -> str:
 
104
  else int(max(np.max(data["y"]), np.max(prediction))) + 1)
105
  result.update(score(data["y"], prediction, n_classes))
106
  result.update({"checkpoint": str(args.checkpoint), "input": str(args.input),
107
+ "decoder": _decoder(model), "sequence": _sequence(model),
108
  "metric_contract": "paper: macro_F1 on observed truth/prediction classes; worst_F1 on full global vocabulary",
109
  "test_usage": "This command evaluates; it does not select or refit a model."})
110
  _write_new(args.output, _json(result))
 
121
  with args.output.open("xb") as stream:
122
  np.savez_compressed(stream, **arrays)
123
  return {"output": str(args.output), "n_predictions": len(prediction),
124
+ "decoder": _decoder(model), "sequence": _sequence(model)}
125
 
126
 
127
  def _training_plan(args: argparse.Namespace) -> dict[str, Any]:
 
145
  if not isinstance(overrides, dict):
146
  raise ValueError("--overrides must be a JSON object")
147
  model = build_family(args.method, seed=args.seed, n_jobs=args.n_jobs,
148
+ direct=args.direct, overrides=overrides, speed=args.speed, sequence=args.sequence)
149
+ if model.use_hmm and (dataset.subject is None or dataset.time_index is None):
150
  raise ValueError("Sequence-decoder fitting requires both subject/session IDs and time_index; "
151
  "use --direct only if intentionally fitting a different direct-decoder variant")
152
+ valid = None
153
+ if args.valid is not None:
154
+ if args.sequence != "recording" or not model.use_hmm:
155
+ raise ValueError("--valid is used only with --sequence recording and HMM smoothing")
156
+ valid = as_har_data(read_npz(args.valid, require_y=True), n_classes=dataset.n_classes,
157
+ source=str(args.valid))
158
+ if valid.label_names != dataset.label_names or valid.X.shape[1:] != dataset.X.shape[1:]:
159
+ raise ValueError("--valid must share the training label_names and time/channel shape")
160
+ elif args.sequence == "recording" and model.use_hmm:
161
+ raise ValueError("--sequence recording needs --valid: the confidence scaling of classifier "
162
+ "scores is chosen on the validation split")
163
  model.fit(dataset.X, dataset.y, groups=dataset.subject, time_index=dataset.time_index,
164
  n_classes=dataset.n_classes)
165
+ if valid is not None:
166
+ model.fit_confidence_scale(valid.X, valid.y)
167
  args.output.parent.mkdir(parents=True, exist_ok=True)
168
  with args.output.open("xb") as stream:
169
  pickle.dump(model, stream, protocol=pickle.HIGHEST_PROTOCOL)
 
171
  "label_names": dataset.label_names, "model_seed": args.seed,
172
  "fit_scope": "explicit input NPZ; no automatic benchmark split or refit",
173
  "use_hmm": getattr(model, "smoother_", None) is not None,
174
+ "speed": args.speed, "ridge_solver": model.ridge_solver, "sequence": args.sequence,
175
+ "sequence_smoothing": _decoder(model) == "sequence",
176
+ "confidence_scale": model.confidence_scale,
177
  "source_status": "newly_fitted", "sha256": sha256_file(args.output),
178
+ "params": model.template.get_params(deep=False)}
179
  _write_new(metadata_path, _json(metadata))
180
  return {"checkpoint": str(args.output), "metadata": str(metadata_path),
181
  "sha256": metadata["sha256"], "fit_seconds": getattr(model, "fit_seconds_", None)}
 
193
  raise FileExistsError(f"Refusing to overwrite {path}")
194
  train, valid = [as_har_data(read_npz(path, require_y=True), n_classes=args.n_classes,
195
  source=str(path)) for path in (args.train, args.valid)]
196
+ model, report = fit_selector(args.method, train, valid, seed=args.seed, n_jobs=args.n_jobs,
197
+ speed=args.speed, sequence=args.sequence)
198
  args.output.parent.mkdir(parents=True, exist_ok=True)
199
  with args.output.open("xb") as stream:
200
  pickle.dump(model, stream, protocol=pickle.HIGHEST_PROTOCOL)
 
202
  "input_shape": list(train.X.shape[1:]), "label_names": train.label_names,
203
  "model_seed": args.seed, "fit_scope": "train+validation",
204
  "use_hmm": getattr(model, "smoother_", None) is not None,
205
+ "speed": args.speed, "sequence": args.sequence,
206
  "source_status": "newly_fitted", "sha256": sha256_file(args.output),
207
  "selection_uses_test": False}
208
  _write_new(metadata_path, _json(metadata))
 
215
  def _search(args: argparse.Namespace) -> dict[str, Any]:
216
  if not args.execute:
217
  return _training_plan(args)
 
218
  from wisp.search.profiles import get_profile
219
+ from wisp_fast.search import run_search
220
 
221
  if args.candidate_workers > args.n_jobs:
222
  raise ValueError("candidate_workers cannot exceed the explicit total n_jobs budget")
 
237
  # Users supply locked partitions; this command never invents a split, and
238
  # selection stays on validation inside the preserved search controller.
239
  profile = replace(get_profile(args.method), max_evaluations=args.max_evaluations)
240
+ result = run_search(
241
+ args.method, parts[0], parts[1], parts[2], output=args.output,
242
+ population=args.population, generations=args.generations,
243
+ max_evaluations=args.max_evaluations, seed=args.seed,
244
+ candidate_workers=args.candidate_workers,
245
  n_jobs_per_candidate=max(1, args.n_jobs // args.candidate_workers),
246
+ ridge_solver=RIDGE_SOLVER_FOR_SPEED[args.speed], sequence=args.sequence,
247
  )
 
248
  model_path = args.output / "selected_model.pkl"
249
  if model_path.is_file():
250
  sidecar = args.output / "selected_model.metadata.json"
 
252
  "label_names": parts[0].label_names, "model_seed": args.seed,
253
  "fit_scope": "train+validation; selection uses validation only",
254
  "sha256": sha256_file(model_path), "source_status": "newly_fitted",
255
+ "search_profile": profile.to_dict(), "speed": args.speed,
256
+ "sequence": args.sequence, "selection_uses_test": False}
257
  if not sidecar.exists():
258
  _write_new(sidecar, _json(metadata))
259
  return result
 
266
  return result
267
 
268
 
269
+ SPEED_HELP = ("exact (default): original Ridge solver, outputs identical to the original "
270
+ "implementation. fast: Ridge strength chosen on a 512-row training subsample, then "
271
+ "fitted on all rows; faster, results can change slightly. Affects Ridge-based models only.")
272
+ SEQUENCE_HELP = ("subject (default): all windows of one participant form one HMM chain, ordered by "
273
+ "time index (original behaviour). recording: each recording forms its own chain; a new "
274
+ "recording starts whenever the time index goes backwards; the confidence scaling of "
275
+ "classifier scores is chosen on the validation split; smoothing is skipped when most "
276
+ "training recordings contain a single activity.")
277
+
278
+
279
+ def _add_options(command: argparse.ArgumentParser) -> None:
280
+ command.add_argument("--speed", choices=SPEEDS, default="exact", help=SPEED_HELP)
281
+ command.add_argument("--sequence", choices=SEQUENCES, default="subject", help=SEQUENCE_HELP)
282
+
283
+
284
  def make_parser() -> argparse.ArgumentParser:
285
  parser = argparse.ArgumentParser(prog="wisp", description="WISP method checkpoints and CPU workflows")
286
+ parser.add_argument("--version", action="version", version=f"WISP release {__version__}")
287
  sub = parser.add_subparsers(dest="command", required=True)
288
  sub.add_parser("list", help="List the full paper method series; no model loads")
289
  inv = sub.add_parser("inventory", help="Read inventory/file presence; no model loads")
 
307
  fit.add_argument("--n-classes", type=_positive)
308
  fit.add_argument("--direct", action="store_true", help="Intentionally fit without the sequence decoder")
309
  fit.add_argument("--overrides", help="Explicit JSON parameter overrides (changes the trained variant)")
310
+ fit.add_argument("--valid", type=Path,
311
+ help="Validation NPZ; required with --sequence recording to choose the confidence scaling")
312
+ _add_options(fit)
313
  fit.add_argument("--execute", action="store_true", help="Actually fit; otherwise print a plan only")
314
  select = sub.add_parser("select-fit", help="Plan or explicitly select a fixed family on validation and refit once")
315
  select.add_argument("--method", choices=tuple(SELECTION_METHODS), required=True)
 
319
  select.add_argument("--seed", type=int, required=True)
320
  select.add_argument("--n-jobs", type=_positive, required=True)
321
  select.add_argument("--n-classes", type=_positive)
322
+ _add_options(select)
323
  select.add_argument("--execute", action="store_true", help="Actually select/refit; otherwise print a plan only")
324
+ search = sub.add_parser("search", help="Plan or explicitly run the WISP-Random/WISP-Evolution CPU search")
325
  search.add_argument("--method", choices=tuple(SEARCH_METHODS), required=True)
326
  for part in ("train", "valid", "test"):
327
  search.add_argument(f"--{part}", type=Path, required=True)
 
333
  search.add_argument("--n-jobs", type=_positive, required=True, help="Total CPU thread budget")
334
  search.add_argument("--candidate-workers", type=_positive, default=1)
335
  search.add_argument("--n-classes", type=_positive)
336
+ _add_options(search)
337
  search.add_argument("--execute", action="store_true", help="Actually search; otherwise print a plan only")
338
  return parser
339
 
src/wisp_release/methods.py CHANGED
@@ -44,9 +44,22 @@ def list_methods() -> list[dict[str, Any]]:
44
  return out
45
 
46
 
47
- def build_family(method_id: str, *, seed: int, n_jobs: int = 1,
48
- direct: bool = False, overrides: dict[str, Any] | None = None) -> Any:
49
- """Construct one fixed WISP family; construction does not fit it."""
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  if method_id not in FIXED_METHODS:
51
  raise ValueError(f"family-fit supports fixed families only: {', '.join(FIXED_METHODS)}")
52
  if n_jobs < 1:
@@ -57,3 +70,30 @@ def build_family(method_id: str, *, seed: int, n_jobs: int = 1,
57
  if direct:
58
  params["use_hmm"] = False
59
  return build_method(method_id, seed=seed, n_jobs=n_jobs, overrides=params)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  return out
45
 
46
 
47
+ SPEEDS = ("exact", "fast")
48
+ SEQUENCES = ("subject", "recording")
49
+ # speed -> Ridge solver of the accelerated engine
50
+ RIDGE_SOLVER_FOR_SPEED = {"exact": "original", "fast": "screened"}
51
+
52
+
53
+ def check_options(speed: str, sequence: str) -> None:
54
+ if speed not in SPEEDS:
55
+ raise ValueError(f"speed must be one of {', '.join(SPEEDS)}; got {speed!r}")
56
+ if sequence not in SEQUENCES:
57
+ raise ValueError(f"sequence must be one of {', '.join(SEQUENCES)}; got {sequence!r}")
58
+
59
+
60
+ def build_reference_family(method_id: str, *, seed: int, n_jobs: int = 1,
61
+ direct: bool = False, overrides: dict[str, Any] | None = None) -> Any:
62
+ """Construct the reference (unaccelerated) estimator of one fixed WISP family."""
63
  if method_id not in FIXED_METHODS:
64
  raise ValueError(f"family-fit supports fixed families only: {', '.join(FIXED_METHODS)}")
65
  if n_jobs < 1:
 
70
  if direct:
71
  params["use_hmm"] = False
72
  return build_method(method_id, seed=seed, n_jobs=n_jobs, overrides=params)
73
+
74
+
75
+ def build_family(method_id: str, *, seed: int, n_jobs: int = 1,
76
+ direct: bool = False, overrides: dict[str, Any] | None = None,
77
+ speed: str = "exact", sequence: str = "subject") -> Any:
78
+ """Construct one fixed WISP family; construction does not fit it.
79
+
80
+ speed:
81
+ ``"exact"`` (default) uses the original Ridge solver; outputs match the
82
+ original implementation. ``"fast"`` chooses the Ridge regularisation
83
+ strength on a 512-row training subsample and then fits on all rows; it is
84
+ faster and results can change slightly. Only models with a Ridge
85
+ classifier (WISP-RC, WISP-CIS and the RC/CIS members of WISP-CSE and
86
+ WISP-ESE) are affected.
87
+ sequence:
88
+ ``"subject"`` (default): all windows of one participant form one HMM chain
89
+ ordered by time index (the original behaviour). ``"recording"``: each
90
+ recording forms its own chain, and a new recording starts whenever the
91
+ time index goes backwards. Call ``model.fit_confidence_scale(X_valid,
92
+ y_valid)`` after fitting to choose the confidence scaling on validation data.
93
+ """
94
+ check_options(speed, sequence)
95
+ template = build_reference_family(method_id, seed=seed, n_jobs=n_jobs, direct=direct,
96
+ overrides=overrides)
97
+ from wisp_fast.engine import with_ridge_solver
98
+
99
+ return with_ridge_solver(template, ridge_solver=RIDGE_SOLVER_FOR_SPEED[speed], sequence=sequence)
src/wisp_release/selection.py CHANGED
@@ -6,7 +6,7 @@ import math
6
  from typing import Any, Callable
7
 
8
  from .checkpoints import predict_checkpoint
9
- from .methods import FIXED_METHODS, build_family
10
  from .scoring import score
11
 
12
  SELECTOR_ORDER = tuple(FIXED_METHODS)
@@ -38,15 +38,28 @@ def _model_size_mb(model: Any) -> float:
38
  return estimate_pickle_size_mb(model)
39
 
40
 
 
 
 
 
41
  def fit_selector(method_id: str, train: Any, valid: Any, *, seed: int, n_jobs: int = 1,
42
- builder: Callable[..., Any] | None = None) -> tuple[Any, dict[str, Any]]:
 
43
  """Fit/evaluate fixed families on validation, then refit the winner once.
44
 
45
  No test input is accepted. ``train`` and ``valid`` are HARData objects with
46
  the same global vocabulary, shape and disjoint participant/session IDs.
47
  ``builder`` is a testing/customization seam; default canonical settings are
48
  the preserved paper registry. Changing a builder changes the experiment.
 
 
 
 
 
 
 
49
  """
 
50
  import numpy as np
51
  from wisp.core.data import concatenate_har
52
 
@@ -64,28 +77,48 @@ def fit_selector(method_id: str, train: Any, valid: Any, *, seed: int, n_jobs: i
64
  if train_groups & valid_groups:
65
  raise ValueError("Participant/session leakage between train and validation")
66
  eligible = SELECT5_FAMILIES if method_id == "wisp_select5" else SELECTOR_ORDER
 
67
  builder = builder or build_family
68
- candidates = []
69
  for family in eligible:
70
- model = builder(family, seed=seed, n_jobs=n_jobs)
71
  model.fit(train.X, train.y, groups=train.subject, time_index=train.time_index,
72
  n_classes=train.n_classes)
 
 
 
73
  prediction = predict_checkpoint(model, valid.X, subject=valid.subject,
74
  time_index=valid.time_index, n_jobs=n_jobs)
75
  candidates.append({"method_id": family,
76
  **score(valid.y, prediction, valid.n_classes),
77
- "model_size_mb": _model_size_mb(model)})
78
- winner = choose_validation_winner(candidates)
 
 
 
 
 
 
 
 
 
 
 
 
79
  combined = concatenate_har(train, valid, source="train+validation")
80
- model = builder(winner, seed=seed, n_jobs=n_jobs)
81
  model.fit(combined.X, combined.y, groups=combined.subject, time_index=combined.time_index,
82
  n_classes=combined.n_classes)
 
 
83
  report = {"method_id": method_id, "selected_method_id": winner,
 
84
  "validation_candidates": candidates, "fit_scope": "train+validation",
85
  "selection_uses_test": False, "model_seed": seed,
86
  "tie_break": ["higher validation macro_f1", "higher full-vocabulary worst_class_f1",
87
  "smaller fitted model default-protocol pickle size", "canonical family order"],
88
- "serialized_size_caveat": "Module namespace renaming may alter serialized byte counts; "
89
- "refitted models are not claimed binary-identical to historical files.",
 
90
  "canonical_family_order": list(SELECTOR_ORDER)}
91
  return model, report
 
6
  from typing import Any, Callable
7
 
8
  from .checkpoints import predict_checkpoint
9
+ from .methods import FIXED_METHODS, build_family, build_reference_family, check_options
10
  from .scoring import score
11
 
12
  SELECTOR_ORDER = tuple(FIXED_METHODS)
 
38
  return estimate_pickle_size_mb(model)
39
 
40
 
41
+ def _quality(record: dict[str, Any]) -> tuple[float, float]:
42
+ return float(record["macro_f1"]), float(record["worst_class_f1"])
43
+
44
+
45
  def fit_selector(method_id: str, train: Any, valid: Any, *, seed: int, n_jobs: int = 1,
46
+ builder: Callable[..., Any] | None = None, speed: str = "exact",
47
+ sequence: str = "subject") -> tuple[Any, dict[str, Any]]:
48
  """Fit/evaluate fixed families on validation, then refit the winner once.
49
 
50
  No test input is accepted. ``train`` and ``valid`` are HARData objects with
51
  the same global vocabulary, shape and disjoint participant/session IDs.
52
  ``builder`` is a testing/customization seam; default canonical settings are
53
  the preserved paper registry. Changing a builder changes the experiment.
54
+ ``speed`` and ``sequence`` are passed to :func:`wisp_release.methods.build_family`.
55
+ With ``sequence="recording"`` each candidate's confidence scaling is chosen on
56
+ validation, and the final train+validation refit reuses the winner's factor.
57
+
58
+ Model size only breaks exact ties on both validation scores. It is measured on
59
+ the reference (unaccelerated) estimator fitted on training data, as in the
60
+ original selector, and is reported only for tied candidates.
61
  """
62
+ check_options(speed, sequence)
63
  import numpy as np
64
  from wisp.core.data import concatenate_har
65
 
 
77
  if train_groups & valid_groups:
78
  raise ValueError("Participant/session leakage between train and validation")
79
  eligible = SELECT5_FAMILIES if method_id == "wisp_select5" else SELECTOR_ORDER
80
+ custom = builder is not None
81
  builder = builder or build_family
82
+ candidates, fitted, scales = [], {}, {}
83
  for family in eligible:
84
+ model = builder(family, seed=seed, n_jobs=n_jobs, speed=speed, sequence=sequence)
85
  model.fit(train.X, train.y, groups=train.subject, time_index=train.time_index,
86
  n_classes=train.n_classes)
87
+ if sequence == "recording" and hasattr(model, "fit_confidence_scale"):
88
+ model.fit_confidence_scale(valid.X, valid.y)
89
+ scales[family] = model.confidence_scale
90
  prediction = predict_checkpoint(model, valid.X, subject=valid.subject,
91
  time_index=valid.time_index, n_jobs=n_jobs)
92
  candidates.append({"method_id": family,
93
  **score(valid.y, prediction, valid.n_classes),
94
+ "model_size_mb": None})
95
+ fitted[family] = model
96
+ best = max(map(_quality, candidates))
97
+ tied = [record for record in candidates if _quality(record) == best]
98
+ for record in tied if len(tied) > 1 else ():
99
+ family = record["method_id"]
100
+ if custom:
101
+ size_model = fitted[family]
102
+ else:
103
+ size_model = build_reference_family(family, seed=seed, n_jobs=n_jobs)
104
+ size_model.fit(train.X, train.y, groups=train.subject, time_index=train.time_index,
105
+ n_classes=train.n_classes)
106
+ record["model_size_mb"] = _model_size_mb(size_model)
107
+ winner = tied[0]["method_id"] if len(tied) == 1 else choose_validation_winner(tied)
108
  combined = concatenate_har(train, valid, source="train+validation")
109
+ model = builder(winner, seed=seed, n_jobs=n_jobs, speed=speed, sequence=sequence)
110
  model.fit(combined.X, combined.y, groups=combined.subject, time_index=combined.time_index,
111
  n_classes=combined.n_classes)
112
+ if scales.get(winner) is not None:
113
+ model.set_confidence_scale(scales[winner])
114
  report = {"method_id": method_id, "selected_method_id": winner,
115
+ "speed": speed, "sequence": sequence, "confidence_scale": scales.get(winner),
116
  "validation_candidates": candidates, "fit_scope": "train+validation",
117
  "selection_uses_test": False, "model_seed": seed,
118
  "tie_break": ["higher validation macro_f1", "higher full-vocabulary worst_class_f1",
119
  "smaller fitted model default-protocol pickle size", "canonical family order"],
120
+ "serialized_size_caveat": "Sizes are measured only for candidates tied on both scores, on the "
121
+ "reference estimator; refitted models are not claimed binary-identical "
122
+ "to historical files.",
123
  "canonical_family_order": list(SELECTOR_ORDER)}
124
  return model, report
tests/test_cis_features.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Backends for faithful finite normalization, ACF, symbolic thresholds and rank queries."""
2
+ import unittest
3
+ from unittest.mock import patch
4
+ import warnings
5
+
6
+ import numpy as np
7
+ from wisp_fast import features as fx
8
+
9
+
10
+ class FaithfulCIS(unittest.TestCase):
11
+ @unittest.skipIf(fx._family_kernels is None,"native kernels unavailable")
12
+ def test_normalization_acf(self):
13
+ rng=np.random.default_rng(643)
14
+ for t in (1,7,8,9,127,128,129,160,257,2049):
15
+ for c in (1,2,3,7):
16
+ x=rng.normal(size=(4,t,c));x[1]=3e4;x[2]=np.rint(x[2])
17
+ x[3,::3,0]=-0.
18
+ ref=fx.ref._safe_zscore_per_window(x)
19
+ np.testing.assert_array_equal(fx._normalize(x),ref)
20
+ lags=(1,0,t,3,7,1)
21
+ np.testing.assert_array_equal(fx._autocorr(x,lags),fx.ref._autocorr_features(x,lags))
22
+ neg=np.full((2,129,1),-0.)
23
+ self.assertEqual(fx._normalize(neg).tobytes(),fx.ref._safe_zscore_per_window(neg).tobytes())
24
+ neg[:,::2]=0.
25
+ self.assertEqual(fx._autocorr(neg,(1,2)).tobytes(),fx.ref._autocorr_features(neg,(1,2)).tobytes())
26
+ x=np.array([[[np.nan,0],[np.inf,1],[2,3]],[[0,1],[0,2],[0,3]]])
27
+ with warnings.catch_warnings():
28
+ warnings.simplefilter("ignore", RuntimeWarning)
29
+ np.testing.assert_array_equal(fx._normalize(x),fx.ref._safe_zscore_per_window(x))
30
+ with self.assertRaises(ValueError):fx._family_kernels.normalize(ref,ref)
31
+ with self.assertRaises(ValueError):fx._family_kernels.autocorr(ref,(1,),ref)
32
+
33
+ @unittest.skipIf(fx._cis_intervals is None,"native interval kernels unavailable")
34
+ def test_range_select_boundaries_and_degenerate(self):
35
+ rng=np.random.default_rng(137)
36
+ for t in (17,63,64,65,127,128,129,160,255,256,257):
37
+ x=np.round(rng.normal(size=(5,t,3)),1);x[0,:,:]=1.25
38
+ x[1,:,0]=np.linspace(0,100,t);x[2,::2,1]=-0.
39
+ for turbo in (True,False):
40
+ m=fx.IntervalDistributionSketch(n_random_intervals=17,quantiles=(0.,.1,.25,.5,.75,.9,1.),
41
+ fast_profile=turbo,zscore=False,random_state=7).fit(x)
42
+ m.intervals_.extend([(0,t),(0,1),(t-1,t),(1,t-1)])
43
+ got=m.transform_normalized(x)
44
+ with patch.object(fx,"_cis_intervals",None):
45
+ reference=m.transform_normalized(x)
46
+ np.testing.assert_array_equal(got,reference)
47
+
48
+ @unittest.skipIf(fx._cis_intervals is None,"native symbolic counts unavailable")
49
+ def test_short_sort_respects_ties(self):
50
+ rng=np.random.default_rng(66)
51
+ for t in (8,32,127,256,512,513):
52
+ v=np.round(rng.normal(size=(4,t,3)),1);v[0]=-.0
53
+ z=fx._symbolic_bundle([("ch",v)],6)["ch"]
54
+ np.testing.assert_array_equal(z,fx._state_counts(v,6))
tests/test_conv_kernels.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Packed random-convolution parity with the previous release backend."""
2
+ from unittest import TestCase, skipIf
3
+ from unittest.mock import patch
4
+ import pickle
5
+ import warnings
6
+ import numpy as np
7
+ from wisp_fast import features as fx
8
+
9
+
10
+ @skipIf(fx._cis_conv is None or fx._kernels is None, 'native convolution unavailable')
11
+ class TestPackedConvolution(TestCase):
12
+ def test_exact_tails_and_pairwise_reductions(self):
13
+ rng = np.random.default_rng(707)
14
+ cases = [15, 16, 17, 18, 19, 23, 32, 35, 37, 63, 96,
15
+ 126, 127, 128, 129, 130, 131, 159, 160, 161, 259, 2049]
16
+ for index, length in enumerate(cases):
17
+ for c in [1, 2, 3, 5]:
18
+ with self.subTest(length=length, channels=c):
19
+ x = rng.normal(size=(3, length, c))
20
+ if index % 4 == 0:
21
+ x = np.rint(x)
22
+ if index % 3 == 0:
23
+ x[0, 3, 0] = np.nan
24
+ x[2, :, -1] = 0.
25
+ for turbo in [False, True]:
26
+ model = fx.RandomConvSketch(n_kernels=16, kernel_lengths=(1, 5, 7, 9, 15),
27
+ fast_profile=turbo, random_state=312)
28
+ model.fit(x)
29
+ with patch.object(fx, '_cis_conv', None):
30
+ old = model.transform(x)
31
+ with warnings.catch_warnings():
32
+ warnings.simplefilter('ignore', RuntimeWarning)
33
+ out = model.transform(x)
34
+ np.testing.assert_array_equal(out, old)
35
+
36
+ def test_validation_and_fallback(self):
37
+ x = np.random.default_rng(874).normal(size=(3, 32, 3))
38
+ m = fx.RandomConvSketch(n_kernels=8).fit(x)
39
+ expected = m.transform(x)
40
+ with patch.object(fx, '_verified_conv_blas', return_value=False):
41
+ np.testing.assert_array_equal(m.transform(x), expected)
42
+ kernel = m.kernels_[0]
43
+ kernel['weights'] *= 0.
44
+ kernel['bias'] = 0.
45
+ m.invalidate_plan()
46
+ np.testing.assert_array_equal(m.transform(x)[:, :4], np.zeros((3,4)))
47
+ p = pickle.loads(pickle.dumps(m))
48
+ self.assertNotIn('_conv_plan', vars(p))
49
+ np.testing.assert_array_equal(m.transform(x), p.transform(x))
50
+ layout, weights, bias, _, _ = m._conv_plan[1]
51
+ for wrong in ('channel', 'length', 'stride', 'output', 'dtype'):
52
+ l = layout.copy()
53
+ if wrong == 'channel': l[0,3] = 4
54
+ if wrong == 'length': l[1,0] = 0
55
+ out = np.empty((3,8,5 if wrong == 'output' else 4))
56
+ with self.assertRaises((ValueError, BufferError)):
57
+ fx._cis_conv.transform(np.ascontiguousarray(x.transpose(0,2,1)),l,
58
+ weights.astype('f4') if wrong=='dtype' else weights,
59
+ bias,40 if wrong=='stride' else 1,False,False,out)
tests/test_engine.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pickle
2
+
3
+ import numpy as np
4
+ import unittest
5
+ from unittest.mock import patch
6
+ from contextlib import nullcontext
7
+ from numpy.testing import assert_allclose, assert_array_equal
8
+ from threadpoolctl import threadpool_limits
9
+
10
+ from wisp.cpu.algorithms import build_cpu_estimator
11
+ from wisp.cpu.hmm import TransitionSmoother
12
+ from wisp.registry import CANONICAL_METHOD_IDS
13
+ from wisp_release.methods import build_reference_family as original
14
+ from wisp_fast.decoder import FastSmoother
15
+ from wisp_fast.engine import accelerate_checkpoint, build_candidate, build_family, TurboBudget
16
+
17
+
18
+ def windows():
19
+ rng = np.random.default_rng(415)
20
+ X = rng.normal(size=(38, 42, 3))
21
+ y = np.resize([0, 1, 3], len(X))
22
+ return X[:27], y[:27], X[27:]
23
+
24
+
25
+ def check_full_recipe_and_converted_checkpoint(family):
26
+ X, y, Q = windows()
27
+ g, t = np.arange(len(X)) % 4, np.arange(len(X))[::-1]
28
+ gq, tq = np.arange(len(Q)) % 3, np.arange(len(Q))[::-1]
29
+ with threadpool_limits(1):
30
+ ref = original(family, seed=9).fit(X, y, groups=g, time_index=t, n_classes=5)
31
+ fast = build_family(family, seed=9).fit(X, y, groups=g, time_index=t, n_classes=5)
32
+ ref_p = ref.predict_proba(Q)
33
+ ref_y = ref.predict(Q, groups=gq, time_index=tq)
34
+ fast_from_ref = accelerate_checkpoint(ref)
35
+ for m in (fast, fast_from_ref):
36
+ p = m.predict_proba(Q)
37
+ assert_allclose(p, ref_p, atol=2e-10, rtol=1e-9)
38
+ assert_array_equal(p[:, [2, 4]], 0)
39
+ assert_array_equal(m.decode_proba(p, groups=gq, time_index=tq), ref_y)
40
+ assert_array_equal(m.predict(Q, batch_size=3, groups=gq, time_index=tq), ref_y)
41
+ assert_array_equal(pickle.loads(pickle.dumps(fast_from_ref)).predict(Q), ref.predict(Q))
42
+
43
+
44
+ def check_turbo_and_chronology(family):
45
+ X, y, Q = windows()
46
+ g, t = np.arange(len(X)) % 4, np.arange(len(X))[::-1]
47
+ b = TurboBudget(kernels=4, intervals=2, shapelets=2, trees=4, max_steps=28, max_train=12)
48
+ with threadpool_limits(1):
49
+ m = build_family(family, mode="turbo", budget=b).fit(X, y, groups=g, time_index=t, n_classes=5)
50
+ assert m.fit_windows_ == 12
51
+ ref_s = TransitionSmoother().fit(y, groups=g, time_index=t, n_classes=5)
52
+ assert_array_equal(m.smoother_.transition_, ref_s.transition_)
53
+ p = m.predict_proba(Q)
54
+ assert_allclose(p.sum(axis=1), 1)
55
+ assert_array_equal(p[:, [2, 4]], 0)
56
+
57
+
58
+ def check_custom_recipes():
59
+ X, y, Q = windows()
60
+ for name, params in [
61
+ ("har_sculpt_forest_hmm", dict(classifier="rf", segment_sizes=(3, 6), symbolic_bins=5)),
62
+ ("rocket_sketch_ridge_hmm", dict(classifier="logreg", n_kernels=11)),
63
+ ("symbolic_interval_forest", {}),
64
+ ("hybrid_cpu_ensemble", {}),
65
+ ("invariant_interval_shape_forest", dict(n_shapelets=3)),
66
+ ("state_rocket_interval_hmm", dict(n_kernels=9, n_intervals=4)),
67
+ ("posterior_cpu_ensemble", dict(weights=(0.0, 0.4, 0.6))),
68
+ ]:
69
+ spec = dict(name=name, route="cpu", params=dict(seed=33, n_jobs=1, use_hmm=False, **params))
70
+ with threadpool_limits(1):
71
+ ref = build_cpu_estimator(spec).fit(X, y)
72
+ fast = build_candidate(spec).fit(X, y)
73
+ assert_allclose(fast.predict_proba(Q), ref.predict_proba(Q), atol=2e-10, rtol=1e-9)
74
+
75
+
76
+ def check_transition_and_tie_order(mode):
77
+ rng = np.random.default_rng(911)
78
+ g = rng.integers(0, 5, 100) if mode != "none" else None
79
+ t = rng.integers(0, 10, 100) if mode == "time" else None
80
+ y = rng.integers(0, 4, 100)
81
+ a = TransitionSmoother().fit(y, groups=g, time_index=t, n_classes=6)
82
+ b = FastSmoother().fit(y, groups=g, time_index=t, n_classes=6)
83
+ assert_array_equal(a.prior_, b.prior_)
84
+ assert_array_equal(a.transition_, b.transition_)
85
+ for p in (rng.uniform(0, 1, (100, 6)), np.ones((100, 6)), np.zeros((100, 6))):
86
+ assert_array_equal(a.predict_from_proba(p, groups=g, time_index=t),
87
+ b.predict_from_proba(p, groups=g, time_index=t))
88
+
89
+ class EngineTests(unittest.TestCase):
90
+ def test_full_recipe_and_checkpoint(self):
91
+ for family in CANONICAL_METHOD_IDS:
92
+ with self.subTest(family=family):
93
+ check_full_recipe_and_converted_checkpoint(family)
94
+
95
+ def test_turbo_and_chronology(self):
96
+ for family in CANONICAL_METHOD_IDS:
97
+ with self.subTest(family=family):
98
+ check_turbo_and_chronology(family)
99
+
100
+ def test_custom_recipes(self):
101
+ check_custom_recipes()
102
+
103
+ def test_controlled_recipes(self):
104
+ from wisp.ablations import CIS_ABLATIONS, build_recipe as ref_builder
105
+ from wisp_fast.engine import build_recipe
106
+ X, y, Q = windows()
107
+ g, t = np.arange(len(X)) % 3, np.arange(len(X))[::-1]
108
+ qg, qt = np.arange(len(Q)) % 3, np.arange(len(Q))[::-1]
109
+ for recipe in CIS_ABLATIONS:
110
+ with self.subTest(recipe_id=recipe.recipe_id), threadpool_limits(1):
111
+ ref = ref_builder(recipe.recipe_id, seed=8, n_jobs=1)
112
+ fast = build_recipe(recipe.recipe_id, seed=8)
113
+ ref.fit(X, y, groups=g, time_index=t, n_classes=5)
114
+ fast.fit(X, y, groups=g, time_index=t, n_classes=5)
115
+ assert (fast.smoother_ is not None) == recipe.use_hmm
116
+ assert len(fast.blocks_) == sum((recipe.use_rocket, recipe.use_intervals, recipe.use_state))
117
+ assert type(ref.model_.named_steps["clf"]) is type(fast.model_.named_steps["clf"])
118
+ assert_allclose(ref.predict_proba(Q), fast.predict_proba(Q), atol=2e-10, rtol=1e-9)
119
+ assert_array_equal(ref.predict(Q, groups=qg, time_index=qt),
120
+ fast.predict(Q, groups=qg, time_index=qt))
121
+
122
+ def test_forest_kernel_and_fallback(self):
123
+ from sklearn.ensemble import ExtraTreesClassifier, RandomForestClassifier
124
+ from wisp_fast.forest import local_proba, ForestPlan
125
+ rng = np.random.default_rng(611)
126
+ X = rng.normal(size=(110, 9))
127
+ y = rng.choice([0, 2, 5], size=len(X))
128
+ weight = rng.uniform(0.1, 2, size=len(X))
129
+ Q = rng.normal(size=(27, 9))
130
+ Q[0, 2] = np.nan
131
+ for cls in (ExtraTreesClassifier, RandomForestClassifier):
132
+ with self.subTest(cls=cls):
133
+ model = cls(n_estimators=11, n_jobs=1, random_state=9,
134
+ min_samples_leaf=4).fit(X, y, sample_weight=weight)
135
+ ref = model.predict_proba(Q)
136
+ plan = ForestPlan(model)
137
+ assert_array_equal(local_proba(model, Q, plan), ref)
138
+ plan.table = None
139
+ assert_array_equal(local_proba(model, Q, plan), ref)
140
+
141
+ def test_decoder(self):
142
+ for backend in ("default", "numpy"):
143
+ ctx = patch("wisp_fast.decoder._native", lambda: None) if backend == "numpy" else nullcontext()
144
+ with ctx:
145
+ for mode in ("none", "group", "time"):
146
+ with self.subTest(backend=backend, mode=mode):
147
+ check_transition_and_tie_order(mode)
148
+
149
+
150
+ if __name__ == "__main__":
151
+ unittest.main()