Skip to content

Commit

Permalink
tf.compat.v1.logging implemented with absl
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushmankumar7 committed Mar 17, 2020
1 parent 1e2ceff commit 3043566
Show file tree
Hide file tree
Showing 26 changed files with 90 additions and 69 deletions.
5 changes: 3 additions & 2 deletions official/benchmark/models/resnet_cifar_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from absl import app
from absl import flags
from absl import logging
import numpy as np
import tensorflow as tf
from official.benchmark.models import resnet_cifar_model
Expand Down Expand Up @@ -100,7 +101,7 @@ def on_batch_begin(self, batch, logs=None):
if lr != self.prev_lr:
self.model.optimizer.learning_rate = lr # lr should be a float here
self.prev_lr = lr
tf.compat.v1.logging.debug(
logging.debug(
'Epoch %05d Batch %05d: LearningRateBatchScheduler '
'change learning rate to %s.', self.epochs, batch, lr)

Expand Down Expand Up @@ -280,6 +281,6 @@ def main(_):


if __name__ == '__main__':
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.INFO)
logging.set_verbosity(logging.INFO)
define_cifar_flags()
app.run(main)
3 changes: 2 additions & 1 deletion official/benchmark/ncf_keras_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from absl import flags
from absl.testing import flagsaver
from absl import logging
import tensorflow as tf

from official.recommendation import ncf_common
Expand Down Expand Up @@ -51,7 +52,7 @@ def __init__(self,
def _setup(self):
"""Sets up and resets flags before each test."""
assert tf.version.VERSION.startswith('2.')
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.INFO)
logging.set_verbosity(logging.INFO)
if NCFKerasBenchmarkBase.local_flags is None:
ncf_common.define_ncf_flags()
# Loads flags to get defaults to then override. List cannot be empty.
Expand Down
3 changes: 2 additions & 1 deletion official/modeling/model_training_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from absl.testing import parameterized
from absl.testing.absltest import mock
from absl import logging
import numpy as np
import tensorflow as tf

Expand Down Expand Up @@ -125,7 +126,7 @@ def summaries_with_matching_keyword(keyword, summary_dir):
if event.summary is not None:
for value in event.summary.value:
if keyword in value.tag:
tf.compat.v1.logging.error(event)
logging.error(event)
yield event.summary


Expand Down
12 changes: 6 additions & 6 deletions official/nlp/transformer/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import numpy as np
import tensorflow as tf

from absl import logging
from official.nlp.transformer.utils import tokenizer

