forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
97 lines (84 loc) · 3.77 KB
/
Utils.cpp
File metadata and controls
97 lines (84 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved.
//
// 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 <ATen/Utils.h>
#include <ATen/ops/empty.h>
#include <ATen/ops/to.h>
#include <c10/core/Layout.h>
#include <c10/core/ScalarType.h>
#include <c10/util/ArrayRef.h>
#include <c10/util/Exception.h>
#include <c10/util/accumulate.h>
#include <algorithm>
#include "paddle/common/macros.h"
#include "paddle/phi/api/include/sparse_api.h"
#include "paddle/phi/api/include/tensor.h"
namespace at {
namespace detail {
template <typename T>
Tensor tensor_cpu(ArrayRef<T> values, const TensorOptions& options) {
constexpr auto native_scalar_type = c10::CppTypeToScalarType<T>::value;
auto result = at::empty(values.size(), options.dtype(native_scalar_type));
PD_CHECK(result.is_contiguous());
std::copy(values.begin(), values.end(), result.template data_ptr<T>());
if (options.dtype() != native_scalar_type) {
return result.to(at::TensorOptions().dtype(options.dtype()));
}
return result;
}
template <typename T>
Tensor tensor_backend(ArrayRef<T> values, const TensorOptions& options) {
auto cpu_tensor =
tensor_cpu(values, options.device(c10::Device(c10::DeviceType::CPU)));
return cpu_tensor.to(options.device());
}
template <typename T>
Tensor tensor_complex_cpu(ArrayRef<T> values, const TensorOptions& options) {
constexpr auto native_scalar_type = c10::CppTypeToScalarType<T>::value;
auto result = at::empty(values.size(), options.dtype(native_scalar_type));
PD_CHECK(result.is_contiguous());
std::copy(values.begin(), values.end(), result.template data_ptr<T>());
if (options.dtype() != native_scalar_type) {
return result.to(at::TensorOptions().dtype(options.dtype()));
}
return result;
}
template <typename T>
Tensor tensor_complex_backend(ArrayRef<T> values,
const TensorOptions& options) {
auto cpu_tensor = tensor_complex_cpu(
values, options.device(c10::Device(c10::DeviceType::CPU)));
return cpu_tensor.to(options.device());
}
} // namespace detail
#define TENSOR(T, _1) \
PADDLE_API Tensor tensor(ArrayRef<T> values, const TensorOptions& options) { \
if (options.device().type() != c10::DeviceType::CPU) { \
return at::detail::tensor_backend(values, options); \
} else { \
return at::detail::tensor_cpu(values, options); \
} \
}
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, TENSOR)
#undef TENSOR
#define TENSOR(T, _1) \
PADDLE_API Tensor tensor(ArrayRef<T> values, const TensorOptions& options) { \
if (options.device().type() != c10::DeviceType::CPU) { \
return at::detail::tensor_complex_backend(values, options); \
} else { \
return at::detail::tensor_complex_cpu(values, options); \
} \
}
AT_FORALL_COMPLEX_TYPES(TENSOR)
#undef TENSOR
} // namespace at