1

I'm trying to create a wrapper for my Tensorflow code so that I can invoke it from a C++ program. Here's a toy example code written in Cython.

# Saved as ufa.pyx

import tensorflow as tf                                                                                                                                                                          
import numpy as np                                                                                                                                                                                

def execute_tf():                                                                                                                                                                                 
        print("Here")                                                                                                                                                                             
        return 0.0                                                                                                                                                                                
        #x = tf.placeholder(tf.float32, [1, 2])                                                                                                                                                   
        #w = tf.constant([0.2, 0.3], tf.float32)                                                                                                                                                  
        #b = tf.constant( -0.1, tf.float32)                                                                                                                                                       
        #linearModel = tf.matmul(x, tf.reshape(w, shape=[2, 1])) + b                                                                                                                              
        #session = tf.Session()                                                                                                                                                                   
        #result = session.run(linearModel, feed_dict={x: np.reshape([-0.1, -0.4], [1, 2])} )                                                                                                      
        #return np.reshape(result, [])                                                                                                                                                            

cdef public float executeTF():                                                                                                                                                                    
        return execute_tf()

As you can see, most of the lines of execute_tf are commented out. Here's the driver code written in C++

// Saved as driver.cpp

#include "Python.h"                                                                                                                                                                               
#include "ufa.h"                                                                                                                                                                                  
#include <iostream>                                                                                                                                                                               
using namespace std;                                                                                                                                                                              

int main(int argc, char *argv[])                                                                                                                                                                  
{                                                                                                                                                                                                 
    Py_Initialize();                                                                                                                                                                              
    PyInit_ufa();                                                                                                                                                                                 
    cout << "Hello world" << endl;                                                                                                                                                                
    executeTF();                                                                                                                                                                                  
    Py_Finalize();                                                                                                                                                                                
    return 0;                                                                                                                                                                                     
}

I compile it as follows

$ cython --cplus ufa.pyx
$ g++ ufa.cpp driver.cpp -L$(python3.6-config --cflags) -I$(python3.6-config --ldflags) -std=c++11 -o test.opt

If I compile this while commenting out import tensorflow as tf, the program runs fine. However, if I import tensorflow, I get the following error

Hello world
NameError: name 'execute_tf' is not defined
Exception ignored in: 'ufa.executeTF'
NameError: name 'execute_tf' is not defined

Any idea why it is happening? Here's some more details of the system configuration:

  • Python: 3.6.10
  • Cython: 0.26
  • GCC: 8.3.1
  • Tensorflow: 1.15.0
  • OS: CentOS
Rahul Bera
  • 11
  • 1
  • 1
    I'd suggest an exception has happened during `PyInit_ufa` that you haven't tested for and from then on everything else is failing. – DavidW Apr 06 '20 at 06:41
  • That probably explains, why your embeded python doesn't find the library: https://stackoverflow.com/q/56857449/5769463, if not you need to share information how your python installation is set up. – ead Apr 06 '20 at 07:21
  • Thanks for the answer @ead. Yes, supplying the PYTHONPATH solved the issue! – Rahul Bera Apr 06 '20 at 09:30

0 Answers0