-
Notifications
You must be signed in to change notification settings - Fork 488
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR #8402: [XLA:CPU] [oneDNN] Enable Dot op (MatMul) in BF16 Type
Imported from GitHub PR #8402 This PR adds BF16 support in oneDNN Matmul op by allowing the Dot op to maintain the BF16 type until handled by OneDnnMatMulRewriter pass. Copybara import of the project: -- 4f7ddbc by Mahmoud Abuzaina <mahmoud.abuzaina@intel.com>: Enable MatMul op in BF16 Merging this change closes #8402 FUTURE_COPYBARA_INTEGRATE_REVIEW=#8402 from Intel-tensorflow:mabuzain/enable-bf16-matmul 4f7ddbc PiperOrigin-RevId: 598823232
- Loading branch information
1 parent
d632353
commit 7bd6d39
Showing
7 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* Copyright 2023 The TensorFlow Authors. 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. | ||
==============================================================================*/ | ||
|
||
#if defined(INTEL_MKL) && defined(ENABLE_ONEDNN_V3) | ||
|
||
#include "xla/service/cpu/cpu_float_support.h" | ||
|
||
#include "xla/service/cpu/onednn_matmul_rewriter.h" | ||
|
||
namespace xla { | ||
namespace cpu { | ||
|
||
bool CpuFloatSupport::IsSupported(const HloInstruction& hlo) const { | ||
switch (hlo.opcode()) { | ||
// Collective ops. | ||
case HloOpcode::kAllGather: | ||
case HloOpcode::kAllReduce: | ||
case HloOpcode::kAllReduceStart: | ||
case HloOpcode::kAllReduceDone: | ||
case HloOpcode::kAllToAll: | ||
case HloOpcode::kCollectivePermute: | ||
case HloOpcode::kReduceScatter: | ||
case HloOpcode::kDot: | ||
return LowPrecisionType() == BF16 && | ||
OneDnnMatMulRewriter::ShouldRewrite(&hlo) && DotSupported(hlo); | ||
// Data movement only ops. | ||
case HloOpcode::kBroadcast: | ||
case HloOpcode::kConcatenate: | ||
case HloOpcode::kCopy: | ||
case HloOpcode::kDynamicSlice: | ||
case HloOpcode::kDynamicUpdateSlice: | ||
case HloOpcode::kGather: | ||
case HloOpcode::kPad: | ||
case HloOpcode::kReshape: | ||
case HloOpcode::kReverse: | ||
case HloOpcode::kScatter: | ||
case HloOpcode::kSelect: | ||
case HloOpcode::kSelectAndScatter: | ||
case HloOpcode::kSlice: | ||
case HloOpcode::kTranspose: | ||
// Other special ops. | ||
case HloOpcode::kBitcast: | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
bool CpuFloatSupport::DotSupported(const HloInstruction& hlo) const { | ||
bool supported = true; | ||
const Shape& lhs_shape = hlo.operand(0)->shape(); | ||
const Shape& rhs_shape = hlo.operand(1)->shape(); | ||
if (lhs_shape.rank() == rhs_shape.rank() && lhs_shape.rank() == 2) { | ||
// If first dim size is 1, it may be removed by a later pass which makes it | ||
// unsupported case. | ||
supported &= lhs_shape.dimensions(0) != 1; | ||
} | ||
return supported; | ||
} | ||
|
||
} // namespace cpu | ||
} // namespace xla | ||
|
||
#endif // INTEL_MKL && ENABLE_ONEDNN_V3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* Copyright 2023 The TensorFlow Authors. 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. | ||
==============================================================================*/ | ||
|
||
#ifndef XLA_SERVICE_CPU_CPU_FLOAT_SUPPORT_H_ | ||
#define XLA_SERVICE_CPU_CPU_FLOAT_SUPPORT_H_ | ||
|
||
#if defined(INTEL_MKL) && defined(ENABLE_ONEDNN_V3) | ||
|
||
#include "xla/service/float_support.h" | ||
|
||
namespace xla { | ||
namespace cpu { | ||
|
||
class CpuFloatSupport : public FloatSupport { | ||
public: | ||
explicit CpuFloatSupport(PrimitiveType low_precision_type) | ||
: FloatSupport(low_precision_type) {} | ||
|
||
bool SupportsLowPrecisionOperand(const HloInstruction& hlo, | ||
int64_t operand_index) const override { | ||
return FloatSupport::SupportsLowPrecisionOperand(hlo, operand_index) || | ||
IsSupported(hlo); | ||
} | ||
|
||
bool SupportsLowPrecisionOutput(const HloInstruction& hlo) const override { | ||
return FloatSupport::SupportsLowPrecisionOutput(hlo) || IsSupported(hlo); | ||
} | ||
|
||
private: | ||
bool IsSupported(const HloInstruction& hlo) const; | ||
// Performs early check for things that cannot be delayed becuase some later | ||
// passes may change the shape of dot inputs. | ||
bool DotSupported(const HloInstruction& hlo) const; | ||
}; | ||
|
||
} // namespace cpu | ||
} // namespace xla | ||
|
||
#endif // INTEL_MKL && ENABLE_ONEDNN_V3 | ||
#endif // XLA_SERVICE_CPU_CPU_FLOAT_SUPPORT_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters