| 34 | |
| 35 | template <typename T> |
| 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> |
| 67 | Output Const(const Scope& scope, const T& v, const TensorShape shape) { |