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 #ifndef MINDSPORE_CCSRC_FL_SERVER_KERNEL_UPDATE_MODEL_KERNEL_H_ 18 #define MINDSPORE_CCSRC_FL_SERVER_KERNEL_UPDATE_MODEL_KERNEL_H_ 19 20 #include <map> 21 #include <memory> 22 #include <string> 23 #include <vector> 24 #include "fl/server/common.h" 25 #include "fl/server/kernel/round/round_kernel.h" 26 #include "fl/server/kernel/round/round_kernel_factory.h" 27 #include "fl/server/executor.h" 28 #ifdef ENABLE_ARMOUR 29 #include "fl/armour/cipher/cipher_meta_storage.h" 30 #endif 31 32 namespace mindspore { 33 namespace fl { 34 namespace server { 35 namespace kernel { 36 // The initial data size sum of federated learning is 0, which will be accumulated in updateModel round. 37 constexpr uint64_t kInitialDataSizeSum = 0; 38 39 class UpdateModelKernel : public RoundKernel { 40 public: UpdateModelKernel()41 UpdateModelKernel() : executor_(nullptr), iteration_time_window_(0) {} 42 ~UpdateModelKernel() override = default; 43 44 void InitKernel(size_t threshold_count) override; 45 bool Launch(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace, 46 const std::vector<AddressPtr> &outputs); 47 bool Reset() override; 48 49 // In some cases, the last updateModel message means this server iteration is finished. 50 void OnLastCountEvent(const std::shared_ptr<ps::core::MessageHandler> &message) override; 51 52 private: 53 ResultCode ReachThresholdForUpdateModel(const std::shared_ptr<FBBuilder> &fbb); 54 ResultCode UpdateModel(const schema::RequestUpdateModel *update_model_req, const std::shared_ptr<FBBuilder> &fbb); 55 std::map<std::string, UploadData> ParseFeatureMap(const schema::RequestUpdateModel *update_model_req); 56 ResultCode CountForUpdateModel(const std::shared_ptr<FBBuilder> &fbb, 57 const schema::RequestUpdateModel *update_model_req); 58 void BuildUpdateModelRsp(const std::shared_ptr<FBBuilder> &fbb, const schema::ResponseCode retcode, 59 const std::string &reason, const std::string &next_req_time); 60 61 // The executor is for updating the model for updateModel request. 62 Executor *executor_; 63 64 // The time window of one iteration. 65 size_t iteration_time_window_; 66 }; 67 } // namespace kernel 68 } // namespace server 69 } // namespace fl 70 } // namespace mindspore 71 #endif // MINDSPORE_CCSRC_FL_SERVER_KERNEL_UPDATE_MODEL_KERNEL_H_ 72