MCPcopy Create free account
hub / github.com/OpenPPL/ppl.nn / SetRandomInputs

Function SetRandomInputs

tools/pplnn.cc:549–595  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

547}
548
549static bool SetRandomInputs(const vector<vector<int64_t>>& input_shapes, Runtime* runtime, vector<string>* input_data) {
550 for (uint32_t c = 0; c < runtime->GetInputCount(); ++c) {
551 auto t = runtime->GetInputTensor(c);
552 auto shape = t->GetShape();
553
554 if (input_shapes.empty()) {
555 auto dim_count = shape->GetRealDimCount();
556 if (dim_count == 0) {
557 continue;
558 }
559
560 auto dims = GenerateRandomDims(dim_count);
561
562 if (shape->GetDim(0) == INVALID_DIM_VALUE) {
563 shape->SetDim(0, 1);
564 }
565 for (uint32_t j = 1; j < dim_count; ++j) {
566 if (shape->GetDim(j) == INVALID_DIM_VALUE) {
567 shape->SetDim(j, dims[j]);
568 }
569 }
570 } else {
571 shape->Reshape(input_shapes[c]);
572 }
573
574 auto nr_element = shape->CalcBytesIncludingPadding() / sizeof(float);
575 vector<float> buffer(nr_element);
576
577 std::default_random_engine eng;
578 std::uniform_real_distribution<float> dis(-1.0f, 1.0f);
579 for (uint32_t i = 0; i < nr_element; ++i) {
580 buffer[i] = dis(eng);
581 }
582
583 ppl::nn::TensorShape src_desc = *t->GetShape();
584 src_desc.SetDataFormat(DATAFORMAT_NDARRAY);
585 auto status = t->ConvertFromHost(buffer.data(), src_desc);
586 if (status != RC_SUCCESS) {
587 LOG(ERROR) << "set tensor[" << t->GetName() << "] content failed: " << GetRetCodeStr(status);
588 return false;
589 }
590
591 input_data->emplace_back(string((const char*)buffer.data(), buffer.size() * sizeof(float)));
592 }
593
594 return true;
595}
596
597static string GetBasename(const string& path) {
598 string last_entry;

Callers 1

mainFunction · 0.70

Calls 7

GetInputTensorMethod · 0.80
GenerateRandomDimsFunction · 0.70
GetInputCountMethod · 0.45
GetShapeMethod · 0.45
ReshapeMethod · 0.45
ConvertFromHostMethod · 0.45
GetNameMethod · 0.45

Tested by

no test coverage detected