1#!/bin/bash
2# Copyright 2016 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#
17# Bash unit tests for TensorFlow Debugger (tfdbg) Python examples that do not
18# involve downloading data. Also tests the binary offline_analyzer.
19#
20# Command-line flags:
21#   --virtualenv: (optional) If set, will test the examples and binaries
22#     against pip install of TensorFlow in a virtualenv.
23
24set -e
25
26# Filter out LOG(INFO)
27export TF_CPP_MIN_LOG_LEVEL=1
28
29IS_VIRTUALENV=0
30PYTHON_BIN_PATH=""
31while true; do
32  if [[ -z "$1" ]]; then
33    break
34  elif [[ "$1" == "--virtualenv" ]]; then
35    IS_VIRTUALENV=1
36    PYTHON_BIN_PATH=$(which python)
37    echo
38    echo "IS_VIRTUALENV = ${IS_VIRTUALENV}"
39    echo "PYTHON_BIN_PATH = ${PYTHON_BIN_PATH}"
40    echo "Will test tfdbg examples and binaries against virtualenv pip install."
41    echo
42  fi
43  shift 1
44done
45
46if [[ -z "${PYTHON_BIN_PATH}" ]]; then
47  DEBUG_FIBONACCI_BIN="$TEST_SRCDIR/org_tensorflow/tensorflow/python/debug/debug_fibonacci"
48  DEBUG_ERRORS_BIN="$TEST_SRCDIR/org_tensorflow/tensorflow/python/debug/debug_errors"
49  DEBUG_MNIST_BIN="$TEST_SRCDIR/org_tensorflow/tensorflow/python/debug/debug_mnist"
50  DEBUG_TFLEARN_IRIS_BIN="$TEST_SRCDIR/org_tensorflow/tensorflow/python/debug/debug_tflearn_iris"
51  DEBUG_KERAS_BIN="$TEST_SRCDIR/org_tensorflow/tensorflow/python/debug/debug_keras"
52  OFFLINE_ANALYZER_BIN="$TEST_SRCDIR/org_tensorflow/tensorflow/python/debug/offline_analyzer"
53else
54  DEBUG_FIBONACCI_BIN="${PYTHON_BIN_PATH} -m tensorflow.python.debug.examples.debug_fibonacci"
55  DEBUG_ERRORS_BIN="${PYTHON_BIN_PATH} -m tensorflow.python.debug.examples.debug_errors"
56  DEBUG_MNIST_BIN="${PYTHON_BIN_PATH} -m tensorflow.python.debug.examples.debug_mnist"
57  DEBUG_TFLEARN_IRIS_BIN="${PYTHON_BIN_PATH} -m tensorflow.python.debug.examples.debug_tflearn_iris"
58  DEBUG_KERAS_BIN="${PYTHON_BIN_PATH} -m tensorflow.python.debug.examples.debug_keras"
59  OFFLINE_ANALYZER_BIN="${PYTHON_BIN_PATH} -m tensorflow.python.debug.cli.offline_analyzer"
60fi
61
62# Override the default ui_type=curses to allow the test to pass in a tty-less
63# test environment.
64cat << EOF | ${DEBUG_FIBONACCI_BIN} --tensor_size=2 --ui_type=readline
65run
66exit
67EOF
68
69cat << EOF | ${DEBUG_ERRORS_BIN} --error=no_error --ui_type=readline
70run
71exit
72EOF
73
74cat << EOF | ${DEBUG_ERRORS_BIN} --error=uninitialized_variable --debug --ui_type=readline
75run
76ni -a -d -t v/read
77exit
78EOF
79
80cat << EOF | ${DEBUG_MNIST_BIN} --debug --max_steps=1 --fake_data --ui_type=readline
81run -t 1
82run --node_name_filter hidden --op_type_filter MatMul
83run -f has_inf_or_nan
84EOF
85
86# Test the custom dump_root option.
87CUSTOM_DUMP_ROOT=$(mktemp -d)
88mkdir -p ${CUSTOM_DUMP_ROOT}
89
90cat << EOF | ${DEBUG_TFLEARN_IRIS_BIN} --debug --fake_data --train_steps=2 --dump_root="${CUSTOM_DUMP_ROOT}" --ui_type=readline
91run -p
92run -f has_inf_or_nan
93EOF
94
95# Verify that the dump root has been cleaned up on exit.
96if [[ -d "${CUSTOM_DUMP_ROOT}" ]]; then
97  echo "ERROR: dump root at ${CUSTOM_DUMP_ROOT} failed to be cleaned up." 1>&2
98  exit 1
99fi
100
101# Test debugging of tf.keras.
102cat << EOF | ${DEBUG_KERAS_BIN} --debug --ui_type=readline
103run -f has_inf_or_nan
104EOF
105
106# Test offline_analyzer.
107echo
108echo "Testing offline_analyzer"
109echo
110
111# TODO(cais): Generate an actual debug dump and load it with offline_analyzer,
112# so that we can test the binary runs with a non-error exit code.
113set +e
114OUTPUT=$(${OFFLINE_ANALYZER_BIN} 2>&1)
115set -e
116
117EXPECTED_OUTPUT="ERROR: dump_dir flag is empty."
118if ! echo "${OUTPUT}" | grep -q "${EXPECTED_OUTPUT}"; then
119  echo "ERROR: offline_analyzer output didn't match expectation: ${OUTPUT}" 1>&2
120  echo "Expected output: ${EXPECTED_OUTPUT}"
121  exit 1
122fi
123
124echo
125echo "SUCCESS: tfdbg examples and binaries test PASSED"
126