github.com/alwaysproblem/mlserving-tutorial@v0.0.0-20221124033215-121cfddbfbf4/TFserving/CustomOp/custom-op/configure.sh (about)

     1  #!/bin/bash
     2  # Copyright 2018 The TensorFlow Authors. All Rights Reserved.
     3  #
     4  # Licensed under the Apache License, Version 2.0 (the "License");
     5  # you may not use this file except in compliance with the License.
     6  # You may obtain a copy of the License at
     7  #
     8  #     http://www.apache.org/licenses/LICENSE-2.0
     9  #
    10  # Unless required by applicable law or agreed to in writing, software
    11  # distributed under the License is distributed on an "AS IS" BASIS,
    12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  # See the License for the specific language governing permissions and
    14  # limitations under the License.
    15  # ==============================================================================
    16  PLATFORM="$(uname -s | tr 'A-Z' 'a-z')"
    17  PIP="pip3"
    18  
    19  function write_to_bazelrc() {
    20    echo "$1" >> .bazelrc
    21  }
    22  
    23  function write_action_env_to_bazelrc() {
    24    write_to_bazelrc "build --action_env $1=\"$2\""
    25  }
    26  
    27  function is_linux() {
    28    [[ "${PLATFORM}" == "linux" ]]
    29  }
    30  
    31  function is_macos() {
    32    [[ "${PLATFORM}" == "darwin" ]]
    33  }
    34  
    35  function is_windows() {
    36    # On windows, the shell script is actually running in msys
    37    [[ "${PLATFORM}" =~ msys_nt*|mingw*|cygwin*|uwin* ]]
    38  }
    39  
    40  function is_ppc64le() {
    41    [[ "$(uname -m)" == "ppc64le" ]]
    42  }
    43  
    44  
    45  # Remove .bazelrc if it already exist
    46  [ -e .bazelrc ] && rm .bazelrc
    47  
    48  # Check if we are building GPU or CPU ops, default CPU
    49  while [[ "$TF_NEED_CUDA" == "" ]]; do
    50    read -p "Do you want to build ops again TensorFlow CPU pip package?"\
    51  " Y or enter for CPU (tensorflow-cpu), N for GPU (tensorflow). [Y/n] " INPUT
    52    case $INPUT in
    53      [Yy]* ) echo "Build with CPU pip package."; TF_NEED_CUDA=0;;
    54      [Nn]* ) echo "Build with GPU pip package."; TF_NEED_CUDA=1;;
    55      "" ) echo "Build with CPU pip package."; TF_NEED_CUDA=0;;
    56      * ) echo "Invalid selection: " $INPUT;;
    57    esac
    58  done
    59  
    60  # Check if we are building against manylinux1 or manylinux2010 pip package,
    61  # default manylinux2010
    62  if is_windows; then
    63    echo "On windows, skipping toolchain flags.."
    64    PIP_MANYLINUX2010=0
    65  else
    66    while [[ "$PIP_MANYLINUX2010" == "" ]]; do
    67      read -p "Does the pip package have tag manylinux2010 (usually the case for nightly release after Aug 1, 2019, or official releases past 1.14.0)?. Y or enter for manylinux2010, N for manylinux1. [Y/n] " INPUT
    68      case $INPUT in
    69        [Yy]* ) PIP_MANYLINUX2010=1;;
    70        [Nn]* ) PIP_MANYLINUX2010=0;;
    71        "" ) PIP_MANYLINUX2010=1;;
    72        * ) echo "Invalid selection: " $INPUT;;
    73      esac
    74    done
    75  fi
    76  
    77  while [[ "$TF_CUDA_VERSION" == "" ]]; do
    78    read -p "Are you building against TensorFlow 2.1(including RCs) or newer?[Y/n] " INPUT
    79    case $INPUT in
    80      [Yy]* ) echo "Build against TensorFlow 2.1 or newer."; TF_CUDA_VERSION=10.1;;
    81      [Nn]* ) echo "Build against TensorFlow <2.1."; TF_CUDA_VERSION=10.0;;
    82      "" ) echo "Build against TensorFlow 2.1 or newer."; TF_CUDA_VERSION=10.1;;
    83      * ) echo "Invalid selection: " $INPUT;;
    84    esac
    85  done
    86  
    87  
    88  # CPU
    89  if [[ "$TF_NEED_CUDA" == "0" ]]; then
    90  
    91    # Check if it's installed
    92    if [[ $(${PIP} show tensorflow-cpu) == *tensorflow-cpu* ]] || [[ $(${PIP} show tf-nightly-cpu) == *tf-nightly-cpu* ]] ; then
    93      echo 'Using installed tensorflow'
    94    else
    95      # Uninstall GPU version if it is installed.
    96      if [[ $(${PIP} show tensorflow) == *tensorflow* ]]; then
    97        echo 'Already have gpu version of tensorflow installed. Uninstalling......\n'
    98        ${PIP} uninstall tensorflow
    99      elif [[ $(${PIP} show tf-nightly) == *tf-nightly* ]]; then
   100        echo 'Already have gpu version of tensorflow installed. Uninstalling......\n'
   101        ${PIP} uninstall tf-nightly
   102      fi
   103      # Install CPU version
   104      echo 'Installing tensorflow-cpu......\n'
   105      ${PIP} install tensorflow-cpu
   106    fi
   107  
   108  else
   109  
   110    # Check if it's installed
   111     if [[ $(${PIP} show tensorflow) == *tensorflow* ]] || [[ $(${PIP} show tf-nightly) == *tf-nightly* ]]; then
   112      echo 'Using installed tensorflow'
   113    else
   114      # Uninstall CPU version if it is installed.
   115      if [[ $(${PIP} show tensorflow-cpu) == *tensorflow-cpu* ]]; then
   116        echo 'Already have tensorflow non-gpu installed. Uninstalling......\n'
   117        ${PIP} uninstall tensorflow
   118      elif [[ $(${PIP} show tf-nightly-cpu) == *tf-nightly-cpu* ]]; then
   119        echo 'Already have tensorflow non-gpu installed. Uninstalling......\n'
   120        ${PIP} uninstall tf-nightly
   121      fi
   122      # Install GPU version
   123      echo 'Installing tensorflow .....\n'
   124      ${PIP} install tensorflow
   125    fi
   126  fi
   127  
   128  
   129  TF_CFLAGS=( $(python3 -c 'import tensorflow as tf; print(" ".join(tf.sysconfig.get_compile_flags()))') )
   130  TF_LFLAGS="$(python3 -c 'import tensorflow as tf; print(" ".join(tf.sysconfig.get_link_flags()))')"
   131  
   132  # write_to_bazelrc "build:cuda --define=using_cuda=true --define=using_cuda_nvcc=true"
   133  # if [[ "$PIP_MANYLINUX2010" == "0" ]]; then
   134  #   write_to_bazelrc "build:cuda --crosstool_top=@local_config_cuda//crosstool:toolchain"
   135  # fi
   136  # Add Ubuntu toolchain flags
   137  # if is_linux; then
   138  #   write_to_bazelrc "build:manylinux2010cuda100 --crosstool_top=//third_party/toolchains/preconfig/ubuntu16.04/gcc7_manylinux2010-nvcc-cuda10.0:toolchain"
   139  #   write_to_bazelrc "build:manylinux2010cuda101 --crosstool_top=//third_party/toolchains/preconfig/ubuntu16.04/gcc7_manylinux2010-nvcc-cuda10.1:toolchain"
   140  # fi
   141  write_to_bazelrc "build --spawn_strategy=standalone"
   142  write_to_bazelrc "build --strategy=Genrule=standalone"
   143  write_to_bazelrc "build -c opt"
   144  
   145  # MSVC (Windows): Standards-conformant preprocessor mode
   146  # See https://docs.microsoft.com/en-us/cpp/preprocessor/preprocessor-experimental-overview
   147  if is_windows; then
   148    write_to_bazelrc "build --copt=/experimental:preprocessor"
   149    write_to_bazelrc "build --host_copt=/experimental:preprocessor"
   150  fi
   151  
   152  if is_windows; then
   153    # Use pywrap_tensorflow instead of tensorflow_framework on Windows
   154    SHARED_LIBRARY_DIR=${TF_CFLAGS:2:-7}"python"
   155  else
   156    SHARED_LIBRARY_DIR=${TF_LFLAGS:2}
   157  fi
   158  SHARED_LIBRARY_NAME=$(echo $TF_LFLAGS | rev | cut -d":" -f1 | rev)
   159  if ! [[ $TF_LFLAGS =~ .*:.* ]]; then
   160    if is_macos; then
   161      SHARED_LIBRARY_NAME="libtensorflow_framework.dylib"
   162    elif is_windows; then
   163      # Use pywrap_tensorflow's import library on Windows. It is in the same dir as the dll/pyd.
   164      SHARED_LIBRARY_NAME="_pywrap_tensorflow_internal.lib"
   165    else
   166      SHARED_LIBRARY_NAME="libtensorflow_framework.so"
   167    fi
   168  fi
   169  
   170  HEADER_DIR=${TF_CFLAGS:2}
   171  if is_windows; then
   172    SHARED_LIBRARY_DIR=${SHARED_LIBRARY_DIR//\\//}
   173    SHARED_LIBRARY_NAME=${SHARED_LIBRARY_NAME//\\//}
   174    HEADER_DIR=${HEADER_DIR//\\//}
   175  fi
   176  write_action_env_to_bazelrc "TF_HEADER_DIR" ${HEADER_DIR}
   177  write_action_env_to_bazelrc "TF_SHARED_LIBRARY_DIR" ${SHARED_LIBRARY_DIR}
   178  write_action_env_to_bazelrc "TF_SHARED_LIBRARY_NAME" ${SHARED_LIBRARY_NAME}
   179  write_action_env_to_bazelrc "TF_NEED_CUDA" ${TF_NEED_CUDA}
   180  
   181  # TODO(yifeif): do not hardcode path
   182  if [[ "$TF_NEED_CUDA" == "1" ]]; then
   183    write_action_env_to_bazelrc "TF_CUDA_VERSION" ${TF_CUDA_VERSION}
   184    write_action_env_to_bazelrc "TF_CUDNN_VERSION" "7"
   185    if is_windows; then
   186      write_action_env_to_bazelrc "CUDNN_INSTALL_PATH" "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v${TF_CUDA_VERSION}"
   187      write_action_env_to_bazelrc "CUDA_TOOLKIT_PATH" "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v${TF_CUDA_VERSION}"
   188    else
   189      write_action_env_to_bazelrc "CUDNN_INSTALL_PATH" "/usr/lib/x86_64-linux-gnu"
   190      write_action_env_to_bazelrc "CUDA_TOOLKIT_PATH" "/usr/local/cuda"
   191    fi
   192    write_to_bazelrc "build --config=cuda"
   193    write_to_bazelrc "test --config=cuda"
   194  fi
   195  
   196  # if [[ "$PIP_MANYLINUX2010" == "1" ]]; then
   197  #   if [[ "$TF_CUDA_VERSION" == "10.0" ]]; then
   198  #     write_to_bazelrc "build --config=manylinux2010cuda100"
   199  #     write_to_bazelrc "test --config=manylinux2010cuda100"
   200  #   else
   201  #     write_to_bazelrc "build --config=manylinux2010cuda101"
   202  #     write_to_bazelrc "test --config=manylinux2010cuda101"
   203  #   fi
   204  # fi