{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "Tce3stUlHN0L" }, "source": [ "##### Copyright 2018 The TensorFlow Authors.\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "cellView": "form", "execution": { "iopub.execute_input": "2023-11-21T02:21:14.804820Z", "iopub.status.busy": "2023-11-21T02:21:14.804209Z", "iopub.status.idle": "2023-11-21T02:21:14.808347Z", "shell.execute_reply": "2023-11-21T02:21:14.807672Z" }, "id": "tuOe1ymfHZPu" }, "outputs": [], "source": [ "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# https://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License." ] }, { "cell_type": "markdown", "metadata": { "id": "s7Bo2MipUnXX" }, "source": [ "# Install TensorFlow for C" ] }, { "cell_type": "markdown", "metadata": { "id": "Birwb-khUOIq" }, "source": [ "\n", " \n", " \n", " \n", " \n", "
\n", " View on TensorFlow.org\n", " \n", " Run in Google Colab\n", " \n", " View source on GitHub\n", " \n", " Download notebook\n", "
" ] }, { "cell_type": "markdown", "metadata": { "id": "kFmEkitOFJSw" }, "source": [ "TensorFlow provides a C API that can be used to build\n", "[bindings for other languages](https://github.com/tensorflow/docs/tree/master/site/en/r1/guide/extend/bindings.md).\n", "The API is defined in\n", "c_api.h\n", "and designed for simplicity and uniformity rather than convenience.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "Vk--31hqIwSV" }, "source": [ "## Nightly libtensorflow C packages\n", "\n", "libtensorflow packages are built nightly and uploaded to GCS for all supported\n", "platforms. They are uploaded to the\n", "[libtensorflow-nightly GCS bucket](https://storage.googleapis.com/libtensorflow-nightly)\n", "and are indexed by operating system and date built. For MacOS and Linux shared\n", "objects, there is a\n", "[script](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/ci_build/builds/libtensorflow_nightly_symlink.sh)\n", "that renames the `.so` files versioned to the current date copied into the\n", "directory with the artifacts." ] }, { "cell_type": "markdown", "metadata": { "id": "qowtdsijFMYZ" }, "source": [ "## Supported Platforms\n", "\n", "TensorFlow for C is supported on the following systems:\n", "\n", "* Linux, 64-bit, x86\n", "* macOS, Version 10.12.6 (Sierra) or higher\n", "* Windows, 64-bit x86" ] }, { "cell_type": "markdown", "metadata": { "id": "hnhAk8y-FSBN" }, "source": [ "## Setup" ] }, { "cell_type": "markdown", "metadata": { "id": "y50y01XUFVb2" }, "source": [ "### Download and extract\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TensorFlow C libraryURL
Linux
Linux CPU onlyhttps://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-2.15.0.tar.gz
Linux GPU supporthttps://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-linux-x86_64-2.15.0.tar.gz
macOS
macOS CPU onlyhttps://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-darwin-x86_64-2.15.0.tar.gz
Windows\n", " \n", "
Windows CPU onlyhttps://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-windows-x86_64-2.15.0.zip
Windows GPU onlyhttps://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-windows-x86_64-2.10.0.zip

" ] }, { "cell_type": "markdown", "metadata": { "id": "b4kWu6k0FaT9" }, "source": [ "Extract the downloaded archive, which contains the header files to include in\n", "your C program and the shared libraries to link against.\n", "\n", "On Linux and macOS, you may want to extract to `/usr/local/lib`:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2023-11-21T02:21:14.812640Z", "iopub.status.busy": "2023-11-21T02:21:14.812071Z", "iopub.status.idle": "2023-11-21T02:21:21.047152Z", "shell.execute_reply": "2023-11-21T02:21:21.046096Z" }, "id": "DrjVyjVJFcon" }, "outputs": [], "source": [ "%%bash\n", "FILENAME=libtensorflow-cpu-linux-x86_64-2.15.0.tar.gz\n", "wget -q --no-check-certificate https://storage.googleapis.com/tensorflow/libtensorflow/${FILENAME}\n", "sudo tar -C /usr/local -xzf ${FILENAME}" ] }, { "cell_type": "markdown", "metadata": { "id": "fcBJDdojJDyk" }, "source": [ "### Linker\n", "\n", "On Linux/macOS, if you extract the TensorFlow C library to a system directory,\n", "such as `/usr/local`, configure the linker with `ldconfig`:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2023-11-21T02:21:21.051979Z", "iopub.status.busy": "2023-11-21T02:21:21.051219Z", "iopub.status.idle": "2023-11-21T02:21:21.096436Z", "shell.execute_reply": "2023-11-21T02:21:21.095467Z" }, "id": "h0STAG82JDZs" }, "outputs": [], "source": [ "%%bash\n", "sudo ldconfig /usr/local/lib" ] }, { "cell_type": "markdown", "metadata": { "id": "ix4HdnNGH6aF" }, "source": [ "If you extract the TensorFlow C library to a non-system directory, such as\n", "`~/mydir`, then configure the linker environmental variables:" ] }, { "cell_type": "markdown", "metadata": { "id": "E6E99eJzIJQs" }, "source": [ "
\n", "
\n", "

Linux

\n", "
\n",
    "export LIBRARY_PATH=$LIBRARY_PATH:~/mydir/lib\n",
    "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/mydir/lib\n",
    "
\n", "
\n", "
\n", "

macOS

\n", "
\n",
    "export LIBRARY_PATH=$LIBRARY_PATH:~/mydir/lib\n",
    "export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:~/mydir/lib\n",
    "
\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": { "id": "qYVWjxqaJVPs" }, "source": [ "## Build" ] }, { "cell_type": "markdown", "metadata": { "id": "UoMUuMJrJXp8" }, "source": [ "### Example program\n", "\n", "With the TensorFlow C library installed, create an example program with the\n", "following source code (`hello_tf.c`):" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2023-11-21T02:21:21.100892Z", "iopub.status.busy": "2023-11-21T02:21:21.100205Z", "iopub.status.idle": "2023-11-21T02:21:21.105955Z", "shell.execute_reply": "2023-11-21T02:21:21.105307Z" }, "id": "b5851f1b" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing hello_tf.c\n" ] } ], "source": [ "%%writefile hello_tf.c\n", "#include \n", "#include \n", "\n", "int main() {\n", " printf(\"Hello from TensorFlow C library version %s\\n\", TF_Version());\n", " return 0;\n", "}" ] }, { "cell_type": "markdown", "metadata": { "id": "H1GFidbrIWzU" }, "source": [ "### Compile\n", "\n", "Compile the example program to create an executable, then run:\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2023-11-21T02:21:21.149889Z", "iopub.status.busy": "2023-11-21T02:21:21.149304Z", "iopub.status.idle": "2023-11-21T02:21:21.642210Z", "shell.execute_reply": "2023-11-21T02:21:21.641297Z" }, "id": "Jph67SAjIX0M" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello from TensorFlow C library version 2.15.0\n" ] } ], "source": [ "%%bash\n", "gcc hello_tf.c -ltensorflow -o hello_tf\n", "\n", "./hello_tf" ] }, { "cell_type": "markdown", "metadata": { "id": "0qtHXROoJwoz" }, "source": [ "Success: The TensorFlow C library is configured.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "YbqbjqOSJ0IL" }, "source": [ "If the program doesn't build, make sure that `gcc` can access the TensorFlow C\n", "library. If extracted to `/usr/local`, explicitly pass the library location to\n", "the compiler:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2023-11-21T02:21:21.646641Z", "iopub.status.busy": "2023-11-21T02:21:21.645960Z", "iopub.status.idle": "2023-11-21T02:21:21.898350Z", "shell.execute_reply": "2023-11-21T02:21:21.897474Z" }, "id": "CdPmM35VJ_77" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello from TensorFlow C library version 2.15.0\n" ] } ], "source": [ "%%bash\n", "gcc -I/usr/local/include -L/usr/local/lib hello_tf.c -ltensorflow -o hello_tf\n", "\n", "./hello_tf" ] }, { "cell_type": "markdown", "metadata": { "id": "ea5fd208" }, "source": [ "## Build from source\n", "\n", "TensorFlow is open source. Read\n", "[the instructions](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/lib_package/README.md){:.external}\n", "to build TensorFlow's C library from source code." ] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "lang_c.ipynb", "toc_visible": true }, "kernelspec": { "display_name": "Python 3", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.18" } }, "nbformat": 4, "nbformat_minor": 0 }