add error message for RandomData

This commit is contained in:
Xiao Tianci 2022-07-15 17:10:16 +08:00
parent e7d7354ba8
commit 65a0a6c315
2 changed files with 37 additions and 2 deletions

View File

@ -137,8 +137,14 @@ Status RandomDataOp::CreateRandomRow(TensorRow *new_row) {
// Now, create a chunk of memory for the entire tensor and copy this byte in repeatedly.
buf = std::make_unique<unsigned char[]>(size_in_bytes);
int ret_code = memset_s(buf.get(), size_in_bytes, random_byte, size_in_bytes);
if (ret_code != 0) {
RETURN_STATUS_UNEXPECTED("[Internal ERROR] memset_s failed to set random bytes for a tensor.");
if (ret_code != EOK) {
std::string error_msg = "RandomData: failed to set random data, ";
if (ret_code == ERANGE) {
RETURN_STATUS_UNEXPECTED(error_msg + "memory size of total data can not be zero or exceed " +
std::to_string(SECUREC_MEM_MAX_LEN) + ", but got: " + std::to_string(size_in_bytes));
} else {
RETURN_STATUS_UNEXPECTED("memset_s method failed with errno_t: " + std::to_string(ret_code));
}
}
RETURN_IF_NOT_OK(Tensor::CreateFromMemory(*new_shape, current_col.Type(), buf.get(), &new_tensor));

View File

@ -652,3 +652,32 @@ TEST_F(MindDataTestPipeline, TestRandomDatasetFail) {
std::shared_ptr<Dataset> ds = RandomData(3)->SetNumWorkers(5);
EXPECT_EQ(ds->CreateIterator(), nullptr);
}
/// Feature: RandomData
/// Description: Test RandomData failed with large tensor shape
/// Expectation: Expecting error message is logged
TEST_F(MindDataTestPipeline, TestRandomDataLargeShape) {
MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRandomDataLargeShape.";
// Create a RandomDataset
std::shared_ptr<SchemaObj> schema = Schema();
ASSERT_OK(
schema->add_column("data", mindspore::DataType::kNumberTypeFloat32, {1, 12, 5, 3, 4, 5, 6, 7, 8, 5, 3, 1, 5, 6}));
std::shared_ptr<Dataset> ds = RandomData(50, schema);
EXPECT_NE(ds, nullptr);
// Create an iterator over the result of the above dataset
// This will trigger the creation of the Execution Tree and launch it.
std::shared_ptr<Iterator> iter = ds->CreateIterator();
EXPECT_NE(iter, nullptr);
// Iterate the dataset and get each row
std::unordered_map<std::string, mindspore::MSTensor> row;
Status s = iter->GetNextRow(&row);
ASSERT_ERROR(s);
ASSERT_TRUE(s.ToString().find("memory size of total data can not be zero or exceed") != std::string::npos);
ASSERT_TRUE(s.StatusCode() == StatusCode::kMDUnexpectedError);
// Manually terminate the pipeline
iter->Stop();
}