mxnet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] zhanghang1989 commented on a change in pull request #11502: [MXNET-614] Adding Synchronized Batch Normalization
Date Mon, 02 Jul 2018 20:54:29 GMT
zhanghang1989 commented on a change in pull request #11502: [MXNET-614] Adding Synchronized
Batch Normalization
URL: https://github.com/apache/incubator-mxnet/pull/11502#discussion_r199608398
 
 

 ##########
 File path: src/operator/contrib/sync_batch_norm-inl.h
 ##########
 @@ -0,0 +1,562 @@
+/*
+ * 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) 2018 by Contributors
+ * \file sync_batch_norm-inl.h
+ * \brief Synchronized BatchNorm modified from BatchNormV1
+ * \author Hang Zhang
+*/
+#ifndef MXNET_OPERATOR_CONTRIB_SYNC_BATCH_NORM_INL_H_
+#define MXNET_OPERATOR_CONTRIB_SYNC_BATCH_NORM_INL_H_
+
+#include <dmlc/logging.h>
+#include <dmlc/parameter.h>
+#include <mxnet/operator.h>
+# include <condition_variable>
+#include <map>
+#include <vector>
+#include <string>
+#include <utility>
+#include "../operator_common.h"
+#include "../mshadow_op.h"
+
+namespace mxnet {
+namespace op {
+
+namespace syncbatchnorm {
+enum BatchNormOpInputs {kData, kGamma, kBeta};
+enum BatchNormOpOutputs {kOut, kMean, kVar};
+enum BatchNormOpAuxiliary {kMovingMean, kMovingVar};
+enum BatchNormBackResource {kTempSpace};
+}  // namespace syncbatchnorm
+
+struct SyncBatchNormParam : public dmlc::Parameter<SyncBatchNormParam> {
+  float eps;
+  float momentum;
+  bool fix_gamma;
+  bool use_global_stats;
+  bool output_mean_var;
+  int ndev;
+  std::string key;
+  DMLC_DECLARE_PARAMETER(SyncBatchNormParam) {
+    DMLC_DECLARE_FIELD(eps).set_default(1e-3f)
+    .describe("Epsilon to prevent div 0");
+    DMLC_DECLARE_FIELD(momentum).set_default(0.9f)
+    .describe("Momentum for moving average");
+    DMLC_DECLARE_FIELD(fix_gamma).set_default(true)
+    .describe("Fix gamma while training");
+    DMLC_DECLARE_FIELD(use_global_stats).set_default(false)
+    .describe("Whether use global moving statistics instead of local batch-norm. "
+              "This will force change batch-norm into a scale shift operator.");
+    DMLC_DECLARE_FIELD(output_mean_var).set_default(false)
+    .describe("Output All,normal mean and var");
+    DMLC_DECLARE_FIELD(ndev).set_default(1)
+      .describe("The count of GPU devices");
+    DMLC_DECLARE_FIELD(key)
+      .set_default("")
+      .describe("Hash key for synchronization");
+  }
+};
+
+// Modified from https://github.com/brucechin/SharedTensor
+template<class T>
+class SharedND {
+ private:
+  int nDev;
+  T mean;
+  T *data;
+  bool *flag;
+  bool meanReady = false;
+  bool meanInited = false;
+  std::mutex mutex_;
+
+ public:
+  explicit SharedND(int ndev) :nDev(ndev) {
+      flag = new bool[ndev];
+      data = new T[ndev];
+      memset(flag, false, ndev * sizeof(bool));
+  }
+
+  ~SharedND() {
+    delete [] flag;
+    delete [] data;
+  }
+
+  bool Push(T input, int index) {
+    if (flag[index] == false) {
+      data[index] = input;
+      flag[index] = true;
+      return true;
+    } else {
+      return false;
+    }
+  }
+
+  T Pop(int index) {
+    std::lock_guard<std::mutex> lock(mutex_);
+    while (!MeanReady()) {}
+    flag[index] = false;
+    T tmp = mean;
+    ResetMean();
+    return tmp;
+  }
+
+  bool MeanReady() {
+    if (meanReady) {
+      return true;
+    }
+    for (int i = 0; i < nDev; i++) {
+      if (!flag[i]) {
+        return false;
+      }
+    }
+    for (int i = 1; i < nDev; i++) {
+      data[0] += data[i];
+    }
+    if (!meanInited) {
+      mean = mshadow::NewTensor<cpu, real_t>(data[0].shape_, 0.0f);
+      meanInited = true;
+    }
+    mean = data[0] * 1.0f /  nDev;
+    meanReady = true;
+    return true;
+  }
+
+  void ResetMean() {
+    for (int i = 0; i < nDev; i++) {
+      if (flag[i]) return;
+    }
+    meanReady = false;
+  }
+};
+
+template<class T>
+class GlobalShared {
+ public:
+  T* Register(const std::string &key, int ndev) {
+    std::lock_guard<std::mutex> lock(mutex_);
+    auto it = registry_.find(key);
+    if (it != registry_.end()) return it->second;
+    T *newT = new T(ndev);
 
 Review comment:
   addressed in the class deconstruction function :)
   https://github.com/zhanghang1989/incubator-mxnet/blob/cc60d11f44e37e954a627c8db43bd1b6fc45e68d/src/operator/contrib/sync_batch_norm-inl.h#L160-L164

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message