-
Notifications
You must be signed in to change notification settings - Fork 155
/
variables.py
64 lines (54 loc) · 2.32 KB
/
variables.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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.
import numpy as np
from onnx import helper, onnx_pb2, TensorProto
import paddle.fluid.core as core
from paddle.fluid.executor import fetch_var
def paddle_variable_to_onnx_tensor(paddle_var_name, block):
# TODO(varunarora): Need to do this only in the case of VarType.LOD_TENSOR.
paddle_var = block.var(paddle_var_name)
shape = paddle_onnx_shape(paddle_var.shape)
return helper.make_tensor_value_info(
paddle_var_name, PADDLE_TO_ONNX_DTYPE[paddle_var.dtype], shape)
def paddle_onnx_shape(paddle_shape):
""" Convert shape info from paddle to onnx
"""
onnx_shape = np.array(list(paddle_shape))
onnx_shape[onnx_shape < 0] = 0
return tuple(onnx_shape)
def paddle_onnx_weight(var, scope):
data = fetch_var(var.name, scope)
weight = helper.make_tensor(
name=var.name,
dims=var.shape,
data_type=PADDLE_TO_ONNX_DTYPE[var.dtype],
vals=data.flatten().tolist())
value_info = helper.make_tensor_value_info(
var.name, PADDLE_TO_ONNX_DTYPE[var.dtype], var.shape)
return weight, value_info
PADDLE_TO_ONNX_DTYPE = {
core.VarDesc.VarType.FP32: onnx_pb2.TensorProto.FLOAT,
core.VarDesc.VarType.FP64: onnx_pb2.TensorProto.DOUBLE,
# '': onnx_pb2.TensorProto.DOUBLE,
core.VarDesc.VarType.INT32: onnx_pb2.TensorProto.INT32,
core.VarDesc.VarType.INT16: onnx_pb2.TensorProto.INT16,
# '': onnx_pb2.TensorProto.INT8,
# '': onnx_pb2.TensorProto.UINT8,
core.VarDesc.VarType.INT16: onnx_pb2.TensorProto.UINT16,
core.VarDesc.VarType.INT64: onnx_pb2.TensorProto.INT64,
# '': onnx_pb2.TensorProto.STRING,
# '': onnx_pb2.TensorProto.COMPLEX64,
# '': onnx_pb2.TensorProto.COMPLEX128,
core.VarDesc.VarType.BOOL: onnx_pb2.TensorProto.BOOL
}