| 43 | } |
| 44 | |
| 45 | void Compile(XlaOpKernelContext* ctx) override { |
| 46 | std::vector<xla::XlaOp> values; |
| 47 | std::vector<TensorShape> shapes; |
| 48 | OP_REQUIRES_OK(ctx, ctx->InputList("values", &values, &shapes)); |
| 49 | const int num = values.size(); |
| 50 | |
| 51 | OP_REQUIRES(ctx, num >= 0, |
| 52 | errors::InvalidArgument("Pack requires >= 1 arguments")); |
| 53 | |
| 54 | // Verify that all input shapes match |
| 55 | for (int i = 1; i < num; i++) { |
| 56 | OP_REQUIRES(ctx, shapes[0].IsSameSize(shapes[i]), |
| 57 | errors::InvalidArgument( |
| 58 | "Shapes of all inputs must match: values[0].shape = ", |
| 59 | shapes[0].DebugString(), " != values[", i, "].shape = ", |
| 60 | shapes[i].DebugString())); |
| 61 | } |
| 62 | |
| 63 | int expanded_num_dims = shapes[0].dims() + 1; |
| 64 | int axis = axis_; |
| 65 | if (axis < 0) axis += expanded_num_dims; |
| 66 | |
| 67 | OP_REQUIRES(ctx, 0 <= axis && axis < expanded_num_dims, |
| 68 | errors::InvalidArgument("axis = ", axis_, " not in [", |
| 69 | -expanded_num_dims, ", ", |
| 70 | expanded_num_dims, ")")); |
| 71 | |
| 72 | std::vector<xla::XlaOp> reshaped_inputs(num); |
| 73 | |
| 74 | TensorShape child_shape(shapes[0]); |
| 75 | child_shape.InsertDim(axis, 1); |
| 76 | |
| 77 | for (int i = 0; i < num; ++i) { |
| 78 | // Reshape the inputs to have an extra dimension of size 1. |
| 79 | reshaped_inputs[i] = xla::Reshape(values[i], child_shape.dim_sizes()); |
| 80 | } |
| 81 | |
| 82 | ctx->SetOutput(0, xla::ConcatInDim(ctx->builder(), reshaped_inputs, axis)); |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | int axis_; |
nothing calls this directly
no test coverage detected