Skip to content

Commit 1625a15

Browse files
committed
Add line profiler
1 parent 52dadd5 commit 1625a15

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
lines changed

cfpq_decomposer/abstract_decomposer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Tuple
33

44
import graphblas
5+
import line_profiler
56
from graphblas.core.dtypes import BOOL
67
from graphblas.core.matrix import Matrix
78

@@ -32,6 +33,7 @@ def column_based_decompose(self, matrix: Matrix):
3233
left_transposed, right_transposed = self.row_based_decompose(matrix.T.new())
3334
return right_transposed.T.new(), left_transposed.T.new()
3435

36+
@line_profiler.profile
3537
def decompose(self, matrix: Matrix) -> Tuple[Matrix, Matrix]:
3638
residual = matrix
3739
if residual.nvals == 0:

cfpq_decomposer/high_performance_decomposer.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from dataclasses import dataclass
22
from typing import Tuple, Any
33

4+
import line_profiler
45
import numpy as np
56
import numpy.typing as npt
67
from graphblas import semiring
@@ -34,12 +35,14 @@ def rows_in_buckets(self):
3435

3536

3637
class HighPerformanceDecomposer(AbstractDecomposer):
38+
@line_profiler.profile
3739
def row_based_decompose(self, matrix: Matrix) -> Tuple[Matrix, Matrix]:
3840
build_left_factor_result = self._build_left_factor(matrix)
3941
right_factor = self._build_right_factor(matrix, build_left_factor_result)
4042
return build_left_factor_result.left_factor, right_factor
4143

4244
@staticmethod
45+
@line_profiler.profile
4346
def _build_left_factor(matrix: Matrix) -> BuildLeftFactorResult:
4447
num_rows, num_cols = matrix.shape
4548

@@ -60,11 +63,13 @@ def _build_left_factor(matrix: Matrix) -> BuildLeftFactorResult:
6063
return BuildLeftFactorResult(left_factor, bucket_sizes)
6164

6265
@staticmethod
66+
@line_profiler.profile
6367
def _group_rows_to_buckets(matrix: Matrix) -> RowGroupingResult:
6468
row_hashes = HighPerformanceDecomposer._compute_row_hashes(matrix)
6569
return HighPerformanceDecomposer._group_row_hashes_to_buckets(row_hashes)
6670

6771
@staticmethod
72+
@line_profiler.profile
6873
def _compute_row_hashes(matrix: Matrix) -> npt.NDArray[Any]:
6974
row_signatures_matrix = HighPerformanceDecomposer._compute_row_signatures_matrix(matrix)
7075
hash_weights = np.random.default_rng().integers(
@@ -77,6 +82,7 @@ def _compute_row_hashes(matrix: Matrix) -> npt.NDArray[Any]:
7782
return row_hashes
7883

7984
@staticmethod
85+
@line_profiler.profile
8086
def _compute_row_signatures_matrix(matrix: Matrix) -> Matrix:
8187
num_rows, num_cols = matrix.shape
8288
hash_coefficients = np.random.randint(1, HASH_PRIME_MODULUS, size=HASH_FUNCTIONS_COUNT, dtype=np.int64)
@@ -90,6 +96,7 @@ def _compute_row_signatures_matrix(matrix: Matrix) -> Matrix:
9096
return row_signatures_matrix
9197

9298
@staticmethod
99+
@line_profiler.profile
93100
def _group_row_hashes_to_buckets(row_hashes: npt.NDArray[Any]) -> RowGroupingResult:
94101

95102
_, row_to_bucket, new_bucket_sizes = np.unique(
@@ -111,6 +118,7 @@ def _group_row_hashes_to_buckets(row_hashes: npt.NDArray[Any]) -> RowGroupingRes
111118
return RowGroupingResult(row_to_bucket=new_row_to_bucket, bucket_sizes=new_bucket_sizes)
112119

113120
@staticmethod
121+
@line_profiler.profile
114122
def _build_right_factor(matrix: Matrix, build_left_factor_result: BuildLeftFactorResult) -> Matrix:
115123
"""
116124
This function essentially computes the value of

cfpq_decomposer/prototype_decomposer.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import random
66

7+
import line_profiler
78
from graphblas.binary import plus
89
from graphblas.core.dtypes import BOOL, INT32
910
from graphblas.core.matrix import Matrix
@@ -26,6 +27,7 @@ class BucketFactor:
2627

2728

2829
class PrototypeDecomposer(AbstractDecomposer):
30+
@line_profiler.profile
2931
def row_based_decompose(self, matrix: Matrix) -> Tuple[Matrix, Matrix]:
3032
row_to_column_sets = self._extract_row_to_column_sets(matrix)
3133
row_minhash_signatures = self._compute_row_minhash_signatures(row_to_column_sets)
@@ -34,6 +36,7 @@ def row_based_decompose(self, matrix: Matrix) -> Tuple[Matrix, Matrix]:
3436
return self._build_factor_matrices(bucket_factors, matrix)
3537

3638
@staticmethod
39+
@line_profiler.profile
3740
def _extract_row_to_column_sets(matrix: Matrix) -> Dict[int, Set[int]]:
3841
row_to_column_sets: Dict[int, Set[int]] = defaultdict(set)
3942
rows, cols, _ = matrix.to_coo()
@@ -42,6 +45,7 @@ def _extract_row_to_column_sets(matrix: Matrix) -> Dict[int, Set[int]]:
4245
return row_to_column_sets
4346

4447
@staticmethod
48+
@line_profiler.profile
4549
def _generate_hash_coefficients_and_offsets() -> List[Tuple[int, int]]:
4650
coefficients_and_offsets: List[Tuple[int, int]] = []
4751
for _ in range(HASH_FUNCTIONS_COUNT):
@@ -68,6 +72,7 @@ def _compute_row_minhash_signatures(
6872
return row_minhash_signatures
6973

7074
@staticmethod
75+
@line_profiler.profile
7176
def _group_rows_by_master_hash(
7277
row_minhash_signatures: Dict[int, Tuple[int, ...]],
7378
) -> Dict[int, List[int]]:
@@ -81,6 +86,7 @@ def _group_rows_by_master_hash(
8186
}
8287

8388
@staticmethod
89+
@line_profiler.profile
8490
def _build_bucket_factors(
8591
master_hash_to_rows: Dict[int, List[int]],
8692
row_to_column_sets: Dict[int, Set[int]],
@@ -94,6 +100,7 @@ def _build_bucket_factors(
94100
return bucket_factors
95101

96102
@staticmethod
103+
@line_profiler.profile
97104
def _filter_rows_by_frequency(
98105
candidate_rows: List[int],
99106
row_to_column_sets: Dict[int, Set[int]],
@@ -114,6 +121,7 @@ def _filter_rows_by_frequency(
114121
return frequency_signature, surviving_rows
115122

116123
@staticmethod
124+
@line_profiler.profile
117125
def _build_bucket_factor(
118126
bucket_rows: List[int],
119127
row_to_column_sets: Dict[int, Set[int]],
@@ -142,6 +150,7 @@ def _build_bucket_factor(
142150
)
143151

144152
@staticmethod
153+
@line_profiler.profile
145154
def _build_factor_matrices(
146155
bucket_factors: List[BucketFactor],
147156
matrix: Matrix,

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ pandas==2.0.3
99
numpy==1.23.5
1010
psutil==5.9.8
1111
tabulate==0.9.0
12+
line_profiler==4.1.0

0 commit comments

Comments
 (0)