1 /** 2 * Copyright 2019-2023 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINDSPORE_CORE_IR_TENSOR_STORAGE_INFO_H_ 18 #define MINDSPORE_CORE_IR_TENSOR_STORAGE_INFO_H_ 19 20 #include <memory> 21 #include <vector> 22 #include <string> 23 #include <utility> 24 #include <sstream> 25 #include "mindapi/base/type_id.h" 26 27 namespace mindspore { 28 struct TensorStorageInfo { 29 public: TensorStorageInfoTensorStorageInfo30 TensorStorageInfo(std::vector<int64_t> new_shape, std::vector<int64_t> new_strides, std::vector<int64_t> ori_shape, 31 std::vector<int64_t> ori_strides, bool is_contiguous) 32 : shape(std::move(new_shape)), 33 strides(std::move(new_strides)), 34 ori_shape(std::move(ori_shape)), 35 ori_strides(std::move(ori_strides)), 36 is_contiguous(is_contiguous) {} TensorStorageInfoTensorStorageInfo37 TensorStorageInfo(std::vector<int64_t> new_shape, std::vector<int64_t> new_strides, size_t new_storage_offset, 38 std::vector<int64_t> ori_shape, std::vector<int64_t> ori_strides, bool is_contiguous) 39 : shape(std::move(new_shape)), 40 strides(std::move(new_strides)), 41 storage_offset(std::move(new_storage_offset)), 42 ori_shape(std::move(ori_shape)), 43 ori_strides(std::move(ori_strides)), 44 is_contiguous(is_contiguous) {} 45 46 template <typename T> VectorToStringTensorStorageInfo47 std::string VectorToString(const std::vector<T> &values) { 48 std::stringstream ss; 49 ss << "["; 50 auto size = values.size(); 51 for (size_t i = 0; i < size; ++i) { 52 ss << values[i]; 53 if (i != size - 1) { 54 ss << ", "; 55 } 56 } 57 ss << "]"; 58 return ss.str(); 59 } 60 ToStringTensorStorageInfo61 std::string ToString() { 62 std::stringstream buf; 63 buf << "TensorStorageInfo(shape=" << VectorToString(shape); 64 buf << " strides=" << VectorToString(strides); 65 buf << " storage_offset=" << std::to_string(storage_offset); 66 buf << " ori_shape=" << VectorToString(ori_shape); 67 buf << " ori_strides=" << VectorToString(ori_strides); 68 buf << " is_contiguous=" << std::to_string(is_contiguous); 69 buf << ")"; 70 return buf.str(); 71 } 72 73 std::vector<int64_t> shape; 74 std::vector<int64_t> strides; 75 size_t storage_offset{0}; 76 std::vector<int64_t> ori_shape; 77 std::vector<int64_t> ori_strides; 78 bool is_contiguous{false}; 79 }; 80 using TensorStorageInfoPtr = std::shared_ptr<TensorStorageInfo>; 81 using TensorStorageInfoPtrList = std::vector<TensorStorageInfoPtr>; 82 83 } // namespace mindspore 84 #endif // MINDSPORE_CORE_IR_TENSOR_STORAGE_INFO_H_ 85