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

Function SetRandomInputs

samples/cpp/others/pplnn_multi_numa_node.cc:261–305  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

259}
260
261static bool SetRandomInputs(const vector<vector<int64_t>>& input_shapes, Runtime* runtime, vector<string>* input_data) {
262 for (uint32_t c = 0; c < runtime->GetInputCount(); ++c) {
263 auto t = runtime->GetInputTensor(c);
264 auto shape = t->GetShape();
265
266 if (input_shapes.empty()) {
267 auto dim_count = shape->GetRealDimCount();
268 auto dims = GenerateRandomDims(dim_count);
269 for (uint32_t j = 2; j < dim_count; ++j) {
270 if (shape->GetDim(j) == 1) {
271 shape->SetDim(j, dims[j]);
272 }
273 }
274 } else {
275 shape->Reshape(input_shapes[c]);
276 }
277
278 auto nr_element = shape->CalcBytesIncludingPadding() / sizeof(float);
279 vector<float> buffer(nr_element);
280
281 std::default_random_engine eng;
282 std::uniform_real_distribution<float> dis(-1.0f, 1.0f);
283 for (uint32_t i = 0; i < nr_element; ++i) {
284 buffer[i] = dis(eng);
285 }
286
287 auto status = t->ReallocBuffer();
288 if (status != RC_SUCCESS) {
289 LOG(ERROR) << "ReallocBuffer for tensor[" << t->GetName() << "] failed: " << GetRetCodeStr(status);
290 return false;
291 }
292
293 TensorShape src_desc = *t->GetShape();
294 src_desc.SetDataFormat(DATAFORMAT_NDARRAY);
295 status = t->ConvertFromHost(buffer.data(), src_desc);
296 if (status != RC_SUCCESS) {
297 LOG(ERROR) << "set tensor[" << t->GetName() << "] content failed: " << GetRetCodeStr(status);
298 return false;
299 }
300
301 input_data->emplace_back(string((const char*)buffer.data(), buffer.size() * sizeof(float)));
302 }
303
304 return true;
305}
306
307static bool SetInputsAllInOne(const string& input_file, const vector<vector<int64_t>>& input_shapes, Runtime* runtime,
308 vector<string>* input_data) {

Callers 1

RunOnNumaNodeFunction · 0.70

Calls 8

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

Tested by

no test coverage detected