tensorflow: Errors when building iOS binaries with selective registration

The header file generated by print_selective_registration_header.py makes use of strcmp, which causes an error when compiling TensorFlow for iOS with selective registration enabled (-DSELECTIVE_REGISTRATION)

Apparently this was fixed in a commit back in October but that commit was reverted the same day for some reason.

I’ve tried building the tool from the commit of TensorFlow that has the fix, but ./configure fails for me on that version (missing targets & zlib download). I’ve also tried replacing calls to strcmp with a constexpr variant, but I’m not much help with C++ and couldn’t get anything to work there.

This prevents me from compiling a slimmed down TensorFlow binary for iOS.

cc @petewarden @cwhipkey

You must complete this information or else your issue will be closed

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow)?: No, just following the instructions in print_selective_registration_header.py and selective_registration.h
  • TensorFlow installed from (source or binary)?: Source
  • TensorFlow version: master (45115c0a985815feef3a97a13d6b082997b38e5d)
  • Bazel version (if compiling from source): 0.4.5
  • CUDA/cuDNN version: N/A
  • GPU Model and Memory: N/A
  • Exact command to reproduce:
    $ bazel build tensorflow/python/tools/print_selective_registration_header
    $ bazel-bin/tensorflow/python/tools/print_selective_registration_header \
      --graphs=graph.pb > tensorflow/core/framework/ops_to_register.h
    $ tensorflow/contrib/makefile/compile_ios_tensorflow.sh "-Os -DSELECTIVE_REGISTRATION"
    
    

Source Code / Logs

Here is the relevant log output on the last command:

In file included from ./tensorflow/core/framework/selective_registration.h:46:
./tensorflow/core/framework/ops_to_register.h:4:23: error: constexpr function never produces a
      constant expression [-Winvalid-constexpr]
constexpr inline bool ShouldRegisterOp(const char op[]) {
                      ^
./tensorflow/core/framework/ops_to_register.h:6:10: note: non-constexpr function 'strcmp' cannot
      be used in a constant expression
     || (strcmp(op, "Add") == 0)
         ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS10.2.sdk/usr/include/string.h:77:6: note:
      declared here
int      strcmp(const char *__s1, const char *__s2);
         ^

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 23 (13 by maintainers)

Most upvoted comments

Alright @cwhipkey it worked! The full solution I used is as follows:

  1. Build the print_selective_registration_header with the right macro for iOS
    $ bazel build --copt="-DUSE_GEMM_FOR_CONV" tensorflow/python/tools/print_selective_registration_header
    
  2. Generate a header in the right path, i.e.
    $ bazel-bin/tensorflow/python/tools/print_selective_registration_header \
      --graphs=graph.pb > tensorflow/core/framework/ops_to_register.h
    
  3. Edit ops_to_register.h
    • Replace all occurrences of strcmp with strcmpc
    • Add the following function definition at the top of the file
      constexpr int strcmpc(const char* a, const char* b)
      {
        return *a == 0 && *b == 0 ? 0 :
        *a == ' ' ? strcmpc(a+1,b) :
        *b == ' ' ? strcmpc(a,b+1) :
        *a == 0 ? -1 :
        *b == 0 ? 1 :
        *a < *b ? -1 :
        *a > *b ? 1 :
        *a == *b ? strcmpc(a+1, b+1) :
        false;
      }
      
  4. Build TensorFlow for iOS with the -DSELECTIVE_REGISTRATION flag:
    $ tensorflow/contrib/makefile/compile_ios_tensorflow.sh "-Os -DSELECTIVE_REGISTRATION"
    

In my case, the resulting libtensorflow-core.a is 45% smaller, the universal .ipa is 6.1MB lighter, and the unzipped payload is 19.5MB lighter. (Of course, your mileage may vary depending on the graph you are using selective registration on.) Thanks everyone!