Similar to MakeFakeLiteral but takes a random number generator engine to enable reusing the engine across randomly generated literals. 'no_duplicates' indicates that there should be no duplicate values in each generated array. This is uniqueness is best-effort only. Some types (half and bfloat16) are not supported and uniqueness cannot be guaranteed if the number of elements exceeds the number of
| 262 | // are not supported and uniqueness cannot be guaranteed if the number of |
| 263 | // elements exceeds the number of different values supported by the type. |
| 264 | StatusOr<Literal> MakeFakeLiteralInternal(const Shape& shape, |
| 265 | std::minstd_rand0* engine, |
| 266 | bool no_duplicates, |
| 267 | bool use_large_range) { |
| 268 | if (shape.IsTuple()) { |
| 269 | std::vector<Literal> elements; |
| 270 | for (const Shape& element_shape : shape.tuple_shapes()) { |
| 271 | TF_ASSIGN_OR_RETURN(Literal element, MakeFakeLiteralInternal( |
| 272 | element_shape, engine, |
| 273 | no_duplicates, use_large_range)); |
| 274 | elements.push_back(std::move(element)); |
| 275 | } |
| 276 | return LiteralUtil::MakeTupleOwned(std::move(elements)); |
| 277 | } |
| 278 | if (engine == nullptr) { |
| 279 | return Literal::CreateFromShape(shape); |
| 280 | } |
| 281 | // Clear tiles/element size in shape's layout before using it for creating |
| 282 | // literal. |
| 283 | Shape new_shape = shape; |
| 284 | new_shape.mutable_layout()->clear_tiles(); |
| 285 | new_shape.mutable_layout()->set_element_size_in_bits(0); |
| 286 | Literal literal(new_shape); |
| 287 | switch (shape.element_type()) { |
| 288 | case BF16: |
| 289 | PopulateWithFloatingPointData<bfloat16>(&literal, engine, no_duplicates, |
| 290 | use_large_range); |
| 291 | break; |
| 292 | case F16: |
| 293 | PopulateWithFloatingPointData<half>(&literal, engine, no_duplicates, |
| 294 | use_large_range); |
| 295 | break; |
| 296 | case F32: |
| 297 | PopulateWithFloatingPointData<float>(&literal, engine, no_duplicates, |
| 298 | use_large_range); |
| 299 | break; |
| 300 | case F64: |
| 301 | PopulateWithFloatingPointData<double>(&literal, engine, no_duplicates, |
| 302 | use_large_range); |
| 303 | break; |
| 304 | case S8: |
| 305 | PopulateWithRandomIntegralData<int8>(&literal, engine, no_duplicates); |
| 306 | break; |
| 307 | case U8: |
| 308 | PopulateWithRandomIntegralData<uint8>(&literal, engine, no_duplicates); |
| 309 | break; |
| 310 | case S16: |
| 311 | PopulateWithRandomIntegralData<int16>(&literal, engine, no_duplicates); |
| 312 | break; |
| 313 | case U16: |
| 314 | PopulateWithRandomIntegralData<uint16>(&literal, engine, no_duplicates); |
| 315 | break; |
| 316 | case S32: |
| 317 | PopulateWithRandomIntegralData<int32>(&literal, engine, no_duplicates); |
| 318 | break; |
| 319 | case U32: |
| 320 | PopulateWithRandomIntegralData<uint32>(&literal, engine, no_duplicates); |
| 321 | break; |
no test coverage detected