diff --git a/mindspore/lite/internal/CMakeLists.txt b/mindspore/lite/internal/CMakeLists.txt index bf1d374309..b1d24d7548 100644 --- a/mindspore/lite/internal/CMakeLists.txt +++ b/mindspore/lite/internal/CMakeLists.txt @@ -27,6 +27,7 @@ set(CCSRC ${CMAKE_CURRENT_SOURCE_DIR}/src/lite_session.cc ${CMAKE_CURRENT_SOURCE_DIR}/src/ms_tensor.cc ${CMAKE_CURRENT_SOURCE_DIR}/src/common/string.cc + ${CMAKE_CURRENT_SOURCE_DIR}/src/common/vector.cc ${TOP_DIR}/src/common/log_adapter.cc ${CMAKE_CURRENT_SOURCE_DIR}/../../core/gvar/logging_level.cc ${TOP_DIR}/src/runtime/allocator.cc diff --git a/mindspore/lite/internal/include/lite_utils.h b/mindspore/lite/internal/include/lite_utils.h index e2eac49f54..35e95d5360 100644 --- a/mindspore/lite/internal/include/lite_utils.h +++ b/mindspore/lite/internal/include/lite_utils.h @@ -16,17 +16,17 @@ #ifndef MINDSPORE_LITE_INTERNAL_INCLUDE_LITE_UTILS_H_ #define MINDSPORE_LITE_INTERNAL_INCLUDE_LITE_UTILS_H_ -#include #include "internal/include/string.h" +#include "internal/include/vector.h" struct MSTensor; struct Node; using TensorPtr = MSTensor *; -using TensorPtrVector = std::vector; -using Uint32Vector = std::vector; -using StringVector = std::vector; -using ShapeVector = std::vector; -using NodePtrVector = std::vector; -using Int32Vector = std::vector; -using Int32VectorVector = std::vector; +using TensorPtrVector = Vector; +using Uint32Vector = Vector; +using StringVector = Vector; +using ShapeVector = Vector; +using NodePtrVector = Vector; +using Int32Vector = Vector; +using Int32VectorVector = Vector; #endif // MINDSPORE_LITE_INCLUDE_LITE_UTILS_H_ diff --git a/mindspore/lite/internal/include/vector.h b/mindspore/lite/internal/include/vector.h new file mode 100644 index 0000000000..bb0cbc9020 --- /dev/null +++ b/mindspore/lite/internal/include/vector.h @@ -0,0 +1,109 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + * + * Licensed 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. + */ +#ifndef MINDSPORE_LITE_INTERNAL_INCLUDE_VECTOR_H +#define MINDSPORE_LITE_INTERNAL_INCLUDE_VECTOR_H + +#include +#include "internal/include/string.h" +#define DEFAULT_CAPACITY 1 + +struct MSTensor; +struct Node; + +template +class Vector { + private: + size_t size_; + size_t elem_size_; + size_t capacity_; + T *data_; + + public: + Vector(); + + explicit Vector(size_t size); + + Vector(const Vector &vector); + + ~Vector(); + + void clear(); + + void push_back(const T &elem); + + void pop_back(); + + void insert(const T &elem, size_t index); + + T *begin(); + + const T *begin() const; + + T *end(); + + const T *end() const; + + T &front(); + + const T &front() const; + + T &back(); + + const T &back() const; + + T &at(size_t index); + + const T &at(size_t index) const; + + T &operator[](size_t index); + + const T &operator[](size_t index) const; + + T *data(); + + const T *data() const; + + size_t size() const; + + size_t capacity() const; + + bool empty() const; + + void erase(size_t index); + + void resize(size_t size); + + void reserve(size_t capacity); +}; + +template +bool operator==(const Vector &lhs, const Vector &rhs) { + if (lhs.size() != rhs.size()) { + return false; + } + for (int i = 0; i < lhs.size(); ++i) { + if (lhs[i] != rhs[i]) { + return false; + } + } + return true; +} + +template +bool operator!=(const Vector &lhs, const Vector &rhs) { + return !(lhs == rhs); +} +#endif // MINDSPORE_LITE_INTERNAL_INCLUDE_VECTOR_H diff --git a/mindspore/lite/internal/src/common/vector.cc b/mindspore/lite/internal/src/common/vector.cc new file mode 100644 index 0000000000..1196d04d93 --- /dev/null +++ b/mindspore/lite/internal/src/common/vector.cc @@ -0,0 +1,259 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + * + * Licensed 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. + */ +#include "internal/include/vector.h" +#include +#include +#include "internal/src/lite_log.h" + +#define min(x, y) ((x < y) ? (x) : (y)) + +template +Vector::Vector() { + size_ = 0; + capacity_ = DEFAULT_CAPACITY; + elem_size_ = sizeof(T); + data_ = nullptr; +} + +template +Vector::Vector(size_t size) { + size_ = size; + elem_size_ = sizeof(T); + capacity_ = size; + data_ = reinterpret_cast(malloc(capacity_ * elem_size_)); + if (data_ == nullptr) { + MS_C_EXCEPTION("malloc data failed"); + } + memset(data_, 0, capacity_ * elem_size_); +} + +template +Vector::Vector(const Vector &vec) { + size_ = vec.size_; + elem_size_ = sizeof(T); + capacity_ = vec.capacity_; + data_ = reinterpret_cast(malloc(capacity_ * elem_size_)); + if (data_ == nullptr) { + MS_C_EXCEPTION("malloc data failed"); + } + memcpy(data_, vec.data_, size_ * elem_size_); +} + +template +Vector::~Vector() { + if (data_) { + free(data_); + } +} + +template +void Vector::clear() { + size_ = 0; + if (data_) { + free(data_); + data_ = nullptr; + } +} + +template +void Vector::push_back(const T &elem) { + if (data_ == nullptr) { + data_ = reinterpret_cast(malloc(capacity_ * elem_size_)); + if (data_ == nullptr) { + MS_C_EXCEPTION("malloc data failed"); + } + } else if (size_ == capacity_) { + resize(size_ + 1); + --size_; + } + memcpy(data_ + size_, &elem, elem_size_); + ++size_; +} + +template +void Vector::pop_back() { + if (size_ > 0) { + --size_; + } else { + MS_C_EXCEPTION("Index is out of range!"); + } +} + +template +void Vector::insert(const T &elem, size_t index) { + if (index <= size_) { + ++size_; + if (size_ > capacity_) { + resize(size_); + } + if (index == size_ - 1) { + push_back(elem); + } else { + memmove(data_ + index + 1, data_ + index, (size_ - index - 1) * elem_size_); + memcpy(data_ + index, &elem, elem_size_); + } + } else { + MS_C_EXCEPTION("Input index is out of range!"); + } +} + +template +T *Vector::begin() { + return data_; +} + +template +const T *Vector::begin() const { + return data_; +} + +template +T *Vector::end() { + return data_ + size_; +} + +template +const T *Vector::end() const { + return data_ + size_; +} + +template +T &Vector::front() { + if (size_ > 0) { + return *data_; + } + MS_C_EXCEPTION("Index is out of range!"); +} + +template +const T &Vector::front() const { + if (size_ > 0) { + return *data_; + } + MS_C_EXCEPTION("Index is out of range!"); +} +template +T &Vector::back() { + if (size_ > 0) { + return *(data_ + size_ - 1); + } + MS_C_EXCEPTION("Index is out of range!"); +} +template +const T &Vector::back() const { + if (size_ > 0) { + return *(data_ + size_ - 1); + } + MS_C_EXCEPTION("Index is out of range!"); +} + +template +T &Vector::at(size_t index) { + if (index < size_) { + return *(data_ + index); + } + MS_C_EXCEPTION("Input index is out of range!"); +} + +template +const T &Vector::at(size_t index) const { + if (index < size_) { + return *(data_ + index); + } + MS_C_EXCEPTION("Input index is out of range!"); +} + +template +T &Vector::operator[](size_t index) { + if (index < size_) { + return *(data_ + index); + } + MS_C_EXCEPTION("Input index is out of range!"); +} + +template +const T &Vector::operator[](size_t index) const { + if (index < size_) { + return *(data_ + index); + } + MS_C_EXCEPTION("Input index is out of range!"); +} + +template +T *Vector::data() { + return data_; +} + +template +const T *Vector::data() const { + return data_; +} + +template +size_t Vector::size() const { + return size_; +} + +template +size_t Vector::capacity() const { + return capacity_; +} + +template +bool Vector::empty() const { + return size_ == 0; +} + +template +void Vector::erase(size_t index) { + if (index == size_ - 1) { + --size_; + } else if (index < size_) { + memmove(data_ + index, data_ + index + 1, (size_ - index - 1) * elem_size_); + --size_; + } else { + MS_C_EXCEPTION("Input index is out of range!"); + } +} + +template +void Vector::resize(size_t size) { + if (size > capacity_) { + capacity_ *= 2; + } + T *tmp = data_; + data_ = reinterpret_cast(malloc(capacity_ * elem_size_)); + if (data_ == nullptr) { + MS_C_EXCEPTION("malloc data failed"); + } + memcpy(data_, tmp, min(size, size_) * elem_size_); + size_ = size; + free(tmp); +} + +template +void Vector::reserve(size_t capacity) { + if (capacity > capacity_) { + capacity_ = capacity; + } +} + +template class Vector; +template class Vector>; +template class Vector; +template class Vector; +template class Vector; +template class Vector; diff --git a/mindspore/lite/internal/src/kernel/fp32/matmul.cc b/mindspore/lite/internal/src/kernel/fp32/matmul.cc index 2e5c1e1795..038f76e4fe 100644 --- a/mindspore/lite/internal/src/kernel/fp32/matmul.cc +++ b/mindspore/lite/internal/src/kernel/fp32/matmul.cc @@ -125,10 +125,10 @@ int DoMatMul(const TensorPtrVector &in_tensors, const TensorPtrVector &out_tenso return RET_PARAM_INVALID; } int batch = 1; - std::vector a_shape = in_tensors[0]->shape_; - std::vector c_shape = out_tensors[0]->shape_; + ShapeVector a_shape = in_tensors[0]->shape_; + ShapeVector c_shape = out_tensors[0]->shape_; if (in_tensors.size() == 3) { - std::vector bias_shape = in_tensors[2]->shape_; + ShapeVector bias_shape = in_tensors[2]->shape_; if (bias_shape[bias_shape.size() - 1] != c_shape[c_shape.size() - 1]) { LITE_ERROR_LOG("The bias' dimension %d is not equal with column %d", bias_shape[bias_shape.size() - 1], c_shape[c_shape.size() - 1]); diff --git a/mindspore/lite/internal/src/lite_log.h b/mindspore/lite/internal/src/lite_log.h index 0bcf9132ca..d8fcdae594 100644 --- a/mindspore/lite/internal/src/lite_log.h +++ b/mindspore/lite/internal/src/lite_log.h @@ -16,6 +16,8 @@ #ifndef MINDSPORE_LITE_INTERNAL_SRC_LITE_LOG_H_ #define MINDSPORE_LITE_INTERNAL_SRC_LITE_LOG_H_ + +#include #ifdef DEBUG #include #endif diff --git a/mindspore/lite/internal/src/ms_tensor.cc b/mindspore/lite/internal/src/ms_tensor.cc index 8f2ab66174..8671000624 100644 --- a/mindspore/lite/internal/src/ms_tensor.cc +++ b/mindspore/lite/internal/src/ms_tensor.cc @@ -14,8 +14,8 @@ * limitations under the License. */ #include -#include -#include +#include "internal/include/string.h" +#include "internal/include/vector.h" #include "internal/include/ms_tensor.h" MSTensor *CreateTensor(TypeId data_type, const ShapeVector &shape) { MSTensor *tensor = new (std::nothrow) MSTensor();