1 /* Copyright 2021 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_CORE_KERNELS_TENSOR_TO_HASH_BUCKET_OP_H_ 17 #define TENSORFLOW_CORE_KERNELS_TENSOR_TO_HASH_BUCKET_OP_H_ 18 19 #include <string> 20 21 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor" 22 #include "tensorflow/core/framework/op_kernel.h" 23 #include "tensorflow/core/framework/tensor.h" 24 #include "tensorflow/core/lib/core/errors.h" 25 #include "tensorflow/core/lib/core/status.h" 26 #include "tensorflow/core/lib/strings/stringprintf.h" 27 #include "tensorflow/core/platform/fingerprint.h" 28 #include "tensorflow/core/platform/macros.h" 29 #include "tensorflow/core/platform/types.h" 30 31 namespace tensorflow { 32 33 namespace functor { 34 35 template <typename Device, typename T> 36 struct LaunchTensorToHashBucket { operatorLaunchTensorToHashBucket37 void operator()(OpKernelContext* c, const int64_t num_buckets, const T* input, 38 const int num_elems, int64_t* output) { 39 string format = "%"; 40 switch (DataTypeToEnum<T>::value) { 41 case DT_INT8: 42 case DT_INT16: 43 case DT_INT32: 44 strings::Appendf(&format, "d"); 45 break; 46 case DT_INT64: 47 strings::Appendf(&format, "lld"); 48 break; 49 default: 50 bool type_not_supported = true; 51 OP_REQUIRES( 52 c, !type_not_supported, 53 errors::InvalidArgument("Type not supported: ", 54 DataTypeString(DataTypeToEnum<T>::value))); 55 } 56 57 for (int i = 0; i < num_elems; ++i) { 58 string input_str = strings::Printf(format.c_str(), input[i]); 59 const uint64 input_hash = Fingerprint64(input_str); 60 const uint64 bucket_id = input_hash % num_buckets; 61 // The number of buckets is always in the positive range of int64 so is 62 // the resulting bucket_id. Casting the bucket_id from uint64 to int64 is 63 // safe. 64 output[i] = static_cast<int64_t>(bucket_id); 65 } 66 } 67 }; 68 69 #if GOOGLE_CUDA 70 template <typename T> 71 struct LaunchTensorToHashBucket<Eigen::GpuDevice, T> { 72 void operator()(OpKernelContext* c, const int64_t num_buckets, const T* input, 73 const int num_elems, int64_t* output); 74 }; 75 #endif // GOOGLE_CUDA 76 } // namespace functor 77 78 } // namespace tensorflow 79 80 #endif // TENSORFLOW_CORE_KERNELS_TENSOR_TO_HASH_BUCKET_OP_H_ 81