Skip to content

Commit b2647cb

Browse files
Merge pull request #48286 from Intel-tensorflow:mabuzain/enable-mkl-auto-mixed-precision
PiperOrigin-RevId: 367043563 Change-Id: Icd69518522d9af55e5b8f9784cbea35463e1082f
2 parents 330e860 + 61796c0 commit b2647cb

3 files changed

Lines changed: 23 additions & 12 deletions

File tree

‎tensorflow/core/grappler/optimizers/auto_mixed_precision.cc‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,7 +2004,7 @@ Status AutoMixedPrecision::Optimize(Cluster* cluster, const GrapplerItem& item,
20042004
return errors::InvalidArgument("cluster == nullptr");
20052005
}
20062006

2007-
#if !defined(ENABLE_MKL)
2007+
#if !defined(INTEL_MKL)
20082008
if (mode_ == AutoMixedPrecisionMode::MKL) {
20092009
return errors::Unimplemented(
20102010
"The auto_mixed_precision_mkl optimizer cannot be used since "
@@ -2014,7 +2014,7 @@ Status AutoMixedPrecision::Optimize(Cluster* cluster, const GrapplerItem& item,
20142014
"https://software.intel.com/en-us/articles/intel-optimization-for-"
20152015
"tensorflow-installation-guide");
20162016
}
2017-
#endif // ENABLE_MKL
2017+
#endif // INTEL_MKL
20182018

20192019
// Start by copying input graph to output.
20202020
*output = item.graph;

‎tensorflow/core/grappler/optimizers/auto_mixed_precision_test.cc‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
1313
limitations under the License.
1414
==============================================================================*/
1515

16-
#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM || ENABLE_MKL
16+
#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM || INTEL_MKL
1717

1818
#include "tensorflow/core/grappler/optimizers/auto_mixed_precision.h"
1919

@@ -1189,7 +1189,7 @@ TEST_F(AutoMixedPrecisionTest, TanhOp) {
11891189

11901190
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
11911191

1192-
#if ENABLE_MKL
1192+
#if INTEL_MKL
11931193

11941194
class AutoMixedPrecisionMklTest : public GrapplerTest {
11951195
protected:
@@ -1203,7 +1203,8 @@ class AutoMixedPrecisionMklTest : public GrapplerTest {
12031203
};
12041204

12051205
TEST_F(AutoMixedPrecisionMklTest, AlreadyBf16) {
1206-
tensorflow::Scope s = tensorflow::Scope::NewRootScope();
1206+
tensorflow::Scope s = tensorflow::Scope::NewRootScope().WithDevice(
1207+
"/job:localhost/replica:0/task:0/device:CPU:0");
12071208
Output input = ops::Const(s.WithOpName("input"), 1.f, {32, 32});
12081209
Output cst1 = ops::Cast(s.WithOpName("cst1"), input, DT_BFLOAT16);
12091210
Output allow1 = ops::MatMul(s.WithOpName("allow1"), cst1, cst1);
@@ -1241,7 +1242,8 @@ TEST_F(AutoMixedPrecisionMklTest, AlreadyBf16) {
12411242
}
12421243

12431244
TEST_F(AutoMixedPrecisionMklTest, Simple) {
1244-
tensorflow::Scope s = tensorflow::Scope::NewRootScope();
1245+
tensorflow::Scope s = tensorflow::Scope::NewRootScope().WithDevice(
1246+
"/job:localhost/replica:0/task:0/device:CPU:0");
12451247
Output input = ops::Const(s.WithOpName("input"), 1.f / 32, {32, 32});
12461248
Output deny1 = ops::Exp(s.WithOpName("deny1"), input);
12471249
Output clr1 = ops::Relu(s.WithOpName("clr1"), deny1);
@@ -1290,7 +1292,8 @@ TEST_F(AutoMixedPrecisionMklTest, Simple) {
12901292
}
12911293

12921294
TEST_F(AutoMixedPrecisionMklTest, TensorListSetGet) {
1293-
tensorflow::Scope s = tensorflow::Scope::NewRootScope();
1295+
tensorflow::Scope s = tensorflow::Scope::NewRootScope().WithDevice(
1296+
"/job:localhost/replica:0/task:0/device:CPU:0");
12941297
tensorflow::Input shape = {32, 32};
12951298
auto tl1 = ops::TensorListReserve(s.WithOpName("tl1"), {32, 32}, 8, DT_FLOAT);
12961299
Output input = ops::Const(s.WithOpName("input"), 1.f / 32, {32, 32});
@@ -1365,10 +1368,10 @@ TEST_F(AutoMixedPrecisionMklTest, TensorListSetGet) {
13651368
}
13661369
}
13671370

1368-
#endif // ENABLE_MKL
1371+
#endif // INTEL_MKL
13691372

13701373
} // namespace
13711374
} // namespace grappler
13721375
} // namespace tensorflow
13731376

1374-
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM || ENABLE_MKL
1377+
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM || INTEL_MKL

‎tensorflow/core/grappler/optimizers/meta_optimizer.cc‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ limitations under the License.
5555
#include "tensorflow/core/lib/gtl/map_util.h"
5656
#include "tensorflow/core/util/dump_graph.h"
5757
#include "tensorflow/core/util/ptr_util.h"
58+
#include "tensorflow/core/util/util.h"
5859
#include "tensorflow/core/util/xla_config_registry.h"
5960

6061
namespace tensorflow {
@@ -218,8 +219,12 @@ std::unique_ptr<GraphOptimizer> MetaOptimizer::MakeNewOptimizer(
218219
/*CPU layout conversion*/ cfg_.cpu_layout_conversion()));
219220
MK_OPT("auto_mixed_precision", "auto_mixed_precision",
220221
new AutoMixedPrecision(AutoMixedPrecisionMode::CUDA));
221-
MK_OPT("auto_mixed_precision_mkl", "auto_mixed_precision_mkl",
222-
new AutoMixedPrecision(AutoMixedPrecisionMode::MKL));
222+
#ifdef INTEL_MKL
223+
if (IsMKLEnabled()) {
224+
MK_OPT("auto_mixed_precision_mkl", "auto_mixed_precision_mkl",
225+
new AutoMixedPrecision(AutoMixedPrecisionMode::MKL));
226+
}
227+
#endif
223228
MK_OPT("memory", "memory_optimization",
224229
new MemoryOptimizer(RewriterConfig::MANUAL));
225230
MK_OPT("common_subgraph_elimination", "common_subgraph_elimination",
@@ -307,12 +312,15 @@ Status MetaOptimizer::InitializeOptimizers(
307312
optimizers->push_back(
308313
MakeUnique<AutoMixedPrecision>(AutoMixedPrecisionMode::CUDA));
309314
}
315+
#ifdef INTEL_MKL
310316
if (AutoMixedPrecisionEnabled(cfg_.auto_mixed_precision_mkl()) &&
311317
AutoMixedPrecisionEnabled(
312-
plugin_configs.toggle_config["auto_mixed_precision_mkl"])) {
318+
plugin_configs.toggle_config["auto_mixed_precision_mkl"]) &&
319+
IsMKLEnabled()) {
313320
optimizers->push_back(
314321
MakeUnique<AutoMixedPrecision>(AutoMixedPrecisionMode::MKL));
315322
}
323+
#endif
316324
if (BOTH_ARE_ON(pin_to_host_optimization)) {
317325
optimizers->push_back(MakeUnique<PinToHostOptimizer>());
318326
}

0 commit comments

Comments
 (0)