Skip to content

Commit

Permalink
Untapify AD values #45
Browse files Browse the repository at this point in the history
  • Loading branch information
longouyang committed Dec 8, 2016
1 parent 04b06dd commit 7fd95df
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 16 deletions.
21 changes: 21 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1706,6 +1706,27 @@
globalStore["x"] = 1 + 1;
globalStore["x"] = 1 + 1;
</code></pre>

untapifying AD values:
<pre><code>// test as a webppl call
var m1 = function() { var x = gaussian(0,1); print(x); return x };
// test as a vanilla js call
var m2 = function() { var x = gaussian(0,1); window.print(x); return x };


print(Infer({
method: 'MCMC',
kernel: 'HMC',
samples: 20,
model: m1
}))

print(Infer({
method: 'MCMC',
kernel: 'HMC',
samples: 20,
model: m2
}))</code></pre>
</div>
<div class="col">
<h3>error handling</h3>
Expand Down
50 changes: 42 additions & 8 deletions docs/webppl-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -42549,6 +42549,36 @@ var ResultList = React.createClass({
var jobsQueue = [],
compileCache = {};

// untape AD values
var unAd = function () {
var ad = global.ad;
if (typeof ad == 'undefined') {
unAd = function (x) {
return x;
};
} else {
unAd = function (x) {
if (global.ad.isLifted(x)) {
return x.x;
} else if (_.isArray(x)) {
return _.map(x, unAd);
} else if (x instanceof ad.tensor.__Tensor) {
// Optimization: tensors don't contain tapes, so return now rather
// than descend into the tensor object.
return x;
} else if (_.isObject(x) && !_.isFunction(x)) {
// Ensure prototype chain is preserved
var proto = Object.getPrototypeOf(x);
var y = _.mapObject(x, unAd);
return _.extendOwn(Object.create(proto), y);
return y;
} else {
return x;
}
};
}
};

var CodeEditor = React.createClass({
displayName: 'CodeEditor',

Expand All @@ -42565,21 +42595,25 @@ var CodeEditor = React.createClass({
// the actively running codebox will inject them into global once it starts running
// ------------------------------------------------------------
print: function (s, k, a, x) {
// make print work as a regular js function
if (arguments.length == 1) {
this.addResult({ type: 'text',
message: typeof x == 'object' ? JSON.stringify(arguments[0]) : arguments[0] });
return;
// make print work as a vanilla js function
var isVanillaCall = arguments.length == 1;
if (isVanillaCall) {
x = arguments[0];
}

// if x has a custom printer, use it
if (x.__print__) {
return k(s, x.__print__(x));
} else {
var type = typeof x;
var _x = unAd(x);
var type = typeof _x;
this.addResult({ type: 'text',
message: _.contains(['object', 'boolean', 'number'], type) ? JSON.stringify(x) : x });
return k(s);
message: _.contains(['object', 'boolean', 'number'], type) ? JSON.stringify(_x) : _x });
if (isVanillaCall) {
return;
} else {
return k(s);
}
}
},
makeResultContainer: function () {
Expand Down
49 changes: 41 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,35 @@ var ResultList = React.createClass({

var jobsQueue = [], compileCache = {};

// untape AD values
var unAd = function() {
var ad = global.ad;
if (typeof ad == 'undefined') {
unAd = function(x) { return x }
} else {
unAd = function(x) {
if (global.ad.isLifted(x)) {
return x.x;
} else if (_.isArray(x)) {
return _.map(x, unAd);
} else if (x instanceof ad.tensor.__Tensor) {
// Optimization: tensors don't contain tapes, so return now rather
// than descend into the tensor object.
return x;
} else if (_.isObject(x) && !_.isFunction(x)) {
// Ensure prototype chain is preserved
var proto = Object.getPrototypeOf(x);
var y = _.mapObject(x, unAd);
return _.extendOwn(Object.create(proto), y);
return y;
} else {
return x;
}
}

}
}

var CodeEditor = React.createClass({
getInitialState: function() {
return {
Expand All @@ -234,21 +263,25 @@ var CodeEditor = React.createClass({
// the actively running codebox will inject them into global once it starts running
// ------------------------------------------------------------
print: function(s,k,a,x) {
// make print work as a regular js function
if (arguments.length == 1) {
this.addResult({type: 'text',
message: typeof x == 'object' ? JSON.stringify(arguments[0]) : arguments[0]})
return;
// make print work as a vanilla js function
var isVanillaCall = (arguments.length == 1);
if (isVanillaCall) {
x = arguments[0];
}

// if x has a custom printer, use it
if (x.__print__) {
return k(s, x.__print__(x));
} else {
var type = typeof x;
var _x = unAd(x);
var type = typeof _x;
this.addResult({type: 'text',
message: _.contains(['object', 'boolean', 'number'], type) ? JSON.stringify(x) : x})
return k(s)
message: _.contains(['object', 'boolean', 'number'], type) ? JSON.stringify(_x) : _x})
if (isVanillaCall) {
return;
} else {
return k(s)
}
}
},
makeResultContainer: function() {
Expand Down

0 comments on commit 7fd95df

Please sign in to comment.