Skip to content
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
19 changes: 14 additions & 5 deletions paddle/cinn/runtime/buffer.cc
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,30 @@ Shape::Shape(const Shape &other)
}

void Shape::Resize(int ndim) {
CHECK_GT(ndim, 0);
PADDLE_ENFORCE_GT(ndim,
0,
phi::errors::InvalidArgument(
"Target dimension to resize must be greater than 0."));
ndims_ = ndim;
if (data_) delete data_;
data_ = new value_type[ndim];
}

Shape::value_type &Shape::operator[](int i) {
CHECK_GT(ndims_, 0) << "shape is empty";
CHECK_LT(i, ndims_) << "index " << i << "out of range " << ndims_;
PADDLE_ENFORCE_GT(ndims_, 0, phi::errors::InvalidArgument("Shape is empty."));
PADDLE_ENFORCE_LT(
i,
ndims_,
phi::errors::OutOfRange("Index %d out of range %d.", i, ndims_));
return data_[i];
}

Shape::value_type Shape::operator[](int i) const {
CHECK_GT(ndims_, 0) << "shape is empty";
CHECK_LT(i, ndims_) << "index " << i << "out of range " << ndims_;
PADDLE_ENFORCE_GT(ndims_, 0, phi::errors::InvalidArgument("Shape is empty."));
PADDLE_ENFORCE_LT(
i,
ndims_,
phi::errors::OutOfRange("Index %d out of range %d.", i, ndims_));
return data_[i];
}

Expand Down
27 changes: 22 additions & 5 deletions paddle/cinn/runtime/buffer.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <glog/logging.h>

