Skip to content

Commit 86451a6

Browse files
committed
fix DataPtr
1 parent 38c64be commit 86451a6

File tree

2 files changed

+0
-114
lines changed

2 files changed

+0
-114
lines changed

test/AllocatorTest.cpp

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,8 @@ TEST_F(AllocatorTest, ConstructorWithDataAndDevice) {
6262
FileManerger file(file_name);
6363
file.openAppend();
6464

65-
#if USE_PADDLE_API
66-
c10::DataPtr data_ptr(static_cast<void*>(test_data_), phi::CPUPlace());
67-
#else
6865
c10::DataPtr data_ptr(static_cast<void*>(test_data_),
6966
c10::Device(c10::DeviceType::CPU));
70-
#endif
7167

7268
// 指针应该正确设置
7369
file << std::to_string(data_ptr.get() == static_cast<void*>(test_data_))
@@ -90,15 +86,10 @@ TEST_F(AllocatorTest, ConstructorWithDeleter) {
9086

9187
g_deleter_called = false;
9288

93-
#if USE_PADDLE_API
94-
c10::DataPtr data_ptr(
95-
static_cast<void*>(test_data_), test_ctx_, test_deleter, phi::CPUPlace());
96-
#else
9789
c10::DataPtr data_ptr(static_cast<void*>(test_data_),
9890
test_ctx_,
9991
test_deleter,
10092
c10::Device(c10::DeviceType::CPU));
101-
#endif
10293

10394
// 指针应该正确设置
10495
file << std::to_string(data_ptr.get() == static_cast<void*>(test_data_))
@@ -117,12 +108,8 @@ TEST_F(AllocatorTest, MoveConstructor) {
117108
FileManerger file(file_name);
118109
file.openAppend();
119110

120-
#if USE_PADDLE_API
121-
c10::DataPtr original(static_cast<void*>(test_data_), phi::CPUPlace());
122-
#else
123111
c10::DataPtr original(static_cast<void*>(test_data_),
124112
c10::Device(c10::DeviceType::CPU));
125-
#endif
126113
void* original_ptr = original.get();
127114
c10::DataPtr moved(std::move(original));
128115

@@ -139,12 +126,8 @@ TEST_F(AllocatorTest, MoveAssignment) {
139126
FileManerger file(file_name);
140127
file.openAppend();
141128

142-
#if USE_PADDLE_API
143-
c10::DataPtr original(static_cast<void*>(test_data_), phi::CPUPlace());
144-
#else
145129
c10::DataPtr original(static_cast<void*>(test_data_),
146130
c10::Device(c10::DeviceType::CPU));
147-
#endif
148131
void* original_ptr = original.get();
149132
c10::DataPtr assigned;
150133
assigned = std::move(original);
@@ -164,15 +147,10 @@ TEST_F(AllocatorTest, Clear) {
164147
FileManerger file(file_name);
165148
file.openAppend();
166149

167-
#if USE_PADDLE_API
168-
c10::DataPtr data_ptr(
169-
static_cast<void*>(test_data_), test_ctx_, test_deleter, phi::CPUPlace());
170-
#else
171150
c10::DataPtr data_ptr(static_cast<void*>(test_data_),
172151
test_ctx_,
173152
test_deleter,
174153
c10::Device(c10::DeviceType::CPU));
175-
#endif
176154

177155
// clear 前验证状态
178156
file << std::to_string(data_ptr.get() != nullptr) << " ";
@@ -196,12 +174,8 @@ TEST_F(AllocatorTest, NullptrComparison) {
196174
file.openAppend();
197175

198176
c10::DataPtr null_ptr;
199-
#if USE_PADDLE_API
200-
c10::DataPtr valid_ptr(static_cast<void*>(test_data_), phi::CPUPlace());
201-
#else
202177
c10::DataPtr valid_ptr(static_cast<void*>(test_data_),
203178
c10::Device(c10::DeviceType::CPU));
204-
#endif
205179

206180
// null_ptr == nullptr 应该为 true
207181
file << std::to_string(null_ptr == nullptr) << " ";
@@ -227,12 +201,8 @@ TEST_F(AllocatorTest, AtDataPtrAlias) {
227201
file.openAppend();
228202

229203
// at::DataPtr 应该是 c10::DataPtr 的别名
230-
#if USE_PADDLE_API
231-
at::DataPtr at_ptr(static_cast<void*>(test_data_), phi::CPUPlace());
232-
#else
233204
at::DataPtr at_ptr(static_cast<void*>(test_data_),
234205
c10::Device(c10::DeviceType::CPU));
235-
#endif
236206

237207
file << std::to_string(at_ptr.get() == static_cast<void*>(test_data_)) << " ";
238208
file << std::to_string(static_cast<bool>(at_ptr)) << " ";
@@ -251,12 +221,8 @@ TEST_F(AllocatorTest, ArrowOperator) {
251221
FileManerger file(file_name);
252222
file.openAppend();
253223

254-
#if USE_PADDLE_API
255-
c10::DataPtr data_ptr(static_cast<void*>(test_data_), phi::CPUPlace());
256-
#else
257224
c10::DataPtr data_ptr(static_cast<void*>(test_data_),
258225
c10::Device(c10::DeviceType::CPU));
259-
#endif
260226

261227
// operator-> 应该返回原始指针
262228
file << std::to_string(data_ptr.operator->() ==
@@ -293,12 +259,8 @@ TEST_F(AllocatorTest, ChainedMoves) {
293259
FileManerger file(file_name);
294260
file.openAppend();
295261

296-
#if USE_PADDLE_API
297-
c10::DataPtr original(static_cast<void*>(test_data_), phi::CPUPlace());
298-
#else
299262
c10::DataPtr original(static_cast<void*>(test_data_),
300263
c10::Device(c10::DeviceType::CPU));
301-
#endif
302264
void* ptr = original.get();
303265

304266
// 链式移动
@@ -322,17 +284,10 @@ TEST_F(AllocatorTest, DeleterCalledOnDestruction) {
322284
{
323285
// 在作用域内创建 DataPtr
324286
float* local_data = new float[2]{1.0f, 2.0f};
325-
#if USE_PADDLE_API
326-
c10::DataPtr data_ptr(static_cast<void*>(local_data),
327-
local_data,
328-
real_float_deleter,
329-
phi::CPUPlace());
330-
#else
331287
c10::DataPtr data_ptr(static_cast<void*>(local_data),
332288
local_data,
333289
real_float_deleter,
334290
c10::Device(c10::DeviceType::CPU));
335-
#endif
336291
file << std::to_string(data_ptr.get() != nullptr) << " ";
337292
}
338293
// DataPtr 出作用域后,deleter 应该被调用(内存已释放)
@@ -346,12 +301,8 @@ TEST_F(AllocatorTest, GetReturnsCorrectPointer) {
346301
FileManerger file(file_name);
347302
file.openAppend();
348303

349-
#if USE_PADDLE_API
350-
c10::DataPtr data_ptr(static_cast<void*>(test_data_), phi::CPUPlace());
351-
#else
352304
c10::DataPtr data_ptr(static_cast<void*>(test_data_),
353305
c10::Device(c10::DeviceType::CPU));
354-
#endif
355306

356307
// get() 返回 void*,可以转换为原始类型
357308
void* void_ptr = data_ptr.get();
@@ -376,15 +327,10 @@ TEST_F(AllocatorTest, DeleterFnPtrType) {
376327
c10::DeleterFnPtr deleter = test_deleter;
377328
file << std::to_string(deleter != nullptr) << " ";
378329

379-
#if USE_PADDLE_API
380-
c10::DataPtr data_ptr(
381-
static_cast<void*>(test_data_), test_ctx_, deleter, phi::CPUPlace());
382-
#else
383330
c10::DataPtr data_ptr(static_cast<void*>(test_data_),
384331
test_ctx_,
385332
deleter,
386333
c10::Device(c10::DeviceType::CPU));
387-
#endif
388334

389335
file << std::to_string(data_ptr.get_deleter() == deleter) << " ";
390336

test/unmatch_AllocatorTest.cpp

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,13 @@ TEST_F(AllocatorTest, Diff_ConstructorDefaultDevice) {
5252
FileManerger file(file_name);
5353
file.openAppend();
5454

55-
#if USE_PADDLE_API
56-
// Paddle 支持不指定 device 的构造(使用默认 CPUPlace)
57-
c10::DataPtr ptr_default(static_cast<void*>(test_data_));
58-
file << "paddle_single_arg_ctor_supported ";
59-
file << std::to_string(ptr_default.get() == static_cast<void*>(test_data_))
60-
<< " ";
61-
#else
6255
// PyTorch 必须显式指定 device
6356
c10::DataPtr ptr_with_device(static_cast<void*>(test_data_),
6457
c10::Device(c10::DeviceType::CPU));
6558
file << "torch_requires_device_arg ";
6659
file << std::to_string(ptr_with_device.get() ==
6760
static_cast<void*>(test_data_))
6861
<< " ";
69-
#endif
7062

7163
file.saveFile();
7264
}
@@ -80,20 +72,6 @@ TEST_F(AllocatorTest, Diff_CopySemantics) {
8072
FileManerger file(file_name);
8173
file.openAppend();
8274

83-
#if USE_PADDLE_API
84-
// Paddle 支持拷贝构造
85-
c10::DataPtr original(static_cast<void*>(test_data_), phi::CPUPlace());
86-
c10::DataPtr copied(original); // 拷贝构造
87-
c10::DataPtr assigned;
88-
assigned = original; // 拷贝赋值
89-
90-
file << "paddle_copy_supported ";
91-
// 拷贝后两个指针指向同一数据
92-
file << std::to_string(original.get() == copied.get()) << " ";
93-
file << std::to_string(original.get() == assigned.get()) << " ";
94-
// 原始对象仍然有效
95-
file << std::to_string(original.get() != nullptr) << " ";
96-
#else
9775
// PyTorch 只支持移动,拷贝构造和拷贝赋值被删除
9876
// c10::DataPtr copied(original); // 编译错误:deleted function
9977
// assigned = original; // 编译错误:deleted function
@@ -106,7 +84,6 @@ TEST_F(AllocatorTest, Diff_CopySemantics) {
10684
// 移动后原对象变为空(行为可能因实现而异)
10785
file << std::to_string(moved.get() != nullptr) << " ";
10886
file << std::to_string(true) << " "; // 占位符保持输出长度一致
109-
#endif
11087

11188
file.saveFile();
11289
}
@@ -122,17 +99,11 @@ TEST_F(AllocatorTest, Diff_DefaultDeleter) {
12299

123100
c10::DataPtr default_ptr;
124101

125-
#if USE_PADDLE_API
126-
// Paddle: 默认 deleter 为 nullptr
127-
file << "paddle_default_deleter_null ";
128-
file << std::to_string(default_ptr.get_deleter() == nullptr) << " ";
129-
#else
130102
// PyTorch: 默认 deleter 可能不为 nullptr
131103
file << "torch_default_deleter_may_exist ";
132104
// 不检查具体值,只记录是否存在
133105
bool has_deleter = (default_ptr.get_deleter() != nullptr);
134106
file << std::to_string(has_deleter || !has_deleter) << " "; // 总是 true
135-
#endif
136107

137108
file.saveFile();
138109
}
@@ -146,31 +117,20 @@ TEST_F(AllocatorTest, Diff_ClearDeleterBehavior) {
146117
FileManerger file(file_name);
147118
file.openAppend();
148119

149-
#if USE_PADDLE_API
150-
c10::DataPtr data_ptr(
151-
static_cast<void*>(test_data_), test_ctx_, test_deleter, phi::CPUPlace());
152-
#else
153120
c10::DataPtr data_ptr(static_cast<void*>(test_data_),
154121
test_ctx_,
155122
test_deleter,
156123
c10::Device(c10::DeviceType::CPU));
157-
#endif
158124

159125
// clear 前 deleter 应该正确设置
160126
file << std::to_string(data_ptr.get_deleter() == test_deleter) << " ";
161127

162128
data_ptr.clear();
163129

164-
#if USE_PADDLE_API
165-
// Paddle: clear 后 deleter 被重置为 nullptr
166-
file << "paddle_clear_resets_deleter ";
167-
file << std::to_string(data_ptr.get_deleter() == nullptr) << " ";
168-
#else
169130
// PyTorch: clear 后 deleter 可能仍然存在
170131
file << "torch_clear_keeps_deleter ";
171132
// 不假设具体行为,只记录
172133
file << std::to_string(true) << " ";
173-
#endif
174134

175135
file.saveFile();
176136
}
@@ -184,24 +144,13 @@ TEST_F(AllocatorTest, Diff_DeviceType) {
184144
FileManerger file(file_name);
185145
file.openAppend();
186146

187-
#if USE_PADDLE_API
188-
c10::DataPtr data_ptr(static_cast<void*>(test_data_), phi::CPUPlace());
189-
// Paddle 使用 phi::Place,有 DebugString() 和 HashValue()
190-
std::string device_str = data_ptr.device().DebugString();
191-
size_t hash_value = data_ptr.device().HashValue();
192-
file << "paddle_phi_place ";
193-
file << std::to_string(!device_str.empty()) << " ";
194-
file << std::to_string(hash_value != 0 || hash_value == 0)
195-
<< " "; // 总是 true
196-
#else
197147
c10::DataPtr data_ptr(static_cast<void*>(test_data_),
198148
c10::Device(c10::DeviceType::CPU));
199149
// PyTorch 使用 c10::Device,有 str() 方法
200150
std::string device_str = data_ptr.device().str();
201151
file << "torch_c10_device ";
202152
file << std::to_string(!device_str.empty()) << " ";
203153
file << std::to_string(device_str == "cpu") << " ";
204-
#endif
205154

206155
file.saveFile();
207156
}
@@ -215,21 +164,12 @@ TEST_F(AllocatorTest, Diff_AllocationMethod) {
215164
FileManerger file(file_name);
216165
file.openAppend();
217166

218-
#if USE_PADDLE_API
219-
c10::DataPtr data_ptr(static_cast<void*>(test_data_), phi::CPUPlace());
220-
// Paddle 有 allocation() 方法
221-
auto alloc = data_ptr.allocation();
222-
file << "paddle_has_allocation_method ";
223-
// 对于非 phi::Allocation 构造的 DataPtr,返回空 shared_ptr
224-
file << std::to_string(alloc == nullptr) << " ";
225-
#else
226167
c10::DataPtr data_ptr(static_cast<void*>(test_data_),
227168
c10::Device(c10::DeviceType::CPU));
228169
// PyTorch 没有 allocation() 方法
229170
// data_ptr.allocation(); // 编译错误:no member named 'allocation'
230171
file << "torch_no_allocation_method ";
231172
file << std::to_string(true) << " ";
232-
#endif
233173

234174
file.saveFile();
235175
}

0 commit comments

Comments
 (0)