From 75080988ff96c984c554d047a9103192bcf4bbed Mon Sep 17 00:00:00 2001 From: wangzhen38 <41941775+wangzhen38@users.noreply.github.com> Date: Thu, 23 Jun 2022 11:44:34 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90code=20format=E3=80=91fix=20cpplint=20?= =?UTF-8?q?style=205=20(#43733)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix cpplint style 5 * fix cpplint style 5 * fix cpplint style 5 * fix cpplint style 5 * fix cpplint style 5 * fix cpplint style 5 * fix cpplint style 5 --- .../fluid/distributed/test/graph_node_test.cc | 86 ++++++++++--------- paddle/fluid/framework/archive.h | 38 ++++---- paddle/fluid/framework/heter_util.h | 17 ++-- .../framework/ir/mkldnn/cpu_bfloat16_pass.cc | 61 +++++++------ 4 files changed, 118 insertions(+), 84 deletions(-) diff --git a/paddle/fluid/distributed/test/graph_node_test.cc b/paddle/fluid/distributed/test/graph_node_test.cc index 9cb244a9ec4..3439ffe8a0c 100644 --- a/paddle/fluid/distributed/test/graph_node_test.cc +++ b/paddle/fluid/distributed/test/graph_node_test.cc @@ -228,32 +228,36 @@ void testFeatureNodeSerializeFloat64() { // void testCache(); void testGraphToBuffer(); -std::string edges[] = { - std::string("37\t45\t0.34"), std::string("37\t145\t0.31"), - std::string("37\t112\t0.21"), std::string("96\t48\t1.4"), - std::string("96\t247\t0.31"), std::string("96\t111\t1.21"), - std::string("59\t45\t0.34"), std::string("59\t145\t0.31"), - std::string("59\t122\t0.21"), std::string("97\t48\t0.34"), - std::string("97\t247\t0.31"), std::string("97\t111\t0.21")}; +const char* edges[] = {"37\t45\t0.34", + "37\t145\t0.31", + "37\t112\t0.21", + "96\t48\t1.4", + "96\t247\t0.31", + "96\t111\t1.21", + "59\t45\t0.34", + "59\t145\t0.31", + "59\t122\t0.21", + "97\t48\t0.34", + "97\t247\t0.31", + "97\t111\t0.21"}; char edge_file_name[] = "edges.txt"; -std::string nodes[] = { - std::string("user\t37\ta 0.34\tb 13 14\tc hello\td abc"), - std::string("user\t96\ta 0.31\tb 15 10\tc 96hello\td abcd"), - std::string("user\t59\ta 0.11\tb 11 14"), - std::string("user\t97\ta 0.11\tb 12 11"), - std::string("item\t45\ta 0.21"), - std::string("item\t145\ta 0.21"), - std::string("item\t112\ta 0.21"), - std::string("item\t48\ta 0.21"), - std::string("item\t247\ta 0.21"), - std::string("item\t111\ta 0.21"), - std::string("item\t46\ta 0.21"), - std::string("item\t146\ta 0.21"), - std::string("item\t122\ta 0.21"), - std::string("item\t49\ta 0.21"), - std::string("item\t248\ta 0.21"), - std::string("item\t113\ta 0.21")}; +const char* nodes[] = {"user\t37\ta 0.34\tb 13 14\tc hello\td abc", + "user\t96\ta 0.31\tb 15 10\tc 96hello\td abcd", + "user\t59\ta 0.11\tb 11 14", + "user\t97\ta 0.11\tb 12 11", + "item\t45\ta 0.21", + "item\t145\ta 0.21", + "item\t112\ta 0.21", + "item\t48\ta 0.21", + "item\t247\ta 0.21", + "item\t111\ta 0.21", + "item\t46\ta 0.21", + "item\t146\ta 0.21", + "item\t122\ta 0.21", + "item\t49\ta 0.21", + "item\t248\ta 0.21", + "item\t113\ta 0.21"}; char node_file_name[] = "nodes.txt"; void prepare_file(char file_name[], bool load_edge) { @@ -335,7 +339,8 @@ void GetDownpourSparseTableProto( /*-------------------------------------------------------------------------*/ -std::string ip_ = "127.0.0.1", ip2 = "127.0.0.1"; +const char* ip_ = "127.0.0.1"; +const char* ip2 = "127.0.0.1"; uint32_t port_ = 5209, port2 = 5210; std::vector host_sign_list_; @@ -382,8 +387,10 @@ void RunServer2() { } void RunClient( - std::map>& dense_regions, - int index, paddle::distributed::PsBaseService* service) { + const std::map>& + dense_regions, + int index, + paddle::distributed::PsBaseService* service) { ::paddle::distributed::PSParameter worker_proto = GetWorkerProto(); paddle::distributed::PaddlePSEnvironment _ps_env; auto servers_ = host_sign_list_.size(); @@ -533,8 +540,8 @@ void RunBrpcPushSparse() { VLOG(0) << "second bound"; client1.load_node_file(std::string("user"), std::string(node_file_name)); client1.load_node_file(std::string("item"), std::string(node_file_name)); - client1.load_edge_file(std::string("user2item"), std::string(edge_file_name), - 0); + client1.load_edge_file( + std::string("user2item"), std::string(edge_file_name), 0); nodes.clear(); VLOG(0) << "start to pull graph list"; nodes = client1.pull_graph_list(std::string("user"), 0, 1, 4, 1); @@ -551,8 +558,8 @@ void RunBrpcPushSparse() { std::cout << "check pull graph list by step " << test_step << std::endl; for (int server_id = 0; server_id < 2; server_id++) { for (int start_step = 0; start_step < test_step; start_step++) { - nodes = client1.pull_graph_list(std::string("item"), server_id, - start_step, 12, test_step); + nodes = client1.pull_graph_list( + std::string("item"), server_id, start_step, 12, test_step); for (auto g : nodes) { count_item_nodes.insert(g.get_id()); } @@ -570,14 +577,15 @@ void RunBrpcPushSparse() { std::vector node_ids; node_ids.push_back(96); node_ids.push_back(37); - res = client1.batch_sample_neighbors(std::string("user2item"), node_ids, 4, - true, false); + res = client1.batch_sample_neighbors( + std::string("user2item"), node_ids, 4, true, false); ASSERT_EQ(res.first[1].size(), 1); std::vector nodes_ids = client2.random_sample_nodes("user", 0, 6); ASSERT_EQ(nodes_ids.size(), 2); - ASSERT_EQ(true, (nodes_ids[0] == 59 && nodes_ids[1] == 37) || - (nodes_ids[0] == 37 && nodes_ids[1] == 59)); + ASSERT_EQ(true, + (nodes_ids[0] == 59 && nodes_ids[1] == 37) || + (nodes_ids[0] == 37 && nodes_ids[1] == 59)); VLOG(0) << "start to test get node feat"; // Test get node feat @@ -598,14 +606,14 @@ void RunBrpcPushSparse() { node_feat[1][0] = "helloworld"; - client1.set_node_feat(std::string("user"), node_ids, feature_names, - node_feat); + client1.set_node_feat( + std::string("user"), node_ids, feature_names, node_feat); // sleep(5); node_feat = client1.get_node_feat(std::string("user"), node_ids, feature_names); VLOG(0) << "get_node_feat: " << node_feat[1][0]; - ASSERT_TRUE(node_feat[1][0] == "helloworld"); + ASSERT_EQ(node_feat[1][0], "helloworld"); // Test string node_ids.clear(); @@ -698,7 +706,7 @@ void testGraphToBuffer() { s.set_feature(0, std::string("hhhh")); s.set_id(65); int size = s.get_size(true); - char str[size]; + char str[size]; // NOLINT s.to_buffer(str, true); s1.recover_from_buffer(str); ASSERT_EQ(s.get_id(), s1.get_id()); diff --git a/paddle/fluid/framework/archive.h b/paddle/fluid/framework/archive.h index 6a8f4ff47f3..a68470492e9 100644 --- a/paddle/fluid/framework/archive.h +++ b/paddle/fluid/framework/archive.h @@ -11,7 +11,6 @@ // 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. - #pragma once #if defined _WIN32 || defined __APPLE__ @@ -92,17 +91,21 @@ class ArchiveBase { char* Buffer() { return buffer_; } - void SetReadBuffer(char* buffer, size_t length, + void SetReadBuffer(char* buffer, + size_t length, std::function&& deleter) { SetBuffer(buffer, length, length, std::move(deleter)); } - void SetWriteBuffer(char* buffer, size_t capacity, + void SetWriteBuffer(char* buffer, + size_t capacity, std::function&& deleter) { SetBuffer(buffer, 0, capacity, std::move(deleter)); } - void SetBuffer(char* buffer, size_t length, size_t capacity, + void SetBuffer(char* buffer, + size_t length, + size_t capacity, std::function&& deleter) { CHECK(length <= capacity); FreeBuffer(); @@ -324,9 +327,10 @@ class Archive : public ArchiveBase { size_t temp = Limit() - Finish(); int len = snprintf(Finish(), temp, fmt, args...); CHECK(len >= 0); // NOLINT - if ((size_t)len >= temp) { + if (static_cast(len) >= temp) { PrepareWrite(len + 1); - CHECK(snprintf(Finish(), (size_t)len + 1, fmt, args...) == len); + CHECK(snprintf(Finish(), static_cast(len) + 1, fmt, args...) == + len); } AdvanceFinish(len); } @@ -351,7 +355,7 @@ Archive& operator>>(Archive& ar, T (&p)[N]) { template Archive& operator<<(Archive& ar, const std::vector& p) { #ifdef _LINUX - ar << (size_t)p.size(); + ar << static_cast(p.size()); #else ar << (uint64_t)p.size(); #endif @@ -377,7 +381,7 @@ Archive& operator>>(Archive& ar, std::vector& p) { template Archive& operator<<(Archive& ar, const std::valarray& p) { #ifdef _LINUX - ar << (size_t)p.size(); + ar << static_cast(p.size()); #else ar << (uint64_t)p.size(); #endif @@ -402,7 +406,7 @@ Archive& operator>>(Archive& ar, std::valarray& p) { inline BinaryArchive& operator<<(BinaryArchive& ar, const std::string& s) { #ifdef _LINUX - ar << (size_t)s.length(); + ar << static_cast(s.length()); #else ar << (uint64_t)s.length(); #endif @@ -482,13 +486,15 @@ Archive& operator<<(Archive& ar, const std::tuple& x) { #ifdef _LINUX template -Archive& DeserializeTuple(Archive& ar, std::tuple& x, // NOLINT +Archive& DeserializeTuple(const Archive& ar, + std::tuple& x, // NOLINT std::integral_constant n) { return ar; } #else template -Archive& DeserializeTuple(Archive& ar, std::tuple& x, // NOLINT +Archive& DeserializeTuple(const Archive& ar, + std::tuple& x, // NOLINT std::integral_constant n) { return ar; } @@ -496,14 +502,16 @@ Archive& DeserializeTuple(Archive& ar, std::tuple& x, // NOLINT #ifdef _LINUX template -Archive& DeserializeTuple(Archive& ar, std::tuple& x, // NOLINT +Archive& DeserializeTuple(const Archive& ar, + std::tuple& x, // NOLINT std::integral_constant n) { return DeserializeTuple(ar, x, std::integral_constant()) >> std::get(x); } #else template -Archive& DeserializeTuple(Archive& ar, std::tuple& x, // NOLINT +Archive& DeserializeTuple(const Archive& ar, + std::tuple& x, // NOLINT std::integral_constant n) { return DeserializeTuple(ar, x, std::integral_constant()) >> std::get(x); @@ -529,7 +537,7 @@ Archive& operator>>(Archive& ar, std::tuple& x) { template \ Archive& operator<<(Archive& ar, \ const MAP_TYPE& p) { \ - ar << (size_t)p.size(); \ + ar << static_cast(p.size()); \ for (auto it = p.begin(); it != p.end(); ++it) { \ ar << *it; \ } \ @@ -579,7 +587,7 @@ ARCHIVE_REPEAT(std::unordered_multimap, p.reserve(size)) #define ARCHIVE_REPEAT(SET_TYPE, RESERVE_STATEMENT) \ template \ Archive& operator<<(Archive& ar, const SET_TYPE& p) { \ - ar << (size_t)p.size(); \ + ar << static_cast(p.size()); \ for (auto it = p.begin(); it != p.end(); ++it) { \ ar << *it; \ } \ diff --git a/paddle/fluid/framework/heter_util.h b/paddle/fluid/framework/heter_util.h index b32b4df349f..c1966ba76bc 100644 --- a/paddle/fluid/framework/heter_util.h +++ b/paddle/fluid/framework/heter_util.h @@ -11,16 +11,17 @@ 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. */ - #pragma once #include +#include #include #include // NOLINT #include #include // NOLINT #include // NOLINT #include // NOLINT +#include #include #if defined(PADDLE_WITH_PSLIB) && !defined(PADDLE_WITH_HETERPS) #include "bthread/bthread.h" @@ -76,9 +77,13 @@ class HeterTask { << std::endl; } } - void PackTask(Scope* scope, int taskid, DataFeed* reader, int cur_batch, + void PackTask(Scope* scope, + int taskid, + DataFeed* reader, + int cur_batch, const ProgramDesc& program); - void PackGpuTask(Scope* thread_scope, DataFeed* reader, + void PackGpuTask(Scope* thread_scope, + DataFeed* reader, const ProgramDesc& program); Scope* scope_{nullptr}; @@ -145,7 +150,7 @@ class HeterObjectPool { #if defined(PADDLE_WITH_PSLIB) && !defined(PADDLE_WITH_HETERPS) struct BthreadMutextGuard { - BthreadMutextGuard(bthread_mutex_t* rho) { + explicit BthreadMutextGuard(bthread_mutex_t* rho) { mutex_ = rho; bthread_mutex_lock(mutex_); } @@ -220,7 +225,7 @@ class HeterList { void SetCap(int num) { cap_ = num; } - bool TryPut(K& key, T& value) { + bool TryPut(const K& key, const T& value) { std::unique_lock lock(mutex_); cond_.wait(lock, [this] { return size < cap_; }); if (task_map_.find(key) != task_map_.end()) { @@ -236,7 +241,7 @@ class HeterList { } } - bool Put(K& key, T& value) { + bool Put(const K& key, const T& value) { std::unique_lock lock(mutex_); cond_.wait(lock, [this] { return size < cap_; }); HeterNode* node = new HeterNode; diff --git a/paddle/fluid/framework/ir/mkldnn/cpu_bfloat16_pass.cc b/paddle/fluid/framework/ir/mkldnn/cpu_bfloat16_pass.cc index 58eec79344d..f1c1b57f3f6 100644 --- a/paddle/fluid/framework/ir/mkldnn/cpu_bfloat16_pass.cc +++ b/paddle/fluid/framework/ir/mkldnn/cpu_bfloat16_pass.cc @@ -11,7 +11,9 @@ limitations under the License. */ #include "paddle/fluid/framework/ir/mkldnn/cpu_bfloat16_pass.h" +#include #include +#include #include #include "paddle/fluid/framework/ir/graph_pattern_detector.h" @@ -43,7 +45,7 @@ class Quanter { VarDesc quant_x_desc( patterns::PDNodeName(get_op_type(), get_op_edge())); - auto quant_x_node = graph.CreateVarNode(&quant_x_desc); + auto quant_x_node = graph->CreateVarNode(&quant_x_desc); const auto xput_name = quant_x_node->Name(); quant_xput_names.emplace_back(xput_name); @@ -64,7 +66,7 @@ class Quanter { virtual ~Quanter() = default; protected: - Graph& graph; + Graph* graph; ir::Node* const op; std::map xputs_map; @@ -72,8 +74,10 @@ class Quanter { int counter = 0; - Quanter(Graph& graph, ir::Node* const op, const VariableNameMap& op_xputs) - : graph(graph), op(op), op_xputs(op_xputs){}; + Quanter(Graph* const graph, + ir::Node* const op, + const VariableNameMap& op_xputs) + : graph(graph), op(op), op_xputs(op_xputs) {} virtual bool IsNotPermittedOpType() const = 0; virtual bool IsNotPermittedName(const std::string& input_name) const = 0; @@ -101,10 +105,11 @@ class Quanter { op_desc.SetAttr("Scale", 1.f); op_desc.SetAttr("Shift", 0.0f); op_desc.SetAttr("bfloat16", true); - op_desc.SetAttr("output_format", op->Op()->HasAttr("data_layout") - ? op->Op()->GetAttr("data_layout") - : std::string("NCHW")); - return graph.CreateOpNode(&op_desc); // OpDesc will be copied. + op_desc.SetAttr("output_format", + op->Op()->HasAttr("data_layout") + ? op->Op()->GetAttr("data_layout") + : std::string("NCHW")); + return graph->CreateOpNode(&op_desc); // OpDesc will be copied. } void UnlinkNodes(ir::Node* a, ir::Node* b) const { @@ -118,16 +123,18 @@ class Quanter { class Quantizer final : public Quanter { public: Quantizer(Graph* const graph, ir::Node* const op) - : Quanter(*graph, op, op->Op()->Inputs()) { + : Quanter(graph, op, op->Op()->Inputs()) { auto inputs = op->inputs; PADDLE_ENFORCE_GE( - inputs.size(), 1, + inputs.size(), + 1, platform::errors::InvalidArgument( - "OP(%s)'s inputs(%d) must be equal or greater than 1.", op->Name(), + "OP(%s)'s inputs(%d) must be equal or greater than 1.", + op->Name(), inputs.size())); for (auto input : inputs) xputs_map[input->Name()] = input; - }; + } protected: bool IsNotPermittedOpType() const override { return false; } @@ -138,18 +145,20 @@ class Quantizer final : public Quanter { // Only the inputs listed in \"permitted_names\" // requires quanitization before the bfloat16 operator. // Other inputs, such as Filter and Bias are reordered in the kernel. - const std::vector permitted_names = {"X", "Y", "Input", - "ResidualData"}; + const std::vector permitted_names = { + "X", "Y", "Input", "ResidualData"}; return std::none_of( - permitted_names.begin(), permitted_names.end(), + permitted_names.begin(), + permitted_names.end(), [&input_name](const std::string& name) { return name == input_name; }); } std::string get_op_type() const override { return "quantize"; }; std::string get_op_edge() const override { return "out"; }; - void link_nodes(ir::Node* const physical_xput_node, ir::Node* const quant_op, + void link_nodes(ir::Node* const physical_xput_node, + ir::Node* const quant_op, ir::Node* const quant_x_node) override { UnlinkNodes(physical_xput_node, op); IR_NODE_LINK_TO(physical_xput_node, quant_op); @@ -166,16 +175,18 @@ class Quantizer final : public Quanter { class DeQuantizer final : public Quanter { public: DeQuantizer(Graph* const graph, ir::Node* const op) - : Quanter(*graph, op, op->Op()->Outputs()) { + : Quanter(graph, op, op->Op()->Outputs()) { auto outputs = op->outputs; PADDLE_ENFORCE_GE( - outputs.size(), 1, + outputs.size(), + 1, platform::errors::InvalidArgument( - "OP(%s)'s outputs(%d) must be equal or greater than 1.", op->Name(), + "OP(%s)'s outputs(%d) must be equal or greater than 1.", + op->Name(), outputs.size())); for (auto output : outputs) xputs_map[output->Name()] = output; - }; + } protected: bool IsNotPermittedOpType() const override { @@ -195,11 +206,12 @@ class DeQuantizer final : public Quanter { auto op_name = op->Name(); if (block_list.count(op_name)) { const auto& op_blocklist = block_list[op_name]; - blocked_outputs.insert(blocked_outputs.begin(), op_blocklist.begin(), - op_blocklist.end()); + blocked_outputs.insert( + blocked_outputs.begin(), op_blocklist.begin(), op_blocklist.end()); } - return std::any_of(blocked_outputs.begin(), blocked_outputs.end(), + return std::any_of(blocked_outputs.begin(), + blocked_outputs.end(), [&output_name](const std::string& name) { return name == output_name; }); @@ -208,7 +220,8 @@ class DeQuantizer final : public Quanter { std::string get_op_type() const override { return "dequantize"; }; std::string get_op_edge() const override { return "in"; }; - void link_nodes(ir::Node* const physical_xput_node, ir::Node* const quant_op, + void link_nodes(ir::Node* const physical_xput_node, + ir::Node* const quant_op, ir::Node* const quant_x_node) override { UnlinkNodes(op, physical_xput_node); IR_NODE_LINK_TO(quant_op, physical_xput_node); -- GitLab