Skip to content

Commit 3c3deaa

Browse files
committed
optimize: cache cumsum_view and split_candidates_view in logML
1 parent 39c45d5 commit 3c3deaa

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

pasio/cython_log_marginal_likelyhood.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ cdef class LogMarginalLikelyhoodComputer(BasicLogMarginalLikelyhoodComputer):
1414
cdef LogGammaComputer log_gamma_alpha_computer
1515
cdef np.ndarray split_candidates
1616
cdef np.ndarray cumsum, logfac_cumsum
17+
cdef long[::1] split_candidates_view
18+
cdef long long[::1] cumsum_view
1719

1820
cdef class LogMarginalLikelyhoodIntAlphaComputer(LogMarginalLikelyhoodComputer):
1921
cdef unsigned int int_alpha

pasio/cython_log_marginal_likelyhood.pyx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ cdef class LogMarginalLikelyhoodComputer(BasicLogMarginalLikelyhoodComputer):
7575
self.logfac_cumsum = np.hstack([0, np.cumsum(count_logfacs)])[split_candidates]
7676

7777
self.segment_creation_cost = alpha * self.log_computer.compute_for_number(0) - self.log_gamma_alpha_computer.compute_for_number(0)
78+
self.cumsum_view = self.cumsum
79+
self.split_candidates_view = self.split_candidates
7880

7981
def total_sum_logfac(self):
8082
last_idx = len(self.logfac_cumsum) - 1
@@ -130,12 +132,11 @@ cdef class LogMarginalLikelyhoodIntAlphaComputer(LogMarginalLikelyhoodComputer):
130132
cdef long long[::1] cumsum_view
131133
cdef double[::1] log_gamma_view, log_view
132134

133-
cumsum_view = self.cumsum
134-
split_candidates_view = self.split_candidates
135+
cumsum_view = self.cumsum_view
136+
split_candidates_view = self.split_candidates_view
135137
# direct memory access is much more efficient than even inlined method call
136138
log_gamma_view = self.log_gamma_computer.precomputed_view
137139
log_view = self.log_computer.precomputed_view
138-
139140
cumsum_last = cumsum_view[stop]
140141
cumsum_last_shifted = self.int_alpha + cumsum_last
141142
split_candidates_last = split_candidates_view[stop]
@@ -187,8 +188,8 @@ cdef class LogMarginalLikelyhoodRealAlphaComputer(LogMarginalLikelyhoodComputer)
187188
cdef long long[::1] cumsum_view
188189
cdef double[::1] log_gamma_alpha_view, log_view
189190

190-
cumsum_view = self.cumsum
191-
split_candidates_view = self.split_candidates
191+
cumsum_view = self.cumsum_view
192+
split_candidates_view = self.split_candidates_view
192193
# direct memory access is much more efficient than even inlined method call
193194
log_gamma_alpha_view = self.log_gamma_alpha_computer.precomputed_view
194195
log_view = self.log_computer.precomputed_view
@@ -200,7 +201,6 @@ cdef class LogMarginalLikelyhoodRealAlphaComputer(LogMarginalLikelyhoodComputer)
200201
log_gamma_alpha_fully_cached = (cumsum_last < self.log_gamma_alpha_computer.cache_size)
201202
log_fully_cached = (split_candidates_last < self.log_computer.cache_size)
202203

203-
204204
if log_gamma_alpha_fully_cached and log_fully_cached:
205205
for i in range(stop):
206206
segment_count = cumsum_last - cumsum_view[i]

0 commit comments

Comments
 (0)