From 004f16389d44970d5a68c3e7afc94249435d970e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zhao=20Wu=20=28Chinese=20Name=3A=20=E5=90=B4=E9=92=8A=29?= Date: Fri, 27 Dec 2019 23:49:57 +0800 Subject: [PATCH] [Runtime] add necessary const qualifier for NDArray container of parameters (#4590) --- include/tvm/runtime/c_runtime_api.h | 2 +- include/tvm/runtime/ndarray.h | 14 +++++++------- src/runtime/ndarray.cc | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/include/tvm/runtime/c_runtime_api.h b/include/tvm/runtime/c_runtime_api.h index dda2a98dac22..8cb86bf725bc 100644 --- a/include/tvm/runtime/c_runtime_api.h +++ b/include/tvm/runtime/c_runtime_api.h @@ -136,7 +136,7 @@ typedef DLDataType TVMType; typedef DLContext TVMContext; /*! - * \brief The tensor array stucture to TVM API. + * \brief The tensor array structure to TVM API. */ typedef DLTensor TVMArray; diff --git a/include/tvm/runtime/ndarray.h b/include/tvm/runtime/ndarray.h index ceb05568ebea..0dac45808e52 100644 --- a/include/tvm/runtime/ndarray.h +++ b/include/tvm/runtime/ndarray.h @@ -118,7 +118,7 @@ class NDArray { * \note The copy may happen asynchrously if it involves a GPU context. * TVMSynchronize is necessary. */ - inline void CopyFrom(DLTensor* other); + inline void CopyFrom(const DLTensor* other); inline void CopyFrom(const NDArray& other); /*! * \brief Copy data content into another array. @@ -188,7 +188,7 @@ class NDArray { * \param stream The stream used in copy. */ TVM_DLL static void CopyFromTo( - DLTensor* from, DLTensor* to, TVMStreamHandle stream = nullptr); + const DLTensor* from, DLTensor* to, TVMStreamHandle stream = nullptr); TVM_DLL std::vector Shape() const; @@ -228,7 +228,7 @@ struct array_type_info { * \param strm The outpu stream * \param tensor The tensor to be saved. */ -inline bool SaveDLTensor(dmlc::Stream* strm, DLTensor* tensor); +inline bool SaveDLTensor(dmlc::Stream* strm, const DLTensor* tensor); /*! * \brief Reference counted Container object used to back NDArray. @@ -369,7 +369,7 @@ inline size_t GetDataSize(const DLTensor& arr) { return size; } -inline void NDArray::CopyFrom(DLTensor* other) { +inline void NDArray::CopyFrom(const DLTensor* other) { CHECK(data_ != nullptr); CopyFromTo(other, &(data_->dl_tensor)); } @@ -413,7 +413,7 @@ inline const DLTensor* NDArray::operator->() const { constexpr uint64_t kTVMNDArrayMagic = 0xDD5E40F096B4A13F; inline bool SaveDLTensor(dmlc::Stream* strm, - DLTensor* tensor) { + const DLTensor* tensor) { uint64_t header = kTVMNDArrayMagic, reserved = 0; strm->Write(header); strm->Write(reserved); @@ -451,7 +451,7 @@ inline bool SaveDLTensor(dmlc::Stream* strm, } else { std::vector bytes(data_byte_size); CHECK_EQ(TVMArrayCopyToBytes( - tensor, dmlc::BeginPtr(bytes), data_byte_size), 0) + const_cast(tensor), dmlc::BeginPtr(bytes), data_byte_size), 0) << TVMGetLastError(); if (!DMLC_IO_NO_ENDIAN_SWAP) { dmlc::ByteSwap(dmlc::BeginPtr(bytes), type_bytes, num_elems); @@ -462,7 +462,7 @@ inline bool SaveDLTensor(dmlc::Stream* strm, } inline void NDArray::Save(dmlc::Stream* strm) const { - SaveDLTensor(strm, const_cast(operator->())); + SaveDLTensor(strm, operator->()); } inline bool NDArray::Load(dmlc::Stream* strm) { diff --git a/src/runtime/ndarray.cc b/src/runtime/ndarray.cc index 9d2d53e03eb8..760d27d3c8fb 100644 --- a/src/runtime/ndarray.cc +++ b/src/runtime/ndarray.cc @@ -160,7 +160,7 @@ NDArray NDArray::FromDLPack(DLManagedTensor* tensor) { return NDArray(data); } -void NDArray::CopyFromTo(DLTensor* from, +void NDArray::CopyFromTo(const DLTensor* from, DLTensor* to, TVMStreamHandle stream) { size_t from_size = GetDataSize(*from);