コード例 #1
0
def matmul(node: NodeWrapper,
           params: Dict[str, np.ndarray],
           xmap: Dict[str, XLayer]):
    """ ONNX MatMul to XLayer Dense/AnyOp conversion function """

    logger.info("ONNX MatMul -> XLayer Dense/AnyOp")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()

    iX = xmap[bottoms[0]]
    bX = xmap[bottoms[1]]

    units = bX.shapes[1]

    if 'Constant' in bX.type:
        X = px.ops.dense(
            op_name=px.stringify(name),
            input_layer=iX,
            weights_layer=bX,
            units=units,
            kernel_layout='IO',
            onnx_id=name
        )
    else:
        X = px.ops.any_op(
            op_name=px.stringify(name),
            in_xlayers=[iX],
            any_shape=[iX.shapes[0], units],
            onnx_id=name
        )

    return [X]
コード例 #2
0
def dynamic_quantize_linear(node: NodeWrapper, params: Dict[str, np.ndarray],
                            xmap: Dict[str, XLayer]):
    """ ONNX DynamicQuantizeLinear to XLayer AnyOp conversion function """

    logger.info("ONNX DynamicQuantizeLinear -> XLayer AnyOp")

    # TODO first name is used for split for now
    name = node.get_outputs()[0]
    nb_outputs = len(node.get_outputs())
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()

    iX = xmap[bottoms[0]]
    d = len(iX.shapes)

    Xs = []
    X = px.ops.any_op(op_name='dql-' + px.stringify(name),
                      in_xlayers=[iX],
                      any_shape=[iX.shapes.tolist(), [1], [1]],
                      onnx_id=name)
    Xs.append(X)

    for idx, tensor_name in enumerate(node.get_outputs()):
        tgi_X = px.ops.tuple_get_item(op_name=px.stringify(tensor_name),
                                      in_xlayers=[X],
                                      index=idx,
                                      onnx_id=tensor_name)
        Xs.append(tgi_X)

    return Xs
コード例 #3
0
def sum(node: NodeWrapper,
        params: Dict[str, np.ndarray],
        xmap: Dict[str, XLayer]):
    """ ONNX Sum to XLayer Add conversion function """

    logger.info("ONNX Sum -> XLayer Add")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()
    assert len(bottoms) >= 2

    aX = xmap[bottoms[0]]  # NCHW
    bX = xmap[bottoms[1]]  # NCHW

    Xs = []
    X = px.ops.add(
        op_name=px.stringify(name),
        in_xlayers=[aX, bX],
        onnx_id=name
    )
    Xs.append(X)

    for i in range(2, len(bottoms)):
        X = px.ops.add(
            op_name=px.stringify(name) + str(i),
            in_xlayers=[Xs[i - 2], xmap[bottoms[i]]],
            onnx_id=name
        )
        Xs.append(X)

    return Xs
コード例 #4
0
def concat(node: NodeWrapper,
           params: Dict[str, np.ndarray],
           xmap: Dict[str, XLayer]):
    """ ONNX Concat to XLayer Concat conversion function """

    logger.info("ONNX Concat -> XLayer Concat")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()

    Xs = [xmap[b] for b in bottoms]
    d = len(Xs[0].shapes)

    axis = int(node_attrs['axis'])
    if axis < 0:
        axis = d + axis

    X = px.ops.concat(
        op_name=px.stringify(name),
        input_layers=Xs,
        axis=axis,
        onnx_id=name
    )

    return [X]
コード例 #5
0
ファイル: onnx_l0_other.py プロジェクト: volcacius/pyxir
def topk(node: NodeWrapper,
         params: Dict[str, np.ndarray],
         xmap: Dict[str, XLayer]):
    """ ONNX TopK to XLayer AnyOp conversion function """

    logger.info("ONNX TopK -> XLayer AnyOp")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()

    iX = xmap[bottoms[0]]
    d = len(iX.shapes)
    in_shape = iX.shapes.tolist()
    k = int(xmap[bottoms[1]].data[0])

    axis = node_attrs['axis'] if 'axis' in node_attrs else -1
    if axis < 0:
        axis += d

    out_shape = [(i if idx != axis else k) for idx, i in enumerate(in_shape)]

    X = px.ops.any_op(
        op_name=px.stringify(name),
        in_xlayers=[iX],
        any_shape=out_shape,
        onnx_id=name
    )

    return [X]
