Exemple #1
0
    def simulate(self, _trace, args):
        return self.makerPSP.simulate(args)

    def weight(self, _trace, newValue, _args):
        # Using newValue.spAux here because args.madeSPAux is liable to be
        # None when detaching. This has something to do with when the Args
        # object is constructed relative to other things that happen
        # during detach/regen. TODO: fix it so that this code is less
        # fragile.

        (_f_compute, f_emu) = newValue.asPythonList()
        answer = f_emu.sp.outputPSP.logDensityOfCounts(self.makerPSP.shared_aux)
        # print "quadmem LKernel weight = %s" % answer
        return answer

allocateQuadmemSP = no_request(MakeMakeQuadMSPOutputPSP())

class QuadMComputerRequestPSP(DeterministicPSP):
    def __init__(self, f_node):
        self.f_node = f_node

    def simulate(self, args):
        id = str(args.operandValues())
        x = args.operandValues()[0]
        pri = args.operandValues()[1]
        forwarded_args = [x]
        exp = ["quadmemmedSP"] + [["quote",val] for val in forwarded_args]
        env = VentureEnvironment(None,["quadmemmedSP"],[self.f_node])
        return Request([ESR(id,exp,emptyAddress,env)])

# TODO Perhaps this could subclass ESRRefOutputPSP to correctly handle
Exemple #2
0
 def simulate(self, args):
     return VentureSPRecord(no_request(MakeGPMSPOutputPSP()))
Exemple #3
0
                                                  vvsum(direction)]),
    deterministic_psp(
        np.add,
        sim_grad=lambda args, direction: [vvsum(direction), direction]),
    deterministic_psp(
        lambda *args: np.sum(args, axis=0),
        sim_grad=lambda args, direction: [direction for _ in args],
        descr="add returns the sum of all its arguments"),
    deterministic_psp(symbolic_zero_left,
                      sim_grad=lambda args, direction: [0, direction]),
    deterministic_psp(symbolic_zero_right,
                      sim_grad=lambda args, direction: [direction, 0]),
    deterministic_psp(lambda a, b: a + b),
])

registerBuiltinSP("add", no_request(generic_add))

generic_sub = dispatching_psp([
    SPType([t.NumberType(), t.NumberType()], t.NumberType()),
    SPType([t.ArrayUnboxedType(t.NumberType()),
            t.NumberType()], t.ArrayUnboxedType(t.NumberType())),
    SPType([t.NumberType(), t.ArrayUnboxedType(t.NumberType())],
           t.ArrayUnboxedType(t.NumberType())),
    SPType([
        t.ArrayUnboxedType(t.NumberType()),
        t.ArrayUnboxedType(t.NumberType())
    ], t.ArrayUnboxedType(t.NumberType()))
], [
    deterministic_psp(
        lambda x, y: x - y,
        sim_grad=lambda args, direction: [direction, -direction],
Exemple #4
0
    def getAAALKernel(self): return GPMDeterministicMakerAAALKernel(self)

class GPMDeterministicMakerAAALKernel(SimulationAAALKernel):
    """
    This is based on DeterministicMakerAAALKernel.
    """
    def __init__(self,makerPSP): self.makerPSP = makerPSP
    def simulate(self, _trace, args):
        return self.makerPSP.simulate(args)
    def weight(self, _trace, newValue, _args):
        (_f_compute, f_emu) = newValue.asPythonList()
        answer = f_emu.sp.outputPSP.logDensityOfCounts(self.makerPSP.shared_aux)
        # print "gpmem LKernel weight = %s" % answer
        return answer

allocateGPmemSP = no_request(MakeMakeGPMSPOutputPSP())

class GPMComputerRequestPSP(DeterministicPSP):
    def __init__(self, f_node):
        self.f_node = f_node

    def simulate(self, args):
        id = str(args.operandValues())
        exp = ["gpmemmedSP"] + [["quote",val] for val in args.operandValues()]
        env = VentureEnvironment(None,["gpmemmedSP"],[self.f_node])
        return Request([ESR(id,exp,emptyAddress,env)])

# TODO Perhaps this could subclass ESRRefOutputPSP to correctly handle
# back-propagation?
class GPMComputerOutputPSP(DeterministicPSP):
    def simulate(self,args):
# (at your option) any later version.
#
# Venture is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Venture.  If not, see <http://www.gnu.org/licenses/>.

import numpy as np

from venture.lite.sp import SPType
from venture.lite.sp_help import deterministic_psp
from venture.lite.sp_help import dispatching_psp
from venture.lite.sp_help import no_request
from venture.lite.sp_registry import registerBuiltinSP
import venture.lite.types as t

generic_biplex = dispatching_psp(
  [SPType([t.BoolType(), t.AnyType(), t.AnyType()], t.AnyType()),
   SPType([t.ArrayUnboxedType(t.NumberType()), t.ArrayUnboxedType(t.NumberType()), t.ArrayUnboxedType(t.NumberType())], t.ArrayUnboxedType(t.NumberType()))],
  [deterministic_psp(lambda p, c, a: c if p else a,
                     sim_grad=lambda args, direction: [0, direction, 0] if args[0] else [0, 0, direction],
                     descr="biplex returns either its second or third argument, depending on the first."),
   deterministic_psp(np.where,
                     # TODO sim_grad
                     descr="vector-wise biplex")])

registerBuiltinSP("biplex", no_request(generic_biplex))