From c914dd302356868fddfcf3bba792443a4470d95e Mon Sep 17 00:00:00 2001 From: liuruilong Date: Tue, 5 Jun 2018 14:17:52 +0800 Subject: [PATCH] remove unused codes --- CMakeLists.txt | 2 +- src/common/log.cpp | 17 -- src/common/protobuf-c.c | 144 --------------- src/common/protobuf-c.h | 108 ------------ src/common/variant.cpp | 13 -- src/framework/data_transform.h | 3 +- src/framework/operator.h | 7 +- src/framework/paddle_mobile_object.cpp | 15 -- src/framework/paddle_mobile_object.h | 32 ---- src/framework/program/block_desc.h | 3 +- src/framework/program/op_desc.h | 3 +- .../program-optimize/fusion_op_register.cpp | 15 -- .../program-optimize/fusion_op_register.h | 2 +- src/framework/program/program-optimize/node.h | 3 +- .../program-optimize/program_optimize.cpp | 4 - src/framework/program/program.h | 3 +- src/framework/program/program_desc.h | 3 +- src/framework/program/var_desc.h | 71 +------- src/framework/selected_rows.h | 77 -------- src/framework/var_type.h | 35 ---- src/framework/variable.h | 3 +- src/io.cpp | 166 +++++++++--------- src/io.h | 3 +- src/operators/op_param.h | 2 +- 24 files changed, 102 insertions(+), 632 deletions(-) delete mode 100644 src/common/log.cpp delete mode 100644 src/common/variant.cpp delete mode 100644 src/framework/paddle_mobile_object.cpp delete mode 100644 src/framework/paddle_mobile_object.h delete mode 100644 src/framework/program/program-optimize/fusion_op_register.cpp delete mode 100644 src/framework/selected_rows.h delete mode 100644 src/framework/var_type.h diff --git a/CMakeLists.txt b/CMakeLists.txt index bd2299f2a7..5635dff9ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.0) project(paddle-mobile) -add_definitions(-DPADDLE_MOBILE_DEBUG) +#add_definitions(-DPADDLE_MOBILE_DEBUG) add_definitions(-DENABLE_EXCEPTION) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") diff --git a/src/common/log.cpp b/src/common/log.cpp deleted file mode 100644 index dbc4554c5f..0000000000 --- a/src/common/log.cpp +++ /dev/null @@ -1,17 +0,0 @@ -/* Copyright (c) 2018 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 "log.h" - -namespace paddle_mobile {} diff --git a/src/common/protobuf-c.c b/src/common/protobuf-c.c index fd0e3d80a2..d90daeecc9 100644 --- a/src/common/protobuf-c.c +++ b/src/common/protobuf-c.c @@ -2638,147 +2638,3 @@ protobuf_c_boolean protobuf_c_message_check(const ProtobufCMessage *message) { typedef void (*GenericHandler)(void *service, const ProtobufCMessage *input, ProtobufCClosure closure, void *closure_data); -void protobuf_c_service_invoke_internal(ProtobufCService *service, - unsigned method_index, - const ProtobufCMessage *input, - ProtobufCClosure closure, - void *closure_data) { - GenericHandler *handlers; - GenericHandler handler; - - /* - * Verify that method_index is within range. If this fails, you are - * likely invoking a newly added method on an old service. (Although - * other memory corruption bugs can cause this assertion too.) - */ - assert(method_index < service->descriptor->n_methods); - - /* - * Get the array of virtual methods (which are enumerated by the - * generated code). - */ - handlers = (GenericHandler *)(service + 1); - - /* - * Get our method and invoke it. - * \todo Seems like handler == NULL is a situation that needs handling. - */ - handler = handlers[method_index]; - (*handler)(service, input, closure, closure_data); -} - -void protobuf_c_service_generated_init( - ProtobufCService *service, const ProtobufCServiceDescriptor *descriptor, - ProtobufCServiceDestroy destroy) { - ASSERT_IS_SERVICE_DESCRIPTOR(descriptor); - service->descriptor = descriptor; - service->destroy = destroy; - service->invoke = protobuf_c_service_invoke_internal; - memset(service + 1, 0, descriptor->n_methods * sizeof(GenericHandler)); -} - -void protobuf_c_service_destroy(ProtobufCService *service) { - service->destroy(service); -} - -/* --- querying the descriptors --- */ - -const ProtobufCEnumValue *protobuf_c_enum_descriptor_get_value_by_name( - const ProtobufCEnumDescriptor *desc, const char *name) { - unsigned start = 0; - unsigned count; - - if (desc == NULL || desc->values_by_name == NULL) return NULL; - - count = desc->n_value_names; - - while (count > 1) { - unsigned mid = start + count / 2; - int rv = strcmp(desc->values_by_name[mid].name, name); - if (rv == 0) - return desc->values + desc->values_by_name[mid].index; - else if (rv < 0) { - count = start + count - (mid + 1); - start = mid + 1; - } else - count = mid - start; - } - if (count == 0) return NULL; - if (strcmp(desc->values_by_name[start].name, name) == 0) - return desc->values + desc->values_by_name[start].index; - return NULL; -} - -const ProtobufCEnumValue *protobuf_c_enum_descriptor_get_value( - const ProtobufCEnumDescriptor *desc, int value) { - int rv = int_range_lookup(desc->n_value_ranges, desc->value_ranges, value); - if (rv < 0) return NULL; - return desc->values + rv; -} - -const ProtobufCFieldDescriptor *protobuf_c_message_descriptor_get_field_by_name( - const ProtobufCMessageDescriptor *desc, const char *name) { - unsigned start = 0; - unsigned count; - const ProtobufCFieldDescriptor *field; - - if (desc == NULL || desc->fields_sorted_by_name == NULL) return NULL; - - count = desc->n_fields; - - while (count > 1) { - unsigned mid = start + count / 2; - int rv; - field = desc->fields + desc->fields_sorted_by_name[mid]; - rv = strcmp(field->name, name); - if (rv == 0) - return field; - else if (rv < 0) { - count = start + count - (mid + 1); - start = mid + 1; - } else - count = mid - start; - } - if (count == 0) return NULL; - field = desc->fields + desc->fields_sorted_by_name[start]; - if (strcmp(field->name, name) == 0) return field; - return NULL; -} - -const ProtobufCFieldDescriptor *protobuf_c_message_descriptor_get_field( - const ProtobufCMessageDescriptor *desc, unsigned value) { - int rv = int_range_lookup(desc->n_field_ranges, desc->field_ranges, value); - if (rv < 0) return NULL; - return desc->fields + rv; -} - -const ProtobufCMethodDescriptor * -protobuf_c_service_descriptor_get_method_by_name( - const ProtobufCServiceDescriptor *desc, const char *name) { - unsigned start = 0; - unsigned count; - - if (desc == NULL || desc->method_indices_by_name == NULL) return NULL; - - count = desc->n_methods; - - while (count > 1) { - unsigned mid = start + count / 2; - unsigned mid_index = desc->method_indices_by_name[mid]; - const char *mid_name = desc->methods[mid_index].name; - int rv = strcmp(mid_name, name); - - if (rv == 0) return desc->methods + desc->method_indices_by_name[mid]; - if (rv < 0) { - count = start + count - (mid + 1); - start = mid + 1; - } else { - count = mid - start; - } - } - if (count == 0) return NULL; - if (strcmp(desc->methods[desc->method_indices_by_name[start]].name, name) == - 0) - return desc->methods + desc->method_indices_by_name[start]; - return NULL; -} diff --git a/src/common/protobuf-c.h b/src/common/protobuf-c.h index a04559d79a..db86d88b86 100644 --- a/src/common/protobuf-c.h +++ b/src/common/protobuf-c.h @@ -798,76 +798,6 @@ uint32_t protobuf_c_version_number(void); */ #define PROTOBUF_C_MIN_COMPILER_VERSION 1000000 -/** - * Look up a `ProtobufCEnumValue` from a `ProtobufCEnumDescriptor` by name. - * - * \param desc - * The `ProtobufCEnumDescriptor` object. - * \param name - * The `name` field from the corresponding `ProtobufCEnumValue` object to - * match. - * \return - * A `ProtobufCEnumValue` object. - * \retval NULL - * If not found or if the optimize_for = CODE_SIZE option was set. - */ -PROTOBUF_C__API -const ProtobufCEnumValue *protobuf_c_enum_descriptor_get_value_by_name( - const ProtobufCEnumDescriptor *desc, const char *name); - -/** - * Look up a `ProtobufCEnumValue` from a `ProtobufCEnumDescriptor` by numeric - * value. - * - * \param desc - * The `ProtobufCEnumDescriptor` object. - * \param value - * The `value` field from the corresponding `ProtobufCEnumValue` object to - * match. - * - * \return - * A `ProtobufCEnumValue` object. - * \retval NULL - * If not found. - */ -PROTOBUF_C__API -const ProtobufCEnumValue *protobuf_c_enum_descriptor_get_value( - const ProtobufCEnumDescriptor *desc, int value); - -/** - * Look up a `ProtobufCFieldDescriptor` from a `ProtobufCMessageDescriptor` by - * the name of the field. - * - * \param desc - * The `ProtobufCMessageDescriptor` object. - * \param name - * The name of the field. - * \return - * A `ProtobufCFieldDescriptor` object. - * \retval NULL - * If not found or if the optimize_for = CODE_SIZE option was set. - */ -PROTOBUF_C__API -const ProtobufCFieldDescriptor *protobuf_c_message_descriptor_get_field_by_name( - const ProtobufCMessageDescriptor *desc, const char *name); - -/** - * Look up a `ProtobufCFieldDescriptor` from a `ProtobufCMessageDescriptor` by - * the tag value of the field. - * - * \param desc - * The `ProtobufCMessageDescriptor` object. - * \param value - * The tag value of the field. - * \return - * A `ProtobufCFieldDescriptor` object. - * \retval NULL - * If not found. - */ -PROTOBUF_C__API -const ProtobufCFieldDescriptor *protobuf_c_message_descriptor_get_field( - const ProtobufCMessageDescriptor *desc, unsigned value); - /** * Determine the number of bytes required to store the serialised message. * @@ -947,32 +877,6 @@ PROTOBUF_C__API void protobuf_c_message_init(const ProtobufCMessageDescriptor *descriptor, void *message); -/** - * Free a service. - * - * \param service - * The service object to free. - */ -PROTOBUF_C__API -void protobuf_c_service_destroy(ProtobufCService *service); - -/** - * Look up a `ProtobufCMethodDescriptor` by name. - * - * \param desc - * Service descriptor. - * \param name - * Name of the method. - * - * \return - * A `ProtobufCMethodDescriptor` object. - * \retval NULL - * If not found or if the optimize_for = CODE_SIZE option was set. - */ -PROTOBUF_C__API -const ProtobufCMethodDescriptor * -protobuf_c_service_descriptor_get_method_by_name( - const ProtobufCServiceDescriptor *desc, const char *name); /** * Initialise a `ProtobufCBufferSimple` object. @@ -1011,18 +915,6 @@ PROTOBUF_C__API void protobuf_c_buffer_simple_append(ProtobufCBuffer *buffer, size_t len, const unsigned char *data); -PROTOBUF_C__API -void protobuf_c_service_generated_init( - ProtobufCService *service, const ProtobufCServiceDescriptor *descriptor, - ProtobufCServiceDestroy destroy); - -PROTOBUF_C__API -void protobuf_c_service_invoke_internal(ProtobufCService *service, - unsigned method_index, - const ProtobufCMessage *input, - ProtobufCClosure closure, - void *closure_data); - /**@}*/ PROTOBUF_C__END_DECLS diff --git a/src/common/variant.cpp b/src/common/variant.cpp deleted file mode 100644 index 6bbf34eae9..0000000000 --- a/src/common/variant.cpp +++ /dev/null @@ -1,13 +0,0 @@ -/* Copyright (c) 2018 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. */ diff --git a/src/framework/data_transform.h b/src/framework/data_transform.h index b3947985d8..e380eacf0e 100644 --- a/src/framework/data_transform.h +++ b/src/framework/data_transform.h @@ -18,10 +18,9 @@ limitations under the License. */ #include #include -#include "framework/op_kernel_type.h" -#include "framework/selected_rows.h" #include "framework/tensor.h" #include "framework/variable.h" +#include "framework/op_kernel_type.h" namespace paddle_mobile { namespace framework { diff --git a/src/framework/operator.h b/src/framework/operator.h index 5fd88a6b20..17a8359d8c 100644 --- a/src/framework/operator.h +++ b/src/framework/operator.h @@ -27,7 +27,6 @@ limitations under the License. */ #include "framework/op_info.h" #include "framework/op_kernel_type.h" #include "framework/op_registry.h" -#include "framework/paddle_mobile_object.h" #include "framework/program/block_desc.h" #include "framework/program/program-optimize/node.h" #include "framework/scope.h" @@ -52,7 +51,7 @@ static T *GetVarValue(const string &key, const VariableNameMap &var_map, } template -class OperatorBase : PaddleMobileObject { +class OperatorBase { public: /* * @b op 基类的实例化方法, op 获取到了 输入、参数以及提前分配好的输出 tensor @@ -121,7 +120,7 @@ class OperatorWithKernel : public OperatorBase { * @b 所有kernel的父类 * */ template -class OpKernelBase : PaddleMobileObject { +class OpKernelBase { public: /* * @b 所有kernel 需实现 Compute 方法 @@ -139,7 +138,7 @@ class OpKernelBase : PaddleMobileObject { std::shared_ptr<::paddle_mobile::framework::Scope> scope) \ : parent_cls(type, inputs, outputs, attrs, scope) {} -class FusionOpMatcher : PaddleMobileObject { +class FusionOpMatcher { public: FusionOpMatcher() {} diff --git a/src/framework/paddle_mobile_object.cpp b/src/framework/paddle_mobile_object.cpp deleted file mode 100644 index acf37a3c11..0000000000 --- a/src/framework/paddle_mobile_object.cpp +++ /dev/null @@ -1,15 +0,0 @@ -/* Copyright (c) 2018 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 "paddle_mobile_object.h" diff --git a/src/framework/paddle_mobile_object.h b/src/framework/paddle_mobile_object.h deleted file mode 100644 index aff4b6c1f1..0000000000 --- a/src/framework/paddle_mobile_object.h +++ /dev/null @@ -1,32 +0,0 @@ -/* Copyright (c) 2018 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. */ - -#pragma once - -#include -#include "stdio.h" - -namespace paddle_mobile { - -class PaddleMobileObject { - public: - virtual std::string ToString() { - char address[128] = {0}; - sprintf(address, "%p", this); - return std::string(address); - } - - private: -}; -} // namespace paddle_mobile diff --git a/src/framework/program/block_desc.h b/src/framework/program/block_desc.h index 84d7a90fc1..e788ab7269 100644 --- a/src/framework/program/block_desc.h +++ b/src/framework/program/block_desc.h @@ -15,14 +15,13 @@ limitations under the License. */ #pragma once #include "framework/framework.pb-c.h" -#include "framework/paddle_mobile_object.h" #include "framework/program/op_desc.h" #include "framework/program/var_desc.h" namespace paddle_mobile { namespace framework { -class BlockDesc : PaddleMobileObject { +class BlockDesc { public: friend class Node; friend class ProgramOptimize; diff --git a/src/framework/program/op_desc.h b/src/framework/program/op_desc.h index 07b903085d..4fdfac253f 100644 --- a/src/framework/program/op_desc.h +++ b/src/framework/program/op_desc.h @@ -20,12 +20,11 @@ limitations under the License. */ #include "common/log.h" #include "common/type_define.h" #include "framework/framework.pb-c.h" -#include "framework/paddle_mobile_object.h" namespace paddle_mobile { namespace framework { -class OpDesc : PaddleMobileObject { +class OpDesc { public: friend class ProgramOptimize; friend class FusionOpMatcher; diff --git a/src/framework/program/program-optimize/fusion_op_register.cpp b/src/framework/program/program-optimize/fusion_op_register.cpp deleted file mode 100644 index 010585166c..0000000000 --- a/src/framework/program/program-optimize/fusion_op_register.cpp +++ /dev/null @@ -1,15 +0,0 @@ -/* Copyright (c) 2018 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 "fusion_op_register.h" diff --git a/src/framework/program/program-optimize/fusion_op_register.h b/src/framework/program/program-optimize/fusion_op_register.h index 4cc83f8c80..ff50dcafbc 100644 --- a/src/framework/program/program-optimize/fusion_op_register.h +++ b/src/framework/program/program-optimize/fusion_op_register.h @@ -17,8 +17,8 @@ limitations under the License. */ #include #include -#include "framework/operator.h" #include "node.h" +#include "framework/operator.h" namespace paddle_mobile { namespace framework { diff --git a/src/framework/program/program-optimize/node.h b/src/framework/program/program-optimize/node.h index 84a7477dc4..24d9ce2fe5 100644 --- a/src/framework/program/program-optimize/node.h +++ b/src/framework/program/program-optimize/node.h @@ -21,13 +21,12 @@ limitations under the License. */ #include #include "common/log.h" -#include "framework/paddle_mobile_object.h" #include "framework/program/op_desc.h" namespace paddle_mobile { namespace framework { -class Node : PaddleMobileObject { +class Node { friend class ProgramOptimize; public: diff --git a/src/framework/program/program-optimize/program_optimize.cpp b/src/framework/program/program-optimize/program_optimize.cpp index 9f657d5369..15724523de 100644 --- a/src/framework/program/program-optimize/program_optimize.cpp +++ b/src/framework/program/program-optimize/program_optimize.cpp @@ -105,11 +105,7 @@ std::shared_ptr ProgramOptimize::FushionOptimize( } } - // DLOG << "node: \n" << *begin_node; - std::vector> op_descs; - // bool can_splite = begin_node->CanSplit({G_OP_TYPE_CONV, - // G_OP_TYPE_BATCHNORM, G_OP_TYPE_DEPTHWISE_CONV}); for (int m = 0; m < nodes.size(); ++m) { auto &node = nodes[m]; op_descs.push_back(node->op_desc_); diff --git a/src/framework/program/program.h b/src/framework/program/program.h index 3a9cbfc1d9..2cea562b70 100644 --- a/src/framework/program/program.h +++ b/src/framework/program/program.h @@ -15,7 +15,6 @@ limitations under the License. */ #pragma once #include "common/types.h" -#include "framework/paddle_mobile_object.h" #include "framework/program/program_desc.h" #include "framework/scope.h" @@ -23,7 +22,7 @@ namespace paddle_mobile { namespace framework { template -class Program : PaddleMobileObject { +class Program { public: std::shared_ptr originProgram; std::shared_ptr optimizeProgram; diff --git a/src/framework/program/program_desc.h b/src/framework/program/program_desc.h index 6aa7dd44ce..5c87f565e1 100644 --- a/src/framework/program/program_desc.h +++ b/src/framework/program/program_desc.h @@ -18,13 +18,12 @@ limitations under the License. */ #include "common/types.h" #include "framework/framework.pb-c.h" -#include "framework/paddle_mobile_object.h" #include "framework/program/block_desc.h" namespace paddle_mobile { namespace framework { -class ProgramDesc : PaddleMobileObject { +class ProgramDesc { public: friend class Node; friend class ProgramOptimize; diff --git a/src/framework/program/var_desc.h b/src/framework/program/var_desc.h index 5ab2fc5617..f6f04f2c70 100644 --- a/src/framework/program/var_desc.h +++ b/src/framework/program/var_desc.h @@ -14,40 +14,14 @@ limitations under the License. */ #pragma once +#include + #include "framework/framework.pb-c.h" -#include "framework/paddle_mobile_object.h" #include "framework/program/tensor_desc.h" namespace paddle_mobile { namespace framework { -/* - -PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__BOOL = 0, - PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__INT16 = 1, - PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__INT32 = 2, - PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__INT64 = 3, - PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__FP16 = 4, - PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__FP32 = 5, - PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__FP64 = 6, - - PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__LOD_TENSOR = 7, - PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__SELECTED_ROWS = 8, - PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__FEED_MINIBATCH = 9, - PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__FETCH_LIST = 10, - PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__STEP_SCOPES = 11, - PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__LOD_RANK_TABLE = 12, - PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__LOD_TENSOR_ARRAY = 13, - PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__PLACE_LIST = 14, - PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__READER = 15, - PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__CHANNEL = 16, - - PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__RAW = 17, - PADDLE_MOBILE__FRAMEWORK__PROTO__VAR_TYPE__TYPE__TUPLE = 18 - - - */ - class VarDesc { public: VarDesc(const VarDesc &var_desc) { @@ -56,14 +30,6 @@ class VarDesc { this->persistable_ = var_desc.persistable_; this->tensor_desc_ = var_desc.tensor_desc_; this->type_ = var_desc.type_; - /* - * - * std::string name_; - bool persistable_; - TensorDesc tensor_desc_; - VarType_Type type_; - VarType_Type data_type_; - * */ } VarDesc(PaddleMobile__Framework__Proto__VarDesc *desc) { type_ = (VarType_Type)desc->type->type; @@ -102,39 +68,6 @@ class VarDesc { const TensorDesc &Tensor_desc() const { return tensor_desc_; } - // const proto::VarType::ChannelDesc &channel_desc() const { - // switch (desc_.type().type()) { - // case proto::VarType::CHANNEL: - // return desc_.type().channel(); - // default: - // break; - // } - // } - - // proto::VarType::Type GetDataType() const { - // switch (desc_.type().type()) { - // case proto::VarType::CHANNEL: - // return channel_desc().data_type(); - // break; - // default: - // return tensor_desc().data_type(); - // } - // } - - // template - // std::vector RepeatedToVector( - // const google::protobuf::RepeatedField &repeated_field) const { - // std::vector ret; - // ret.reserve(repeated_field.size()); - // std::copy(repeated_field.begin(), repeated_field.end(), - // std::back_inserter(ret)); - // return ret; - // } - - // std::vector GetShape() const { - // return this->RepeatedToVector(tensor_desc().dims()); - // } - private: std::string name_; bool persistable_; diff --git a/src/framework/selected_rows.h b/src/framework/selected_rows.h deleted file mode 100644 index f59bd1aabf..0000000000 --- a/src/framework/selected_rows.h +++ /dev/null @@ -1,77 +0,0 @@ -/* Copyright (c) 2018 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. */ - -#pragma once - -#include - -#include "lod_tensor.h" -#include "tensor.h" - -namespace paddle_mobile { -namespace framework { - -class SelectedRows { - public: - SelectedRows(const std::vector &rows, const int64_t &height) - : rows_(rows), height_(height) { - value_.reset(new Tensor()); - } - - SelectedRows() { - height_ = 0; - value_.reset(new Tensor()); - } - - const Tensor &value() const { return *value_; } - - Tensor *mutable_value() { return value_.get(); } - - int64_t height() const { return height_; } - - void set_height(int64_t height) { height_ = height; } - - const std::vector &rows() const { return rows_; } - - std::vector *mutable_rows() { return &rows_; } - - void set_rows(const std::vector &rows) { rows_ = rows; } - - /** - * get the index of id in rows - */ - int64_t index(int64_t id) const { - auto it = std::find(rows_.begin(), rows_.end(), id); - // PADDLE_ENFORCE(it != rows_.end(), "id should be in rows"); - return static_cast(std::distance(rows_.begin(), it)); - } - - DDim GetCompleteDims() const { - std::vector dims = vectorize(value_->dims()); - dims[0] = height_; - return make_ddim(dims); - } - - private: - // Notice: rows can be duplicate. We can have {0, 4, 7, 0, 5, 7, 9} - // here. - // SelectedRows are simply concated when adding together. Until a - // SelectedRows add a Tensor, will the duplicate rows be handled. - std::vector rows_; - std::unique_ptr value_{nullptr}; - int64_t height_; -}; - -} // namespace framework -} // namespace paddle_mobile diff --git a/src/framework/var_type.h b/src/framework/var_type.h deleted file mode 100644 index 5e132c7375..0000000000 --- a/src/framework/var_type.h +++ /dev/null @@ -1,35 +0,0 @@ -/* Copyright (c) 2018 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. */ - -#pragma once -#include "framework.pb.h" -#include "lod_tensor.h" -#include "selected_rows.h" -#include "variable.h" - -namespace paddle_mobile { -namespace framework { -inline proto::VarType::Type ToVarType(std::type_index type) { - if (type.hash_code() == typeid(LoDTensor).hash_code()) { - return proto::VarType_Type_LOD_TENSOR; - } else if (type.hash_code() == typeid(SelectedRows).hash_code()) { - return proto::VarType_Type_SELECTED_ROWS; - } else { - // PADDLE_THROW("ToVarType:Unsupported type %s", - // type.name()); - } -} - -} // namespace framework -} // namespace paddle_mobile diff --git a/src/framework/variable.h b/src/framework/variable.h index 07cb6377e0..769616cd12 100644 --- a/src/framework/variable.h +++ b/src/framework/variable.h @@ -20,13 +20,12 @@ limitations under the License. */ #include #include #include "../common/variant.h" -#include "paddle_mobile_object.h" namespace paddle_mobile { namespace framework { using std::string; -class Variable : public PaddleMobileObject { +class Variable { public: template const T *Get() const { diff --git a/src/io.cpp b/src/io.cpp index e208bc71c7..082339c67e 100644 --- a/src/io.cpp +++ b/src/io.cpp @@ -13,33 +13,34 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "io.h" -#include #include #include "common/log.h" #include "common/enforce.h" -#include "framework/framework.pb-c.h" -#include "framework/lod_tensor.h" -#include "framework/operator.h" -#include "framework/program/program-optimize/program_optimize.h" -#include "framework/program/program_desc.h" -#include "framework/program/var_desc.h" #include "framework/scope.h" #include "framework/tensor.h" +#include "framework/operator.h" +#include "framework/lod_tensor.h" +#include "framework/framework.pb-c.h" +#include "framework/program/var_desc.h" +#include "framework/program/program_desc.h" +#include "framework/program/program-optimize/program_optimize.h" namespace paddle_mobile { using framework::Variable; -void ReadBinaryFile(const std::string &filename, std::string *contents) { - std::ifstream fin(filename, std::ios::in | std::ios::binary); - PADDLE_MOBILE_ENFORCE(fin.is_open(), "open file: %s failed", - filename.c_str()); - fin.seekg(0, std::ios::end); - contents->clear(); - contents->resize(fin.tellg()); - fin.seekg(0, std::ios::beg); - fin.read(&(contents->at(0)), contents->size()); - fin.close(); +char *Get_binary_data(std::string filename) { + FILE *file = fopen(filename.c_str(), "rb"); + PADDLE_MOBILE_ENFORCE(file != nullptr, "can't open file: %s ", filename.c_str()); + fseek(file, 0, SEEK_END); + long size = ftell(file); + PADDLE_MOBILE_ENFORCE(size > 0, "size is too small"); + rewind(file); + char *data = new char[size]; + size_t bytes_read = fread(data, 1, size, file); + PADDLE_MOBILE_ENFORCE(bytes_read == size, "read binary file bytes do not match with fseek"); + fclose(file); + return data; } static size_t ReadBuffer(const char *file_name, uint8_t **out) { @@ -70,64 +71,49 @@ void Loader::LoadVar(framework::Variable *variable, const framework::VarDesc &var_desc, const std::string &file_path) { auto tensor = variable->GetMutable(); - std::ifstream is(file_path); - PADDLE_MOBILE_ENFORCE(is.is_open(), "open file: %s failed", - file_path.c_str()); - - std::fpos pos; - pos = is.tellg(); // save current position - is.seekg(0, std::ios::end); - is.seekg(pos); // restore saved position + char *data = Get_binary_data(file_path); // 1. version - uint32_t version; - is.read(reinterpret_cast(&version), sizeof(version)); + uint32_t version = *(uint32_t *)data; + data += sizeof(uint32_t); // 2 Lod information - uint64_t lod_level; - is.read(reinterpret_cast(&lod_level), sizeof(lod_level)); + uint32_t lod_level = *(uint64_t *)data; + data += sizeof(uint64_t); + auto &lod = *tensor->mutable_lod(); lod.resize(lod_level); for (uint64_t i = 0; i < lod_level; ++i) { - uint64_t size; - is.read(reinterpret_cast(&size), sizeof(size)); + + uint32_t size = *(uint64_t *)data; + data += sizeof(uint64_t); + std::vector tmp(size / sizeof(size_t)); - is.read(reinterpret_cast(tmp.data()), - static_cast(size)); - for (auto j : tmp) { - LOG(kLOG_DEBUG1) << " lod - " << j; + + for (int k = 0; k < tmp.size(); ++k) { + tmp[k] = *(size_t *)data; } lod[i] = tmp; } // 3. tensor version - uint32_t tensor_version; - is.read(reinterpret_cast(&tensor_version), sizeof(tensor_version)); + uint32_t tensor_version = *(uint32_t *)data; + data += sizeof(uint32_t); + // 4. tensor desc - int32_t size; - is.read(reinterpret_cast(&size), sizeof(size)); + uint32_t size = *(int32_t *)data; + data += sizeof(int32_t); + std::unique_ptr buf(new char[size]); - is.read(reinterpret_cast(buf.get()), size); + + for (int m = 0; m < size; ++m) { + buf.get()[m] = data[m]; + } const framework::TensorDesc &desc = var_desc.Tensor_desc(); PaddleMobile__Framework__Proto__VarType__TensorDesc *tensor_desc = NULL; - // void *v; - // PaddleMobile__Framework__Proto__VarType__TensorDesc_Closure()(tensor_desc, - // buf.get()); - - // DLOG << "PaddleMobile__Framework__Proto__VarType__TensorDesc_Closure- " << - // tensor_desc; - - // framework::TensorDesc &tensor_desc = variable-> - // PaddleMobile__Framework__Proto__ProgramDesc *c_program; - // uint8_t *proto_buf = NULL; - // size_t read_size = ReadBuffer(file_path.c_str(), &proto_buf); - // c_program = paddle_mobile__framework__proto__program_desc__unpack(NULL, - // read_size, buf); - - // paddle_mobile__framework__proto__var_type__tensor_desc__init() int memory_size = 1; for (auto l : desc.Dims()) { @@ -162,8 +148,11 @@ void Loader::LoadVar(framework::Variable *variable, break; } - is.read(static_cast(memory), memory_size * type_size); - is.close(); + for (int n = 0; n < memory_size * type_size; ++n) { + static_cast(memory)[n] = data[n]; + } + + delete data; } template @@ -276,29 +265,37 @@ template void Executor::LoadMemory(const framework::VarDesc var_desc, framework::LoDTensor *tensor, const std::string &file_path) { - std::ifstream is(file_path); - PADDLE_MOBILE_ENFORCE(is.is_open(), "open file: %s failed", - file_path.c_str()); - std::fpos pos; - pos = is.tellg(); // save current position - is.seekg(0, std::ios::end); - is.seekg(pos); // restore saved position + char *origin_data = Get_binary_data(file_path); + char *data = origin_data; // 1. version - uint32_t version; - is.read(reinterpret_cast(&version), sizeof(version)); + uint32_t version = *(uint32_t *)data; + data += sizeof(uint32_t); + DLOG << "version: " << version; // 2 Lod information - uint64_t lod_level; - is.read(reinterpret_cast(&lod_level), sizeof(lod_level)); + uint64_t lod_level = *(uint64_t *)data; + data += sizeof(uint64_t); + DLOG << "lod_level: " << lod_level; + + auto &lod = *tensor->mutable_lod(); lod.resize(lod_level); for (uint64_t i = 0; i < lod_level; ++i) { - uint64_t size; - is.read(reinterpret_cast(&size), sizeof(size)); + + uint64_t size = *(uint64_t *)data; + data += sizeof(uint64_t); + DLOG << "lod size: " << i << size; + + std::vector tmp(size / sizeof(size_t)); - is.read(reinterpret_cast(tmp.data()), - static_cast(size)); + + for (int k = 0; k < tmp.size(); ++k) { + tmp[k] = *(size_t *)data; + DLOG << "tmp[k]: " << k << *(size_t *)data; + data += sizeof(size_t); + } + for (auto j : tmp) { LOG(kLOG_DEBUG1) << " lod - " << j; } @@ -306,17 +303,22 @@ void Executor::LoadMemory(const framework::VarDesc var_desc, } // 3. tensor version - uint32_t tensor_version; - is.read(reinterpret_cast(&tensor_version), sizeof(tensor_version)); + uint32_t tensor_version = *(uint32_t *)data; + data += sizeof(uint32_t); + DLOG << "tensor_version: " << tensor_version; // 4. tensor desc - int32_t size; - is.read(reinterpret_cast(&size), sizeof(size)); + int32_t size = *(int32_t *)data; + data += sizeof(int32_t); + DLOG << "tensor desc size: " << size; + std::unique_ptr buf(new char[size]); - is.read(reinterpret_cast(buf.get()), size); + for (int m = 0; m < size; ++m) { + buf.get()[m] = data[m]; + } + data += (sizeof(char) * size); const framework::TensorDesc &desc = var_desc.Tensor_desc(); - int memory_size = 1; for (auto l : desc.Dims()) { memory_size *= l; @@ -332,6 +334,7 @@ void Executor::LoadMemory(const framework::VarDesc var_desc, break; case framework::VARTYPE_TYPE_FP32: type_size = 4; + DLOG << " type size: " << type_size; memory = tensor->mutable_data(); break; case framework::VARTYPE_TYPE_FP64: @@ -350,8 +353,11 @@ void Executor::LoadMemory(const framework::VarDesc var_desc, break; } - is.read(static_cast(memory), memory_size * type_size); - is.close(); + for (int n = 0; n < memory_size * type_size; ++n) { + static_cast(memory)[n] = data[n]; + } + + delete origin_data; } template diff --git a/src/io.h b/src/io.h index bb36385154..13a6761b81 100644 --- a/src/io.h +++ b/src/io.h @@ -22,14 +22,13 @@ limitations under the License. */ #include "common/types.h" #include "framework/lod_tensor.h" #include "framework/operator.h" -#include "framework/paddle_mobile_object.h" #include "framework/program/program.h" #include "framework/tensor.h" namespace paddle_mobile { template -class Loader : PaddleMobileObject { +class Loader { public: const framework::Program Load(const std::string &dirname, bool optimize = false); diff --git a/src/operators/op_param.h b/src/operators/op_param.h index 0ce187c084..11f619346e 100644 --- a/src/operators/op_param.h +++ b/src/operators/op_param.h @@ -34,7 +34,7 @@ using framework::Tensor; using std::string; using std::vector; -class OpParam : PaddleMobileObject { +class OpParam { protected: template static T *InputFrom(const VariableNameMap &inputs, const Scope &scope) { -- GitLab