Skip to content

Commit 926e107

Browse files
committed
version
1 parent 14242ea commit 926e107

File tree

11 files changed

+102
-3
lines changed

11 files changed

+102
-3
lines changed

paddle/fluid/framework/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ cc_library(operator SRCS operator.cc DEPS op_info device_context tensor scope gl
116116
endif(NOT WIN32)
117117

118118
cc_test(operator_test SRCS operator_test.cc DEPS operator op_registry device_context)
119-
cc_library(proto_desc SRCS var_desc.cc op_desc.cc block_desc.cc program_desc.cc DEPS shape_inference op_info operator glog)
119+
120+
cc_library(version SRCS version.cc)
121+
cc_library(proto_desc SRCS var_desc.cc op_desc.cc block_desc.cc program_desc.cc DEPS shape_inference op_info operator glog version)
120122

121123
cc_library(op_registry SRCS op_registry.cc DEPS op_proto_maker op_info operator glog proto_desc)
122124
nv_test(op_registry_test SRCS op_registry_test.cc DEPS op_registry)

paddle/fluid/framework/framework.proto

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ syntax = "proto2";
1616
option optimize_for = LITE_RUNTIME;
1717
package paddle.framework.proto;
1818

19+
// Any incompatible changes to ProgramDesc and its dependencies should
20+
// raise the version defined version.h.
21+
//
22+
// Serailization and Deserialization codes should be modified in a way
23+
// that supports old versions following the version and compatibility policy.
24+
message Version { optional int64 version = 1 [ default = -1 ]; }
25+
1926
enum AttrType {
2027
INT = 0;
2128
FLOAT = 1;
@@ -180,4 +187,8 @@ message BlockDesc {
180187
// for more details.
181188
// TODO(panyx0718): A model can have multiple programs. Need a
182189
// way to distinguish them. Maybe ID or name?
183-
message ProgramDesc { repeated BlockDesc blocks = 1; }
190+
message ProgramDesc {
191+
repeated BlockDesc blocks = 1;
192+
193+
optional Version version = 2;
194+
}

paddle/fluid/framework/program_desc.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License. */
1515
#include "paddle/fluid/framework/program_desc.h"
1616
#include "paddle/fluid/framework/block_desc.h"
1717
#include "paddle/fluid/framework/feed_fetch_type.h"
18+
#include "paddle/fluid/framework/version.h"
1819

1920
namespace paddle {
2021
namespace framework {
@@ -31,14 +32,21 @@ void ProgramDesc::Flush() {
3132
for (auto &block : blocks_) {
3233
block->Flush();
3334
}
35+
// If not loaded, use current code version.
36+
if (desc_.version().version() < 0) {
37+
desc_.mutable_version()->set_version(kCurProgramVersion);
38+
}
3439
}
3540

3641
proto::ProgramDesc *ProgramDesc::Proto() {
3742
Flush();
3843
return &desc_;
3944
}
4045

46+
int ProgramDesc::Version() const { return desc_.version().version(); }
47+
4148
ProgramDesc::ProgramDesc() {
49+
desc_.mutable_version()->set_version(kCurProgramVersion);
4250
auto *block = desc_.mutable_blocks()->Add();
4351
block->set_idx(kRootBlockIndex);
4452
block->set_parent_idx(kNoneBlockIndex);

paddle/fluid/framework/program_desc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class ProgramDesc {
5757

5858
proto::ProgramDesc *Proto();
5959

60+
int Version() const;
61+
6062
// The output variable of feed_op is referenced as feed_target.
6163
// This function is used to collect the output variable's name of all
6264
// feed_ops.

paddle/fluid/framework/version.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include "paddle/fluid/framework/version.h"
16+
#include <algorithm>
17+
18+
namespace paddle {
19+
namespace framework {
20+
bool IsProgramVersionSupported(int version) {
21+
static int num_supported =
22+
sizeof(kSupportedProgramVersion) / sizeof(kSupportedProgramVersion[0]);
23+
return std::find(kSupportedProgramVersion,
24+
kSupportedProgramVersion + num_supported,
25+
version) != kSupportedProgramVersion + num_supported;
26+
}
27+
} // namespace framework
28+
} // namespace paddle

paddle/fluid/framework/version.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#pragma once
16+
17+
namespace paddle {
18+
namespace framework {
19+
20+
// The program version the current codes generate.
21+
constexpr int kCurProgramVersion = 0;
22+
23+
// The program version that was generated by previous or current codes
24+
// and supported by current codes.
25+
constexpr int kSupportedProgramVersion[] = {0};
26+
27+
bool IsProgramVersionSupported(int version);
28+
29+
} // namespace framework
30+
} // namespace paddle

paddle/fluid/inference/io.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ limitations under the License. */
2020
#include "paddle/fluid/framework/block_desc.h"
2121
#include "paddle/fluid/framework/feed_fetch_type.h"
2222
#include "paddle/fluid/framework/op_registry.h"
23+
#include "paddle/fluid/framework/version.h"
2324
#include "paddle/fluid/platform/cpu_helper.h"
2425
#include "paddle/fluid/pybind/pybind.h"
2526

@@ -124,6 +125,7 @@ std::unique_ptr<framework::ProgramDesc> Load(framework::Executor* executor,
124125

125126
std::unique_ptr<framework::ProgramDesc> main_program(
126127
new framework::ProgramDesc(program_desc_str));
128+
PADDLE_ENFORCE(framework::IsProgramVersionSupported(main_program->Version()));
127129

128130
LoadPersistables(executor, scope, *main_program, dirname, "");
129131
return main_program;
@@ -138,6 +140,7 @@ std::unique_ptr<framework::ProgramDesc> Load(
138140

139141
std::unique_ptr<framework::ProgramDesc> main_program(
140142
new framework::ProgramDesc(program_desc_str));
143+
PADDLE_ENFORCE(framework::IsProgramVersionSupported(main_program->Version()));
141144

142145
LoadPersistables(executor, scope, *main_program, "", param_filename);
143146
return main_program;

paddle/fluid/pybind/protobuf.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,10 @@ void BindProgramDesc(pybind11::module *m) {
137137
PADDLE_ENFORCE(desc->ParseFromString(data),
138138
"Fail to parse ProgramDesc from string. This could "
139139
"be a bug of Paddle.");
140-
});
140+
})
141+
.def("_version", [](pd::ProgramDesc &self) -> int64_t {
142+
return self.Proto()->version().version();
143+
});
141144
}
142145

143146
void BindBlockDesc(pybind11::module *m) {

paddle/fluid/pybind/pybind.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,10 @@ All parameter, weight, gradient are variables in Paddle.
517517
m.def("init_glog", framework::InitGLOG);
518518
m.def("init_devices",
519519
[](bool init_p2p) { framework::InitDevices(init_p2p); });
520+
m.def("_supported_version", []() {
521+
std::vector<int> supported_versions;
522+
return supported_versions;
523+
});
520524

521525
m.def("is_compiled_with_cuda", IsCompiledWithCUDA);
522526
m.def("is_compiled_with_dist", IsCompiledWithDIST);

python/paddle/fluid/framework.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,9 @@ def get_desc(self):
15641564
"""
15651565
return self.desc
15661566

1567+
def _version(self):
1568+
return self.desc._version()
1569+
15671570
def clone(self, for_test=False):
15681571
"""
15691572
Create a new, duplicated program.

0 commit comments

Comments
 (0)