forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharange.h
More file actions
151 lines (136 loc) · 5.62 KB
/
arange.h
File metadata and controls
151 lines (136 loc) · 5.62 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) 2025 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.
#pragma once
#include <ATen/core/Tensor.h>
#include <ATen/native/RangeUtils.h>
#include <c10/core/TensorOptions.h>
#include <utils/pinned_place.h>
#include <optional>
#include "paddle/phi/api/include/api.h"
#include "paddle/phi/common/place.h"
namespace at {
namespace detail {
inline bool _PD_IsIntegralArangeScalar(const at::Scalar& scalar) {
switch (scalar.dtype()) {
case phi::DataType::BOOL:
case phi::DataType::UINT8:
case phi::DataType::INT8:
case phi::DataType::UINT16:
case phi::DataType::INT16:
case phi::DataType::UINT32:
case phi::DataType::INT32:
case phi::DataType::UINT64:
case phi::DataType::INT64:
return true;
default:
return false;
}
}
inline at::ScalarType _PD_ResolveArangeDtype(const at::Scalar& start,
const at::Scalar& end,
const at::Scalar& step,
const at::TensorOptions& options) {
if (options.has_dtype()) {
return options.dtype().toScalarType();
}
if (_PD_IsIntegralArangeScalar(start) && _PD_IsIntegralArangeScalar(end) &&
_PD_IsIntegralArangeScalar(step)) {
return at::kLong;
}
return c10::get_default_dtype_as_scalartype();
}
} // namespace detail
inline at::Tensor arange(const at::Scalar& start,
const at::Scalar& end,
const at::Scalar& step,
at::TensorOptions options = {}) {
// Match PyTorch: step must be non-zero and consistent with (end - start).
at::native::arange_check_bounds(start, end, step);
auto dtype = detail::_PD_ResolveArangeDtype(start, end, step, options);
if (options.pinned_memory()) {
// Pinning memory is only supported for CPU tensors
if (options.has_device() && !options.device().is_cpu()) {
PD_THROW(
"pin_memory=true requires device to be CPU, but got non-CPU device");
}
phi::Place base_place = options._PD_GetPlace();
phi::Place pinned_place = compat::_PD_GetCreatePinnedPlace(base_place);
auto dense = paddle::experimental::arange(
paddle::experimental::full(
{}, start.to<double>(), phi::DataType::FLOAT64),
paddle::experimental::full(
{}, end.to<double>(), phi::DataType::FLOAT64),
paddle::experimental::full(
{}, step.to<double>(), phi::DataType::FLOAT64),
compat::_PD_AtenScalarTypeToPhiDataType(dtype),
phi::CPUPlace());
return dense.copy_to(pinned_place, /*blocking=*/true);
}
return paddle::experimental::arange(
paddle::experimental::full(
{}, start.to<double>(), phi::DataType::FLOAT64),
paddle::experimental::full({}, end.to<double>(), phi::DataType::FLOAT64),
paddle::experimental::full({}, step.to<double>(), phi::DataType::FLOAT64),
compat::_PD_AtenScalarTypeToPhiDataType(dtype),
options._PD_GetPlace());
}
inline at::Tensor arange(const at::Scalar& end,
at::TensorOptions options = {}) {
return arange(/*start=*/0, end, /*step=*/1, options);
}
inline at::Tensor arange(const at::Scalar& end,
::std::optional<at::ScalarType> dtype,
::std::optional<at::Layout> layout,
::std::optional<at::Device> device,
::std::optional<bool> pin_memory) {
auto options = at::TensorOptions()
.dtype(dtype)
.layout(layout)
.device(device)
.pinned_memory(pin_memory);
return arange(/*start=*/0, end, /*step=*/1, options);
}
inline at::Tensor arange(const at::Scalar& start,
const at::Scalar& end,
at::TensorOptions options = {}) {
return arange(start, end, /*step=*/1, options);
}
inline at::Tensor arange(const at::Scalar& start,
const at::Scalar& end,
::std::optional<at::ScalarType> dtype,
::std::optional<at::Layout> layout,
::std::optional<at::Device> device,
::std::optional<bool> pin_memory) {
auto options = at::TensorOptions()
.dtype(dtype)
.layout(layout)
.device(device)
.pinned_memory(pin_memory);
return arange(start, end, /*step=*/1, options);
}
inline at::Tensor arange(const at::Scalar& start,
const at::Scalar& end,
const at::Scalar& step,
::std::optional<at::ScalarType> dtype,
::std::optional<at::Layout> layout,
::std::optional<at::Device> device,
::std::optional<bool> pin_memory) {
auto options = at::TensorOptions()
.dtype(dtype)
.layout(layout)
.device(device)
.pinned_memory(pin_memory);
return arange(start, end, step, options);
}
} // namespace at