| 52 | } |
| 53 | |
| 54 | void Compute(OpKernelContext* c) override { |
| 55 | const Tensor& params = c->input(0); |
| 56 | const Tensor& indices = c->input(1); |
| 57 | OP_REQUIRES( |
| 58 | c, TensorShapeUtils::IsVectorOrHigher(params.shape()), |
| 59 | errors::InvalidArgument("params must be at least 1 dimensional")); |
| 60 | |
| 61 | // GatherV2 added an axis argument. For backwards compatibility with Gather, |
| 62 | // fall back to axis 0 if the op does not have an axis input. |
| 63 | int64 axis = 0; |
| 64 | bool axis_is_set = false; // Indicates whether the axis argument was set. |
| 65 | if (c->num_inputs() == 3) { |
| 66 | axis_is_set = true; |
| 67 | const Tensor& axis_tensor = c->input(2); |
| 68 | OP_REQUIRES(c, TensorShapeUtils::IsScalar(axis_tensor.shape()), |
| 69 | errors::InvalidArgument("axis must be scalar")); |
| 70 | |
| 71 | if (axis_tensor.dtype() == DT_INT32) { |
| 72 | axis = axis_tensor.scalar<int32>()(); |
| 73 | } else if (axis_tensor.dtype() == DT_INT64) { |
| 74 | axis = axis_tensor.scalar<int64>()(); |
| 75 | } else { |
| 76 | OP_REQUIRES(c, false, |
| 77 | errors::InvalidArgument("axis must be int32 or int64.")); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | OP_REQUIRES( |
| 82 | c, axis >= -params.dims() && axis < params.dims(), |
| 83 | errors::InvalidArgument("Expected axis in the range [", -params.dims(), |
| 84 | ", ", params.dims(), "), but got ", axis)); |
| 85 | |
| 86 | if (axis < 0) { |
| 87 | axis = params.dims() + axis; |
| 88 | } |
| 89 | |
| 90 | if (batch_dims_ != 0) { |
| 91 | if (batch_dims_ < 0) { |
| 92 | batch_dims_ = indices.dims() + batch_dims_; |
| 93 | } |
| 94 | |
| 95 | if (!axis_is_set) axis = batch_dims_; |
| 96 | |
| 97 | OP_REQUIRES( |
| 98 | c, batch_dims_ >= -indices.dims() && batch_dims_ < indices.dims(), |
| 99 | errors::InvalidArgument("Expected batch_dims in the range [", |
| 100 | -indices.dims(), ", ", indices.dims(), |
| 101 | "), but got ", batch_dims_)); |
| 102 | |
| 103 | OP_REQUIRES(c, batch_dims_ < params.dims(), |
| 104 | errors::InvalidArgument("batch_dims (", batch_dims_, |
| 105 | ") must be less than rank(params) (", |
| 106 | params.dims(), ").")); |
| 107 | |
| 108 | OP_REQUIRES(c, axis >= batch_dims_, |
| 109 | errors::InvalidArgument("batch_dims (", batch_dims_, |
| 110 | ") must be less than or equal to ", |
| 111 | "axis (", axis, ").")); |
nothing calls this directly
no test coverage detected