fastmixture2 2.0.6__tar.gz → 2.0.8__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. {fastmixture2-2.0.6/fastmixture2.egg-info → fastmixture2-2.0.8}/PKG-INFO +4 -3
  2. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/README.md +3 -2
  3. fastmixture2-2.0.8/fastmixture2/__init__.py +1 -0
  4. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/fastmixture2/defaults.py +8 -0
  5. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/fastmixture2/em.pyx +36 -31
  6. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/fastmixture2/functions.py +13 -22
  7. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/fastmixture2/gpu.py +278 -77
  8. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/fastmixture2/kernels.py +15 -13
  9. fastmixture2-2.0.8/fastmixture2/plink.py +142 -0
  10. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/fastmixture2/shared.pyx +134 -22
  11. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/fastmixture2/svd.pyx +30 -7
  12. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/fastmixture2/utils.py +66 -39
  13. {fastmixture2-2.0.6 → fastmixture2-2.0.8/fastmixture2.egg-info}/PKG-INFO +4 -3
  14. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/check_portable.py +4 -0
  15. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_adversarial_io.py +25 -0
  16. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_cli.py +7 -0
  17. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_cpu_scaling.py +77 -5
  18. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_cpu_subset.py +62 -4
  19. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_genotypes.py +93 -1
  20. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_gpu_called.py +19 -5
  21. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_gpu_gl_precision.py +17 -10
  22. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_gpu_modes.py +11 -2
  23. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_gpu_streamed.py +222 -0
  24. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_gpu_subset.py +85 -2
  25. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_gpu_svd_cache.py +4 -8
  26. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_initialization.py +2 -2
  27. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_likelihoods.py +20 -0
  28. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_numerical_equivalence.py +3 -5
  29. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_plink_files.py +74 -71
  30. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_subset_modes.py +49 -1
  31. fastmixture2-2.0.6/fastmixture2/__init__.py +0 -1
  32. fastmixture2-2.0.6/fastmixture2/plink.py +0 -148
  33. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/LICENSE +0 -0
  34. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/MANIFEST.in +0 -0
  35. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/fastmixture2/fastmixture2.py +0 -0
  36. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/fastmixture2/main.py +0 -0
  37. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/fastmixture2/packed.pxd +0 -0
  38. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/fastmixture2/sqp.pyx +0 -0
  39. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/fastmixture2/threads.pxd +0 -0
  40. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/fastmixture2.egg-info/SOURCES.txt +0 -0
  41. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/fastmixture2.egg-info/dependency_links.txt +0 -0
  42. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/fastmixture2.egg-info/entry_points.txt +0 -0
  43. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/fastmixture2.egg-info/requires.txt +0 -0
  44. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/fastmixture2.egg-info/top_level.txt +0 -0
  45. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/pyproject.toml +0 -0
  46. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/setup.cfg +0 -0
  47. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/setup.py +0 -0
  48. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/helpers.py +0 -0
  49. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/run_tests.py +0 -0
  50. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_bootstrap.py +0 -0
  51. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_convergence.py +0 -0
  52. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_cross_validation.py +0 -0
  53. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_distribution.py +0 -0
  54. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_frequency_cache.py +0 -0
  55. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_gpu_em.py +0 -0
  56. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_gpu_large.py +0 -0
  57. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_gpu_precision.py +0 -0
  58. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_gpu_safety.py +0 -0
  59. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_gpu_stat_reuse.py +0 -0
  60. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_gpu_supervision.py +0 -0
  61. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_optimizers.py +0 -0
  62. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_q_constraints.py +0 -0
  63. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_restarts.py +0 -0
  64. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_sqp_scoring.py +0 -0
  65. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_sqp_solver.py +0 -0
  66. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/test_threads.py +0 -0
  67. {fastmixture2-2.0.6 → fastmixture2-2.0.8}/tests/validate_distribution.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fastmixture2
3
- Version: 2.0.6
3
+ Version: 2.0.8
4
4
  Summary: Biobank-scale ancestry estimation in unrelated individuals
