<commit_before>/* Copyright (c) 2016 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 <fstream>
#include <ostream>
#include <thread>  // NOLINT
#include <vector>

#include "paddle/fluid/operators/async_listen_and_serv_op.h"

#include "paddle/utils/StringUtil.h"

namespace paddle {
namespace operators {

void RunServer(std::shared_ptr<detail::SyncGRPCServer> service) {
  service->RunAsyncUpdate();
  VLOG(4) << "RunServer thread end";
}

static void AsyncExecuteBlock(framework::Executor *executor,
                              framework::ExecutorPrepareContext *prepared,
                              framework::Scope *scope) {
  framework::Async([&executor, &prepared, &scope]() {
    try {
      executor->RunPreparedContext(prepared, scope, false, false);
    } catch (std::exception &e) {
      LOG(ERROR) << "run sub program error " << e.what();
    }
  });
}

AsyncListenAndServOp::AsyncListenAndServOp(
    const std::string &type, const framework::VariableNameMap &inputs,
    const framework::VariableNameMap &outputs,
    const framework::AttributeMap &attrs)
    : OperatorBase(type, inputs, outputs, attrs) {}

int AsyncListenAndServOp::GetSelectedPort() const {
  return rpc_service_->GetSelectedPort();
}

void AsyncListenAndServOp::Stop() {
  rpc_service_->Push(LISTEN_TERMINATE_MESSAGE);
  server_thread_->join();
}

void AsyncListenAndServOp::RunImpl(const framework::Scope &scope,
                                   const platform::Place &dev_place) const {
  platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance();
  auto &dev_ctx = *pool.Get(dev_place);
  framework::Scope &recv_scope = scope.NewScope();

  if (!rpc_service_) {
    std::string endpoint = Attr<std::string>("endpoint");
    rpc_service_.reset(new detail::SyncGRPCServer(endpoint));
  }

  // grad name to block id
  std::unordered_map<std::string, int32_t> grad_to_id;
  std::unordered_map<int32_t, std::string> id_to_grad;

  auto grad_map_str = Attr<std::vector<std::string>>("grad_map");
  for (auto &grad_and_id : grad_map_str) {
    std::vector<std::string> pieces;
    paddle::str::split(grad_and_id, ' ', &pieces);
    PADDLE_ENFORCE_EQ(pieces.size(), 2);
    PADDLE_ENFORCE_EQ(grad_to_id.count(pieces[0]), 0);
    int block_id = std::stoi(pieces[1]);
    grad_to_id[pieces[0]] = block_id;
    id_to_grad[block_id] = pieces[0];
  }

  auto *optimize_block = Attr<framework::BlockDesc *>(kOptimizeBlock);
  auto *prefetch_block = Attr<framework::BlockDesc *>(kPrefetchBlock);
  auto *program = optimize_block->Program();
  size_t num_blocks = program->Size();
  PADDLE_ENFORCE_GE(num_blocks, 2,
                    "server program should have at least 2 blocks");

  framework::Executor executor(dev_place);
  std::vector<int> block_list;
  for (size_t blkid = 1; blkid < num_blocks; ++blkid) {
    if (blkid != static_cast<size_t>(prefetch_block->ID())) {
      block_list.push_back(blkid);
    }
  }
  PADDLE_ENFORCE_EQ(grad_map_str.size(), block_list.size(),
                    "grad num should be equal to optimize block num");
  auto optimize_prepared = executor.Prepare(*program, block_list);

  std::unordered_map<std::string,
                     std::shared_ptr<framework::ExecutorPrepareContext>>
      grad_to_prepared;
  for (size_t i = 0; i < block_list.size(); ++i) {<commit_msg>add split string<commit_after>/* Copyright (c) 2016 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 <fstream>
#include <ostream>
#include <thread>  // NOLINT
#include <vector>

#include "paddle/fluid/operators/async_listen_and_serv_op.h"

#include "paddle/utils/StringUtil.h"

namespace paddle {
namespace operators {

static void split(const std::string &str, char sep,
                  std::vector<std::string> *pieces) {
  pieces->clear();
  if (str.empty()) {
    return;
  }
  size_t pos = 0;
  size_t next = str.find(sep, pos);
  while (next != std::string::npos) {
    pieces->push_back(str.substr(pos, next - pos));
    pos = next + 1;
    next = str.find(sep, pos);
  }
  if (!str.substr(pos).empty()) {
    pieces->push_back(str.substr(pos));
  }
}

void RunServer(std::shared_ptr<detail::SyncGRPCServer> service) {
  service->RunAsyncUpdate();
  VLOG(4) << "RunServer thread end";
}

static void AsyncExecuteBlock(framework::Executor *executor,
                              framework::ExecutorPrepareContext *prepared,
                              framework::Scope *scope) {
  framework::Async([&executor, &prepared, &scope]() {
    try {
      executor->RunPreparedContext(prepared, scope, false, false);
    } catch (std::exception &e) {
      LOG(ERROR) << "run sub program error " << e.what();
    }
  });
}

AsyncListenAndServOp::AsyncListenAndServOp(
    const std::string &type, const framework::VariableNameMap &inputs,
    const framework::VariableNameMap &outputs,
    const framework::AttributeMap &attrs)
    : OperatorBase(type, inputs, outputs, attrs) {}

int AsyncListenAndServOp::GetSelectedPort() const {
  return rpc_service_->GetSelectedPort();
}

void AsyncListenAndServOp::Stop() {
  rpc_service_->Push(LISTEN_TERMINATE_MESSAGE);
  server_thread_->join();
}

void AsyncListenAndServOp::RunImpl(const framework::Scope &scope,
                                   const platform::Place &dev_place) const {
  platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance();
  auto &dev_ctx = *pool.Get(dev_place);
  framework::Scope &recv_scope = scope.NewScope();

  if (!rpc_service_) {
    std::string endpoint = Attr<std::string>("endpoint");
    rpc_service_.reset(new detail::SyncGRPCServer(endpoint));
  }

  // grad name to block id
  std::unordered_map<std::string, int32_t> grad_to_id;
  std::unordered_map<int32_t, std::string> id_to_grad;

  auto grad_map_str = Attr<std::vector<std::string>>("grad_map");
  for (auto &grad_and_id : grad_map_str) {
    std::vector<std::string> pieces;
    split(grad_and_id, ' ', &pieces);
    PADDLE_ENFORCE_EQ(pieces.size(), 2);
    PADDLE_ENFORCE_EQ(grad_to_id.count(pieces[0]), 0);
    int block_id = std::stoi(pieces[1]);
    grad_to_id[pieces[0]] = block_id;
    id_to_grad[block_id] = pieces[0];
  }

  auto *optimize_block = Attr<framework::BlockDesc *>(kOptimizeBlock);
  auto *prefetch_block = Attr<framework::BlockDesc *>(kPrefetchBlock);
  auto *program = optimize_block->Program();
  size_t num_blocks = program->Size();
  PADDLE_ENFORCE_GE(num_blocks, 2,
                    "server program should have at least 2 blocks");

  framework::Executor executor(dev_place);
  std::vector<int> block_list;
  for (size_t blkid = 1; blkid < num_blocks; ++blkid) {
    if (blkid != static_cast<size_t>(prefetch_block->ID())) {
      block_list.push_back(blkid);
    }
  }
  PADDLE_ENFORCE_EQ(grad_map_str.size(), block_list.size(),
                    "grad num should be equal to optimize block num");
  auto optimize_prepared = executor.Prepare(*program, block_list);

  std::unordered_map<std::string,
                     std::shared_ptr<framework::ExecutorPrepareContext>>
      grad_to_prepared;
  for (size_t i = 0; i < block_list.size(); ++i) {
{'fraction_non_alphanumeric': 0.1281704013789707, 'fraction_numerical': 0.005909874415168678, 'mean_word_length': 4.18379068283344, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '929248', 'n_tokens_mistral': 2641, 'n_tokens_neox': 2542, 'n_words': 605}