| 1199 | } // namespace |
| 1200 | |
| 1201 | Status MakeRandomTensor(const std::shared_ptr<DataType>& type, |
| 1202 | const std::vector<int64_t>& shape, bool row_major_p, |
| 1203 | std::shared_ptr<Tensor>* out, uint32_t seed) { |
| 1204 | const auto& element_type = arrow::internal::checked_cast<const FixedWidthType&>(*type); |
| 1205 | std::vector<int64_t> strides; |
| 1206 | if (row_major_p) { |
| 1207 | RETURN_NOT_OK(arrow::internal::ComputeRowMajorStrides(element_type, shape, &strides)); |
| 1208 | } else { |
| 1209 | RETURN_NOT_OK( |
| 1210 | arrow::internal::ComputeColumnMajorStrides(element_type, shape, &strides)); |
| 1211 | } |
| 1212 | |
| 1213 | const int64_t element_size = element_type.bit_width() / CHAR_BIT; |
| 1214 | const int64_t len = |
| 1215 | std::accumulate(shape.begin(), shape.end(), int64_t(1), std::multiplies<int64_t>()); |
| 1216 | |
| 1217 | ARROW_ASSIGN_OR_RAISE(std::shared_ptr<Buffer> buf, AllocateBuffer(element_size * len)); |
| 1218 | |
| 1219 | switch (type->id()) { |
| 1220 | case Type::INT8: |
| 1221 | FillRandomData<int8_t, uint32_t, std::uniform_int_distribution<int16_t>>( |
| 1222 | reinterpret_cast<int8_t*>(buf->mutable_data()), len, -128, 127, seed); |
| 1223 | break; |
| 1224 | case Type::UINT8: |
| 1225 | FillRandomData<uint8_t, uint32_t, std::uniform_int_distribution<uint16_t>>( |
| 1226 | reinterpret_cast<uint8_t*>(buf->mutable_data()), len, 0, 255, seed); |
| 1227 | break; |
| 1228 | case Type::INT16: |
| 1229 | FillRandomData(reinterpret_cast<int16_t*>(buf->mutable_data()), len, seed); |
| 1230 | break; |
| 1231 | case Type::UINT16: |
| 1232 | FillRandomData(reinterpret_cast<uint16_t*>(buf->mutable_data()), len, seed); |
| 1233 | break; |
| 1234 | case Type::INT32: |
| 1235 | FillRandomData(reinterpret_cast<int32_t*>(buf->mutable_data()), len, seed); |
| 1236 | break; |
| 1237 | case Type::UINT32: |
| 1238 | FillRandomData(reinterpret_cast<uint32_t*>(buf->mutable_data()), len, seed); |
| 1239 | break; |
| 1240 | case Type::INT64: |
| 1241 | FillRandomData(reinterpret_cast<int64_t*>(buf->mutable_data()), len, seed); |
| 1242 | break; |
| 1243 | case Type::UINT64: |
| 1244 | FillRandomData(reinterpret_cast<uint64_t*>(buf->mutable_data()), len, seed); |
| 1245 | break; |
| 1246 | case Type::HALF_FLOAT: |
| 1247 | FillRandomData(reinterpret_cast<int16_t*>(buf->mutable_data()), len, seed); |
| 1248 | break; |
| 1249 | case Type::FLOAT: |
| 1250 | FillRandomData(reinterpret_cast<float*>(buf->mutable_data()), len, seed); |
| 1251 | break; |
| 1252 | case Type::DOUBLE: |
| 1253 | FillRandomData(reinterpret_cast<double*>(buf->mutable_data()), len, seed); |
| 1254 | break; |
| 1255 | default: |
| 1256 | return Status::Invalid(type->ToString(), " is not valid data type for a tensor"); |
| 1257 | } |
| 1258 |
no test coverage detected