コード例 #6
0
ファイル: onnx_l0_other.py プロジェクト: volcacius/pyxir
def range(node: NodeWrapper,
          params: Dict[str, np.ndarray],
          xmap: Dict[str, XLayer]):
    """ ONNX Range to XLayer AnyOp conversion function """

    logger.info("ONNX Range -> XLayer AnyOp")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()

    start = int(xmap[bottoms[0]].data[0])
    limit = int(xmap[bottoms[1]].data[0])
    delta = int(xmap[bottoms[2]].data[0])

    shape = [max(math.ceil((limit - start) / delta), 0)]

    X = px.ops.any_op(
        op_name=px.stringify(name),
        in_xlayers=[],
        any_shape=shape,
        onnx_id=name
    )

    return [X]
コード例 #7
0
def un_squeeze(node: NodeWrapper, params: Dict[str, np.ndarray],
               xmap: Dict[str, XLayer]):
    """ ONNX UnSqueeze to XLayer AnyOp conversion function """

    logger.info("ONNX Unsqueeze -> XLayer AnyOp")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()

    iX = xmap[bottoms[0]]  # NCHW
    d = len(iX.shapes)

    axes = [int(i) if i > 0 else (d + i) for i in node_attrs['axes']]

    out_shape = iX.shapes.tolist()
    for axis in axes:
        out_shape.insert(axis, 1)

    X = px.ops.any_op(op_name=px.stringify(name),
                      in_xlayers=[iX],
                      any_shape=out_shape,
                      onnx_id=name)

    return [X]
コード例 #8
0
def softmax_cross_entropy_loss(node: NodeWrapper, params: Dict[str,
                                                               np.ndarray],
                               xmap: Dict[str, XLayer]):
    """ ONNX SoftmaxCrossEntropyLoss to XLayer AnyOp conversion function """

    logger.info("ONNX SoftmaxCrossEntropyLoss -> XLayer AnyOp")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()

    iX = xmap[bottoms[0]]
    out_shape = xmap[bottoms[1]].shapes.tolist()

    reduction = str(node_attrs['reduction']) \
        if 'reduction' in node_attrs else 'mean'
    if reduction != 'none':
        out_shape = [1]

    X = px.ops.any_op(op_name=px.stringify(name),
                      in_xlayers=[iX],
                      any_shape=out_shape,
                      onnx_id=name)

    return [X]
