Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update predictor.register_output_hook: support paddle::Tensor #54254

Merged
merged 3 commits into from
Jun 2, 2023

Conversation

yuanlehome
Copy link
Contributor

@yuanlehome yuanlehome commented May 31, 2023

PR types

New features

PR changes

Others

Description

upgrade predictor.register_output_hook api: support paddle::Tensor(C++), paddle.Tensor(Python)

之前register_output_hook打印中间tensor的功能,获取的是paddle_infer::Tensor,使用起来不方便,升级了下支持paddle::Tensor,c++端和python端接口均不变,使用register_output_hook接口。第一版参考 #47050

Python 示例:

def hook(op_name: str, output_name: str, tensor: paddle.Tensor):
        print(op_name, output_name, tensor)

predictor.register_output_hook(hook)

C++ 示例:

// 使用 paddle::Tensor 需要包含这个头文件
#include "paddle/extension.h"

void get_output_tensor(const std::string &op_type,
                       const std::string &tensor_name,
                       const paddle::Tensor &tensor) {
  if(tensor.dtype() != paddle::DataType::FLOAT32) return;
  auto cpu_tensor = tensor.copy_to(paddle::CPUPlace{}, true);
  // using TYPE = phi::dtype::float16;
  using TYPE = float;

  std::stringstream ss;

  // op type and tensor name
  ss << std::left << std::setw(20) << op_type << std::setw(40) << tensor_name;

  // tensor shape
  std::string shape_str;
  shape_str += "[" + std::to_string(cpu_tensor.shape()[0]);
  for (size_t i = 1; i < cpu_tensor.shape().size(); i++) {
    shape_str += "," + std::to_string(cpu_tensor.shape()[i]);
  }
  shape_str += "]";
  ss << std::setw(20) << shape_str;

  // tensor data mean and variance
  TYPE sum{0};
  for (size_t i = 0; i < cpu_tensor.numel(); i++) {
    sum += cpu_tensor.data<TYPE>()[i];
  }
  TYPE mean = sum / TYPE(cpu_tensor.numel());
  TYPE accum{0};
  for (size_t i = 0; i < cpu_tensor.numel(); i++) {
    accum += (cpu_tensor.data<TYPE>()[i] - mean) *
             (cpu_tensor.data<TYPE>()[i] - mean);
  }
  TYPE variance = accum / TYPE(cpu_tensor.numel());
  ss << std::setw(20) << mean << std::setw(20) << variance;

  std::cout << ss.str() << std::endl;
}

C++ 输出示例:
image

update: hook函数原本支持的paddle_infer::Tensor不再支持,以后统一使用paddle::Tensor。

@paddle-bot
Copy link

paddle-bot bot commented May 31, 2023

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

Copy link
Contributor

@jiweibo jiweibo left a comment

Choose a reason for hiding this comment

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

LGTM

@jiweibo jiweibo merged commit f1c9c50 into PaddlePaddle:develop Jun 2, 2023
@SCP-KAKA
Copy link

SCP-KAKA commented Jun 8, 2023

使用pip安装的paddlepaddle中没有这个接口吗?

@yuanlehome
Copy link
Contributor Author

使用pip安装的paddlepaddle中没有这个接口吗?

有的。参考这个 https://github.com/PaddlePaddle/Paddle-Inference-Demo/blob/master/python/gpu/resnet50/infer_resnet.py#L65 调predictor.register_output_hook就可以了。

@yuanlehome yuanlehome deleted the register_output_hook_v2 branch June 8, 2023 06:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants