Skip to content

Commit

Permalink
move scalar and polish enforce
Browse files Browse the repository at this point in the history
  • Loading branch information
chenwhql committed Oct 22, 2021
1 parent 19b1095 commit 24ef6c5
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 35 deletions.
1 change: 1 addition & 0 deletions paddle/fluid/framework/operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ limitations under the License. */
#include "paddle/fluid/framework/var_type.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/profiler.h"
#include "paddle/pten/common/scalar.h"

namespace paddle {
namespace framework {
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/imperative/prepared_operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "paddle/fluid/framework/details/nan_inf_utils.h"
#include "paddle/fluid/framework/pten_utils.h"
#include "paddle/fluid/imperative/infer_shape_context.h"
#include "paddle/pten/common/scalar.h"
#include "paddle/utils/small_vector.h"
#ifdef PADDLE_WITH_XPU
#include "paddle/fluid/platform/xpu/xpu_op_list.h"
Expand Down
1 change: 0 additions & 1 deletion paddle/pten/api/include/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ limitations under the License. */
#include "paddle/pten/core/dense_tensor.h"
#include "paddle/pten/core/kernel_context.h"
#include "paddle/pten/core/kernel_factory.h"
#include "paddle/pten/core/scalar.h"
#include "paddle/pten/core/tensor_meta.h"
5 changes: 4 additions & 1 deletion paddle/pten/common/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License. */

#include <ostream>

#include "paddle/fluid/platform/enforce.h"

namespace paddle {
namespace experimental {

Expand Down Expand Up @@ -78,7 +80,8 @@ inline std::ostream& operator<<(std::ostream& os, Backend backend) {
os << "CUDNN";
break;
default:
throw std::runtime_error("Invalid Backend type.");
PADDLE_THROW(platform::errors::InvalidArgument(
"Invalid enum backend type `%d`.", static_cast<int>(backend)));
}
return os;
}
Expand Down
7 changes: 3 additions & 4 deletions paddle/pten/common/data_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ limitations under the License. */
#include "paddle/fluid/platform/bfloat16.h"
#include "paddle/fluid/platform/complex.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/errors.h"
#include "paddle/fluid/platform/float16.h"

namespace paddle {
Expand Down Expand Up @@ -164,13 +163,13 @@ inline std::ostream& operator<<(std::ostream& os, DataType dtype) {
os << "complex128";
break;
default:
// TODO(chenweihang): change to enforce later
throw std::runtime_error("Invalid DataType type.");
PADDLE_THROW(platform::errors::InvalidArgument(
"Invalid enum data type `%d`.", static_cast<int>(dtype)));
}
return os;
}

inline DataType& operator++(DataType& dtype, int) {
inline DataType& operator++(DataType dtype, int) {
dtype =
DataType(static_cast<std::underlying_type<DataType>::type>(dtype) + 1);
return dtype;
Expand Down
12 changes: 7 additions & 5 deletions paddle/pten/common/layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ limitations under the License. */

#pragma once

#include "paddle/fluid/platform/enforce.h"

namespace paddle {
namespace experimental {

Expand All @@ -26,8 +28,8 @@ enum class DataLayout {
NUM_DATA_LAYOUTS,
};

inline std::ostream& operator<<(std::ostream& os, DataLayout dtype) {
switch (dtype) {
inline std::ostream& operator<<(std::ostream& os, DataLayout layout) {
switch (layout) {
case DataLayout::UNDEFINED:
os << "Undefined";
break;
Expand All @@ -44,13 +46,13 @@ inline std::ostream& operator<<(std::ostream& os, DataLayout dtype) {
os << "MKLDNN";
break;
default:
// TODO(chenweihang): change to enforce later
throw std::runtime_error("Invalid DataLayout type.");
PADDLE_THROW(platform::errors::InvalidArgument(
"Invalid enum data layout type `%d`.", static_cast<int>(layout)));
}
return os;
}

inline DataLayout& operator++(DataLayout& layout, int) {
inline DataLayout& operator++(DataLayout layout, int) {
layout = DataLayout(
static_cast<std::underlying_type<DataLayout>::type>(layout) + 1);
return layout;
Expand Down
17 changes: 14 additions & 3 deletions paddle/pten/core/scalar.h → paddle/pten/common/scalar.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ limitations under the License. */

#pragma once

namespace pten {
#include <cstdint>

#include "paddle/fluid/platform/enforce.h"

namespace paddle {
namespace experimental {

class Scalar {
public:
Expand Down Expand Up @@ -43,7 +48,8 @@ class Scalar {
case Tag::HAS_B:
return static_cast<T>(data_.b);
default:
throw std::runtime_error("Invalid Scalar type.");
PADDLE_THROW(platform::errors::InvalidArgument(
"Invalid enum scalar type tag `%d`.", static_cast<int>(tag)));
}
}

Expand All @@ -60,4 +66,9 @@ class Scalar {
} data_;
};

} // namespace pten
} // namespace experimental
} // namespace paddle

namespace pten {
using Scalar = paddle::experimental::Scalar;
}
4 changes: 2 additions & 2 deletions paddle/pten/core/kernel_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

#pragma once

#include "paddle/pten/common/scalar.h"
#include "paddle/pten/core/dense_tensor.h"
#include "paddle/pten/core/kernel_context.h"
#include "paddle/pten/core/kernel_def.h"
#include "paddle/pten/core/scalar.h"

// See Note [ Why still include the fluid headers? ]
#include "paddle/fluid/platform/device_context.h"
Expand Down Expand Up @@ -163,7 +163,7 @@ struct KernelImpl<Return (*)(Args...), kernel_fn> {
PT_SPECIALIZE_KernelCallHelper_FOR_ATTRIBUTE(int);
PT_SPECIALIZE_KernelCallHelper_FOR_ATTRIBUTE(int64_t);
PT_SPECIALIZE_KernelCallHelper_FOR_ATTRIBUTE(paddle::platform::float16);
PT_SPECIALIZE_KernelCallHelper_FOR_ATTRIBUTE(const pten::Scalar&);
PT_SPECIALIZE_KernelCallHelper_FOR_ATTRIBUTE(const Scalar&);

/* Output Helpers */

Expand Down
11 changes: 5 additions & 6 deletions paddle/pten/hapi/include/backend_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ limitations under the License. */

#include <ostream>

// TODO(chenweihang): move this file into hapi/include when compile
#include "paddle/fluid/platform/enforce.h"
#include "paddle/pten/common/backend.h"

namespace paddle {
namespace experimental {

Expand All @@ -39,10 +38,10 @@ class BackendSet final {
uint64_t bitset() const { return bitset_; }

bool inline Has(Backend b) const {
// TODO(chenweihang): replace by internal assert method later
if (b == Backend::UNDEFINED) {
throw std::runtime_error("Backend argument can't be UNDEFINED.");
}
PADDLE_ENFORCE_NE(b,
Backend::UNDEFINED,
platform::errors::InvalidArgument(
"Backend argument can't be UNDEFINED."));
return static_cast<bool>(bitset_ & BackendSet(b).bitset());
}
bool IsEmpty() const { return bitset_ == 0; }
Expand Down
4 changes: 2 additions & 2 deletions paddle/pten/hapi/include/creation.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
#pragma once

#include "paddle/pten/common/data_type.h"
#include "paddle/pten/core/scalar.h"
#include "paddle/pten/common/scalar.h"
#include "paddle/pten/hapi/include/tensor.h"

namespace paddle {
namespace experimental {

Tensor full_like(const Tensor& x,
const pten::Scalar& value,
const Scalar& value,
DataType dtype = DataType::UNDEFINED);

Tensor ones_like(const Tensor& x, DataType dtype = DataType::UNDEFINED);
Expand Down
7 changes: 4 additions & 3 deletions paddle/pten/hapi/include/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ limitations under the License. */
* or the corresponding components will be re-implemented.
*/
#include "paddle/fluid/framework/ddim.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/place.h"

namespace paddle {
Expand Down Expand Up @@ -93,9 +94,9 @@ class Tensor final {
*/
explicit Tensor(std::shared_ptr<pten::TensorBase> tensor_impl)
: impl_(std::move(tensor_impl)) {
if (impl_.get() == nullptr) {
throw std::runtime_error("TensorImpl with nullptr is not supported");
}
PADDLE_ENFORCE_NOT_NULL(impl_,
platform::errors::InvalidArgument(
"TensorImpl with nullptr is not supported"));
}

/* Part 2: Dimension, DataType and DataLayout methods */
Expand Down
6 changes: 3 additions & 3 deletions paddle/pten/hapi/lib/creation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace paddle {
namespace experimental {

Tensor full_like(const Tensor& x,
const pten::Scalar& value,
const Scalar& value,
paddle::experimental::DataType dtype) {
// 1. Get kernel signature and kernel
auto kernel_key_set = ParseKernelKeyByInputArgs(x);
Expand Down Expand Up @@ -63,11 +63,11 @@ Tensor full_like(const Tensor& x,
return out;
}

Tensor ones_like(const Tensor& x, paddle::experimental::DataType dtype) {
Tensor ones_like(const Tensor& x, DataType dtype) {
return full_like(x, 1, dtype);
}

Tensor zeros_like(const Tensor& x, paddle::experimental::DataType dtype) {
Tensor zeros_like(const Tensor& x, DataType dtype) {
return full_like(x, 0, dtype);
}

Expand Down
2 changes: 1 addition & 1 deletion paddle/pten/kernels/cpu/creation.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

#pragma once

#include "paddle/pten/common/scalar.h"
#include "paddle/pten/core/dense_tensor.h"
#include "paddle/pten/core/scalar.h"

#include "paddle/fluid/platform/device_context.h"

Expand Down
2 changes: 1 addition & 1 deletion paddle/pten/kernels/cuda/creation.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
// CUDA and HIP use same api
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)

#include "paddle/pten/common/scalar.h"
#include "paddle/pten/core/dense_tensor.h"
#include "paddle/pten/core/scalar.h"

#include "paddle/fluid/platform/device_context.h"

Expand Down
8 changes: 5 additions & 3 deletions paddle/pten/kernels/cuda/math.cu
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ limitations under the License. */
namespace cub = hipcub;
#endif

#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/float16.h"
#include "paddle/pten/core/convert_utils.h"
#include "paddle/pten/core/kernel_registry.h"
Expand Down Expand Up @@ -104,9 +105,10 @@ void ScaleHost(const CUDAContext& dev_ctx,
float bias,
bool bias_after_scale,
DenseTensor* out) {
if (paddle::platform::is_gpu_place(scale.place())) {
throw std::runtime_error("scale host place error.");
}
PADDLE_ENFORCE_EQ(paddle::platform::is_gpu_place(scale.place()),
false,
paddle::platform::errors::InvalidArgument(
"Scale argument isn't a host tensor."));
eigen::Scale<CUDAContext, T>(dev_ctx,
x,
static_cast<float>(*scale.data<T>()),
Expand Down

1 comment on commit 24ef6c5

@paddle-bot-old
Copy link

@paddle-bot-old paddle-bot-old bot commented on 24ef6c5 Oct 22, 2021

Choose a reason for hiding this comment

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

🕵️ CI failures summary

🔍 PR: #34425 Commit ID: 24ef6c5 contains failed CI.

🔹 Failed: PR-CI-APPROVAL

approve_failed
2021-10-22 15:35:07 正在保存至: “bk.txt”
2021-10-22 15:35:07 0K 100% 2.71M=0s
2021-10-22 15:35:07 2021-10-22 15:35:07 (2.71 MB/s) - 已保存 “bk.txt” [5/5])
2021-10-22 15:35:15 ****************
2021-10-22 15:35:15 0. You must have one RD (lanxianghit (Recommend), phlrain or luotao1) approval for changing the FLAGS, which manages the environment variables.
2021-10-22 15:35:15 1. You must have Dianhai approval for change 20+ files or add than 1000+ lines of content.
2021-10-22 15:35:15 2. You must have one RD (XiaoguangHu01,chenwhql,zhiqiu,Xreki,luotao1) approval for paddle/fluid/framework/operator.h, which manages the underlying code for fluid.
2021-10-22 15:35:15 3. You must have one RD (zhiqiu (Recommend) , phlrain) approval for the changes of paddle/fluid/pybind/op_function_generator.cc, which manages the logic of automatic generating op functions for dygraph.
2021-10-22 15:35:15 4. You must have one RD (XiaoguangHu01,chenwhql,zhiqiu,Xreki,luotao1) approval for the usage of const_cast.
2021-10-22 15:35:15 5. You must have one RD (Avin0323(Recommend) or zhouwei25 or wanghuancoder or luotao1) approval for modifying unity_build_rule.cmake which the rules of Unity Build.
2021-10-22 15:35:15 There are 6 approved errors.
2021-10-22 15:35:15 ****************
2021-10-22 15:35:15 + EXCODE=6
2021-10-22 15:35:15 + echo 'EXCODE: 6'
2021-10-22 15:35:15 EXCODE: 6
2021-10-22 15:35:15 + echo 'ipipe_log_param_EXCODE: 6'
2021-10-22 15:35:15 ipipe_log_param_EXCODE: 6
2021-10-22 15:35:15 + exit 6

🔹 Failed: PR-CI-NPU

Unknown Failed
2021-10-22 15:44:27 + set +x
2021-10-22 15:44:27 + SOURCE=/paddle/build/coverage-diff
2021-10-22 15:44:27 + [[ -d /paddle/build/coverage-diff ]]
2021-10-22 15:44:27 + [[ -f /paddle/build/coverage-diff ]]
2021-10-22 15:44:27 + echo 'No such file or directory: /paddle/build/coverage-diff'
2021-10-22 15:44:27 + exit 0
2021-10-22 15:44:27 No such file or directory: /paddle/build/coverage-diff
2021-10-22 15:44:27 report uploaded
2021-10-22 15:44:27 ===================================================================
2021-10-22 15:44:27 c++-coverage
2021-10-22 15:44:27 https://xly.bce.baidu.com/ipipe/ipipe-report/report/8548555/c++-coverage/
2021-10-22 15:44:27 ===================================================================
2021-10-22 15:44:27 + [[ 7 -eq 0 ]]
2021-10-22 15:44:27 + [[ 7 -eq 4 ]]
2021-10-22 15:44:27 + [[ 7 -eq 6 ]]
2021-10-22 15:44:27 + [[ 7 -eq 7 ]]
2021-10-22 15:44:27 + echo 'Sorry, build failed.'
2021-10-22 15:44:27 + exit 7
2021-10-22 15:44:27 Sorry, build failed.

🔹 Failed: PR-CI-musl

Unknown Failed
2021-10-22 16:18:56 /paddle/paddle/pten/common/data_type.h:175:10: error: reference to local variable 'dtype' returned [-Werror=return-local-addr]
2021-10-22 16:18:56 175 | return dtype;
2021-10-22 16:18:56 | ^~~~~
2021-10-22 16:18:56 /paddle/paddle/pten/common/data_type.h:172:38: note: declared here
2021-10-22 16:18:56 172 | inline DataType& operator++(DataType dtype, int) {
2021-10-22 16:18:56 | ~~~~~~~~~^~~~~
2021-10-22 16:18:56 In file included from /paddle/paddle/pten/core/kernel_factory.h:25,
2021-10-22 16:18:56 from /paddle/paddle/pten/core/kernel_factory.cc:15:
2021-10-22 16:18:56 /paddle/paddle/pten/common/layout.h: In function 'paddle::experimental::DataLayout& paddle::experimental::operator++(paddle::experimental::DataLayout, int)':
2021-10-22 16:18:56 /paddle/paddle/pten/common/layout.h:58:10: error: reference to local variable 'layout' returned [-Werror=return-local-addr]
2021-10-22 16:18:56 58 | return layout;
2021-10-22 16:18:56 | ^~~~~~
2021-10-22 16:18:56 /paddle/paddle/pten/common/layout.h:55:42: note: declared here
2021-10-22 16:18:56 55 | inline DataLayout& operator++(DataLayout layout, int) {
2021-10-22 16:18:56 | ~~~~~~~~~~~^~~~~~
2021-10-22 16:18:56 cc1plus: all warnings being treated as errors
2021-10-22 16:18:56 make[2]: *** [paddle/pten/core/CMakeFiles/kernel_factory.dir/build.make:63: paddle/pten/core/CMakeFiles/kernel_factory.dir/kernel_factory.cc.o] Error 1
2021-10-22 16:18:56 make[1]: *** [CMakeFiles/Makefile2:1553: paddle/pten/core/CMakeFiles/kernel_factory.dir/all] Error 2
2021-10-22 16:18:56 make: *** [Makefile:130: all] Error 2

🔹 Failed: PR-CI-Inference

Unknown Failed
2021-10-22 16:18:30 ++ awk '{print $4}'
2021-10-22 16:18:30 + rate=97.16
2021-10-22 16:18:30 + echo 'ccache hit rate: 97.16%'
2021-10-22 16:18:30 ccache hit rate: 97.16%
2021-10-22 16:18:30 + echo 'ipipe_log_param_Ccache_Hit_Rate: 97.16%'
2021-10-22 16:18:30 + '[' 2 '!=' 0 ']'
2021-10-22 16:18:30 + exit 7
2021-10-22 16:18:30 + EXCODE=7
2021-10-22 16:18:30 + '[' 7 -eq 0 ']'
2021-10-22 16:18:30 + [[ 7 -eq 0 ]]
2021-10-22 16:18:30 + [[ 7 -eq 4 ]]
2021-10-22 16:18:30 + [[ 7 -eq 5 ]]
2021-10-22 16:18:30 + [[ 7 -eq 6 ]]
2021-10-22 16:18:30 + [[ 7 -eq 7 ]]
2021-10-22 16:18:30 + echo 'Sorry, build failed.'
2021-10-22 16:18:30 Sorry, build failed.
2021-10-22 16:18:30 + set -x
2021-10-22 16:18:30 + exit 7
2021-10-22 16:18:30 {build code state=7}

🔹 Failed: PR-CI-GpuPS

Unknown Failed
2021-10-22 16:21:33 + rate=95.86
2021-10-22 16:21:33 + echo 'ccache hit rate: 95.86%'
2021-10-22 16:21:33 ccache hit rate: 95.86%
2021-10-22 16:21:33 + echo 'ipipe_log_param_Ccache_Hit_Rate: 95.86%'
2021-10-22 16:21:33 + '[' 2 '!=' 0 ']'
2021-10-22 16:21:33 + exit 7
2021-10-22 16:21:33 + EXCODE=7
2021-10-22 16:21:33 + '[' 7 -eq 0 ']'
2021-10-22 16:21:33 + [[ 7 -eq 0 ]]
2021-10-22 16:21:33 + [[ 7 -eq 4 ]]
2021-10-22 16:21:33 + [[ 7 -eq 5 ]]
2021-10-22 16:21:33 + [[ 7 -eq 6 ]]
2021-10-22 16:21:33 + [[ 7 -eq 7 ]]
2021-10-22 16:21:33 + echo 'Sorry, build failed.'
2021-10-22 16:21:33 Sorry, build failed.
2021-10-22 16:21:33 + set -x
2021-10-22 16:21:33 + exit 7
2021-10-22 16:21:33 {build code state=7}
2021-10-22 16:21:43 kill agent BUILD_CODE_FAIL

🔹 Failed: PR-CI-Py3

Unknown Failed
2021-10-22 16:23:19 ++ awk '{print $4}'
2021-10-22 16:23:20 + rate=100.00
2021-10-22 16:23:20 + echo 'ccache hit rate: 100.00%'
2021-10-22 16:23:20 ccache hit rate: 100.00%
2021-10-22 16:23:20 + echo 'ipipe_log_param_Ccache_Hit_Rate: 100.00%'
2021-10-22 16:23:20 + '[' 2 '!=' 0 ']'
2021-10-22 16:23:20 + exit 7
2021-10-22 16:23:20 + EXCODE=7
2021-10-22 16:23:20 + '[' 7 -eq 0 ']'
2021-10-22 16:23:20 + [[ 7 -eq 0 ]]
2021-10-22 16:23:20 + [[ 7 -eq 4 ]]
2021-10-22 16:23:20 + [[ 7 -eq 5 ]]
2021-10-22 16:23:20 + [[ 7 -eq 6 ]]
2021-10-22 16:23:20 + [[ 7 -eq 7 ]]
2021-10-22 16:23:20 + echo 'Sorry, build failed.'
2021-10-22 16:23:20 Sorry, build failed.
2021-10-22 16:23:20 + set -x
2021-10-22 16:23:20 + exit 7
2021-10-22 16:23:20 {build code state=7}

🔹 Failed: PR-CI-Coverage

Unknown Failed
2021-10-22 16:25:57 make: *** [all] Error 2
2021-10-22 16:25:57 + build_error=2
2021-10-22 16:25:57 + collect_ccache_hits
2021-10-22 16:25:57 ++ ccache -s
2021-10-22 16:25:57 ++ grep 'cache hit rate'
2021-10-22 16:25:57 ++ awk '{print $4}'
2021-10-22 16:25:57 + rate=68.24
2021-10-22 16:25:57 + echo 'ccache hit rate: 68.24%'
2021-10-22 16:25:57 ccache hit rate: 68.24%
2021-10-22 16:25:57 + echo 'ipipe_log_param_Ccache_Hit_Rate: 68.24%'
2021-10-22 16:25:57 + '[' 2 '!=' 0 ']'
2021-10-22 16:25:57 + exit 7
2021-10-22 16:25:57 + EXCODE=7
2021-10-22 16:25:57 + '[' 7 -eq 0 ']'
2021-10-22 16:25:57 + set +x
2021-10-22 16:25:57 Sorry, build failed.
2021-10-22 16:25:57 + exit 7
2021-10-22 16:25:57 {build code state=7}
2021-10-22 16:26:07 kill agent BUILD_CODE_FAIL

Please sign in to comment.