Skip to content

Commit 334dfe1

Browse files
committed
Add time values as sampler stats for NUTS
1 parent 747db63 commit 334dfe1

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

pymc3/step_methods/hmc/base_hmc.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
from collections import namedtuple
16+
import time
1617

1718
import numpy as np
1819
import logging
@@ -132,6 +133,9 @@ def _hamiltonian_step(self, start, p0, step_size):
132133

133134
def astep(self, q0):
134135
"""Perform a single HMC iteration."""
136+
perf_start = time.perf_counter_ns()
137+
process_start = time.process_time_ns()
138+
135139
p0 = self.potential.random()
136140
start = self.integrator.compute_state(q0, p0)
137141

@@ -166,6 +170,9 @@ def astep(self, q0):
166170

167171
hmc_step = self._hamiltonian_step(start, p0, step_size)
168172

173+
perf_end = time.perf_counter_ns()
174+
process_end = time.process_time_ns()
175+
169176
self.step_adapt.update(hmc_step.accept_stat, adapt_step)
170177
self.potential.update(hmc_step.end.q, hmc_step.end.q_grad, self.tune)
171178
if hmc_step.divergence_info:
@@ -191,7 +198,13 @@ def astep(self, q0):
191198
if not self.tune:
192199
self._samples_after_tune += 1
193200

194-
stats = {"tune": self.tune, "diverging": bool(hmc_step.divergence_info)}
201+
stats = {
202+
"tune": self.tune,
203+
"diverging": bool(hmc_step.divergence_info),
204+
"perf_counter_diff_ns": perf_end - perf_start,
205+
"process_time_diff_ns": process_end - process_start,
206+
"perf_counter_ns": perf_end,
207+
}
195208

196209
stats.update(hmc_step.stats)
197210
stats.update(self.step_adapt.stats())

pymc3/step_methods/hmc/hmc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ class HamiltonianMC(BaseHMC):
4848
'path_length': np.float64,
4949
'accepted': np.bool,
5050
'model_logp': np.float64,
51+
'process_time_diff_ns': np.int64,
52+
'perf_counter_diff_ns': np.int64,
53+
'perf_counter_ns': np.int64,
5154
}]
5255

5356
def __init__(self, vars=None, path_length=2., max_steps=1024, **kwargs):

pymc3/step_methods/hmc/nuts.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ class NUTS(BaseHMC):
7272
samples, the step size is set to this value. This should converge
7373
during tuning.
7474
- `model_logp`: The model log-likelihood for this sample.
75+
- `process_time_diff_ns`: The time it took to draw the sample, as defined
76+
by the python standard library `time.process_time_ns`. This counts all
77+
the CPU time, including worker processes in BLAS and OpenMP.
78+
- `perf_counter_diff_ns`: The time it took to draw the sample, as defined
79+
by the python standard library `time.perf_counter_ns` (wall time).
80+
- `perf_counter_ns`: The value of the `time.perf_counter_ns` after drawing
81+
the sample.
7582
7683
References
7784
----------
@@ -96,6 +103,9 @@ class NUTS(BaseHMC):
96103
"energy": np.float64,
97104
"max_energy_error": np.float64,
98105
"model_logp": np.float64,
106+
"process_time_diff_ns": np.int64,
107+
"perf_counter_diff_ns": np.int64,
108+
"perf_counter_ns": np.int64,
99109
}
100110
]
101111

0 commit comments

Comments
 (0)