/******************************************************************************* * Copyright (c) 2019-2020 Konduit K.K. * * 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. * * 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 Oleh Semeniv (oleg.semeniv@gmail.com) // #include #include #include #include namespace sd { namespace ops { namespace helpers { ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// template static void adamUpdater_(const NDArray& gradient, const NDArray& initStateU, const NDArray& initStateM, NDArray& update, NDArray& stateU, NDArray& stateM, const double dLr, const double dBeta1, const double dBeta2, const double dEpsilon, const int nIteration) { const T* grad = gradient.bufferAsT(); const T* initU = initStateU.bufferAsT(); const T* initM = initStateM.bufferAsT(); T* up = update.bufferAsT(); T* stU = stateU.bufferAsT(); T* stM = stateM.bufferAsT(); const T lr = static_cast(dLr); const T beta1 = static_cast(dBeta1); const T beta2 = static_cast(dBeta2); const T epsilon = static_cast(dEpsilon); const T iteration = static_cast(nIteration); const T beta1T = sd::math::nd4j_pow(beta1, (iteration + 1)); const T beta2T = sd::math::nd4j_pow(beta2, (iteration + 1)); T epsilonT = lr * sd::math::nd4j_sqrt(1. - beta2T) / (1.0 - beta1T); if (sd::math::nd4j_isnan(epsilonT) || 0 == epsilonT || sd::math::nd4j_isinf(epsilonT)) epsilonT = epsilon; bool bEws1 = 1 == gradient.ews() && 1 == update.ews() && 1 == stateM.ews() && 1 == initStateM.ews() && 1 == stateU.ews() && 1 == initStateU.ews(); bool bSameOrdering = gradient.ordering() == update.ordering() && update.ordering() == stateU.ordering() && stateU.ordering() == initStateU.ordering() && stateU.ordering() == initStateM.ordering() && stateM.ordering() == initStateM.ordering(); if (bEws1 && bSameOrdering) { auto func = PRAGMA_THREADS_FOR{ for (auto i = start; i < stop; i++) { stM[i] = beta1 * initM[i] + grad[i] * (1 - beta1); stU[i] = beta2 * initU[i] + grad[i] * grad[i] * (1 - beta2); up[i] = (stM[i] * epsilonT) / (sd::math::nd4j_sqrt(stU[i]) + epsilon); } }; samediff::Threads::parallel_for(func, 0, gradient.lengthOf(), 1); return; } bool bXZsame = shape::haveSameShapeAndStrides(gradient.getShapeInfo(), update.getShapeInfo()); bool bXInVSame = shape::haveSameShapeAndStrides(gradient.getShapeInfo(), initStateU.getShapeInfo()); bool bXStVSame = shape::haveSameShapeAndStrides(gradient.getShapeInfo(), stateU.getShapeInfo()); bool bXInMSame = shape::haveSameShapeAndStrides(gradient.getShapeInfo(), initStateM.getShapeInfo()); bool bXStMSame = shape::haveSameShapeAndStrides(gradient.getShapeInfo(), stateM.getShapeInfo()); auto func = PRAGMA_THREADS_FOR{ int coords[MAX_RANK]; for (auto i = start; i < stop; i++) { shape::index2coordsCPU(start, i, gradient.getShapeInfo(), coords); const auto xOffset = shape::getOffset(gradient.getShapeInfo(), coords); const auto zOffset = bXZsame ? xOffset : shape::getOffset(update.getShapeInfo(), coords); const auto initUOffset = bXInVSame ? xOffset : shape::getOffset(initStateU.getShapeInfo(), coords); const auto stUOffset = bXStVSame ? xOffset : shape::getOffset(stateU.getShapeInfo(), coords); const auto initMOffset = bXInVSame ? xOffset : shape::getOffset(initStateM.getShapeInfo(), coords); const auto stMOffset = bXStMSame ? xOffset : shape::getOffset(stateM.getShapeInfo(), coords); stM[stMOffset] = beta1 * initM[initMOffset] + grad[xOffset] * (1 - beta1); stU[stUOffset] = beta2 * initU[initUOffset] + grad[xOffset] * grad[xOffset] * (1 - beta2); up[zOffset] = (stM[stMOffset] * epsilonT) / (sd::math::nd4j_sqrt(stU[stUOffset]) + epsilon); } }; samediff::Threads::parallel_for(func, 0, gradient.lengthOf(), 1); return; } void updaterAdam(sd::LaunchContext* context, const NDArray& gradient, const NDArray& initStateU, const NDArray& initStateM, NDArray& update, NDArray& stateU, NDArray& stateM, const double dLr, const double dBeta1, const double dBeta2, const double dEpsilon, const int nIteration) { BUILD_SINGLE_SELECTOR(gradient.dataType(), adamUpdater_, (gradient, initStateU, initStateM, update, stateU, stateM, dLr, dBeta1, dBeta2, dEpsilon, nIteration), FLOAT_TYPES); } } } }