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

Function SetRandomInputs

tools/pplnn.py:431–471  ·  view source on GitHub ↗
(in_shapes, runtime)

Source from the content-addressed store, hash-verified

429# ---------------------------------------------------------------------------- #
430
431def SetRandomInputs(in_shapes, runtime):
432 def GenerateRandomDims(shape):
433 dims = shape.GetDims()
434 dim_count = len(dims)
435 if dim_count > 0:
436 if dims[0] == pplnn.INVALID_DIM_VALUE:
437 dims[0] = 1
438 for i in range(1, dim_count):
439 if dims[i] == pplnn.INVALID_DIM_VALUE:
440 dims[i] = random.randint(128, 641)
441 if dims[i] % 2 != 0:
442 dims[i] = dims[i] + 1
443 return dims
444
445 rng = np.random.default_rng()
446 for i in range(runtime.GetInputCount()):
447 tensor = runtime.GetInputTensor(i)
448 shape = tensor.GetShape()
449 data_type = shape.GetDataType()
450
451 np_data_type = g_pplnntype2numpytype[data_type]
452 if data_type in (pplcommon.DATATYPE_FLOAT16, pplcommon.DATATYPE_FLOAT32, pplcommon.DATATYPE_FLOAT64):
453 lower_bound = -1.0
454 upper_bound = 1.0
455 else:
456 info = np.iinfo(np_data_type)
457 lower_bound = info.min
458 upper_bound = info.max
459
460 dims = []
461 if in_shapes:
462 dims = in_shapes[i]
463 else:
464 dims = GenerateRandomDims(shape)
465
466 in_data = (upper_bound - lower_bound) * rng.random(dims, dtype = np_data_type) * lower_bound
467 status = tensor.ConvertFromHost(in_data)
468 if status != pplcommon.RC_SUCCESS:
469 logging.error("copy data to tensor[" + tensor.GetName() + "] failed: " +
470 pplcommon.GetRetCodeStr(status))
471 sys.exit(-1)
472
473# ---------------------------------------------------------------------------- #
474

Callers 1

pplnn.pyFile · 0.70

Calls 6

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

Tested by

no test coverage detected