nalin0503 commited on
Commit
0741d58
·
1 Parent(s): c1814c9

try new dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +58 -38
Dockerfile CHANGED
@@ -1,4 +1,4 @@
1
- FROM docker.io/nvidia/cuda:12.3.2-cudnn9-devel-ubuntu22.04
2
 
3
  # Set environment variables
4
  ENV DEBIAN_FRONTEND=noninteractive
@@ -25,74 +25,94 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
25
  # Set working directory
26
  WORKDIR /app
27
 
28
- # Copy requirements but modify TensorFlow version
29
  COPY requirements.txt /app/
30
- # Use TensorFlow 2.15.0 which has better compatibility with newer CUDA versions
31
- RUN sed -i 's/tensorflow==2.18.0/tensorflow==2.15.0/' /app/requirements.txt
32
 
33
- # Install Python dependencies
34
  RUN pip3 install --no-cache-dir --upgrade pip setuptools wheel
35
- RUN pip3 install --no-cache-dir -r requirements.txt
36
-
37
- # Install compatible OpenCV
 
 
38
  RUN pip3 install --no-cache-dir opencv-python-headless opencv-contrib-python-headless
39
 
40
  # Copy application code
41
  COPY . /app/
42
 
43
- # Create a more robust CPU fallback implementation
44
  RUN echo 'import tensorflow as tf\n\
45
  import os\n\
46
- import sys\n\
47
  \n\
48
  # Set TensorFlow logging level\n\
49
  os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"\n\
50
  \n\
51
- # Function to modify FILM.py for CPU fallback\n\
52
- def ensure_cpu_fallback():\n\
53
- # Check if we have a GPU available and supported\n\
54
  try:\n\
55
- gpus = tf.config.list_physical_devices("GPU")\n\
56
- if len(gpus) > 0:\n\
57
- for gpu in gpus:\n\
58
- tf.config.experimental.set_memory_growth(gpu, True)\n\
59
- print(f"Available GPUs: {len(gpus)}")\n\
 
 
 
60
  else:\n\
61
- print("No GPUs found, will run on CPU only")\n\
62
  except Exception as e:\n\
63
- print(f"Error setting up GPU: {e}")\n\
 
 
 
64
  \n\
65
- # Call the function\n\
66
- ensure_cpu_fallback()\n\
67
  ' > /app/tf_setup.py
68
 
69
- # Modify FILM.py to use CPU if GPU fails
70
  RUN if [ -f "/app/FILM.py" ]; then \
71
  # Import our setup at the top of the file\
72
- sed -i '1s/^/import sys\nimport os\nimport tensorflow as tf\nfrom tf_setup import *\n/' /app/FILM.py && \
73
- # Add try-except around model call\
74
- sed -i '/def __call__/a\ try:' /app/FILM.py && \
75
- sed -i '/result = self._model/i\ # Try with GPU' /app/FILM.py && \
76
- sed -i '/result = self._model/a\ except Exception as e:\n print(f"GPU inference failed: {e}, falling back to CPU")\n with tf.device("/cpu:0"):\n result = self._model(inputs, training=False)' /app/FILM.py; \
 
 
 
 
 
77
  fi
78
 
79
  # Set environment variables for GPU compatibility
80
- ENV LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:/usr/local/cuda/lib64
81
  ENV PATH=/usr/local/cuda/bin:${PATH}
 
82
  ENV TF_FORCE_GPU_ALLOW_GROWTH=true
83
 
84
- RUN ldconfig -p | grep cudnn
85
-
86
- # Expose port for Streamlit
87
- EXPOSE 8501
88
-
89
- # Create a startup script that ensures proper execution
90
  RUN echo '#!/bin/bash\n\
91
- # Ensure library paths are set correctly\n\
92
- export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:/usr/local/cuda/lib64\n\
93
- # Run the app\n\
 
 
 
 
 
 
 
 
 
 
 
 
94
  exec streamlit run app.py --server.port=8501 --server.address=0.0.0.0\n\
95
  ' > /app/start.sh && chmod +x /app/start.sh
96
 
 
 
 
97
  # Use the startup script
98
  CMD ["/app/start.sh"]
 
1
+ FROM nvidia/cuda:11.8.0-cudnn8-devel-ubuntu22.04
2
 
3
  # Set environment variables
4
  ENV DEBIAN_FRONTEND=noninteractive
 
25
  # Set working directory
26
  WORKDIR /app
27
 
28
+ # Copy requirements.txt
29
  COPY requirements.txt /app/
 
 
30
 
31
+ # Install Python dependencies with specific compatible versions
32
  RUN pip3 install --no-cache-dir --upgrade pip setuptools wheel
33
+ # Install TensorFlow with GPU support (compatible with CUDA 11.8)
34
+ RUN pip3 install --no-cache-dir tensorflow==2.12.0
35
+ # Install other dependencies but skip tensorflow (already installed)
36
+ RUN pip3 install --no-cache-dir --no-deps -r requirements.txt
37
+ RUN pip3 install --no-cache-dir tensorflow-hub==0.14.0
38
  RUN pip3 install --no-cache-dir opencv-python-headless opencv-contrib-python-headless
