converter_test.sh 2.86 KB
Newer Older
Terry Koo's avatar
Terry Koo committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
# Copyright 2018 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

# Test for converter tool.  To update the testdata, run the test with a single
# command-line argument specifying the path to the testdata directory.


set -e
set -u

# Infer the location of the data dependencies.
if [[ -d "${BASH_SOURCE[0]}.runfiles" ]]; then
  # Use the ".runfiles" directory if available (this typically happens when
  # running manually).  SyntaxNet does not specify a workspace name, so the
  # runfiles are placed in ".runfiles/__main__".  If SyntaxNet is configured
  # with a workspace name, then change "__main__" to that name.  See
  # https://github.com/bazelbuild/bazel/wiki/Updating-the-runfiles-tree-structure
  RUNFILES="${BASH_SOURCE[0]}.runfiles/__main__"

else
  # Otherwise, use this recipe borrowed from
  # https://github.com/bazelbuild/bazel/blob/7d265e07e7a1e37f04d53342710e4f21d9ee8083/examples/shell/test.sh#L21
  # shellcheck disable=SC2091
  RUNFILES="${RUNFILES:-"$("$(cd "$(dirname "${BASH_SOURCE[0]}")")"; pwd)"}"
fi
readonly RUNFILES

readonly RUNTIME="${RUNFILES}/dragnn/runtime"
readonly CONVERTER="${RUNTIME}/converter"
readonly SAVED_MODEL="${RUNTIME}/testdata/rnn_tagger"
readonly MASTER_SPEC="${SAVED_MODEL}/assets.extra/master_spec"
readonly EXPECTED="${RUNTIME}/testdata/converter_output"
readonly OUTPUT="${TEST_TMPDIR:-/tmp/$$}/converted"

# Fails the test with a message.
function fail() {
  echo "$@" 1>&2  # print to stderr
  exit 1
}

# Asserts that a file exists.
function assert_file_exists() {
  if [[ ! -f "$1" ]]; then
    fail "missing file: $1"
  fi
}

# Asserts that two files have the same content.
function assert_file_content_eq() {
  assert_file_exists "$1"
  assert_file_exists "$2"
  if ! diff -u "$1" "$2"; then
    fail "files differ: $1 $2"
  fi
}

rm -rf "${OUTPUT}"

"${CONVERTER}" \
  --saved_model_dir="${SAVED_MODEL}" \
  --master_spec_file="${MASTER_SPEC}" \
  --output_dir="${OUTPUT}" \
  --logtostderr

for file in \
  'MasterSpec' \
  'ArrayVariableStoreData' \
  'ArrayVariableStoreSpec'; do
  if [[ $# -gt 0 ]]; then
    # Update expected output.
    rm -f "$1/${file}"
    cp -f "${OUTPUT}/${file}" "$1/${file}"
  else
    # Compare to expected output.
    assert_file_content_eq "${OUTPUT}/${file}" "${EXPECTED}/${file}"
  fi
done

rm -rf "${OUTPUT}"