-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_code_hash_only_cpu_kernels.cc
126 lines (111 loc) · 4.47 KB
/
binary_code_hash_only_cpu_kernels.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include <string>
#include <iostream>
#include "tensorflow/core/framework/op_kernel.h"
using namespace tensorflow;
template <typename T>
class BinaryCodeHashOp : public OpKernel {
public:
explicit BinaryCodeHashOp(OpKernelConstruction* context) : OpKernel(context) {
// Check the inputs
OP_REQUIRES_OK(context, context->GetAttr("length", &length_));
OP_REQUIRES_OK(context, context->GetAttr("t", &t_));
OP_REQUIRES_OK(context, context->GetAttr("strategy", &strategy_));
OP_REQUIRES(context, length_ > 0,
errors::InvalidArgument("Need length > 0, got ", length_));
OP_REQUIRES(context, t_ > 0,
errors::InvalidArgument("Need t > 0, got ", t_));
OP_REQUIRES(context, length_ >= t_,
errors::InvalidArgument("Need length >= t, got length: ", length_, " and t: ", t_));
// std::cout << "length: " << length_ << ", t: " << t_ << ", strategy: " << strategy_ << std::endl;
}
void Compute(OpKernelContext* context) override {
// Grab the input tensor
const Tensor& input_tensor = context->input(0);
auto input = input_tensor.flat<T>();
// OP_REQUIRES(context, TensorShapeUtils::IsVector(input_tensor.shape()),
// errors::InvalidArgument("BinaryCodeHash expects a 1-D vector."));
// Create an output tensor
int block_num;
int block_length;
if (strategy_ == "succession"){
block_num = (length_ - 1) / t_ + 1;
block_length = t_;
} else {
block_num = t_ + 1;
block_length = (length_ - 1) / block_num + 1;
}
Tensor* output_tensor = NULL;
// OP_REQUIRES_OK(context, context->allocate_output(0, input_tensor.shape(),
// &output_tensor));
// tensorflow::TensorShape output_shape({input_tensor.shape().dim_size(0), block_num});
tensorflow::TensorShape output_shape = input_tensor.shape();
output_shape.AddDim(block_num); // Add New dimension
OP_REQUIRES_OK(context, context->allocate_output(0, output_shape, &output_tensor));
auto output_flat = output_tensor->flat<T>();
// Compute Binary Code Hash.
const int N = input.size();
// std::cout << "input size: " << N << ", output size: " << output_flat.size() << std::endl;
for (int i = 0; i < N; i++) {
// Convert into binary
int binary_code[length_];
T num = input(i);
for(int k=0; k<length_; k++){
if (num > 0){
binary_code[k] = num % 2;
num = num / 2;
} else {
binary_code[k] = 0;
}
}
// Convert into 10base every block
if (strategy_ == "succession"){
for (int n = 0; n < block_num; n++){
T num = 0;
T start_index = n * (1 << block_length);
for (int m = 0; m < t_; m++){
if (n*t_+m>=length_){
break;
}
if (binary_code[n*t_+m] == 1){
num += 1 << m;
}
}
output_flat(i*block_num+n) = num + start_index;
}
}else { // skip
for (int n = 0; n < block_num; n++){
T num = 0;
T start_index = n * (1 << block_length);
for (int m = n; m < length_; m+=t_+1){
if (binary_code[m] == 1){
num += 1 << m;
}
}
output_flat(i*block_num+n) = num + start_index;
}
}
}
}
private:
int length_;
int t_;
std::string strategy_;
};
// REGISTER_KERNEL_BUILDER(Name("BinaryCodeHash").Device(DEVICE_CPU).TypeConstraint<T>("T"), BinaryCodeHashOp<T>);
#define REGISTER_CPU(T) \
REGISTER_KERNEL_BUILDER( \
Name("BinaryCodeHash").Device(DEVICE_CPU).TypeConstraint<T>("T"), \
BinaryCodeHashOp<T>);
REGISTER_CPU(int64);
REGISTER_CPU(int32);