From 07ff396d27c2c8559cfee90166431d3b9f1e734a Mon Sep 17 00:00:00 2001 From: wuxun-zhang Date: Wed, 22 May 2019 16:12:12 +0800 Subject: [PATCH 1/8] fix type inconsistent when using C++ API to load params file --- cpp-package/include/mxnet-cpp/ndarray.h | 5 +++-- cpp-package/include/mxnet-cpp/ndarray.hpp | 8 ++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/cpp-package/include/mxnet-cpp/ndarray.h b/cpp-package/include/mxnet-cpp/ndarray.h index 6f37d91aa68e..2482facce8c5 100644 --- a/cpp-package/include/mxnet-cpp/ndarray.h +++ b/cpp-package/include/mxnet-cpp/ndarray.h @@ -131,11 +131,12 @@ class NDArray { /*! * \brief construct a new dynamic NDArray * \param shape the shape of array - * \param constext context of NDArray + * \param context context of NDArray * \param delay_alloc whether delay the allocation + * \param dtype data type of NDArray */ NDArray(const std::vector &shape, const Context &context, - bool delay_alloc = true); + bool delay_alloc = true, int dtype = 0); /*! * \brief construct a new dynamic NDArray * \param shape the shape of array diff --git a/cpp-package/include/mxnet-cpp/ndarray.hpp b/cpp-package/include/mxnet-cpp/ndarray.hpp index d0438305a62e..f96307f414ef 100644 --- a/cpp-package/include/mxnet-cpp/ndarray.hpp +++ b/cpp-package/include/mxnet-cpp/ndarray.hpp @@ -47,10 +47,10 @@ inline NDArray::NDArray(const NDArrayHandle &handle) { blob_ptr_ = std::make_shared(handle); } inline NDArray::NDArray(const std::vector &shape, const Context &context, - bool delay_alloc) { + bool delay_alloc, int dtype) { NDArrayHandle handle; - CHECK_EQ(MXNDArrayCreate(shape.data(), shape.size(), context.GetDeviceType(), - context.GetDeviceId(), delay_alloc, &handle), + CHECK_EQ(MXNDArrayCreateEx(shape.data(), shape.size(), context.GetDeviceType(), + context.GetDeviceId(), delay_alloc, dtype, &handle), 0); blob_ptr_ = std::make_shared(handle); } @@ -208,7 +208,7 @@ inline void NDArray::SyncCopyToCPU(std::vector *data, size_t size) { MXNDArraySyncCopyToCPU(blob_ptr_->handle_, data->data(), size); } inline NDArray NDArray::Copy(const Context &ctx) const { - NDArray ret(GetShape(), ctx); + NDArray ret(GetShape(), ctx, true, this->GetDType()); Operator("_copyto")(*this).Invoke(ret); return ret; } From 383ef718a1572863eabc8956a33e51179246fc3a Mon Sep 17 00:00:00 2001 From: wuxun-zhang Date: Thu, 23 May 2019 10:24:46 +0800 Subject: [PATCH 2/8] add test case --- cpp-package/example/test_ndarray.cpp | 56 +++++++++++++++++++++++ cpp-package/include/mxnet-cpp/ndarray.h | 3 +- cpp-package/include/mxnet-cpp/ndarray.hpp | 7 +-- cpp-package/tests/ci_test.sh | 3 ++ 4 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 cpp-package/example/test_ndarray.cpp diff --git a/cpp-package/example/test_ndarray.cpp b/cpp-package/example/test_ndarray.cpp new file mode 100644 index 000000000000..5b85b4fe13e1 --- /dev/null +++ b/cpp-package/example/test_ndarray.cpp @@ -0,0 +1,56 @@ +/* + * 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. + * + * The file is used for testing if there exist type inconsistency + * when using Copy API to create a new NDArray. + * By running: build/test_ndarray. + */ +#include +#include "mxnet/c_api.h" +#include "dmlc/logging.h" +#include "mxnet-cpp/MxNetCpp.h" +using namespace mxnet::cpp; + +enum TypeFlag { + kFloat32 = 0, + kFloat16 = 1, + kInt8 = 2 +}; + +int main(int argc, char** argv){ + std::vector shape1{128, 2, 32}; + Shape shape2(32, 8, 64); + + int gpu_count = 0; + if(MXGetGPUCount(&gpu_count) != 0){ + LOG(ERROR) << "MXGetGPUCount failed"; + return -1; + } + + Context context = (gpu_count > 0) ? Context::gpu() : Context::cpu(); + + NDArray src1(shape1, context, true, kFloat16); + NDArray src2(shape2, context, false, kInt8); + NDArray dst1, dst2; + dst1 = src1.Copy(context); + dst2 = src2.Copy(context); + + CHECK_EQ(src1.GetDType(), dst1.GetDType()); + CHECK_EQ(src2.GetDType(), dst2.GetDType()); + return 0; +} \ No newline at end of file diff --git a/cpp-package/include/mxnet-cpp/ndarray.h b/cpp-package/include/mxnet-cpp/ndarray.h index 2482facce8c5..3eea2e7ab41c 100644 --- a/cpp-package/include/mxnet-cpp/ndarray.h +++ b/cpp-package/include/mxnet-cpp/ndarray.h @@ -143,7 +143,8 @@ class NDArray { * \param constext context of NDArray * \param delay_alloc whether delay the allocation */ - NDArray(const Shape &shape, const Context &context, bool delay_alloc = true); + NDArray(const Shape &shape, const Context &context, + bool delay_alloc = true, int dtype = 0); NDArray(const mx_float *data, size_t size); /*! * \brief construct a new dynamic NDArray diff --git a/cpp-package/include/mxnet-cpp/ndarray.hpp b/cpp-package/include/mxnet-cpp/ndarray.hpp index f96307f414ef..09524b202ce0 100644 --- a/cpp-package/include/mxnet-cpp/ndarray.hpp +++ b/cpp-package/include/mxnet-cpp/ndarray.hpp @@ -54,10 +54,11 @@ inline NDArray::NDArray(const std::vector &shape, const Context &contex 0); blob_ptr_ = std::make_shared(handle); } -inline NDArray::NDArray(const Shape &shape, const Context &context, bool delay_alloc) { +inline NDArray::NDArray(const Shape &shape, const Context &context, + bool delay_alloc, int dtype) { NDArrayHandle handle; - CHECK_EQ(MXNDArrayCreate(shape.data(), shape.ndim(), context.GetDeviceType(), - context.GetDeviceId(), delay_alloc, &handle), + CHECK_EQ(MXNDArrayCreateEx(shape.data(), shape.ndim(), context.GetDeviceType(), + context.GetDeviceId(), delay_alloc, dtype, &handle), 0); blob_ptr_ = std::make_shared(handle); } diff --git a/cpp-package/tests/ci_test.sh b/cpp-package/tests/ci_test.sh index 2d1f8e4f68e6..9311e378cd94 100755 --- a/cpp-package/tests/ci_test.sh +++ b/cpp-package/tests/ci_test.sh @@ -57,6 +57,9 @@ cp ../../build/cpp-package/example/test_kvstore . cp ../../build/cpp-package/example/test_score . ./test_score 0.93 +cp ../../build/cpp-package/example/test_ndarray . +./test_ndarray + sh unittests/unit_test_mlp_csv.sh cd inference From 8b73ab955bd0b385919abb607e458926284955e7 Mon Sep 17 00:00:00 2001 From: wuxun-zhang Date: Thu, 23 May 2019 10:58:10 +0800 Subject: [PATCH 3/8] fix cpplint --- cpp-package/example/test_ndarray.cpp | 17 +++++++++-------- cpp-package/include/mxnet-cpp/ndarray.h | 3 ++- cpp-package/include/mxnet-cpp/ndarray.hpp | 2 +- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/cpp-package/example/test_ndarray.cpp b/cpp-package/example/test_ndarray.cpp index 5b85b4fe13e1..ec23265b3363 100644 --- a/cpp-package/example/test_ndarray.cpp +++ b/cpp-package/example/test_ndarray.cpp @@ -16,9 +16,6 @@ * specific language governing permissions and limitations * under the License. * - * The file is used for testing if there exist type inconsistency - * when using Copy API to create a new NDArray. - * By running: build/test_ndarray. */ #include #include "mxnet/c_api.h" @@ -32,25 +29,29 @@ enum TypeFlag { kInt8 = 2 }; -int main(int argc, char** argv){ +/* + * The file is used for testing if there exist type inconsistency + * when using Copy API to create a new NDArray. + * By running: build/test_ndarray. + */ +int main(int argc, char** argv) { std::vector shape1{128, 2, 32}; Shape shape2(32, 8, 64); int gpu_count = 0; - if(MXGetGPUCount(&gpu_count) != 0){ + if (MXGetGPUCount(&gpu_count) != 0) { LOG(ERROR) << "MXGetGPUCount failed"; return -1; } Context context = (gpu_count > 0) ? Context::gpu() : Context::cpu(); - + NDArray src1(shape1, context, true, kFloat16); NDArray src2(shape2, context, false, kInt8); NDArray dst1, dst2; dst1 = src1.Copy(context); dst2 = src2.Copy(context); - CHECK_EQ(src1.GetDType(), dst1.GetDType()); CHECK_EQ(src2.GetDType(), dst2.GetDType()); return 0; -} \ No newline at end of file +} diff --git a/cpp-package/include/mxnet-cpp/ndarray.h b/cpp-package/include/mxnet-cpp/ndarray.h index 3eea2e7ab41c..7953b61393fe 100644 --- a/cpp-package/include/mxnet-cpp/ndarray.h +++ b/cpp-package/include/mxnet-cpp/ndarray.h @@ -142,8 +142,9 @@ class NDArray { * \param shape the shape of array * \param constext context of NDArray * \param delay_alloc whether delay the allocation + * \param dtype data type of NDArray */ - NDArray(const Shape &shape, const Context &context, + NDArray(const Shape &shape, const Context &context, bool delay_alloc = true, int dtype = 0); NDArray(const mx_float *data, size_t size); /*! diff --git a/cpp-package/include/mxnet-cpp/ndarray.hpp b/cpp-package/include/mxnet-cpp/ndarray.hpp index 09524b202ce0..8ab26df4b076 100644 --- a/cpp-package/include/mxnet-cpp/ndarray.hpp +++ b/cpp-package/include/mxnet-cpp/ndarray.hpp @@ -54,7 +54,7 @@ inline NDArray::NDArray(const std::vector &shape, const Context &contex 0); blob_ptr_ = std::make_shared(handle); } -inline NDArray::NDArray(const Shape &shape, const Context &context, +inline NDArray::NDArray(const Shape &shape, const Context &context, bool delay_alloc, int dtype) { NDArrayHandle handle; CHECK_EQ(MXNDArrayCreateEx(shape.data(), shape.ndim(), context.GetDeviceType(), From 18d8478f0cbf4b3a3bdfd3649f03d0fa24d17cce Mon Sep 17 00:00:00 2001 From: wuxun-zhang Date: Thu, 23 May 2019 15:44:43 +0800 Subject: [PATCH 4/8] address comment --- cpp-package/example/test_ndarray.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/cpp-package/example/test_ndarray.cpp b/cpp-package/example/test_ndarray.cpp index ec23265b3363..b01be4047790 100644 --- a/cpp-package/example/test_ndarray.cpp +++ b/cpp-package/example/test_ndarray.cpp @@ -51,6 +51,7 @@ int main(int argc, char** argv) { NDArray dst1, dst2; dst1 = src1.Copy(context); dst2 = src2.Copy(context); + NDArray::WaitAll(); CHECK_EQ(src1.GetDType(), dst1.GetDType()); CHECK_EQ(src2.GetDType(), dst2.GetDType()); return 0; From 6e94e892cf1f2b4a97bd38ec3040e618258a6751 Mon Sep 17 00:00:00 2001 From: wuxun-zhang Date: Thu, 23 May 2019 18:48:37 +0800 Subject: [PATCH 5/8] retrigger CI From 4aec21a12f958b8b1b289892d49ea5895bc3858f Mon Sep 17 00:00:00 2001 From: wuxun-zhang Date: Fri, 24 May 2019 08:55:05 +0800 Subject: [PATCH 6/8] fix comments --- .../example/{test_ndarray.cpp => test_ndarray_copy.cpp} | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) rename cpp-package/example/{test_ndarray.cpp => test_ndarray_copy.cpp} (92%) diff --git a/cpp-package/example/test_ndarray.cpp b/cpp-package/example/test_ndarray_copy.cpp similarity index 92% rename from cpp-package/example/test_ndarray.cpp rename to cpp-package/example/test_ndarray_copy.cpp index b01be4047790..a3b3011993fa 100644 --- a/cpp-package/example/test_ndarray.cpp +++ b/cpp-package/example/test_ndarray_copy.cpp @@ -25,8 +25,12 @@ using namespace mxnet::cpp; enum TypeFlag { kFloat32 = 0, - kFloat16 = 1, - kInt8 = 2 + kFloat64 = 1, + kFloat16 = 2, + kUint8 = 3, + kInt32 = 4, + kInt8 = 5, + kInt64 = 6, }; /* From f01f931b8d623cf66cd21270b870fccea24b30af Mon Sep 17 00:00:00 2001 From: wuxun-zhang Date: Fri, 24 May 2019 09:00:25 +0800 Subject: [PATCH 7/8] modify ci_test --- cpp-package/tests/ci_test.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cpp-package/tests/ci_test.sh b/cpp-package/tests/ci_test.sh index 9311e378cd94..ef7fceacfd6e 100755 --- a/cpp-package/tests/ci_test.sh +++ b/cpp-package/tests/ci_test.sh @@ -57,8 +57,8 @@ cp ../../build/cpp-package/example/test_kvstore . cp ../../build/cpp-package/example/test_score . ./test_score 0.93 -cp ../../build/cpp-package/example/test_ndarray . -./test_ndarray +cp ../../build/cpp-package/example/test_ndarray_copy . +./test_ndarray_copy sh unittests/unit_test_mlp_csv.sh From 427f83de5390cee62241a95703abcc47011242e6 Mon Sep 17 00:00:00 2001 From: wuxun-zhang Date: Fri, 24 May 2019 14:06:49 +0800 Subject: [PATCH 8/8] fix indentation --- cpp-package/include/mxnet-cpp/ndarray.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cpp-package/include/mxnet-cpp/ndarray.hpp b/cpp-package/include/mxnet-cpp/ndarray.hpp index 8ab26df4b076..283fff1a3b92 100644 --- a/cpp-package/include/mxnet-cpp/ndarray.hpp +++ b/cpp-package/include/mxnet-cpp/ndarray.hpp @@ -50,7 +50,7 @@ inline NDArray::NDArray(const std::vector &shape, const Context &contex bool delay_alloc, int dtype) { NDArrayHandle handle; CHECK_EQ(MXNDArrayCreateEx(shape.data(), shape.size(), context.GetDeviceType(), - context.GetDeviceId(), delay_alloc, dtype, &handle), + context.GetDeviceId(), delay_alloc, dtype, &handle), 0); blob_ptr_ = std::make_shared(handle); } @@ -58,7 +58,7 @@ inline NDArray::NDArray(const Shape &shape, const Context &context, bool delay_alloc, int dtype) { NDArrayHandle handle; CHECK_EQ(MXNDArrayCreateEx(shape.data(), shape.ndim(), context.GetDeviceType(), - context.GetDeviceId(), delay_alloc, dtype, &handle), + context.GetDeviceId(), delay_alloc, dtype, &handle), 0); blob_ptr_ = std::make_shared(handle); }