Home | History | Annotate | Download | only in util
      1 /* Copyright 2017 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 
     16 // Functions for getting information about kernels registered in the binary.
     17 #ifndef TENSORFLOW_PYTHON_UTIL_UTIL_H_
     18 #define TENSORFLOW_PYTHON_UTIL_UTIL_H_
     19 
     20 #include <Python.h>
     21 
     22 namespace tensorflow {
     23 namespace swig {
     24 
     25 // Implements the same interface as tensorflow.util.nest.is_sequence
     26 // Returns a true if its input is a collections.Sequence (except strings).
     27 //
     28 // Args:
     29 //   seq: an input sequence.
     30 //
     31 // Returns:
     32 //   True if the sequence is a not a string and is a collections.Sequence or a
     33 //   dict.
     34 bool IsSequence(PyObject* o);
     35 
     36 // Implements the same interface as tensorflow.util.nest.flatten
     37 //
     38 // Returns a flat list from a given nested structure.
     39 //
     40 // If `nest` is not a sequence, tuple, or dict, then returns a single-element
     41 // list: `[nest]`.
     42 //
     43 // In the case of dict instances, the sequence consists of the values, sorted by
     44 // key to ensure deterministic behavior. This is true also for `OrderedDict`
     45 // instances: their sequence order is ignored, the sorting order of keys is
     46 // used instead. The same convention is followed in `pack_sequence_as`. This
     47 // correctly repacks dicts and `OrderedDict`s after they have been flattened,
     48 // and also allows flattening an `OrderedDict` and then repacking it back using
     49 // a correponding plain dict, or vice-versa.
     50 // Dictionaries with non-sortable keys cannot be flattened.
     51 //
     52 // Args:
     53 //   nest: an arbitrarily nested structure or a scalar object. Note, numpy
     54 //       arrays are considered scalars.
     55 //
     56 // Returns:
     57 //   A Python list, the flattened version of the input.
     58 //   On error, returns nullptr
     59 //
     60 // Raises:
     61 //   TypeError: The nest is or contains a dict with non-sortable keys.
     62 PyObject* Flatten(PyObject* nested);
     63 
     64 // RegisterSequenceClass is used to pass PyTypeObject for collections.Sequence
     65 // (which is defined in python) into the C++ world.
     66 // Alternative approach could be to import the collections modules and retrieve
     67 // the type from the module. This approach also requires some trigger from
     68 // Python so that we know that Python interpreter had been initialzied.
     69 void RegisterSequenceClass(PyObject* sequence_class);
     70 
     71 }  // namespace swig
     72 }  // namespace tensorflow
     73 
     74 #endif  // TENSORFLOW_PYTHON_UTIL_UTIL_H_
     75