From: Yelin Jeong Date: Fri, 28 Jul 2023 01:55:21 +0000 (+0900) Subject: [MachineLearning.Inference] Change internal rank limit and default value X-Git-Tag: accepted/tizen/unified/20231205.024657~193 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=41d8eeca0422891f996854266fcda16f5cba3947;p=platform%2Fcore%2Fcsapi%2Ftizenfx.git [MachineLearning.Inference] Change internal rank limit and default value This patch changes internal rank limit in MachineLeanring.Inference. To use higher rank limit, ml_tensors_info_create_extended is needed. Also, default dimension value is changed to 0 from 1. Signed-off-by: Yelin Jeong --- diff --git a/src/Tizen.MachineLearning.Inference/Interop/Interop.Nnstreamer.cs b/src/Tizen.MachineLearning.Inference/Interop/Interop.Nnstreamer.cs index 1d6d5df..b8c0c4e 100755 --- a/src/Tizen.MachineLearning.Inference/Interop/Interop.Nnstreamer.cs +++ b/src/Tizen.MachineLearning.Inference/Interop/Interop.Nnstreamer.cs @@ -235,6 +235,10 @@ internal static partial class Interop [DllImport(Libraries.MlCommon, EntryPoint = "ml_tensors_info_create", CallingConvention = CallingConvention.Cdecl)] internal static extern NNStreamerError CreateTensorsInfo(out IntPtr info); + /* int ml_tensors_info_create_extended (ml_tensors_info_h *info) */ + [DllImport(Libraries.MlCommon, EntryPoint = "ml_tensors_info_create_extended", CallingConvention = CallingConvention.Cdecl)] + internal static extern NNStreamerError CreateTensorsInfoExtended(out IntPtr info); + /* int ml_tensors_info_destroy (ml_tensors_info_h info) */ [DllImport(Libraries.MlCommon, EntryPoint = "ml_tensors_info_destroy", CallingConvention = CallingConvention.Cdecl)] internal static extern NNStreamerError DestroyTensorsInfo(IntPtr info); diff --git a/src/Tizen.MachineLearning.Inference/Tizen.MachineLearning.Inference/Commons.cs b/src/Tizen.MachineLearning.Inference/Tizen.MachineLearning.Inference/Commons.cs index fb81b3c..951ca33 100755 --- a/src/Tizen.MachineLearning.Inference/Tizen.MachineLearning.Inference/Commons.cs +++ b/src/Tizen.MachineLearning.Inference/Tizen.MachineLearning.Inference/Commons.cs @@ -267,7 +267,7 @@ namespace Tizen.MachineLearning.Inference /// /// The maximum rank that NNStreamer supports with Tizen APIs. /// - internal const int RankLimit = 4; + internal const int RankLimit = 16; /// /// The maximum number of other/tensor instances that other/tensors may have. diff --git a/src/Tizen.MachineLearning.Inference/Tizen.MachineLearning.Inference/TensorsInfo.cs b/src/Tizen.MachineLearning.Inference/Tizen.MachineLearning.Inference/TensorsInfo.cs index 2df0d49..094c044 100755 --- a/src/Tizen.MachineLearning.Inference/Tizen.MachineLearning.Inference/TensorsInfo.cs +++ b/src/Tizen.MachineLearning.Inference/Tizen.MachineLearning.Inference/TensorsInfo.cs @@ -95,7 +95,9 @@ namespace Tizen.MachineLearning.Inference if (idx >= Tensor.SizeLimit) { throw new IndexOutOfRangeException("Max size of the tensors is " + Tensor.SizeLimit); } - _infoList.Add(new TensorInfo(name, type, dimension)); + + int[] dim = ConvertDimension(dimension); + _infoList.Add(new TensorInfo(name, type, dim)); if (_handle != IntPtr.Zero) { @@ -104,7 +106,7 @@ namespace Tizen.MachineLearning.Inference ret = Interop.Util.SetTensorsCount(_handle, _infoList.Count); NNStreamer.CheckException(ret, "Failed to set the number of tensors"); - UpdateInfoHandle(_handle, idx, name, type, dimension); + UpdateInfoHandle(_handle, idx, name, type, dim); } } @@ -204,12 +206,15 @@ namespace Tizen.MachineLearning.Inference NNStreamer.CheckNNStreamerSupport(); CheckIndexBoundary(idx); - _infoList[idx].SetDimension(dimension); + + int[] dim = ConvertDimension(dimension); + + _infoList[idx].SetDimension(dim); if (_handle != IntPtr.Zero) { NNStreamerError ret = NNStreamerError.None; - ret = Interop.Util.SetTensorDimension(_handle, idx, dimension); + ret = Interop.Util.SetTensorDimension(_handle, idx, dim); NNStreamer.CheckException(ret, "unable to set the dimension of tensor: " + idx.ToString()); } } @@ -412,7 +417,7 @@ namespace Tizen.MachineLearning.Inference NNStreamer.CheckException(ret, "number of Tensor in TensorsInfo is invalid: " + _infoList.Count); /* Create TensorsInfo object */ - ret = Interop.Util.CreateTensorsInfo(out ret_handle); + ret = Interop.Util.CreateTensorsInfoExtended(out ret_handle); NNStreamer.CheckException(ret, "fail to create TensorsInfo object"); /* Set the number of tensors */ @@ -469,6 +474,29 @@ namespace Tizen.MachineLearning.Inference _disposed = true; } + private static int[] ConvertDimension(int[] dimension) + { + if (dimension == null) { + throw NNStreamerExceptionFactory.CreateException(NNStreamerError.InvalidParameter, "The dimension is null, it should be a valid array."); + } + + if (dimension.Length > Tensor.RankLimit) { + throw new IndexOutOfRangeException("Max rank limit is " + Tensor.RankLimit); + } + + int[] dim = new int[Tensor.RankLimit]; + int i; + + for (i = 0 ; i < dimension.Length ; i++) { + dim[i] = dimension[i]; + } + for (; i < Tensor.RankLimit ; i++) { + dim[i] = 0; + } + + return dim; + } + private void UpdateInfoHandle(IntPtr handle, int idx, string name, TensorType type, int[] dimension) { if (handle != IntPtr.Zero) @@ -513,11 +541,11 @@ namespace Tizen.MachineLearning.Inference public void SetDimension(int[] dimension) { if (dimension == null) { - throw new ArgumentException("Max size of the tensor rank is" + Tensor.RankLimit); + throw new ArgumentException("The dimension is null, it should be a valid array."); } - if (dimension.Length > Tensor.RankLimit) { - throw new ArgumentException("Max size of the tensor rank is" + Tensor.RankLimit); + if (dimension.Length != Tensor.RankLimit) { + throw new ArgumentException("The length of the dimension should be " + Tensor.RankLimit); } Dimension = (int[])dimension.Clone(); } @@ -555,6 +583,8 @@ namespace Tizen.MachineLearning.Inference } for (int i = 0; i < Tensor.RankLimit; ++i) { + if (Dimension[i] == 0) + break; size *= Dimension[i]; } return size; diff --git a/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Model.cs b/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Model.cs index 08672eb..d92636e 100644 --- a/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Model.cs +++ b/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Model.cs @@ -339,7 +339,7 @@ namespace Tizen.MachineLearning.Train internal static TensorsInfo CreateTensorsInfoFormHandle(IntPtr handle) { - const int RankLimit = 4; + const int RankLimit = 16; NNTrainerError ret = NNTrainerError.None; int count;