Skip to content

Commit

Permalink
Formatting applied
Browse files Browse the repository at this point in the history
  • Loading branch information
DEKHTIARJonathan committed Feb 14, 2022
1 parent 65ed25c commit 951f264
Show file tree
Hide file tree
Showing 13 changed files with 771 additions and 357 deletions.
1 change: 1 addition & 0 deletions apply_py_formatting.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yapf --in-place --recursive --parallel --exclude 'tftrt/blog_posts/**/*' .
115 changes: 115 additions & 0 deletions perflab_tftrt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
export TF_TRT_SHOW_DETAILED_REPORT=1

LOGDIR="/workspace/bench_logs"
mkdir -p ${LOGDIR}/

EXEC_MODES=(
"TF_NATIVE"
"TFTRT_FP32"
"TFTRT_FP16"
"TFTRT_INT8"
)

IMAGE_CLASSIFICATION_MODELS=(
# # Historical Models
# "inception_v3"
# "inception_v4"
# "mobilenet_v1"
# "mobilenet_v2"
# "nasnet_large"
# "nasnet_mobile"
# "resnet_v1.5_50_tfv2"
# "resnet_v1_50"
# "resnet_v2_50"
# "vgg_16"
# "vgg_19"
# # JOC Model
# "resnet50-v1.5_tf1_ngc"
# # Waymo Models
"resnet50_v2_backbone"
# "resnet50_v2_sparse_backbone"
)

OBJECT_DETECTION_MODELS=(
# "faster_rcnn_resnet50_coco"
# "ssd_mobilenet_v1_fpn_coco"
"ssd_resnet_50_fpn_coco"
# "ssd_inception_v2_coco"
# "ssd_mobilenet_v1_coco"
# "ssd_mobilenet_v2_coco"
# "ssdlite_mobilenet_v2_coco"
)

TRANSFORMER_MODELS=(
# "bart_base"
# "bert_base_cased"
"bert_base_uncased"
)

COMMON_BENCH_FLAGS="--debug --use_synthetic_data --num_iterations=800"

for EXEC_MODE in "${EXEC_MODES[@]}"; do