5
5
  Author-email: "J. Meisner" <meisnerucph@gmail.com>, "L. Thunegard" <lisathunegard@icloud.com>
6
6
  License-Expression: GPL-3.0-only
@@ -158,8 +158,9 @@ rescue and float64 likelihood checks. CPU fitting uses float64. CUDA uses
158
158
  mixed precision followed by float64 refinement.
159
159
 
160
160
  Processing uses bounded tiles. Total memory still grows with panel size and K.
161
- Temporary files use `TMPDIR`. For large panels, point it to disk-backed scratch
162
- with sufficient space.
161
+ Initialization subsets use memory-mapped temporary files when needed. Temporary
162
+ files use `TMPDIR`. For large panels, point it to disk-backed scratch with
163
+ sufficient space.
163
164
 
164
165
  The defaults are `--threads 1`, `--seed 42`, `--iter 1000` and `--tole 1e-10`.
165
166
  `--threads` also controls CPU work during GPU fits.
@@ -127,8 +127,9 @@ rescue and float64 likelihood checks. CPU fitting uses float64. CUDA uses
127
127
  mixed precision followed by float64 refinement.
128
128
 
129
129
  Processing uses bounded tiles. Total memory still grows with panel size and K.
130
- Temporary files use `TMPDIR`. For large panels, point it to disk-backed scratch
131
- with sufficient space.
130
+ Initialization subsets use memory-mapped temporary files when needed. Temporary
131
+ files use `TMPDIR`. For large panels, point it to disk-backed scratch with
132
+ sufficient space.
132
133
 
133
134
  The defaults are `--threads 1`, `--seed 42`, `--iter 1000` and `--tole 1e-10`.
134
135
  `--threads` also controls CPU work during GPU fits.
