Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

[Discussion] Sharing Operators between DL Frameworks  #4735

Closed

Description

@tqchen

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

  1. tqchen commented on Jan 19, 2017

    @tqchen
    MemberAuthor

    I also had similar discussion with @Yangqing and @ajtulloch before.

  2. Yangqing commented on Jan 19, 2017

    @Yangqing

    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.

  3. soumith commented on Jan 19, 2017

    @soumith

    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:

    • one function that consumes:
      • a void* pointer
      • a long* pointer of sizes
      • a long* pointer of strides
      • nDims
        and it spits out a common Tensor descriptor
    • Each frameworks defines a convertor to and from the common descriptor
    • Frameworks factor out their ops to be used as independently compiled libs

    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?

  4. soumith commented on Jan 19, 2017

    @soumith

    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.

  5. tqchen commented on Jan 19, 2017

    @tqchen
    MemberAuthor

    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

    #include <common_nn_op.h>
    
    void InitMXNetOps() {
       for (auto reg: Registry::ListBinaryOps()) {
           register(reg->name, reg->function);
       }
    }

    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.

  6. tqchen commented on Jan 19, 2017

    @tqchen
    MemberAuthor

    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.

    BinaryOp(const Tensor& lhs, const Tensor& rhs, Tensor*out);
    BianryOpShape(const Shape& lhs, const Shape& rhs, Shape* out);

    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.

  7. piiswrong commented on Jan 19, 2017

    @piiswrong
    Contributor

    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.

  8. piiswrong commented on Jan 19, 2017

    @piiswrong
    Contributor

    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.

  9. tqchen commented on Jan 19, 2017

    @tqchen
    MemberAuthor

    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

  10. Yangqing commented on Jan 19, 2017

    @Yangqing

    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.

  11. jermainewang commented on Jan 20, 2017

    @jermainewang
    Contributor

    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.

  12. Yangqing commented on Jan 20, 2017

    @Yangqing

    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.)

  13. tqchen commented on Jan 21, 2017

    @tqchen
    MemberAuthor

    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.

  14. bhack commented on Feb 2, 2017

    @bhack

    Anybody is interested in Tensor API?

  15. bhack commented on Feb 2, 2017

    @bhack

    You can find the source code of an inital AMD implementation of the standard

  16. 82 remaining items

  17. bhack commented on Feb 12, 2017

    @bhack

    Common Dnn Api/Core Dnn Api/Common Kernel Api etc..?

  18. naibaf7 commented on Feb 12, 2017

    @naibaf7

    @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?

  19. bhack commented on Feb 12, 2017

    @bhack

    @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?

  20. mli commented on Feb 13, 2017

    @mli
    Contributor

    how about libop?

    i prefer to not use tensor because mathematically tensor has 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.

  21. Yangqing commented on Feb 13, 2017

    @Yangqing

    Following the idea of BLAS we can probably call it BDAS (basic deep-learning algebra subprograms) - sounds like "badass".

  22. piiswrong commented on Feb 13, 2017

    @piiswrong
    Contributor

    LOL. In that spirit how about BAsic Neural Artificial Network Algebra Subroutines (BANANAS)

  23. mli commented on Feb 13, 2017

    @mli
    Contributor
  24. bhack commented on Feb 13, 2017

    @bhack

    DLPACK is not so bad.

  25. futurely commented on Feb 13, 2017

    @futurely
  26. bhack commented on Feb 13, 2017

    @bhack
  27. cyberfire commented on Feb 14, 2017

    @cyberfire

    Just two points:

    1. Before naming the project, it should be clear about the targets and boundaries of this project clear first: how many issues are there and what issues are to be resolved at what stage.
    2. Using an agile method to start from easy targets: those are probably to be resolved quickly and easy to get agreement from all participants.
  28. jli05 commented on Feb 14, 2017

    @jli05
    Contributor

    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.

  29. tqchen commented on Feb 24, 2017

    @tqchen
    MemberAuthor
  30. tqchen commented on Feb 24, 2017

    @tqchen
    MemberAuthor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions