2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 namespace Tizen.MachineLearning.Inference
24 /// Possible data element types of Tensor in NNStreamer.
26 /// <since_tizen> 6 </since_tizen>
27 public enum TensorType
29 #pragma warning disable CA1720 // Identifier contains type name
35 /// Unsigned integer 32bit
43 /// Unsigned integer 16bit
51 /// Unsigned integer 8bit
67 /// Unsigned integer 64bit
70 #pragma warning restore CA1720 // Identifier contains type name
73 internal enum NNStreamerError
75 None = Tizen.Internals.Errors.ErrorCode.None,
76 InvalidParameter = Tizen.Internals.Errors.ErrorCode.InvalidParameter,
77 StreamsPipe = Tizen.Internals.Errors.ErrorCode.StreamsPipe,
78 TryAgain = Tizen.Internals.Errors.ErrorCode.TryAgain,
79 Unknown = Tizen.Internals.Errors.ErrorCode.Unknown,
80 TimedOut = Tizen.Internals.Errors.ErrorCode.TimedOut,
81 NotSupported = Tizen.Internals.Errors.ErrorCode.NotSupported,
82 PermissionDenied = Tizen.Internals.Errors.ErrorCode.PermissionDenied,
83 OutOfMemory = Tizen.Internals.Errors.ErrorCode.OutOfMemory,
84 QuotaExceeded = Tizen.Internals.Errors.ErrorCode.QuotaExceeded,
85 InvalidOperation = Tizen.Internals.Errors.ErrorCode.InvalidOperation,
88 internal enum SwitchType
94 internal enum PipelineBufferPolicy
101 /// States of NNStreamer pipeline.
103 /// <since_tizen> 8 </since_tizen>
104 public enum PipelineState
107 /// Initial state of the pipeline.
111 /// The pipeline is ready to go to PAUSED.
115 /// The pipeline is stopped, ready to accept and process data.
119 /// The pipeline is started and the data is flowing.
125 /// Types of Neural Network Framework.
127 /// <since_tizen> 6 </since_tizen>
131 /// NNHW is not specified (Try to determine the NNFW with file extension).
135 /// Custom filter (Independent shared object).
139 /// Tensorflow-lite (.tflite).
143 /// Tensorflow (.pb).
147 /// Neural Network Inference framework, which is developed by SR (Samsung Research).
151 /// Intel Movidius Neural Compute SDK (libmvnc).
153 /// <since_tizen> 8 </since_tizen>
158 /// <since_tizen> 8 </since_tizen>
161 /// VeriSilicon's Vivante.
163 /// <since_tizen> 8 </since_tizen>
166 /// Google Coral Edge TPU (USB).
168 /// <since_tizen> 8 </since_tizen>
171 /// Arm Neural Network framework (support for caffe and tensorflow-lite).
173 /// <since_tizen> 8 </since_tizen>
176 /// Qualcomm SNPE (Snapdgragon Neural Processing Engine (.dlc)
178 /// <since_tizen> 9 </since_tizen>
183 /// <since_tizen> 9 </since_tizen>
186 /// Inference supported from NNTrainer, SR On-device Training Framework
188 /// <since_tizen> 9 </since_tizen>
189 NNTrainerInferenceFW = 12,
191 /// Inference framework for Samsung Tizen TV
193 /// <since_tizen> 9 </since_tizen>
196 /// TriXEngine accesses TRIV/TRIA NPU low-level drivers directly (.tvn). You may need to use high-level drivers wrapping this low-level driver in some devices: e.g., AIFW
198 /// <since_tizen> 9 </since_tizen>
203 /// Types of hardware resources to be used for NNFWs. Note that if the affinity (nnn) is not supported by the driver or hardware, it is ignored.
205 /// <since_tizen> 6 </since_tizen>
209 /// Hardware resource is not specified.
213 /// Try to schedule and optimize if possible.
217 /// Any CPU if possible.
221 /// SIMD in CPU if possible.
222 /// <since_tizen> 8 </since_tizen>
228 /// <since_tizen> 8 </since_tizen>
231 /// Any GPU if possible.
235 /// Any NPU if possible.
239 /// Intel Movidius Stick.
241 /// <since_tizen> 8 </since_tizen>
242 NPUMovidius = 0x3001,
244 /// Google Coral Edge TPU (USB).
246 /// <since_tizen> 8 </since_tizen>
249 /// VeriSilicon's Vivante.
251 /// <since_tizen> 8 </since_tizen>
254 /// Samsung S.LSI NPU
256 /// <since_tizen> 9 </since_tizen>
259 /// Any SR (Samsung Research) made NPU.
261 /// <since_tizen> 8 </since_tizen>
265 internal static class Tensor
268 /// The maximum rank that NNStreamer supports with Tizen APIs.
270 internal const int RankLimit = 4;
273 /// The maximum number of other/tensor instances that other/tensors may have.
275 internal const int SizeLimit = 16;
278 /// Unknown Type of Tensor information. It is internally used for error check.
280 internal const int UnknownType = 10;
283 /// Invalid count of TensorsData. It is internally used for error check.
285 internal const int InvalidCount = -1;
288 internal static class NNStreamer
290 internal const string TAG = "ML.Inference";
292 internal const string FeatureKey = "http://tizen.org/feature/machine_learning.inference";
294 private static int _alreadyChecked = -1; /* -1: not yet, 0: Not Support, 1: Support */
296 internal static void CheckException(NNStreamerError error, string msg)
298 if (error != NNStreamerError.None)
300 Log.Error(NNStreamer.TAG, msg + ": " + error.ToString());
301 throw NNStreamerExceptionFactory.CreateException(error, msg);
305 internal static void CheckNNStreamerSupport()
307 if (_alreadyChecked == 1)
310 string msg = "Machine Learning Inference Feature is not supported.";
311 if (_alreadyChecked == 0)
313 Log.Error(NNStreamer.TAG, msg);
314 throw NNStreamerExceptionFactory.CreateException(NNStreamerError.NotSupported, msg);
317 /* Feature Key check */
318 bool isSupported = false;
319 bool error = Information.TryGetValue<bool>(FeatureKey, out isSupported);
320 if (!error || !isSupported)
324 Log.Error(NNStreamer.TAG, msg);
325 throw NNStreamerExceptionFactory.CreateException(NNStreamerError.NotSupported, msg);
328 /* Check required so files */
331 Interop.Util.CheckNNFWAvailability(NNFWType.TensorflowLite, HWType.CPU, out isSupported);
333 catch (DllNotFoundException)
336 Log.Error(NNStreamer.TAG, msg);
337 throw NNStreamerExceptionFactory.CreateException(NNStreamerError.NotSupported, msg);
344 internal class NNStreamerExceptionFactory
346 internal static Exception CreateException(NNStreamerError err, string msg)
352 case NNStreamerError.InvalidParameter:
353 exp = new ArgumentException(msg);
356 case NNStreamerError.NotSupported:
357 exp = new NotSupportedException(msg);
360 case NNStreamerError.PermissionDenied:
361 exp = new UnauthorizedAccessException(msg);
364 case NNStreamerError.StreamsPipe:
365 case NNStreamerError.TryAgain:
366 exp = new InvalidOperationException(msg);
369 case NNStreamerError.TimedOut:
370 exp = new TimeoutException(msg);
373 case NNStreamerError.QuotaExceeded:
374 exp = new IndexOutOfRangeException(msg);
378 exp = new InvalidOperationException(msg);