This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial commit * license fix * changed path var, formatting * add test to linux stages in ci * disable test on osx stage in ci * cleaned up example CMakeLists.txt removed -shared from GPU * moved windows check Co-authored-by: Ubuntu <[email protected]> Co-authored-by: Manu Seth <[email protected]>
- Loading branch information
1 parent
3bf556d
commit 6bc0647
Showing
13 changed files
with
383 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# specify CXX sources | ||
FILE(GLOB CXX_SRCS | ||
# Required files | ||
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/lib_api.cc | ||
# Your custom files | ||
${CMAKE_CURRENT_SOURCE_DIR}/init_lib.cc | ||
${CMAKE_CURRENT_SOURCE_DIR}/min_ex.cc | ||
) | ||
|
||
# create library & set libraries | ||
add_library(external_lib SHARED ${CXX_SRCS}) | ||
target_link_libraries(external_lib PUBLIC mxnet) | ||
|
||
if(USE_CUDA) | ||
# specify GPU sources (optional) | ||
FILE(GLOB CU_SRCS "*.cu") | ||
target_sources(external_lib PUBLIC ${CU_SRCS}) | ||
endif(USE_CUDA) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<!--- Licensed to the Apache Software Foundation (ASF) under one --> | ||
<!--- or more contributor license agreements. See the NOTICE file --> | ||
<!--- distributed with this work for additional information --> | ||
<!--- regarding copyright ownership. The ASF licenses this file --> | ||
<!--- to you under the Apache License, Version 2.0 (the --> | ||
<!--- "License"); you may not use this file except in compliance --> | ||
<!--- with the License. You may obtain a copy of the License at --> | ||
|
||
<!--- http://www.apache.org/licenses/LICENSE-2.0 --> | ||
|
||
<!--- Unless required by applicable law or agreed to in writing, --> | ||
<!--- software distributed under the License is distributed on an --> | ||
<!--- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY --> | ||
<!--- KIND, either express or implied. See the License for the --> | ||
<!--- specific language governing permissions and limitations --> | ||
<!--- under the License. --> | ||
|
||
External Operators Example and Tutorial | ||
======================================= | ||
|
||
## Introduction | ||
|
||
Extending MXNet with custom components used to mean distributing a custom fork. This feature allows adding custom components to MXNet by dynamically loading external libraries at runtime. Currently it is only supported on Linux systems (Windows and Mac are __NOT__ supported). | ||
|
||
## Getting Started | ||
|
||
### Have MXNet Ready | ||
|
||
For this tutorial, clone MXNet from source like: | ||
``` | ||
git clone https://github.com/apache/incubator-mxnet.git --recursive --init | ||
``` | ||
|
||
Build MXNet like: | ||
``` | ||
cp config/linux.cmake config.cmake | ||
mkdir build | ||
cd build | ||
cmake .. | ||
cmake --build . | ||
``` | ||
|
||
## Run An Example | ||
|
||
This example shows compiling a custom backend operator and then dynamically loading it into MXNet at runtime. Go to the **lib_external_ops** directory and follow these steps: | ||
|
||
1. Touch or modify the **min_ex.cc** and/or **min_ex-inl.h** file(s) | ||
2. Go into the **build** directory that was created when building MXNet. | ||
3. Run `cmake .. -DBUILD_EXTENSION_PATH=$(pwd)/../example/extensions/lib_external_ops` | ||
4. Run `cmake --build .` | ||
5. Go to the **example/extensions/lib_external_ops** directory again | ||
6. Run `python test_loading.py` to execute the test program. You should see the following output: | ||
``` | ||
Operator not registered yet | ||
MXNet version 20000 supported | ||
[] | ||
Operator executed successfully | ||
``` | ||
|
||
## Writing an External Operator Library | ||
To build your own library containing custom components, compose a C++ source file like `mycomp_lib.cc`, include the `lib_api.h` header file, compile the `lib_api.cc` file, and implement the following required function: | ||
- `initialize` - Library Initialization Function | ||
|
||
Then create a CMakeLists.txt file and set `mxnet` as a link library like: | ||
``` | ||
add_library(external_lib SHARED ${SRCS}) | ||
target_link_libraries(external_lib PUBLIC mxnet) | ||
``` | ||
|
||
Next, build MXNet and set the path to your directory with the CMakeLists.txt file via the `BUILD_EXTENSION_PATH` option. This will build your library with all of the MXNet includes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/*! | ||
* Copyright (c) 2020 by Contributors | ||
* \file init_lib.cc | ||
* \brief initialize function implementation library file | ||
*/ | ||
|
||
#include <iostream> | ||
#include "mxnet/lib_api.h" | ||
|
||
using namespace mxnet::ext; | ||
|
||
MXReturnValue initialize(int version) { | ||
if (version >= 10700) { | ||
std::cout << "MXNet version " << version << " supported" << std::endl; | ||
return MX_SUCCESS; | ||
} else { | ||
MX_ERROR_MSG << "MXNet version " << version << " not supported"; | ||
return MX_FAIL; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/*! | ||
* Copyright (c) 2020 by Contributors | ||
* \file min_ex-inl.h | ||
* \brief example external operator header file | ||
*/ | ||
|
||
#ifndef MXNET_OPERATOR_TENSOR_MIN_EX_OP_INL_H_ | ||
#define MXNET_OPERATOR_TENSOR_MIN_EX_OP_INL_H_ | ||
|
||
#include <dmlc/parameter.h> | ||
#include <vector> | ||
#include <algorithm> | ||
#include "operator/mxnet_op.h" | ||
#include "operator/operator_common.h" | ||
#include "operator/elemwise_op_common.h" | ||
|
||
namespace mxnet { | ||
namespace op { | ||
|
||
template<typename xpu> | ||
void MinExForward(const nnvm::NodeAttrs& attrs, | ||
const OpContext& ctx, | ||
const std::vector<TBlob>& inputs, | ||
const std::vector<OpReqType>& req, | ||
const std::vector<TBlob>& outputs) { | ||
//do nothing | ||
} | ||
|
||
|
||
inline bool MinExOpShape(const nnvm::NodeAttrs& attrs, | ||
mxnet::ShapeVector* in_attrs, | ||
mxnet::ShapeVector* out_attrs) { | ||
//do nothing | ||
return true; | ||
} | ||
|
||
inline bool MinExOpType(const nnvm::NodeAttrs& attrs, | ||
std::vector<int> *in_attrs, | ||
std::vector<int> *out_attrs) { | ||
//do nothing | ||
return true; | ||
} | ||
|
||
} // namespace op | ||
} // namespace mxnet | ||
|
||
#endif // MXNET_OPERATOR_TENSOR_MIN_EX_OP_INL_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/*! | ||
* Copyright (c) 2020 by Contributors | ||
* \file min_ex.cc | ||
* \brief example external operator source file | ||
*/ | ||
|
||
#include "min_ex-inl.h" | ||
|
||
namespace mxnet { | ||
namespace op { | ||
|
||
NNVM_REGISTER_OP(min_ex) | ||
.describe("some description") | ||
.set_num_inputs(0) | ||
.set_num_outputs(0) | ||
.set_attr<mxnet::FInferShape>("FInferShape", MinExOpShape) | ||
.set_attr<nnvm::FInferType>("FInferType", MinExOpType) | ||
.set_attr<FCompute>("FCompute<cpu>", MinExForward<cpu>); | ||
|
||
} // namespace op | ||
} // namespace mxnet |
Oops, something went wrong.