1 /* Copyright 2015 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/core/framework/kernel_def_builder.h" 17 #include "tensorflow/core/framework/attr_value.pb.h" 18 #include "tensorflow/core/framework/kernel_def.pb_text.h" 19 #include "tensorflow/core/framework/kernel_def.pb.h" 20 21 namespace tensorflow { 22 KernelDefBuilder(const char * op_name)23KernelDefBuilder::KernelDefBuilder(const char* op_name) { 24 kernel_def_ = new KernelDef; 25 kernel_def_->set_op(op_name); 26 } 27 ~KernelDefBuilder()28KernelDefBuilder::~KernelDefBuilder() { 29 DCHECK(kernel_def_ == nullptr) << "Did not call Build()"; 30 } 31 Device(const char * device_type)32KernelDefBuilder& KernelDefBuilder::Device(const char* device_type) { 33 kernel_def_->set_device_type(device_type); 34 return *this; 35 } 36 TypeConstraint(const char * attr_name,gtl::ArraySlice<DataType> allowed)37KernelDefBuilder& KernelDefBuilder::TypeConstraint( 38 const char* attr_name, gtl::ArraySlice<DataType> allowed) { 39 auto* constraint = kernel_def_->add_constraint(); 40 constraint->set_name(attr_name); 41 auto* allowed_values = constraint->mutable_allowed_values()->mutable_list(); 42 for (DataType dt : allowed) { 43 allowed_values->add_type(dt); 44 } 45 return *this; 46 } 47 TypeConstraint(const char * attr_name,DataType allowed)48KernelDefBuilder& KernelDefBuilder::TypeConstraint(const char* attr_name, 49 DataType allowed) { 50 auto* constraint = kernel_def_->add_constraint(); 51 constraint->set_name(attr_name); 52 constraint->mutable_allowed_values()->mutable_list()->add_type(allowed); 53 return *this; 54 } 55 HostMemory(const char * arg_name)56KernelDefBuilder& KernelDefBuilder::HostMemory(const char* arg_name) { 57 kernel_def_->add_host_memory_arg(arg_name); 58 return *this; 59 } 60 Label(const char * label)61KernelDefBuilder& KernelDefBuilder::Label(const char* label) { 62 CHECK_EQ(kernel_def_->label(), "") 63 << "Trying to set a kernel's label a second time: '" << label 64 << "' in: " << ProtoShortDebugString(*kernel_def_); 65 kernel_def_->set_label(label); 66 return *this; 67 } 68 Priority(int32 priority)69KernelDefBuilder& KernelDefBuilder::Priority(int32 priority) { 70 kernel_def_->set_priority(priority); 71 return *this; 72 } 73 Build()74const KernelDef* KernelDefBuilder::Build() { 75 KernelDef* r = kernel_def_; 76 kernel_def_ = nullptr; 77 return r; 78 } 79 80 } // namespace tensorflow 81