def test_shape(test_dir, backend, a_shape, start, end, dtype): Op = "Shape" a = dtype(np.random.randn(*a_shape)) out = np.array(a.shape[start:end]).astype(np.int64) kwargs = {} if start is not None: kwargs["start"] = start if end is not None: kwargs["end"] = end node = onnx.helper.make_node(Op, inputs=["a"], outputs=["out"], **kwargs) graph = make_onnx_graph( node, inputs=[a], outputs=[out], tensors=[], tensor_names=[], name=Op + "_test", ) expected_output = run_onnx(graph, [a]) config = ONNXConfig(backend).parse_io(graph) compiler = Compiler(graph, config, test_dir, Frontend.ONNX) mpc_output = compiler.compile_and_run([a]) assert_almost_equal( model_output=expected_output, mpc_tensor=mpc_output, precision=2 ) return
def test_reducemean(test_dir, backend, a_shape, axes, keepdims, dtype): Op = "ReduceMean" a = dtype(np.random.randn(*a_shape)) out = np.mean( a, axis=(None if axes is None else tuple(axes)), keepdims=keepdims == 1 ) kwargs = {"name": Op, "inputs": ["a"], "outputs": ["out"], "keepdims": keepdims} if axes is not None: kwargs["axes"] = axes node = helper.make_node(Op, **kwargs) graph = make_onnx_graph( node, inputs=[a], outputs=[out], tensors=[], tensor_names=[], name=Op + "_test", ) expected_output = run_onnx(graph, [a]) config = ONNXConfig(backend).parse_io(graph) compiler = Compiler(graph, config, test_dir, Frontend.ONNX) mpc_output = compiler.compile_and_run([a]) assert_almost_equal( model_output=expected_output, mpc_tensor=mpc_output, precision=2 ) return
def test_matmul(test_dir, backend, a_shape, b_shape, transpose_a, transpose_b, bisModel, dtype): if backend == "2PC_HE": pytest.skip( "Assertion error in 2PC_HE FCField::matrix_multiplication Assertion `num_cols == 1' failed." ) graph = tf.Graph() a_inp = dtype(np.random.randn(*a_shape)) b_inp = dtype(np.random.randn(*b_shape)) with graph.as_default(): a = tf.compat.v1.placeholder(tf.as_dtype(dtype), shape=a_inp.shape, name="a") if bisModel: b = tf.constant(b_inp, name="b") else: b = tf.compat.v1.placeholder(tf.as_dtype(dtype), shape=b_inp.shape, name="b") output = tf.matmul(a, b, transpose_a, transpose_b, name="output") with tf.compat.v1.Session(graph=graph) as sess: feed_dict = {a: a_inp} if not bisModel: feed_dict[b] = b_inp expected_output = sess.run(output, feed_dict=feed_dict) config = Config(backend).add_input(a).add_output(output) if not bisModel: config.add_input(b) config.config["scale"] = 12 compiler = Compiler(graph, config, test_dir) mpc_output = compiler.compile_and_run([a_inp]) assert_almost_equal(tf_output=expected_output, mpc_tensor=mpc_output, precision=2) return
def test_arith_binop(test_dir, backend, Op, a_shape, b_shape, dtype): onnx_to_np_op = {"Add": np.add, "Sub": np.subtract, "Mul": np.multiply} a = np.random.randn(*a_shape).astype(dtype) b = np.random.randn(*b_shape).astype(dtype) out = onnx_to_np_op[Op](a, b) node = helper.make_node( Op, inputs=["a", "b"], outputs=["out"], ) graph = make_onnx_graph( node, inputs=[a], outputs=[out], tensors=[b], tensor_names=["b"], name=Op + "_test", ) expected_output = run_onnx(graph, [a]) config = ONNXConfig(backend).parse_io(graph) compiler = Compiler(graph, config, test_dir, Frontend.ONNX) mpc_output = compiler.compile_and_run([a]) assert_almost_equal(model_output=expected_output, mpc_tensor=mpc_output, precision=2) return
def test_pool(test_dir, backend, tfOp, a_shape, ksize, strides, padding, data_format, dtype): if backend.startswith("2PC") and tfOp == tf.nn.max_pool: pytest.skip("[SCI][maxpool] Output mismatch bug") graph = tf.Graph() a_inp = dtype(np.random.randn(*a_shape)) with graph.as_default(): a = tf.compat.v1.placeholder(tf.as_dtype(dtype), shape=a_inp.shape, name="a") output = tfOp( a, ksize=ksize, strides=strides, padding=padding, data_format=data_format, name="output", ) with tf.compat.v1.Session(graph=graph) as sess: expected_output = sess.run(output, feed_dict={a: a_inp}) config = Config(backend).add_input(a).add_output(output) compiler = Compiler(graph, config, test_dir) mpc_output = compiler.compile_and_run([a_inp]) assert_almost_equal(tf_output=expected_output, mpc_tensor=mpc_output, precision=2) return
def test_matmul(test_dir, backend, a_shape, b_shape, transpose_a, transpose_b, bisModel, dtype): if backend == "2PC_HE" and a_shape[0] != 1: pytest.skip("HE only supports vector matrix multiplication") graph = tf.Graph() a_inp = dtype(np.random.randn(*a_shape)) b_inp = dtype(np.random.randn(*b_shape)) with graph.as_default(): a = tf.compat.v1.placeholder(tf.as_dtype(dtype), shape=a_inp.shape, name="a") if bisModel: b = tf.constant(b_inp, name="b") else: b = tf.compat.v1.placeholder(tf.as_dtype(dtype), shape=b_inp.shape, name="b") output = tf.matmul(a, b, transpose_a, transpose_b, name="output") with tf.compat.v1.Session(graph=graph) as sess: feed_dict = {a: a_inp} if not bisModel: feed_dict[b] = b_inp expected_output = sess.run(output, feed_dict=feed_dict) config = TFConfig(backend).add_input(a).add_output(output) if not bisModel: config.add_input(b) compiler = Compiler(graph, config, test_dir) mpc_output = compiler.compile_and_run([a_inp]) assert_almost_equal(model_output=expected_output, mpc_tensor=mpc_output, precision=2) return
def test_fused_batch_norm(test_dir, backend, tfOp, a_shape, scale, offset, mean, variance, dtype): graph = tf.Graph() a_inp = dtype(np.random.randn(*a_shape)) with graph.as_default(): a = tf.compat.v1.placeholder(tf.as_dtype(dtype), shape=a_inp.shape, name="a") output = tfOp( x=a, scale=scale, offset=offset, mean=mean, variance=variance, is_training=False, name="output", ) with tf.compat.v1.Session(graph=graph) as sess: expected_output = sess.run(output, feed_dict={a: a_inp}) assert expected_output is not None config = Config(backend).add_input(a).add_output(output) compiler = Compiler(graph, config, test_dir) mpc_output = compiler.compile_and_run([a_inp]) assert_almost_equal(tf_output=expected_output, mpc_tensor=mpc_output, precision=2) return
def test_global_avgpool(test_dir, backend, a_shape, dtype): a = dtype(np.random.randn(*a_shape)) out = np.mean(a, axis=tuple(range(2, np.ndim(a))), keepdims=True) Op = "GlobalAveragePool" node = helper.make_node( Op, inputs=[ "a", ], outputs=["out"], ) graph = make_onnx_graph( node, inputs=[a], outputs=[out], tensors=[], tensor_names=[], name=Op + "_test", ) expected_output = run_onnx(graph, [a]) config = ONNXConfig(backend).parse_io(graph) compiler = Compiler(graph, config, test_dir, Frontend.ONNX) mpc_output = compiler.compile_and_run([a]) assert_almost_equal( model_output=expected_output, mpc_tensor=mpc_output, precision=2 ) return
def test_non_linear(test_dir, backend, Op, a_shape, dtype): a = np.random.randn(*a_shape).astype(dtype) if Op == "Relu": out = np.clip(a, 0, np.inf) elif Op == "Sigmoid": out = 1.0 / (1.0 + np.exp(np.negative(a))) elif Op == "Tanh": out = np.tanh(a) elif Op == "Sqrt": out = np.sqrt(a) node = helper.make_node( Op, inputs=[ "a", ], outputs=["out"], ) graph = make_onnx_graph( node, inputs=[a], outputs=[out], tensors=[], tensor_names=[], name=Op + "_test", ) expected_output = run_onnx(graph, [a]) config = ONNXConfig(backend).parse_io(graph) compiler = Compiler(graph, config, test_dir, Frontend.ONNX) mpc_output = compiler.compile_and_run([a]) assert_almost_equal(model_output=expected_output, mpc_tensor=mpc_output, precision=2) return
def test_conv_transpose( test_dir, backend, tfOp, a_shape, kernel_shape, output_shape, strides, padding, dtype, ): if backend in ["2PC_HE", "2PC_OT"]: pytest.skip("[conv3d] Missing Support in SCI") graph = tf.Graph() a_inp = dtype(np.random.randn(*a_shape)) kernel_inp = dtype(np.random.randn(*kernel_shape)) with graph.as_default(): a = tf.compat.v1.placeholder(tf.as_dtype(dtype), shape=a_inp.shape, name="a") filters = tf.constant(kernel_inp, name="filter") output = tfOp(a, filters, output_shape, strides, padding, name="output") with tf.compat.v1.Session(graph=graph) as sess: expected_output = sess.run(output, feed_dict={a: a_inp}) config = Config(backend).add_input(a).add_output(output) config.config["scale"] = 12 compiler = Compiler(graph, config, test_dir) mpc_output = compiler.compile_and_run([a_inp]) assert_almost_equal(tf_output=expected_output, mpc_tensor=mpc_output, precision=2) return
def test_uop(test_dir, backend, Op, a_shape, dtype): a = dtype(np.random.randn(*a_shape)) if Op == "Neg": out = np.negative(a) elif Op == "Floor": out = np.floor(a) elif Op == "Identity": out = np.negative(a) node = helper.make_node( Op, inputs=["a"], outputs=["out"], ) graph = make_onnx_graph( node, inputs=[a], outputs=[out], tensors=[], tensor_names=[], name=Op + "_test", ) expected_output = run_onnx(graph, [a]) config = ONNXConfig(backend).parse_io(graph) compiler = Compiler(graph, config, test_dir, Frontend.ONNX) mpc_output = compiler.compile_and_run([a]) assert_almost_equal( model_output=expected_output, mpc_tensor=mpc_output, precision=2 ) return
def test_equal(test_dir, backend, a_val, b_val, dtype): Op = "Equal" a = np.array(a_val).astype(dtype) b = np.array(b_val).astype(dtype) out = np.equal(a, b) node = helper.make_node( Op, inputs=["a", "b"], outputs=["out"], ) graph = make_onnx_graph( node, inputs=[a], outputs=[out], tensors=[b], tensor_names=["b"], name=Op + "_test", ) expected_output = run_onnx(graph, [a]) config = ONNXConfig(backend).parse_io(graph) compiler = Compiler(graph, config, test_dir, Frontend.ONNX) mpc_output = compiler.compile_and_run([a]) assert_almost_equal(model_output=expected_output, mpc_tensor=mpc_output, precision=2) return
def test_batch_norm(test_dir, backend, a_shape, scale_val, bias_val, mean_val, var_val, dtype): Op = "BatchNormalization" a = np.random.randn(*a_shape).astype(dtype) scale = np.array(scale_val).astype(dtype) bias = np.array(bias_val).astype(dtype) mean = np.array(mean_val).astype(dtype) var = np.array(var_val).astype(dtype) out = _batchnorm_test_mode(a, scale, bias, mean, var).astype(dtype) node = onnx.helper.make_node( Op, inputs=["a", "scale", "bias", "mean", "var"], outputs=["out"], ) graph = make_onnx_graph( node, inputs=[a], outputs=[out], tensors=[scale, bias, mean, var], tensor_names=["scale", "bias", "mean", "var"], name=Op + "_test", ) expected_output = run_onnx(graph, [a]) config = ONNXConfig(backend).parse_io(graph) compiler = Compiler(graph, config, test_dir, Frontend.ONNX) mpc_output = compiler.compile_and_run([a]) assert_almost_equal(model_output=expected_output, mpc_tensor=mpc_output, precision=2) return
def test_gemm(test_dir, backend, a_shape, b_shape, c_shape, alpha, beta, transA, transB, dtype): Op = "Gemm" a = np.random.randn(*a_shape).astype(dtype) b = np.random.randn(*b_shape).astype(dtype) kwargs = {"inputs": ["a", "b"], "outputs": ["out"]} npkwargs = {} if c_shape is not None: kwargs["inputs"].append("c") c = dtype(np.random.randn(*c_shape)) npkwargs["C"] = c if alpha is not None: kwargs["alpha"] = alpha npkwargs["alpha"] = alpha if beta is not None: kwargs["beta"] = beta npkwargs["beta"] = beta if transA == 1: kwargs["transA"] = 1 npkwargs["transA"] = 1 if transB == 1: kwargs["transB"] = 1 npkwargs["transB"] = 1 out = gemm_reference_implementation(a, b, **npkwargs) node = onnx.helper.make_node(Op, **kwargs) kwargs = { "inputs": [a], "outputs": [out], "tensors": [b], "tensor_names": ["b"], "name": Op + "_test", } if c_shape is not None: kwargs["tensors"].append(c) kwargs["tensor_names"].append("c") graph = make_onnx_graph(node, **kwargs) expected_output = run_onnx(graph, [a]) config = ONNXConfig(backend).parse_io(graph) compiler = Compiler(graph, config, test_dir, Frontend.ONNX) mpc_output = compiler.compile_and_run([a]) assert_almost_equal(model_output=expected_output, mpc_tensor=mpc_output, precision=2) return
def test_fill(test_dir, backend, a_shape, value): graph = tf.Graph() with graph.as_default(): output = tf.fill(a_shape, value) with tf.compat.v1.Session(graph=graph) as sess: expected_output = sess.run(output) config = Config(backend).add_output(output) compiler = Compiler(graph, config, test_dir) mpc_output = compiler.compile_and_run([], timeoutSeconds=60) assert_almost_equal(tf_output=expected_output, mpc_tensor=mpc_output, precision=2) return
def test_div(test_dir, backend, tfOp, a_val, divisor, dtype): graph = tf.Graph() a_inp = np.array(a_val) with graph.as_default(): b = tf.constant(divisor, name="b") a = tf.compat.v1.placeholder(tf.as_dtype(b.dtype), shape=a_inp.shape, name="a") output = tfOp(a, b, name="output") with tf.compat.v1.Session(graph=graph) as sess: expected_output = sess.run(output, feed_dict={a: a_inp}) config = Config(backend).add_input(a).add_output(output) compiler = Compiler(graph, config, test_dir) mpc_output = compiler.compile_and_run([a_inp]) assert_almost_equal(tf_output=expected_output, mpc_tensor=mpc_output, precision=2) return
def test_constant(test_dir, backend, shape, attribute): Op = "Constant" kwargs = {} print("Shape = ", shape) if attribute == "value": values = np.random.randn(*shape).astype(np.float32) kwargs[attribute] = onnx.helper.make_tensor( name="const_tensor", data_type=onnx.TensorProto.FLOAT, dims=values.shape, vals=values.flatten().astype(float), ) elif attribute == "value_float": values = np.random.randn(1).astype(np.float32) kwargs[attribute] = values[0] elif attribute == "value_floats": values = np.random.randn(*shape).astype(np.float32) kwargs[attribute] = values.flatten().astype(float) elif attribute == "value_int": values = np.array(np.random.randint(-(2 ** 32 - 1), 2 ** 32 - 1)).astype( np.int64 ) kwargs[attribute] = int(values) elif attribute == "value_ints": values = np.random.randint(-(2 ** 32 - 1), 2 ** 32 - 1, shape).astype(np.int32) print(values) kwargs[attribute] = values.flatten().astype(int) kwargs["inputs"] = [] kwargs["outputs"] = ["values"] node = helper.make_node(Op, **kwargs) graph = make_onnx_graph( node, inputs=[], outputs=[values], tensors=[], tensor_names=[], name=Op + "_test", ) expected_output = run_onnx(graph, []) config = ONNXConfig(backend).parse_io(graph) compiler = Compiler(graph, config, test_dir, Frontend.ONNX) mpc_output = compiler.compile_and_run([]) assert_almost_equal( model_output=expected_output, mpc_tensor=mpc_output, precision=2 ) return
def test_transpose(test_dir, backend, a_shape, perm, dtype): graph = tf.Graph() a_inp = dtype(np.random.randn(*a_shape)) with graph.as_default(): a = tf.compat.v1.placeholder(tf.as_dtype(dtype), shape=a_inp.shape, name="a") output = tf.transpose(a, perm, name="output") with tf.compat.v1.Session(graph=graph) as sess: expected_output = sess.run(output, feed_dict={a: a_inp}) config = TFConfig(backend).add_input(a).add_output(output) compiler = Compiler(graph, config, test_dir) mpc_output = compiler.compile_and_run([a_inp]) assert_almost_equal( model_output=expected_output, mpc_tensor=mpc_output, precision=2 ) return
def test_bias_add(test_dir, backend, a_shape, b_shape, data_format, dtype): graph = tf.Graph() a_inp = dtype(np.random.randn(*a_shape)) b_inp = dtype(np.random.randn(*b_shape)) with graph.as_default(): a = tf.compat.v1.placeholder(tf.as_dtype(dtype), shape=a_inp.shape, name="a") b = tf.constant(b_inp, name="b") output = tf.nn.bias_add(value=a, bias=b, data_format=data_format, name="output") with tf.compat.v1.Session(graph=graph) as sess: expected_output = sess.run(output, feed_dict={a: a_inp}) config = Config(backend).add_input(a).add_output(output) compiler = Compiler(graph, config, test_dir) mpc_output = compiler.compile_and_run([a_inp]) assert_almost_equal(tf_output=expected_output, mpc_tensor=mpc_output, precision=2) return
def test_conv(test_dir, backend, a_shape, kernel_shape, pads, strides, output_shape, group, dtype): Op = "Conv" if len(a_shape) == 4: version = 2 # 2d elif len(a_shape) == 5: version = 3 # 3d if version == 3 and backend in ["2PC_HE", "2PC_OT"]: pytest.skip("[conv3d] Missing Support in SCI") a = np.random.randn(*a_shape).astype(dtype) kernel = np.random.randn(*kernel_shape).astype(dtype) # Only need this for its shape out = np.zeros(output_shape).astype(dtype) hw_kernel_shape = kernel_shape[-version:] node = onnx.helper.make_node( Op, inputs=["a", "kernel"], outputs=["output"], kernel_shape=hw_kernel_shape, pads=pads, strides=strides, group=group, # Default values for other attributes: dilations=[1, 1], groups=1 ) graph = make_onnx_graph( node, inputs=[a], outputs=[out], tensors=[kernel], tensor_names=["kernel"], name=Op + "_test", ) expected_output = run_onnx(graph, [a]) config = ONNXConfig(backend).parse_io(graph) config.config["scale"] = 12 compiler = Compiler(graph, config, test_dir, Frontend.ONNX) mpc_output = compiler.compile_and_run([a]) assert_almost_equal(model_output=expected_output, mpc_tensor=mpc_output, precision=2) return
def test_maxpool( test_dir, backend, a_shape, kernel_shape, pads, strides, auto_pad, output_shape, dtype, ): Op = "MaxPool" a = np.random.randn(*a_shape).astype(dtype) # Only need this for its shape out = np.zeros(output_shape).astype(dtype) kwargs = { "inputs": ["a"], "outputs": ["output"], "kernel_shape": kernel_shape, "strides": strides, } if auto_pad is "NOTSET": kwargs["pads"] = pads else: kwargs["auto_pad"] = auto_pad node = onnx.helper.make_node(Op, **kwargs) graph = make_onnx_graph( node, inputs=[a], outputs=[out], tensors=[], tensor_names=[], name=Op + "_test", ) expected_output = run_onnx(graph, [a]) config = ONNXConfig(backend).parse_io(graph) compiler = Compiler(graph, config, test_dir, Frontend.ONNX) mpc_output = compiler.compile_and_run([a]) assert_almost_equal( model_output=expected_output, mpc_tensor=mpc_output, precision=2 ) return
def test_uop(test_dir, backend, tfOp, a_shape, dtype): if backend.startswith("2PC") and tfOp == tf.math.square: pytest.skip("[SCI][square] Secret Secret mul not implemented") graph = tf.Graph() a_inp = dtype(np.random.randn(*a_shape)) with graph.as_default(): a = tf.compat.v1.placeholder(tf.as_dtype(dtype), shape=a_inp.shape, name="a") output = tfOp(a, name="output") with tf.compat.v1.Session(graph=graph) as sess: expected_output = sess.run(output, feed_dict={a: a_inp}) config = Config(backend).add_input(a).add_output(output) compiler = Compiler(graph, config, test_dir) mpc_output = compiler.compile_and_run([a_inp]) assert_almost_equal(tf_output=expected_output, mpc_tensor=mpc_output, precision=2) return
def test_equal(test_dir, backend, a, b, dtype): graph = tf.Graph() a_inp = dtype(np.array(a)) b_inp = dtype(np.array(b)) with graph.as_default(): a = tf.compat.v1.placeholder(tf.as_dtype(dtype), shape=a_inp.shape, name="a") b = tf.constant(b_inp, name="b") output = tf.math.equal(a, b, name="output") with tf.compat.v1.Session(graph=graph) as sess: expected_output = sess.run(output, feed_dict={a: a_inp}) config = TFConfig(backend).add_input(a).add_output(output) compiler = Compiler(graph, config, test_dir) mpc_output = compiler.compile_and_run([a_inp]) assert_almost_equal(model_output=expected_output, mpc_tensor=mpc_output, precision=2) return
def test_conv(test_dir, backend, tfOp, a_shape, kernel_shape, strides, padding, dtype): graph = tf.Graph() a_inp = dtype(np.random.randn(*a_shape)) kernel_inp = dtype(np.random.randn(*kernel_shape)) with graph.as_default(): a = tf.compat.v1.placeholder(tf.as_dtype(dtype), shape=a_inp.shape, name="a") filters = tf.constant(kernel_inp, name="filter") output = tfOp(a, filters, strides, padding, name="output") with tf.compat.v1.Session(graph=graph) as sess: expected_output = sess.run(output, feed_dict={a: a_inp}) config = Config(backend).add_input(a).add_output(output) compiler = Compiler(graph, config, test_dir) mpc_output = compiler.compile_and_run([a_inp]) assert_almost_equal(tf_output=expected_output, mpc_tensor=mpc_output, precision=2) return
def test_matmul(test_dir, backend, a_shape, b_shape, bisModel, dtype): if backend == "2PC_HE" and a_shape[0] != 1: pytest.skip("HE only supports vector matrix multiplication") Op = "MatMul" a = np.random.randn(*a_shape).astype(dtype) b = np.random.randn(*b_shape).astype(dtype) out = np.matmul(a, b) node = onnx.helper.make_node( Op, inputs=["a", "b"], outputs=["out"], ) if not bisModel: graph = make_onnx_graph( node, inputs=[a, b], outputs=[out], name=Op + "_test", ) expected_output = run_onnx(graph, [a, b]) else: graph = make_onnx_graph( node, inputs=[a], outputs=[out], tensors=[b], tensor_names=["b"], name=Op + "_test", ) expected_output = run_onnx(graph, [a]) config = ONNXConfig(backend).parse_io(graph) compiler = Compiler(graph, config, test_dir, Frontend.ONNX) if not bisModel: mpc_output = compiler.compile_and_run([a, b]) else: mpc_output = compiler.compile_and_run([a]) assert_almost_equal(model_output=expected_output, mpc_tensor=mpc_output, precision=2) return
def test_split(test_dir, backend, a_shape, num_or_size_splits, axis, dtype): graph = tf.Graph() a_inp = dtype(np.random.randn(*a_shape)) with graph.as_default(): a = tf.compat.v1.placeholder(tf.as_dtype(dtype), shape=a_inp.shape, name="a") output = tf.split(a, num_or_size_splits, axis, name="output") with tf.compat.v1.Session(graph=graph) as sess: expected_output = sess.run(output, feed_dict={a: a_inp}) if type(output) == list: tf_output = output[-1] tf_expected_output = expected_output[-1] else: tf_output = output tf_expected_output = expected_output config = TFConfig(backend).add_input(a).add_output(tf_output) compiler = Compiler(graph, config, test_dir) mpc_output = compiler.compile_and_run([a_inp]) assert_almost_equal( model_output=tf_expected_output, mpc_tensor=mpc_output, precision=2 ) return
def test_pad(test_dir, backend, a_shape, paddings, mode, constant_values, dtype): graph = tf.Graph() a_inp = dtype(np.random.randn(*a_shape)) with graph.as_default(): a = tf.compat.v1.placeholder(tf.as_dtype(dtype), shape=a_inp.shape, name="a") pad = tf.constant(paddings, name="paddings") output = tf.pad(a, pad, mode=mode, constant_values=constant_values, name="output") with tf.compat.v1.Session(graph=graph) as sess: expected_output = sess.run(output, feed_dict={a: a_inp}) config = Config(backend).add_input(a).add_output(output) compiler = Compiler(graph, config, test_dir) mpc_output = compiler.compile_and_run([a_inp]) assert_almost_equal(tf_output=expected_output, mpc_tensor=mpc_output, precision=2) return
def test_non_linear(test_dir, backend, tfOp, a_shape, dtype): if backend not in ["2PC_OT", "CPP"] and tfOp in [ tf.math.sqrt, tf.math.rsqrt, tf.math.sigmoid, tf.math.tanh, ]: pytest.skip( "Operation {op} not supported for backend {backend}".format( op=tfOp.__name__, backend=backend)) if a_shape == []: pytest.skip( "[Athos] Missing Support for tan/sig/sqrt/relu of scalar (0-d) variables" ) graph = tf.Graph() a_inp = dtype(np.random.randn(*a_shape)) if tfOp in [tf.math.sqrt, tf.math.rsqrt]: a_inp = np.abs(a_inp) with graph.as_default(): a = tf.compat.v1.placeholder(tf.as_dtype(dtype), shape=a_inp.shape, name="a") output = tfOp(a, name="output") with tf.compat.v1.Session(graph=graph) as sess: expected_output = sess.run(output, feed_dict={a: a_inp}) assert expected_output is not None config = TFConfig(backend).add_input(a).add_output(output) config.config["scale"] = 12 compiler = Compiler(graph, config, test_dir) mpc_output = compiler.compile_and_run([a_inp]) assert_almost_equal(model_output=expected_output, mpc_tensor=mpc_output, precision=2) return
def test_cast(test_dir, backend, from_type, to_type, compile_time): Op = "Cast" shape = (3, 4) if "STRING" != from_type: input = np.random.random_sample(shape).astype( TENSOR_TYPE_TO_NP_TYPE[getattr(TensorProto, from_type)] ) if "STRING" == to_type: # Converting input to str, then give it object dtype for generating script ss = [] for i in input.flatten(): s = str(i).encode("utf-8") su = s.decode("utf-8") ss.append(su) output = np.array(ss).astype(object).reshape([3, 4]) else: output = input.astype(TENSOR_TYPE_TO_NP_TYPE[getattr(TensorProto, to_type)]) else: input = np.array( [ "0.47892547", "0.48033667", "0.49968487", "0.81910545", "0.47031248", "0.816468", "0.21087195", "0.7229038", "NaN", "INF", "+INF", "-INF", ], dtype=np.dtype(object), ).reshape([3, 4]) output = input.astype(TENSOR_TYPE_TO_NP_TYPE[getattr(TensorProto, to_type)]) node = onnx.helper.make_node( Op, inputs=["input"], outputs=["output"], to=getattr(TensorProto, to_type), ) if compile_time == True: graph = make_onnx_graph( node, inputs=[], outputs=[output], tensors=[input], tensor_names=["input"], name=Op + "_test", ) expected_output = run_onnx(graph, []) else: graph = make_onnx_graph( node, inputs=[input], outputs=[output], tensors=[], tensor_names=[], name=Op + "_test", ) expected_output = run_onnx(graph, [input]) config = ONNXConfig(backend).parse_io(graph) compiler = Compiler(graph, config, test_dir, Frontend.ONNX) if compile_time == True: mpc_output = compiler.compile_and_run([]) else: mpc_output = compiler.compile_and_run([input]) assert_almost_equal( model_output=expected_output, mpc_tensor=mpc_output, precision=2 ) return