_EXTRA_DECODE_LENGTH = 100
Expand Down Expand Up @@ -117,7 +117,7 @@ def input_generator():
maxlen=params["decode_max_length"],
dtype="int32",
padding="post")
tf.compat.v1.logging.info("Decoding batch %d out of %d.", i,
logging.info("Decoding batch %d out of %d.", i,
num_decode_batches)
yield batch

Expand Down Expand Up @@ -172,7 +172,7 @@ def text_as_per_replica():
translation = _trim_and_decode(val_outputs[j], subtokenizer)
translations.append(translation)
if print_all_translations:
tf.compat.v1.logging.info(
logging.info(
"Translating:\n\tInput: %s\n\tOutput: %s" %
(sorted_inputs[j + i * batch_size], translation))

Expand All @@ -181,7 +181,7 @@ def text_as_per_replica():
if tf.io.gfile.isdir(output_file):
raise ValueError("File output is a directory, will not save outputs to "
"file.")
tf.compat.v1.logging.info("Writing to file %s" % output_file)
logging.info("Writing to file %s" % output_file)
with tf.compat.v1.gfile.Open(output_file, "w") as f:
for i in sorted_keys:
f.write("%s\n" % translations[i])
Expand All @@ -191,10 +191,10 @@ def translate_from_text(model, subtokenizer, txt):
encoded_txt = _encode_and_add_eos(txt, subtokenizer)
result = model.predict(encoded_txt)
outputs = result["outputs"]
tf.compat.v1.logging.info("Original: \"%s\"" % txt)
logging.info("Original: \"%s\"" % txt)
translate_from_input(outputs, subtokenizer)


def translate_from_input(outputs, subtokenizer):
translation = _trim_and_decode(outputs, subtokenizer)
tf.compat.v1.logging.info("Translation: \"%s\"" % translation)
logging.info("Translation: \"%s\"" % translation)
21 changes: 11 additions & 10 deletions official/nlp/transformer/utils/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import re
import sys
import unicodedata
from absl import logging

import numpy as np
import six
Expand Down Expand Up @@ -63,7 +64,7 @@ class Subtokenizer(object):

def __init__(self, vocab_file, reserved_tokens=None):
"""Initializes class, creating a vocab file if data_files is provided."""
tf.compat.v1.logging.info("Initializing Subtokenizer from file %s." %
logging.info("Initializing Subtokenizer from file %s." %
vocab_file)

if reserved_tokens is None:
Expand Down Expand Up @@ -109,15 +110,15 @@ def init_from_files(
reserved_tokens = RESERVED_TOKENS

if tf.io.gfile.exists(vocab_file):
tf.compat.v1.logging.info("Vocab file already exists (%s)" % vocab_file)
logging.info("Vocab file already exists (%s)" % vocab_file)
else:
tf.compat.v1.logging.info("Begin steps to create subtoken vocabulary...")
logging.info("Begin steps to create subtoken vocabulary...")
token_counts = _count_tokens(files, file_byte_limit, correct_strip)
alphabet = _generate_alphabet_dict(token_counts)
subtoken_list = _generate_subtokens_with_target_vocab_size(
token_counts, alphabet, target_vocab_size, threshold, min_count,
reserved_tokens)
tf.compat.v1.logging.info("Generated vocabulary with %d subtokens." %
logging.info("Generated vocabulary with %d subtokens." %
len(subtoken_list))
_save_vocab_file(vocab_file, subtoken_list)
return Subtokenizer(vocab_file)
Expand Down Expand Up @@ -402,7 +403,7 @@ def _generate_subtokens_with_target_vocab_size(
reserved_tokens = RESERVED_TOKENS

if min_count is not None:
tf.compat.v1.logging.info(
logging.info(
"Using min_count=%d to generate vocab with target size %d" %
(min_count, target_size))
return _generate_subtokens(
Expand All @@ -411,13 +412,13 @@ def _generate_subtokens_with_target_vocab_size(
def bisect(min_val, max_val):
"""Recursive function to binary search for subtoken vocabulary."""
cur_count = (min_val + max_val) // 2
tf.compat.v1.logging.info("Binary search: trying min_count=%d (%d %d)" %
logging.info("Binary search: trying min_count=%d (%d %d)" %
(cur_count, min_val, max_val))
subtoken_list = _generate_subtokens(
token_counts, alphabet, cur_count, reserved_tokens=reserved_tokens)

val = len(subtoken_list)
tf.compat.v1.logging.info(
logging.info(
"Binary search: min_count=%d resulted in %d tokens" % (cur_count, val))

within_threshold = abs(val - target_size) < threshold
Expand All @@ -434,7 +435,7 @@ def bisect(min_val, max_val):
return other_subtoken_list
return subtoken_list

tf.compat.v1.logging.info("Finding best min_count to get target size of %d" %
logging.info("Finding best min_count to get target size of %d" %
target_size)
return bisect(_MIN_MIN_COUNT, _MAX_MIN_COUNT)

Expand Down Expand Up @@ -603,7 +604,7 @@ def _generate_subtokens(
# subtoken_dict, count how often the resulting subtokens appear, and update
# the dictionary with subtokens w/ high enough counts.
for i in xrange(num_iterations):
tf.compat.v1.logging.info("\tGenerating subtokens: iteration %d" % i)
logging.info("\tGenerating subtokens: iteration %d" % i)
# Generate new subtoken->id dictionary using the new subtoken list.
subtoken_dict = _list_to_index_dict(subtoken_list)

Expand All @@ -616,5 +617,5 @@ def _generate_subtokens(
subtoken_list, max_subtoken_length = _gen_new_subtoken_list(
subtoken_counts, min_count, alphabet, reserved_tokens)

tf.compat.v1.logging.info("\tVocab size: %d" % len(subtoken_list))
logging.info("\tVocab size: %d" % len(subtoken_list))
return subtoken_list
3 changes: 2 additions & 1 deletion official/r1/mnist/mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from __future__ import division
from __future__ import print_function

from absl import logging
from absl import app as absl_app
from absl import flags
from six.moves import range
Expand Down Expand Up @@ -243,6 +244,6 @@ def main(_):


if __name__ == '__main__':
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.INFO)
logging.set_verbosity(logging.INFO)
define_mnist_flags()
absl_app.run(main)
4 changes: 2 additions & 2 deletions official/r1/mnist/mnist_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import unittest

import tensorflow as tf # pylint: disable=g-bad-import-order

from absl import logging
from official.r1.mnist import mnist
from official.utils.misc import keras_utils

Expand Down Expand Up @@ -143,5 +143,5 @@ def benchmark_train_step_time(self):


if __name__ == '__main__':
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
logging.set_verbosity(logging.ERROR)
tf.test.main()
7 changes: 4 additions & 3 deletions official/r1/resnet/cifar10_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import os

from absl import logging
from absl import app as absl_app
from absl import flags
from six.moves import range
Expand Down Expand Up @@ -139,7 +140,7 @@ def input_fn(is_training,
dataset = tf.data.FixedLengthRecordDataset(filenames, _RECORD_BYTES)

if input_context:
tf.compat.v1.logging.info(
logging.info(
'Sharding the dataset: input_pipeline_id=%d num_input_pipelines=%d' % (
input_context.input_pipeline_id, input_context.num_input_pipelines))
dataset = dataset.shard(input_context.num_input_pipelines,
Expand Down Expand Up @@ -270,7 +271,7 @@ def run_cifar(flags_obj):
Dictionary of results. Including final accuracy.
"""
if flags_obj.image_bytes_as_serving_input:
tf.compat.v1.logging.fatal(
logging.fatal(
'--image_bytes_as_serving_input cannot be set to True for CIFAR. '
'This flag is only applicable to ImageNet.')
return
Expand All @@ -291,6 +292,6 @@ def main(_):


if __name__ == '__main__':
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.INFO)
logging.set_verbosity(logging.INFO)
define_cifar_flags()
absl_app.run(main)
3 changes: 2 additions & 1 deletion official/r1/resnet/cifar10_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@

import numpy as np
import tensorflow as tf # pylint: disable=g-bad-import-order
from absl import logging

from official.r1.resnet import cifar10_main
from official.utils.misc import keras_utils
from official.utils.testing import integration

tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
logging.set_verbosity(logging.ERROR)

_BATCH_SIZE = 128
_HEIGHT = 32
Expand Down
3 changes: 2 additions & 1 deletion official/r1/resnet/estimator_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import time

from absl import flags
from absl import logging
from absl.testing import flagsaver
import tensorflow as tf # pylint: disable=g-bad-import-order

Expand Down Expand Up @@ -56,7 +57,7 @@ def _get_model_dir(self, folder_name):

def _setup(self):
"""Sets up and resets flags before each test."""
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.INFO)
logging.set_verbosity(logging.INFO)
if EstimatorBenchmark.local_flags is None:
for flag_method in self.flag_methods:
flag_method()
Expand Down
5 changes: 3 additions & 2 deletions official/r1/resnet/imagenet_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from absl import app as absl_app
from absl import flags
from absl import logging
from six.moves import range
import tensorflow as tf

Expand Down Expand Up @@ -194,7 +195,7 @@ def input_fn(is_training,
dataset = tf.data.Dataset.from_tensor_slices(filenames)

if input_context:
tf.compat.v1.logging.info(
logging.info(
'Sharding the dataset: input_pipeline_id=%d num_input_pipelines=%d' % (
input_context.input_pipeline_id, input_context.num_input_pipelines))
dataset = dataset.shard(input_context.num_input_pipelines,
Expand Down Expand Up @@ -387,6 +388,6 @@ def main(_):


if __name__ == '__main__':
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.INFO)
logging.set_verbosity(logging.INFO)
define_imagenet_flags()
absl_app.run(main)
2 changes: 1 addition & 1 deletion official/r1/resnet/imagenet_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from official.utils.misc import keras_utils
from official.utils.testing import integration

tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
logging.set_verbosity(logging.ERROR)

_BATCH_SIZE = 32
_LABEL_CLASSES = 1001
Expand Down
15 changes: 8 additions & 7 deletions official/r1/resnet/resnet_run_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import multiprocessing
import os

from absl import logging
from absl import flags
import tensorflow as tf

Expand Down Expand Up @@ -83,7 +84,7 @@ def process_record_dataset(dataset,
options.experimental_threading.private_threadpool_size = (
datasets_num_private_threads)
dataset = dataset.with_options(options)
tf.compat.v1.logging.info('datasets_num_private_threads: %s',
logging.info('datasets_num_private_threads: %s',
datasets_num_private_threads)

# Disable intra-op parallelism to optimize for throughput instead of latency.
Expand Down Expand Up @@ -205,16 +206,16 @@ def override_flags_and_set_envars_for_gpu_thread_pool(flags_obj):
what has been set by the user on the command-line.
"""
cpu_count = multiprocessing.cpu_count()
tf.compat.v1.logging.info('Logical CPU cores: %s', cpu_count)
logging.info('Logical CPU cores: %s', cpu_count)

# Sets up thread pool for each GPU for op scheduling.
per_gpu_thread_count = 1
total_gpu_thread_count = per_gpu_thread_count * flags_obj.num_gpus
os.environ['TF_GPU_THREAD_MODE'] = flags_obj.tf_gpu_thread_mode
os.environ['TF_GPU_THREAD_COUNT'] = str(per_gpu_thread_count)
tf.compat.v1.logging.info('TF_GPU_THREAD_COUNT: %s',
logging.info('TF_GPU_THREAD_COUNT: %s',
os.environ['TF_GPU_THREAD_COUNT'])
tf.compat.v1.logging.info('TF_GPU_THREAD_MODE: %s',
logging.info('TF_GPU_THREAD_MODE: %s',
os.environ['TF_GPU_THREAD_MODE'])

# Reduces general thread pool by number of threads used for GPU pool.
Expand Down Expand Up @@ -648,7 +649,7 @@ def input_fn_eval():
hooks=train_hooks,
max_steps=flags_obj.max_train_steps)
eval_spec = tf.estimator.EvalSpec(input_fn=input_fn_eval)
tf.compat.v1.logging.info('Starting to train and evaluate.')
logging.info('Starting to train and evaluate.')
tf.estimator.train_and_evaluate(classifier, train_spec, eval_spec)
# tf.estimator.train_and_evalute doesn't return anything in multi-worker
# case.
Expand All @@ -671,7 +672,7 @@ def input_fn_eval():
schedule[-1] = train_epochs - sum(schedule[:-1]) # over counting.

for cycle_index, num_train_epochs in enumerate(schedule):
tf.compat.v1.logging.info('Starting cycle: %d/%d', cycle_index,
logging.info('Starting cycle: %d/%d', cycle_index,
int(n_loops))

if num_train_epochs:
Expand All @@ -691,7 +692,7 @@ def input_fn_eval():
# allows the eval (which is generally unimportant in those circumstances)
# to terminate. Note that eval will run for max_train_steps each loop,
# regardless of the global_step count.
tf.compat.v1.logging.info('Starting to evaluate.')
logging.info('Starting to evaluate.')
eval_results = classifier.evaluate(input_fn=input_fn_eval,
steps=flags_obj.max_train_steps)

Expand Down
Loading

0 comments on commit 3043566

Please sign in to comment.