#include <string>
#include "paddle/common/enforce.h"
/**
* runtime::Buffer is an encapsulation of memory operations.
*/
Expand Down Expand Up @@ -68,9 +69,13 @@ class Buffer {

//! Allocate the memory in host device.
void AllocHost() {
CHECK(shape_.defined());
PADDLE_ENFORCE_EQ(
shape_.defined(),
true,
phi::errors::InvalidArgument("shape haven't been defined."));
data_ = new T[shape_.num_elements()];
CHECK(data_) << "alloc buffer failed";
PADDLE_ENFORCE_NOT_NULL(data_,
phi::errors::NotFound("alloc buffer failed."));
}
//! Deallocate the memory in host device.
void DeallocHost() {
Expand All @@ -79,15 +84,27 @@ class Buffer {
}

T& operator()(int i0) {
CHECK_EQ(shape_.ndims(), 1);
PADDLE_ENFORCE_EQ(shape_.ndims(),
1,
phi::errors::InvalidArgument(
"Expected shape has 1 dimension, but recevied %d.",
shape_.ndims()));
return static_cast<T*>(data_)[i0];
}
T& operator()(int i0, int i1) {
CHECK_EQ(shape_.ndims(), 2);
PADDLE_ENFORCE_EQ(shape_.ndims(),
2,
phi::errors::InvalidArgument(
"Expected shape has 2 dimensions, but recevied %d.",
shape_.ndims()));
return static_cast<T*>(data_)[i0 * shape_[0] + i1];
}
T& operator()(int i0, int i1, int i2) {
CHECK_EQ(shape_.ndims(), 3);
PADDLE_ENFORCE_EQ(shape_.ndims(),
3,
phi::errors::InvalidArgument(
"Expected shape has 3 dimensions, but recevied %d.",
shape_.ndims()));
return static_cast<T*>(
data_)[i0 * shape_[1] * shape_[2] + i1 * shape_[2] + i2];
}
Expand Down
32 changes: 24 additions & 8 deletions paddle/cinn/runtime/cpu/cblas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "paddle/cinn/backends/extern_func_jit_register.h"
#include "paddle/cinn/common/cas.h"
#include "paddle/common/enforce.h"

namespace {

Expand Down Expand Up @@ -117,8 +118,11 @@ void cinn_call_cholesky_host(
memcpy(out->memory, x->memory, x->memory_size);

uint8_t bits = x->type.bits;
CHECK(bits == 32 || bits == 64)
<< "Unsupported bits = " << bits << " float data type for cholesky";
PADDLE_ENFORCE_EQ(
bits == 32 || bits == 64,
true,
phi::errors::InvalidArgument(
"Unsupported bits = %d float data type for cholesky.", bits));
char uplo = upper ? 'U' : 'L';
for (int i = 0; i < batch_size; i++) {
if (bits == 32) {
Expand All @@ -141,8 +145,12 @@ CINN_REGISTER_HELPER(cinn_cpu_mkl) {

FunctionProto::shape_inference_t inference_shape_gemm =
[](const std::vector<Expr>& args, int offset) {
CHECK_EQ(offset, 0UL) << "Only one output";
CHECK_EQ(args.size(), 12UL) << "Wrong number of arguments passed in";
PADDLE_ENFORCE_EQ(
offset, 0UL, phi::errors::InvalidArgument("Only one output."));
PADDLE_ENFORCE_EQ(args.size(),
12UL,
phi::errors::InvalidArgument(
"Wrong number of arguments passed in."));
auto M = cinn::common::AutoSimplify(args[1]);
auto N = cinn::common::AutoSimplify(args[2]);
std::vector<Expr> shape;
Expand All @@ -153,11 +161,16 @@ CINN_REGISTER_HELPER(cinn_cpu_mkl) {

FunctionProto::shape_inference_t inference_shape_gemm_batch =
[](const std::vector<Expr>& args, int offset) {
CHECK_EQ(offset, 0UL) << "Only one output";
CHECK_EQ(args.size(), 16UL) << "Wrong number of arguments passed in";
PADDLE_ENFORCE_EQ(
offset, 0UL, phi::errors::InvalidArgument("Only one output."));
PADDLE_ENFORCE_EQ(args.size(),
16UL,
phi::errors::InvalidArgument(
"Wrong number of arguments passed in."));
auto& A = args[14];
auto A_tensor = A.as_tensor();
CHECK(A_tensor);
PADDLE_ENFORCE_NOT_NULL(
A_tensor, phi::errors::InvalidArgument("expected type is tensor."));

auto batch_size = cinn::common::AutoSimplify(args[1]);
int32_t batch_size_val = batch_size.as_int32();
Expand All @@ -169,7 +182,10 @@ CINN_REGISTER_HELPER(cinn_cpu_mkl) {
int total = 1;
for (auto& v : A_tensor->shape) {
auto val = cinn::common::AutoSimplify(v);
CHECK(val.is_constant());
PADDLE_ENFORCE_EQ(
val.is_constant(),
true,
phi::errors::InvalidArgument("expected type is constant."));
shape.push_back(val);
total *= val.as_int32();
if (total >= batch_size_val) break;
Expand Down
37 changes: 25 additions & 12 deletions paddle/cinn/runtime/cpu/mkl_math.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,32 @@
#include "paddle/cinn/backends/extern_func_jit_register.h"
#include "paddle/cinn/backends/function_prototype.h"
#include "paddle/cinn/runtime/cpu/host_intrinsics.h"
#include "paddle/common/enforce.h"

#define CINN_MKL_VECTOR_MATH_FP(fn__, name__) \
void cinn_mkl_##name__##_v_fp32(cinn_buffer_t *x, cinn_buffer_t *out) { \
CHECK_EQ(x->num_elements(), out->num_elements()); \
vs##fn__(x->num_elements(), \
reinterpret_cast<float *>(x->memory), \
reinterpret_cast<float *>(out->memory)); \
} \
void cinn_mkl_##name__##_v_fp64(cinn_buffer_t *x, cinn_buffer_t *out) { \
CHECK_EQ(x->num_elements(), out->num_elements()); \
vd##fn__(x->num_elements(), \
reinterpret_cast<double *>(x->memory), \
reinterpret_cast<double *>(out->memory)); \
#define CINN_MKL_VECTOR_MATH_FP(fn__, name__) \
void cinn_mkl_##name__##_v_fp32(cinn_buffer_t *x, cinn_buffer_t *out) { \
PADDLE_ENFORCE_EQ( \
x->num_elements(), \
out->num_elements(), \
phi::errors::InvalidArgument("X's number of elements (%d) should " \
"be equal to output's (%d).", \
x->num_elements(), \
out->num_elements())); \
vs##fn__(x->num_elements(), \
reinterpret_cast<float *>(x->memory), \
reinterpret_cast<float *>(out->memory)); \
} \
void cinn_mkl_##name__##_v_fp64(cinn_buffer_t *x, cinn_buffer_t *out) { \
PADDLE_ENFORCE_EQ( \
x->num_elements(), \
out->num_elements(), \
phi::errors::InvalidArgument("X's number of elements (%d) should " \
"be equal to output's (%d).", \
x->num_elements(), \
out->num_elements())); \
vd##fn__(x->num_elements(), \
reinterpret_cast<double *>(x->memory), \
reinterpret_cast<double *>(out->memory)); \
}

CINN_MKL_VECTOR_MATH_FP(Exp, exp);
Expand Down
10 changes: 9 additions & 1 deletion paddle/cinn/runtime/cpu/mkl_math_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "paddle/cinn/common/test_helper.h"
#include "paddle/cinn/runtime/cpu/host_intrinsics.h"
#include "paddle/cinn/runtime/cpu/use_extern_funcs.h"
#include "paddle/common/enforce.h"

namespace cinn {
namespace runtime {
Expand Down Expand Up @@ -89,11 +90,18 @@ void TestCallElementwise(const std::string &fn_name,

jit->Link(module);
auto fn = jit->Lookup("fn");
CHECK(fn);
PADDLE_ENFORCE_NOT_NULL(fn, phi::errors::NotFound("fn is not found."));
auto fn_ = reinterpret_cast<void (*)(void *, int32_t)>(fn);

cinn_buffer_t *A_buf;
if (set_value != 0) {
PADDLE_ENFORCE_EQ(
x->num_elements(),
out->num_elements(),
phi::errors::InvalidArgument("X's number of elements (%d) should "
"be equal to output's (%d).",
x->num_elements(),
out->num_elements()));
A_buf = CreateBuffer({10, 10}, false, set_value);
} else {
A_buf = CreateBuffer({10, 10});
Expand Down
6 changes: 5 additions & 1 deletion paddle/cinn/runtime/cpu/mkldnn_math.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "paddle/cinn/backends/extern_func_jit_register.h"
#include "paddle/cinn/common/cas.h"
#include "paddle/common/enforce.h"

using dnnl::algorithm;
using dnnl::memory;
Expand Down Expand Up @@ -163,7 +164,10 @@ CINN_REGISTER_HELPER(cinn_cpu_mkldnn) {

FunctionProto::shape_inference_t inference_shape_conv2d_nchw =
[](const std::vector<Expr>& args, int offset) {
CHECK_EQ(args.size(), 16UL) << "Wrong number of arguments passed in";
PADDLE_ENFORCE_EQ(args.size(),
16UL,
phi::errors::InvalidArgument(
"Wrong number of arguments passed in."));
auto N = cinn::common::AutoSimplify(args[0]);
int input_h = cinn::common::AutoSimplify(args[2]).as_int32();
int input_w = cinn::common::AutoSimplify(args[3]).as_int32();
Expand Down
1 change: 1 addition & 0 deletions paddle/cinn/runtime/cpu/thread_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "paddle/cinn/backends/llvm/runtime_symbol_registry.h"
#include "paddle/cinn/common/cas.h"
#include "paddle/cinn/runtime/intrinsic.h"
#include "paddle/common/enforce.h"

int max_concurrency() {
int max_concurrency = 1;
Expand Down
12 changes: 9 additions & 3 deletions paddle/cinn/runtime/cuda/cuda_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@
#include "paddle/cinn/runtime/cuda/cuda_util.h"
#include "paddle/cinn/runtime/flags.h"
#include "paddle/cinn/utils/profiler.h"
#include "paddle/common/enforce.h"

namespace cinn {
namespace runtime {
namespace cuda {

CUDAModule::CUDAModule(const std::string& data, Kind kind)
: data_(data), kind_(kind) {
CHECK(!data.empty());
PADDLE_ENFORCE_NE(
data.empty(), true, phi::errors::PreconditionNotMet("data is is empty!"));

cudaGetDeviceCount(&num_devices_);
CHECK_GT(num_devices_, 0) << "No available devices";
PADDLE_ENFORCE_GT(
num_devices_, 0, phi::errors::ResourceExhausted("No available devices!"));

// TODO(Superjomn) Determine whether to initialize all the devices.
int current_device_id;
Expand All @@ -61,7 +64,10 @@ void CUDAModule::LaunchKernel(int device_id,
<< ", blockDim.y:" << blockDim.y << ", blockDim.z:" << blockDim.z
<< ", share_memory_size:" << share_memory_size;
auto function = GetFunction(device_id, func_name);
CHECK(function);
PADDLE_ENFORCE_NOT_NULL(
function,
phi::errors::NotFound(
"%s function not found on device %d.", func_name, device_id));
cinn::utils::RecordEvent record_run("cuLaunchKernel",
cinn::utils::EventType::kInstruction);
CUDA_DRIVER_CALL(cuLaunchKernel(function,
Expand Down
21 changes: 16 additions & 5 deletions paddle/cinn/runtime/cuda/cuda_module_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "paddle/cinn/runtime/cuda/cuda_util.h"
#include "paddle/cinn/runtime/cuda/test_util.h"
#include "paddle/cinn/runtime/cuda/use_extern_funcs.h"
#include "paddle/common/enforce.h"

namespace cinn {
namespace runtime {
Expand All @@ -43,7 +44,7 @@ void saxpy(float a, float *x, float *y, float *out, size_t n)
)ROC";

auto ptx = compiler(source_code);
CHECK(!ptx.empty());
PADDLE_ENFORCE_NE(ptx.empty(), true, phi::errors::NotFound("ptx is empty!"));

CUDAModule module(ptx, CUDAModule::Kind::PTX);
auto func = module.GetFunction(0, "saxpy");
Expand Down Expand Up @@ -73,7 +74,8 @@ TEST(CUDAModule, float16) {
)";

auto ptx = compiler(source_code);
CHECK(!ptx.empty());
PADDLE_ENFORCE_NE(
ptx.empty(), true, phi::errors::NotFound("ptx is empty!"));
return ptx;
};

Expand Down Expand Up @@ -116,7 +118,11 @@ TEST(CUDAModule, float16) {
[](float x, float16 y) -> bool {
return std::abs(x - static_cast<float>(y)) < 1e-2f;
});
CHECK(res) << "The difference between two arrays exceeds the bound.";
PADDLE_ENFORCE_EQ(
res,
true,
phi::errors::PreconditionNotMet(
"The difference between two arrays exceeds the bound."));
}

TEST(CUDAModule, bfloat16) {
Expand All @@ -142,7 +148,8 @@ TEST(CUDAModule, bfloat16) {
)";

auto ptx = compiler(source_code);
CHECK(!ptx.empty());
PADDLE_ENFORCE_NE(
ptx.empty(), true, phi::errors::NotFound("ptx is empty!"));
return ptx;
};

Expand Down Expand Up @@ -185,7 +192,11 @@ TEST(CUDAModule, bfloat16) {
[](float x, bfloat16 y) -> bool {
return std::abs(x - static_cast<float>(y)) < 1e-2f;
});
CHECK(res) << "The difference between two arrays exceeds the bound.";
PADDLE_ENFORCE_EQ(
res,
true,
phi::errors::PreconditionNotMet(
"The difference between two arrays exceeds the bound."));
}

} // namespace cuda
Expand Down
Loading