Home | History | Annotate | Download | only in service
      1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #include "tensorflow/compiler/xla/service/bfloat16_support.h"
     17 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
     18 #include "tensorflow/compiler/xla/service/hlo_opcode.h"
     19 
     20 namespace xla {
     21 
     22 bool BFloat16Support::SupportsBF16Operand(const HloInstruction& hlo,
     23                                           int64 operand_index) const {
     24   switch (hlo.opcode()) {
     25     case HloOpcode::kCall:
     26     case HloOpcode::kConditional:
     27     case HloOpcode::kCustomCall:
     28     case HloOpcode::kGetTupleElement:
     29     case HloOpcode::kTuple:
     30     case HloOpcode::kWhile:
     31       return true;
     32     case HloOpcode::kConvert:
     33       CHECK_EQ(operand_index, 0);
     34       return hlo.operand(0)->shape().element_type() == BF16;
     35     default:
     36       break;
     37   }
     38   return false;
     39 }
     40 
     41 bool BFloat16Support::SupportsBF16Output(const HloInstruction& hlo) const {
     42   switch (hlo.opcode()) {
     43     case HloOpcode::kCall:
     44     case HloOpcode::kConditional:
     45     case HloOpcode::kCustomCall:
     46     case HloOpcode::kGetTupleElement:
     47     case HloOpcode::kTuple:
     48     case HloOpcode::kWhile:
     49       return true;
     50     case HloOpcode::kConvert:
     51       return hlo.shape().element_type() == BF16;
     52     default:
     53       break;
     54   }
     55   return false;
     56 }
     57 
     58 bool BFloat16Support::SupportsMixedPrecisions(const HloInstruction& hlo) const {
     59   switch (hlo.opcode()) {
     60     case HloOpcode::kCall:
     61     case HloOpcode::kConditional:
     62     case HloOpcode::kConvert:
     63     case HloOpcode::kCustomCall:
     64     case HloOpcode::kGetTupleElement:
     65     case HloOpcode::kTuple:
     66     case HloOpcode::kWhile:
     67       return true;
     68     default:
     69       break;
     70   }
     71   return false;
     72 }
     73 
     74 /* static */
     75 bool BFloat16Support::EffectiveOperandPrecisionIsOutputPrecision(
     76     const HloInstruction& hlo, int64 operand_index) {
     77   switch (hlo.opcode()) {
     78     case HloOpcode::kAbs:
     79     case HloOpcode::kBroadcast:
     80     case HloOpcode::kClamp:
     81     case HloOpcode::kConcatenate:
     82     case HloOpcode::kCopy:
     83     case HloOpcode::kGetTupleElement:
     84     case HloOpcode::kMaximum:
     85     case HloOpcode::kMinimum:
     86     case HloOpcode::kPad:
     87     case HloOpcode::kReshape:
     88     case HloOpcode::kReverse:
     89     case HloOpcode::kSlice:
     90     case HloOpcode::kSort:
     91     case HloOpcode::kTranspose:
     92     case HloOpcode::kTuple:
     93       return true;
     94     case HloOpcode::kDynamicSlice:
     95       return operand_index == 0;
     96     case HloOpcode::kDynamicUpdateSlice:
     97       return operand_index == 0 || operand_index == 1;
     98     case HloOpcode::kSelect:
     99       return operand_index == 1 || operand_index == 2;
    100     default:
    101       break;
    102   }
    103   return false;
    104 }
    105 
    106 bool BFloat16Support::EffectiveOperandPrecisionIsBF16(
    107     const HloInstruction& hlo, int64 operand_index) const {
    108   return false;
    109 }
    110 
    111 }  // namespace xla
    112