(self, optree, numpy_call_dict=numpy_call_dict_cpu)
return postfix_stack[0]
Execute the optree. Break optree into sub-optrees if necessary.

        Arguments:
            optree: (OpTreeNode): the OpTreeNode object that represents all
                                  the operations
Execute the optree. Break optree into sub-optrees if necessary.
Execute the optree . Break optree into sub - optrees if necessary .
def execute(self, optree, numpy_call_dict=numpy_call_dict_cpu):
        """
        Execute the optree. Break optree into sub-optrees if necessary.

        Arguments:
            optree: (OpTreeNode): the OpTreeNode object that represents all
                                  the operations
        """
        # deal with onehot specially for now
        if (len(optree) == 3 and isinstance(optree[2], OpTreeNode) and
                optree[2][0]['op'] == 'onehot'):
            assert optree[0]['op'] == 'assign'
            assert isinstance(optree[1], Tensor)
            # get the output buffer
            array_output = optree[1]._tensor

            # get the output shape and onehot representation length will be on
            # this axis
            numpy_axis = optree[2][0]['axis']
            numpy_ind0 = optree[2][0]['idx']._tensor.squeeze()

            numpy_ind_len = numpy_ind0.size
            numpy_ind1 = list(range(numpy_ind_len))

            # ind for indexing
            numpy_ind = np.zeros((2, numpy_ind_len), dtype=np.int32)
            numpy_ind[numpy_axis] = numpy_ind0
            numpy_ind[1 - numpy_axis] = numpy_ind1
            array_output[:] = 0
            array_output[numpy_ind.tolist()] = 1

            return array_output

        # get post order stack
        postfix_stack = optree.traverse(list())

        # init compute stack
        compute_stack = []

        # iterate through postfix stack to compute result
        for p in postfix_stack:
            if isinstance(p, dict):
                # TODO add rand and onehot here
                if p['op'] in OpCollection.unary_ops:
                    left = compute_stack.pop()
                    compute_stack.append(numpy_call_dict[p['op']](left))
                elif p['op'] in OpCollection.binary_ops:
                    right = compute_stack.pop()
                    left = compute_stack.pop()
                    compute_stack.append(numpy_call_dict[p['op']](left, right))
                elif p['op'] in OpCollection.reduction_ops:
                    left = compute_stack.pop()
                    compute_stack.append(numpy_call_dict[p['op']](p, left))
                elif p['op'] in OpCollection.zero_operand_ops:
                    compute_stack.append(numpy_call_dict[p['op']](None))
                else:
                    raise NotImplementedError
            elif isinstance(p, CPUTensor):
                compute_stack.append(p._tensor)
            else:
                compute_stack.append(p)

        assert len(compute_stack) == 1
        return postfix_stack[0]
def execute ( self , optree , numpy_call_dict = numpy_call_dict_cpu ) : # deal with onehot specially for now if ( len ( optree ) == 3 and isinstance ( optree [ 2 ] , OpTreeNode ) and optree [ 2 ] [ 0 ] [ 'op' ] == 'onehot' ) : assert optree [ 0 ] [ 'op' ] == 'assign' assert isinstance ( optree [ 1 ] , Tensor ) # get the output buffer array_output = optree [ 1 ] . _tensor # get the output shape and onehot representation length will be on # this axis numpy_axis = optree [ 2 ] [ 0 ] [ 'axis' ] numpy_ind0 = optree [ 2 ] [ 0 ] [ 'idx' ] . _tensor . squeeze ( ) numpy_ind_len = numpy_ind0 . size numpy_ind1 = list ( range ( numpy_ind_len ) ) # ind for indexing numpy_ind = np . zeros ( ( 2 , numpy_ind_len ) , dtype = np . int32 ) numpy_ind [ numpy_axis ] = numpy_ind0 numpy_ind [ 1 - numpy_axis ] = numpy_ind1 array_output [ : ] = 0 array_output [ numpy_ind . tolist ( ) ] = 1 return array_output # get post order stack postfix_stack = optree . traverse ( list ( ) ) # init compute stack compute_stack = [ ] # iterate through postfix stack to compute result for p in postfix_stack : if isinstance ( p , dict ) : # TODO add rand and onehot here if p [ 'op' ] in OpCollection . unary_ops : left = compute_stack . pop ( ) compute_stack . append ( numpy_call_dict [ p [ 'op' ] ] ( left ) ) elif p [ 'op' ] in OpCollection . binary_ops : right = compute_stack . pop ( ) left = compute_stack . pop ( ) compute_stack . append ( numpy_call_dict [ p [ 'op' ] ] ( left , right ) ) elif p [ 'op' ] in OpCollection . reduction_ops : left = compute_stack . pop ( ) compute_stack . append ( numpy_call_dict [ p [ 'op' ] ] ( p , left ) ) elif p [ 'op' ] in OpCollection . zero_operand_ops : compute_stack . append ( numpy_call_dict [ p [ 'op' ] ] ( None ) ) else : raise NotImplementedError elif isinstance ( p , CPUTensor ) : compute_stack . append ( p . _tensor ) else : compute_stack . append ( p ) assert len ( compute_stack ) == 1 return postfix_stack [ 0 ]