def decorrelate_transformer_converter(scope, operator, container):
    op = operator.raw_operator
    opv = container.target_opset
    out = operator.outputs

    X = operator.inputs[0]

    dtype = guess_numpy_type(X.type)
    options = container.get_options(op, dict(use_gemm=False))
    use_gemm = options['use_gemm']
    print('conversion: use_gemm=', use_gemm)

    if use_gemm:
        Y = OnnxGemm(X,
                     op.coef_.astype(dtype),
                     (-op.mean_ @ op.coef_).astype(dtype),
                     op_version=opv,
                     alpha=1.,
                     beta=1.,
                     output_names=out[:1])
    else:
        Y = OnnxMatMul(OnnxSub(X, op.mean_.astype(dtype), op_version=opv),
                       op.coef_.astype(dtype),
                       op_version=opv,
                       output_names=out[:1])
    Y.add_to(scope, container)
Beispiel #2
0
 def test_onnx_micro_runtime_gemm(self):
     opset = TestOnnxMicroRuntime.opset
     x = numpy.array([1, 2, 4, 5]).astype(numpy.float32).reshape((2, 2))
     for ta in [0, 1]:
         for tb in [0, 1]:
             cop = OnnxGemm('X',
                            'X',
                            'X',
                            op_version=opset,
                            alpha=1.,
                            beta=1.,
                            output_names=['Y'],
                            transA=ta,
                            transB=tb)
             model_def = cop.to_onnx({'X': x}, target_opset=opset)
             rt = OnnxMicroRuntime(model_def)
             out = rt.run({'X': x})
             xa = x.T if ta else x
             xb = x.T if tb else x
             self.assertEqual(numpy.matmul(xa, xb) + x, out['Y'])
def decorrelate_transformer_converter(scope, operator, container):
    op = operator.raw_operator
    opv = container.target_opset
    out = operator.outputs

    X = operator.inputs[0]

    dtype = guess_numpy_type(X.type)

    Y1 = OnnxMatMul(
        OnnxSub(X, op.mean_.astype(dtype), op_version=opv),
        op.coef_.astype(dtype),
        op_version=opv, output_names=out[:1])

    Y2 = OnnxGemm(X, op.coef_.astype(dtype),
                  (- op.mean_ @ op.coef_).astype(dtype),
                  op_version=opv, alpha=1., beta=1.,
                  output_names=out[1:2])

    Y1.add_to(scope, container)
    Y2.add_to(scope, container)
Beispiel #4
0
        def conv(scope, operator, container):
            X = operator.inputs[0]
            out = operator.outputs
            op = operator.raw_operator

            C = op.cluster_centers_
            C2 = row_norms(C, squared=True)

            N = X.type.shape[0]
            zeros = np.zeros((N, ))

            rs = OnnxReduceSumSquare(X, axes=[1], keepdims=1)
            z = OnnxAdd(rs, OnnxGemm(X, C, zeros, alpha=-2., transB=1))
            y2 = OnnxAdd(C2, z)
            lo = OnnxArgMin(y2, axis=1, keepdims=0, output_names=out[:1])
            y2s = OnnxSqrt(y2, output_names=out[1:])

            lo.add_to(scope, container)
            y2s.add_to(scope, container)
        def conv(scope, operator, container):
            X = operator.inputs[0]
            out = operator.outputs
            op = operator.raw_operator
            dtype = guess_numpy_type(X.type)

            C = op.cluster_centers_
            C2 = row_norms(C, squared=True).astype(dtype)
            C = C.astype(dtype)

            rs = OnnxReduceSumSquare(X,
                                     axes=[1],
                                     keepdims=1,
                                     op_version=container.target_opset)

            N = X.type.shape[0]
            if isinstance(N, int):
                zeros = np.zeros((N, ))
            else:
                zeros = OnnxMul(rs,
                                np.array([0], dtype=np.float32),
                                op_version=container.target_opset)

            z = OnnxAdd(rs,
                        OnnxGemm(X,
                                 C,
                                 zeros,
                                 alpha=-2.,
                                 transB=1,
                                 op_version=container.target_opset),
                        op_version=container.target_opset)
            y2 = OnnxAdd(C2, z, op_version=container.target_opset)
            lo = OnnxArgMin(y2,
                            axis=1,
                            keepdims=0,
                            output_names=out[:1],
                            op_version=container.target_opset)
            y2s = OnnxSqrt(y2,
                           output_names=out[1:],
                           op_version=container.target_opset)

            lo.add_to(scope, container)
            y2s.add_to(scope, container)
    def test_onnxt_runtime_gemm(self):
        idi = numpy.array([[1, 0], [1, 1]], dtype=numpy.float64)
        cst = numpy.array([4, 5], dtype=numpy.float32)
        X = numpy.array([[1, 2], [3, 4]], dtype=numpy.float64)

        onx = OnnxGemm('X', idi, cst, output_names=['Y'])
        model_def = onx.to_onnx({'X': idi.astype(numpy.float32)})
        oinf = OnnxInference(model_def)
        got = oinf.run({'X': X})
        self.assertEqual(list(sorted(got)), ['Y'])
        self.assertEqualArray(numpy.dot(X, idi) + cst, got['Y'], decimal=6)

        onx = OnnxGemm('X', idi, cst, transA=1, transB=1, output_names=['Y'])
        model_def = onx.to_onnx({'X': idi.astype(numpy.float32)})
        oinf = OnnxInference(model_def)
        got = oinf.run({'X': X})
        self.assertEqual(list(sorted(got)), ['Y'])
        self.assertEqualArray(numpy.dot(X.T, idi.T) + cst, got['Y'], decimal=6)

        onx = OnnxGemm('X', idi, cst, transA=1, output_names=['Y'])
        model_def = onx.to_onnx({'X': idi.astype(numpy.float32)})
        oinf = OnnxInference(model_def)
        got = oinf.run({'X': X})
        self.assertEqual(list(sorted(got)), ['Y'])
        self.assertEqualArray(numpy.dot(X.T, idi) + cst, got['Y'], decimal=6)

        onx = OnnxGemm('X', idi, cst, transB=1, output_names=['Y'])
        model_def = onx.to_onnx({'X': idi.astype(numpy.float32)})
        oinf = OnnxInference(model_def)
        got = oinf.run({'X': X})
        self.assertEqual(list(sorted(got)), ['Y'])
        self.assertEqualArray(numpy.dot(X, idi.T) + cst, got['Y'], decimal=6)