fastmixture2 2.0.8__tar.gz → 2.0.9__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 (66) hide show
  1. {fastmixture2-2.0.8/fastmixture2.egg-info → fastmixture2-2.0.9}/PKG-INFO +1 -1
  2. fastmixture2-2.0.9/fastmixture2/__init__.py +1 -0
  3. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/fastmixture2/sqp.pyx +12 -4
  4. {fastmixture2-2.0.8 → fastmixture2-2.0.9/fastmixture2.egg-info}/PKG-INFO +1 -1
  5. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_cpu_scaling.py +154 -91
  6. fastmixture2-2.0.8/fastmixture2/__init__.py +0 -1
  7. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/LICENSE +0 -0
  8. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/MANIFEST.in +0 -0
  9. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/README.md +0 -0
  10. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/fastmixture2/defaults.py +0 -0
  11. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/fastmixture2/em.pyx +0 -0
  12. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/fastmixture2/fastmixture2.py +0 -0
  13. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/fastmixture2/functions.py +0 -0
  14. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/fastmixture2/gpu.py +0 -0
  15. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/fastmixture2/kernels.py +0 -0
  16. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/fastmixture2/main.py +0 -0
  17. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/fastmixture2/packed.pxd +0 -0
  18. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/fastmixture2/plink.py +0 -0
  19. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/fastmixture2/shared.pyx +0 -0
  20. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/fastmixture2/svd.pyx +0 -0
  21. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/fastmixture2/threads.pxd +0 -0
  22. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/fastmixture2/utils.py +0 -0
  23. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/fastmixture2.egg-info/SOURCES.txt +0 -0
  24. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/fastmixture2.egg-info/dependency_links.txt +0 -0
  25. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/fastmixture2.egg-info/entry_points.txt +0 -0
  26. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/fastmixture2.egg-info/requires.txt +0 -0
  27. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/fastmixture2.egg-info/top_level.txt +0 -0
  28. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/pyproject.toml +0 -0
  29. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/setup.cfg +0 -0
  30. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/setup.py +0 -0
  31. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/check_portable.py +0 -0
  32. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/helpers.py +0 -0
  33. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/run_tests.py +0 -0
  34. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_adversarial_io.py +0 -0
  35. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_bootstrap.py +0 -0
  36. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_cli.py +0 -0
  37. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_convergence.py +0 -0
  38. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_cpu_subset.py +0 -0
  39. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_cross_validation.py +0 -0
  40. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_distribution.py +0 -0
  41. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_frequency_cache.py +0 -0
  42. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_genotypes.py +0 -0
  43. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_gpu_called.py +0 -0
  44. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_gpu_em.py +0 -0
  45. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_gpu_gl_precision.py +0 -0
  46. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_gpu_large.py +0 -0
  47. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_gpu_modes.py +0 -0
  48. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_gpu_precision.py +0 -0
  49. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_gpu_safety.py +0 -0
  50. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_gpu_stat_reuse.py +0 -0
  51. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_gpu_streamed.py +0 -0
  52. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_gpu_subset.py +0 -0
  53. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_gpu_supervision.py +0 -0
  54. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_gpu_svd_cache.py +0 -0
  55. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_initialization.py +0 -0
  56. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_likelihoods.py +0 -0
  57. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_numerical_equivalence.py +0 -0
  58. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_optimizers.py +0 -0
  59. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_plink_files.py +0 -0
  60. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_q_constraints.py +0 -0
  61. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_restarts.py +0 -0
  62. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_sqp_scoring.py +0 -0
  63. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_sqp_solver.py +0 -0
  64. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_subset_modes.py +0 -0
  65. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/test_threads.py +0 -0
  66. {fastmixture2-2.0.8 → fastmixture2-2.0.9}/tests/validate_distribution.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fastmixture2
3
- Version: 2.0.8
3
+ Version: 2.0.9
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
@@ -0,0 +1 @@
1
+ __version__ = "2.0.9"
@@ -715,6 +715,14 @@ def _blasSetter(fn):
715
715
  return setThreads
716
716
 