if [[ ${exec_mode} == "TF_NATIVE" ]]; then
ADDITIONAL_ARGUMENTS=""
JOBNAME="tf_native"
else
TFTRT_PRECISION=${EXEC_MODE#*_}
ADDITIONAL_ARGUMENTS="--use_tftrt --precision=${TFTRT_PRECISION}"

if [[ ${EXEC_MODE} != "TFTRT_INT8" ]]; then
ADDITIONAL_ARGUMENTS="${ADDITIONAL_ARGUMENTS} --use_dynamic_shape"
fi
JOBNAME="tftrt_${TFTRT_PRECISION}"
fi

# ========================= IMAGE CLASSIFICATION ========================= #

cd /workspace/tftrt/examples/image_classification/

RUN_ARGS="--data_dir=/data/imagenet --input_saved_model_dir=/models/image_classification"
RUN_ARGS="${RUN_ARGS} ${COMMON_BENCH_FLAGS} --batch_size=128"

if [[ ${EXEC_MODE} == "TFTRT_INT8" ]]; then
RUN_ARGS="${RUN_ARGS} --num_calib_inputs=1280"
fi

for model_name in "${IMAGE_CLASSIFICATION_MODELS[@]}"; do
script -q -c "./scripts/${model_name}.sh ${RUN_ARGS} ${ADDITIONAL_ARGUMENTS}" /dev/null | tee "${LOGDIR}/${JOBNAME}_${model_name}.log"
done

# ========================= OBJECT DETECTION ========================= #

cd /workspace/tftrt/examples/object_detection/

RUN_ARGS="--data_dir=/data/coco2017 --input_saved_model_dir=/models/object_detection"
RUN_ARGS="${RUN_ARGS} ${COMMON_BENCH_FLAGS} --batch_size=8"

if [[ ${EXEC_MODE} == "TFTRT_INT8" ]]; then
RUN_ARGS="${RUN_ARGS} --num_calib_inputs=80"
fi

for model_name in "${OBJECT_DETECTION_MODELS[@]}"; do
script -q -c "./scripts/${model_name}.sh ${RUN_ARGS} ${ADDITIONAL_ARGUMENTS}" /dev/null | tee "${LOGDIR}/${JOBNAME}_${model_name}.log"
done

# ========================= TRANSFORMERS ========================= #

if [[ ${EXEC_MODE} != "TFTRT_INT8" ]]; then

cd /workspace/tftrt/examples/transformers/

RUN_ARGS="--input_saved_model_dir=/models/transformers"
RUN_ARGS="${RUN_ARGS} ${COMMON_BENCH_FLAGS} --batch_size=32"

if [[ "$EXEC_MODE" =~ ^TFTRT_.* ]]; then
RUN_ARGS="${RUN_ARGS} --minimum_segment_size=20"
fi

for model_name in "${TRANSFORMER_MODELS[@]}"; do
script -q -c "./scripts/${model_name}.sh ${RUN_ARGS} ${ADDITIONAL_ARGUMENTS}" /dev/null | tee "${LOGDIR}/${JOBNAME}_${model_name}.log"
done

fi

done
35 changes: 35 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[tool:pytest]
minversion = 6.0
addopts = -ra -q -s
testpaths =
tests

[yapf]
based_on_style = google

# The number of columns to use for indentation.
indent_width = 4

# The column limit.
column_limit = 80

# Place each dictionary entry onto its own line.
each_dict_entry_on_separate_line = True

# Put closing brackets on a separate line, dedented, if the bracketed
# expression can't fit in a single line. Applies to all kinds of brackets,
# including function definitions and calls. For example:
#
# config = {
# 'key1': 'value1',
# 'key2': 'value2',
# } # <--- this bracket is dedented and on a separate line
#
# time_series = self.remote_client.query_entity_counters(
# entity='dev3246.region1',
# key='dns.query_latency_tcp',
# transform=Transformation.AVERAGE(window=timedelta(seconds=60)),
# start_ts=now()-timedelta(days=3),
# end_ts=now(),
# ) # <--- this bracket is dedented and on a separate line
dedent_closing_brackets=True
2 changes: 2 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
94 changes: 94 additions & 0 deletions tests/test_yapf_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import unittest

import pygments
from pygments import console

from tests.utils import list_all_py_files
from tests.utils import CustomTestCase

from yapf.yapflib.yapf_api import FormatCode


def _read_utf_8_file(filename):
if sys.version_info.major == 2: ## Python 2 specific
with open(filename, 'rb') as f:
return unicode(f.read(), 'utf-8')
else:
with open(filename, encoding='utf-8') as f:
return f.read()


def print_color(msg, color):
print(pygments.console.colorize(color, msg))


class YAPF_Style_Test(unittest.TestCase):

@classmethod
def setUpClass(cls):

cls.badly_formatted_files = list()
cls.files_2_test = list_all_py_files()

def test_files_format(self):

total_analyzed_files = 0
for file in list_all_py_files():

total_analyzed_files += 1

try:

print(f"Testing: {file:100s}", end="")
code = _read_utf_8_file(file)

# https://pypi.python.org/pypi/yapf/0.20.2#example-as-a-module
diff, changed = FormatCode(
code,
filename=file,
style_config='setup.cfg',
print_diff=True
)

if changed:
print_color("FAILURE", "red")
self.badly_formatted_files.append(file)
else:
print_color("SUCCESS", "green")

except Exception as e:
print_color("FAILURE", "red")("FAILURE")
print(
"Error while processing file: `%s`\n"
"Error: %s" % (file, str(e))
)

str_err = ""

if self.badly_formatted_files:
for filename in self.badly_formatted_files:
str_err += f"yapf -i --style=setup.cfg {filename}\n"

str_err = "\n======================================================================================\n" \
f"Bad Coding Style: {len(self.badly_formatted_files)} file(s) need to be formatted, run the following commands to fix: \n" \
f"{str_err}" \
"======================================================================================"

passing_files = total_analyzed_files - len(self.badly_formatted_files)
print_color(
f"\nPASSING: {passing_files} / {total_analyzed_files}",
"green" if str_err == "" else "red"
)

if str_err != "":
print_color(str_err, "red")

self.assertEqual(str_err, "")


if __name__ == '__main__':
unittest.main()
34 changes: 34 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import unittest

from contextlib import contextmanager
from glob import glob, iglob

__all__ = [
'CustomTestCase',
'list_all_py_files',
]


class CustomTestCase(unittest.TestCase):

@contextmanager
def assertNotRaises(self, exc_type):
try:
yield None
except exc_type:
raise self.failureException('{} raised'.format(exc_type.__name__))


_excludes_paths = ["tftrt/blog_posts/", "tftrt/examples/third_party"]


def list_all_py_files():
for _dir in ['tests', 'tftrt']:
for _file in iglob(f"{_dir}/**/*.py", recursive=True):
if any([path in _file for path in _excludes_paths]):
continue
yield _file
Loading

0 comments on commit 951f264

Please sign in to comment.