<p dir="auto">I'm trying to write mixed C tensorflow code with python tensorflow code by<br>
embedding the CPython interpreter in my application.</p>
<p dir="auto">I'm mainly doing this because defining the model is only really possible in<br>
Python at the moment due to the lack of gradients (<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="195080234" data-permission-text="Title is private" data-url="https://github.com/tensorflow/tensorflow/issues/6268" data-hovercard-type="issue" data-hovercard-url="/tensorflow/tensorflow/issues/6268/hovercard" href="https://github.com/tensorflow/tensorflow/issues/6268">#6268</a>), and I want to define<br>
new models from the C side at speed without needing to invoke or<br>
communicate to an external python process to get a new model.</p>
<p dir="auto">To reproduce the problem is quite straightforward, simply <code class="notranslate">import tensorflow</code><br>
in python after the libtensorflow library has already been dynamically linked.<br>
Here is a quick reproducer in pure python which will not run:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import ctypes

tf_dll = ctypes.CDLL(&quot;/usr/local/lib/libtensorflow.so&quot;)

import tensorflow"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">ctypes</span>

<span class="pl-s1">tf_dll</span> <span class="pl-c1">=</span> <span class="pl-s1">ctypes</span>.<span class="pl-v">CDLL</span>(<span class="pl-s">"/usr/local/lib/libtensorflow.so"</span>)

<span class="pl-k">import</span> <span class="pl-s1">tensorflow</span></pre></div>
<p dir="auto">libtensorflow can be obtained like so:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="TF_TYPE=cpu # Set to gpu for GPU support
TF_OS=linux
curl -L \
  &quot;https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-${TF_TYPE}-${TF_OS}-x86_64-1.0.0.tar.gz&quot; |
sudo tar -C /usr/local -xz"><pre class="notranslate"><code class="notranslate">TF_TYPE=cpu # Set to gpu for GPU support
TF_OS=linux
curl -L \
  "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-${TF_TYPE}-${TF_OS}-x86_64-1.0.0.tar.gz" |
sudo tar -C /usr/local -xz
</code></pre></div>
<p dir="auto">Here are two fatal messages I have encountered (the first from the Python reproducer above, the second from a C program):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="F tensorflow/stream_executor/cuda/cuda_platform.cc:180] Check failed: ::perftools::gputools::port::Status::OK() == (MultiPlatformManager::RegisterPlatform(std::move(platform))) (OK vs. Internal: platform is already registered with name: &quot;CUDA&quot;)"><pre class="notranslate"><code class="notranslate">F tensorflow/stream_executor/cuda/cuda_platform.cc:180] Check failed: ::perftools::gputools::port::Status::OK() == (MultiPlatformManager::RegisterPlatform(std::move(platform))) (OK vs. Internal: platform is already registered with name: "CUDA")
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="F tensorflow/core/lib/monitoring/collection_registry.cc:77] Cannot register 2 metrics with the same name: /tensorflow/cc/saved_model/load_attempt_count"><pre class="notranslate"><code class="notranslate">F tensorflow/core/lib/monitoring/collection_registry.cc:77] Cannot register 2 metrics with the same name: /tensorflow/cc/saved_model/load_attempt_count
</code></pre></div>
<p dir="auto">I assume the problem is that the <code class="notranslate">_pywrap_tensorflow.so</code> has tensorflow<br>
statically linked into them, so they don't use libtensorflow. Then you have<br>
two shared libraries conflicting with one another.</p>
<p dir="auto">Is there a way to avoid this conflict?</p>
<p dir="auto">Notice that I'm not looking for a tutorial like <a href="https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/how_tos/adding_an_op/index.md">this</a>.<br>
<strong>What I'm trying to do</strong></p>
<p dir="auto">I'm new to C++ and bazel and I want to make some change on the convolution operation in tensorflow, so I decide that my first step is to create an ops just like it.</p>
<p dir="auto"><strong>What I have done</strong></p>
<p dir="auto">I copied conv_ops.cc from //tensorflow/core/kernels and change the name of the ops registrated in my new_conv_ops.cc. I also changed some name of the functions in the file to avoid duplication. And here is my BUILD file.<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/9963412/16803076/c495f610-4936-11e6-9e28-9aa27dd55e4d.png"><img src="https://cloud.githubusercontent.com/assets/9963412/16803076/c495f610-4936-11e6-9e28-9aa27dd55e4d.png" alt="qq 20160713201655" style="max-width: 100%;"></a></p>
<p dir="auto">As you can see, I copy the deps attributes of conv_ops from //tensorflow/core/kernels/BUILD. Then I use "bazel build -c opt //tensorflow/core/user_ops:new_conv_ops.so" to build the new op.</p>
<p dir="auto"><strong>What my problem is</strong></p>
<p dir="auto">Then I got this error.<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/9963412/16803048/9b960250-4936-11e6-90ab-7e7cd3b81b96.png"><img src="https://cloud.githubusercontent.com/assets/9963412/16803048/9b960250-4936-11e6-90ab-7e7cd3b81b96.png" alt="" style="max-width: 100%;"></a></p>
<p dir="auto">I tried to delete bounds_check and got same error for the next deps. Then I realize that there is some problem for including h files in //tensorflow/core/kernels from //tensorflow/core/user_ops. So how can I perfectely create a new op excatcly like conv_ops?</p>
<hr>