| 43 | } |
| 44 | |
| 45 | void Compile(XlaOpKernelContext* ctx) override { |
| 46 | const int num = num_outputs(); |
| 47 | const TensorShape input_shape = ctx->InputShape(0); |
| 48 | |
| 49 | int axis = axis_; |
| 50 | if (axis < 0) axis += input_shape.dims(); |
| 51 | |
| 52 | OP_REQUIRES(ctx, 0 <= axis && axis < input_shape.dims(), |
| 53 | errors::InvalidArgument("axis = ", axis_, " not in [", |
| 54 | -input_shape.dims(), ", ", |
| 55 | input_shape.dims(), ")")); |
| 56 | |
| 57 | OP_REQUIRES( |
| 58 | ctx, input_shape.dims() > 0 && input_shape.dim_size(axis) == num, |
| 59 | errors::InvalidArgument("Input shape axis ", axis, " must equal ", num, |
| 60 | ", got shape ", input_shape.DebugString())); |
| 61 | |
| 62 | auto output_shape = input_shape; |
| 63 | output_shape.RemoveDim(axis); |
| 64 | |
| 65 | auto input = ctx->Input(0); |
| 66 | |
| 67 | std::vector<int64> start_indices(input_shape.dims(), 0); |
| 68 | std::vector<int64> limit_indices(input_shape.dims()); |
| 69 | std::vector<int64> strides(input_shape.dims(), 1); |
| 70 | for (int i = 0; i < input_shape.dims(); ++i) { |
| 71 | limit_indices[i] = input_shape.dim_size(i); |
| 72 | } |
| 73 | |
| 74 | for (int i = 0; i < num; ++i) { |
| 75 | start_indices[axis] = i; |
| 76 | limit_indices[axis] = i + 1; |
| 77 | auto slice = xla::Slice(input, start_indices, limit_indices, strides); |
| 78 | // Reshape to drop the 'axis' dimension. |
| 79 | auto result = xla::Reshape(slice, output_shape.dim_sizes()); |
| 80 | ctx->SetOutput(i, result); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | int axis_; |
nothing calls this directly
no test coverage detected