Skip to content

Commit 7481523

Browse files
authored
Revert "[Cpp API Compatibility Test] add toBackend、item、data、meta、to、cpu " (#16)
paddle仓库新增的item API PR还没有合入,待合入之后再合入测试PR
1 parent b12586d commit 7481523

File tree

1 file changed

+0
-224
lines changed

1 file changed

+0
-224
lines changed

test/TensorTest.cpp

Lines changed: 0 additions & 224 deletions
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,6 @@ TEST_F(TensorTest, IsCpu) {
144144
EXPECT_TRUE(tensor.is_cpu());
145145
}
146146

147-
// 测试 cpu
148-
TEST_F(TensorTest, Cpu) {
149-
at::Tensor cpu_tensor = tensor.cpu();
150-
151-
EXPECT_TRUE(cpu_tensor.is_cpu());
152-
EXPECT_EQ(cpu_tensor.device().type(), c10::DeviceType::CPU);
153-
EXPECT_EQ(cpu_tensor.numel(), tensor.numel());
154-
EXPECT_FLOAT_EQ(cpu_tensor.data_ptr<float>()[0], tensor.data_ptr<float>()[0]);
155-
}
156-
157147
// 测试 is_cuda (在 CPU tensor 上应该返回 false)
158148
TEST_F(TensorTest, IsCuda) {
159149
// Tensor tensor(paddle_tensor_);
@@ -180,219 +170,5 @@ TEST_F(TensorTest, Transpose) {
180170
EXPECT_EQ(transposed.sizes()[2], 2);
181171
}
182172

183-
// 测试 toBackend
184-
TEST_F(TensorTest, ToBackend) {
185-
// 测试转换到 CPU backend
186-
at::Tensor cpu_tensor = tensor.toBackend(c10::Backend::CPU);
187-
EXPECT_TRUE(cpu_tensor.is_cpu());
188-
EXPECT_EQ(cpu_tensor.device().type(), c10::DeviceType::CPU);
189-
EXPECT_EQ(cpu_tensor.numel(), tensor.numel());
190-
191-
// 测试多次调用 toBackend(CPU) - 应该返回相同的 tensor
192-
at::Tensor cpu_tensor2 = cpu_tensor.toBackend(c10::Backend::CPU);
193-
EXPECT_EQ(cpu_tensor.data_ptr(), cpu_tensor2.data_ptr());
194-
195-
// 验证数据内容
196-
EXPECT_FLOAT_EQ(cpu_tensor.data_ptr<float>()[0], 1.0f);
197-
}
198-
199-
// 测试 item
200-
TEST_F(TensorTest, Item) {
201-
// 创建一个单元素 tensor
202-
std::vector<float> single_data = {3.14f};
203-
paddle::Tensor single_paddle_tensor(std::make_shared<phi::DenseTensor>(
204-
std::make_shared<phi::Allocation>(single_data.data(),
205-
single_data.size() * sizeof(float),
206-
phi::CPUPlace{}),
207-
phi::DenseTensorMeta(phi::DataType::FLOAT32, phi::make_ddim({1}))));
208-
at::Tensor single_tensor(single_paddle_tensor);
209-
210-
at::Scalar scalar_value = single_tensor.item();
211-
EXPECT_FLOAT_EQ(scalar_value.to<float>(), 3.14f);
212-
213-
// 测试多元素 tensor 应该抛出异常
214-
EXPECT_THROW(tensor.item(), std::exception);
215-
216-
// 测试不同数据类型 - int32
217-
at::Tensor int_tensor = at::ones({1}, at::kInt);
218-
at::Scalar int_scalar = int_tensor.item();
219-
EXPECT_EQ(int_scalar.to<int>(), 1);
220-
221-
// 测试不同数据类型 - int64
222-
at::Tensor long_tensor = at::ones({1}, at::kLong);
223-
at::Scalar long_scalar = long_tensor.item();
224-
EXPECT_EQ(long_scalar.to<int64_t>(), 1);
225-
226-
// 测试不同数据类型 - double
227-
at::Tensor double_tensor = at::ones({1}, at::kDouble);
228-
at::Scalar double_scalar = double_tensor.item();
229-
EXPECT_DOUBLE_EQ(double_scalar.to<double>(), 1.0);
230-
231-
// 测试不同数据类型 - bool
232-
at::Tensor bool_tensor = at::ones({1}, at::kBool);
233-
at::Scalar bool_scalar = bool_tensor.item();
234-
EXPECT_TRUE(bool_scalar.to<bool>());
235-
236-
// 测试更多数据类型 - int32 自定义值
237-
std::vector<int32_t> int32_data = {42};
238-
paddle::Tensor int32_paddle_tensor(std::make_shared<phi::DenseTensor>(
239-
std::make_shared<phi::Allocation>(int32_data.data(),
240-
int32_data.size() * sizeof(int32_t),
241-
phi::CPUPlace{}),
242-
phi::DenseTensorMeta(phi::DataType::INT32, phi::make_ddim({1}))));
243-
at::Tensor int32_tensor(int32_paddle_tensor);
244-
EXPECT_EQ(int32_tensor.item().to<int32_t>(), 42);
245-
246-
// 测试更多数据类型 - int64 大数值
247-
std::vector<int64_t> int64_data = {123456789L};
248-
paddle::Tensor int64_paddle_tensor(std::make_shared<phi::DenseTensor>(
249-
std::make_shared<phi::Allocation>(int64_data.data(),
250-
int64_data.size() * sizeof(int64_t),
251-
phi::CPUPlace{}),
252-
phi::DenseTensorMeta(phi::DataType::INT64, phi::make_ddim({1}))));
253-
at::Tensor int64_tensor(int64_paddle_tensor);
254-
EXPECT_EQ(int64_tensor.item().to<int64_t>(), 123456789L);
255-
256-
// 测试更多数据类型 - float64
257-
std::vector<double> float64_data = {2.71828};
258-
paddle::Tensor float64_paddle_tensor(std::make_shared<phi::DenseTensor>(
259-
std::make_shared<phi::Allocation>(float64_data.data(),
260-
float64_data.size() * sizeof(double),
261-
phi::CPUPlace{}),
262-
phi::DenseTensorMeta(phi::DataType::FLOAT64, phi::make_ddim({1}))));
263-
at::Tensor float64_tensor(float64_paddle_tensor);
264-
EXPECT_DOUBLE_EQ(float64_tensor.item().to<double>(), 2.71828);
265-
266-
// 测试更多数据类型 - bool false
267-
bool bool_false_data = false;
268-
paddle::Tensor bool_false_paddle_tensor(std::make_shared<phi::DenseTensor>(
269-
std::make_shared<phi::Allocation>(
270-
&bool_false_data, sizeof(bool), phi::CPUPlace{}),
271-
phi::DenseTensorMeta(phi::DataType::BOOL, phi::make_ddim({1}))));
272-
at::Tensor bool_false_tensor(bool_false_paddle_tensor);
273-
EXPECT_FALSE(bool_false_tensor.item().to<bool>());
274-
275-
// 测试多元素 tensor 二维
276-
at::Tensor multi_elem_2d = at::ones({2, 1}, at::kFloat);
277-
EXPECT_THROW(multi_elem_2d.item(), std::exception);
278-
}
279-
280-
// 测试 data 方法
281-
TEST_F(TensorTest, Data) {
282-
// Tensor tensor(paddle_tensor_);
283-
284-
void* float_data = tensor.data_ptr<float>();
285-
EXPECT_NE(float_data, nullptr);
286-
287-
// 验证数据内容
288-
float* data_as_float = static_cast<float*>(float_data);
289-
EXPECT_FLOAT_EQ(data_as_float[0], 1.0f);
290-
291-
// 测试不同类型的 tensor
292-
at::Tensor int_tensor = at::ones({2, 3}, at::kInt);
293-
void* int_data = int_tensor.data_ptr<int>();
294-
EXPECT_NE(int_data, nullptr);
295-
296-
int* data_as_int = static_cast<int*>(int_data);
297-
EXPECT_EQ(data_as_int[0], 1);
298-
}
299-
300-
// 测试 meta 方法
301-
TEST_F(TensorTest, Meta) {
302-
// Tensor tensor(paddle_tensor_);
303-
304-
// meta() 应该抛出异常,因为 Paddle 不支持
305-
EXPECT_THROW(tensor.meta(), std::exception);
306-
}
307-
308-
// 测试 to 方法 (TensorOptions 版本)
309-
TEST_F(TensorTest, ToWithOptions) {
310-
// Tensor tensor(paddle_tensor_);
311-
312-
// 测试转换到不同的数据类型
313-
at::Tensor double_tensor = tensor.to(at::TensorOptions().dtype(at::kDouble));
314-
EXPECT_EQ(double_tensor.dtype(), at::kDouble);
315-
EXPECT_EQ(double_tensor.numel(), tensor.numel());
316-
317-
// 测试 copy 参数
318-
at::Tensor copied_tensor =
319-
tensor.to(at::TensorOptions().dtype(at::kFloat), false, true);
320-
EXPECT_EQ(copied_tensor.dtype(), at::kFloat);
321-
EXPECT_EQ(copied_tensor.numel(), tensor.numel());
322-
// 验证是复制而不是引用
323-
EXPECT_NE(copied_tensor.data_ptr(), tensor.data_ptr());
324-
}
325-
326-
// 测试 to 方法 (ScalarType 版本)
327-
TEST_F(TensorTest, ToWithScalarType) {
328-
// Tensor tensor(paddle_tensor_);
329-
330-
// 测试转换到 double
331-
at::Tensor double_tensor = tensor.to(at::kDouble);
332-
EXPECT_EQ(double_tensor.dtype(), at::kDouble);
333-
EXPECT_EQ(double_tensor.numel(), tensor.numel());
334-
335-
// 测试转换到 int
336-
at::Tensor int_tensor = tensor.to(at::kInt);
337-
EXPECT_EQ(int_tensor.dtype(), at::kInt);
338-
EXPECT_EQ(int_tensor.numel(), tensor.numel());
339-
340-
// 测试转换到 long
341-
at::Tensor long_tensor = tensor.to(at::kLong);
342-
EXPECT_EQ(long_tensor.dtype(), at::kLong);
343-
EXPECT_EQ(long_tensor.numel(), tensor.numel());
344-
345-
// 验证数据内容 (float 1.0 -> int 1)
346-
int_tensor.fill_(5.7); // 5.7 should be truncated to 5
347-
int* int_data = int_tensor.data_ptr<int>();
348-
EXPECT_EQ(int_data[0], 5);
349-
}
350-
351-
// 测试 toBackend 行为
352-
TEST_F(TensorTest, ToBackendBehavior) {
353-
// Tensor tensor(paddle_tensor_);
354-
355-
// toBackend 总是会复制 tensor(当前实现)
356-
at::Tensor cpu_tensor1 = tensor.toBackend(c10::Backend::CPU);
357-
at::Tensor cpu_tensor2 = cpu_tensor1.toBackend(c10::Backend::CPU);
358-
359-
// 验证都在 CPU 上
360-
EXPECT_TRUE(cpu_tensor1.is_cpu());
361-
EXPECT_TRUE(cpu_tensor2.is_cpu());
362-
EXPECT_EQ(cpu_tensor1.device().type(), c10::DeviceType::CPU);
363-
EXPECT_EQ(cpu_tensor2.device().type(), c10::DeviceType::CPU);
364-
365-
// 验证数据内容相同(即使是不同的副本)
366-
EXPECT_FLOAT_EQ(cpu_tensor1.data_ptr<float>()[0], 1.0f);
367-
EXPECT_FLOAT_EQ(cpu_tensor2.data_ptr<float>()[0], 1.0f);
368-
369-
// 验证形状和元素数量相同
370-
EXPECT_EQ(cpu_tensor1.numel(), tensor.numel());
371-
EXPECT_EQ(cpu_tensor2.numel(), tensor.numel());
372-
}
373-
374-
// 测试 cpu 行为
375-
TEST_F(TensorTest, CpuBehavior) {
376-
// Tensor tensor(paddle_tensor_);
377-
378-
// 第一次调用 cpu()
379-
at::Tensor cpu_tensor1 = tensor.cpu();
380-
EXPECT_TRUE(cpu_tensor1.is_cpu());
381-
EXPECT_EQ(cpu_tensor1.device().type(), c10::DeviceType::CPU);
382-
383-
// 再次调用 cpu(),当前实现会创建新的副本
384-
at::Tensor cpu_tensor2 = cpu_tensor1.cpu();
385-
EXPECT_TRUE(cpu_tensor2.is_cpu());
386-
387-
// 验证数据内容
388-
EXPECT_FLOAT_EQ(cpu_tensor1.data_ptr<float>()[0], 1.0f);
389-
EXPECT_FLOAT_EQ(cpu_tensor2.data_ptr<float>()[0], 1.0f);
390-
391-
// 验证是有效的 tensor
392-
EXPECT_EQ(cpu_tensor1.numel(), tensor.numel());
393-
EXPECT_EQ(cpu_tensor2.numel(), tensor.numel());
394-
EXPECT_EQ(cpu_tensor1.dim(), tensor.dim());
395-
}
396-
397173
} // namespace test
398174
} // namespace at

0 commit comments

Comments
 (0)