[Ml][Trainer] Add JS stubs for ML Trainer API
[platform/core/api/webapi-plugins.git] / src / ml / js / ml_trainer.js
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 var MachineLearningTrainer = function() {};
18
19 var OptimizerType = {
20     OPTIMIZER_ADAM: 'OPTIMIZER_ADAM',
21     OPTIMIZER_SGD: 'OPTIMIZER_SGD',
22     OPTIMIZER_UNKNOWN: 'OPTIMIZER_UNKNOWN'
23 };
24
25 var DatasetType = {
26     DATASET_GENERATOR: 'DATASET_GENERATOR',
27     DATASET_FILE: 'DATASET_FILE',
28     DATASET_UNKNOWN: 'DATASET_UNKNOWN'
29 };
30
31 var LayerType = {
32     LAYER_IN: 'LAYER_IN',
33     LAYER_FC: 'LAYER_FC',
34     LAYER_BN: 'LAYER_BN',
35     LAYER_CONV2D: 'LAYER_CONV2D',
36     LAYER_POOLING2D: 'LAYER_POOLING2D',
37     LAYER_FLATTEN: 'LAYER_FLATTEN',
38     LAYER_ACTIVATION: 'LAYER_ACTIVATION',
39     LAYER_ADDITION: 'LAYER_ADDITION',
40     LAYER_CONCAT: 'LAYER_CONCAT',
41     LAYER_MULTIOUT: 'LAYER_MULTIOUT',
42     LAYER_LOSS: 'LAYER_LOSS',
43     LAYER_BACKBONE_NNSTREAMER: 'LAYER_BACKBONE_NNSTREAMER',
44     LAYER_BACKBONE_TFLITE: 'LAYER_BACKBONE_TFLITE',
45     LAYER_EMBEDDING: 'LAYER_EMBEDDING',
46     LAYER_RNN: 'LAYER_RNN',
47     LAYER_UNKNOWN: 'LAYER_UNKNOWN'
48 };
49
50 var VerbosityLevel = {
51     SUMMARY_MODEL: 'SUMMARY_MODEL',
52     SUMMARY_LAYER: 'SUMMARY_LAYER',
53     SUMMARY_TENSOR: 'SUMMARY_TENSOR'
54 };
55
56 var Layer = function(id) {
57     Object.defineProperties(this, {
58         name: {
59             enumerable: true,
60             get: function() {
61                 // TODO
62             }
63         },
64         type: {
65             enumerable: true,
66             get: function() {
67                 // TODO
68             }
69         },
70         _id: { value: id, writable: false, enumerable: false }
71     });
72 };
73
74 var ValidSetPropertyExceptions = [
75     'InvalidValuesError',
76     'TypeMismatchError',
77     'AbortError'
78 ];
79
80 Layer.prototype.setProperty = function() {
81     var args = validator_.validateArgs(arguments, [
82         {
83             name: 'name',
84             type: types_.STRING
85         },
86         {
87             name: 'value',
88             type: types_.STRING
89         }
90     ]);
91
92     if (!args.has.name || !args.has.value) {
93         throw new WebAPIException(
94             WebAPIException.TYPE_MISMATCH_ERR,
95             'Invalid parameter: ' + (args.has.name ? 'value' : 'name') + ' is undefined'
96         );
97     }
98
99     var callArgs = {
100         id: this._id,
101         name: args.name,
102         value: args.value
103     };
104
105     var result = native_.callSync('MLTrainerLayerSetProperty', callArgs);
106
107     if (native_.isFailure(result)) {
108         throw native_.getErrorObjectAndValidate(
109             result,
110             ValidSetPropertyExceptions,
111             AbortError
112         );
113     }
114 };
115
116 var Optimizer = function(id) {
117     Object.defineProperties(this, {
118         type: {
119             enumerable: true,
120             get: function() {
121                 // TODO
122             }
123         },
124         _id: { value: id, writable: false, enumerable: false }
125     });
126 };
127
128 Optimizer.prototype.setProperty = function() {
129     var args = validator_.validateArgs(arguments, [
130         {
131             name: 'name',
132             type: types_.STRING
133         },
134         {
135             name: 'value',
136             type: types_.STRING
137         }
138     ]);
139
140     if (!args.has.name || !args.has.value) {
141         throw new WebAPIException(
142             WebAPIException.TYPE_MISMATCH_ERR,
143             'Invalid parameter: ' + (args.has.name ? 'value' : 'name') + ' is undefined'
144         );
145     }
146
147     var callArgs = {
148         id: this._id,
149         name: args.name,
150         value: args.value
151     };
152
153     var result = native_.callSync('MLTrainerOptimizerSetProperty', callArgs);
154
155     if (native_.isFailure(result)) {
156         throw native_.getErrorObjectAndValidate(
157             result,
158             ValidSetPropertyExceptions,
159             AbortError
160         );
161     }
162 };
163
164 var Dataset = function(id) {
165     Object.defineProperties(this, {
166         type: {
167             enumerable: true,
168             get: function() {
169                 // TODO
170             }
171         },
172         _id: { value: id, writable: false, enumerable: false }
173     });
174 };
175
176 Dataset.prototype.setProperty = function() {
177     var args = validator_.validateArgs(arguments, [
178         {
179             name: 'name',
180             type: types_.STRING
181         },
182         {
183             name: 'value',
184             type: types_.STRING
185         }
186     ]);
187
188     if (!args.has.name || !args.has.value) {
189         throw new WebAPIException(
190             WebAPIException.TYPE_MISMATCH_ERR,
191             'Invalid parameter: ' + (args.has.name ? 'value' : 'name') + ' is undefined'
192         );
193     }
194
195     var callArgs = {
196         id: this._id,
197         name: args.name,
198         value: args.value
199     };
200
201     var result = native_.callSync('MLTrainerDatasetSetProperty', callArgs);
202
203     if (native_.isFailure(result)) {
204         throw native_.getErrorObjectAndValidate(
205             result,
206             ValidSetPropertyExceptions,
207             AbortError
208         );
209     }
210 };
211
212 var Model = function(id) {
213     Object.defineProperties(this, {
214         _id: { value: id, writable: false, enumerable: false }
215     });
216 };
217
218 function ValidateCompileOptions(options) {
219     // TODO:
220 }
221
222 Model.prototype.compile = function() {
223     var args = validator.validateArgs(arguments, [
224         {
225             name: 'options',
226             type: validator.Types.DICTIONARY,
227             optional: true,
228             nullable: true
229         }
230     ]);
231     if (args.has.options) {
232         ValidateCompileOptions(args.options);
233     }
234     // TODO:
235 };
236
237 function ValidateRunOptions(options) {
238     // TODO:
239 }
240
241 Model.prototype.run = function() {
242     var args = validator.validateArgs(arguments, [
243         {
244             name: 'options',
245             type: validator.Types.DICTIONARY,
246             optional: true,
247             nullable: true
248         }
249     ]);
250     if (args.has.options) {
251         ValidateRunOptions(args.options);
252     }
253     // TODO
254 };
255
256 Model.prototype.summarize = function() {
257     var args = validator_.validateArgs(arguments, [
258         {
259             name: 'level',
260             type: types_.ENUM,
261             values: Object.values(VerbosityLevel),
262             optional: false
263         }
264     ]);
265     // TODO
266 };
267
268 Model.prototype.addLayer = function() {
269     var args = validator_.validateArgs(arguments, [
270         {
271             name: 'layer',
272             type: types_.PLATFORM_OBJECT,
273             values: Layer
274         }
275     ]);
276     // TODO
277 };
278
279 Model.prototype.setDataset = function() {
280     var args = validator_.validateArgs(arguments, [
281         {
282             name: 'dataset',
283             type: types_.PLATFORM_OBJECT,
284             values: Dataset
285         }
286     ]);
287     // TODO
288 };
289
290 Model.prototype.setOptimizer = function() {
291     var args = validator_.validateArgs(arguments, [
292         {
293             name: 'optimizer',
294             type: types_.PLATFORM_OBJECT,
295             values: Optimizer
296         }
297     ]);
298     // TODO
299 };
300
301 var ValidCreateLayerExceptions = ['NotSupportedError', 'TypeMismatchError', 'AbortError'];
302
303 var NO_ID = -1;
304 MachineLearningTrainer.prototype.createLayer = function() {
305     var args = validator_.validateArgs(arguments, [
306         {
307             name: 'type',
308             type: types_.ENUM,
309             values: Object.values(LayerType),
310             optional: false
311         }
312     ]);
313
314     // TODO
315     return new Layer(NO_ID);
316 };
317
318 function ValidateDatasetPaths(train, validate, test) {
319     // TODO
320 }
321
322 MachineLearningTrainer.prototype.createGeneratorDataset = function() {
323     var args = validator_.validateArgs(arguments, [
324         {
325             name: 'train',
326             type: types_.STRING
327         },
328         {
329             name: 'validate',
330             type: types_.STRING,
331             optional: true,
332             nullable: true
333         },
334         {
335             name: 'test',
336             type: types_.STRING,
337             optional: true,
338             nullable: true
339         }
340     ]);
341     ValidateDatasetPaths(args.train, args.validate.args.test);
342
343     // TODO
344     return new Dataset(NO_ID);
345 };
346
347 MachineLearningTrainer.prototype.createFileDataset = function() {
348     var args = validator_.validateArgs(arguments, [
349         {
350             name: 'train',
351             type: types_.FUNCTION
352         },
353         {
354             name: 'validate',
355             type: types_.FUNCTION,
356             optional: true,
357             nullable: true
358         },
359         {
360             name: 'test',
361             type: types_.FUNCTION,
362             optional: true,
363             nullable: true
364         }
365     ]);
366     ValidateDatasetPaths(args.train, args.validate.args.test);
367
368     // TODO
369     return new Dataset(NO_ID);
370 };
371
372 MachineLearningTrainer.prototype.createOptimizer = function() {
373     var args = validator_.validateArgs(arguments, [
374         {
375             name: 'optimizer',
376             type: types_.ENUM,
377             values: Object.values(OptimizerType),
378             optional: false
379         }
380     ]);
381
382     // TODO
383     return new Optimizer(NO_ID);
384 };
385
386 MachineLearningTrainer.prototype.constructModelWithConfiguration = function() {
387     var args = validator_.validateArgs(arguments, [
388         {
389             name: 'configPath',
390             type: types_.STRING,
391             optional: true
392         }
393     ]);
394     if (args.has.configPath) {
395         try {
396             args.configPath = tizen.filesystem.toURI(args.configPath);
397         } catch (e) {
398             throw new WebAPIException(WebAPIException.NOT_FOUND_ERR, 'Path is invalid');
399         }
400     }
401
402     // TODO
403     return new Model(NO_ID);
404 };
405
406 MachineLearningTrainer.prototype.constructModel = function() {
407     // TODO
408     return new Model(NO_ID);
409 };