| 36 | is_gpu_(ctx->device_type().type_string() == DEVICE_GPU_XLA_JIT) {} |
| 37 | |
| 38 | void XlaArgMinMaxOp::Compile(XlaOpKernelContext* ctx) { |
| 39 | const TensorShape input_shape = ctx->InputShape(0); |
| 40 | const TensorShape dimension_shape = ctx->InputShape(1); |
| 41 | |
| 42 | OP_REQUIRES(ctx, TensorShapeUtils::IsScalar(dimension_shape), |
| 43 | errors::InvalidArgument( |
| 44 | "dim must be a scalar, but received tensor of shape: ", |
| 45 | dimension_shape.DebugString())); |
| 46 | |
| 47 | int64 dim; |
| 48 | OP_REQUIRES_OK(ctx, ctx->ConstantInputAsIntScalar(1, &dim)); |
| 49 | |
| 50 | const int input_dims = input_shape.dims(); |
| 51 | const int axis = dim < 0 ? dim + input_dims : dim; |
| 52 | |
| 53 | OP_REQUIRES( |
| 54 | ctx, axis >= 0 && axis < input_dims, |
| 55 | errors::InvalidArgument("Expected dimension in the range [", -input_dims, |
| 56 | ", ", input_dims, "), but got ", dim)); |
| 57 | const int64 axis_size = input_shape.dim_size(axis); |
| 58 | OP_REQUIRES( |
| 59 | ctx, axis_size > 0, |
| 60 | errors::InvalidArgument("Reduction axis ", dim, " is empty in shape ", |
| 61 | input_shape.DebugString())); |
| 62 | |
| 63 | DataType index_type = output_type(0); |
| 64 | xla::PrimitiveType index_xla_type; |
| 65 | OP_REQUIRES_OK(ctx, DataTypeToPrimitiveType(index_type, &index_xla_type)); |
| 66 | |
| 67 | xla::XlaOp input = ctx->Input(0); |
| 68 | xla::XlaOp output; |
| 69 | // One pass ArgMin/ArgMax is slow on GPUs. |
| 70 | if (is_min_) { |
| 71 | if (is_gpu_) { |
| 72 | output = xla::ArgMinTwoPass(input, index_xla_type, axis); |
| 73 | } else { |
| 74 | output = xla::ArgMin(input, index_xla_type, axis); |
| 75 | } |
| 76 | } else { |
| 77 | if (is_gpu_) { |
| 78 | output = xla::ArgMaxTwoPass(input, index_xla_type, axis); |
| 79 | } else { |
| 80 | output = xla::ArgMax(input, index_xla_type, axis); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | ctx->SetOutput(0, output); |
| 85 | } |
| 86 | |
| 87 | XlaArgMaxOp::XlaArgMaxOp(OpKernelConstruction* ctx) |
| 88 | : XlaArgMinMaxOp(ctx, /*is_min=*/false) {} |
nothing calls this directly
no test coverage detected