eric-haibin-lin commented on a change in pull request #16893: Multi-tensor LAMB URL: https://github.com/apache/incubator-mxnet/pull/16893#discussion_r352268600
########## File path: src/operator/contrib/multi_lamb-inl.h ########## @@ -0,0 +1,332 @@ +/* + * 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 multi_lamb-inl.h + * \brief vectorized lars coefficient computed from sums of squared weights and grads + * \author Moises Hernandez + */ +#ifndef MXNET_OPERATOR_CONTRIB_MULTI_LAMB_INL_H_ +#define MXNET_OPERATOR_CONTRIB_MULTI_LAMB_INL_H_ + +#include <dmlc/parameter.h> +#include <mxnet/operator.h> +#include <mxnet/operator_util.h> +#include <mxnet/op_attr_types.h> +#include <mshadow/base.h> +#include <nnvm/op.h> +#include <nnvm/op_attr_types.h> +#include <vector> +#include "../operator_common.h" +#include "../mshadow_op.h" +#include "../mxnet_op.h" +#include "../tensor/init_op.h" +#include "../tensor/util/tensor_util-inl.h" +#include "multi_sum_sq-inl.h" + +namespace mxnet { +namespace op { + +namespace multilamb { +enum MultiLambUpdateResource {kTempSpace}; +} // namespace multilamb + +struct MultiLAMBParam : public dmlc::Parameter<MultiLAMBParam> { + float learning_rate; + float beta1; + float beta2; + float epsilon; + float wd; + float rescale_grad; + float lower_bound; + float upper_bound; + float clip_gradient; + bool bias_correction; + int num_tensors; + mxnet::Tuple<int> step_count; + + DMLC_DECLARE_PARAMETER(MultiLAMBParam) { + DMLC_DECLARE_FIELD(learning_rate) + .set_default(0.001f) + .describe("Learning rate"); + DMLC_DECLARE_FIELD(beta1) + .set_default(0.9f) + .describe("Exponential decay rate for the first moment estimates."); + DMLC_DECLARE_FIELD(beta2) + .set_default(0.999f) + .describe("Exponential decay rate for the second moment estimates."); + DMLC_DECLARE_FIELD(epsilon) + .set_default(1e-6f) + .describe("Small value to avoid division by 0."); + DMLC_DECLARE_FIELD(wd) + .set_default(0.0f) + .describe("Weight decay augments the objective function with a " + "regularization term that penalizes large weights. " + "The penalty scales with the square of the magnitude of each weight."); + DMLC_DECLARE_FIELD(rescale_grad) + .set_default(1.0f) + .describe("Gradient rescaling factor"); + DMLC_DECLARE_FIELD(lower_bound) + .set_default(1e-3f) + .describe("Lower limit of norm of weight."); + DMLC_DECLARE_FIELD(upper_bound) + .set_default(10.0f) + .describe("Upper limit of norm of weight."); + DMLC_DECLARE_FIELD(clip_gradient) + .set_default(-1.0f) + .describe("Clip gradient to the range of [-clip_gradient, clip_gradient] " + "If clip_gradient <= 0, gradient clipping is turned off. " + "grad = max(min(grad, clip_gradient), -clip_gradient)."); + DMLC_DECLARE_FIELD(bias_correction) + .set_default(false) + .describe("Whether to use bias correction."); + DMLC_DECLARE_FIELD(step_count) + .describe("Step count for each tensor"); + DMLC_DECLARE_FIELD(num_tensors) + .set_default(1) + .describe("Number of tensors"); + } +}; + +template<typename ParamType, int input_stride> +inline bool MultiLAMB_InferShape(const nnvm::NodeAttrs& attrs, + mxnet::ShapeVector *in_attrs, + mxnet::ShapeVector *out_attrs) { + const ParamType& param = dmlc::get<ParamType>(attrs.parsed); + CHECK_EQ(in_attrs->size(), input_stride * param.num_tensors); + CHECK_EQ(out_attrs->size(), param.num_tensors); + + bool all_inferred = true; + auto& input_shapes = *in_attrs; + auto& output_shapes = *out_attrs; + + CHECK_EQ(param.step_count.ndim(), param.num_tensors) + << "Number of step counts is inconsistent with num_weights." + << "Expected number of step counts: " + << param.num_tensors << ", and got " << param.step_count.ndim(); + + // Weights, gradients, mean and variance + for (int i = 0; i < param.num_tensors; ++i) { + mxnet::ShapeVector input_vec; + mxnet::ShapeVector output_vec({output_shapes[i]}); + for (int j = 0; j < input_stride; ++j) { + input_vec.push_back(input_shapes[i * input_stride + j]); + } + all_inferred = all_inferred && ElemwiseShape<input_stride, 1>(attrs, &input_vec, &output_vec); + } + return all_inferred; +} + +template <typename ParamType, int input_stride> +inline bool MP_MultiLAMB_InferType(const nnvm::NodeAttrs& attrs, + std::vector<int> *in_attrs, + std::vector<int> *out_attrs) { + const ParamType& param = dmlc::get<ParamType>(attrs.parsed); + CHECK_EQ(in_attrs->size(), input_stride * param.num_tensors); + CHECK_EQ(out_attrs->size(), param.num_tensors); + + bool all_inferred = true; + auto& input_types = *in_attrs; + auto& output_types = *out_attrs; + + // weights, gradients + for (int i = 0; i < param.num_tensors; ++i) { + std::vector<int> input_vec; + std::vector<int> output_vec({output_types[i]}); + for (int j = 0; j < 2; ++j) { + input_vec.push_back(input_types[i * input_stride + j]); + } + all_inferred = all_inferred && + ElemwiseType<2, 1>(attrs, &input_vec, &output_vec); + } + + // mean, var, temp_g, weights32 (master copies of weights) + for (int i = 0; i < param.num_tensors; ++i) { + TYPE_ASSIGN_CHECK(input_types, input_stride * i + 2, mshadow::kFloat32); + TYPE_ASSIGN_CHECK(input_types, input_stride * i + 3, mshadow::kFloat32); + TYPE_ASSIGN_CHECK(input_types, input_stride * i + 4, mshadow::kFloat32); + TYPE_ASSIGN_CHECK(input_types, input_stride * i + input_stride - 1, mshadow::kFloat32); + } + return all_inferred; +} + +template<typename T> +class LAMB_type_identity { + public: + using type = T; +}; + +template<typename T> +class LAMB_single_precision { + public: + using type = float; +}; + +template<typename DType, typename MPDType> +struct MultiLAMBKernelParam { + static const int N = 50; + size_t ntensors; + size_t max_size; + size_t total_size; + size_t sizes[N]; + DType* weights[N]; + DType* grads[N]; + MPDType* mean[N]; + MPDType* var[N]; + MPDType* temp_g[N]; + MPDType* weights32[N]; + DType* out_data[N]; + int step_count[N]; + + // gpu + int chunk_size = 65536; + int nchunks; +}; + +template<typename xpu, + typename DType, + typename MPDType, + typename ParamType = MultiLAMBParam, + int input_stride = 5> +void FillMultiLAMBKernelParam(const nnvm::NodeAttrs& attrs, + const OpContext &ctx, + const std::vector<TBlob> &inputs, + const std::vector<TBlob> &outputs, + MultiLAMBKernelParam<DType, MPDType> *multi_param) { + const ParamType& p = nnvm::get<ParamType>(attrs.parsed); + mxnet_op::Stream<xpu>* s = ctx.get_stream<xpu>(); + + multi_param->ntensors = p.num_tensors; + multi_param->total_size = 0; + multi_param->max_size = 0; + multi_param->nchunks = 0; + + constexpr bool isSame = std::is_same<DType, MPDType>::value; Review comment: isSame -> is_same ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
