• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "fl/armour/cipher/cipher_unmask.h"
18 #include "fl/server/common.h"
19 #include "fl/server/local_meta_store.h"
20 #include "fl/armour/cipher/cipher_meta_storage.h"
21 
22 namespace mindspore {
23 namespace armour {
UnMask(const std::map<std::string,AddressPtr> & data)24 bool CipherUnmask::UnMask(const std::map<std::string, AddressPtr> &data) {
25   MS_LOG(INFO) << "CipherMgr::UnMask START";
26   clock_t start_time = clock();
27   std::vector<float> noise;
28 
29   bool ret = cipher_init_->cipher_meta_storage_.GetClientNoisesFromServer(fl::server::kCtxClientNoises, &noise);
30   if (!ret || noise.size() != cipher_init_->featuremap_) {
31     MS_LOG(WARNING) << "Client noises is not ready";
32     return false;
33   }
34 
35   size_t data_size = fl::server::LocalMetaStore::GetInstance().value<size_t>(fl::server::kCtxFedAvgTotalDataSize);
36   if (data_size == 0) {
37     MS_LOG(ERROR) << "FedAvgTotalDataSize equals to 0";
38     return false;
39   }
40   int sum_size = 0;
41   for (auto iter = data.begin(); iter != data.end(); ++iter) {
42     if (iter->second == nullptr) {
43       MS_LOG(ERROR) << "AddressPtr is nullptr";
44       return false;
45     }
46     size_t size_data = iter->second->size / sizeof(float);
47     float *in_data = reinterpret_cast<float *>(iter->second->addr);
48     for (size_t i = 0; i < size_data; ++i) {
49       in_data[i] = in_data[i] + noise[i + IntToSize(sum_size)] / data_size;
50     }
51     sum_size += IntToSize(size_data);
52     for (size_t i = 0; i < data.size(); ++i) {
53       MS_LOG(INFO) << " index : " << i << " in_data unmask: " << in_data[i] * data_size;
54     }
55   }
56   MS_LOG(INFO) << "CipherMgr::UnMask sum_size : " << sum_size;
57   MS_LOG(INFO) << "CipherMgr::UnMask feature_map : " << cipher_init_->featuremap_;
58   clock_t end_time = clock();
59   double duration = static_cast<double>((end_time - start_time) * 1.0 / CLOCKS_PER_SEC);
60   MS_LOG(INFO) << "Unmask success time is : " << duration;
61   return true;
62 }
63 }  // namespace armour
64 }  // namespace mindspore
65