1 /* Copyright 2016 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 #ifndef TENSORFLOW_CC_OPS_CONST_OP_H_
17 #define TENSORFLOW_CC_OPS_CONST_OP_H_
18
19 #include "tensorflow/cc/framework/ops.h"
20 #include "tensorflow/cc/framework/scope.h"
21 #include "tensorflow/core/graph/node_builder.h"
22
23 namespace tensorflow {
24 namespace ops {
25
26 /// @defgroup const_op Const Op
27 /// @{
28
29 Output Const(const Scope& scope, const Input::Initializer& val);
30
31 Output ConstFromProto(const Scope& scope, const TensorProto& proto);
32
33 NodeBuilder::NodeOut AsNodeOut(const Scope& scope, const Input& inp);
34
35 template <typename T>
Const(const Scope & scope,const Input::Initializer & val)36 Output Const(const Scope& scope, const Input::Initializer& val) {
37 auto orig_const_output = Const(scope, val);
38 if (!scope.ok()) return Output();
39
40 typedef typename Input::Initializer::RealType<T>::type DstT;
41
42 if (val.tensor.dtype() == DataTypeToEnum<DstT>::v()) {
43 return orig_const_output;
44 }
45 if (val.tensor.NumElements() == 0) {
46 Tensor t(DataTypeToEnum<DstT>::v(), val.tensor.shape());
47 return Const(scope, Input::Initializer(t));
48 }
49
50 // TODO(keveman): Refactor Cast op's kernel implementation such that the code
51 // can be directly called here instead of adding the Cast op to the graph.
52 auto orig_const = AsNodeOut(scope, orig_const_output);
53 const auto cast_op_name = scope.GetUniqueNameForOp("Cast");
54
55 auto cast_builder = NodeBuilder(cast_op_name, "Cast")
56 .Input(orig_const)
57 .Attr("DstT", DataTypeToEnum<DstT>::v());
58 scope.UpdateBuilder(&cast_builder);
59 Node* ret;
60 scope.UpdateStatus(cast_builder.Finalize(scope.graph(), &ret));
61 if (!scope.ok()) return Output();
62 scope.UpdateStatus(scope.DoShapeInference(ret));
63 return Output(ret, 0);
64 }
65
66 template <typename T>
Const(const Scope & scope,const T & v,const TensorShape shape)67 Output Const(const Scope& scope, const T& v, const TensorShape shape) {
68 return Const(scope, Input::Initializer(v, shape));
69 }
70
71 template <typename T>
Const(const Scope & scope,const std::initializer_list<T> & v,const TensorShape shape)72 Output Const(const Scope& scope, const std::initializer_list<T>& v,
73 const TensorShape shape) {
74 return Const(scope, Input::Initializer(v, shape));
75 }
76
77 std::vector<NodeBuilder::NodeOut> AsNodeOutList(const Scope& scope,
78 const InputList& inp);
79
80 /// }@
81
82 } // namespace ops
83 } // namespace tensorflow
84
85 #endif // TENSORFLOW_CC_OPS_CONST_OP_H_
86