From b47107a7789f5aad65800baa6239d49d18dd9b51 Mon Sep 17 00:00:00 2001
From: "Xiaoming (Jason) Cui" <xiaoming.cui@intel.com>
Date: Sat, 16 May 2020 00:10:24 -0700
Subject: [PATCH 1/5] [INTEL MKL] Added input name in the bfloat16 namescope
 interface so that it can create correct node name when it is needed. The
 default value of the name is '', which will make sure the change is backward
 compatible, and won't affect existing models which suse bfloat16 namescope

---
 tensorflow/python/tpu/bfloat16.py      |  4 +--
 tensorflow/python/tpu/bfloat16_test.py | 38 ++++++++++++++++++++++++--
 2 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/tensorflow/python/tpu/bfloat16.py b/tensorflow/python/tpu/bfloat16.py
index 70f71815e51..c0bade4ecf7 100644
--- a/tensorflow/python/tpu/bfloat16.py
+++ b/tensorflow/python/tpu/bfloat16.py
@@ -70,11 +70,11 @@ def _get_custom_getter():
 
 @tf_export(v1=['tpu.bfloat16_scope'])
 @tf_contextlib.contextmanager
-def bfloat16_scope():
+def bfloat16_scope(name = ''):
   """Scope class for bfloat16 variables so that the model uses custom getter.
 
   This enables variables to be read as bfloat16 type when using get_variable.
   """
   with variable_scope.variable_scope(
-      '', custom_getter=_get_custom_getter()) as varscope:
+      name, custom_getter=_get_custom_getter()) as varscope:
     yield varscope
diff --git a/tensorflow/python/tpu/bfloat16_test.py b/tensorflow/python/tpu/bfloat16_test.py
index 78157ea86c2..5e59efb7617 100644
--- a/tensorflow/python/tpu/bfloat16_test.py
+++ b/tensorflow/python/tpu/bfloat16_test.py
@@ -24,15 +24,49 @@ from tensorflow.python.framework import test_util
 from tensorflow.python.ops import variable_scope
 from tensorflow.python.platform import test
 from tensorflow.python.tpu import bfloat16
-
+from tensorflow.python.framework import ops
+from tensorflow.python.ops import math_ops
+from tensorflow.python.ops import variables
 
 class BFloat16ScopeTest(test.TestCase):
 
-  def testScopeName(self):
+  def testDefaultScopeName(self):
     """Test if name for the variable scope is propagated correctly."""
     with bfloat16.bfloat16_scope() as bf:
       self.assertEqual(bf.name, "")
 
+  def testCustomScopeName(self):
+    """Test if custom name for the variable scope is propagated correctly."""
+    name = 'bfloat16'
+    with bfloat16.bfloat16_scope('bfloat16') as bf:
+      self.assertEqual(bf.name, name)
+
+  def testVariableName(self):
+    """Test if custom name for the variable scope is propagated correctly."""
+    g = ops.Graph()
+    with g.as_default():
+      a = variables.Variable(2.2, name='var_a')
+      b = variables.Variable(3.3, name='var_b')
+      d = variables.Variable(4.4, name='var_b')
+      with g.name_scope('scope1'):
+        with bfloat16.bfloat16_scope("bf16"):
+          a = math_ops.cast(a, dtypes.bfloat16)
+          b = math_ops.cast(b, dtypes.bfloat16)
+          c = math_ops.add(a, b, name='addition')
+        with bfloat16.bfloat16_scope():
+          d = math_ops.cast(d, dtypes.bfloat16)
+          math_ops.add(c, d, name='addition')
+
+    g_ops = g.get_operations()
+    ops_name = []
+    for op in g_ops:
+      ops_name.append(str(op.name))
+
+    self.assertIn('scope1/bf16/addition', ops_name)
+    self.assertIn('scope1/bf16/Cast', ops_name)
+    self.assertIn('scope1/addition', ops_name)
+    self.assertIn('scope1/Cast', ops_name)
+
   @test_util.run_deprecated_v1
   def testRequestedDType(self):
     """Test if requested dtype is honored in the getter.

From bee45404472b4d083809657239542dd7d903982f Mon Sep 17 00:00:00 2001
From: "Xiaoming (Jason) Cui" <xiaoming.cui@intel.com>
Date: Mon, 18 May 2020 15:03:54 -0700
Subject: [PATCH 2/5] [INTEL MKL] minor format changing in the code

---
 tensorflow/python/tpu/bfloat16.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tensorflow/python/tpu/bfloat16.py b/tensorflow/python/tpu/bfloat16.py
index c0bade4ecf7..0e4a1441fcf 100644
--- a/tensorflow/python/tpu/bfloat16.py
+++ b/tensorflow/python/tpu/bfloat16.py
@@ -70,7 +70,7 @@ def _get_custom_getter():
 
 @tf_export(v1=['tpu.bfloat16_scope'])
 @tf_contextlib.contextmanager
-def bfloat16_scope(name = ''):
+def bfloat16_scope(name=''):
   """Scope class for bfloat16 variables so that the model uses custom getter.
 
   This enables variables to be read as bfloat16 type when using get_variable.

