Skip to content

Commit

Permalink
Merge pull request #868 from null-a/no-cache-ad
Browse files Browse the repository at this point in the history
Throw error when attempting to `cache` on AD nodes.
  • Loading branch information
stuhlmueller committed Jul 5, 2017
2 parents ccda654 + 2461a27 commit a718598
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/functions/other.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ Other
though for technical reasons it must currently be called as
``dp.cache`` for this to work.

``cache`` does not support caching functions of scalar/tensor
arguments when performing inference with gradient based algorithms.
(e.g. :ref:`HMC <hmc>`, :ref:`ELBO <elbo>`.) Attempting to do so
will produce an error.

.. js:function:: mem(fn)

Returns a memoized version of ``fn``. The memoized function is
Expand Down
2 changes: 2 additions & 0 deletions docs/inference/methods.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ Example usage::

Infer({method: 'MCMC', kernel: 'MH', model: model});

.. _hmc:

.. describe:: HMC

Implements Hamiltonian Monte Carlo. [neal11]_
Expand Down
6 changes: 6 additions & 0 deletions src/headerUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ module.exports = function(env) {
var c = LRU(maxSize);
var cf = function(s, k, a) {
var args = Array.prototype.slice.call(arguments, 3);

if (_.some(args, ad.isLifted)) {
throw new Error('Cannot cache functions of scalar/tensor arguments ' +
'when performing automatic differentiation.');
}

var stringedArgs = serialize(args);
if (c.has(stringedArgs)) {
return k(s, c.get(stringedArgs));
Expand Down

0 comments on commit a718598

Please sign in to comment.