Skip to content

Commit c5a16f9

Browse files
MarkDaoustcopybara-github
authored andcommitted
Testable install instructions.
PiperOrigin-RevId: 414771628
1 parent 9ed343e commit c5a16f9

File tree

2 files changed

+380
-144
lines changed

2 files changed

+380
-144
lines changed

site/en/install/lang_c.ipynb

Lines changed: 380 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,380 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"id": "Tce3stUlHN0L"
7+
},
8+
"source": [
9+
"##### Copyright 2018 The TensorFlow Authors.\n"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": null,
15+
"metadata": {
16+
"cellView": "form",
17+
"id": "tuOe1ymfHZPu"
18+
},
19+
"outputs": [],
20+
"source": [
21+
"#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
22+
"# you may not use this file except in compliance with the License.\n",
23+
"# You may obtain a copy of the License at\n",
24+
"#\n",
25+
"# https://www.apache.org/licenses/LICENSE-2.0\n",
26+
"#\n",
27+
"# Unless required by applicable law or agreed to in writing, software\n",
28+
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
29+
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
30+
"# See the License for the specific language governing permissions and\n",
31+
"# limitations under the License."
32+
]
33+
},
34+
{
35+
"cell_type": "markdown",
36+
"metadata": {
37+
"id": "s7Bo2MipUnXX"
38+
},
39+
"source": [
40+
"# Install TensorFlow for C"
41+
]
42+
},
43+
{
44+
"cell_type": "markdown",
45+
"metadata": {
46+
"id": "Birwb-khUOIq"
47+
},
48+
"source": [
49+
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
50+
" <td>\n",
51+
" <a target=\"_blank\" href=\"https://www.tensorflow.org/guide/distributed_training\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />View on TensorFlow.org</a>\n",
52+
" </td>\n",
53+
" <td>\n",
54+
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/guide/distributed_training.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
55+
" </td>\n",
56+
" <td>\n",
57+
" <a target=\"_blank\" href=\"https://github.com/tensorflow/docs/blob/master/site/en/guide/distributed_training.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
58+
" </td>\n",
59+
" <td>\n",
60+
" <a href=\"https://storage.googleapis.com/tensorflow_docs/docs/site/en/guide/distributed_training.ipynb\"><img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n",
61+
" </td>\n",
62+
"</table>"
63+
]
64+
},
65+
{
66+
"cell_type": "markdown",
67+
"metadata": {
68+
"id": "kFmEkitOFJSw"
69+
},
70+
"source": [
71+
"TensorFlow provides a C API that can be used to build\n",
72+
"[bindings for other languages](https://github.com/tensorflow/docs/tree/master/site/en/r1/guide/extend/bindings.md).\n",
73+
"The API is defined in\n",
74+
"<a href=\"https://github.com/tensorflow/tensorflow/blob/master/tensorflow/c/c_api.h\" class=\"external\"><code>c_api.h</code></a>\n",
75+
"and designed for simplicity and uniformity rather than convenience.\n"
76+
]
77+
},
78+
{
79+
"cell_type": "markdown",
80+
"metadata": {
81+
"id": "Vk--31hqIwSV"
82+
},
83+
"source": [
84+
"## Nightly Libtensorflow C packages\n",
85+
"\n",
86+
"Libtensorflow packages are built nightly and uploaded to GCS for all supported\n",
87+
"platforms. They are uploaded to the\n",
88+
"[libtensorflow-nightly GCS bucket](https://storage.googleapis.com/libtensorflow-nightly)\n",
89+
"and are indexed by operating system and date built. For MacOS and Linux shared\n",
90+
"objects, we have a\n",
91+
"[script](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/ci_build/builds/libtensorflow_nightly_symlink.sh)\n",
92+
"that renames the .so files versioned to the current date copied into the\n",
93+
"directory with the artifacts."
94+
]
95+
},
96+
{
97+
"cell_type": "markdown",
98+
"metadata": {
99+
"id": "qowtdsijFMYZ"
100+
},
101+
"source": [
102+
"\n",
103+
"\n",
104+
"## Supported Platforms\n",
105+
"\n",
106+
"TensorFlow for C is supported on the following systems:\n",
107+
"\n",
108+
"* Linux, 64-bit, x86\n",
109+
"* macOS, Version 10.12.6 (Sierra) or higher\n",
110+
"* Windows, 64-bit x86"
111+
]
112+
},
113+
{
114+
"cell_type": "markdown",
115+
"metadata": {
116+
"id": "hnhAk8y-FSBN"
117+
},
118+
"source": [
119+
"## Setup"
120+
]
121+
},
122+
{
123+
"cell_type": "markdown",
124+
"metadata": {
125+
"id": "y50y01XUFVb2"
126+
},
127+
"source": [
128+
"\n",
129+
"### Download & extract\n",
130+
"\n",
131+
"<table>\n",
132+
" <tr><th>TensorFlow C library</th><th>URL</th></tr>\n",
133+
" <tr class=\"alt\"><td colspan=\"2\">Linux</td></tr>\n",
134+
" <tr>\n",
135+
" <td>Linux CPU only</td>\n",
136+
" <td class=\"devsite-click-to-copy\"><a href=\"https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-2.7.0.tar.gz\">https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-2.7.0.tar.gz</a></td>\n",
137+
" </tr>\n",
138+
" <tr>\n",
139+
" <td>Linux GPU support</td>\n",
140+
" <td class=\"devsite-click-to-copy\"><a href=\"https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-linux-x86_64-2.7.0.tar.gz\">https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-linux-x86_64-2.7.0.tar.gz</a></td>\n",
141+
" </tr>\n",
142+
" <tr class=\"alt\"><td colspan=\"2\">macOS</td></tr>\n",
143+
" <tr>\n",
144+
" <td>macOS CPU only</td>\n",
145+
" <td class=\"devsite-click-to-copy\"><a href=\"https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-darwin-x86_64-2.7.0.tar.gz\">https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-darwin-x86_64-2.7.0.tar.gz</a></td>\n",
146+
" </tr>\n",
147+
" <tr class=\"alt\"><td colspan=\"2\">Windows</td></tr>\n",
148+
" <tr>\n",
149+
" <td>Windows CPU only</td>\n",
150+
" <td class=\"devsite-click-to-copy\"><a href=\"https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-windows-x86_64-2.7.0.zip\">https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-windows-x86_64-2.7.0.zip</a></td>\n",
151+
" </tr>\n",
152+
" <tr>\n",
153+
" <td>Windows GPU only</td>\n",
154+
" <td class=\"devsite-click-to-copy\"><a href=\"https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-windows-x86_64-2.7.0.zip\">https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-windows-x86_64-2.7.0.zip</a></td>\n",
155+
" </tr>\n",
156+
"</table>"
157+
]
158+
},
159+
{
160+
"cell_type": "markdown",
161+
"metadata": {
162+
"id": "b4kWu6k0FaT9"
163+
},
164+
"source": [
165+
"Extract the downloaded archive, which contains the header files to include in\n",
166+
"your C program and the shared libraries to link against.\n",
167+
"\n",
168+
"On Linux and macOS, you may want to extract to `/usr/local/lib`:"
169+
]
170+
},
171+
{
172+
"cell_type": "code",
173+
"execution_count": null,
174+
"metadata": {
175+
"id": "DrjVyjVJFcon"
176+
},
177+
"outputs": [],
178+
"source": [
179+
"%%bash\n",
180+
"FILENAME=libtensorflow-cpu-linux-x86_64-2.7.0.tar.gz\n",
181+
"wget -q --no-check-certificate https://storage.googleapis.com/tensorflow/libtensorflow/${FILENAME}\n",
182+
"sudo tar -C /usr/local -xzf ${FILENAME}"
183+
]
184+
},
185+
{
186+
"cell_type": "markdown",
187+
"metadata": {
188+
"id": "fcBJDdojJDyk"
189+
},
190+
"source": [
191+
"\n",
192+
"### Linker\n",
193+
"\n",
194+
"On Linux/macOS, if you extract the TensorFlow C library to a system directory,\n",
195+
"such as `/usr/local`, configure the linker with `ldconfig`:"
196+
]
197+
},
198+
{
199+
"cell_type": "code",
200+
"execution_count": null,
201+
"metadata": {
202+
"id": "h0STAG82JDZs"
203+
},
204+
"outputs": [],
205+
"source": [
206+
"%%bash\n",
207+
"sudo ldconfig /usr/local/lib"
208+
]
209+
},
210+
{
211+
"cell_type": "markdown",
212+
"metadata": {
213+
"id": "ix4HdnNGH6aF"
214+
},
215+
"source": [
216+
"If you extract the TensorFlow C library to a non-system directory, such as\n",
217+
"`~/mydir`, then configure the linker environmental variables:"
218+
]
219+
},
220+
{
221+
"cell_type": "markdown",
222+
"metadata": {
223+
"id": "E6E99eJzIJQs"
224+
},
225+
"source": [
226+
"<div class=\"ds-selector-tabs\">\n",
227+
"<section>\n",
228+
"<h3>Linux</h3>\n",
229+
"<pre class=\"prettyprint lang-bsh\">\n",
230+
"export LIBRARY_PATH=$LIBRARY_PATH:~/mydir/lib\n",
231+
"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/mydir/lib\n",
232+
"</pre>\n",
233+
"</section>\n",
234+
"<section>\n",
235+
"<h3>macOS</h3>\n",
236+
"<pre class=\"prettyprint lang-bsh\">\n",
237+
"export LIBRARY_PATH=$LIBRARY_PATH:~/mydir/lib\n",
238+
"export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:~/mydir/lib\n",
239+
"</pre>\n",
240+
"</section>\n",
241+
"</div><!--/ds-selector-tabs-->"
242+
]
243+
},
244+
{
245+
"cell_type": "markdown",
246+
"metadata": {
247+
"id": "qYVWjxqaJVPs"
248+
},
249+
"source": [
250+
"## Build"
251+
]
252+
},
253+
{
254+
"cell_type": "markdown",
255+
"metadata": {
256+
"id": "UoMUuMJrJXp8"
257+
},
258+
"source": [
259+
"### Example program\n",
260+
"\n",
261+
"With the TensorFlow C library installed, create an example program with the\n",
262+
"following source code (`hello_tf.c`):"
263+
]
264+
},
265+
{
266+
"cell_type": "code",
267+
"execution_count": null,
268+
"metadata": {
269+
"attributes": {
270+
"classes": [
271+
"c"
272+
],
273+
"id": ""
274+
},
275+
"id": "b5851f1b"
276+
},
277+
"outputs": [],
278+
"source": [
279+
"%%writefile hello_tf.c\n",
280+
"#include <stdio.h>\n",
281+
"#include <tensorflow/c/c_api.h>\n",
282+
"\n",
283+
"int main() {\n",
284+
" printf(\"Hello from TensorFlow C library version %s\\n\", TF_Version());\n",
285+
" return 0;\n",
286+
"}"
287+
]
288+
},
289+
{
290+
"cell_type": "markdown",
291+
"metadata": {
292+
"id": "H1GFidbrIWzU"
293+
},
294+
"source": [
295+
"### Compile\n",
296+
"\n",
297+
"Compile the example program to create an executable, then run:\n"
298+
]
299+
},
300+
{
301+
"cell_type": "code",
302+
"execution_count": null,
303+
"metadata": {
304+
"id": "Jph67SAjIX0M"
305+
},
306+
"outputs": [],
307+
"source": [
308+
"%%bash\n",
309+
"gcc hello_tf.c -ltensorflow -o hello_tf\n",
310+
"\n",
311+
"./hello_tf"
312+
]
313+
},
314+
{
315+
"cell_type": "markdown",
316+
"metadata": {
317+
"id": "0qtHXROoJwoz"
318+
},
319+
"source": [
320+
"Success: The TensorFlow C library is configured.\n"
321+
]
322+
},
323+
{
324+
"cell_type": "markdown",
325+
"metadata": {
326+
"id": "YbqbjqOSJ0IL"
327+
},
328+
"source": [
329+
"If the program doesn't build, make sure that `gcc` can access the TensorFlow C\n",
330+
"library. If extracted to `/usr/local`, explicitly pass the library location to\n",
331+
"the compiler:"
332+
]
333+
},
334+
{
335+
"cell_type": "code",
336+
"execution_count": null,
337+
"metadata": {
338+
"id": "CdPmM35VJ_77"
339+
},
340+
"outputs": [],
341+
"source": [
342+
"%%bash\n",
343+
"gcc -I/usr/local/include -L/usr/local/lib hello_tf.c -ltensorflow -o hello_tf\n",
344+
"\n",
345+
"./hello_tf"
346+
]
347+
},
348+
{
349+
"cell_type": "markdown",
350+
"metadata": {
351+
"id": "ea5fd208"
352+
},
353+
"source": [
354+
"\n",
355+
"\n",
356+
"\n",
357+
"## Build from source\n",
358+
"\n",
359+
"TensorFlow is open source. Read\n",
360+
"[the instructions](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/lib_package/README.md){:.external}\n",
361+
"to build TensorFlow's C library from source code."
362+
]
363+
}
364+
],
365+
"metadata": {
366+
"colab": {
367+
"collapsed_sections": [],
368+
"name": "Copy of lang_c.ipynb",
369+
"private_outputs": true,
370+
"provenance": [],
371+
"toc_visible": true
372+
},
373+
"kernelspec": {
374+
"display_name": "Python 3",
375+
"name": "python3"
376+
}
377+
},
378+
"nbformat": 4,
379+
"nbformat_minor": 0
380+
}

0 commit comments

Comments
 (0)