コード例 #9
0
def reshape(node: NodeWrapper, params: Dict[str, np.ndarray],
            xmap: Dict[str, XLayer]):
    """ ONNX Reshape to XLayer Reshape conversion function """

    logger.info("ONNX Reshape -> XLayer Reshape")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()

    iX = xmap[bottoms[0]]
    iX_size = iX.sizes

    onnx_newshape = [int(i) for i in list(xmap[bottoms[1]].data[0])]
    newshape = []
    minus_one_idx = None
    for idx, i in enumerate(onnx_newshape):
        if i == 0:
            newshape.append(iX.shapes[idx])
        elif i == -1:
            newshape.append(-1)
            minus_one_idx = idx
        else:
            newshape.append(i)
    if minus_one_idx is not None:
        newshape[minus_one_idx] = \
            int(iX_size[0] // abs(int(np.prod(newshape))))

    X = px.ops.reshape(op_name=px.stringify(name),
                       input_layer=iX,
                       newshape=newshape,
                       onnx_id=name)

    return [X]
コード例 #10
0
def one_hot(node: NodeWrapper, params: Dict[str, np.ndarray],
            xmap: Dict[str, XLayer]):
    """ ONNX OneHot to XLayer AnyOp conversion function """

    logger.info("ONNX OneHot -> XLayer AnyOp")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()

    iX = xmap[bottoms[0]]
    d = len(iX.shapes)
    depth = int(xmap[bottoms[1]].data[0])

    axis = str(node_attrs['axis']) if 'axis' in node_attrs else -1
    if axis < 0:
        axis += d

    out_shape = iX.shapes.tolist()
    out_shape.insert(axis + 1, depth)

    X = px.ops.any_op(op_name=px.stringify(name),
                      in_xlayers=[iX],
                      any_shape=out_shape,
                      onnx_id=name)

    return [X]
コード例 #11
0
def hard_max(node: NodeWrapper, params: Dict[str, np.ndarray],
             xmap: Dict[str, XLayer]):
    """ ONNX Hardmax to XLayer AnyOp conversion function

    Input tensor shape: N dims
    Output tensor shape: 2D
    """

    logger.info("ONNX Hardmax -> XLayer AnyOp")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()

    iX = xmap[bottoms[0]]
    d = len(iX.shapes)

    axis = int(node_attrs['axis']) if 'axis' in node_attrs else 1
    if axis < 0:
        axis = d + axis

    in_shape = iX.shapes.tolist()
    dim_0 = int(np.prod(in_shape[:axis]))
    dim_1 = int(np.prod(in_shape[axis:]))

    X = px.ops.any_op(op_name=px.stringify(name),
                      in_xlayers=[iX],
                      any_shape=[dim_0, dim_1],
                      onnx_id=name)

    return [X]
コード例 #12
0
ファイル: onnx_l1_basic_nn.py プロジェクト: minglu2019/pyxir
    def get_tensor_constant_add(tX, bias_X):

        if len(bias_X.shapes) == 1:
            X = xlf.get_xop_factory_func('BiasAdd')(op_name=px.stringify(name),
                                                    axis=1,
                                                    input_layer=tX,
                                                    bias_layer=bias_X,
                                                    onnx_id=name)
            Xs_tmp = [X]
        else:
            X = px.ops.add(op_name=px.stringify(name),
                           in_xlayers=[tX, bias_X],
                           onnx_id=name)
            Xs_tmp = [bias_X, X]

        return Xs_tmp
コード例 #13
0
def pad(node: NodeWrapper, params: Dict[str, np.ndarray], xmap: Dict[str,
                                                                     XLayer]):
    """ ONNX Pad to XLayer Pad conversion function """

    logger.info("ONNX Pad -> XLayer Pad")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()

    iX = xmap[bottoms[0]]  # NCHW
    if len(bottoms) > 1:
        padding = [int(i) for i in xmap[bottoms[1]].data[0]]
        pad_value = float(xmap[bottoms[2]].data[0])
    else:
        pad_str = 'pads' if 'pads' in node_attrs else 'paddings'
        padding = [int(i) for i in node_attrs[pad_str]]
        pad_value = float(node_attrs['value']) \
            if 'value' in node_attrs else 0.

    h = len(padding) // 2
    padding = [[padding[i], padding[i + h]] for i in range(h)]

    X = px.ops.pad(op_name=px.stringify(name),
                   input_layer=iX,
                   padding=padding,
                   pad_value=pad_value,
                   onnx_id=name)

    return [X]
コード例 #14
0
def mod(node: NodeWrapper,
        params: Dict[str, np.ndarray],
        xmap: Dict[str, XLayer]):
    """ ONNX Mod to XLayer AnyOp conversion function """

    logger.info("ONNX Mod -> XLayer AnyOp")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()
    assert len(bottoms) == 2

    aX = xmap[bottoms[0]]
    bX = xmap[bottoms[1]]

    out_shape = get_numpy_broadcasted_shape(aX.shapes.tolist(),
                                            bX.shapes.tolist())

    X = px.ops.any_op(
        op_name=px.stringify(name),
        in_xlayers=[aX, bX],
        any_shape=out_shape,
        onnx_id=name
    )

    return [X]
コード例 #15
0
def space_to_depth(node: NodeWrapper, params: Dict[str, np.ndarray],
                   xmap: Dict[str, XLayer]):
    """ ONNX SpaceToDepth to XLayer AnyOp conversion function """

    logger.info("ONNX SpaceToDepth -> XLayer AnyOp")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()

    iX = xmap[bottoms[0]]
    in_b, in_c, in_h, in_w = iX.shapes.tolist()

    blocksize = int(node_attrs['blocksize'])
    shape = [
        in_b, in_c * blocksize * blocksize, in_h // blocksize,
        in_w // blocksize
    ]

    X = px.ops.any_op(op_name=px.stringify(name),
                      in_xlayers=[iX],
                      any_shape=shape,
                      onnx_id=name)

    return [X]
コード例 #16
0
def argmin(node: NodeWrapper, params: Dict[str, np.ndarray],
           xmap: Dict[str, XLayer]):
    """ ONNX ArgMin to XLayer AnyOp conversion function """

    logger.info("ONNX ArgMin -> XLayer AnyOp")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()

    iX = xmap[bottoms[0]]

    axis = int(node_attrs['axis']) if 'axis' in node_attrs else 0
    keepdims = bool(int(node_attrs['keepdims'])) if 'keepdims' in node_attrs \
        else True

    in_shape = iX.shapes.tolist()
    if keepdims:
        shape = [i if idx != axis else 1 for idx, i in enumerate(in_shape)]
    else:
        shape = [i for idx, i in enumerate(in_shape) if idx != axis]

    X = px.ops.any_op(op_name=px.stringify(name),
                      in_xlayers=[iX],
                      any_shape=shape,
                      onnx_id=name)

    return [X]
コード例 #17
0
def softmax(node: NodeWrapper,
            params: Dict[str, np.ndarray],
            xmap: Dict[str, XLayer]):
    """ ONNX Softmax to XLayer Softmax conversion function """

    logger.info("ONNX Softmax -> XLayer Softmax")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()

    iX = xmap[bottoms[0]]  # NCHW
    d = len(iX.shapes)

    axis = int(node_attrs['axis']) if 'axis' in node_attrs else 1
    if axis < 0:
        axis += d

    X = px.ops.softmax(
        op_name=px.stringify(name),
        in_xlayers=[iX],
        axis=axis,
        onnx_id=name)

    return [X]
コード例 #18
0
def resize(node: NodeWrapper, params: Dict[str, np.ndarray],
           xmap: Dict[str, XLayer]):
    """ ONNX Resize to XLayer AnyOp conversion function """

    logger.info("ONNX Resize -> XLayer AnyOp")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()

    iX = xmap[bottoms[0]]
    d = len(iX.shapes)
    in_shape = iX.shapes.tolist()

    if len(bottoms) == 4:
        out_shape = [int(i) for i in list(xmap[bottoms[3]].data[0])] \
            if len(bottoms) == 4 else None
    else:
        roi = [float(i) for i in list(xmap[bottoms[1]].data[0])]
        scales = [float(i) for i in list(xmap[bottoms[2]].data[0])]

        h = len(in_shape)

        out_shape = [
            math.floor(in_dim * (roi[idx + h] - roi[idx]) * scales[idx])
            for idx, in_dim in enumerate(in_shape)
        ]

    X = px.ops.any_op(op_name=px.stringify(name),
                      in_xlayers=[iX],
                      any_shape=out_shape,
                      onnx_id=name)

    return [X]
コード例 #19
0
def roi_align(node: NodeWrapper, params: Dict[str, np.ndarray],
              xmap: Dict[str, XLayer]):
    """ ONNX RoiAlign to XLayer AnyOp conversion function """

    logger.info("ONNX RoiAlign -> XLayer AnyOp")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()

    iX = xmap[bottoms[0]]
    _, in_c, _, _ = iX.shapes.tolist()
    in_shape = iX.shapes.tolist()

    rois = xmap[bottoms[1]]
    num_rois, _ = rois.shapes.tolist()

    out_h = node_attrs['output_height'] if 'output_height' in node_attrs \
        else 1
    out_w = node_attrs['output_width'] if 'output_width' in node_attrs \
        else 1

    out_shape = [num_rois, in_c, out_h, out_w]

    X = px.ops.any_op(op_name=px.stringify(name),
                      in_xlayers=[iX],
                      any_shape=out_shape,
                      onnx_id=name)

    return [X]
コード例 #20
0
def min(node: NodeWrapper, params: Dict[str, np.ndarray], xmap: Dict[str,
                                                                     XLayer]):
    """ ONNX Min to XLayer AnyOp conversion function """

    logger.info("ONNX Min -> XLayer AnyOp")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()

    aX = xmap[bottoms[0]]
    in_shape = aX.shapes.tolist()

    in_xlayers = [aX]

    for i in range(1, len(bottoms)):
        bX = xmap[bottoms[i]]
        in_shape = get_numpy_broadcasted_shape(in_shape, bX.shapes.tolist())
        in_xlayers.append(bX)

    X = px.ops.any_op(op_name=px.stringify(name),
                      in_xlayers=in_xlayers,
                      any_shape=in_shape,
                      onnx_id=name)

    return [X]
コード例 #21
0
def generic_reduce(op_type: str, node: NodeWrapper,
                   params: Dict[str, np.ndarray], xmap: Dict[str, XLayer]):
    """ ONNX Reduce to XLayer AnyOp conversion function """

    logger.info("ONNX {} -> XLayer AnyOp".format(op_type))

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()

    iX = xmap[bottoms[0]]
    d = len(iX.shapes)
    in_shape = iX.shapes.tolist()

    axes = [int(i) if i > 0 else int(d + i) for i in node_attrs['axes']]
    keepdims = bool(node_attrs['keepdims']) if 'keepdims' in node_attrs \
        else True

    out_shape = [(i if idx not in axes else 1)
                 for idx, i in enumerate(in_shape)
                 if (keepdims or i not in axes)]

    X = px.ops.any_op(op_name=px.stringify(name),
                      in_xlayers=[iX],
                      any_shape=out_shape,
                      onnx_id=name)

    return [X]
コード例 #22
0
def gather(node: NodeWrapper, params: Dict[str, np.ndarray],
           xmap: Dict[str, XLayer]):
    """ ONNX Gather to XLayer Take conversion function """

    logger.info("ONNX Gather -> XLayer Take")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()

    iX = xmap[bottoms[0]]
    d = len(iX.shapes)
    indices = xmap[bottoms[1]]

    axis = int(node_attrs['axis']) if 'axis' in node_attrs else 0
    if axis < 0:
        axis = axis + d

    X = px.ops.take(op_name=px.stringify(name),
                    in_xlayers=[iX, indices],
                    axis=axis,
                    onnx_id=name)

    return [X]
コード例 #23
0
def gather_nd(node: NodeWrapper, params: Dict[str, np.ndarray],
              xmap: Dict[str, XLayer]):
    """ ONNX GatherND to XLayer AnyOp conversion function """

    logger.info("ONNX GatherND -> XLayer AnyOp")

    raise NotImplementedError("ONNX GatherND operator translation not"
                              " supported")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()

    iX = xmap[bottoms[0]]
    d = len(iX.shapes)
    indices = xmap[bottoms[1]].data[0]

    batch_dims = node_attrs['batch_dims'] if 'batch_dims' in node_attrs \
        else 0

    in_shape = iX.shapes.tolist()
    if indices[-1] == (d - batch_dims):
        out_shape = in_shape[:batch_dims] + [len(indices)]
    out_shape = []
    for elem in indices:
        d = batch_dims + elem.flatten().shape[0]
        out_shape.extend(in_shape[d:])

    X = px.ops.any_op(op_name=px.stringify(name),
                      in_xlayers=[iX],
                      any_shape=out_shape,
                      onnx_id=name)

    return [X]
コード例 #24
0
def max_roi_pool(node: NodeWrapper, params: Dict[str, np.ndarray],
                 xmap: Dict[str, XLayer]):
    """ ONNX MaxRoiPool to XLayer AnyOp conversion function """

    logger.info("ONNX MaxRoiPool -> XLayer AnyOp")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()

    iX = xmap[bottoms[0]]  # NCHW
    _, in_c, in_h, in_w = iX.shapes
    rois = xmap[bottoms[1]]
    num_rois = rois.shapes[0]

    out_h, out_w = [int(i) for i in node_attrs['pooled_shape']]

    out_shape = [num_rois, in_c, out_h, out_w]

    X = px.ops.any_op(op_name=px.stringify(name),
                      in_xlayers=[iX],
                      any_shape=out_shape,
                      onnx_id=name)

    return [X]
コード例 #25
0
def global_max_pool(node: NodeWrapper, params: Dict[str, np.ndarray],
                    xmap: Dict[str, XLayer]):
    """ ONNX GlobalMaxPool to XLayer Pooling (Max) conversion function """

    logger.info("ONNX GlobalMaxPool -> XLayer Pooling (Max)")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()

    iX = xmap[bottoms[0]]  # NCHW
    _, in_c, in_h, in_w = iX.shapes

    # Quant_info (optional)
    vai_quant_in = node_attrs['vai_quant_in']\
        if 'vai_quant_in' in node_attrs else []
    vai_quant_out = node_attrs['vai_quant_out']\
        if 'vai_quant_out' in node_attrs else []
    vai_quant = node_attrs['vai_quant']\
        if 'vai_quant' in node_attrs else []

    X = xlf.get_xop_factory_func('GlobalPooling')(op_name=px.stringify(name),
                                                  input_layer=iX,
                                                  pool_type='Max',
                                                  layout='NCHW',
                                                  vai_quant=vai_quant,
                                                  vai_quant_in=vai_quant_in,
                                                  vai_quant_out=vai_quant_out,
                                                  onnx_id=name)

    return [X]
コード例 #26
0
def flatten(node, params, xmap):
    # type: (NodeWrapper, Dict[str, np.ndarray], Dict[str, XLayer])
    #   -> List[XLayer]
    """
    ONNX Flatten to XLayer Flatten or Reshape conversion function

    ONNX: Flattens the input tensor into a 2D matrix. If input tensor has
    shape (d_0, d_1, ... d_n) then the output will have shape
    (d_0 X d_1 ... d_(axis-1), d_axis X d_(axis+1) ... X dn).
    See https://github.com/onnx/onnx/blob/master/docs/Operators.md#Flatten
    """

    logger.info("ONNX Flatten -> XLayer Flatten/Reshape")

    assert len(node.get_outputs()) == 1
    assert len(node.get_inputs()) == 1

    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()

    iX = xmap[bottoms[0]]
    shape = iX.shapes.tolist()
    rank = len(shape)

    axis = node_attrs['axis'] if 'axis' in node_attrs else 1
    assert axis >= -rank and axis <= rank

    if axis == 1 or axis == -(rank - 1):
        X = xlf.get_xop_factory_func('Flatten')(op_name=px.stringify(name),
                                                input_layer=iX,
                                                onnx_id=name)
    else:
        shape_1 = int(np.prod(shape[:axis])) if shape[:axis] != [] else 1
        shape_2 = int(np.prod(shape[axis:])) if shape[axis:] != [] else 1

        newshape = [shape_1, shape_2]

        X = xlf.get_xop_factory_func('Reshape')(op_name=px.stringify(name),
                                                newshape=newshape,
                                                input_layer=iX,
                                                onnx_id=name)

    return [X]
コード例 #27
0
def sub(node: NodeWrapper,
        params: Dict[str, np.ndarray],
        xmap: Dict[str, XLayer]):
    """ ONNX Sub to XLayer Sub conversion function """

    logger.info("ONNX Sub -> XLayer Sub")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    assert len(bottoms) == 2
    node_attrs = node.get_attributes()

    aX = xmap[bottoms[0]]  # NCHW
    bX = xmap[bottoms[1]]  # NCHW

    # Quant_info (optional)
    vai_quant_in = node_attrs['vai_quant_in']\
        if 'vai_quant_in' in node_attrs else []
    vai_quant_out = node_attrs['vai_quant_out']\
        if 'vai_quant_out' in node_attrs else []
    vai_quant = node_attrs['vai_quant']\
        if 'vai_quant' in node_attrs else []

    if 'Constant' in aX.type and 'Constant' in bX.type:
        data = np.subtract(aX.data[0], bX.data[0])
        X = xlf.get_xop_factory_func('Constant')(
            op_name=px.stringify(name),
            value=data,
            onnx_id=name
        )
    else:
        X = px.ops.sub(
            op_name=px.stringify(name),
            in_xlayers=[aX, bX],
            vai_quant=vai_quant,
            vai_quant_in=vai_quant_in,
            vai_quant_out=vai_quant_out,
            onnx_id=name
        )

    return [X]
コード例 #28
0
def split(node: NodeWrapper, params: Dict[str, np.ndarray],
          xmap: Dict[str, XLayer]):
    """ ONNX Split to XLayer Split conversion function """

    logger.info("ONNX Split -> XLayer Split")

    # TODO first name is used for split for now
    name = node.get_outputs()[0]
    nb_outputs = len(node.get_outputs())
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()

    iX = xmap[bottoms[0]]
    d = len(iX.shapes)

    axis = int(node_attrs['axis']) if 'axis' in node_attrs else 0
    if axis < 0:
        axis = d + axis

    split = [int(s) for s in node_attrs['split']] if 'split' in node_attrs \
        else [int(iX.shapes[axis] // nb_outputs)] * nb_outputs
    indices = [split[0]]
    for i in range(1, len(split) - 1):
        indices.append(indices[i - 1] + split[i])

    Xs = []
    X = px.ops.split(op_name='split-' + px.stringify(name),
                     in_xlayers=[iX],
                     axis=axis,
                     indices=indices,
                     onnx_id=name)
    Xs.append(X)

    for idx, tensor_name in enumerate(node.get_outputs()):
        tgi_X = px.ops.tuple_get_item(op_name=px.stringify(tensor_name),
                                      in_xlayers=[X],
                                      index=idx,
                                      onnx_id=tensor_name)
        Xs.append(tgi_X)

    return Xs
コード例 #29
0
def relu(node: NodeWrapper, params: Dict[str, np.ndarray],
         xmap: Dict[str, XLayer]) -> List[XLayer]:
    """ONNX Relu to XLayer ReLU conversion function"""

    logger.info("ONNX Relu -> XLayer Relu")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()

    iX = xmap[bottoms[0]]  # NCHW
    X = px.ops.relu(px.stringify(name), [iX], onnx_id=name)
    return [X]
コード例 #30
0
def upsample(node: NodeWrapper, params: Dict[str, np.ndarray],
             xmap: Dict[str, XLayer]):
    """ ONNX Upsample to XLayer Upsampling2D conversion function """

    logger.info("ONNX Upsample -> XLayer Upsampling2D")

    assert len(node.get_outputs()) == 1
    name = node.get_outputs()[0]
    bottoms = node.get_inputs()
    node_attrs = node.get_attributes()

    assert len(bottoms) == 2 or 'scales' in node_attrs

    iX = xmap[bottoms[0]]  # NCHW

    scales = [
        float(i) for i in (list(xmap[bottoms[1]].data[0]) if 'scales' not in
                           node_attrs else node_attrs['scales'])
    ]
    assert len(scales) == len(iX.shapes)
    scale_n, scale_c, scale_h, scale_w = scales

    if scale_n != 1:
        raise NotImplementedError("Unsupported upsampling layer with scale"
                                  " for batch dim != 1")
    if scale_c != 1:
        raise NotImplementedError("Unsupported upsampling layer with scale"
                                  " for channel dim != 1")

    mode = node_attrs['mode'] if 'mode' in node_attrs \
        else 'nearest'
    if mode == 'nearest':
        mode = 'nearest_neighbor'

    # Quant_info (optional)
    vai_quant_in = node_attrs['vai_quant_in']\
        if 'vai_quant_in' in node_attrs else []
    vai_quant_out = node_attrs['vai_quant_out']\
        if 'vai_quant_out' in node_attrs else []
    vai_quant = node_attrs['vai_quant']\
        if 'vai_quant' in node_attrs else []

    X = px.ops.upsampling2d(op_name=px.stringify(name),
                            in_xlayers=[iX],
                            scale_h=scale_h,
                            scale_w=scale_w,
                            data_layout='NCHW',
                            method=mode,
                            onnx_id=name)

    return [X]