Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: added note on updating Front-end language binding to support LTS.

...

Current MXNet only supports maximal tensors size around 4 billon (2^32). This is because uint32_t is used as default data type for tensor size as well as indexing variables. This limitation has created many problems when larger tensors are used in the model.
A naive solution to this problem is to replace all uint32_t in the MXNet backend source code by int64_t. This solution is not viable, however, because many data structures use uint32_t as data type for its members. Unnecessarily replacing these variables to int64_t will increase the memory consumption causing another limitation. Second, MXNet has many submodule dependencies. Updating the variable types in MXNet repository is not enough. We also need to make sure different libraries, such as MKLDNN, MShadow etc. supports the int64_t integer data type. Third, many front end APIs assumes unsigned 32-bit integer interface. Only updating the interface in C/C++ will cause all the language bindings to fail.

Therefore, we need a systematic approach to enhance MXNet to support large tensors.

Deliverables

  1. Support commonly used operators in MXNet
  2. Investigate ways to evaluate and mitigate performance impact
  3. Add Benchmark tests for large tensor operators
  4. Write a blog post about memory allocation research

Operators to be supported

This project is to enable MXNet to support large tensors. It should also provide guideline for future developers of the correct data type to choose when defining an integer variables in the MXNet backend. We should also provide a performance benchmark at operator level as well as model level between 64-bit integer and 32-bit integers. Moreover, we need to provide a mechanism to prevent future PRs breaking this support.

The following spreadsheet epic keeps track of the operators that need support large array operationshave been supported and the ones to be supported:

...

Jira

...

server

...

ASF JIRA

...

serverId5aa69414-a9e9-3523-82ec-879b028fb15b
keyMXNET-1184

If support for additional operators is required then please add a task to the JIRA epic.

Open Questions

  • MKLDNN support
  • CuDNN support

Challenges

  • How to address this problem across all submodules
    • mixed data types are used in several submodules such as mshadow, dlpack etc.
  • How to address this problem across all language bindings
    • C APIs are used by many language bindings
  • CUDNN and MKLDNN support
    • Need to make sure CUDNN and MKLDNN operators also support large indices
  • Performance impact
    • there are known performance differences between int32 and int64 operations. How to make sure these differences would not cause severe performance regression in model training an inference
    • memory footprint
  • Backward compatibility

Proposed Approach

Due to the challenges mentioned above, we plan to take staged development for this feature.

Stage 1: Use a compiler to enable/disable int64 support. By default this switch is off to prevent any performance impact

Stage 2: Benchmark performance between int64 and int32; fixing the performance difference that cause severe impact to training and inference. Turn on compiler flag by default

...

Open Questions

  • How to verify all operators support large tensors
  • Impact to GPU
  • MKLDNN support
  • CuDNN support

...

To support large tensor operations in MXNet backend, we need to update the followings:
1) Support large tensor size in NDArray data structure. We need to make sure the data structure of a tensor can hold sufficiently large number of elements.

2) Allow index loop to go beyond 2^31:
In CPU operator implementation, the kernel always use a Map() function to process each data element. The indexing variable need to use int64_t
A PR has been submitted to address a subset of the operators:
https://github.com/apache/incubator-mxnet/pull/13418

3) Update different API interfaces
This involves the API interface between MXNet backend and different front end language.
E.g. On the language binding side (front-end language like Python), int64_t values needs to be passed to the C++ backend. (Python: `ctypes.c_int64`) Otherwise, the value gets truncated at the language binding side.


There are two defined data types used in MXNET backend in addition to the native integer types: index_t, and dim_t. An earlier PR has been submitted to use int64_t for index_t and dim_t:
https://github.com/apache/incubator-mxnet/pull/11742
https://github.com/dmlc/mshadow/pull/348
The shape size currently is also using uint32_t. Although the total number of elements can go above 4 billion, it is very rare for one dimension to go above that number. Therefore, we may be able to keep the shape size in uint32_t
Since int32 are used by many language bindings, we will add extra 64-bit C APIs and use them in Python bindings first. Other language binds can choose to use the 64-bit APIs if they also plan to support large indices.

Future Development Guideline

...

Challenges

  • How to address this problem across all submodules
  • How to address this problem across all language bindings
  • GPU and MKLDNN support
  • This change potentially suffers from memory fragmentation. Since allocation on very large contiguous chunks of memory is not easy for OS. It uses virtual memory to achieve this. But this is also limited to the page size used by OS to allocate memory on Physical Memory. If the memory is too fragmented that OS cannot even allocate memory of the size of smallest page chunk then it will give memory allocation error.Approach to mitigate this problem: A new data structure based on the requirements of customer's that require creation of very large tensors. The new data structure can be optimized for either random access or contiguous access and should address the problem of memory fragmentation by allocating memory of smaller sized chunks.

Backward compatibility

We should support all existing operators with uint32_t data types.

...

  • Add nightly test to test all existing operators with tensor size over 5 billion. To test each operator in Python, we can leverage the existing check_speed() utility function.

...