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
MXNet Extensions enhancements2 #19016
Merged
samskalicky
merged 51 commits into
apache:master
from
samskalicky:extensions_enhancements2
Sep 1, 2020
Merged
Changes from 50 commits
Commits
Show all changes
51 commits
Select commit
Hold shift + click to select a range
4fea36d
initial commit
6d5fce2
Merge branch 'master' of https://github.com/apache/incubator-mxnet in…
3cea397
split lib_api.h into lib_api.cc, updated examples for 2.0/gluon
ead2684
fixed licenses
51ce458
whitespace
1ec330b
whitespace
bee854b
modernize
c36363e
fix modernize
53b4136
fix modernize
2953891
fix modernize
0c0cceb
fixed move
7cbc99b
added lib_api.cc to CMakeLists.txt for example libs
6965cd7
working example
db84377
Merge branch 'master' of https://github.com/apache/incubator-mxnet in…
42d00d0
remove GLOBAL to fix protobuf issue
2379eed
fixed library unload
afa87a7
added test target
b2f5a19
fixed sanity
68a3733
changed destructor to default
f90d8ad
Merge branch 'master' of https://github.com/apache/incubator-mxnet in…
04e88fb
added /LD option for customop_gpu_lib target
f4aaa84
Merge branch 'master' of https://github.com/apache/incubator-mxnet in…
b9f67ef
moved /LD inside the <>
4b9a4dc
diff compile flags for relu_lib.cu and lib_api.cc
4afe182
set CMAKE_VERBOSE_MAKEFILE for debug
7a36a40
added -v to ninja
8607847
added /MT
4165d02
another try
55b441f
Merge branch 'master' of https://github.com/apache/incubator-mxnet in…
f7de08e
changed /MT to -MT
4b7d119
set flags for cxx separately
c3719fd
split /LD /MT flags
1a79284
Merge branch 'master' of https://github.com/apache/incubator-mxnet in…
f6b9082
Merge branch 'master' of https://github.com/apache/incubator-mxnet in…
4335985
refactored cuda APIs into header file
1ef7b0a
removed debugging stuff
d7e241b
Merge branch 'master' of https://github.com/apache/incubator-mxnet in…
cac8fba
updated instructions for gpu build
36e0a6a
moved building into cmakelists
caaa011
moved build stuff into separate CMakeLists.txt
27c46d7
fixed gpu example
591141a
fixed license
0a4621d
added dlmc library dependency
c2e534b
added nnvm dependency
fd9f836
removed nnvm dmlc dependencies, added WINDOWS_EXPORT_ALL_SYMBOLS option
545aff6
fixed WINDOWS_EXPORT_ALL_SYMBOLS
df85c38
changed nnvm to shared library
bc80960
backed out external ops changes
ab0cc43
split relu example into separate files to test separate lib_api.h/cc
8fd4e2d
sanity
2431c9e
addressed initial review items
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ | |
*/ | ||
|
||
#include <iostream> | ||
#include "lib_api.h" | ||
#include "mxnet/lib_api.h" | ||
|
||
using namespace mxnet::ext; | ||
|
||
|
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,171 @@ | ||
/* | ||
* 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 relu_lib.cu | ||
* \brief simple custom relu and noisy relu operator implemented using CUDA function | ||
*/ | ||
|
||
#include <iostream> | ||
#include "relu_lib.h" | ||
|
||
using namespace mxnet::ext; | ||
|
||
MXReturnValue parseAttrs(const std::unordered_map<std::string, std::string>& attrs, | ||
int* num_in, int* num_out) { | ||
*num_in = 1; | ||
*num_out = 1; | ||
return MX_SUCCESS; | ||
} | ||
|
||
MXReturnValue inferType(const std::unordered_map<std::string, std::string>& attrs, | ||
std::vector<int>* intypes, | ||
std::vector<int>* outtypes) { | ||
outtypes->at(0) = intypes->at(0); | ||
return MX_SUCCESS; | ||
} | ||
|
||
MXReturnValue inferShape(const std::unordered_map<std::string, std::string>& attrs, | ||
std::vector<std::vector<unsigned int>>* inshapes, | ||
std::vector<std::vector<unsigned int>>* outshapes) { | ||
outshapes->at(0) = inshapes->at(0); | ||
return MX_SUCCESS; | ||
} | ||
|
||
MXReturnValue forwardCPU(const std::unordered_map<std::string, std::string>& attrs, | ||
std::vector<MXTensor>* inputs, | ||
std::vector<MXTensor>* outputs, | ||
const OpResource& res) { | ||
float* in_data = inputs->at(0).data<float>(); | ||
float* out_data = outputs->at(0).data<float>(); | ||
for (int i=0; i<inputs->at(0).size(); i++) { | ||
out_data[i] = in_data[i] > 0 ? in_data[i] : 0; | ||
} | ||
return MX_SUCCESS; | ||
} | ||
|
||
MXReturnValue backwardCPU(const std::unordered_map<std::string, std::string>& attrs, | ||
std::vector<MXTensor>* inputs, | ||
std::vector<MXTensor>* outputs, | ||
const OpResource& res) { | ||
float* out_grad = inputs->at(0).data<float>(); | ||
float* in_data = inputs->at(1).data<float>(); | ||
float* in_grad = outputs->at(0).data<float>(); | ||
for (int i=0; i<inputs->at(1).size(); i++) { | ||
in_grad[i] = in_data[i] > 0 ? 1 * out_grad[i] : 0; | ||
} | ||
return MX_SUCCESS; | ||
} | ||
|
||
REGISTER_OP(my_relu) | ||
.setParseAttrs(parseAttrs) | ||
.setInferType(inferType) | ||
.setInferShape(inferShape) | ||
.setForward(forwardCPU, "cpu") | ||
.setForward(forwardGPU, "gpu") | ||
.setBackward(backwardCPU, "cpu") | ||
.setBackward(backwardGPU, "gpu"); | ||
|
||
|
||
MyStatefulReluCPU::MyStatefulReluCPU(const std::unordered_map<std::string, std::string>& attrs) | ||
: attrs_(attrs) {} | ||
|
||
MXReturnValue MyStatefulReluCPU::Forward(std::vector<MXTensor>* inputs, | ||
std::vector<MXTensor>* outputs, | ||
const OpResource& op_res) { | ||
return forwardCPU(attrs_, inputs, outputs, op_res); | ||
} | ||
|
||
MXReturnValue MyStatefulReluCPU::Backward(std::vector<MXTensor>* inputs, | ||
std::vector<MXTensor>* outputs, | ||
const OpResource& op_res) { | ||
return backwardCPU(attrs_, inputs, outputs, op_res); | ||
} | ||
|
||
MyStatefulReluGPU::MyStatefulReluGPU(const std::unordered_map<std::string, std::string>& attrs) | ||
: attrs_(attrs) {} | ||
|
||
MXReturnValue MyStatefulReluGPU::Forward(std::vector<MXTensor>* inputs, | ||
std::vector<MXTensor>* outputs, | ||
const OpResource& op_res) { | ||
return forwardGPU(attrs_, inputs, outputs, op_res); | ||
} | ||
|
||
MXReturnValue MyStatefulReluGPU::Backward(std::vector<MXTensor>* inputs, | ||
std::vector<MXTensor>* outputs, | ||
const OpResource& op_res) { | ||
return backwardGPU(attrs_, inputs, outputs, op_res); | ||
} | ||
|
||
|
||
MXReturnValue createOpStateCPU(const std::unordered_map<std::string, std::string>& attrs, | ||
CustomStatefulOp** op_inst) { | ||
*op_inst = new MyStatefulReluCPU(attrs); | ||
return MX_SUCCESS; | ||
} | ||
|
||
MXReturnValue createOpStateGPU(const std::unordered_map<std::string, std::string>& attrs, | ||
CustomStatefulOp** op_inst) { | ||
*op_inst = new MyStatefulReluGPU(attrs); | ||
return MX_SUCCESS; | ||
} | ||
|
||
REGISTER_OP(my_state_relu) | ||
.setParseAttrs(parseAttrs) | ||
.setInferType(inferType) | ||
.setInferShape(inferShape) | ||
.setCreateOpState(createOpStateCPU, "cpu") | ||
.setCreateOpState(createOpStateGPU, "gpu"); | ||
|
||
MXReturnValue noisyForwardCPU(const std::unordered_map<std::string, std::string>& attrs, | ||
std::vector<MXTensor>* inputs, | ||
std::vector<MXTensor>* outputs, | ||
const OpResource& res) { | ||
float* in_data = inputs->at(0).data<float>(); | ||
float* out_data = outputs->at(0).data<float>(); | ||
|
||
mx_cpu_rand_t* states = res.get_cpu_rand_states(); | ||
std::normal_distribution<float> dist_normal; | ||
|
||
for (int i=0; i<inputs->at(0).size(); ++i) { | ||
float noise = dist_normal(*states); | ||
out_data[i] = in_data[i] + noise > 0 ? in_data[i] + noise : 0; | ||
} | ||
return MX_SUCCESS; | ||
} | ||
|
||
REGISTER_OP(my_noisy_relu) | ||
.setParseAttrs(parseAttrs) | ||
.setInferType(inferType) | ||
.setInferShape(inferShape) | ||
.setForward(noisyForwardCPU, "cpu") | ||
.setForward(noisyForwardGPU, "gpu") | ||
.setBackward(backwardCPU, "cpu") | ||
.setBackward(backwardGPU, "gpu"); | ||
|
||
MXReturnValue initialize(int version) { | ||
if (version >= 20000) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are you sure about this? since gemm_lib is still 10700 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we should update all the examples to 20000 on master. ill do that in the next PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. discussed offline and we will change example corresponding to master |
||
std::cout << "MXNet version " << version << " supported" << std::endl; | ||
return MX_SUCCESS; | ||
} else { | ||
MX_ERROR_MSG << "MXNet version " << version << " not supported"; | ||
return MX_FAIL; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Offline discussion with @leezu, in another PR we should move this build code into CMakeLists.txt for each example and use
add_subdirectory
to include it and replace the current Makefiles so theres only 1 set of build steps for each example.