Example #1
0
def And(onnx_node,
        ng_inputs):  # type: (NodeWrapper, List[NgraphNode]) -> NgraphNode
    """Perform the `and` logical operation elementwise on two input tensors with numpy-style broadcasting."""
    left, right = numpy_style_broadcast_for_binary_operation(
        onnx_node, ng_inputs)
    left = ng.convert(ng.not_equal(left, 0), int)
    right = ng.convert(ng.not_equal(right, 0), int)
    return ng.convert(left * right, bool)
Example #2
0
def Xor(onnx_node, ng_inputs):  # type: (NodeWrapper, List[NgraphNode]) -> NgraphNode
    """Perform the `xor` logical operation elementwise on two input tensors."""
    left, right = broadcast_for_binary_operation(onnx_node, ng_inputs)
    not_left = ng.convert(ng.equal(left, 0), int)
    left = ng.convert(ng.not_equal(left, 0), int)
    right = ng.convert(ng.not_equal(right, 0), int)
    not_right = ng.convert(ng.equal(right, 0), int)

    return ((not_left * right) + (not_right * left)) > 0
Example #3
0
def Cast(onnx_node,
         ng_inputs):  # type: (NodeWrapper, List[NgraphNode]) -> NgraphNode
    """Limit input tensor values within specified interval."""
    data = ng_inputs[0]
    cast_to_type = onnx_node.get_attribute_value('to')
    if cast_to_type is None:
        raise ValueError('Cast node (%s): \'to\' attribute is required.')

    input_tensor_type = get_dtype(data.get_element_type())
    new_type = onnx_tensor_type_to_numpy_type(cast_to_type)
    unsupported_types = [
        onnx_tensor_type_to_numpy_type('COMPLEX64'),
        onnx_tensor_type_to_numpy_type('COMPLEX128'),
    ]

    if input_tensor_type in unsupported_types:
        raise ValueError(
            'Cast node (%s): input tensor data type (%s) is not supported.',
            onnx_node.name, str(input_tensor_type))
    if new_type in unsupported_types:
        raise ValueError(
            'Cast node (%s): casting to type (%s) is not supported.',
            onnx_node.name, str(new_type))

    return ng.convert(data, new_type)
Example #4
0
def ThresholdedRelu(
        onnx_node,
        ng_inputs):  # type: (NodeWrapper, List[NgraphNode]) -> NgraphNode
    """Apply the Thresholded Relu function to the input tensor elementwise.

    f(x) = 0 for x <= alpha, f(x) = x for x > alpha
    """
    x = ng_inputs[0]
    alpha = onnx_node.get_attribute_value('alpha', 1.0)
    x_map = ng.convert(ng.greater(x, alpha), get_dtype(x.get_element_type()))
    x = x * x_map
    return x
Example #5
0
def get_bool_nodes(
        nodes):  # type: (Tuple[NgraphNode, ...]) -> Tuple[NgraphNode, ...]
    """Convert each input node to bool data type if necessary.

    :param nodes: Input nodes to be converted.
    :return: Converted nodes.
    """
    bool_nodes = []
    for node in nodes:
        if not node.get_element_type() == NgraphType.boolean:
            bool_nodes.append(ng.convert(node, bool))
            logger.warning('Converting node of type: <{}> to bool.'.format(
                get_dtype(node.get_element_type())))
        else:
            bool_nodes.append(node)

    return tuple(bool_nodes)
Example #6
0
def test_tensor_iterator():
    from ngraph.utils.tensor_iterator_types import (
        GraphBody,
        TensorIteratorSliceInputDesc,
        TensorIteratorMergedInputDesc,
        TensorIteratorInvariantInputDesc,
        TensorIteratorBodyOutputDesc,
        TensorIteratorConcatOutputDesc,
    )

    #  Body parameters
    body_timestep = ng.parameter([], np.int32, "timestep")
    body_data_in = ng.parameter([1, 2, 2], np.float32, "body_in")
    body_prev_cma = ng.parameter([2, 2], np.float32, "body_prev_cma")
    body_const_one = ng.parameter([], np.int32, "body_const_one")

    # CMA = cumulative moving average
    prev_cum_sum = ng.multiply(ng.convert(body_timestep, "f32"), body_prev_cma)
    curr_cum_sum = ng.add(prev_cum_sum, ng.squeeze(body_data_in, [0]))
    elem_cnt = ng.add(body_const_one, body_timestep)
    curr_cma = ng.divide(curr_cum_sum, ng.convert(elem_cnt, "f32"))
    cma_hist = ng.unsqueeze(curr_cma, [0])

    # TI inputs
    data = ng.parameter([16, 2, 2], np.float32, "data")
    # Iterations count
    zero = ng.constant(0, dtype=np.int32)
    one = ng.constant(1, dtype=np.int32)
    initial_cma = ng.constant(np.zeros([2, 2], dtype=np.float32),
                              dtype=np.float32)
    iter_cnt = ng.range(zero, np.int32(16), np.int32(1))
    ti_inputs = [iter_cnt, data, initial_cma, one]

    graph_body = GraphBody(
        [body_timestep, body_data_in, body_prev_cma, body_const_one],
        [curr_cma, cma_hist])
    ti_slice_input_desc = [
        # timestep
        # input_idx, body_param_idx, start, stride, part_size, end, axis
        TensorIteratorSliceInputDesc(0, 0, 0, 1, 1, -1, 0),
        # data
        TensorIteratorSliceInputDesc(1, 1, 0, 1, 1, -1, 0),
    ]
    ti_merged_input_desc = [
        # body prev/curr_cma
        TensorIteratorMergedInputDesc(2, 2, 0),
    ]
    ti_invariant_input_desc = [
        # body const one
        TensorIteratorInvariantInputDesc(3, 3),
    ]

    # TI outputs
    ti_body_output_desc = [
        # final average
        TensorIteratorBodyOutputDesc(0, 0, -1),
    ]
    ti_concat_output_desc = [
        # history of cma
        TensorIteratorConcatOutputDesc(1, 1, 0, 1, 1, -1, 0),
    ]

    node = ng.tensor_iterator(
        ti_inputs,
        graph_body,
        ti_slice_input_desc,
        ti_merged_input_desc,
        ti_invariant_input_desc,
        ti_body_output_desc,
        ti_concat_output_desc,
    )

    assert node.get_type_name() == "TensorIterator"
    assert node.get_output_size() == 2
    # final average
    assert list(node.get_output_shape(0)) == [2, 2]
    # cma history
    assert list(node.get_output_shape(1)) == [16, 2, 2]
Example #7
0
def Not(onnx_node, ng_inputs):  # type: (NodeWrapper, List[NgraphNode]) -> NgraphNode
    """Return the negation of the input tensor elementwise."""
    data = ng.convert(ng.not_equal(ng_inputs[0], 0), bool)
    return ng.logical_not(data)