1 /** 2 * Copyright 2019 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_CCSRC_FRONTEND_PARALLEL_TENSOR_LAYOUT_TENSOR_INFO_H_ 18 #define MINDSPORE_CCSRC_FRONTEND_PARALLEL_TENSOR_LAYOUT_TENSOR_INFO_H_ 19 20 #include <cstdint> 21 #include <string> 22 #include <utility> 23 #include <vector> 24 25 #include "frontend/parallel/device_matrix.h" 26 #include "frontend/parallel/status.h" 27 #include "frontend/parallel/tensor_layout/tensor_layout.h" 28 29 namespace mindspore { 30 namespace parallel { 31 using Shapes = std::vector<Shape>; 32 33 class TensorInfo { 34 public: TensorInfo(const TensorLayout & tensor_layout,Shape shape,Shape slice_shape)35 TensorInfo(const TensorLayout &tensor_layout, Shape shape, Shape slice_shape) 36 : tensor_layout_(tensor_layout), shape_(std::move(shape)), slice_shape_(std::move(slice_shape)) {} TensorInfo(const TensorLayout & tensor_layout)37 explicit TensorInfo(const TensorLayout &tensor_layout) : tensor_layout_(tensor_layout) { 38 shape_ = tensor_layout.tensor_shape().array(); 39 slice_shape_ = tensor_layout.slice_shape().array(); 40 } 41 // trivial default constructor will not initialize c language types. 42 TensorInfo() = default; 43 ~TensorInfo() = default; tensor_layout()44 TensorLayout tensor_layout() const { return tensor_layout_; } slice_shape()45 Shape slice_shape() const { return slice_shape_; } shape()46 Shape shape() const { return shape_; } set_reduce_dim(const std::vector<int64_t> & dim)47 void set_reduce_dim(const std::vector<int64_t> &dim) { reduce_dim_ = dim; } reduce_dim()48 std::vector<int64_t> reduce_dim() const { return reduce_dim_; } InferStrategy()49 Dimensions InferStrategy() const { 50 Dimensions stra; 51 for (size_t i = 0; i < shape_.size(); ++i) { 52 if ((slice_shape_[i] == 0) || (shape_[i] % slice_shape_[i] != 0)) { 53 return stra; 54 } 55 int64_t dim = (int64_t)(shape_[i] / slice_shape_[i]); 56 stra.push_back(dim); 57 } 58 return stra; 59 } 60 61 private: 62 TensorLayout tensor_layout_; 63 Shape shape_; 64 Shape slice_shape_; 65 // reduce method's reduce dim 66 std::vector<int64_t> reduce_dim_; 67 }; 68 } // namespace parallel 69 } // namespace mindspore 70 71 #endif // MINDSPORE_CCSRC_FRONTEND_PARALLEL_TENSOR_LAYOUT_TENSOR_INFO_H_ 72