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

Function SetRandomInputs

samples/cpp/api/api_intro.cc:27–55  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

25using namespace ppl::common;
26
27static bool SetRandomInputs(Runtime* runtime) {
28 for (uint32_t c = 0; c < runtime->GetInputCount(); ++c) {
29 auto t = runtime->GetInputTensor(c);
30 auto& shape = *t->GetShape();
31
32 auto nr_element = shape.CalcBytesIncludingPadding() / sizeof(float);
33 vector<float> buffer(nr_element);
34
35 // fill random input data
36 std::default_random_engine eng;
37 std::uniform_real_distribution<float> dis(-1.0f, 1.0f);
38 for (uint32_t i = 0; i < nr_element; ++i) {
39 buffer[i] = dis(eng);
40 }
41
42 // our random data is treated as NDARRAY
43 ppl::nn::TensorShape src_desc = *t->GetShape();
44 src_desc.SetDataFormat(DATAFORMAT_NDARRAY);
45
46 // input tensors may require different data format
47 auto status = t->ConvertFromHost(buffer.data(), src_desc);
48 if (status != RC_SUCCESS) {
49 cerr << "set tensor[" << t->GetName() << "] content failed: " << GetRetCodeStr(status) << endl;
50 return false;
51 }
52 }
53
54 return true;
55}
56
57static void PrintInputOutputInfo(const Runtime* runtime) {
58 cout << "----- input info -----" << endl;

Callers 1

mainFunction · 0.70

Calls 5

GetInputTensorMethod · 0.80
GetInputCountMethod · 0.45
GetShapeMethod · 0.45
ConvertFromHostMethod · 0.45
GetNameMethod · 0.45

Tested by

no test coverage detected