aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/client/tf_session.i
blob: 40731aba7d4ed8bb281191d719b3ddfcd2db1ddc (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/* 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/platform/base.i"

%{

#include "tensorflow/c/python_api.h"
#include "tensorflow/python/client/tf_session_helper.h"
#include "tensorflow/core/framework/session_state.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/strings/stringprintf.h"
#include "tensorflow/core/public/version.h"

%}

%include "tensorflow/python/client/tf_sessionrun_wrapper.i"

// Required to use PyArray_* functions.
%init %{
tensorflow::ImportNumpy();
%}

// TensorFlow version and GraphDef versions
%constant const char* __version__ = TF_VERSION_STRING;
%constant int GRAPH_DEF_VERSION = TF_GRAPH_DEF_VERSION;
%constant int GRAPH_DEF_VERSION_MIN_CONSUMER = TF_GRAPH_DEF_VERSION_MIN_CONSUMER;
%constant int GRAPH_DEF_VERSION_MIN_PRODUCER = TF_GRAPH_DEF_VERSION_MIN_PRODUCER;

// Git version information
%constant const char* __git_version__ = tf_git_version();

// Compiler
%constant const char* __compiler_version__ = tf_compiler_version();

// _GLIBCXX_USE_CXX11_ABI flag value
%constant const int __cxx11_abi_flag__ = tf_cxx11_abi_flag();

// Release the Python GIL for the duration of most methods.
%exception {
  Py_BEGIN_ALLOW_THREADS;
  $action
  Py_END_ALLOW_THREADS;
}

// The target input to TF_SetTarget() is passed as a null-terminated
// const char*.
%typemap(in) (const char* target) {
  $1 = PyBytes_AsString($input);
   if (!$1) {
    // Python has raised an error.
    SWIG_fail;
  }
}

// Constants used by TensorHandle (get_session_handle).
%constant const char* TENSOR_HANDLE_KEY = tensorflow::SessionState::kTensorHandleResourceTypeName;

// Convert TF_OperationName output to unicode python string
%typemap(out) const char* TF_OperationName {
  $result = PyUnicode_FromString($1);
}

// Convert TF_OperationOpType output to unicode python string
%typemap(out) const char* TF_OperationOpType {
  $result = PyUnicode_FromString($1);
}

// Convert TF_DeviceListMemoryBytes and TF_Dim int64_t output to Python integers
%typemap(out) int64_t {
  $result = PyInt_FromLong($1);
}

// We use TF_OperationGetControlInputs_wrapper instead of
// TF_OperationGetControlInputs
%ignore TF_OperationGetControlInputs;
%unignore TF_OperationGetControlInputs_wrapper;
// See comment for "%noexception TF_SessionRun_wrapper;"
%noexception TF_OperationGetControlInputs_wrapper;

// Build a Python list of TF_Operation* and return it.
%typemap(out) std::vector<TF_Operation*> tensorflow::TF_OperationGetControlInputs_wrapper {
  $result = PyList_New($1.size());
  if (!$result) {
    SWIG_exception_fail(SWIG_MemoryError, "$symname: couldn't create list");
  }

  for (size_t i = 0; i < $1.size(); ++i) {
    PyList_SET_ITEM($result, i, SWIG_NewPointerObj(
                            $1[i], SWIGTYPE_p_TF_Operation, 0));
  }
}

%unignore GetOperationInputs;
// See comment for "%noexception TF_SessionRun_wrapper;"
%noexception GetOperationInputs;

// Build a Python list of TF_Outputs and return it.
// TODO(skyewm): is there some way to generalize this pattern? Maybe a macro?
%typemap(out) std::vector<TF_Output> tensorflow::GetOperationInputs {
  $result = PyList_New($1.size());
  if (!$result) {
    SWIG_exception_fail(SWIG_MemoryError, "$symname: couldn't create list");
  }

  // Unwrap the generated SwigValueWrapper<std::vector<TF_Output>> via &
  std::vector<TF_Output>* tf_outputs = &$1;
  for (size_t i = 0; i < $1.size(); ++i) {
    // We used wrapped heap-allocated pointers in the Python runtime (this is
    // what SWIG generates by default for functions returning TF_Output).
    TF_Output* tf_output_ptr = new TF_Output((*tf_outputs)[i]);
    // Use SWIG_POINTER_OWN so the TF_Output* is deleted by Python.
    PyList_SET_ITEM($result, i,
                    SWIG_NewPointerObj(tf_output_ptr, SWIGTYPE_p_TF_Output,
                                       SWIG_POINTER_OWN));
  }
}


////////////////////////////////////////////////////////////////////////////////
// BEGIN TYPEMAPS FOR tensorflow::TF_Run_wrapper()
////////////////////////////////////////////////////////////////////////////////

// Converts a python list of strings to NameVector.
// Has multiple users including feeds/fetches names and function output names
%typemap(in) const tensorflow::NameVector& (
    tensorflow::NameVector temp,
    tensorflow::Safe_PyObjectPtr temp_string_list(
        tensorflow::make_safe(static_cast<PyObject*>(nullptr)))) {
  if (!PyList_Check($input)) {
    SWIG_exception_fail(
        SWIG_TypeError,
        tensorflow::strings::Printf(
            "Expected a python list for conversion "
            "to tensorflow::NameVector but got %s",
            Py_TYPE($input)->tp_name).c_str());
  }

  Py_ssize_t len = PyList_Size($input);

  temp_string_list = tensorflow::make_safe(PyList_New(len));
  if (!temp_string_list) {
    SWIG_exception_fail(
        SWIG_MemoryError,
        tensorflow::strings::Printf("Failed to create a list of size %zd",
                                    len).c_str());
  }

  for (Py_ssize_t i = 0; i < len; ++i) {
    PyObject* elem = PyList_GetItem($input, i);
    if (!elem) {
      SWIG_fail;
    }

    // Keep a reference to the string in case the incoming list is modified.
    PyList_SET_ITEM(temp_string_list.get(), i, elem);
    Py_INCREF(elem);

    char* string_elem = PyBytes_AsString(elem);
    if (!string_elem) {
      SWIG_exception_fail(
          SWIG_TypeError,
          tensorflow::strings::Printf(
              "Element %zd was of type %s instead of a string",
              i, Py_TYPE(elem)->tp_name).c_str());
    }

    // TODO(mrry): Avoid copying the fetch name in, if this impacts performance.
    temp.push_back(string_elem);
  }
  $1 = &temp;
}

// Define temporaries for the argout outputs.
%typemap(in, numinputs=0) tensorflow::PyObjectVector* out_values (
    tensorflow::PyObjectVector temp) {
  $1 = &temp;
}
// TODO(iga): move this and the corresponding typemap(argout) to
// tf_sessionrun_wrapper.i once we get rid of this code for DeprecatedSession.
%typemap(in, numinputs=0) char** out_handle (
    char* temp) {
  $1 = &temp;
}

// Build a Python list of outputs and return it.
%typemap(argout) tensorflow::PyObjectVector* out_values {
  std::vector<tensorflow::Safe_PyObjectPtr> out_values_safe;
  for (size_t i = 0; i < $1->size(); ++i) {
    out_values_safe.emplace_back(tensorflow::make_safe($1->at(i)));
  }

  $result = PyList_New($1->size());
  if (!$result) {
    SWIG_exception_fail(
        SWIG_MemoryError,
        tensorflow::strings::Printf("Failed to create a list of size %zd",
                                    $1->size()).c_str());
  }

  for (size_t i = 0; i < $1->size(); ++i) {
    PyList_SET_ITEM($result, i, $1->at(i));
    out_values_safe[i].release();
  }
}

// Return the handle as a python string object.
%typemap(argout) char** out_handle {
%#if PY_MAJOR_VERSION < 3
  $result = PyString_FromStringAndSize(
%#else
  $result = PyUnicode_FromStringAndSize(
%#endif
    *$1, *$1 == nullptr ? 0 : strlen(*$1));
  delete[] *$1;
}

////////////////////////////////////////////////////////////////////////////////
// END TYPEMAPS FOR tensorflow::TF_Run_wrapper()
////////////////////////////////////////////////////////////////////////////////

// Typemap for TF_Status* inputs that automatically unwraps a ScopedTFStatus.
// This can also handle a wrapped TF_Status* input.
%typemap(in) (TF_Status*) {
  PyObject* wrapped_tf_status;
  if (strcmp(Py_TYPE($input)->tp_name, "ScopedTFStatus") == 0) {
    DCHECK(PyObject_HasAttrString($input, "status"))
        << "ScopedTFStatus.status not found! Do you need to modify "
           "tf_session.i?";
    wrapped_tf_status = PyObject_GetAttrString($input, "status");
  } else {
    // Assume wrapped TF_Status*
    wrapped_tf_status = $input;
  }
  DCHECK_EQ(strcmp(Py_TYPE(wrapped_tf_status)->tp_name, "SwigPyObject"), 0)
      << Py_TYPE(wrapped_tf_status)->tp_name;

  // The following is the default SWIG code generated for TF_Status*
  void* tf_status = nullptr;
  int r = SWIG_ConvertPtr(wrapped_tf_status, &tf_status,
                          $descriptor(TF_Status*), 0 | 0);
  if (!SWIG_IsOK(r)) {
    SWIG_exception_fail(
        SWIG_ArgError(r),
        "in method '_TF_DeleteStatus', argument 1 of type 'TF_Status *'");
  }
  $1 = reinterpret_cast<TF_Status*>(tf_status);
}

// Typemap for functions that return a TF_Buffer struct. This typemap creates a
// Python string from the TF_Buffer and returns it. The TF_Buffer.data string
// is not expected to be NULL-terminated, and TF_Buffer.length does not count
// the terminator.
%typemap(out) TF_Buffer (TF_GetOpList,TF_GetBuffer) {
  $result = PyBytes_FromStringAndSize(
      reinterpret_cast<const char*>($1.data), $1.length);
}

%inline %{
// Helper function to convert a Python list of Tensors to a C++ vector of
// TF_Outputs.
//
// Returns true if successful. Otherwise, returns false and sets error_msg.
bool PyTensorListToVector(PyObject* py_tensor_list,
                          std::vector<TF_Output>* vec,
                          string* error_msg) {
  if (!PyList_Check(py_tensor_list)) {
    *error_msg = "expected Python list.";
    return false;
  }
  size_t size = PyList_Size(py_tensor_list);
  for (int i = 0; i < size; ++i) {
    PyObject* item = PyList_GetItem(py_tensor_list, i);
    TF_Output* input_ptr;
    if (!SWIG_IsOK(SWIG_ConvertPtr(item, reinterpret_cast<void**>(&input_ptr),
                                   SWIGTYPE_p_TF_Output, 0))) {
      *error_msg = "expected Python list of wrapped TF_Output objects. "
          "Found python list of something else.";
      return false;
    }
    vec->push_back(*input_ptr);
  }
  return true;
}
%}

// Converts input Python list of wrapped TF_Outputs into a single array
%typemap(in) (const TF_Output* inputs, int num_inputs)
    (std::vector<TF_Output> inputs) {
  string error_msg;
  if (!PyTensorListToVector($input, &inputs, &error_msg)) {
    SWIG_exception_fail(SWIG_TypeError, ("$symname: " + error_msg).c_str());
  }
  $1 = inputs.data();
  $2 = inputs.size();
}

// TODO(skyewm): SWIG emits a warning for the const char* in TF_WhileParams,
// skip for now
%ignore TF_WhileParams;
%ignore TF_NewWhile;
%ignore TF_FinishWhile;
%ignore TF_AbortWhile;

// These are defined below, avoid duplicate definitions
%ignore TF_Run;
%ignore TF_PRun;
%ignore TF_PRunSetup;

// We use TF_SessionRun_wrapper instead of TF_SessionRun
%ignore TF_SessionRun;
%unignore TF_SessionRun_wrapper;
// The %exception block above releases the Python GIL for the length of each
// wrapped method. We disable this behavior for TF_SessionRun_wrapper because it
// uses Python method(s) that expect the GIL to be held (at least
// PyArray_Return, maybe others).
%noexception TF_SessionRun_wrapper;

// We use TF_SessionPRunSetup_wrapper instead of TF_SessionPRunSetup
%ignore TF_SessionPRunSetup;
%unignore TF_SessionPRunSetup_wrapper;
// See comment for "%noexception TF_SessionRun_wrapper;"
%noexception TF_SessionPRunSetup_wrapper;

// We use TF_SessionPRun_wrapper instead of TF_SessionPRun
%ignore TF_SessionPRun;
%unignore TF_SessionPRun_wrapper;
// See comment for "%noexception TF_SessionRun_wrapper;"
%noexception TF_SessionPRun_wrapper;

%rename("_TF_SetTarget") TF_SetTarget;
%rename("_TF_SetConfig") TF_SetConfig;
%rename("_TF_NewSessionOptions") TF_NewSessionOptions;

%include "tensorflow/c/c_api.h"
%include "tensorflow/c/python_api.h"


%ignoreall
%insert("python") %{
  def TF_NewSessionOptions(target=None, config=None):
    # NOTE: target and config are validated in the session constructor.
    opts = _TF_NewSessionOptions()
    if target is not None:
      _TF_SetTarget(opts, target)
    if config is not None:
      from tensorflow.python.framework import errors
      with errors.raise_exception_on_not_ok_status() as status:
        config_str = config.SerializeToString()
        _TF_SetConfig(opts, config_str, status)
    return opts
%}

// Include the wrapper for TF_Run from tf_session_helper.h.

// The %exception block above releases the Python GIL for the length
// of each wrapped method. We disable this behavior for TF_Run
// because it uses the Python allocator.
%noexception tensorflow::TF_Run_wrapper;
%rename(TF_Run) tensorflow::TF_Run_wrapper;
%unignore tensorflow;
%unignore TF_Run;
%unignore EqualGraphDefWrapper;

// Include the wrapper for TF_PRunSetup from tf_session_helper.h.

// The %exception block above releases the Python GIL for the length
// of each wrapped method. We disable this behavior for TF_PRunSetup
// because it uses the Python allocator.
%noexception tensorflow::TF_PRunSetup_wrapper;
%rename(TF_PRunSetup) tensorflow::TF_PRunSetup_wrapper;
%unignore tensorflow;
%unignore TF_PRunSetup;

// Include the wrapper for TF_PRun from tf_session_helper.h.

// The %exception block above releases the Python GIL for the length
// of each wrapped method. We disable this behavior for TF_PRun
// because it uses the Python allocator.
%noexception tensorflow::TF_PRun_wrapper;
%rename(TF_PRun) tensorflow::TF_PRun_wrapper;
%unignore tensorflow;
%unignore TF_PRun;

%unignore tensorflow::TF_Reset_wrapper;
%insert("python") %{
def TF_Reset(target, containers=None, config=None):
  from tensorflow.python.framework import errors
  opts = TF_NewSessionOptions(target=target, config=config)
  try:
    with errors.raise_exception_on_not_ok_status() as status:
      TF_Reset_wrapper(opts, containers, status)
  finally:
    TF_DeleteSessionOptions(opts)
%}

// We use TF_GraphToFunction_wrapper instead of TF_GraphToFunction
%ignore TF_GraphToFunction;
// TF_GraphToFunction_wrapper does not use any Python methods and
// does not require GIL to be held.
%unignore TF_GraphToFunction_wrapper;

// $input is a Python list of wrapped TF_Operations
%typemap(in) (const std::vector<TF_Operation*>* opers)
    (std::vector<TF_Operation*> opers) {
  if ($input != Py_None) {
    if (!PyList_Check($input)) {
      SWIG_exception_fail(SWIG_TypeError, "$symname: expected list");
    }
    size_t size = PyList_Size($input);
    for (int i = 0; i < size; ++i) {
      PyObject* item = PyList_GetItem($input, i);
      TF_Operation* oper_ptr;
      SWIG_ConvertPtr(item, reinterpret_cast<void**>(&oper_ptr),
                      $descriptor(TF_Operation*), 0);
      opers.push_back(oper_ptr);
    }
    $1 = &opers;
  } else {
    $1 = nullptr;
  }
}

%include "tensorflow/python/client/tf_session_helper.h"

%unignoreall