Turi Create  4.0
unity_global.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef TURI_UNITY_GLOBAL_HPP
7 #define TURI_UNITY_GLOBAL_HPP
8 
9 #include <model_server/lib/unity_base_types.hpp>
10 #include <model_server/lib/toolkit_function_specification.hpp>
11 #include <model_server/lib/toolkit_function_registry.hpp>
12 #include <model_server/lib/toolkit_class_registry.hpp>
13 #include <model_server/lib/api/unity_global_interface.hpp>
14 
15 namespace turi {
16 
17 /**
18  * \ingroup unity
19  * The \ref turi::unity_global and \ref turi::unity_global_base classes
20  * implement a singleton object on the server side which is exposed to the
21  * client via the cppipc system. This singleton object provides other
22  * miscellaneous uncategorized services (global functions) to the python layer.
23  */
25  private:
26  toolkit_function_registry* toolkit_functions;
27  toolkit_class_registry* classes;
28 
29  struct so_registration_list {
30  void* dl;
31  std::string modulename; // base filename of the shared library excluding the extension
32  std::string original_soname; // also the key in dynamic_loaded_toolkits
33  std::string effective_soname;
34  std::vector<std::string> functions;
35  std::vector<std::string> classes;
36  };
37  // map of soname to registration
38  std::map<std::string, so_registration_list> dynamic_loaded_toolkits;
39  variant_map_type load_model_impl(turi::iarchive& iarc, bool include_data);
40 
41  public:
42  static constexpr char const * const OLD_CLASS_MAGIC_HEADER = "GLMODELX";
43  static constexpr char const * const CLASS_MAGIC_HEADER = "TCMODEL0";
44 
45  /**
46  * Constructor
47  * \param reg Pointer to Toolkit registry. Since Unity Global manaages
48  * toolkit execution
49  * \param server Pointer to the comm server object. Toolkit execution status
50  * will be emitted there.
51  */
52  unity_global(toolkit_function_registry* _toolkit_functions,
53  toolkit_class_registry* _classes);
54 
55  virtual ~unity_global();
56 
57  /**
58  * Get the version string
59  */
60  std::string get_version();
61 
62  /**
63  * Constructs a graph from a binary file on disk, or HDFS
64  */
65  std::shared_ptr<unity_sgraph_base> load_graph(std::string fname);
66 
67  /**
68  * Lists the names of all regsitered classes.
69  */
70  std::vector<std::string> list_toolkit_classes();
71 
72  /**
73  * Load toolkit class from file.
74  *
75  * Returns variant_map varmap
76  * - varmap['archive_version'] if 0, is the legacy version. 1 is the current version.
77  *
78  * Archive version 1:
79  *
80  * - varmap['model_name'] stores the C++ model name. Always available
81  * - varmap['model'] stores the unity toolkit class object pointer.
82  * May not be always available.
83  * - varmap['side_data'] stores a varmap of any additional side data
84  * serialized along with the model. May not be always available.
85  *
86  *
87  * Legacy:
88  * - varmap['model_wrapper'] stores the serialized lambda to construct the python class.
89  * - varmap['model_base'] stores the unity toolkit class object pointer.
90  *
91  * Throws an exception if there is an error reading the url or serializing to
92  * a proper toolkit class object.
93  */
94  // serialized to a file path
95  variant_map_type load_model(const std::string& url);
96  // in-memory data (NOTE: only works for models that don't use anything except objects.bin)
97  variant_map_type load_model_from_data(std::istream& data);
98 
99  /**
100  * Save a toolkit class to file.
101  *
102  * \param tkclass The pointer to the actual toolkit class object.
103  * \param sidedata Any additional side information
104  * \param url The destination url to store the class.
105  */
106  void save_model(std::shared_ptr<model_base> tkclass,
107  const variant_map_type& sidedata, const std::string& url);
108 
109  void save_model_to_data(std::shared_ptr<model_base> tkclass, std::ostream& out);
110 
111  /**
112  * Saves a model using an alternative model saving method where a model class
113  * is not available.
114  *
115  * \param model_name A unique string name of the model
116  * \param sidedata Any additional side information
117  * \param url The destination url to store the class.
118  */
119  void save_model2(const std::string& model_name,
120  const variant_map_type& sidedata, const std::string& url);
121 
122  /**
123  * Lists the names of all registered toolkit functions.
124  */
125  std::vector<std::string> list_toolkit_functions();
126 
127  /**
128  * Returns a dictionary describing the toolkit. It will return a dictionary
129  * with 2 fields:
130  * - "name": The name of the toolkit
131  * - "arguments": The list of input parameters
132  * - "documentation"
133  */
134  std::map<std::string, flexible_type> describe_toolkit_function(std::string toolkitname);
135 
136 
137  /**
138  * Returns a dictionary describing the class. It will return a dictionary
139  * with 2 fields:
140  * - "name": The name of the toolkit
141  * - "functions": A dictionary with key: function name, and value,
142  * a list of input parameters.
143  * - "get_properties": The list of all readable properties of the class
144  * - "set_properties": The list of all writable properties of the class
145  * - "documentation"
146  */
147  std::map<std::string, flexible_type> describe_toolkit_class(std::string class_name);
148 
149 
150  /**
151  * Creates a class instance.
152  */
153  std::shared_ptr<model_base> create_toolkit_class(std::string class_name);
154 
155  /**
156  * Runs a toolkit of the specified name, and with the specified arguments.
157  * Returns a toolkit_function_response_type which contains the result of the toolkit
158  * execution (success/failure) as well as any additional returned state
159  * (graphs/classes/etc). Will throw an exception if the toolkit name was not
160  * found.
161  */
162  toolkit_function_response_type run_toolkit(std::string toolkit_name,
163  variant_map_type& arguments);
164 
165  /**
166  * Internal utility function. Gets the structure of the lazy
167  * evaluation dag for the graph operations.
168  */
169  std::string get_graph_dag();
170 
171  /**
172  * Evaluate a pickled python lambda with the given argument.
173  */
174  flexible_type eval_lambda(const std::string& pylambda_string, const flexible_type& arg);
175 
176  /**
177  * Evaluate a pickled python lambda with dictionary argument.
178  */
179  flexible_type eval_dict_lambda(const std::string& pylambda_string,
180  const std::vector<std::string>& keys,
181  const std::vector<flexible_type>& args);
182 
183  /**
184  * Evaluate a pickled python lambda on a list of argument in parallel.
185  */
186  std::vector<flexible_type> parallel_eval_lambda(const std::string& pylambda_string,
187  const std::vector<flexible_type>& arg);
188 
189  /**
190  * \internal
191  * Reads the content of the given url.
192  *
193  * Return a string containing the content of the given url.
194  *
195  * Throws exception if IO error occurs.
196  *
197  * \note This function should only be used for internal testing.
198  */
199  std::string __read__(const std::string& url);
200 
201  /**
202  * \internal
203  * Writes the content of to the given url.
204  *
205  * Throws exception if IO error occurs.
206  *
207  * \note This function should only be used for internal testing.
208  */
209  void __write__(const std::string& url, const std::string& content);
210 
211  /**
212  * \internal
213  * Creates a directory that will have the given url.
214  *
215  * Throws exception if directory already exists.
216  */
217  bool __mkdir__(const std::string& url);
218 
219  /**
220  * \internal
221  * Changes permissions of the given url.
222  */
223  bool __chmod__(const std::string& url, short mode);
224 
225  /**
226  * \internal
227  * Returns the size of the process heap.
228  * May not be available. Returns 0 if unavailable.
229  */
230  size_t __get_heap_size__();
231 
232 
233  /**
234  * \internal
235  * Returns the amount of memory used inside the heap.
236  * May not be available. Returns 0 if unavailable.
237  */
238  size_t __get_allocated_size__();
239 
240  /**
241  * \internal
242  * Sets the logging level
243  */
244  void set_log_level(size_t);
245 
246  /**
247  * \internal
248  * Lists all the global configuration values. If runtime_modifiable == true,
249  * lists all global values which can be modified at runtime. If runtime ==
250  * false, lists all global values which can only be modified by environment
251  * variables.
252  */
253  std::map<std::string, flexible_type> list_globals(bool runtime_modifiable);
254 
255 
256  /**
257  * \internal
258  * Sets a modifiable global configuration value. Returns an empty string
259  * on success and an error string on failure.
260  */
261  std::string set_global(std::string key, flexible_type value);
262 
263  /**
264  * \internal
265  * Create a sequentially increasing (or decreasing) SArray.
266  *
267  * If 'reverse' is true, counts down instead of up.
268  */
269  std::shared_ptr<unity_sarray_base> create_sequential_sarray(ssize_t size, ssize_t start, bool reverse);
270 
271  /**
272  * Attempts to load a toolkit from a shared library.
273  * Returns an empty string on success.
274  * An string describing the error on failure.
275  *
276  * The so will be loaded with prefix [module_subpath].[filename].[...]
277  * For instance: if the so is called "example.so" containing a function
278  * square_root.
279  * \code
280  * load_toolkit("example.so", "")
281  * \endcode
282  * will load the toolkit function into "example.square_root".
283  * (it will also appear in tc.extensions.example.square_root)
284  *
285  * \code
286  * load_toolkit("example.so", "pika")
287  * \endcode
288  * will load the toolkit function "pika.example.square_root".
289  * (it will also appear in tc.extensions.pika.example.square_root)
290  *
291  * module_subpath can also be ".."
292  * \code
293  * load_toolkit("example.so", "..")
294  * \endcode
295  * In which case it will appear anywhere except in the top level of
296  * tc.extensions as tc.extensions.square_root.
297  */
298  std::string load_toolkit(std::string soname,
299  std::string module_subpath);
300 
301  /**
302  * Lists all the functions in a toolkit. Raises an exception if the toolkit
303  * was not previously loaded by load_toolkit.
304  */
305  std::vector<std::string> list_toolkit_functions_in_dynamic_module(std::string soname);
306 
307  /**
308  * Lists all the classes in a toolkit. Raises an exception if the toolkit
309  * was not previously loaded by load_toolkit.
310  */
311  std::vector<std::string> list_toolkit_classes_in_dynamic_module(std::string soname);
312  /**
313  * \internal
314  * Retrieve the folder that is currently being used to hold temp files.
315  *
316  * This is where all the SFrame files and such are located.
317  */
318  std::string get_current_cache_file_location();
319 
320  /**
321  * Returns a pointer to the toolkit function registry
322  */
324 
325  /**
326  * Returns a pointer to the toolkit class registry
327  */
329 
330 
331  /**
332  * Given a url, returns the type of the Turi object, return value could be:
333  * model, graph, sframe, sarray
334  */
335  std::string get_turicreate_object_type(const std::string& url);
336 
337 
338  /**
339  * A alternate implementation of var which knows how to
340  * save models. Models are special because they rely on the unity_global
341  * registry. Fully compatible with variant_deep_save otherwise.
342  * \see variant_deep_save
343  */
344  void model_variant_deep_save(const variant_type& var, oarchive& oarc);
345 
346  /**
347  * A alternate implementation of variant_deep_load which knows how to
348  * save models. Models are special because they rely on the unity_global
349  * registry. Fully compatible with variant_deep_load otherwise.
350 
351  * \see variant_deep_load
352  */
354 };
355 }
356 #endif
flexible_type eval_dict_lambda(const std::string &pylambda_string, const std::vector< std::string > &keys, const std::vector< flexible_type > &args)
std::map< std::string, flexible_type > describe_toolkit_class(std::string class_name)
std::string set_global(std::string key, flexible_type value)
std::string load_toolkit(std::string soname, std::string module_subpath)
std::vector< std::string > list_toolkit_classes()
size_t __get_heap_size__()
void save_model2(const std::string &model_name, const variant_map_type &sidedata, const std::string &url)
The serialization input archive object which, provided with a reference to an istream, will read from the istream, providing deserialization capabilities.
Definition: iarchive.hpp:60
void model_variant_deep_save(const variant_type &var, oarchive &oarc)
flexible_type eval_lambda(const std::string &pylambda_string, const flexible_type &arg)
variant_map_type load_model(const std::string &url)
std::string get_version()
std::set< Key > keys(const std::map< Key, T > &map)
Definition: stl_util.hpp:358
std::vector< std::string > list_toolkit_classes_in_dynamic_module(std::string soname)
std::string get_current_cache_file_location()
boost::make_recursive_variant< flexible_type, std::shared_ptr< unity_sgraph_base >, dataframe_t, std::shared_ptr< model_base >, std::shared_ptr< unity_sframe_base >, std::shared_ptr< unity_sarray_base >, std::map< std::string, boost::recursive_variant_ >, std::vector< boost::recursive_variant_ >, boost::recursive_wrapper< function_closure_info > >::type variant_type
Definition: variant.hpp:24
std::vector< std::string > list_toolkit_functions()
std::string __read__(const std::string &url)
unity_global(toolkit_function_registry *_toolkit_functions, toolkit_class_registry *_classes)
std::string get_turicreate_object_type(const std::string &url)
std::vector< std::string > list_toolkit_functions_in_dynamic_module(std::string soname)
void set_log_level(size_t)
toolkit_function_registry * get_toolkit_function_registry()
void save_model(std::shared_ptr< model_base > tkclass, const variant_map_type &sidedata, const std::string &url)
std::shared_ptr< unity_sarray_base > create_sequential_sarray(ssize_t size, ssize_t start, bool reverse)
void __write__(const std::string &url, const std::string &content)
toolkit_function_response_type run_toolkit(std::string toolkit_name, variant_map_type &arguments)
bool __mkdir__(const std::string &url)
size_t __get_allocated_size__()
std::map< std::string, flexible_type > describe_toolkit_function(std::string toolkitname)
std::map< std::string, flexible_type > list_globals(bool runtime_modifiable)
toolkit_class_registry * get_toolkit_class_registry()
The serialization output archive object which, provided with a reference to an ostream, will write to the ostream, providing serialization capabilities.
Definition: oarchive.hpp:80
bool __chmod__(const std::string &url, short mode)
std::string get_graph_dag()
void model_variant_deep_load(variant_type &var, iarchive &iarc)
std::vector< flexible_type > parallel_eval_lambda(const std::string &pylambda_string, const std::vector< flexible_type > &arg)
std::shared_ptr< model_base > create_toolkit_class(std::string class_name)
std::shared_ptr< unity_sgraph_base > load_graph(std::string fname)