unpack_sparse_features.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Copyright 2016 Google Inc. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #define EIGEN_USE_THREADS
  13. #include <memory>
  14. #include <string>
  15. #include <utility>
  16. #include <vector>
  17. #include "syntaxnet/sparse.pb.h"
  18. #include "syntaxnet/utils.h"
  19. #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
  20. #include "tensorflow/core/framework/op_kernel.h"
  21. #include "tensorflow/core/framework/tensor.h"
  22. #include "tensorflow/core/framework/types.h"
  23. using tensorflow::DEVICE_CPU;
  24. using tensorflow::DT_FLOAT;
  25. using tensorflow::DT_INT32;
  26. using tensorflow::DT_INT64;
  27. using tensorflow::DT_STRING;
  28. using tensorflow::OpKernel;
  29. using tensorflow::OpKernelConstruction;
  30. using tensorflow::OpKernelContext;
  31. using tensorflow::Tensor;
  32. using tensorflow::TensorShape;
  33. using tensorflow::errors::InvalidArgument;
  34. namespace syntaxnet {
  35. // Operator to unpack ids and weights stored in SparseFeatures proto.
  36. class UnpackSyntaxNetSparseFeatures : public OpKernel {
  37. public:
  38. explicit UnpackSyntaxNetSparseFeatures(OpKernelConstruction *context)
  39. : OpKernel(context) {
  40. OP_REQUIRES_OK(context, context->MatchSignature(
  41. {DT_STRING}, {DT_INT32, DT_INT64, DT_FLOAT}));
  42. }
  43. void Compute(OpKernelContext *context) override {
  44. const Tensor &input = context->input(0);
  45. OP_REQUIRES(context, IsLegacyVector(input.shape()),
  46. InvalidArgument("input should be a vector."));
  47. const int64 n = input.NumElements();
  48. const auto input_vec = input.flat<string>();
  49. SparseFeatures sf;
  50. int output_size = 0;
  51. std::vector<std::pair<int64, float> > id_and_weight;
  52. // Guess that we'll be averaging a handful of ids per SparseFeatures record.
  53. id_and_weight.reserve(n * 4);
  54. std::vector<int> num_ids(n);
  55. for (int64 i = 0; i < n; ++i) {
  56. OP_REQUIRES(context, sf.ParseFromString(input_vec(i)),
  57. InvalidArgument("Couldn't parse as SparseFeature"));
  58. OP_REQUIRES(context,
  59. sf.weight_size() == 0 || sf.weight_size() == sf.id_size(),
  60. InvalidArgument(tensorflow::strings::StrCat(
  61. "Incorrect number of weights", sf.DebugString())));
  62. int n_ids = sf.id_size();
  63. num_ids[i] = n_ids;
  64. output_size += n_ids;
  65. for (int j = 0; j < n_ids; j++) {
  66. float w = (sf.weight_size() > 0) ? sf.weight(j) : 1.0f;
  67. id_and_weight.push_back(std::make_pair(sf.id(j), w));
  68. }
  69. }
  70. Tensor *indices_t;
  71. OP_REQUIRES_OK(context, context->allocate_output(
  72. 0, TensorShape({output_size}), &indices_t));
  73. Tensor *ids_t;
  74. OP_REQUIRES_OK(context, context->allocate_output(
  75. 1, TensorShape({output_size}), &ids_t));
  76. Tensor *weights_t;
  77. OP_REQUIRES_OK(context, context->allocate_output(
  78. 2, TensorShape({output_size}), &weights_t));
  79. auto indices = indices_t->vec<int32>();
  80. auto ids = ids_t->vec<int64>();
  81. auto weights = weights_t->vec<float>();
  82. int c = 0;
  83. for (int64 i = 0; i < n; ++i) {
  84. for (int j = 0; j < num_ids[i]; ++j) {
  85. indices(c) = i;
  86. ids(c) = id_and_weight[c].first;
  87. weights(c) = id_and_weight[c].second;
  88. ++c;
  89. }
  90. }
  91. }
  92. };
  93. REGISTER_KERNEL_BUILDER(
  94. Name("UnpackSyntaxNetSparseFeatures").Device(DEVICE_CPU),
  95. UnpackSyntaxNetSparseFeatures);
  96. } // namespace syntaxnet