From 5ee67850468e5055ecec6d4434256d791fc85fe1 Mon Sep 17 00:00:00 2001
From: "Xiaoming (Jason) Cui" <xiaoming.cui@intel.com>
Date: Mon, 18 May 2020 17:40:28 -0700
Subject: [PATCH 3/5] [INTEL MKL] Updated goldens because of API changes so
 that unit test api_test can pass

---
 tensorflow/tools/api/golden/v1/tensorflow.tpu.pbtxt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tensorflow/tools/api/golden/v1/tensorflow.tpu.pbtxt b/tensorflow/tools/api/golden/v1/tensorflow.tpu.pbtxt
index 7f9f0e874f9..bd2d8e9b49a 100644
--- a/tensorflow/tools/api/golden/v1/tensorflow.tpu.pbtxt
+++ b/tensorflow/tools/api/golden/v1/tensorflow.tpu.pbtxt
@@ -18,7 +18,7 @@ tf_module {
   }
   member_method {
     name: "bfloat16_scope"
-    argspec: "args=[], varargs=None, keywords=None, defaults=None"
+    argspec: "args=[\'name\'], varargs=None, keywords=None, defaults=[\'\'], "
   }
   member_method {
     name: "core"

From f484a668027538127947bec0d00bdc77b266a76d Mon Sep 17 00:00:00 2001
From: "Xiaoming (Jason) Cui" <xiaoming.cui@intel.com>
Date: Wed, 27 May 2020 11:10:44 -0700
Subject: [PATCH 4/5] [INTEL MKL] Changed the default value of input parameter
 to None to align with API policy

---
 tensorflow/python/tpu/bfloat16.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/tensorflow/python/tpu/bfloat16.py b/tensorflow/python/tpu/bfloat16.py
index 0e4a1441fcf..9761d7f7a0e 100644
--- a/tensorflow/python/tpu/bfloat16.py
+++ b/tensorflow/python/tpu/bfloat16.py
@@ -70,11 +70,13 @@ def _get_custom_getter():
 
 @tf_export(v1=['tpu.bfloat16_scope'])
 @tf_contextlib.contextmanager
-def bfloat16_scope(name=''):
+def bfloat16_scope(name=None):
   """Scope class for bfloat16 variables so that the model uses custom getter.
 
   This enables variables to be read as bfloat16 type when using get_variable.
   """
+  if name is None:
+    name = ''
   with variable_scope.variable_scope(
       name, custom_getter=_get_custom_getter()) as varscope:
     yield varscope

From c620ad3a861709243099a9f85670accc3953a54e Mon Sep 17 00:00:00 2001
From: "Xiaoming (Jason) Cui" <xiaoming.cui@intel.com>
Date: Thu, 28 May 2020 16:33:16 -0700
Subject: [PATCH 5/5] [INTEL MKL] Updated the v1 goldens due to API interface
 changes

---
 tensorflow/tools/api/golden/v1/tensorflow.tpu.pbtxt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tensorflow/tools/api/golden/v1/tensorflow.tpu.pbtxt b/tensorflow/tools/api/golden/v1/tensorflow.tpu.pbtxt
index bd2d8e9b49a..47213e34e3d 100644
--- a/tensorflow/tools/api/golden/v1/tensorflow.tpu.pbtxt
+++ b/tensorflow/tools/api/golden/v1/tensorflow.tpu.pbtxt
@@ -18,7 +18,7 @@ tf_module {
   }
   member_method {
     name: "bfloat16_scope"
-    argspec: "args=[\'name\'], varargs=None, keywords=None, defaults=[\'\'], "
+    argspec: "args=[\'name\'], varargs=None, keywords=None, defaults=[\'None\'], "
   }
   member_method {
     name: "core"