Repository navigation
[Discussion] Sharing Operators between DL Frameworks #4735
Description
See This Link for discussion repo
This discussion started from dmlc/minpy#129, with @soumith THC is a tensor library that backs torch. I open this issue in MXNet repo so more developers can see it.
First of all, it is possible reuse operator libraries between frameworks, for example
- Support for THC and Torch Module was done in Torch Plugin, with interfacing to torch's lua library.
- MXNet supports reuse operators from caffe
It is always interesting to see interchangeability happen. For example, schedule pytorch operations in mxnet's async engine, or run mxnet's declarative API to directly share data with pytorch's array.
However, there is some engineering obstacles in doing so, which I would like to explain what these obstacles are, and hopefully this can motivate the community to move forward, and make this easier.
Coupled Operator Data Structure Components
An operator can mean many things, here are some basic components on what the operators are:
- Data structure that holds(shape) pointers to the array
- Possible memory allocator to handle run-time memory allocation
- Resource handles, if external resources is needed
- Scheduling related objects if array support synchronize execution
Why such coupling prevents reuse? There are two reasons
- Many systems have their own memory allocator and ways of resource handling code.
- While having memory allocator enables runtime memory allocations, sometimes memory allocation is not preferred at all(e.g. BLAS calls where all memory are pre-allocated)
To resolve this problem, an operator library design should enable operators that accept user managed memory resources, when possible, not introduce allocator or resource management, but give hints to the user(CuDNN's workspace requirement eliminates the need to internal memory allocator).
From this point of view, CuDNN an cuBLAS are good examples. THC is nice, but still encapsulate memory allocator(which is needed sometimes for dynamic operators).
Lack of Unified Operator Interface
The second obstacle is mainly lack of common operator interface. This is a problem of CUDNN and THC that prevents reusing. Take CuDNN for example, each CuDNN API is a C function, with its own interface, to adopt the operator, there need to be one(or multiple) adapting function per operator.
Consider instead, if there is an unified operator interface(the following is a mock design), where each TBlob is a reference to the data fields and shape, and every function gets registered to the registry with their name
using FCompute = std::function<void (
array_view<TBlob> ins, array_view<TBlob> outs, map kwargs, stream stream)>Then it only takes one function to extract, and reuse all operators and automatically expose them to front end. In MXNet, it even directly generates the symbolic counterpart from the same imperative operator, if gradient is provided.
Problem of One Unified Operator Interface
There is always a flip side of the coin. Assume that we go with a unified operator interface. As a matter of fact, that is what MXNet, TensorFlow and Caffe have done. The problem now becomes what the interface should look like? One trap that framework designer always falls into is that we need one interface that rules them all.
Since one interface rules them all, we want to support all possible operators, what about the ones that need runtime memory allocations? Maybe add memory allocator to it, what about the ones that is asynchronize? In the end, the interface have to include memory-allocator, scheduling module in some way,
and that introduces the "Coupled Operator Data Structure Components" problem. The operator interface become deeply coupled with the rest of the framework and not reusable.
A Better Solution: A Few Unified Interfaces
Can we get the best of both worlds, having as few data structures and interfaces as possible, while still not introducing coupling to allocator and scheduling as much as possible? I think the answer is yes and we need to jump out from the ideal of one interface that rules all the operators.
I can categorize the operators roughly in three categories
- type1: Basic operators: The ones that can do shape inference based on input shape, can take memory pointer, stream and go
- type2: Basic+ operators: Same as basic operator, but also need to declare some additional resources(workspace)
- type3: Complicated operators: The ones that requires runtime memory allocator, its output shape depends on content of the data.
If we design for general operator interface, the answer will usually looks like type3. However, type 1 and 2 dominates 90%+ of the major operators we are using.
If we design one operator interfaces for each type, this problem is solved. So that frameworks can pull and interact with each type in their own way.
It is much easier to do things like static memory planning if type1 and type2 are explicitly introduced. This is one additional layer of wrapping on top of THC and CuDNN is is lacking so far.
A registry system like NNVM could come very handy to easily resgister these informations, and get pull out by the libraries.
The Hope
I have always hopped that there is a minimum set of operator interface standard in C++, that can be shared across libraries. I think we have a good idea on what the solution looks like. While most system tends to become opague and coupled, I think this kind of transparent way can help evolve the community in a healthy way. This being said, there is always effort to make these happen. This involves a open discussion on what the interfaces should be and commitment from framework builders. I would really love to see this happen, and that is why I spend more than one hour writing this.
Unfortunately, most frameworks already have kinda of "enough collection of operators", so having a unified operator interface will contribute little to each framework in terms of usability in short term. Naturally this would be given lower priority. That is why commitment is needed to bring this out for longer term benefit
Activity
tqchen commentedon Jan 19, 2017
I also had similar discussion with @Yangqing and @ajtulloch before.
Yangqing commentedon Jan 19, 2017
Great initiative! I think a lot of components can be shared if we refactor them in simple APIs. Would love to work together on this front.
soumith commentedon Jan 19, 2017
The fundamental issue into having a Unified interface is that it needs a full buy-in. Anything short will make it a partial or full failure.
For this reason, I think what the CuDNN team did is actually correct.
For this reason, i think focusing on simplicity and reducing the friction of buy-in, and allowing a way to have partial buy-in will make more folks participate.
So, I think we should define:
and it spits out a common Tensor descriptor
Keeping it stupid and simple like this is the path of least resistance that will get us forward.
I dont feel confident that defining and maintaining a common registry will practically break ground, especially because it has a huge initial overhead from each of the framework writers (who are all busy with their own problems).
What do you guys think?
soumith commentedon Jan 19, 2017
what I proposed only works for stateless operations initially, but i think that's where we should start from. Defining statefulness right now will lead to disagreements and complications.
tqchen commentedon Jan 19, 2017
I think stateless is a good starting point(essentially the type 1 operator). I do however, would like to have a few set unified interfaces in someway, and a registry that is decentralized.
So the scenario I hoped looks like
This enables one function to import all the operators that is provided in the operator library. It would indeed require a bit of registry code from the operator library side, for example, a wrap around THC or the library @soumith suggested.
This reduces the effort of importing and adapting new operators. However, the interface indeed need to be simple enough, like the one that contains a few tensor data structures.
tqchen commentedon Jan 19, 2017
As I mentioned earlier, I do not agree on one unified operator interface, but I do like to see if there can be a few candidates that we can agree on. For example, binary operators.
The idea is to reduce the overhead of adaptation code, which otherwise need to happen for each operator, and it makes the framework builder harder to switch in.
piiswrong commentedon Jan 19, 2017
The easiest way of doing this is copy-pasting kernels, which we having been doing for a while.
a blas like interface is a good idea. But this is only worth while if the operator is complicated enough (i.e. longer than the code required to call it...). Sharing elementwise add probably isn't necessary.
having a TensorDescriptor like data structure further complicates this since you need to spend 20+ lines constructing these descriptors.
Unified operator interface is in theory the right way to do things, but obviously we all think our own interface is the best interface. So not sure if this will go anywhere anytime soon.
Examples of operators that are worth sharing: broadcast-reduce ops, embedding.
One thing we can do without having to agree to anything is some "principled copy pasta" wiki page where we share operator implementations without necessarily using the same interface.
A easily pair testing framework for verifying correctness on top of that is also good.
piiswrong commentedon Jan 19, 2017
Also instead of sharing compiled code, a header only library where you use all data structure and array indexing as macros that can be redefined for each framework is much easier.
For example mxnet doesn't support stride while torch does. So indexing works differently. This can be solved by defining different macros.
tqchen commentedon Jan 19, 2017
I see there are two ends of the current existing discussions
*** Build One Library that everybody will use***
Can use simple common data structure, with each framework calls into the same function. @soumith 's proposal is a better solution for this end. Since as long as the data structure is agreed, there is no problem of calling into the functions.
The problem is it is hard to convince developer to be fully committed to a shared core library.
*** Being able to import operators from other libraries ***
The major concern of me to do this is what is the overhead of importing? That is why some simple common interface, as with data structure might be desirable. So the cost of importing does not involve effort per operator, but instead one effort for importing all operators that all frameworks currently define.
*** What are the set of Interfaces***
To be clear, I do not think the MXNet's(nor interface of existing frameworks) interface is the best way to do operator sharing. But I do think there are set of cleaner and minimum interfaces that we might agree on, just like we can agree on the data structures
Yangqing commentedon Jan 19, 2017
Interface wise, if I may - the cudnn type interface is a good start and this is what I have been telling other vendors too. If there are implementations for e.g. OpenCL, OpenGL, Vulkan etc, this will make the frameworks' life much better.
jermainewang commentedon Jan 20, 2017
I think the interface includes two parts: (1) the function routines (2) tensor data structure. For example, I remember in THC, it has support for stride and offset, which is lacking in MXNet. If we use cudnn's way, then we need to include all these information in function arguments, which may be a problem for future extension.
Yangqing commentedon Jan 20, 2017
It makes sense to start with a non-strided version, I think - Caffe/2 does not use stride either and assumes a 256-byte alignment for storage.
RE cudnn - I actually think that having a pure C interface is good for extension, since it would make cross-language integration much easier. For example Python C extension doesn't do a very good C++ support.
(Oh by the way, pybind11 is awesome.)
tqchen commentedon Jan 21, 2017
I am all for C interface for stable ABI, on the other hand. As a matter of fact, almost all dmlc projects interfaces through C API.
It is always possible to have an auxiliary c++ registry if we can categorize the functions, and return the function handles.
bhack commentedon Feb 2, 2017
Anybody is interested in Tensor API?
bhack commentedon Feb 2, 2017
You can find the source code of an inital AMD implementation of the standard
82 remaining items
bhack commentedon Feb 12, 2017
Common Dnn Api/Core Dnn Api/Common Kernel Api etc..?
naibaf7 commentedon Feb 12, 2017
@tqchen I'd happy to wrap my LibDNN standalone library with the new interface once a reasonable collection of headers is available, to see how well that would work :)
And I guess @edgarriba could test out the host-side code in tiny-dnn?
@bhack What do you mean by that? Probably nothing beyond the interface for operators should be shared in this project?
bhack commentedon Feb 12, 2017
@naibaf7 I think that the boundaries are operators API that ingest this common tensor design.. I don't think that executors/scheduling will come on the table. What is your vision?
mli commentedon Feb 13, 2017
how about
libop?i prefer to not use
tensorbecause mathematicallytensorhas a rich of properties, while most operators we are using are just elemental-wise, so n-dimensional array is better to name the data structure.Yangqing commentedon Feb 13, 2017
Following the idea of BLAS we can probably call it BDAS (basic deep-learning algebra subprograms) - sounds like "badass".
piiswrong commentedon Feb 13, 2017
LOL. In that spirit how about BAsic Neural Artificial Network Algebra Subroutines (BANANAS)
mli commentedon Feb 13, 2017
bhack commentedon Feb 13, 2017
DLPACK is not so bad.
futurely commentedon Feb 13, 2017
TensorFlow & Keras combined have the largest user base and are growing most rapidly. You should bring those guys on board for this proposal to make the biggest impact.
http://www.timqian.com/star-history/#tensorflow/tensorflow&fchollet/keras&dmlc/mxnet&BVLC/caffe&Microsoft/CNTK&torch/torch7&Theano/Theano

bhack commentedon Feb 13, 2017
/cc @fchollet
cyberfire commentedon Feb 14, 2017
Just two points:
jli05 commentedon Feb 14, 2017
As a side topic, I personally think how to allow MXNet scale out over micro-kernel multi-server OSes and scale down on limited-battery devices is also important.
tqchen commentedon Feb 24, 2017
created a repo here https://github.com/dmlc/dlpack
tqchen commentedon Feb 24, 2017
Let us move the discussion to https://github.com/dmlc/dlpack/issues,