@@ -0,0 +1 @@
1
+ __version__ = "2.0.8"
@@ -100,6 +100,14 @@ def sqpMarkerChunk(M, K, chunk=SQP_MARKER_CHUNK):
100
100
  return max(1, min(M, chunk, SQP_MARKER_WORKSPACE_BYTES // b))
101
101
 
102
102
 
103
+ ### Bound CPU sample shards
104
+ def sqpSampleChunk(N, K, chunk=None):
105
+ if chunk is None:
106
+ chunk = 16_384 if K <= 16 else SQP_SAMPLE_CHUNK
107
+ n = max(1, SQP_SAMPLE_WORKSPACE_BYTES // (8 * K * (K + 1)))
108
+ return min(N, chunk, n)
109
+
110
+
103
111
  ### Bound the CUDA tile size
104
112
  def gpuTileBytes(size, limit=GPU_TILE_BYTES):
105
113
  return min(limit, max(GPU_MIN_TILE_BYTES, size // 8))
@@ -27,6 +27,10 @@ cdef extern from *:
27
27
  #include <stddef.h>
28
28
  #include <stdint.h>
29
29
 
30
+ static inline void fm_em_barrier(void) {
31
+ #pragma omp barrier
32
+ }
33
+
30
34
  // Select vectorized portable EM rows
31
35
  #if defined(FASTMIXTURE2_DISPATCH) && defined(__GLIBC__) && \
32
36
  (defined(__x86_64__) || defined(__i386__)) && \
@@ -511,6 +515,7 @@ cdef extern from *:
511
515
  void fm_q_weight_15(const f64*, const f64*, f64*, const u8*, size_t, u32) noexcept nogil
512
516
  void fm_q_weight_16(const f64*, const f64*, f64*, const u8*, size_t, u32) noexcept nogil
513
517
  size_t fm_em_workspace_bytes(size_t, size_t) noexcept nogil
518
+ void fm_em_barrier() noexcept nogil
514
519
 
515
520
  cdef const f64 PRO_MIN = 1e-5
516
521
  cdef const f64 PRO_MAX = 1.0 - 1e-5
@@ -1733,7 +1738,6 @@ cdef int _tiledEm(
1733
1738
  Py_ssize_t n_blk = min(4096, max(16, _EM_STATS_TILE_BYTES // (8 * K)))
1734
1739
  Py_ssize_t mb, nb, b, e, beg, end, j, marker, z, i, k, x, r
1735
1740
  int tid, n_thr, n_use
1736
- int used[1]
1737
1741
  bint fixed = P1 is None
1738
1742
  bint masked = fold >= 0
1739
1743
  bint weighted = weights is not None
@@ -1757,32 +1761,28 @@ cdef int _tiledEm(
1757
1761
  pa = pb = NULL
1758
1762
  n_use = min(N, omp.omp_get_max_threads())
1759
1763
  n_thr = n_use
1760
- used[0] = n_use
1761
1764
  g_all = <u8*>calloc(n_use * n_blk, sizeof(u8)) if not gl else NULL
1762
1765
  p_all = NULL if fixed else <f64*>calloc(n_use * c * 2 * K, sizeof(f64))
1763
1766
  if (not gl and g_all is NULL) or (not fixed and p_all is NULL):
1764
1767
  free(g_all)
1765
1768
  free(p_all)
1766
1769
  _memoryError()
1767
- for mb in range((M + c - 1) // c):
1768
- b = mb * c
1769
- e = min(b + c, M)
1770
- with nogil, parallel(num_threads=n_use):
1771
- tid = omp.omp_get_thread_num()
1772
- n_thr = omp.omp_get_num_threads()
1773
-
1774
- # Initialize thread-local scratch before storage dispatch
1775
- g = NULL
1776
- g_buf = NULL
1777
- pa = pb = NULL
1778
- sa = sb = 0.0
1779
- if tid == 0:
1780
- used[0] = n_thr
1781
- beg = N * tid // n_thr
1782
- end = N * (tid + 1) // n_thr
1783
- if not gl:
1784
- g_buf = &g_all[tid * n_blk]
1785
- g = g_buf
1770
+ with nogil, parallel(num_threads=n_use):
1771
+ # Reuse workers and buffers across marker blocks
1772
+ tid = omp.omp_get_thread_num()
1773
+ n_thr = omp.omp_get_num_threads()
1774
+ g = NULL
1775
+ g_buf = NULL
1776
+ pa = pb = NULL
1777
+ sa = sb = 0.0
1778
+ beg = N * tid // n_thr
1779
+ end = N * (tid + 1) // n_thr
1780
+ if not gl:
1781
+ g_buf = &g_all[tid * n_blk]
1782
+ g = g_buf
1783
+ for mb in range((M + c - 1) // c):
1784
+ b = mb * c
1785
+ e = min(b + c, M)
1786
1786
  if not fixed:
1787
1787
  for x in range(c * 2 * K):
1788
1788
  p_all[tid * c * 2 * K + x] = 0.0
@@ -1846,16 +1846,21 @@ cdef int _tiledEm(
1846
1846
  fm_em_partial_generic(
1847
1847
  &P[marker, 0], &Q[z, 0], &T[z, 0], g, r, pa, pb, w, K
1848
1848
  )
1849
- if not fixed:
1850
- for j in prange(e - b, schedule="static", num_threads=min(n_use, e - b)):
1851
- marker = b + j if s is None else s[b + j]
1852
- for k in range(2 * K):
1853
- for tid in range(1, used[0]):
1854
- p_all[j * 2 * K + k] += p_all[(tid * c + j) * 2 * K + k]
1855
- _outerAccelP(
1856
- &P[marker, 0], &P1[marker, 0],
1857
- &p_all[j * 2 * K], &p_all[j * 2 * K + K], K
1858
- )
1849
+ if not fixed:
1850
+ # Reduce P in a fixed worker order
1851
+ fm_em_barrier()
1852
+ for j in range((e - b) * tid // n_thr, (e - b) * (tid + 1) // n_thr):
1853
+ marker = b + j if s is None else s[b + j]
1854
+ for k in range(2 * K):
1855
+ for i in range(1, n_thr):
1856
+ p_all[j * 2 * K + k] += p_all[(i * c + j) * 2 * K + k]
1857
+ _outerAccelP(
1858
+ &P[marker, 0], &P1[marker, 0],
1859
+ &p_all[j * 2 * K], &p_all[j * 2 * K + K], K
1860
+ )
1861
+
1862
+ # Keep sample shards on the same marker block
1863
+ fm_em_barrier()
1859
1864
  free(g_all)
1860
1865
  free(p_all)
1861
1866
  return 0
@@ -447,21 +447,10 @@ def simplexBasis(K):
447
447
 
448
448
 
449
449
  ### Allocate bounded SQP workspaces
450
- def sqpState(
451
- P,
452
- Q,
453
- m_chunk=defaults.SQP_MARKER_CHUNK,
454
- n_chunk=defaults.SQP_SAMPLE_CHUNK,
455
- fixed=False,
456
- score=False,
457
- ):
450
+ def sqpState(P, Q, m_chunk=defaults.SQP_MARKER_CHUNK, n_chunk=None, fixed=False, score=False):
458
451
  K = defaults.validateK(Q.shape[1])
459
452
  m_chunk = defaults.sqpMarkerChunk(P.shape[0], K, m_chunk)
460
-
461
- # Bound Q statistics independently of panel size
462
- b = (K * K + K) * 8
463
- N = max(1, defaults.SQP_SAMPLE_WORKSPACE_BYTES // b)
464
- n_chunk = min(Q.shape[0], n_chunk, N)
453
+ n_chunk = defaults.sqpSampleChunk(Q.shape[0], K, n_chunk)
465
454
 
466
455
  # Omit P curvature buffers in projection mode
467
456
  S = {
@@ -524,7 +513,7 @@ def sqpMap(G, P, Q, P_out, Q_out, S, y=None, markers=None, weights=None, score=F
524
513
  and (
525
514
  not g.flags.writeable
526
515
  if rows is None
527
- else all(not x.flags.writeable for x in G["data"].parts)
516
+ else all(not x.flags.writeable for x in G["data"]["parts"])
528
517
  )
529
518
  )
530
519
  if hint:
@@ -532,8 +521,14 @@ def sqpMap(G, P, Q, P_out, Q_out, S, y=None, markers=None, weights=None, score=F
532
521
  try:
533
522
  for b in range(0, 0 if reuse else stop, c):
534
523
  e = min(b + c, stop)
535
- if y is not None and y.ndim == 1 and np.all(y[b:e]):
536
- continue
524
+ if y is not None and y.ndim == 1:
525
+ # Trim fixed rows at shard boundaries
526
+ u = y[b:e]
527
+ if np.all(u):
528
+ continue
529
+ if u[0] or u[-1]:
530
+ v = u == 0
531
+ b, e = b + v.argmax(), e - v[::-1].argmax()
537
532
  q = e - b
538
533
  if touch:
539
534
  shared.prefaultPacked(g, b, e, rows=rows)
@@ -675,7 +670,7 @@ def sqpRun(G, P, Q, q_nrm, y, run, L0=None, fixed=False, subset=False):
675
670
  E,
676
671
  Q,
677
672
  run.get("m_chunk", defaults.SQP_MARKER_CHUNK),
678
- run.get("n_chunk", defaults.SQP_SAMPLE_CHUNK),
673
+ run.get("n_chunk"),
679
674
  fixed=fixed,
680
675
  score=(
681
676
  y is None
@@ -876,11 +871,7 @@ def likelihoodRun(G, P, Q, q_nrm, y, run, L0=None, fixed=False, subset=False):
876
871
  T = None
877
872
 
878
873
  S = sqpState(
879
- P,
880
- Q,
881
- run.get("m_chunk", defaults.SQP_MARKER_CHUNK),
882
- run.get("n_chunk", defaults.SQP_SAMPLE_CHUNK),
883
- fixed=fixed,
874
+ P, Q, run.get("m_chunk", defaults.SQP_MARKER_CHUNK), run.get("n_chunk"), fixed=fixed
884
875
  )
885
876
  S["Z"] = zalState(P[:0] if fixed else P, Q)
886
877
  S["tol"] = tole * nrm