-
Notifications
You must be signed in to change notification settings - Fork 6.8k
[RFC] FFI: New Containers and Improve Object System #19672
Comments
It seems that the key difference between the TVM NDArray and MXNet NDArray wrt to FFI is that in TVM, the Ref will own a copy of the NDArray, whereas in the current implementation, MXNet NDArrayHandle in the FFI only owns a pointer to the underlying array. Changing the MXNet FFI to also own the underlying NDArray (copy constructor instead of pointer) should resolve the issue. Copy of the MXNet array would be cheap, as it only holds some metadata information and a shared pointer to the underlying storage chunk. WDYT? |
Yes, I agree with you. Owning the underlying NDArray instead of a pointer in NDArrayHandle should achieve the goal. |
This is the follow up PR for RFC #19672. Map container is added and more data types are supported by new FFI, like dictionary, list of strings. Make ADT container and MAP container support NDArray type. Adopt PackedFunc based FFI on CachedOp. Some CachedOp functions are implemented: create, free, invoke, get_optimized_symbol
Problem statement
Currently MXNet is using PackedFunc based FFI[1] to improve performance of imperative execution. C APIs will be registered as PackedFunc in the runtime system and we can call any of these PackedFunc via only one exposed API. But, PackedFunc based FFI has only ADT(Algebatic data type), Integer and Float these contaienrs registered in runtime system, and also it is limited to only few data types and structures as input, like Integer, Float, NDArray, list of Integers and list of Floats, tuple. For some complex data structures, like python dictionary, list of strings, we need to parse them or unzip them in front end first and then feed into packed function, which may reduce code readability and introduce other overheads. To adopt PackedFunc based FFI on some existed imperative calls or some other API calls in the future, new containers are needed in runtime system, like map.
Current Containers
In current MXNet FFI, we pack python arguments into C arguments that FFI accept through
_make_mxnet_args
and convertlist
ortuple
types into arguments that FFI accept throughconvert_to_node
function.Current container in use in PackedFunc based FFI is ADT, which is Algebatic data type. Since ADT container only accepts Object types as fields, Integer and Float types are also registered as Objects so that we can feed list of Integers and Floats into PackedFunc based FFI. However, here are the limitations that only list of Integers and list of Floats are accepted by
convert_to_node
function, other structures like list of strings, dictionary will raise ValueError.Proposed solutions
Our PackedFunc based FFI is mainly adapted from TVM project. So, we can also adapt Map object and String object from TVM.
Examples
A simple example of using Map container
We can register a map related C function, such as sum of all the values in the Map.
After compile and build the library, we can call
MapVSum
on Python side to sum all the diction values.Example for adopting new FFI on CreateCachedOp with map container
Some more discussions
In order to visit the member of the container, like get attribute value for each entry in map from front end, we can improve our object system by adopting VisitAttrs from TVM, which should provide us with a reflection API to visit each member of the object. I think it should be necessary if we want to fully benefit from new FFI.
We may need to find a way to make our containers capable of holding NDArrays. FFI containers are mainly adapted from TVM and can only hold ObjectRefs from object system. Different from TVM's NDArray, which inherits from ObjectRef and there is customized smart pointer called ObjectPtr to count references, NDArray in MXNet uses shared pointer to hold internal data. Holding NDArray in container requires NDArrayHandle can affect NDArray's reference counting.
References
Reference and related literature
[1] MXNet FFI for Operator Imperative Invocation #17510
Known implementations
TVM Runtime SystemL https://tvm.apache.org/docs/dev/runtime.html
DGL FFI Containers: https://docs.dgl.ai/developer/ffi.html
The text was updated successfully, but these errors were encountered: