| 162 | } |
| 163 | |
| 164 | void Compile(XlaOpKernelContext* context) override { |
| 165 | auto input = context->Input(0); |
| 166 | auto input_shape = context->InputShape(0); |
| 167 | auto indices = context->Input(1); |
| 168 | auto indices_shape = context->InputShape(1); |
| 169 | |
| 170 | absl::optional<int64> axis; |
| 171 | if (context->num_inputs() == 3) { |
| 172 | const TensorShape axis_shape = context->InputShape(2); |
| 173 | OP_REQUIRES(context, TensorShapeUtils::IsScalar(axis_shape), |
| 174 | errors::InvalidArgument("axis must be scalar")); |
| 175 | DataType axis_type = input_type(2); |
| 176 | OP_REQUIRES(context, axis_type == DT_INT32 || axis_type == DT_INT64, |
| 177 | errors::InvalidArgument("axis must be int32 or int64")); |
| 178 | |
| 179 | int64 axis_input; |
| 180 | OP_REQUIRES_OK(context, |
| 181 | context->ConstantInputAsIntScalar(2, &axis_input)); |
| 182 | |
| 183 | const auto params_dims = input_shape.dims(); |
| 184 | OP_REQUIRES(context, |
| 185 | -params_dims <= axis_input && axis_input < params_dims, |
| 186 | errors::InvalidArgument("Expected axis in the range [", |
| 187 | -params_dims, ", ", params_dims, |
| 188 | "), but got ", axis_input)); |
| 189 | if (axis_input < 0) { |
| 190 | axis_input += params_dims; |
| 191 | } |
| 192 | axis = axis_input; |
| 193 | } |
| 194 | |
| 195 | if (batch_dims_ != 0) { |
| 196 | if (batch_dims_ < 0) { |
| 197 | batch_dims_ = indices_shape.dims() + batch_dims_; |
| 198 | } |
| 199 | |
| 200 | axis = axis.value_or(batch_dims_); |
| 201 | |
| 202 | OP_REQUIRES(context, |
| 203 | batch_dims_ >= -indices_shape.dims() && |
| 204 | batch_dims_ < indices_shape.dims(), |
| 205 | errors::InvalidArgument("Expected batch_dims in the range [", |
| 206 | -indices_shape.dims(), ", ", |
| 207 | indices_shape.dims(), "), but got ", |
| 208 | batch_dims_)); |
| 209 | |
| 210 | OP_REQUIRES(context, batch_dims_ < input_shape.dims(), |
| 211 | errors::InvalidArgument("batch_dims (", batch_dims_, |
| 212 | ") must be less than rank(input) (", |
| 213 | input_shape.dims(), ").")); |
| 214 | |
| 215 | OP_REQUIRES(context, *axis >= batch_dims_, |
| 216 | errors::InvalidArgument("batch_dims (", batch_dims_, |
| 217 | ") must be less than or equal to ", |
| 218 | "axis (", *axis, ").")); |
| 219 | } |
| 220 | |
| 221 | axis = axis.value_or(0); |
nothing calls this directly
no test coverage detected