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 #include "tensorflow/core/framework/common_shape_fns.h" 17 #include "tensorflow/core/framework/op.h" 18 19 namespace tensorflow { 20 21 using shape_inference::InferenceContext; 22 23 REGISTER_OP("ZeroInitializer") 24 .Input("ref: Ref(T)") 25 .Output("output_ref: Ref(T)") 26 .Attr("T: realnumbertype") 27 .SetAllowsUninitializedInput() __anonb348ebde0102(InferenceContext* c) 28 .SetShapeFn([](InferenceContext* c) { 29 c->set_output(0, c->input(0)); 30 return Status::OK(); 31 }) 32 .Doc(R"doc( 33 Initialize 'ref' with all zeros. This op requires that the tensor is not 34 initialized. The tensor will first be allocated memory, then be filled with all 35 zeros. This op is intended to save memory during initialization, 36 if you use this op, you should not run initializer of the 'ref' tensor. 37 38 ref: Should be from a `Variable` node. 39 output_ref:= Same as "ref". 40 )doc"); 41 42 REGISTER_OP("ZeroVarInitializer") 43 .Input("var: resource") 44 .Output("output_var: resource") 45 .Attr("dtype: type") 46 .Attr("shape: shape") 47 .SetAllowsUninitializedInput() __anonb348ebde0202(InferenceContext* c) 48 .SetShapeFn([](InferenceContext* c) { 49 c->set_output(0, c->Scalar()); 50 DataType t; 51 TF_RETURN_IF_ERROR(c->GetAttr("dtype", &t)); 52 PartialTensorShape p; 53 TF_RETURN_IF_ERROR(c->GetAttr("shape", &p)); 54 shape_inference::ShapeHandle s; 55 TF_RETURN_IF_ERROR(c->MakeShapeFromPartialTensorShape(p, &s)); 56 c->set_output_handle_shapes_and_types( 57 0, std::vector<shape_inference::ShapeAndType>{{s, t}}); 58 59 return Status::OK(); 60 }) 61 .Doc(R"doc( 62 Initialize 'var' with all zeros. This op requires that the resource var is not 63 initialized. The var will first be allocated memory, then be filled with all 64 zeros. This op is intended to save memory during initialization, 65 if you use this op, you should not run initializer of the var. 66 67 var: Should be a ResourceVariable. 68 output_var:= Same as "var". 69 )doc"); 70 71 } // namespace tensorflow 72