[GitHub] [incubator-tvm] wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type
wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type URL: https://github.com/apache/incubator-tvm/pull/4346#discussion_r350577340 ## File path: include/tvm/runtime/container.h ## @@ -0,0 +1,282 @@ +/* + * 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. + */ + +/*! + * \file tvm/runtime/container.h + * \brief Common POD(plain old data) container types. + */ +#ifndef TVM_RUNTIME_CONTAINER_H_ +#define TVM_RUNTIME_CONTAINER_H_ +#include +#include +#include + +#include +#include +#include +#include + +namespace tvm { +namespace runtime { + +/*! + * \brief Base template for classes with array like memory layout. + * + *It provides general methods to access the memory. The memory + *layout is ArrayType + [ElemType]. The alignment of ArrayType + *and ElemType is handled by the memory allocator. + * + * \tparam ArrayType The array header type, contains object specific metadata. + * \tparam ElemType The type of objects stored in the array right after + * ArrayType. + * + * \code + * // Example usage of the template to define a simple array wrapper + * class ArrayObj : public InplaceArrayBase { + * public: + * // Wrap EmplaceInit to initialize the elements + * template + * void Init(Iterator begin, Iterator end) { + * size_t num_elems = std::distance(begin, end); + * auto it = begin; + * for (size_t i = 0; i < num_elems; ++i) { + * InplaceArrayBase::EmplaceInit(i, *it++); + * } + * } + * } + * + * void test_function() { + * vector fields; + * auto ptr = make_inplace_array_object(fields.size()); + * ptr->Init(fields.begin(), fields.end()); + * + * // Access the 0th element in the array. + * assert(ptr->operator[](0) == fields[0]); + * } + * + * \endcode + */ +template +class InplaceArrayBase { + public: + /*! + * \brief Access element at index + * \param idx The index of the element. + * \return Const reference to ElemType at the index. + */ + const ElemType& operator[](size_t idx) const { +size_t size = Self()->size(); +CHECK_LT(idx, size) << "Index " << idx << " out of bounds " << size << "\n"; +return *(reinterpret_cast(AddressOf(idx))); + } + + /*! + * \brief Access element at index + * \param idx The index of the element. + * \return Reference to ElemType at the index. + */ + ElemType& operator[](size_t idx) { +size_t size = Self()->size(); +CHECK_LT(idx, size) << "Index " << idx << " out of bounds " << size << "\n"; +return *(reinterpret_cast(AddressOf(idx))); + } + + /*! + * \brief Destroy the Inplace Array Base object + */ + ~InplaceArrayBase() { +if (!(std::is_standard_layout::value && + std::is_trivial::value)) { + size_t size = Self()->size(); + for (size_t i = 0; i < size; ++i) { +ElemType* fp = reinterpret_cast(AddressOf(i)); +fp->ElemType::~ElemType(); + } +} + } + + protected: + /*! + * \brief Construct a value in place with the arguments. + * + * \tparam Args Type parameters of the arguments. + * \param idx Index of the element. + * \param args Arguments to construct the new value. + */ + template + void EmplaceInit(size_t idx, Args&&... args) { +CHECK_LT(idx, Self()->capacity()) << "InplaceArray out of capacity\n"; +void* field_ptr = AddressOf(idx); +new (field_ptr) ElemType(std::forward(args)...); + } + + private: + /*! + * \brief Return the self object for the array. + * + * \return Pointer to ArrayType. + */ + inline ArrayType* Self() const { +return static_cast(const_cast(this)); + } + + /*! + * \brief Return the raw pointer to the element at idx. + * + * \param idx The index of the element. + * \return Raw pointer to the element. + */ + void* AddressOf(size_t idx) const { +static_assert(alignof(ArrayType) % alignof(ElemType) == 0 && + sizeof(ArrayType) % alignof(ElemType) == 0, + "The size and alignment of ArrayType should respect " + "ElemType's alignment."); + +size_t kDataStart = sizeof(ArrayType); +ArrayType* self = Self(); +char* data_start = reinterpret_cast(self) + kDataStart; +return
[GitHub] [incubator-tvm] wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type
wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type URL: https://github.com/apache/incubator-tvm/pull/4346#discussion_r350525069 ## File path: include/tvm/runtime/container.h ## @@ -0,0 +1,282 @@ +/* + * 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. + */ + +/*! + * \file tvm/runtime/container.h + * \brief Common POD(plain old data) container types. + */ +#ifndef TVM_RUNTIME_CONTAINER_H_ +#define TVM_RUNTIME_CONTAINER_H_ +#include +#include +#include + +#include +#include +#include +#include + +namespace tvm { +namespace runtime { + +/*! + * \brief Base template for classes with array like memory layout. + * + *It provides general methods to access the memory. The memory + *layout is ArrayType + [ElemType]. The alignment of ArrayType + *and ElemType is handled by the memory allocator. + * + * \tparam ArrayType The array header type, contains object specific metadata. + * \tparam ElemType The type of objects stored in the array right after + * ArrayType. + * + * \code + * // Example usage of the template to define a simple array wrapper + * class ArrayObj : public InplaceArrayBase { + * public: + * // Wrap EmplaceInit to initialize the elements + * template + * void Init(Iterator begin, Iterator end) { + * size_t num_elems = std::distance(begin, end); + * auto it = begin; + * for (size_t i = 0; i < num_elems; ++i) { + * InplaceArrayBase::EmplaceInit(i, *it++); + * } + * } + * } + * + * void test_function() { + * vector fields; + * auto ptr = make_inplace_array_object(fields.size()); + * ptr->Init(fields.begin(), fields.end()); + * + * // Access the 0th element in the array. + * assert(ptr->operator[](0) == fields[0]); + * } + * + * \endcode + */ +template +class InplaceArrayBase { + public: + /*! + * \brief Access element at index + * \param idx The index of the element. + * \return Const reference to ElemType at the index. + */ + const ElemType& operator[](size_t idx) const { +size_t size = Self()->size(); +CHECK_LT(idx, size) << "Index " << idx << " out of bounds " << size << "\n"; +return *(reinterpret_cast(AddressOf(idx))); + } + + /*! + * \brief Access element at index + * \param idx The index of the element. + * \return Reference to ElemType at the index. + */ + ElemType& operator[](size_t idx) { +size_t size = Self()->size(); +CHECK_LT(idx, size) << "Index " << idx << " out of bounds " << size << "\n"; +return *(reinterpret_cast(AddressOf(idx))); + } + + /*! + * \brief Destroy the Inplace Array Base object + */ + ~InplaceArrayBase() { +if (!(std::is_standard_layout::value && + std::is_trivial::value)) { + size_t size = Self()->size(); + for (size_t i = 0; i < size; ++i) { +ElemType* fp = reinterpret_cast(AddressOf(i)); +fp->ElemType::~ElemType(); + } +} + } + + protected: + /*! + * \brief Construct a value in place with the arguments. + * + * \tparam Args Type parameters of the arguments. + * \param idx Index of the element. + * \param args Arguments to construct the new value. + */ + template + void EmplaceInit(size_t idx, Args&&... args) { +CHECK_LT(idx, Self()->capacity()) << "InplaceArray out of capacity\n"; +void* field_ptr = AddressOf(idx); Review comment: OK, please ignore the second comment, you mentioned to check the capacity in child class. 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: us...@infra.apache.org With regards, Apache Git Services
[GitHub] [incubator-tvm] wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type
wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type URL: https://github.com/apache/incubator-tvm/pull/4346#discussion_r350524105 ## File path: include/tvm/runtime/container.h ## @@ -0,0 +1,282 @@ +/* + * 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. + */ + +/*! + * \file tvm/runtime/container.h + * \brief Common POD(plain old data) container types. + */ +#ifndef TVM_RUNTIME_CONTAINER_H_ +#define TVM_RUNTIME_CONTAINER_H_ +#include +#include +#include + +#include +#include +#include +#include + +namespace tvm { +namespace runtime { + +/*! + * \brief Base template for classes with array like memory layout. + * + *It provides general methods to access the memory. The memory + *layout is ArrayType + [ElemType]. The alignment of ArrayType + *and ElemType is handled by the memory allocator. + * + * \tparam ArrayType The array header type, contains object specific metadata. + * \tparam ElemType The type of objects stored in the array right after + * ArrayType. + * + * \code + * // Example usage of the template to define a simple array wrapper + * class ArrayObj : public InplaceArrayBase { + * public: + * // Wrap EmplaceInit to initialize the elements + * template + * void Init(Iterator begin, Iterator end) { + * size_t num_elems = std::distance(begin, end); + * auto it = begin; + * for (size_t i = 0; i < num_elems; ++i) { + * InplaceArrayBase::EmplaceInit(i, *it++); + * } + * } + * } + * + * void test_function() { + * vector fields; + * auto ptr = make_inplace_array_object(fields.size()); + * ptr->Init(fields.begin(), fields.end()); + * + * // Access the 0th element in the array. + * assert(ptr->operator[](0) == fields[0]); + * } + * + * \endcode + */ +template +class InplaceArrayBase { + public: + /*! + * \brief Access element at index + * \param idx The index of the element. + * \return Const reference to ElemType at the index. + */ + const ElemType& operator[](size_t idx) const { +size_t size = Self()->size(); +CHECK_LT(idx, size) << "Index " << idx << " out of bounds " << size << "\n"; +return *(reinterpret_cast(AddressOf(idx))); + } + + /*! + * \brief Access element at index + * \param idx The index of the element. + * \return Reference to ElemType at the index. + */ + ElemType& operator[](size_t idx) { +size_t size = Self()->size(); +CHECK_LT(idx, size) << "Index " << idx << " out of bounds " << size << "\n"; +return *(reinterpret_cast(AddressOf(idx))); + } + + /*! + * \brief Destroy the Inplace Array Base object + */ + ~InplaceArrayBase() { +if (!(std::is_standard_layout::value && + std::is_trivial::value)) { + size_t size = Self()->size(); + for (size_t i = 0; i < size; ++i) { +ElemType* fp = reinterpret_cast(AddressOf(i)); +fp->ElemType::~ElemType(); + } +} + } + + protected: + /*! + * \brief Construct a value in place with the arguments. + * + * \tparam Args Type parameters of the arguments. + * \param idx Index of the element. + * \param args Arguments to construct the new value. + */ + template + void EmplaceInit(size_t idx, Args&&... args) { +CHECK_LT(idx, Self()->capacity()) << "InplaceArray out of capacity\n"; +void* field_ptr = AddressOf(idx); Review comment: Also another interesting issue(bug) I found is that for ADT, we have to have capacity field in the object definition, otherwise it's impossible to have `size` and `capacity` at the same time while maintain exception safety. Right now we only have `size` field, it's used in both `size` and `capacity`. It's fine without exception. But under exception, we will try to destruct object that's not initialized anyway. 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: us...@infra.apache.org With regards, Apache Git Services
[GitHub] [incubator-tvm] wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type
wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type URL: https://github.com/apache/incubator-tvm/pull/4346#discussion_r350522281 ## File path: include/tvm/runtime/container.h ## @@ -0,0 +1,282 @@ +/* + * 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. + */ + +/*! + * \file tvm/runtime/container.h + * \brief Common POD(plain old data) container types. + */ +#ifndef TVM_RUNTIME_CONTAINER_H_ +#define TVM_RUNTIME_CONTAINER_H_ +#include +#include +#include + +#include +#include +#include +#include + +namespace tvm { +namespace runtime { + +/*! + * \brief Base template for classes with array like memory layout. + * + *It provides general methods to access the memory. The memory + *layout is ArrayType + [ElemType]. The alignment of ArrayType + *and ElemType is handled by the memory allocator. + * + * \tparam ArrayType The array header type, contains object specific metadata. + * \tparam ElemType The type of objects stored in the array right after + * ArrayType. + * + * \code + * // Example usage of the template to define a simple array wrapper + * class ArrayObj : public InplaceArrayBase { + * public: + * // Wrap EmplaceInit to initialize the elements + * template + * void Init(Iterator begin, Iterator end) { + * size_t num_elems = std::distance(begin, end); + * auto it = begin; + * for (size_t i = 0; i < num_elems; ++i) { + * InplaceArrayBase::EmplaceInit(i, *it++); + * } + * } + * } + * + * void test_function() { + * vector fields; + * auto ptr = make_inplace_array_object(fields.size()); + * ptr->Init(fields.begin(), fields.end()); + * + * // Access the 0th element in the array. + * assert(ptr->operator[](0) == fields[0]); + * } + * + * \endcode + */ +template +class InplaceArrayBase { + public: + /*! + * \brief Access element at index + * \param idx The index of the element. + * \return Const reference to ElemType at the index. + */ + const ElemType& operator[](size_t idx) const { +size_t size = Self()->size(); +CHECK_LT(idx, size) << "Index " << idx << " out of bounds " << size << "\n"; +return *(reinterpret_cast(AddressOf(idx))); + } + + /*! + * \brief Access element at index + * \param idx The index of the element. + * \return Reference to ElemType at the index. + */ + ElemType& operator[](size_t idx) { +size_t size = Self()->size(); +CHECK_LT(idx, size) << "Index " << idx << " out of bounds " << size << "\n"; +return *(reinterpret_cast(AddressOf(idx))); + } + + /*! + * \brief Destroy the Inplace Array Base object + */ + ~InplaceArrayBase() { +if (!(std::is_standard_layout::value && + std::is_trivial::value)) { + size_t size = Self()->size(); + for (size_t i = 0; i < size; ++i) { +ElemType* fp = reinterpret_cast(AddressOf(i)); +fp->ElemType::~ElemType(); + } +} + } + + protected: + /*! + * \brief Construct a value in place with the arguments. + * + * \tparam Args Type parameters of the arguments. + * \param idx Index of the element. + * \param args Arguments to construct the new value. + */ + template + void EmplaceInit(size_t idx, Args&&... args) { +CHECK_LT(idx, Self()->capacity()) << "InplaceArray out of capacity\n"; +void* field_ptr = AddressOf(idx); Review comment: I agree there is exception safety issue here. You solution is like my previous version, `InplaceArrayBase` calls childclass's `size` and `set_size` to manage the size, so under exception case, it will only destructs elements that are initialized. Another solution is to delegate the responsibility to child class, the child class increment the size one by one while calling `EmplaceInit`. The reason I prefer the second solution a bit is that `EmplacePushBack` is essentially `emplace_back`, which makes the template semantically look like a mutable object( I guess it's against the goal to put `push_back`, `assign`, `empalce_back` in child class). `EmplaceInit` is clearer that it's only used for initialization, the object itself is still immutable. What do you think?
[GitHub] [incubator-tvm] wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type
wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type URL: https://github.com/apache/incubator-tvm/pull/4346#discussion_r349940924 ## File path: include/tvm/runtime/memory.h ## @@ -124,11 +145,69 @@ class SimpleObjAllocator : }; }; +// Array allocator that uses new/delete. +class ArrayObjAllocator : + public ObjAllocatorBase { + public: + template + class Handler { Review comment: makes sense. 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: us...@infra.apache.org With regards, Apache Git Services
[GitHub] [incubator-tvm] wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type
wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type URL: https://github.com/apache/incubator-tvm/pull/4346#discussion_r349931640 ## File path: src/runtime/vm/vm.cc ## @@ -921,7 +923,7 @@ void VirtualMachine::RunLoop() { CHECK(tuple != nullptr) << "Object is not data type object, register " << instr.object << ", Object tag " << object->type_index(); -auto field = tuple->fields[instr.field_index]; +auto field = (*tuple)[instr.field_index]; Review comment: done. 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: us...@infra.apache.org With regards, Apache Git Services
[GitHub] [incubator-tvm] wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type
wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type URL: https://github.com/apache/incubator-tvm/pull/4346#discussion_r349930024 ## File path: include/tvm/runtime/container.h ## @@ -0,0 +1,259 @@ +/* + * 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. + */ + +/*! + * \file tvm/runtime/container.h + * \brief Common POD(plain old data) container types. + */ +#ifndef TVM_RUNTIME_CONTAINER_H_ +#define TVM_RUNTIME_CONTAINER_H_ +#include +#include +#include + +#include +#include +#include +#include + +namespace tvm { +namespace runtime { + +/*! + * \brief Base template for classes with array like memory layout. + * + *It provides general methods to access the memory. The memory + *layout is ArrayType + [ElemType]. The alignment of ArrayType + *and ElemType is handled by the memory allocator. + * + * \tparam ArrayType + * \tparam ElemType + */ +template +class InplaceArrayBase { + public: + /*! + * \brief Replace the elements in the array. + * + * \tparam Iterator Iterator type of the array. + * \param begin The begin iterator. + * \param end The end iterator. + */ + template + void assign(Iterator begin, Iterator end) { +size_t num_elems = std::distance(begin, end); +auto it = begin; +for (size_t i = 0; i < num_elems; ++i) { + void* field_ptr = AddressOf(i); + new (field_ptr) ElemType(*it); + ++it; +} +Self()->set_size(num_elems); + } + + /*! + * \brief Replace the elements in the array. + * + * \param init The initializer list of elements. + */ + void assign(std::initializer_list init) { +assign(init.begin(), init.end()); + } + + /*! + * \brief Access element at index + * \param idx The index of the element. + * \return Const reference to ElemType at the index. + */ + const ElemType& operator[](size_t idx) const { +size_t size = Self()->size(); +CHECK_LT(idx, size) << "Index " << idx << " out of bounds " << size << "\n"; +return *(reinterpret_cast(AddressOf(idx))); + } + + /*! + * \brief Access element at index + * \param idx The index of the element. + * \return Reference to ElemType at the index. + */ + ElemType& operator[](size_t idx) { return this->operator[](idx); } + + /*! + * \brief Push a value to the end of the array. + * + * \param val The value to be added. + */ + void push_back(const ElemType& val) { +size_t size = Self()->size(); +CHECK_LT(size, Self()->capacity()); +(*this)[size] = val; +Self()->set_size(size + 1); + } + + /*! + * \brief Construct a value in the end of the array. + * + * \tparam Args Type parameters of the arguments. + * \param args Arguments used to construct the new value. + */ + template + void emplace_back(Args&&... args) { +size_t size = Self()->size(); +CHECK_LT(size, Self()->capacity()); +EmplaceInit(size, std::forward(args)...); +Self()->set_size(size + 1); + } + + /*! + * \brief Destroy the Inplace Array Base object + */ + ~InplaceArrayBase() { +if (!IsPOD()) { + size_t size = Self()->size(); + for (size_t i = 0; i < size; ++i) { +ElemType* fp = reinterpret_cast(AddressOf(i)); +fp->ElemType::~ElemType(); + } +} + } + + protected: + /*! + * \brief Construct a value in place with the arguments. + * + * \tparam Args Type parameters of the arguments. + * \param idx Index of the element. + * \param args Arguments to construct the new value. + */ + template + void EmplaceInit(size_t idx, Args&&... args) { +size_t size = Self()->size(); +CHECK_LT(size, Self()->capacity()) << "InplaceArray out of capacity\n"; +void* field_ptr = AddressOf(size); +new (field_ptr) ElemType(std::forward(args)...); +Self()->set_size(size + 1); + } + + private: + /*! + * \brief If the ElemType is Plain Old Data. + */ + inline bool IsPOD() const { +return std::is_standard_layout::value && + std::is_trivial::value; + } + + /*! + * \brief Return the self object for the array. + * + * \return Pointer to ArrayType. + */ + inline ArrayType* Self() const { +return
[GitHub] [incubator-tvm] wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type
wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type URL: https://github.com/apache/incubator-tvm/pull/4346#discussion_r349926988 ## File path: include/tvm/runtime/container.h ## @@ -0,0 +1,259 @@ +/* + * 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. + */ + +/*! + * \file tvm/runtime/container.h + * \brief Common POD(plain old data) container types. + */ +#ifndef TVM_RUNTIME_CONTAINER_H_ +#define TVM_RUNTIME_CONTAINER_H_ +#include +#include +#include + +#include +#include +#include +#include + +namespace tvm { +namespace runtime { + +/*! + * \brief Base template for classes with array like memory layout. + * + *It provides general methods to access the memory. The memory + *layout is ArrayType + [ElemType]. The alignment of ArrayType + *and ElemType is handled by the memory allocator. + * + * \tparam ArrayType + * \tparam ElemType + */ +template +class InplaceArrayBase { + public: + /*! + * \brief Replace the elements in the array. + * + * \tparam Iterator Iterator type of the array. + * \param begin The begin iterator. + * \param end The end iterator. + */ + template + void assign(Iterator begin, Iterator end) { Review comment: done 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: us...@infra.apache.org With regards, Apache Git Services
[GitHub] [incubator-tvm] wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type
wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type URL: https://github.com/apache/incubator-tvm/pull/4346#discussion_r349905963 ## File path: include/tvm/runtime/container.h ## @@ -0,0 +1,259 @@ +/* + * 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. + */ + +/*! + * \file tvm/runtime/container.h + * \brief Common POD(plain old data) container types. + */ +#ifndef TVM_RUNTIME_CONTAINER_H_ +#define TVM_RUNTIME_CONTAINER_H_ +#include +#include +#include + +#include +#include +#include +#include + +namespace tvm { +namespace runtime { + +/*! + * \brief Base template for classes with array like memory layout. + * + *It provides general methods to access the memory. The memory + *layout is ArrayType + [ElemType]. The alignment of ArrayType + *and ElemType is handled by the memory allocator. + * + * \tparam ArrayType + * \tparam ElemType + */ +template +class InplaceArrayBase { + public: + /*! + * \brief Replace the elements in the array. + * + * \tparam Iterator Iterator type of the array. + * \param begin The begin iterator. + * \param end The end iterator. + */ + template + void assign(Iterator begin, Iterator end) { +size_t num_elems = std::distance(begin, end); +auto it = begin; +for (size_t i = 0; i < num_elems; ++i) { + void* field_ptr = AddressOf(i); + new (field_ptr) ElemType(*it); + ++it; +} +Self()->set_size(num_elems); + } + + /*! + * \brief Replace the elements in the array. + * + * \param init The initializer list of elements. + */ + void assign(std::initializer_list init) { Review comment: done 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: us...@infra.apache.org With regards, Apache Git Services
[GitHub] [incubator-tvm] wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type
wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type URL: https://github.com/apache/incubator-tvm/pull/4346#discussion_r349905954 ## File path: include/tvm/runtime/memory.h ## @@ -73,6 +74,26 @@ class ObjAllocatorBase { ptr->deleter_ = Handler::Deleter(); return ObjectPtr(ptr); } + + /*! + * \tparam ArrayType The type to be allocated. + * \tparam ElemType The type of array element. + * \tparam Args The constructor signature. + * \param num_elems The number of array elements. + * \param args The arguments. + */ + template + inline ObjectPtr make_inplace_array(size_t num_elems, Args&&... args) { +using Handler = typename Derived::template Handler; +static_assert(std::is_base_of::value, + "make_node can only be used to create NodeBase"); Review comment: Good catch! fixed. 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: us...@infra.apache.org With regards, Apache Git Services
[GitHub] [incubator-tvm] wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type
wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type URL: https://github.com/apache/incubator-tvm/pull/4346#discussion_r349905959 ## File path: src/runtime/container.cc ## @@ -0,0 +1,37 @@ +/* + * 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. + */ +/* + * \file src/runtime/container.cc + * \brief POD container type implementations. + */ +#include +#include + +#include + +#include "object_internal.h" +#include "runtime_base.h" + +namespace tvm { +namespace runtime { + +ADT ADT::Tuple(std::vector fields) { return ADT(0, fields); } Review comment: done 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: us...@infra.apache.org With regards, Apache Git Services
[GitHub] [incubator-tvm] wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type
wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type URL: https://github.com/apache/incubator-tvm/pull/4346#discussion_r349905324 ## File path: include/tvm/runtime/container.h ## @@ -0,0 +1,259 @@ +/* + * 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. + */ + +/*! + * \file tvm/runtime/container.h + * \brief Common POD(plain old data) container types. + */ +#ifndef TVM_RUNTIME_CONTAINER_H_ +#define TVM_RUNTIME_CONTAINER_H_ +#include +#include +#include + +#include +#include +#include +#include + +namespace tvm { +namespace runtime { + +/*! + * \brief Base template for classes with array like memory layout. + * + *It provides general methods to access the memory. The memory + *layout is ArrayType + [ElemType]. The alignment of ArrayType + *and ElemType is handled by the memory allocator. + * + * \tparam ArrayType + * \tparam ElemType + */ +template +class InplaceArrayBase { + public: + /*! + * \brief Replace the elements in the array. + * + * \tparam Iterator Iterator type of the array. + * \param begin The begin iterator. + * \param end The end iterator. + */ + template + void assign(Iterator begin, Iterator end) { +size_t num_elems = std::distance(begin, end); +auto it = begin; +for (size_t i = 0; i < num_elems; ++i) { + void* field_ptr = AddressOf(i); + new (field_ptr) ElemType(*it); + ++it; +} +Self()->set_size(num_elems); + } + + /*! + * \brief Replace the elements in the array. + * + * \param init The initializer list of elements. + */ + void assign(std::initializer_list init) { +assign(init.begin(), init.end()); + } + + /*! + * \brief Access element at index + * \param idx The index of the element. + * \return Const reference to ElemType at the index. + */ + const ElemType& operator[](size_t idx) const { +size_t size = Self()->size(); +CHECK_LT(idx, size) << "Index " << idx << " out of bounds " << size << "\n"; +return *(reinterpret_cast(AddressOf(idx))); + } + + /*! + * \brief Access element at index + * \param idx The index of the element. + * \return Reference to ElemType at the index. + */ + ElemType& operator[](size_t idx) { return this->operator[](idx); } + + /*! + * \brief Push a value to the end of the array. + * + * \param val The value to be added. + */ + void push_back(const ElemType& val) { +size_t size = Self()->size(); +CHECK_LT(size, Self()->capacity()); +(*this)[size] = val; +Self()->set_size(size + 1); + } + + /*! + * \brief Construct a value in the end of the array. + * + * \tparam Args Type parameters of the arguments. + * \param args Arguments used to construct the new value. + */ + template + void emplace_back(Args&&... args) { +size_t size = Self()->size(); +CHECK_LT(size, Self()->capacity()); +EmplaceInit(size, std::forward(args)...); +Self()->set_size(size + 1); + } + + /*! + * \brief Destroy the Inplace Array Base object + */ + ~InplaceArrayBase() { +if (!IsPOD()) { + size_t size = Self()->size(); + for (size_t i = 0; i < size; ++i) { +ElemType* fp = reinterpret_cast(AddressOf(i)); +fp->ElemType::~ElemType(); + } +} + } + + protected: + /*! + * \brief Construct a value in place with the arguments. + * + * \tparam Args Type parameters of the arguments. + * \param idx Index of the element. + * \param args Arguments to construct the new value. + */ + template + void EmplaceInit(size_t idx, Args&&... args) { +size_t size = Self()->size(); +CHECK_LT(size, Self()->capacity()) << "InplaceArray out of capacity\n"; +void* field_ptr = AddressOf(size); +new (field_ptr) ElemType(std::forward(args)...); +Self()->set_size(size + 1); + } + + private: + /*! + * \brief If the ElemType is Plain Old Data. + */ + inline bool IsPOD() const { +return std::is_standard_layout::value && + std::is_trivial::value; Review comment: `is_pod` will be [deprecated](https://en.cppreference.com/w/cpp/types/is_pod) in c++20, some
[GitHub] [incubator-tvm] wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type
wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type URL: https://github.com/apache/incubator-tvm/pull/4346#discussion_r347595585 ## File path: include/tvm/runtime/container.h ## @@ -0,0 +1,220 @@ +/* + * 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. + */ + +/*! + * \file tvm/runtime/container.h + * \brief Common POD(plain old data) container types. + */ +#ifndef TVM_RUNTIME_CONTAINER_H_ +#define TVM_RUNTIME_CONTAINER_H_ +#include +#include +#include +#include + +namespace tvm { +namespace runtime { + +/** + * @brief Base template for classes with array like memory layout. + * + *It provides general methods to access the memory. The memory + *layout is ArrayType + [ElemType]. The alignment of ArrayType + *and ElemType is handled by the memory allocator. + * + * @tparam ArrayType + * @tparam ElemType + */ +template +class InplaceArrayBase { + public: + /** + * @brief Initialize the elements in the array. + */ + void Init() { Review comment: Oh, I was not aware of this use case. I think it would be great if we can just provide `std::vector` interface(`operator[]`, `push_back`, `size`) in `InplaceArrayBase`. To support dynamic insertion, it can query child class with `size()` and `capacity()` in `push_back` implementation. And I won't support dynamically grow the underlying storage since the actual memory management is done by child class, it's also difficult to grow this memory. 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: us...@infra.apache.org With regards, Apache Git Services
[GitHub] [incubator-tvm] wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type
wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type URL: https://github.com/apache/incubator-tvm/pull/4346#discussion_r347109138 ## File path: src/runtime/container.cc ## @@ -0,0 +1,54 @@ +/* + * 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. + */ +/* + * \file src/runtime/container.cc + * \brief POD container type implementations. + */ +#include +#include +#include +#include "object_internal.h" +#include "runtime_base.h" + +namespace tvm { +namespace runtime { + +ObjectRef* ADTObj::fields() const { + if (this->size_ <= 0) { +return nullptr; + } + auto p = reinterpret_cast(const_cast(this)) + 2; + return reinterpret_cast(p); +} + +ADT::ADT(uint32_t tag, std::vector fields) { + auto ptr = make_object_with_extra_space(fields.size() * sizeof(ObjectRef)); + ptr->tag_ = tag; + ptr->size_ = fields.size(); + for (size_t i = 0; i < fields.size(); ++i) { +ptr->fields()[i] = fields[i]; Review comment: Will do. Seems like we should also delete the assignment operator of `ObjectRef` so we can get compile time error early. 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: us...@infra.apache.org With regards, Apache Git Services
[GitHub] [incubator-tvm] wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type
wweic commented on a change in pull request #4346: [Runtime] Make ADTObject POD container type URL: https://github.com/apache/incubator-tvm/pull/4346#discussion_r347072383 ## File path: include/tvm/runtime/memory.h ## @@ -104,10 +124,22 @@ class SimpleObjAllocator : return reinterpret_cast(data); } +template +static T* NewWithExtraSpace(SimpleObjAllocator*, size_t extra, Args&&... args) { + // TODO(wweic): use aligned_alloc in the future + void* data = std::malloc(sizeof(T) + extra); Review comment: @tqchen `fields.size()` is unknown until runtime, so `aligned_storage` won't work(it requires the size of memory to be compile time constant) I think. I agree with alignment, that's why I left a note of using `aligned_alloc`. But our c++ support seems to be c++11. `aligned_alloc` is available after c++17. We can have a simple implementation of `aligned_alloc` and switch to it later when we have c++17. What do you think? 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: us...@infra.apache.org With regards, Apache Git Services