717
717
 
718
+ ### Balance sample tiles across worker waves
719
+ cdef inline int _sampleTile(Py_ssize_t N, int nt) noexcept nogil:
720
+ cdef Py_ssize_t n = nt
721
+ if nt > 1:
722
+ n *= max(1, (N + 64 * nt - 1) // (64 * nt))
723
+ return min(64, max(8, ((N + n - 1) // n + 7) // 8 * 8))
724
+
725
+
718
726
  ### Bind SciPy BLAS lazily and bound the tile scratch across workers
719
727
  def blasWorkspace(
720
728
  int K, int M, int N, bint fixed=False,
@@ -728,7 +736,7 @@ def blasWorkspace(
728
736
  cdef:
729
737
  int kp = K * (K + 1) // 2
730
738
  int nt = max(1, omp.omp_get_max_threads())
731
- int ti = min(64, max(8, ((<Py_ssize_t>N + nt - 1) // nt + 7) // 8 * 8))
739
+ int ti = _sampleTile(N, nt)
732
740
  int tiles = (<Py_ssize_t>N + ti - 1) // ti
733
741
  Py_ssize_t width = 2 * 64 * 64 + 64 * kp + 64 * (kp + K) + 64 * K
734
742
  object cap, info
@@ -1000,7 +1008,7 @@ def updateBlas(
1000
1008
  return 0.0 if score else None
1001
1009
  ms, ws = &markers[0], &weights[0]
1002
1010
  if q_side:
1003
- ti = min(64, max(8, ((<Py_ssize_t>N + nt - 1) // nt + 7) // 8 * 8))
1011
+ ti = _sampleTile(N, nt)
1004
1012
  owner = ti if q_side else tj
1005
1013
  source = tj if q_side else ti
1006
1014
  tiles = ((<Py_ssize_t>N if q_side else <Py_ssize_t>M) + owner - 1) // owner
@@ -1015,9 +1023,9 @@ def updateBlas(
1015
1023
  if work.shape[1] < width:
1016
1024
  raise ValueError("BLAS workspace is smaller than one tile")
1017
1025
 
1018
- # Keep high-K products and worker scratch within 64 MiB
1026
+ # Cache reused products within the worker memory budget
1019
1027
  nr = P.shape[0] if q_side else N
1020
- if K >= 16 and ms == NULL and (
1028
+ if tiles > 1 and (not q_side or (K >= 16 and ms == NULL)) and (
1021
1029
  8 * (nr * <Py_ssize_t>kp + work.shape[0] * work.shape[1]) <= 64 << 20
1022
1030
  ):
1023
1031
  import numpy as np
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fastmixture2
3
- Version: 2.0.8
3
+ Version: 2.0.9
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
@@ -469,105 +469,168 @@ class BlasTest(unittest.TestCase):
469
469
  self.assertEqual(events, ["MADV_RANDOM", "q", "MADV_NORMAL"])
470
470
 
471
471
  def test_outer_products_are_fresh_and_match_allocation_fallback(self):
472
- for k in (1, 5, 16, 32, 64, 128):
473
- G, P, Q, _ = makeEmFixture("packed", 139, 137, k)
474
- G["data"].flags.writeable = False
475
- pool, work = sqp.blasWorkspace(k, P.shape[0], Q.shape[0])
476
- B = functions.simplexBasis(k)
477
- for q_side in (False, True):
478
- shape = Q.shape if q_side else P.shape
479
- out, ref = np.empty(shape), np.empty(shape)
480
- H, H0 = np.empty((*shape, k)), np.empty((*shape, k))
481
- g, g0 = np.empty(shape), np.empty(shape)
482
- for changed in (False, True):
483
- if changed:
484
- P[:] = 1.0 - P
485
- Q[:] = np.roll(Q, 1, axis=1)
486
- P.flags.writeable = Q.flags.writeable = False
472
+ for kind in ("packed", "likelihood"):
473
+ for k in (1, 2, 5, 10, 16, 32, 64, 128):
474
+ G, P, Q, _ = makeEmFixture(kind, 139, 137, k)
475
+ G["data"].flags.writeable = False
476
+ gl = kernels.isLikelihood(G)
477
+ data = (None, G["data"]) if gl else (G["data"], None)
478
+ pool, work = sqp.blasWorkspace(k, P.shape[0], Q.shape[0])
479
+ B = functions.simplexBasis(k)
480
+ for q_side in (False, True):
481
+ shape = Q.shape if q_side else P.shape
482
+ out, ref = np.empty(shape), np.empty(shape)
483
+ H, H0 = np.empty((*shape, k)), np.empty((*shape, k))
484
+ g, g0 = np.empty(shape), np.empty(shape)
485
+ for changed in (False, True):
486
+ if changed:
487
+ P[:] = 1.0 - P
488
+ Q[:] = np.roll(Q, 1, axis=1)
489
+ P.flags.writeable = Q.flags.writeable = False
490
+ with (
491
+ self.subTest(kind=kind, K=k, q_side=q_side, changed=changed),
492
+ pool.limit(limits=1, user_api="blas"),
493
+ ):
494
+ with patch.object(np, "empty", wraps=np.empty) as alloc:
495
+ sqp.updateBlas(
496
+ *data, P, Q, out, H, g, B, work, q_side, gl, Q.shape[0]
497
+ )
498
+ nr = P.shape[0] if q_side else Q.shape[0]
499
+ size = nr * k * (k + 1) // 2 * 8
500
+ cached = (not q_side or k >= 16) and work.nbytes + size <= 64 << 20
501
+ self.assertEqual(alloc.call_count, int(cached))
502
+ if cached:
503
+ self.assertEqual(alloc.call_args.args[0], (nr, k * (k + 1) // 2))
504
+ with patch.object(np, "empty", side_effect=MemoryError):
505
+ sqp.updateBlas(
506
+ *data, P, Q, ref, H0, g0, B, work, q_side, gl, Q.shape[0]
507
+ )
508
+ np.testing.assert_allclose(H, H0, atol=3e-12, rtol=3e-12)
509
+ np.testing.assert_allclose(g, g0, atol=3e-12, rtol=3e-12)
510
+ np.testing.assert_allclose(out, ref, atol=3e-12, rtol=3e-12)
511
+ P.flags.writeable = Q.flags.writeable = True
512
+
513
+ def test_outer_products_respect_worker_memory_and_weighted_rows(self):
514
+ for kind in ("packed", "likelihood"):
515
+ for k in (5, 16):
516
+ G, P, Q, _ = makeEmFixture(kind, 139, 137, k)
517
+ gl = kernels.isLikelihood(G)
518
+ data = (None, G["data"]) if gl else (G["data"], None)
519
+ pool, work = sqp.blasWorkspace(k, P.shape[0], Q.shape[0])
520
+ B = functions.simplexBasis(k)
521
+ wide = np.empty((work.shape[0], (64 << 20) // (8 * work.shape[0]) + 1))
522
+ ids = np.arange(0, P.shape[0], 2, dtype=np.uint32)
523
+ weights = np.arange(ids.size, dtype=np.uint32) % 3
524
+ p = P.copy()
525
+ for q_side in (False, True):
526
+ P = p.copy()
527
+ shape = Q.shape if q_side else P.shape
528
+ out, ref = np.empty(shape), np.empty(shape)
529
+ H, H0 = np.empty((*shape, k)), np.empty((*shape, k))
530
+ g, g0 = np.empty(shape), np.empty(shape)
487
531
  with (
488
- self.subTest(K=k, q_side=q_side, changed=changed),
532
+ self.subTest(kind=kind, K=k, q_side=q_side),
489
533
  pool.limit(limits=1, user_api="blas"),
490
534
  ):
491
- with patch.object(np, "empty", wraps=np.empty) as alloc:
492
- sqp.updateBlas(
493
- G["data"], None, P, Q, out, H, g, B, work, q_side, False, Q.shape[0]
494
- )
495
- nr = P.shape[0] if q_side else Q.shape[0]
496
- size = nr * k * (k + 1) // 2 * 8
497
- cached = k >= 16 and work.nbytes + size <= 64 << 20
498
- self.assertEqual(alloc.call_count, int(cached))
499
- if cached:
500
- self.assertEqual(alloc.call_args.args[0], (nr, k * (k + 1) // 2))
501
- with patch.object(np, "empty", side_effect=MemoryError):
502
- sqp.updateBlas(
503
- G["data"],
504
- None,
505
- P,
506
- Q,
507
- ref,
508
- H0,
509
- g0,
510
- B,
511
- work,
512
- q_side,
513
- False,
514
- Q.shape[0],
515
- )
535
+ sqp.updateBlas(*data, P, Q, ref, H0, g0, B, work, q_side, gl, Q.shape[0])
536
+ with patch.object(np, "empty", side_effect=AssertionError("Cache budget")):
537
+ sqp.updateBlas(*data, P, Q, out, H, g, B, wide, q_side, gl, Q.shape[0])
516
538
  np.testing.assert_allclose(H, H0, atol=3e-12, rtol=3e-12)
517
539
  np.testing.assert_allclose(g, g0, atol=3e-12, rtol=3e-12)
518
540
  np.testing.assert_allclose(out, ref, atol=3e-12, rtol=3e-12)
519
- P.flags.writeable = Q.flags.writeable = True
541
+ P[ids[weights == 0]] = np.nan
542
+ P.flags.writeable = Q.flags.writeable = False
543
+ for cached in (True, False):
544
+ with patch.object(
545
+ np,
546
+ "empty",
547
+ wraps=np.empty,
548
+ side_effect=None if cached else MemoryError,
549
+ ) as alloc:
550
+ sqp.updateBlas(
551
+ *data,
552
+ P,
553
+ Q,
554
+ out,
555
+ H,
556
+ g,
557
+ B,
558
+ work,
559
+ q_side,
560
+ gl,
561
+ Q.shape[0],
562
+ markers=ids,
563
+ weights=weights,
564
+ )
565
+ self.assertEqual(alloc.call_count, int(not q_side))
566
+ active = np.arange(Q.shape[0]) if q_side else ids[weights > 0]
567
+ self.assertTrue(np.isfinite(out[active]).all())
568
+ if cached:
569
+ ref[:], H0[:], g0[:] = out, H, g
570
+ else:
571
+ np.testing.assert_allclose(
572
+ H[active], H0[active], atol=3e-12, rtol=3e-12
573
+ )
574
+ np.testing.assert_allclose(
575
+ g[active], g0[active], atol=3e-12, rtol=3e-12
576
+ )
577
+ np.testing.assert_allclose(
578
+ out[active], ref[active], atol=3e-12, rtol=3e-12
579
+ )
580
+ P.flags.writeable = Q.flags.writeable = True
520
581
 
521
- def test_outer_products_respect_worker_memory_and_weighted_rows(self):
522
- k = 16
523
- G, P, Q, _ = makeEmFixture("packed", 139, 137, k)
524
- pool, work = sqp.blasWorkspace(k, P.shape[0], Q.shape[0])
525
- B = functions.simplexBasis(k)
526
- wide = np.empty((work.shape[0], (64 << 20) // (8 * work.shape[0]) + 1))
527
- ids = np.arange(0, P.shape[0], 2, dtype=np.uint32)
528
- weights = np.arange(ids.size, dtype=np.uint32) % 3
529
- p = P.copy()
530
- for q_side in (False, True):
531
- P = p.copy()
532
- shape = Q.shape if q_side else P.shape
533
- out, ref = np.empty(shape), np.empty(shape)
534
- H, H0 = np.empty((*shape, k)), np.empty((*shape, k))
535
- g, g0 = np.empty(shape), np.empty(shape)
536
- with self.subTest(q_side=q_side), pool.limit(limits=1, user_api="blas"):
537
- sqp.updateBlas(
538
- G["data"], None, P, Q, ref, H0, g0, B, work, q_side, False, Q.shape[0]
539
- )
540
- with patch.object(np, "empty", side_effect=AssertionError("Cache exceeded budget")):
541
- sqp.updateBlas(
542
- G["data"], None, P, Q, out, H, g, B, wide, q_side, False, Q.shape[0]
543
- )
544
- np.testing.assert_allclose(H, H0, atol=3e-12, rtol=3e-12)
545
- np.testing.assert_allclose(g, g0, atol=3e-12, rtol=3e-12)
546
- np.testing.assert_allclose(out, ref, atol=3e-12, rtol=3e-12)
547
- P[ids[weights == 0]] = np.nan
548
- P.flags.writeable = Q.flags.writeable = False
549
- with patch.object(
550
- np, "empty", side_effect=AssertionError("Weighted cache allocated")
582
+ def test_single_tile_skips_unused_product_cache(self):
583
+ for k in (5, 16):
584
+ for q_side in (False, True):
585
+ m, n = (139, 7) if q_side else (63, 137)
586
+ G, P, Q, _ = makeEmFixture("packed", m, n, k)
587
+ pool, work = sqp.blasWorkspace(k, m, n)
588
+ B = functions.simplexBasis(k)
589
+ shape = Q.shape if q_side else P.shape
590
+ out, H, g = np.empty(shape), np.empty((*shape, k)), np.empty(shape)
591
+ with (
592
+ self.subTest(K=k, q_side=q_side),
593
+ pool.limit(limits=1, user_api="blas"),
594
+ patch.object(np, "empty", side_effect=AssertionError("Unused cache")),
551
595
  ):
552
- sqp.updateBlas(
553
- G["data"],
554
- None,
555
- P,
556
- Q,
557
- out,
558
- H,
559
- g,
560
- B,
561
- work,
562
- q_side,
563
- False,
564
- Q.shape[0],
565
- markers=ids,
566
- weights=weights,
567
- )
568
- active = np.arange(Q.shape[0]) if q_side else ids[weights > 0]
569
- self.assertTrue(np.isfinite(out[active]).all())
570
- P.flags.writeable = Q.flags.writeable = True
596
+ sqp.updateBlas(G["data"], None, P, Q, out, H, g, B, work, q_side, False, n)
597
+ self.assertTrue(np.isfinite(out).all())
598
+
599
+ def test_balanced_q_tiles_preserve_sharded_scores_and_statistics(self):
600
+ for n in (157, 626, 2504):
601
+ G, P, Q, _ = makeEmFixture("packed", 131, n, 5)
602
+ G["data"].flags.writeable = False
603
+ pool, work = sqp.blasWorkspace(5, 131, n, fixed=True, score=True)
604
+ B = functions.simplexBasis(5)
605
+ result = []
606
+ for c in (n, 53):
607
+ out, H, g = np.empty_like(Q), np.empty((n, 5, 5)), np.empty_like(Q)
608
+ ll = 0.0
609
+ with pool.limit(limits=1, user_api="blas"):
610
+ for b in range(0, n, c):
611
+ e = min(b + c, n)
612
+ ll += sqp.updateBlas(
613
+ G["data"],
614
+ None,
615
+ P,
616
+ Q[b:e],
617
+ out[b:e],
618
+ H[b:e],
619
+ g[b:e],
620
+ B,
621
+ work,
622
+ True,
623
+ False,
624
+ n,
625
+ n0=b,
626
+ score=True,
627
+ )
628
+ result.append((out, H, g))
629
+ np.testing.assert_allclose(
630
+ ll, kernels.loglikeMissing(G, P, Q), atol=2e-8, rtol=2e-13
631
+ )
632
+ for a, b in zip(*result, strict=True):
633
+ np.testing.assert_allclose(a, b, atol=2e-10, rtol=2e-10)
571
634
 
572
635
  def test_all_components_and_panel_sizes_use_blas(self):
573
636
  for k in (2, 3, 4, 5, 6, 7, 8, 16, 128):
@@ -1 +0,0 @@
1
- __version__ = "2.0.8"
File without changes
File without changes
File without changes
File without changes
File without changes