Skip to content

Conversation

@pengcheng888
Copy link
Collaborator

@pengcheng888 pengcheng888 commented Jan 15, 2026

目标版本
main

功能描述
c++和python中的tensor添加打印函数,可以全局临时配置一下参数:
(1) 触发省略的阈值:数据量过多时,显示部分数据;
(2) 每行显示字符数量:显示宽度合适,自动换行;
(3) 精度:设置显示的小数点位数;
(4) edgeitems: 省略显示时,设置显示的数据数量

c++的cout函数支持的类型: BOOL,I8,I16,I32,I64,U8,U16,U32,U64,BF16,F16,F32,F64
python的print函数支持的类型: BOOL,I8,I16,I32,I64,U8,BF16,F16,F32,F64

python代码std::cout测试:
临时修改后,自动恢复原始配置
Screenshot from 2026-01-15 09-55-25

全局修改后,配置生效
Screenshot from 2026-01-15 09-54-50

print不同的数据类型
Screenshot from 2026-01-15 09-53-30

python代码print测试:
临时修改后,自动恢复原始配置
Screenshot from 2026-01-15 10-24-50

全局修改后,配置生效
Screenshot from 2026-01-15 10-24-33

cout不同的数据类型
Screenshot from 2026-01-15 10-24-19


import infinicore.context as context
import infinicore.nn as nn
from infinicore._tensor_str import printoptions, set_printoptions
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

torch中把print的一些 函数调用放到了_tensor_str.py文件中,于是也新建了一个 _tensor_str

"strided_from_blob",
"zeros",
"set_printoptions",
"printoptions",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

torch中存在一下两个函数
"set_printoptions":全局配置的函数
"printoptions": 临时配置的函数

std::string repr = m1.extra_repr();
spdlog::debug("Linear module representation: {}", repr);

// Test forward with residual connection
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c++的linear有个版移除了一个函数,导致c++测试编译不过。于是,在src/infinicore-test/test_nn_module.cc中注释或删除了传递的参数


// Prepare test data (stored outside loop to ensure lifetime)
bool bool_data[4] = {true, false, true, false};
int8_t i8_data[4] = {-128, -64, 32, 127};
Copy link
Collaborator Author

@pengcheng888 pengcheng888 Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

由于infinicore.Tensor没有rand函数,没法生成随机数,所以就事前固定了测试数据

set_installdir(os.getenv("INFINI_ROOT") or (os.getenv(is_host("windows") and "HOMEPATH" or "HOME") .. "/.infini"))
target_end()

target("infinicore-test")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c++的infinicore的测试脚本出错,于是添加上了缺少的"infinicore_cpp_api"依赖

template <typename T>
T item_impl(const std::byte *data, DataType dtype) {
switch (dtype) {
case DataType::F16: {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

由于c++没有原生的F16和BF16格式数据,为了能cout打印,把F16和BF16转成了float类型去显示

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[DEV] 提供显示美观的print函数,用于打印infinicore.Tensor数据

1 participant