39
 
40
  # Copy application code
41
  COPY . /app/
42
 
43
+ # Create a robust CPU fallback implementation
44
  RUN echo 'import tensorflow as tf\n\
45
  import os\n\
 
46
  \n\
47
  # Set TensorFlow logging level\n\
48
  os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"\n\
49
  \n\
50
+ # Function to setup GPU with memory growth or fallback to CPU\n\
51
+ def setup_tensorflow():\n\
 
52
  try:\n\
53
+ # List physical devices\n\
54
+ physical_devices = tf.config.list_physical_devices("GPU")\n\
55
+ if len(physical_devices) > 0:\n\
56
+ print(f"Found {len(physical_devices)} GPU(s)")\n\
57
+ for device in physical_devices:\n\
58
+ # Allow memory growth to avoid allocating all GPU memory at once\n\
59
+ tf.config.experimental.set_memory_growth(device, True)\n\
60
+ print(f"Enabled memory growth for {device}")\n\
61
  else:\n\
62
+ print("No GPU found. Running on CPU.")\n\
63
  except Exception as e:\n\
64
+ print(f"Error setting up TensorFlow: {e}")\n\
65
+ print("Disabling GPU and falling back to CPU")\n\
66
+ # Force CPU usage if there was an error with GPU setup\n\
67
+ os.environ["CUDA_VISIBLE_DEVICES"] = "-1"\n\
68
  \n\
69
+ # Call the setup function\n\
70
+ setup_tensorflow()\n\
71
  ' > /app/tf_setup.py
72
 
73
+ # Modify FILM.py to properly handle CPU fallback
74
  RUN if [ -f "/app/FILM.py" ]; then \
75
  # Import our setup at the top of the file\
76
+ sed -i '1s/^/import tensorflow as tf\nfrom tf_setup import setup_tensorflow\n/' /app/FILM.py && \
77
+ # Add GPU check and CPU fallback in __init__\
78
+ sed -i '/def __init__/a\ # Check if GPU is disabled and use CPU if needed\n if "CUDA_VISIBLE_DEVICES" in os.environ and os.environ["CUDA_VISIBLE_DEVICES"] == "-1":\n print("GPU is disabled, using CPU for FILM")\n self._device = "/cpu:0"\n else:\n self._device = "/gpu:0"\n print(f"FILM will use device: {self._device}")' /app/FILM.py && \
79
+ # Add device context to __call__\
80
+ sed -i '/def __call__/a\ with tf.device(self._device):' /app/FILM.py && \
81
+ # Fix the model call indentation after adding the with statement\
82
+ sed -i 's/ result = self._model/ try:\n result = self._model/g' /app/FILM.py && \
83
+ sed -i '/result = self._model/a\ except Exception as e:\n print(f"Error during model inference: {e}, trying CPU fallback")\n with tf.device("/cpu:0"):\n result = self._model(inputs, training=False)' /app/FILM.py; \
84
+ # Make sure os is imported if not already\
85
+ sed -i '1s/^/import os\n/' /app/FILM.py; \
86
  fi
87
 
88
  # Set environment variables for GPU compatibility
89
+ ENV LD_LIBRARY_PATH=/usr/local/cuda/lib64:/usr/lib/x86_64-linux-gnu:${LD_LIBRARY_PATH}
90
  ENV PATH=/usr/local/cuda/bin:${PATH}
91
+ ENV CUDA_VISIBLE_DEVICES=0
92
  ENV TF_FORCE_GPU_ALLOW_GROWTH=true
93
 
94
+ # Create a startup script with proper error handling
 
 
 
 
 
95
  RUN echo '#!/bin/bash\n\
96
+ set -e\n\
97
+ \n\
98
+ # Check CUDA and cuDNN status\n\
99
+ echo "CUDA libraries:"\n\
100
+ ldconfig -p | grep cuda\n\
101
+ echo "cuDNN libraries:"\n\
102
+ ldconfig -p | grep cudnn\n\
103
+ \n\
104
+ # Test TensorFlow GPU\n\
105
+ python3 -c "import tensorflow as tf; print(\\"Num GPUs Available: \\", len(tf.config.list_physical_devices(\\"GPU\\")))" || {\n\
106
+ echo "TensorFlow GPU test failed, falling back to CPU"\n\
107
+ export CUDA_VISIBLE_DEVICES=-1\n\
108
+ }\n\
109
+ \n\
110
+ # Run the app with proper error handling\n\
111
  exec streamlit run app.py --server.port=8501 --server.address=0.0.0.0\n\
112
  ' > /app/start.sh && chmod +x /app/start.sh
113
 
114
+ # Expose port for Streamlit
115
+ EXPOSE 8501
116
+
117
  # Use the startup script
118
  CMD ["/app/start.sh"]