1 /* Copyright 2022 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/mlir/tensorflow/utils/attribute_utils.h" 17 18 #include <algorithm> 19 20 #include "mlir/IR/Attributes.h" // from @llvm-project 21 #include "mlir/IR/BuiltinAttributes.h" // from @llvm-project 22 #include "mlir/IR/Operation.h" // from @llvm-project 23 #include "tensorflow/compiler/tf2xla/tf2xla_defs.h" 24 25 namespace mlir { 26 namespace TF { 27 28 using ::tensorflow::kValidDeviceTypes; 29 HasValidCompilationAndReplicationAttributes(Operation & op)30LogicalResult HasValidCompilationAndReplicationAttributes(Operation& op) { 31 auto replicate_attr = op.getAttrOfType<StringAttr>(kReplicationInfoAttr); 32 auto compile_attr = op.getAttrOfType<StringAttr>(kCompileDeviceTypeAttr); 33 if (replicate_attr && !compile_attr) { 34 return op.emitOpError() 35 << "has '" << kReplicationInfoAttr << "' attribute but not '" 36 << kCompileDeviceTypeAttr << "' attribute which is unsupported"; 37 } 38 if (replicate_attr && replicate_attr.getValue().empty()) { 39 return op.emitOpError() 40 << "has an empty '" << kReplicationInfoAttr << "' attribute"; 41 } 42 if (compile_attr) { 43 auto value = compile_attr.getValue(); 44 // TODO(b/229028654): Remove string conversion once we have C++17. 45 absl::string_view device_type(value.data(), value.size()); 46 auto it = std::find(kValidDeviceTypes.begin(), kValidDeviceTypes.end(), 47 device_type); 48 if (it == kValidDeviceTypes.end()) { 49 return op.emitOpError() << "has invalid '" << kCompileDeviceTypeAttr 50 << "' value '" << compile_attr.getValue() << "'"; 51 } 52 } 53 return success(); 54 } 55 56 } // namespace TF 57 } // namespace mlir 58