expr.const函数,用于根据指定的值创建一个Constant类。
    注意,value可以是bool, int, float, numpy.ndarray, tvm.nd.NDArray
    python/tvm/relay/expr.py

    1. def const(value, dtype=None):
    2. """Create a constant value.
    3. Parameters
    4. ----------
    5. value: Union[bool, int, float, numpy.ndarray, tvm.nd.NDArray]
    6. The constant value.
    7. dtype: str, optional
    8. The data type of the value.
    9. Note
    10. ----
    11. When dtype is None, we use the following rule:
    12. - int maps to "int32"
    13. - float maps to "float32"
    14. - bool maps to "bool"
    15. - other using the same default rule as numpy.
    16. """
    17. if isinstance(value, (_base.numeric_types, (bool, list))):
    18. value = _np.array(value, dtype=dtype)
    19. if not dtype:
    20. # when dtype is None: int maps to "int32", float maps to "float32"
    21. map_dtype = {
    22. _np.dtype('int64'): _np.int32,
    23. _np.dtype('float64'): _np.float32
    24. }.get(value.dtype, None)
    25. if map_dtype:
    26. value = value.astype(map_dtype)
    27. if isinstance(value, (_np.ndarray, _np.generic)):
    28. value = _nd.array(value)
    29. if not isinstance(value, _nd.NDArray):
    30. raise ValueError("value has to be scalar or NDArray")
    31. return Constant(value)

    该函数有一个bug, 如果value是_np.ndarray 或_np.generic类型,提供的dtype是无效的。如果要指定dtype,需要先将value.astype(dtype)。