ptrendx commented on a change in pull request #19011:
URL: https://github.com/apache/incubator-mxnet/pull/19011#discussion_r486616070



##########
File path: src/operator/subgraph/tensorrt/tensorrt_int8_calibrator.cc
##########
@@ -0,0 +1,149 @@
+/*
+ * 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) 2020 by Contributors
+ * \file tensorrt-inl.h
+ * \brief TensorRT operation registration
+ * \author Serge Panev
+*/
+
+#if MXNET_USE_TENSORRT
+
+#include "./tensorrt_int8_calibrator.h"
+
+#include <atomic>
+#include <unordered_map>
+
+namespace onnx_to_tensorrt {
+
+// set the batch size before constructing the thread to execute engine
+int TRTInt8Calibrator::getBatchSize() const { return batch_size_; }
+
+TRTInt8Calibrator::TRTInt8Calibrator(
+    std::unordered_map<std::string, mxnet::NDArray> params_map,
+    std::unordered_map<std::string, std::pair<void*, size_t>> input_buffers,
+    int batch_size, int n_iter)
+    : batch_size_(batch_size),
+      done_(false),
+      params_map_(params_map),
+      input_buffers_(std::move(input_buffers)),
+      // Make sure setBatch() waits until getBatch() is called (the first 
time).
+      calib_running_(true),
+      batch_is_set_(false),
+      n_iter_(n_iter) {}
+
+bool TRTInt8Calibrator::setBatch(const std::unordered_map<std::string, void*>& 
data,
+                                 const cudaStream_t stream) {
+  std::unique_lock<std::mutex> lk(mutex_);
+  // Wait while the queue is full or calibration is running.
+  cv_.wait(lk, [&]{ return (!calib_running_ && !batch_is_set_) || done_; });
+  if (done_)
+    return false;
+  n_iter_--;
+
+  for (const auto& it : data) {
+    auto in_it = input_buffers_.find(it.first);
+    if (in_it == input_buffers_.end()) {
+      LOG(FATAL) << "TensorRT op input name '" << it.first
+                 << "' does not match with the buffer names";
+    }
+    const auto& buff_and_size = in_it->second;
+    auto status = cudaMemcpyAsync(buff_and_size.first, it.second, 
buff_and_size.second,
+                                  cudaMemcpyDeviceToDevice, stream);
+    if (status != cudaSuccess) {
+      LOG(FATAL) << "cudaMemcpy in  TensorRT op for '" << it.first
+                 << "' failed with " << status;
+    }
+  }
+  // TODO(spanev): see if we can use something like cudaStreamAddCallback here
+  cudaStreamSynchronize(stream);

Review comment:
       This also could return an error.




----------------------------------------------------------------
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]


Reply via email to