未验证 提交 d409d632 编写于 作者: Y Yanzhan Yang 提交者: GitHub

add model obfuscate feature (#1718)

* add model obfuscate feature

* fix style

* fix array length bug

* add merge.sh to merge static library into one combined library

* disable OpenMP for iOS
上级 a1224621
cmake_minimum_required(VERSION 3.0.0)
# basic build option
option(USE_OPENMP "build with openmp support" ON)
if(IS_IOS)
option(USE_OPENMP "build with openmp support" OFF)
else()
option(USE_OPENMP "build with openmp support" ON)
endif()
option(USE_EXCEPTION "build with exception" ON)
option(WITH_LOGGING "print logging for debug" OFF)
option(WITH_SYMBOL "build with all symbols" ON) # turn off if use jni or ios io
......
......@@ -141,6 +141,7 @@ enum MemoryOptimizationLevel {
struct PaddleMobileConfigInternal {
bool load_when_predict = false;
MemoryOptimizationLevel memory_optimization_level = FullMemoryOptimization;
std::string model_obfuscate_key = "";
};
extern const char *G_OP_TYPE_CONV;
......
......@@ -32,4 +32,15 @@ char *ReadFileToBuff(std::string filename) {
return data;
}
int GetFileLength(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);
int size = ftell(file);
PADDLE_MOBILE_ENFORCE(size > 0, "file should not be empty");
fclose(file);
return size;
}
} // namespace paddle_mobile
......@@ -21,4 +21,6 @@ namespace paddle_mobile {
char *ReadFileToBuff(std::string filename);
int GetFileLength(std::string filename);
} // namespace paddle_mobile
......@@ -30,6 +30,7 @@ limitations under the License. */
#include "framework/tensor.h"
#include "memory/t_malloc.h"
#include "pass/memory_optimize.h"
#include "pass/model_obfuscate.h"
#ifdef PADDLE_MOBILE_CL
#include "framework/cl/cl_image.h"
#endif
......@@ -241,9 +242,17 @@ void Executor<Device, T>::InitCombineMemory() {
if (program_.combined_params_buf && program_.combined_params_len) {
origin_data = reinterpret_cast<char *>(
const_cast<uint8_t *>(program_.combined_params_buf));
if (config_.model_obfuscate_key != "") {
auto obfuscator = pass::ModelObfuscatePass(config_.model_obfuscate_key);
obfuscator.convert_data(origin_data, program_.combined_params_len);
}
} else {
self_alloc = true;
origin_data = ReadFileToBuff(program_.para_path);
if (config_.model_obfuscate_key != "") {
auto obfuscator = pass::ModelObfuscatePass(config_.model_obfuscate_key);
obfuscator.convert_data(origin_data, GetFileLength(program_.para_path));
}
}
PADDLE_MOBILE_ENFORCE(origin_data != nullptr, "data == nullptr");
char *data = origin_data;
......@@ -930,10 +939,18 @@ void Executor<GPU_CL, float>::InitCombineMemory() {
if (program_.combined_params_buf && program_.combined_params_len) {
LOG(kLOG_INFO) << "use outter memory";
origin_data = reinterpret_cast<char *>(program_.combined_params_buf);
if (config_.model_obfuscate_key != "") {
auto obfuscator = pass::ModelObfuscatePass(config_.model_obfuscate_key);
obfuscator.convert_data(origin_data, program_.combined_params_len);
}
} else {
LOG(kLOG_INFO) << " begin init combine memory";
self_alloc = true;
origin_data = ReadFileToBuff(program_.para_path);
if (config_.model_obfuscate_key != "") {
auto obfuscator = pass::ModelObfuscatePass(config_.model_obfuscate_key);
obfuscator.convert_data(origin_data, GetFileLength(program_.para_path));
}
}
PADDLE_MOBILE_ENFORCE(origin_data != nullptr, "origin_data==nullptr!!!");
float *data = reinterpret_cast<float *>(origin_data);
......
......@@ -13,8 +13,8 @@ See the License for the specific language governing permissions and
limitations under the License. */
#include "pass/memory_optimize.h"
#include "framework/lod_tensor.h"
#include <algorithm>
#include "framework/lod_tensor.h"
namespace paddle_mobile {
namespace pass {
......
......@@ -19,6 +19,7 @@ limitations under the License. */
#include <unordered_map>
#include <vector>
#include "framework/program/program.h"
#include "pass/pass_base.h"
namespace paddle_mobile {
namespace pass {
......@@ -29,12 +30,6 @@ typedef struct {
bool visited;
} VarNode;
class PassBase {
public:
PassBase() {}
virtual ~PassBase() {}
};
// MemoryOptPass will analyze the program, and reuse memory between
// variables as much as possible
class MemoryOptPass : public PassBase {
......
/* 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 "pass/model_obfuscate.h"
namespace paddle_mobile {
namespace pass {
ModelObfuscatePass::ModelObfuscatePass(std::string key) {
for (auto c : key) {
acc *= base;
acc += (int)c;
acc %= stride;
}
acc += stride;
}
void ModelObfuscatePass::convert_data(char *data, int len) {
for (int i = 0; i < len; i += acc) {
data[i] = 255 - data[i];
}
}
} // namespace pass
} // namespace paddle_mobile
/* 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 <string>
#include "pass/pass_base.h"
namespace paddle_mobile {
namespace pass {
class ModelObfuscatePass : public PassBase {
public:
ModelObfuscatePass(std::string key);
void convert_data(char *data, int len);
int version = 1;
private:
int acc = 0;
int base = 17;
int stride = 100;
};
} // namespace pass
} // namespace paddle_mobile
/* 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
namespace paddle_mobile {
namespace pass {
class PassBase {
public:
PassBase() {}
virtual ~PassBase() {}
};
} // namespace pass
} // namespace paddle_mobile
......@@ -14,6 +14,7 @@ limitations under the License. */
#include <iostream>
#include <string>
#include <fstream>
#include "../test_helper.h"
#include "../test_include.h"
......@@ -34,6 +35,15 @@ void test(int argc, char *argv[]) {
config.memory_optimization_level = enable_memory_optimization
? MemoryOptimizationWithoutFeeds
: NoMemoryOptimization;
// save obfuscated model
// config.model_obfuscate_key = "asdf";
// std::ofstream out_file("new-params", std::ofstream::binary);
// char *out_data = ReadFileToBuff("./checked_model/params");
// int len = GetFileLength("./checked_model/params");
// out_file.write(out_data, len);
// out_file.close();
paddle_mobile::PaddleMobile<paddle_mobile::CPU> paddle_mobile(config);
paddle_mobile.SetThreadNum(1);
......
......@@ -20,6 +20,7 @@ limitations under the License. */
#include "./test_helper.h"
#include "common/enforce.h"
#include "common/util.h"
#include "common/log.h"
#include "executor_for_test.h"
#include "framework/ddim.h"
......
#!/bin/sh
# Combined all static libaries in the current directory into a single static library
# It is hardcoded to use the i386, armv7, and armv7s architectures; this can easily be changed via the 'archs' variable at the top
# The script takes a single argument, which is the name of the final, combined library to be created.
#
# For example:
# => combine_static_libraries.sh combined-library
#
# Script by Evan Schoenberg, Regular Rate and Rhythm Software
# Thanks to Claudiu Ursache for his blog post at http://www.cvursache.com/2013/10/06/Combining-Multi-Arch-Binaries/ which detailed the technique automated by this script
#####
# $1 = Name of output archive
#####
# archs=(i386 armv7 armv7s)
archs=(armv7 arm64)
libraries=(*.a)
libtool="/usr/bin/libtool"
echo "Combining ${libraries[*]}..."
for library in ${libraries[*]}
do
lipo -info $library
# Extract individual architectures for this library
for arch in ${archs[*]}
do
lipo -extract $arch $library -o ${library}_${arch}.a
done
done
# Combine results of the same architecture into a library for that architecture
source_combined=""
for arch in ${archs[*]}
do
source_libraries=""
for library in ${libraries[*]}
do
source_libraries="${source_libraries} ${library}_${arch}.a"
done
$libtool -static ${source_libraries} -o "${1}_${arch}.a"
source_combined="${source_combined} ${1}_${arch}.a"
# Delete intermediate files
rm ${source_libraries}
done
# Merge the combined library for each architecture into a single fat binary
lipo -create $source_combined -o $1.a
# Delete intermediate files
rm ${source_combined}
# Show info on the output library as confirmation
echo "Combination complete."
lipo -info $1.a
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册