This repository was 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
Aggregated zero grad #16446
Merged
Merged
Aggregated zero grad #16446
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3f2b68e
Trigger CI
drivanov f203d9a
Merge branch 'master' of https://github.com/drivanov/incubator-mxnet
drivanov f1fe0ad
Merge branch 'master' of https://github.com/apache/incubator-mxnet
drivanov e0faa63
Merge branch 'master' of https://github.com/apache/incubator-mxnet
drivanov 2dc607d
Merge branch 'master' of https://github.com/apache/incubator-mxnet
drivanov 0c6402e
Merge branch 'master' of https://github.com/apache/incubator-mxnet
drivanov d345ef9
Merge branch 'master' of https://github.com/apache/incubator-mxnet
drivanov d6b4882
Aggregated zeroing of the gradients/arrays
drivanov 5486616
New files for aggregated zeroing of the gradients/arrays
drivanov 3d35a8b
Adding possibility to reset the arrays of different types.
drivanov 5ca58e7
Minor cleanup
drivanov 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 hidden or 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 hidden or 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,99 @@ | ||
/* | ||
* 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) 2019 by Contributors | ||
* \file reset_arrays-inl.h | ||
* \brief setting all array element values to zeros | ||
* \author Moises Hernandez-Fernandez, Andrei Ivanov | ||
*/ | ||
|
||
#ifndef MXNET_OPERATOR_CONTRIB_RESET_ARRAYS_INL_H_ | ||
#define MXNET_OPERATOR_CONTRIB_RESET_ARRAYS_INL_H_ | ||
|
||
#include <mxnet/operator_util.h> | ||
#include <vector> | ||
#include <limits> | ||
#include <algorithm> | ||
#include "../elemwise_op_common.h" | ||
#include "../mshadow_op.h" | ||
#include "../mxnet_op.h" | ||
#include "../tensor/init_op.h" | ||
|
||
namespace mxnet { | ||
namespace op { | ||
|
||
struct ResetArraysParam : public dmlc::Parameter<ResetArraysParam> { | ||
int num_arrays; | ||
DMLC_DECLARE_PARAMETER(ResetArraysParam) { | ||
DMLC_DECLARE_FIELD(num_arrays) | ||
.describe("number of input arrays."); | ||
} | ||
}; | ||
inline bool ResetArraysShape(const NodeAttrs& attrs, | ||
std::vector<mxnet::TShape>* in_shape, | ||
std::vector<mxnet::TShape>* out_shape) { | ||
const auto& p = dmlc::get<ResetArraysParam>(attrs.parsed); | ||
CHECK_EQ(in_shape->size(), p.num_arrays); | ||
for (auto s : *in_shape) { | ||
if (s.ndim() == 0) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
inline bool ResetArraysType(const NodeAttrs& attrs, | ||
std::vector<int>* in_type, | ||
std::vector<int>* out_type) { | ||
const auto& param_ = dmlc::get<ResetArraysParam>(attrs.parsed); | ||
CHECK_EQ(in_type->size(), param_.num_arrays); | ||
const int dtype = (*in_type)[0]; | ||
CHECK_NE(dtype, -1) << "First input must have specified type"; | ||
for (size_t i = 0; i < in_type->size(); ++i) { | ||
const auto currType = (*in_type)[i]; | ||
if (currType == -1) | ||
(*in_type)[i] = dtype; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
template<typename xpu> | ||
void ResetMemory(void *pntr, size_t len, mshadow::Stream<xpu> *s); | ||
|
||
template<typename xpu> | ||
void ResetArrays(const nnvm::NodeAttrs& attrs, | ||
const OpContext &ctx, | ||
const std::vector<TBlob> &inputs, | ||
const std::vector<OpReqType> &req, | ||
const std::vector<TBlob> &outputs) { | ||
auto s = ctx.get_stream<xpu>(); | ||
const auto& p = nnvm::get<ResetArraysParam>(attrs.parsed); | ||
for (int i = 0; i < p.num_arrays; i++) { // array index in inputs | ||
const size_t size = inputs[i].shape_.Size(); | ||
MSHADOW_REAL_TYPE_SWITCH(inputs[i].type_flag_, DType, | ||
ResetMemory(inputs[i].FlatTo2D<xpu, DType>(s).dptr_, size * sizeof(DType), s); | ||
) | ||
} | ||
} | ||
|
||
} // namespace op | ||
} // namespace mxnet | ||
|
||
#endif // MXNET_OPERATOR_CONTRIB_RESET_ARRAYS_INL_H_ |
This file contains hidden or 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,74 @@ | ||
/* | ||
* 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) 2019 by Contributors | ||
* \file reset_arrays.cc | ||
* \brief setting all array element values to zeros | ||
* \author Moises Hernandez-Fernandez, Andrei Ivanov | ||
*/ | ||
|
||
#include "./reset_arrays-inl.h" | ||
|
||
namespace mxnet { | ||
namespace op { | ||
|
||
DMLC_REGISTER_PARAMETER(ResetArraysParam); | ||
|
||
NNVM_REGISTER_OP(reset_arrays) | ||
.describe(R"code(Set to zero multiple arrays | ||
)code" ADD_FILELINE) | ||
.set_num_inputs([](const nnvm::NodeAttrs& attrs) { | ||
return static_cast<uint32_t>(dmlc::get<ResetArraysParam>(attrs.parsed).num_arrays); | ||
}) | ||
.set_attr<nnvm::FMutateInputs>("FMutateInputs", | ||
[](const nnvm::NodeAttrs& attrs) { | ||
const uint32_t num_args = dmlc::get<ResetArraysParam>(attrs.parsed).num_arrays; | ||
std::vector<uint32_t> ret; | ||
for (uint32_t i = 0; i < num_args; ++i) { | ||
ret.push_back(i); | ||
} | ||
return ret; | ||
}) | ||
.set_num_outputs(0) | ||
.set_attr_parser(ParamParser<ResetArraysParam>) | ||
.set_attr<mxnet::FInferShape>("FInferShape", ResetArraysShape) | ||
.set_attr<nnvm::FInferType>("FInferType", ResetArraysType) | ||
.set_attr<nnvm::FListInputNames>("FListInputNames", | ||
[](const NodeAttrs& attrs) { | ||
const uint32_t num_args = dmlc::get<ResetArraysParam>(attrs.parsed).num_arrays; | ||
std::vector<std::string> ret; | ||
for (uint32_t i = 0; i < num_args; ++i) { | ||
ret.push_back(std::string("array_") + std::to_string(i)); | ||
} | ||
return ret; | ||
}) | ||
.add_argument("data", "NDArray-or-Symbol[]", "Arrays") | ||
.add_arguments(ResetArraysParam::__FIELDS__()); | ||
|
||
NNVM_REGISTER_OP(reset_arrays) | ||
.set_attr<FCompute>("FCompute<cpu>", ResetArrays<cpu>); | ||
|
||
template<> | ||
void ResetMemory<cpu>(void *pntr, size_t len, mshadow::Stream<cpu> *s) { | ||
memset(pntr, 0, len); | ||
} | ||
|
||
} // namespace op | ||
} // namespace mxnet |
This file contains hidden or 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) 2019 by Contributors | ||
* \file reset_arrays.cu | ||
* \brief setting all array element values to zeros | ||
* \author Moises Hernandez-Fernandez, Andrei Ivanov | ||
*/ | ||
#include "./reset_arrays-inl.h" | ||
|
||
namespace mxnet { | ||
namespace op { | ||
|
||
template<> | ||
void ResetMemory<gpu>(void *pntr, size_t len, mshadow::Stream<gpu> *s) { | ||
CUDA_CALL(cudaMemsetAsync(pntr, 0, len, mshadow::Stream<gpu>::GetStream(s))); | ||
} | ||
|
||
NNVM_REGISTER_OP(reset_arrays) | ||
.set_attr<FCompute>("FCompute<gpu>", ResetArrays<gpu>); | ||
|
||
} // namespace op | ||
} // namespace mxnet |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.