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

[Tensor API & Prim] Support multiple Tensor C++ api #50731

Merged
merged 2 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 12 additions & 20 deletions paddle/fluid/prim/api/auto_code_generated/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,22 @@ execute_process(
${static_prim_api_cc_path})
message("copy tmp_xxx_prim_api to xxx_prim_api")

set(tmp_eager_tensor_operants_cc_path
"${PADDLE_SOURCE_DIR}/paddle/fluid/prim/utils/eager/eager_tensor_operants.cc.tmp"
)
set(tmp_eager_tensor_operants_h_path
"${PADDLE_SOURCE_DIR}/paddle/fluid/prim/utils/eager/eager_tensor_operants.h.tmp"
)
set(tmp_static_tensor_operants_cc_path
"${PADDLE_SOURCE_DIR}/paddle/fluid/prim/utils/static/static_tensor_operants.cc.tmp"
)
set(tmp_static_tensor_operants_h_path
"${PADDLE_SOURCE_DIR}/paddle/fluid/prim/utils/static/static_tensor_operants.h.tmp"
)
set(eager_tensor_operants_cc_path
"${PADDLE_SOURCE_DIR}/paddle/fluid/prim/utils/eager/eager_tensor_operants.cc"
)
${PADDLE_SOURCE_DIR}/paddle/fluid/prim/utils/eager/eager_tensor_operants.cc)
set(eager_tensor_operants_h_path
"${PADDLE_SOURCE_DIR}/paddle/fluid/prim/utils/eager/eager_tensor_operants.h"
)
${PADDLE_SOURCE_DIR}/paddle/fluid/prim/utils/eager/eager_tensor_operants.h)
set(static_tensor_operants_cc_path
"${PADDLE_SOURCE_DIR}/paddle/fluid/prim/utils/static/static_tensor_operants.cc"
${PADDLE_SOURCE_DIR}/paddle/fluid/prim/utils/static/static_tensor_operants.cc
)
set(static_tensor_operants_h_path
"${PADDLE_SOURCE_DIR}/paddle/fluid/prim/utils/static/static_tensor_operants.h"
)
${PADDLE_SOURCE_DIR}/paddle/fluid/prim/utils/static/static_tensor_operants.h
)
set(tmp_eager_tensor_operants_cc_path ${eager_tensor_operants_cc_path}.tmp)
set(tmp_eager_tensor_operants_h_path ${eager_tensor_operants_h_path}.tmp)
set(tmp_static_tensor_operants_cc_path ${static_tensor_operants_cc_path}.tmp)
set(tmp_static_tensor_operants_h_path ${static_tensor_operants_h_path}.tmp)
set(tensor_api_yaml_path
${PADDLE_SOURCE_DIR}/paddle/phi/api/yaml/tensor_api.yaml)

