Ejemplo n.º 1
0
    /// <summary>
    /// Query a DirectML device for its support for a particular data type within tensors
    /// </summary>
    /// <param name="dataType">The data type about which you're querying for support.</param>
    /// <returns>TRUE if the tensor data type is supported within tensors by the DirectML device. Otherwise, FALSE.</returns>
    public unsafe bool CheckTensorDataTypeSupport(TensorDataType dataType)
    {
        var query = new FeatureQueryTensorDataTypeSupport()
        {
            DataType = dataType
        };
        var data = new FeatureDataTensorDataTypeSupport();

        CheckFeatureSupport(Feature.TensorDataTypeSupport, sizeof(FeatureQueryTensorDataTypeSupport), new(&query), sizeof(FeatureDataTensorDataTypeSupport), new(&data));

        return(data.IsSupported);
    }
    /// <summary>
    /// Calculates the minimum implied tensor size in bytes given the data type, sizes, and
    /// strides.
    /// </summary>
    /// <param name="dataType"></param>
    /// <param name="sizes"></param>
    /// <param name="strides"></param>
    /// <returns></returns>
    /// <remarks>
    /// Based on the DirectMLX.h DMLCalcBufferTensorSize function. See
    /// <see href="https://github.com/microsoft/DirectML/blob/master/Libraries/DirectMLX.h"/>.
    /// </remarks>
    public static long CalculateMinimumImpliedSize(
        TensorDataType dataType,
        int[] sizes,
        int[]?strides = null)
    {
        var elementSizeInBytes = dataType switch
        {
            TensorDataType.Uint64 or TensorDataType.Int64 or TensorDataType.Float64 => 8,
            TensorDataType.Uint32 or TensorDataType.Int32 or TensorDataType.Float32 => 4,
            TensorDataType.Uint16 or TensorDataType.Int16 or TensorDataType.Float16 => 2,
            TensorDataType.Uint8 or TensorDataType.Int8 => 1,
            _ => 0
        };

        long minimumImpliedSizeInBytes;

        if (strides == null)
        {
            minimumImpliedSizeInBytes = 1;
            for (var i = 0; i < sizes.Length; i++)
            {
                minimumImpliedSizeInBytes *= sizes[i];
            }
            minimumImpliedSizeInBytes *= elementSizeInBytes;
        }
        else
        {
            var indexOfLastElement = 0;
            for (var i = 0; i < sizes.Length; i++)
            {
                indexOfLastElement += (sizes[i] - 1) * strides[i];
            }
            minimumImpliedSizeInBytes = (indexOfLastElement + 1) * elementSizeInBytes;
        }

        // Round up to the nearest 4 bytes.
        return(minimumImpliedSizeInBytes + 3 & ~3L);
    }
Ejemplo n.º 3
0
    public static long CalculateBufferTensorSize(
        TensorDataType dataType,
        int[] sizes,
        int[]?strides)
    {
        long elementSizeInBytes = 0;

        switch (dataType)
        {
        case TensorDataType.Float32:
        case TensorDataType.Uint32:
        case TensorDataType.Int32:
            elementSizeInBytes = 4;
            break;

        case TensorDataType.Float16:
        case TensorDataType.Uint16:
        case TensorDataType.Int16:
            elementSizeInBytes = 2;
            break;

        case TensorDataType.Uint8:
        case TensorDataType.Int8:
            elementSizeInBytes = 1;
            break;

        //case DML_TENSOR_DATA_TYPE_FLOAT64:
        //case DML_TENSOR_DATA_TYPE_UINT64:
        //case DML_TENSOR_DATA_TYPE_INT64:
        //    elementSizeInBytes = 8;
        //    break;

        default:
            return(0);    // Invalid data type
        }

        long minimumImpliedSizeInBytes;

        if (strides == null)
        {
            minimumImpliedSizeInBytes = 1;
            for (int i = 0; i < sizes.Length; i++)
            {
                minimumImpliedSizeInBytes *= sizes[i];
            }
            minimumImpliedSizeInBytes *= elementSizeInBytes;
        }
        else
        {
            int indexOfLastElement = 0;
            for (int i = 0; i < sizes.Length; i++)
            {
                indexOfLastElement += (sizes[i] - 1) * strides[i];
            }
            minimumImpliedSizeInBytes = (indexOfLastElement + 1) * elementSizeInBytes;
        }

        // Round up to the nearest 4 bytes.
        minimumImpliedSizeInBytes = (minimumImpliedSizeInBytes + 3) & ~3L;

        return(minimumImpliedSizeInBytes);
    }
 /// <summary>
 /// Calculates the minimum implied tensor size in bytes given the data type, sizes, and
 /// strides.
 /// </summary>
 /// <param name="dataType"></param>
 /// <param name="sizes"></param>
 /// <returns></returns>
 /// <remarks>
 /// Based on the DirectMLX.h DMLCalcBufferTensorSize function. See
 /// <see href="https://github.com/microsoft/DirectML/blob/master/Libraries/DirectMLX.h"/>.
 /// </remarks>
 public static long CalculateMinimumImpliedSize(
     TensorDataType dataType,
     params int[] sizes)
 {
     return(CalculateMinimumImpliedSize(dataType, sizes, null));
 }