forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy_checkpoint_reader.i
More file actions
130 lines (115 loc) · 4.28 KB
/
py_checkpoint_reader.i
File metadata and controls
130 lines (115 loc) · 4.28 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* Copyright 2015 The TensorFlow 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 "tensorflow/python/lib/core/strings.i"
%include "tensorflow/python/platform/base.i"
%{
#include "tensorflow/c/checkpoint_reader.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/python/lib/core/py_func.h"
%}
%typemap(out) const tensorflow::checkpoint::TensorSliceReader::VarToShapeMap& {
tensorflow::Safe_PyObjectPtr output_map(tensorflow::make_safe(PyDict_New()));
for (auto v : *$1) {
%#if PY_MAJOR_VERSION >= 3
tensorflow::Safe_PyObjectPtr key(
tensorflow::make_safe(PyUnicode_FromStringAndSize(v.first.c_str(),
v.first.size())));
%#else
tensorflow::Safe_PyObjectPtr key(
tensorflow::make_safe(PyString_FromStringAndSize(v.first.c_str(),
v.first.size())));
%#endif
if (!key) {
SWIG_fail;
}
size_t dims = v.second.dims();
tensorflow::Safe_PyObjectPtr value(tensorflow::make_safe(PyList_New(dims)));
if (!value) {
SWIG_fail;
}
for (size_t i = 0; i < dims; ++i) {
%#if PY_MAJOR_VERSION >= 3
tensorflow::Safe_PyObjectPtr dim_value(
tensorflow::make_safe(PyLong_FromLong(v.second.dim_size(i))));
%#else
tensorflow::Safe_PyObjectPtr dim_value(
tensorflow::make_safe(PyInt_FromLong(v.second.dim_size(i))));
%#endif
if (!dim_value) {
SWIG_fail;
}
PyList_SET_ITEM(value.get(), i, dim_value.release());
}
if (PyDict_SetItem(output_map.get(), key.get(), value.get()) == -1) {
SWIG_fail;
} else {
key.release();
value.release();
}
}
$result = output_map.release();
}
%{
static PyObject* CheckpointReader_GetTensor(
tensorflow::checkpoint::CheckpointReader* reader,
const string& name,
TF_Status* out_status) {
PyObject* py_obj = Py_None;
std::unique_ptr<tensorflow::Tensor> tensor;
reader->GetTensor(name, &tensor, out_status);
if (TF_GetCode(out_status) == TF_OK) {
tensorflow::Status status =
tensorflow::ConvertTensorToNdarray(*tensor.get(), &py_obj);
if (!status.ok()) {
Set_TF_Status_from_Status(out_status, status);
}
}
return py_obj;
}
%}
// Wrap this function.
PyObject* CheckpointReader_GetTensor(
tensorflow::checkpoint::CheckpointReader* reader,
const string& name,
TF_Status* out_status);
%ignoreall
%unignore tensorflow;
%unignore tensorflow::checkpoint;
%unignore tensorflow::checkpoint::CheckpointReader;
%unignore tensorflow::checkpoint::CheckpointReader::CheckpointReader;
%unignore tensorflow::checkpoint::CheckpointReader::~CheckpointReader;
%rename("debug_string") tensorflow::checkpoint::CheckpointReader::DebugString;
%rename("get_variable_to_shape_map") tensorflow::checkpoint::CheckpointReader::GetVariableToShapeMap;
%rename("_HasTensor") tensorflow::checkpoint::CheckpointReader::HasTensor;
%unignore CheckpointReader_GetTensor;
%extend tensorflow::checkpoint::CheckpointReader {
%insert("python") %{
def has_tensor(self, tensor_str):
from tensorflow.python.util import compat
return self._HasTensor(compat.as_bytes(tensor_str))
def get_tensor(self, tensor_str):
from tensorflow.python.framework import errors
with errors.raise_exception_on_not_ok_status() as status:
from tensorflow.python.util import compat
return CheckpointReader_GetTensor(self, compat.as_bytes(tensor_str),
status)
%}
}
%insert("python") %{
def NewCheckpointReader(filepattern):
from tensorflow.python.framework import errors
with errors.raise_exception_on_not_ok_status() as status:
from tensorflow.python.util import compat
return CheckpointReader(compat.as_bytes(filepattern), status)
%}
%include "tensorflow/c/checkpoint_reader.h"
%unignoreall