1 /* Copyright 2020 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/full_type_util.h"
17
18 #include "tensorflow/core/framework/attr_value.pb.h"
19 #include "tensorflow/core/framework/full_type.pb.h"
20 #include "tensorflow/core/framework/node_def.pb.h"
21 #include "tensorflow/core/framework/node_def_util.h"
22 #include "tensorflow/core/framework/op_def.pb.h"
23 #include "tensorflow/core/framework/types.h"
24 #include "tensorflow/core/platform/statusor.h"
25
26 namespace tensorflow {
27
28 namespace full_type {
29
Unary(FullTypeId t,const string & var_name)30 OpTypeConstructor Unary(FullTypeId t, const string& var_name) {
31 return [t, var_name](OpDef* op_def) {
32 FullTypeDef* tdef =
33 op_def->mutable_output_arg(0)->mutable_experimental_full_type();
34 tdef->set_type_id(t);
35
36 FullTypeDef* arg = tdef->add_args();
37 arg->set_type_id(TFT_VAR);
38 arg->set_s(var_name);
39
40 return Status::OK();
41 };
42 }
43
UnaryGeneric(FullTypeId t)44 OpTypeConstructor UnaryGeneric(FullTypeId t) {
45 return [t](OpDef* op_def) {
46 FullTypeDef* tdef =
47 op_def->mutable_output_arg(0)->mutable_experimental_full_type();
48 tdef->set_type_id(t);
49
50 FullTypeDef* arg = tdef->add_args();
51 arg->set_type_id(TFT_ANY);
52
53 return Status::OK();
54 };
55 }
56
UnaryTensorContainer(FullTypeId t,FullTypeId dtype)57 OpTypeConstructor UnaryTensorContainer(FullTypeId t, FullTypeId dtype) {
58 return [t, dtype](OpDef* op_def) {
59 FullTypeDef* tdef =
60 op_def->mutable_output_arg(0)->mutable_experimental_full_type();
61 tdef->set_type_id(t);
62
63 FullTypeDef* arg = tdef->add_args();
64 arg->set_type_id(TFT_TENSOR);
65 FullTypeDef* targ = arg->add_args();
66 targ->set_type_id(dtype);
67
68 return Status::OK();
69 };
70 }
71
SpecializeType(const AttrSlice & attrs,const OpDef & op_def)72 StatusOr<FullTypeDef> SpecializeType(const AttrSlice& attrs,
73 const OpDef& op_def) {
74 FullTypeDef ft;
75 ft.set_type_id(TFT_PRODUCT);
76
77 for (int i = 0; i < op_def.output_arg_size(); i++) {
78 auto* t = ft.add_args();
79
80 *t = op_def.output_arg(i).experimental_full_type();
81
82 // Resolve dependent types. The convention for op registrations is to use
83 // attributes as type variables.
84 // See https://www.tensorflow.org/guide/create_op#type_polymorphism.
85 // Once the op signature can be defined entirely in FullType, this
86 // convention can be deprecated.
87 //
88 // Note: While this code performs some basic verifications, it generally
89 // assumes consistent op defs and attributes. If more complete
90 // verifications are needed, they should be done by separately, and in a
91 // way that can be reused for type inference.
92 for (int j = 0; j < t->args_size(); j++) {
93 auto* arg = t->mutable_args(i);
94 if (arg->type_id() == TFT_VAR) {
95 const auto* attr = attrs.Find(arg->s());
96 DCHECK(attr != nullptr);
97 if (attr->value_case() == AttrValue::kList) {
98 const auto& attr_list = attr->list();
99 arg->set_type_id(TFT_PRODUCT);
100 for (int i = 0; i < attr_list.type_size(); i++) {
101 map_dtype_to_tensor(attr_list.type(i), arg->add_args());
102 }
103
104 } else if (attr->value_case() == AttrValue::kType) {
105 map_dtype_to_tensor(attr->type(), arg);
106
107 } else {
108 return Status(error::UNIMPLEMENTED,
109 absl::StrCat("unknown attribute type",
110 attrs.DebugString(), " key=", arg->s()));
111 }
112
113 arg->clear_s();
114 }
115 }
116 }
117
118 return ft;
119 }
120
121 } // namespace full_type
122
123 } // namespace tensorflow
124