message("Prim tensor operants code generator")
execute_process(
Expand All @@ -114,7 +106,7 @@ execute_process(
${tmp_eager_tensor_operants_cc_path} --static_tensor_operants_header_path
${tmp_static_tensor_operants_h_path} --static_tensor_operants_source_path
${tmp_static_tensor_operants_cc_path} --api_prim_yaml_path
${api_prim_yaml_path}
${tensor_api_yaml_path}
RESULT_VARIABLE _result)
if(${_result})
message(FATAL_ERROR "Prim tensor operants generate failed, exiting.")
Expand Down
10 changes: 8 additions & 2 deletions paddle/fluid/prim/api/auto_code_generated/tensor_operants_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

#include "paddle/phi/api/include/operants_base.h"
#include "paddle/phi/api/include/tensor.h"
#include "paddle/phi/common/scalar.h"
#include "paddle/phi/common/int_array.h"
#include "paddle/phi/core/macros.h"

"""
Expand All @@ -35,6 +37,8 @@
namespace prim {

using Tensor = paddle::experimental::Tensor;
using Scalar = paddle::experimental::Scalar;
using IntArray = paddle::experimental::IntArray;
using TensorOperantsBase = paddle::operants::TensorOperantsBase;

class EagerTensorOperants : public TensorOperantsBase {
Expand Down Expand Up @@ -85,6 +89,8 @@ class EagerTensorOperants : public TensorOperantsBase {

#include "paddle/phi/api/include/operants_base.h"
#include "paddle/phi/api/include/tensor.h"
#include "paddle/phi/common/scalar.h"
#include "paddle/phi/common/int_array.h"
#include "paddle/phi/core/macros.h"

"""
Expand All @@ -95,6 +101,8 @@ class EagerTensorOperants : public TensorOperantsBase {
namespace prim {

using Tensor = paddle::experimental::Tensor;
using Scalar = paddle::experimental::Scalar;
using IntArray = paddle::experimental::IntArray;
using TensorOperantsBase = paddle::operants::TensorOperantsBase;

class StaticTensorOperants : public TensorOperantsBase {
Expand Down Expand Up @@ -269,8 +277,6 @@ def generate_tensor_operants_api(

with open(api_prim_path, 'rt') as f:
api_prims = yaml.safe_load(f)
# white list temporarily
api_prims = ('add', 'subtract', 'multiply', 'divide')

for api in apis:
eager_api = PrimTensorAPI(api, api_prims)
Expand Down
38 changes: 17 additions & 21 deletions paddle/fluid/prim/api/composite_backward/composite_backward_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ using IntArray =
template <typename T>
void tanh_grad(const Tensor& out, const Tensor& grad_out, Tensor* grad_x) {
if (!grad_x) return;
auto tmp = pow<T>(out, 2.0);
auto tmp = out.pow(2.0);
tmp = scale<T>(tmp, -1.0, 1.0, true);
auto grad_x_tmp = grad_out * tmp;
set_output<T>(grad_x_tmp, grad_x);
Expand All @@ -48,8 +48,8 @@ void subtract_grad(const Tensor& x,
if (!reduce_dim.size()) {
by_pass<T>(scale_out_grad, dy);
} else {
auto dy_reduce_res = sum<T>(
scale_out_grad, phi::vectorize(reduce_dim), y.dtype(), false);
auto dy_reduce_res =
scale_out_grad.sum(phi::vectorize(reduce_dim), y.dtype(), false);
auto dy_tmp = reshape<T>(dy_reduce_res, phi::vectorize(y.dims()));
set_output<T>(dy_tmp, dy);
}
Expand All @@ -65,7 +65,7 @@ void subtract_grad(const Tensor& x,
by_pass<T>(out_grad, dx);
} else {
auto dx_reduce_res =
sum<T>(out_grad, phi::vectorize(reduce_dim), x.dtype(), false);
out_grad.sum(phi::vectorize(reduce_dim), x.dtype(), false);
auto dx_tmp = reshape<T>(dx_reduce_res, phi::vectorize(x.dims()));
set_output<T>(dx_tmp, dx);
}
Expand All @@ -90,7 +90,7 @@ void add_grad(const Tensor& x,
by_pass<T>(out_grad, dy);
} else {
auto dy_reduce_res =
sum<T>(out_grad, phi::vectorize(reduce_dim), y.dtype(), false);
out_grad.sum(phi::vectorize(reduce_dim), y.dtype(), false);
auto dy_tmp = reshape<T>(dy_reduce_res, phi::vectorize(y.dims()));
set_output<T>(dy_tmp, dy);
}
Expand All @@ -107,7 +107,7 @@ void add_grad(const Tensor& x,
by_pass<T>(out_grad, dx);
} else {
auto dx_reduce_res =
sum<T>(out_grad, phi::vectorize(reduce_dim), x.dtype(), false);
out_grad.sum(phi::vectorize(reduce_dim), x.dtype(), false);
auto dx_tmp = reshape<T>(dx_reduce_res, phi::vectorize(x.dims()));
set_output<T>(dx_tmp, dx);
}
Expand Down Expand Up @@ -138,7 +138,7 @@ void sum_grad(const Tensor& x,
}
auto x_grad_tmp = Tensor();
if (x_dim_size == 1) {
x_grad_tmp = expand<T>(out_grad, IntArray(x_dim));
x_grad_tmp = out_grad.expand(IntArray(x_dim));
} else {
if (!keepdim) {
auto axis_ = std::vector<int64_t>();
Expand All @@ -150,9 +150,9 @@ void sum_grad(const Tensor& x,
axis_ = axis.GetData();
}
auto out_grad_ = unsqueeze<T>(out_grad, axis_);
x_grad_tmp = expand<T>(out_grad_, IntArray(x_dim));
x_grad_tmp = out_grad_.expand(IntArray(x_dim));
} else {
x_grad_tmp = expand<T>(out_grad, IntArray(x_dim));
x_grad_tmp = out_grad.expand(IntArray(x_dim));
}
}

Expand All @@ -169,7 +169,7 @@ void divide_grad(const Tensor& x,
Tensor* dy) {
if (dy) {
// dy = -(x/y^2) * dout
auto tmp0 = pow<T>(y, 2.0);
auto tmp0 = y.pow(2.0);
auto tmp1 = x / tmp0;
auto tmp2 = scale<T>(tmp1, -1.0, 0.0, true);
auto dy_res = tmp2 * out_grad;
Expand All @@ -180,7 +180,7 @@ void divide_grad(const Tensor& x,
set_output<T>(dy_res, dy);
} else {
auto dy_reduce_res =
sum<T>(dy_res, phi::vectorize(reduce_dim), y.dtype(), false);
dy_res.sum(phi::vectorize(reduce_dim), y.dtype(), false);
auto dy_tmp = reshape<T>(dy_reduce_res, phi::vectorize(y.dims()));
set_output<T>(dy_tmp, dy);
}
Expand All @@ -199,7 +199,7 @@ void divide_grad(const Tensor& x,
set_output<T>(dx_res, dx);
} else {
auto dx_reduce_res =
sum<T>(dx_res, phi::vectorize(reduce_dim), x.dtype(), false);
dx_res.sum(phi::vectorize(reduce_dim), x.dtype(), false);
auto dx_tmp = reshape<T>(dx_reduce_res, phi::vectorize(x.dims()));
set_output<T>(dx_tmp, dx);
}
Expand Down Expand Up @@ -233,10 +233,8 @@ void multiply_grad(const Tensor& x,
if (!axes.size()) {
set_output<T>(x_grad_unreduce, x_grad);
} else {
auto x_grad_reduced = sum<T>(x_grad_unreduce,
phi::vectorize(axes),
x_grad_unreduce.dtype(),
false);
auto x_grad_reduced = x_grad_unreduce.sum(
phi::vectorize(axes), x_grad_unreduce.dtype(), false);
if (x_grad_reduced.dims().size() != x.dims().size()) {
x_grad_reduced = reshape<T>(x_grad_reduced, x.shape());
}
Expand All @@ -253,10 +251,8 @@ void multiply_grad(const Tensor& x,
if (!axes.size()) {
set_output<T>(y_grad_unreduce, y_grad);
} else {
auto y_grad_reduced = sum<T>(y_grad_unreduce,
phi::vectorize(axes),
y_grad_unreduce.dtype(),
false);
auto y_grad_reduced = y_grad_unreduce.sum(
phi::vectorize(axes), y_grad_unreduce.dtype(), false);
if (y_grad_reduced.dims().size() != y.dims().size()) {
y_grad_reduced = reshape<T>(y_grad_reduced, y.shape());
}
Expand All @@ -280,7 +276,7 @@ void expand_grad(const Tensor& x,
if (!axes.size()) {
by_pass<T>(out_grad, x_grad);
} else {
auto reduced = sum<T>(out_grad, phi::vectorize(axes), x.dtype(), false);
auto reduced = out_grad.sum(phi::vectorize(axes), x.dtype(), false);
if (reduced.dims().size() != x.dims().size()) {
reduced = reshape<T>(reduced, x.shape());
}
Expand Down
33 changes: 33 additions & 0 deletions paddle/phi/api/include/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ namespace paddle {

namespace experimental {

class Tensor;

template <typename T>
class ScalarBase;
using Scalar = paddle::experimental::ScalarBase<Tensor>;

template <typename T>
class IntArrayBase;
using IntArray = paddle::experimental::IntArrayBase<Tensor>;

class AbstractAutogradMeta {
public:
// No AbstractAutogradMeta should be created
Expand Down Expand Up @@ -646,12 +656,35 @@ class PADDLE_API Tensor final {
*/
std::string name_{""};

public:
// Tensor C++ APIs
// Example: Tensor add(const Tensor& other) const;
Tensor add(const Tensor& y) const;
Tensor divide(const Tensor& y) const;
Tensor multiply(const Tensor& y) const;
Tensor subtract(const Tensor& y) const;
Tensor exp() const;
Tensor floor() const;
Tensor gather_nd(const Tensor& index) const;
Tensor log() const;
Tensor pow(const Scalar& y) const;
Tensor roll(const IntArray& shifts, const std::vector<int64_t>& axis) const;
Tensor scatter(const Tensor& index,
const Tensor& updates,
bool overwrite) const;
Tensor scatter_nd_add(const Tensor& index, const Tensor& updates) const;
Tensor abs() const;
Tensor assign() const;
Tensor elementwise_pow(const Tensor& y) const;
Tensor expand(const IntArray& shape) const;
Tensor matmul(const Tensor& y, bool transpose_x, bool transpose_y) const;
Tensor max(const IntArray& axis, bool keepdim) const;
Tensor maximum(const Tensor& y) const;
Tensor minimum(const Tensor& y) const;
Tensor prod(const IntArray& dims, bool keep_dim, bool reduce_all) const;
jiahy0825 marked this conversation as resolved.
Show resolved Hide resolved
Tensor scale(const Scalar& scale, float bias, bool bias_after_scale) const;
Tensor sum(const IntArray& axis, DataType dtype, bool keepdim) const;
Tensor tile(const IntArray& repeat_times) const;
};

} // namespace experimental
Expand Down
5 changes: 4 additions & 1 deletion paddle/phi/api/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ set(wrapped_infermeta_source_file
${CMAKE_SOURCE_DIR}/paddle/phi/infermeta/generated.cc)

# tensor and tensor operants file
set(tensor_api_yaml_path
${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/tensor_api.yaml)
set(tensor_gen_file
${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/generator/tensor_gen.py)
${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/generator/tensor_operants_gen.py)
set(operants_base_file
${CMAKE_SOURCE_DIR}/paddle/phi/api/include/operants_base.h)
set(tensor_api_source_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/tensor_api.cc)
Expand Down Expand Up @@ -249,6 +251,7 @@ execute_process(
--phi_tensor_operants_source_path ${phi_tensor_operants_source_file_tmp}
--operants_manager_header_path ${operants_manager_header_file_tmp}
--operants_manager_source_path ${operants_manager_source_file_tmp}
--tensor_api_yaml_path ${tensor_api_yaml_path}
RESULT_VARIABLE _result)
if(${_result})
message(FATAL_ERROR "tensor codegen failed, exiting.")
Expand Down
Loading