Skip to content

Commit 861991b

Browse files
SigureMocodex
andcommitted
Fix compat pinned fill semantics
Co-authored-by: Codex <codex@openai.com>
1 parent 3751716 commit 861991b

File tree

10 files changed

+146
-169
lines changed

10 files changed

+146
-169
lines changed

paddle/phi/api/include/compat/ATen/core/TensorBase.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include <c10/core/SymIntArrayRef.h>
2626
#include <c10/core/TensorOptions.h>
2727
#include <utils/int_array_ref_conversion.h>
28-
#include <utils/mapped_pinned_tensor.h>
28+
#include <utils/pinned_tensor_ops.h>
2929
#include <utils/scalar_type_conversion.h>
3030
#include <algorithm>
3131
#include <iostream>
@@ -113,10 +113,11 @@ class PADDLE_API TensorBase {
113113
return backend_str + scalar_type_str + "Type";
114114
}
115115

116-
// Returns the pointer kernels should use. For CUDA-pinned tensors this is
117-
// the mapped device-visible alias rather than the raw host address.
118116
void* data_ptr() const {
119-
return compat::_PD_GetKernelVisibleDataPtr(tensor_);
117+
if (!tensor_.defined()) {
118+
return nullptr;
119+
}
120+
return const_cast<void*>(tensor_.data());
120121
}
121122
template <typename T>
122123
T* data_ptr() const {
@@ -267,12 +268,12 @@ class PADDLE_API TensorBase {
267268
}
268269

269270
const TensorBase& fill_(const at::Scalar& scalar) const {
270-
paddle::experimental::fill_(const_cast<PaddleTensor&>(tensor_), scalar);
271+
compat::_PD_FillTensorInplace(&const_cast<PaddleTensor&>(tensor_), scalar);
271272
return *this;
272273
}
273274

274275
const TensorBase& zero_() const {
275-
paddle::experimental::fill_(const_cast<PaddleTensor&>(tensor_), 0.0);
276+
compat::_PD_FillTensorInplace(&const_cast<PaddleTensor&>(tensor_), 0.0);
276277
return *this;
277278
}
278279

paddle/phi/api/include/compat/ATen/core/TensorBody.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include <c10/core/Stream.h>
2424
#include <c10/core/SymIntArrayRef.h>
2525
#include <c10/util/OptionalArrayRef.h>
26-
#include <utils/mapped_pinned_tensor.h>
26+
#include <utils/pinned_tensor_ops.h>
2727
#include "paddle/phi/api/include/api.h"
2828
#include "paddle/phi/api/include/tensor.h"
2929
#include "paddle/phi/common/int_array.h"
@@ -128,7 +128,10 @@ class Tensor : public TensorBase {
128128
}
129129

130130
void* data_ptr() const {
131-
return compat::_PD_GetKernelVisibleDataPtr(tensor_);
131+
if (!tensor_.defined()) {
132+
return nullptr;
133+
}
134+
return const_cast<void*>(tensor_.data());
132135
}
133136
template <typename T>
134137
T* data_ptr() const {
@@ -408,12 +411,12 @@ class Tensor : public TensorBase {
408411
at::Tensor unflatten_symint(int64_t dim, c10::SymIntArrayRef sizes) const;
409412

410413
Tensor& fill_(const at::Scalar& value) const {
411-
paddle::experimental::fill_(const_cast<PaddleTensor&>(tensor_), value);
414+
compat::_PD_FillTensorInplace(&const_cast<PaddleTensor&>(tensor_), value);
412415
return const_cast<at::Tensor&>(*this);
413416
}
414417

415418
Tensor& zero_() const {
416-
paddle::experimental::fill_(const_cast<PaddleTensor&>(tensor_), 0.0);
419+
compat::_PD_FillTensorInplace(&const_cast<PaddleTensor&>(tensor_), 0.0);
417420
return const_cast<at::Tensor&>(*this);
418421
}
419422

@@ -490,7 +493,7 @@ class Tensor : public TensorBase {
490493
#endif
491494
}
492495

493-
return compat::_PD_CopyTensorToPinnedPlace(tensor_, pinned_place);
496+
return tensor_.copy_to(pinned_place, /*blocking=*/true);
494497
}
495498

496499
at::Tensor narrow_copy(int64_t dim, int64_t start, int64_t length) const;

paddle/phi/api/include/compat/ATen/ops/empty.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <ATen/core/Tensor.h>
1818
#include <c10/core/TensorOptions.h>
1919
#include <utils/dense_sparse_conversion.h>
20-
#include <utils/mapped_pinned_tensor.h>
2120
#include <utils/pinned_place.h>
2221
#include <optional>
2322
#include <string_view>
@@ -42,10 +41,11 @@ inline at::Tensor empty(
4241
}
4342
phi::Place pinned_place =
4443
compat::_PD_GetCreatePinnedPlace(options._PD_GetPlace());
45-
auto dense = compat::_PD_EmptyPinnedTensor(
44+
auto dense = paddle::experimental::empty(
4645
size._PD_ToPaddleIntArray(),
4746
compat::_PD_AtenScalarTypeToPhiDataType(options.dtype()),
48-
pinned_place);
47+
phi::CPUPlace());
48+
dense = dense.copy_to(pinned_place, /*blocking=*/true);
4949
return compat::_PD_ConvertToSparseIfNeeded(dense, options.layout());
5050
}
5151
auto dense = paddle::experimental::empty(

paddle/phi/api/include/compat/ATen/ops/empty_like.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <ATen/core/Tensor.h>
1818
#include <c10/core/TensorOptions.h>
1919
#include <utils/dense_sparse_conversion.h>
20-
#include <utils/mapped_pinned_tensor.h>
2120
#include <utils/pinned_place.h>
2221

2322
#include <optional>
@@ -50,7 +49,7 @@ inline at::Tensor empty_like(
5049
phi::CPUPlace());
5150
phi::Place base_place = options._PD_GetPlace();
5251
phi::Place pinned_place = compat::_PD_GetCreatePinnedPlace(base_place);
53-
dense = compat::_PD_CopyTensorToPinnedPlace(dense_cpu, pinned_place);
52+
dense = dense_cpu.copy_to(pinned_place, /*blocking=*/true);
5453
} else {
5554
auto place = options.device_opt().value_or(self.device());
5655
dense = paddle::experimental::empty_like(

paddle/phi/api/include/compat/ATen/ops/new_empty.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
#include <ATen/core/Tensor.h>
1818
#include <c10/core/TensorOptions.h>
19-
#include <utils/mapped_pinned_tensor.h>
2019
#include <utils/pinned_place.h>
2120
#include <optional>
2221
#include <string_view>
@@ -44,8 +43,9 @@ inline Tensor Tensor::new_empty(at::IntArrayRef size,
4443
"pin_memory=true requires device to be CPU, but got non-CPU device");
4544
}
4645
phi::Place pinned_place = compat::_PD_GetCreatePinnedPlace(pd_place);
47-
result = compat::_PD_EmptyPinnedTensor(
48-
size._PD_ToPaddleIntArray(), pd_dtype, pinned_place);
46+
result = paddle::experimental::empty(
47+
size._PD_ToPaddleIntArray(), pd_dtype, phi::CPUPlace())
48+
.copy_to(pinned_place, /*blocking=*/true);
4949
} else {
5050
result = paddle::experimental::empty(
5151
size._PD_ToPaddleIntArray(), pd_dtype, pd_place);

paddle/phi/api/include/compat/utils/mapped_pinned_tensor.h

Lines changed: 0 additions & 150 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include <c10/core/Scalar.h>
18+
19+
#include "paddle/common/ddim.h"
20+
#include "paddle/phi/api/include/api.h"
21+
#include "paddle/phi/api/include/tensor.h"
22+
#include "paddle/phi/common/int_array.h"
23+
#include "paddle/phi/common/place.h"
24+
25+
namespace compat {
26+
27+
inline bool _PD_IsHostPinnedTensor(const paddle::Tensor& tensor) {
28+
const auto& place = tensor.place();
29+
return phi::is_cuda_pinned_place(place) || phi::is_xpu_pinned_place(place);
30+
}
31+
32+
inline void _PD_FillTensorInplace(paddle::Tensor* tensor,
33+
const c10::Scalar& value) {
34+
if (!_PD_IsHostPinnedTensor(*tensor)) {
35+
paddle::experimental::fill_(*tensor, value);
36+
return;
37+
}
38+
39+
auto cpu_src = paddle::experimental::full(
40+
phi::IntArray(common::vectorize<int64_t>(tensor->dims())),
41+
value,
42+
tensor->dtype(),
43+
phi::CPUPlace());
44+
tensor->copy_(cpu_src, tensor->place(), /*blocking=*/true);
45+
}
46+
47+
} // namespace compat

python/paddle/tensor/manipulation.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,26 @@ def _attr_offsets_check(offset_val):
11341134
return out
11351135

11361136

1137+
@dygraph_only
1138+
def _is_host_pinned_tensor(x: Tensor) -> bool:
1139+
place = x.place
1140+
return (
1141+
hasattr(place, "is_cuda_pinned_place") and place.is_cuda_pinned_place()
1142+
) or (hasattr(place, "is_xpu_pinned_place") and place.is_xpu_pinned_place())
1143+
1144+
1145+
@dygraph_only
1146+
def _fill_host_pinned_tensor_inplace(x: Tensor, value: float) -> Tensor:
1147+
cpu_src = paddle.full(
1148+
shape=x.shape,
1149+
fill_value=value,
1150+
dtype=x.dtype,
1151+
device=paddle.CPUPlace(),
1152+
)
1153+
x.copy_(cpu_src, True)
1154+
return x
1155+
1156+
11371157
@dygraph_only
11381158
def fill_(x: Tensor, value: float) -> Tensor:
11391159
"""
@@ -1165,6 +1185,8 @@ def fill_(x: Tensor, value: float) -> Tensor:
11651185
raise TypeError(
11661186
f"The type of 'value' must be int or float, but received {type(value)}."
11671187
)
1188+
if _is_host_pinned_tensor(x):
1189+
return _fill_host_pinned_tensor_inplace(x, value)
11681190
return _C_ops.fill_(x, value)
11691191

11701192

@@ -1194,6 +1216,8 @@ def zero_(x: Tensor) -> Tensor:
11941216
[0, 0, 0, 0, 0]
11951217
11961218
"""
1219+
if _is_host_pinned_tensor(x):
1220+
return _fill_host_pinned_tensor_inplace(x, 0.0)
11971221
return _C_ops.fill_(x, 0.0)
11981222

11991223

0 commit comments

Comments
 (0)