Runs an R2 => R1 reduction test with the given number of (rows, cols).
| 228 | |
| 229 | // Runs an R2 => R1 reduction test with the given number of (rows, cols). |
| 230 | void RunR2ToR1Test(int64 rows, int64 cols, int64 minor = 1, int64 major = 0) { |
| 231 | XlaBuilder builder(TestName()); |
| 232 | XlaComputation add_f32 = CreateScalarAddComputation(F32, &builder); |
| 233 | const Shape input_shape = ShapeUtil::MakeShape(F32, {rows, cols}); |
| 234 | auto input = Parameter(&builder, 0, input_shape, "input"); |
| 235 | auto zero = ConstantR0<float>(&builder, 0.0); |
| 236 | Reduce(input, zero, add_f32, /*dimensions_to_reduce=*/{0}); |
| 237 | |
| 238 | Array2D<float> input_data(rows, cols); |
| 239 | input_data.FillRandom(3.14f, 0.04); |
| 240 | Literal input_literal = LiteralUtil::CreateR2FromArray2D(input_data); |
| 241 | input_literal = |
| 242 | input_literal.Relayout(LayoutUtil::MakeLayout({minor, major})); |
| 243 | std::unique_ptr<GlobalData> input_global_data = |
| 244 | client_->TransferToServer(input_literal).ConsumeValueOrDie(); |
| 245 | |
| 246 | std::vector<float> expected; |
| 247 | for (int64 colno = 0; colno < cols; ++colno) { |
| 248 | float column_sum = 0; |
| 249 | for (int64 rowno = 0; rowno < rows; ++rowno) { |
| 250 | column_sum += input_data(rowno, colno); |
| 251 | } |
| 252 | expected.push_back(column_sum); |
| 253 | } |
| 254 | ComputeAndCompareR1<float>(&builder, expected, {input_global_data.get()}, |
| 255 | ErrorSpec(0.01, 1e-4)); |
| 256 | } |
| 257 | |
| 258 | template <typename NativeT> |
| 259 | void ComputeAndCompareGeneric( |
nothing calls this directly
no test coverage detected