1 /**
2 * Copyright 2019-2020 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 #ifndef MINDSPORE_CORE_UTILS_TENSOR_CONSTRUCT_UTILS_H_
17 #define MINDSPORE_CORE_UTILS_TENSOR_CONSTRUCT_UTILS_H_
18 #include <vector>
19 #include <algorithm>
20 #include "ir/tensor.h"
21 #include "ir/dtype/type_id.h"
22 namespace mindspore {
23 template <typename T>
SetTensorData(void * data,const T & num,size_t data_length)24 void SetTensorData(void *data, const T &num, size_t data_length) {
25 MS_EXCEPTION_IF_NULL(data);
26 auto tensor_data = reinterpret_cast<T *>(data);
27 std::fill(tensor_data, tensor_data + data_length, num);
28 }
29 class MS_CORE_API TensorConstructUtils {
30 public:
31 static tensor::TensorPtr CreateZerosTensor(const TypePtr &type, const std::vector<int64_t> &shape);
32 static tensor::TensorPtr CreateOnesTensor(const TypePtr &type, const std::vector<int64_t> &shape,
33 bool skip_exception = false);
34 static tensor::TensorPtr CreateTensor(const TypePtr &type, const std::vector<int64_t> &shape, void *data);
35
36 private:
37 static TypeId ExtractTypeId(const TypePtr &type);
38 };
39 } // namespace mindspore
40 #endif // MINDSPORE_CORE_UTILS_TENSOR_CONSTRUCT_UTILS_H_
41