/* Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include #include "gtest/gtest.h" #include "paddle/fluid/framework/tensor.h" #include "paddle/fluid/framework/operator.h" #include "paddle/fluid/framework/scope.h" #include "aby3_context.h" #include "core/paddlefl_mpc/mpc_protocol/mesh_network.h" #include "core/paddlefl_mpc/mpc_protocol/context_holder.h" #include "fixedpoint_tensor.h" namespace aby3 { using g_ctx_holder = paddle::mpc::ContextHolder; using Fix64N16 = FixedPointTensor; using AbstractContext = paddle::mpc::AbstractContext; class FixedTensorTest : public ::testing::Test { public: paddle::platform::CPUDeviceContext _cpu_ctx; std::shared_ptr _exec_ctx; std::shared_ptr _mpc_ctx[3]; std::shared_ptr _store; std::thread _t[3]; std::shared_ptr _s_tensor_factory; virtual ~FixedTensorTest() noexcept {} void SetUp() { paddle::framework::OperatorBase* op = nullptr; paddle::framework::Scope scope; paddle::framework::RuntimeContext ctx({}, {}); // only device_ctx is needed _exec_ctx = std::make_shared( *op, scope, _cpu_ctx, ctx); _store = std::make_shared(); std::thread t[3]; for (size_t i = 0; i < 3; ++i) { _t[i] = std::thread(&FixedTensorTest::gen_mpc_ctx, this, i); } for (auto& ti : _t) { ti.join(); } _s_tensor_factory = std::make_shared(&_cpu_ctx); } std::shared_ptr gen_network(size_t idx) { return std::make_shared(idx, "127.0.0.1", 3, "test_prefix", _store); } void gen_mpc_ctx(size_t idx) { auto net = gen_network(idx); net->init(); _mpc_ctx[idx] = std::make_shared(idx, net); } std::shared_ptr> gen(std::vector shape) { return _s_tensor_factory->template create(shape); } }; std::shared_ptr> gen(std::vector shape) { return g_ctx_holder::tensor_factory()->template create(shape); } template PaddleTensor test_fixedt_gen_paddle_tensor(std::vector& input, std::vector& shape, paddle::platform::CPUDeviceContext& cpu_ctx) { PaddleTensor ret(&cpu_ctx); ret.reshape(shape); T* ret_ptr = ret.data(); for (int i = 0; i < ret.numel(); i++) { *(ret_ptr + i) = (T) (input[i] * pow(2, N)); } return ret; } template bool test_fixedt_check_tensor_eq(const TensorAdapter* in1, const TensorAdapter* in2, double precision = 0.0001) { // check shape std::vector shape1, shape2; shape1 = in1->shape(); shape2 = in2->shape(); size_t scale = in1->scaling_factor(); if (shape1.size() != shape2.size()) { std::cout << "shape size error: shape1.size: "<numel(); i++) { if (std::abs(*(in1->data() + i) - *(in2->data() + i)) > precision * pow(2, scale)) { std::cout << "result error: inx: "<