cavis/libnd4j/include/ops/declarable/helpers/cpu/clip.cpp

209 lines
8.2 KiB
C++
Raw Normal View History

2021-02-01 13:31:45 +01:00
/* ******************************************************************************
*
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
2021-02-01 13:31:45 +01:00
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
* 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.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/
//
// @author Yurii Shyrma (iuriish@yahoo.com)
// @author sgazeos@gmail.com
// @author raver119@gmail.com
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
//
#include <ops/declarable/helpers/transforms.h>
#include <execution/Threads.h>
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
namespace sd {
namespace ops {
namespace helpers {
//////////////////////////////////////////////////////////////////////////
void clipByNorm(sd::LaunchContext* context, NDArray& input, NDArray& output, const std::vector<int>& dimensions, const NDArray& clipNorm, const bool isInplace, const bool useAverage) {
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
NDArray* z = nullptr;
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
if(isInplace) {
z = &input;
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
}
else {
output.assign(input);
z = &output;
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
}
if(dimensions.empty()) {
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
const NDArray actualNorm = useAverage ? z->reduceAlongDimension(reduce::Norm2, {}) / z->lengthOf() : z->reduceAlongDimension(reduce::Norm2, {});
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
if(actualNorm.e<float>(0) > clipNorm.e<float>(0))
*z *= clipNorm / actualNorm;
}
else {
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
auto listOfSubArrs = z->allTensorsAlongDimension(dimensions);
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
auto func = PRAGMA_THREADS_FOR {
for (auto i = start; i < stop; i++) {
const NDArray actualNorm = useAverage ? listOfSubArrs.at(i)->reduceAlongDimension(reduce::Norm2, {}) / listOfSubArrs.at(i)->lengthOf() : listOfSubArrs.at(i)->reduceAlongDimension(reduce::Norm2, {});
if(actualNorm.e<float>(0) > clipNorm.e<float>(0))
*listOfSubArrs.at(i) *= clipNorm / actualNorm;
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
}
};
samediff::Threads::parallel_tad(func, 0, listOfSubArrs.size());
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
}
}
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
//////////////////////////////////////////////////////////////////////////
template<typename T>
static void clipByNormBp_(const NDArray& input, const NDArray& gradO, NDArray& gradI, const std::vector<int>& dimensions, const NDArray& clipNorm, const bool useAverage) {
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
const int rank = input.rankOf();
auto norm2 = input.reduceAlongDimension(reduce::Norm2, dimensions);
auto sums = input.reduceAlongDimension(reduce::Sum, dimensions);
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
if(norm2.lengthOf() == 1) {
const T norm = useAverage ? norm2.e<T>(0) / input.lengthOf() : norm2.e<T>(0);
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
auto clipVal = clipNorm.e<T>(0);
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
if(norm > clipVal) {
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
const T sum = sums.e<T>(0); // reduce to scalar
const T factor1 = clipVal / norm;
const T factor2 = static_cast<T>(1.f) / (norm * norm); // 1 / (norm*norm*norm)
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
auto lambda = LAMBDA_TT(x, y, sum, factor1, factor2) {
return factor1 * y * (static_cast<T>(1.f) - factor2 * x * sum);
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
};
const_cast<NDArray&>(input).applyPairwiseLambda<T>(const_cast<NDArray&>(gradO), lambda, gradI);
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
}
else
gradI.assign(gradO);
}
else {
auto gradISubArrs = gradI.allTensorsAlongDimension({dimensions});
auto gradOSubArrs = gradO.allTensorsAlongDimension({dimensions});
auto inputSubArrs = input.allTensorsAlongDimension({dimensions});
auto clipVal = clipNorm.e<T>(0);
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
auto func = PRAGMA_THREADS_FOR {
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
for (auto i = start; i < stop; i++) {
auto gradOSubArr = gradOSubArrs.at(i);
auto gradISubArr = gradISubArrs.at(i);
const T norm = useAverage ? norm2.e<T>(i) / gradISubArr->lengthOf() : norm2.e<T>(i);
if (norm > clipVal) {
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
auto inputSubArr = inputSubArrs.at(i);
const T sum = sums.e<T>(i); // reduce to scalar
const T factor1 = clipVal / norm;
const T factor2 = static_cast<T>(1.f) / (norm * norm); // 1 / (norm*norm*norm)
auto lambda = LAMBDA_TT(x, y, sum, factor1, factor2) {
return factor1 * y * (static_cast<T>(1.f) - factor2 * x * sum);
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
};
inputSubArr->applyPairwiseLambda<T>(*gradOSubArr, lambda, *gradISubArr);
}
else
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
gradISubArr->assign(gradOSubArr);
}
};
samediff::Threads::parallel_tad(func, 0, gradISubArrs.size());
}
}
BUILD_SINGLE_TEMPLATE(template void clipByNormBp_, (const NDArray& input, const NDArray& gradO, NDArray& gradI, const std::vector<int>& dimensions, const NDArray& clipNorm, const bool useAverage), FLOAT_TYPES);
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
//////////////////////////////////////////////////////////////////////////
void clipByNormBp(sd::LaunchContext* context, const NDArray& input, const NDArray& gradO, NDArray& gradI, const std::vector<int>& dimensions, const NDArray& clipNorm, const bool useAverage) {
const NDArray& castedInput = gradI.dataType() == input.dataType() ? input : input.cast(gradI.dataType());
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
BUILD_SINGLE_SELECTOR(gradI.dataType(), clipByNormBp_, (castedInput, gradO, gradI, dimensions, clipNorm, useAverage), FLOAT_TYPES);
}
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
template <typename T>
static void clipByGlobalNorm_(std::vector<NDArray*> const& inputs, double clipNorm, sd::memory::Workspace* workspace, std::vector<NDArray*>& outputs, bool isInplace) {
T globalNorm = 0; //NDArrayFactory::create<T>(0, inputs[0]->getContext()); //sqrt(sum([l2norm(t)**2 for t in t_list]))
// PRAGMA_OMP_PARALLEL_FOR_SIMD_REDUCTION(sumT : globalNorm)
for (size_t i = 0; i < inputs.size(); i++) {
auto input = inputs[i];
auto l2norm = input->reduceNumber(reduce::Norm2);
globalNorm += l2norm.t<T>(0) * l2norm.t<T>(0);
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
}
//globalNorm.applyTransform(transform::Sqrt, nullptr, nullptr);// = sd::math::nd4j_sqrt(globalNorm);
auto normS = sd::math::nd4j_sqrt<T,T>(globalNorm);
outputs[inputs.size()]->p(0, normS);
const T factor = clipNorm / normS;
// PRAGMA_OMP_PARALLEL_FOR
for (size_t e = 0; e < inputs.size(); e++) {
// all-reduce
auto input = inputs[e];
auto output = outputs[e];
if (normS <= clipNorm) {
output->assign(input);
}
else {
auto lambda = LAMBDA_T(_x, factor) { return _x * factor; };
input->applyLambda<T>(lambda, *output);
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
}
}
}
void clipByGlobalNorm(sd::LaunchContext * context, std::vector<NDArray*> const& inputs, double clipNorm, sd::memory::Workspace* workspace, std::vector<NDArray*>& outputs, bool isInplace) {
BUILD_SINGLE_SELECTOR(outputs[0]->dataType(), clipByGlobalNorm_, (inputs, clipNorm, workspace, outputs, isInplace), FLOAT_TYPES);
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
}
BUILD_SINGLE_TEMPLATE(template void clipByGlobalNorm_, (std::vector<NDArray*> const& inputs, double clipNorm, sd::memory::Workspace* workspace, std::vector<NDArray*>& outputs, bool isInplace), FLOAT_TYPES);
Backpropagation implementation of mergemax, mergeadd and mergeavg ops (#343) * libnd4j: first step of merge_max implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed typos Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some corrections for mergeMaxBp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j test added for mergemax_bp Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j fixed several problems tests added, check with gradCheck Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j remove duplicated tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j split implementation of transforms ops into separate file implementation Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j code clean up, added mergeavg_bp and mergeadd_bp, need testing Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j merge master, fixed typos and added tests Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some minor fixes Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j added helper for mergeAddBp operation, this permits to skip nullify Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j file renaming changes and cuda some corrections, need some additional corrections Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j some additional corrections for merge ops Signed-off-by: Oleg <oleg.semeniv@gmail.com> * libnd4j more corrections per request for cuda more proper usage Signed-off-by: Oleg <oleg.semeniv@gmail.com>
2020-03-25 06:40:30 +01:00
template <typename T>
static void clipByValue_(NDArray& input, double leftBound, double rightBound, NDArray& output) {
auto routine = LAMBDA_T(_x, leftBound, rightBound) {
if (_x > rightBound) return rightBound;
if (_x < leftBound) return leftBound;
return _x;
};
input.applyLambda<T>(routine, output);
}
void clipByValue(sd::LaunchContext * context, NDArray& input, double leftBound, double rightBound, NDArray& output) {
BUILD_SINGLE_SELECTOR(input.dataType(), clipByValue_, (input, leftBound, rightBound, output), FLOAT_TYPES);
}
BUILD_SINGLE_TEMPLATE(template void clipByValue_, (NDArray& input, double leftBound, double rightBound, NDArray& output);, FLOAT_TYPES);
}
}
}