Example #1
0
class Extend(Op):
    # See doc in instance of this Op after the class definition.
    def __init__(self, inplace=False):
        self.inplace = inplace
        if self.inplace:
            self.destroy_map = {0: [0]}
            # TODO: make destroy_handler support having views and
            # destroyed version of multiple inputs.
            # self.view_map = {0: [1]}
        else:
            # TODO: make destroy_handler support multiple view
            # self.view_map = {0: [0, 1]}
            self.view_map = {0: [0]}

    def __eq__(self, other):
        return type(self) == type(other) and self.inplace == other.inplace

    def __hash__(self):
        return hash(type(self)) ^ hash(self.inplace)

    def make_node(self, x, toAppend):
        assert isinstance(x.type, TypedListType)
        assert x.type == toAppend.type
        return Apply(self, [x, toAppend], [x.type()])

    def perform(self, node, (x, toAppend), (out, )):
        if not self.inplace:
            out[0] = list(x)
        else:
            out[0] = x
        # need to copy toAppend due to destroy_handler limitation
        if toAppend:
            o = out[0]
            for i in toAppend:
                o.append(_lessbroken_deepcopy(i))
Example #2
0
 def perform(self, node, inputs, outputs):
     (x, index, toInsert) = inputs
     (out, ) = outputs
     if not self.inplace:
         out[0] = list(x)
     else:
         out[0] = x
     # need to copy toAppend due to destroy_handler limitation
     toInsert = _lessbroken_deepcopy(toInsert)
     out[0].insert(index, toInsert)
Example #3
0
 def perform(self, node, inputs, outputs):
     (x, index, toInsert) = inputs
     (out,) = outputs
     if not self.inplace:
         out[0] = list(x)
     else:
         out[0] = x
     # need to copy toAppend due to destroy_handler limitation
     toInsert = _lessbroken_deepcopy(toInsert)
     out[0].insert(index, toInsert)
Example #4
0
 def perform(self, node, inputs, outputs):
     (x, toAppend) = inputs
     (out, ) = outputs
     if not self.inplace:
         out[0] = list(x)
     else:
         out[0] = x
     # need to copy toAppend due to destroy_handler limitation
     if toAppend:
         o = out[0]
         for i in toAppend:
             o.append(_lessbroken_deepcopy(i))
Example #5
0
 def perform(self, node, inputs, outputs):
     (x, toAppend) = inputs
     (out,) = outputs
     if not self.inplace:
         out[0] = list(x)
     else:
         out[0] = x
     # need to copy toAppend due to destroy_handler limitation
     if toAppend:
         o = out[0]
         for i in toAppend:
             o.append(_lessbroken_deepcopy(i))
Example #6
0
class Insert(Op):
    # See doc in instance of this Op after the class definition.
    def __init__(self, inplace=False):
        self.inplace = inplace
        if self.inplace:
            self.destroy_map = {0: [0]}
            # TODO: make destroy_handler support having views and
            # destroyed version of multiple inputs.
            # self.view_map = {0: [2]}
        else:
            # TODO: make destroy_handler support multiple view
            # self.view_map = {0: [0, 2]}
            self.view_map = {0: [0]}

    def __eq__(self, other):
        return type(self) == type(other) and self.inplace == other.inplace

    def __hash__(self):
        return hash(type(self)) ^ hash(self.inplace)

    def make_node(self, x, index, toInsert):
        assert isinstance(x.type, TypedListType)
        assert x.ttype == toInsert.type
        if not isinstance(index, Variable):
            index = T.constant(index, ndim=0, dtype='int64')
        else:
            assert index.dtype == 'int64'
            assert isinstance(index, T.TensorVariable) and index.ndim == 0
        return Apply(self, [x, index, toInsert], [x.type()])

    def perform(self, node, (x, index, toInsert), (out, )):
        if not self.inplace:
            out[0] = list(x)
        else:
            out[0] = x
        # need to copy toAppend due to destroy_handler limitation
        toInsert = _lessbroken_deepcopy(toInsert)
        out[0].insert(index, toInsert)
Example #7
0
 def perform(self, node, inputs, outputs):
     (out, ) = outputs
     # We need to make sure that we don't get a view on our inputs
     out[0] = [_lessbroken_deepcopy(inp) for inp in inputs]
Example #8
0
 def perform(self, node, inputs, outputs):
     (out,) = outputs
     # We need to make sure that we don't get a view on our inputs
     out[0] = [_lessbroken_deepcopy(inp) for inp in inputs]