diff --git a/CMakeLists.txt b/CMakeLists.txt index bd2299f2a7aff101c5760492070bf5bdea4eca7a..5635dff9ef0d98f43510d1fe8b219c18e56cd2a1 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 dbc4554c5f2ef6ea2dc9ec76714277e8e24b0c8a..0000000000000000000000000000000000000000 --- 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 fd0e3d80a21282fe7bb600c2fdb174411fa315a3..d90daeecc90e931d895dbb90fddde21d4a3a62ca 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 a04559d79a02cf11d2b8f5f168c34377c313bc9a..db86d88b86f3e9c1e2a663d6a2f5577b398ea4ce 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 6bbf34eae933d69d00517c723326111901444ab0..0000000000000000000000000000000000000000 --- 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 b3947985d8b09e183c690b4d51093c2ae96e7d80..e380eacf0eb43970f5ee997d82de011ee075cee3 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 5fd88a6b209a33dd5554ff11ddf54514f53387ec..17a8359d8caf8b70c4aa2b3d50c0f8cdbf78549c 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 acf37a3c117af3bf2bc70aac624335399b4a796b..0000000000000000000000000000000000000000 --- 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 aff4b6c1f178ba70c756c49721ac9b34de82c71c..0000000000000000000000000000000000000000 --- 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 84d7a90fc11ddf360eacb01be9456ced4a30dad8..e788ab7269e2e57672e28dc139504ef95a82bdf2 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 07b903085d5d9044b93e3e9309390c9a3976580d..4fdfac253f0525b288983e8bcf9c1b4eff8f393d 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 010585166cc0828612a48c128f8753338ef16ff9..0000000000000000000000000000000000000000 --- 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 4cc83f8c80ab86ee6dcc1e3c395f872419da2be7..ff50dcafbc5524e350c1d2774933e257f80582ca 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 84a7477dc4c952d64df3b7364bf3072e31dec49b..24d9ce2fe538fcb25110602afcdab264bf5941b5 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 9f657d5369fd9d52d63b709c3ad438ff59e46f32..15724523ded18e14cecf5d5aacf506992dadb3b4 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 3a9cbfc1d9e3f3d099bcfeac32260613fc4dccc3..2cea562b70eb1d6e8a0b893cfdf82c926ca3eb14 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 6aa7dd44ce5880d8b1db0e2b3ffad2e0bd31d46e..5c87f565e13df1564343b43150a5696c3adaca39 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 5ab2fc56178b8d48d2dfb637817eca13b53677d5..f6f04f2c7026166e1024dcc1a4b2a233deac649b 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 f59bd1aabfb7a0571b484fa21375acb4cb8254d3..0000000000000000000000000000000000000000 --- 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 5e132c73759bfa3a863023baf52df6ef41365047..0000000000000000000000000000000000000000 --- 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 07cb6377e0c9ca89f828eded887b8d1da2d8aae6..769616cd1238f49f1aa676d837b0b4850eb1a246 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 e208bc71c705adf72c5135f9c8f0b962feaf73c1..082339c67edba119dffced082cc065d3524c1ddc 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 bb363851547a02fb98e5b8ccddebda2a464b8376..13a6761b81c2679afc02d220c11056584f84f5dd 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 0ce187c084975c53e433b9428ad14bf11212a5a1..11f619346eed9f3b4309ec983de6f0b09f1142b7 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) {