1 /* Copyright 2019 The TensorFlow 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 #ifndef TENSORFLOW_LITE_PYTHON_INTERPRETER_WRAPPER_NUMPY_H_ 16 #define TENSORFLOW_LITE_PYTHON_INTERPRETER_WRAPPER_NUMPY_H_ 17 18 #ifdef PyArray_Type 19 #error "Numpy cannot be included before numpy.h." 20 #endif 21 22 // Disallow Numpy 1.7 deprecated symbols. 23 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION 24 25 // To handle PyArray_* calles, numpy defines a static lookup table called 26 // PyArray_API, or PY_ARRAY_UNIQUE_SYMBOL, if defined. This causes the 27 // PyArray_* pointers to be different for different translation units, unless 28 // we take care of selectivel defined NO_IMPORT_ARRAY. 29 // 30 // Virtually every usage will define NO_IMPORT_ARRAY, and will have access to 31 // the lookup table via: 32 // extern void **PyArray_API; 33 // In numpy.cc we will define TFLITE_IMPORT_NUMPY, effectively disabling that 34 // and instead using: 35 // void **PyArray_API; 36 // which is initialized when ImportNumpy() is called. 37 // 38 // If we don't define PY_ARRAY_UNIQUE_SYMBOL then PyArray_API is a static 39 // variable, which causes strange crashes when the pointers are used across 40 // translation unit boundaries. 41 // 42 // For mone info see https://sourceforge.net/p/numpy/mailman/message/5700519 43 // See also tensorflow/python/lib/core/numpy.h for a similar approach. 44 #define PY_ARRAY_UNIQUE_SYMBOL _tensorflow_numpy_api 45 #ifndef TFLITE_IMPORT_NUMPY 46 #define NO_IMPORT_ARRAY 47 #endif 48 49 #include <Python.h> 50 51 #include "numpy/arrayobject.h" 52 #include "numpy/ufuncobject.h" 53 54 namespace tflite { 55 namespace python { 56 57 void ImportNumpy(); 58 59 } // namespace python 60 } // namespace tflite 61 62 #endif // TENSORFLOW_LITE_PYTHON_INTERPRETER_WRAPPER_NUMPY_H_ 63