xref: /aosp_15_r20/external/federated-compute/fcp/tensorflow/crc32_op.cc (revision 14675a029014e728ec732f129a32e299b2da0601)
1 /*
2  * Copyright 2020 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "fcp/tensorflow/tensor_crc32.h"
17 #include "tensorflow/core/framework/op.h"
18 #include "tensorflow/core/framework/op_kernel.h"
19 #include "tensorflow/core/framework/shape_inference.h"
20 #include "tensorflow/core/framework/tensor.h"
21 #include "tensorflow/core/framework/types.h"
22 #include "tensorflow/core/lib/core/errors.h"
23 #include "tensorflow/core/lib/hash/crc32c.h"
24 
25 namespace fcp {
26 namespace tensorflow {
27 namespace checksums {
28 
29 using ::tensorflow::DEVICE_CPU;
30 using ::tensorflow::OpKernel;
31 using ::tensorflow::OpKernelConstruction;
32 using ::tensorflow::OpKernelContext;
33 using ::tensorflow::StringPiece;
34 using ::tensorflow::Tensor;
35 using ::tensorflow::shape_inference::InferenceContext;
36 
37 REGISTER_OP("CRC32")
38     .Input("input: T")
39     .Attr("T: type")
40     .Output("checksum: uint32")
__anon1731f43d0102(InferenceContext* c) 41     .SetShapeFn([](InferenceContext* c) {
42       c->set_output(0, c->Scalar());
43       return ::tensorflow::OkStatus();
44     });
45 
46 class CRC32Op : public OpKernel {
47  public:
CRC32Op(OpKernelConstruction * context)48   explicit CRC32Op(OpKernelConstruction* context) : OpKernel(context) {}
49 
Compute(OpKernelContext * context)50   void Compute(OpKernelContext* context) override {
51     // Create an output tensor
52     Tensor* output_tensor = nullptr;
53     OP_REQUIRES_OK(context, context->allocate_output(0, {}, &output_tensor));
54 
55     // Store CRC32 of input tensor in output.
56     output_tensor->scalar<uint32_t>()() = TensorToCRC32(context->input(0));
57   }
58 };
59 
60 REGISTER_KERNEL_BUILDER(Name("CRC32").Device(DEVICE_CPU), CRC32Op);
61 }  // namespace checksums
62 }  // namespace tensorflow
63 }  // namespace fcp
64