Skip to content

Commit 257aecd

Browse files
authored
Merge pull request #996 from xiebaiyuan/develop
add some python tools for paddlemobile
2 parents f100efc + 6c0c6a9 commit 257aecd

File tree

8 files changed

+1985
-0
lines changed

8 files changed

+1985
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# encoding:utf-8
2+
import math
3+
import re
4+
5+
6+
def Real2HalfFloat(data):
7+
MINNUM = -65536
8+
MAXNUM = 65535
9+
FloatVal = 0
10+
if data:
11+
if data < MINNUM:
12+
data = MINNUM
13+
if data > MAXNUM:
14+
data = MAXNUM
15+
16+
sign = 0
17+
if data < 0:
18+
sign = 1
19+
data = -data
20+
21+
exp = math.floor((math.log2(data)))
22+
expout = exp + 16
23+
24+
Mantial = round(data / pow(2, exp - 10)) - 1024
25+
26+
if expout <= 0:
27+
FloatVal = 0
28+
else:
29+
FloatVal = sign * 32768 + expout * 1024 + Mantial
30+
return FloatVal
31+
32+
33+
def ReadCfloatData(sourcefile):
34+
input = []
35+
with open(sourcfile, 'r') as f:
36+
for line in f.readlines():
37+
line = line.strip()
38+
line = re.sub('\s+', ' ', line) # 两个数字间多个空格
39+
input.append(line.split(' '))
40+
destfile = sourcefile.replace('.dat', '')
41+
destfile = destfile.replace('.txt', '')
42+
destfile += 'Out.dat'
43+
with open(destfile, 'w') as fw:
44+
for i in range(len(input)):
45+
if len(input[i]) == 2:
46+
real = Real2HalfFloat(float(input[i][0]))
47+
imag = Real2HalfFloat(float(input[i][1]))
48+
result = real * 65536 + imag
49+
if imag and not real:
50+
fw.write('0x0000' + "%X" % result + '\n')
51+
elif not imag and not real:
52+
fw.write('0x00000000' + '\n')
53+
else:
54+
fw.write('0x' + "%X" % result + '\n')
55+
elif len(input[i]) == 1:
56+
result = Real2HalfFloat(float(input[i][0]))
57+
if result:
58+
fw.write('0x' + "%X" % result + '\n')
59+
else:
60+
fw.write('0x0000' + '\n')
61+
62+
63+
if __name__ == '__main__':
64+
print('Tips: Input number 0 if you want to exit!\n')
65+
while True:
66+
sourcfile = input("input source file:\n")
67+
if sourcfile is '0':
68+
break
69+
ReadCfloatData(sourcfile)
70+
print('Transfer Success!')
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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+
syntax = "proto2";
16+
option optimize_for = LITE_RUNTIME;
17+
package paddle_mobile.framework.proto;
18+
19+
enum AttrType {
20+
INT = 0;
21+
FLOAT = 1;
22+
STRING = 2;
23+
INTS = 3;
24+
FLOATS = 4;
25+
STRINGS = 5;
26+
BOOLEAN = 6;
27+
BOOLEANS = 7;
28+
BLOCK = 8;
29+
LONG = 9;
30+
}
31+
32+
// OpDesc describes an instance of a C++ framework::OperatorBase
33+
// derived class type.
34+
message OpDesc {
35+
36+
message Attr {
37+
required string name = 1;
38+
required AttrType type = 2;
39+
optional int32 i = 3;
40+
optional float f = 4;
41+
optional string s = 5;
42+
repeated int32 ints = 6;
43+
repeated float floats = 7;
44+
repeated string strings = 8;
45+
optional bool b = 10;
46+
repeated bool bools = 11;
47+
optional int32 block_idx = 12;
48+
optional int64 l = 13;
49+
};
50+
51+
message Var {
52+
required string parameter = 1;
53+
repeated string arguments = 2;
54+
};
55+
56+
required string type = 3;
57+
repeated Var inputs = 1;
58+
repeated Var outputs = 2;
59+
repeated Attr attrs = 4;
60+
optional bool is_target = 5 [ default = false ];
61+
};
62+
63+
// OpProto describes a C++ framework::OperatorBase derived class.
64+
message OpProto {
65+
66+
// VarProto describes the C++ type framework::Variable.
67+
message Var {
68+
required string name = 1;
69+
required string comment = 2;
70+
71+
optional bool duplicable = 3 [ default = false ];
72+
optional bool intermediate = 4 [ default = false ];
73+
optional bool dispensable = 5 [ default = false ];
74+
}
75+
76+
// AttrProto describes the C++ type Attribute.
77+
message Attr {
78+
required string name = 1;
79+
required AttrType type = 2;
80+
required string comment = 3;
81+
// If that attribute is generated, it means the Paddle third
82+
// language binding has responsibility to fill that
83+
// attribute. End-User should not set that attribute.
84+
optional bool generated = 4 [ default = false ];
85+
}
86+
87+
required string type = 1;
88+
repeated Var inputs = 2;
89+
repeated Var outputs = 3;
90+
repeated Attr attrs = 4;
91+
required string comment = 5;
92+
}
93+
94+
message VarType {
95+
enum Type {
96+
// Pod Types
97+
BOOL = 0;
98+
INT16 = 1;
99+
INT32 = 2;
100+
INT64 = 3;
101+
FP16 = 4;
102+
FP32 = 5;
103+
FP64 = 6;
104+
105+
// Other types that may need additional descriptions
106+
LOD_TENSOR = 7;
107+
SELECTED_ROWS = 8;
108+
FEED_MINIBATCH = 9;
109+
FETCH_LIST = 10;
110+
STEP_SCOPES = 11;
111+
LOD_RANK_TABLE = 12;
112+
LOD_TENSOR_ARRAY = 13;
113+
PLACE_LIST = 14;
114+
READER = 15;
115+
CHANNEL = 16;
116+
// Any runtime decided variable type is raw
117+
// raw variables should manage their own allocations
118+
// in operators like nccl_op
119+
RAW = 17;
120+
TUPLE = 18;
121+
}
122+
123+
required Type type = 1;
124+
125+
message TensorDesc {
126+
// Should only be PODType. Is enforced in C++
127+
required Type data_type = 1;
128+
repeated int64 dims = 2; // [UNK, 640, 480] is saved as [-1, 640, 480]
129+
}
130+
optional TensorDesc selected_rows = 2;
131+
132+
message LoDTensorDesc {
133+
required TensorDesc tensor = 1;
134+
optional int32 lod_level = 2 [ default = 0 ];
135+
}
136+
optional LoDTensorDesc lod_tensor = 3;
137+
138+
message LoDTensorArrayDesc {
139+
required TensorDesc tensor = 1;
140+
optional int32 lod_level = 2 [ default = 0 ];
141+
}
142+
optional LoDTensorArrayDesc tensor_array = 4;
143+
144+
message ReaderDesc { repeated LoDTensorDesc lod_tensor = 1; }
145+
optional ReaderDesc reader = 5;
146+
147+
message ChannelDesc {
148+
required Type data_type = 1;
149+
required int64 capacity = 2;
150+
}
151+
optional ChannelDesc channel = 6;
152+
153+
message Tuple { repeated Type element_type = 1; }
154+
optional Tuple tuple = 7;
155+
}
156+
157+
message VarDesc {
158+
required string name = 1;
159+
required VarType type = 2;
160+
optional bool persistable = 3 [ default = false ];
161+
}
162+
163+
message BlockDesc {
164+
required int32 idx = 1;
165+
required int32 parent_idx = 2;
166+
repeated VarDesc vars = 3;
167+
repeated OpDesc ops = 4;
168+
optional int32 forward_block_idx = 5 [ default = -1 ];
169+
}
170+
171+
// Please refer to
172+
// https://github.com/PaddlePaddle/Paddle/blob/develop/doc/design/program.md
173+
// for more details.
174+
// TODO(panyx0718): A model can have multiple programs. Need a
175+
// way to distinguish them. Maybe ID or name?
176+
message ProgramDesc { repeated BlockDesc blocks = 1; }

0 commit comments

Comments
 (0)