| 1228 | } // namespace |
| 1229 | |
| 1230 | Status MakeRandomTensor(const std::shared_ptr<DataType>& type, |
| 1231 | const std::vector<int64_t>& shape, bool row_major_p, |
| 1232 | std::shared_ptr<Tensor>* out, uint32_t seed) { |
| 1233 | const auto& element_type = arrow::internal::checked_cast<const FixedWidthType&>(*type); |
| 1234 | std::vector<int64_t> strides; |
| 1235 | if (row_major_p) { |
| 1236 | RETURN_NOT_OK(arrow::internal::ComputeRowMajorStrides(element_type, shape, &strides)); |
| 1237 | } else { |
| 1238 | RETURN_NOT_OK( |
| 1239 | arrow::internal::ComputeColumnMajorStrides(element_type, shape, &strides)); |
| 1240 | } |
| 1241 | |
| 1242 | const int64_t element_size = element_type.bit_width() / CHAR_BIT; |
| 1243 | const int64_t len = |
| 1244 | std::accumulate(shape.begin(), shape.end(), int64_t(1), std::multiplies<int64_t>()); |
| 1245 | |
| 1246 | ARROW_ASSIGN_OR_RAISE(std::shared_ptr<Buffer> buf, AllocateBuffer(element_size * len)); |
| 1247 | |
| 1248 | switch (type->id()) { |
| 1249 | case Type::INT8: |
| 1250 | FillRandomData<int8_t, uint32_t, std::uniform_int_distribution<int16_t>>( |
| 1251 | reinterpret_cast<int8_t*>(buf->mutable_data()), len, -128, 127, seed); |
| 1252 | break; |
| 1253 | case Type::UINT8: |
| 1254 | FillRandomData<uint8_t, uint32_t, std::uniform_int_distribution<uint16_t>>( |
| 1255 | reinterpret_cast<uint8_t*>(buf->mutable_data()), len, 0, 255, seed); |
| 1256 | break; |
| 1257 | case Type::INT16: |
| 1258 | FillRandomData(reinterpret_cast<int16_t*>(buf->mutable_data()), len, seed); |
| 1259 | break; |
| 1260 | case Type::UINT16: |
| 1261 | FillRandomData(reinterpret_cast<uint16_t*>(buf->mutable_data()), len, seed); |
| 1262 | break; |
| 1263 | case Type::INT32: |
| 1264 | FillRandomData(reinterpret_cast<int32_t*>(buf->mutable_data()), len, seed); |
| 1265 | break; |
| 1266 | case Type::UINT32: |
| 1267 | FillRandomData(reinterpret_cast<uint32_t*>(buf->mutable_data()), len, seed); |
| 1268 | break; |
| 1269 | case Type::INT64: |
| 1270 | FillRandomData(reinterpret_cast<int64_t*>(buf->mutable_data()), len, seed); |
| 1271 | break; |
| 1272 | case Type::UINT64: |
| 1273 | FillRandomData(reinterpret_cast<uint64_t*>(buf->mutable_data()), len, seed); |
| 1274 | break; |
| 1275 | case Type::HALF_FLOAT: |
| 1276 | FillRandomData(reinterpret_cast<int16_t*>(buf->mutable_data()), len, seed); |
| 1277 | break; |
| 1278 | case Type::FLOAT: |
| 1279 | FillRandomData(reinterpret_cast<float*>(buf->mutable_data()), len, seed); |
| 1280 | break; |
| 1281 | case Type::DOUBLE: |
| 1282 | FillRandomData(reinterpret_cast<double*>(buf->mutable_data()), len, seed); |
| 1283 | break; |
| 1284 | default: |
| 1285 | return Status::Invalid(type->ToString(), " is not valid data type for a tensor"); |
| 1286 | } |
| 1287 |
no test coverage detected