Skip to content

s/ET_LOG_MSG_AND_RETURN_IF_FALSE/ET_CHECK_OR_RETURN_FALSE #8469

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

Merged
merged 11 commits into from
Feb 18, 2025
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
42 changes: 20 additions & 22 deletions extension/llm/custom_ops/op_sdpa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,46 +594,46 @@ bool validate_flash_attention_args(
const Tensor& key,
const Tensor& value,
const optional<Tensor>& attn_mask) {
ET_LOG_MSG_AND_RETURN_IF_FALSE(query.dim() == 4, "query must be a 4D tensor");
ET_LOG_MSG_AND_RETURN_IF_FALSE(key.dim() == 4, "key must be a 4D tensor");
ET_LOG_MSG_AND_RETURN_IF_FALSE(value.dim() == 4, "value must be a 4D tensor");
ET_CHECK_OR_RETURN_FALSE(query.dim() == 4, "query must be a 4D tensor");
ET_CHECK_OR_RETURN_FALSE(key.dim() == 4, "key must be a 4D tensor");
ET_CHECK_OR_RETURN_FALSE(value.dim() == 4, "value must be a 4D tensor");

// Sizes
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
(query.size(3) == value.size(3)) && (key.size(3) == value.size(3)),
"scaled_dot_product_attention_flash_attention: Q/K/V should have the same head size");

ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
(query.scalar_type() == ScalarType::Float), "Query must be Float type");

ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
(query.scalar_type() == key.scalar_type()) &&
(query.scalar_type() == value.scalar_type()),
"Key and Value must have the same data type as Query");

ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
!attn_mask.has_value() || attn_mask.value().dim() == 2,
"Attention mask must be a 2D tensor");

ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
!attn_mask.has_value() ||
attn_mask.value().scalar_type() == query.scalar_type(),
"Attention mask must be a 2D tensor");

ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
is_contiguous_dim_order(query.dim_order().data(), query.dim()),
"key cache must be in contiguous dim order");

ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
is_contiguous_dim_order(key.dim_order().data(), key.dim()),
"value cache must be in contiguous dim order");

ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
is_contiguous_dim_order(value.dim_order().data(), value.dim()),
"value cache must be in contiguous dim order");

if (attn_mask.has_value()) {
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
is_contiguous_dim_order(
attn_mask.value().dim_order().data(), attn_mask.value().dim()),
"value cache must be in contiguous dim order");
Expand All @@ -647,21 +647,19 @@ bool validate_cache_params(
const Tensor& v_cache,
int64_t start_pos,
int64_t seq_length) {
ET_LOG_MSG_AND_RETURN_IF_FALSE(
k_cache.dim() == 4, "kcache must be a 4D tensor");
ET_CHECK_OR_RETURN_FALSE(k_cache.dim() == 4, "kcache must be a 4D tensor");

ET_LOG_MSG_AND_RETURN_IF_FALSE(
v_cache.dim() == 4, "v_cache must be a 4D tensor");
ET_CHECK_OR_RETURN_FALSE(v_cache.dim() == 4, "v_cache must be a 4D tensor");

ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
start_pos < k_cache.size(1),
"start_pos must be less than key cache at dim 1");

ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
start_pos < v_cache.size(1),
"start_pos must be less than value cache at dim 1");

ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
(start_pos + seq_length) <= k_cache.size(1),
"start_post + seq_length must be less than max seq length supported by key cache."
"start pos: %" PRId64 ", seq_length: %" PRId64
Expand All @@ -671,7 +669,7 @@ bool validate_cache_params(
seq_length,
k_cache.size(1));

ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
(start_pos + seq_length) <= v_cache.size(1),
"start_post + seq_length must be less than max seq length supported by key cache."
"start pos: %" PRId64 ", seq_length: %" PRId64
Expand All @@ -682,11 +680,11 @@ bool validate_cache_params(
v_cache.size(1));

// Make sure they are in contiguous dim order
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
is_contiguous_dim_order(k_cache.dim_order().data(), k_cache.dim()),
"key cache must be in contiguous dim order");

ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
is_contiguous_dim_order(v_cache.dim_order().data(), v_cache.dim()),
"value cache must be in contiguous dim order");

Expand Down
12 changes: 6 additions & 6 deletions extension/llm/custom_ops/op_update_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ bool validate_cache_params(
const Tensor& quantized_cache,
int64_t start_pos,
int64_t seq_length) {
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
quantized_cache.dim() == 4, "quantized cache must be a 4D tensor");

ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
quantized_value.dim() == 4, "quantized_value must be a 4D tensor");

ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
start_pos < quantized_cache.size(1),
"start_pos must be less than cache size at dim 1");

ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
(start_pos + seq_length) <= quantized_cache.size(1),
"start_post + seq_length must be less than max seq length supported by cache."
"start pos: %" PRId64 ", seq_length: %" PRId64
Expand All @@ -46,12 +46,12 @@ bool validate_cache_params(
quantized_cache.size(1));

// Make sure they are in contiguous dim order
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
is_contiguous_dim_order(
quantized_cache.dim_order().data(), quantized_cache.dim()),
"quantized cache must be in contiguous dim order");

ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
is_contiguous_dim_order(
quantized_value.dim_order().data(), quantized_value.dim()),
"quantized value must be in contiguous dim order");
Expand Down
17 changes: 8 additions & 9 deletions kernels/optimized/cpu/op_bmm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,38 @@ namespace {
// Verifies that the parameters are valid.
bool check_bmm_out_args(const Tensor& self, const Tensor& mat2, Tensor& out) {
// Ensure dimensions is 3 for all input and out
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
self.dim() == mat2.dim(),
"self.dim() %zd != mat2.dim() %zd",
self.dim(),
mat2.dim());
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
self.dim() == out.dim(),
"self.dim() %zd != out.dim() %zd",
self.dim(),
out.dim());
ET_LOG_MSG_AND_RETURN_IF_FALSE(
self.dim() == 3, "self.dim() %zd != 3", self.dim());
ET_CHECK_OR_RETURN_FALSE(self.dim() == 3, "self.dim() %zd != 3", self.dim());
// Ensure batch larger than or equals to 0
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
self.size(0) >= 0, "self.size(0) %zd < 0", self.size(0));
// Ensure batches are the same
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
self.size(0) == mat2.size(0),
"self.size(0) %zd != mat2.size(0) %zd",
self.size(0),
mat2.size(0));
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
self.size(0) == out.size(0),
"self.size(0) %zd != out.size(0) %zd",
self.size(0),
out.size(0));
// Ensure the out size is compatible with input tensors
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
mat2.size(2) == out.size(2),
"mat2.size(2) %zd != out.size(2) %zd",
mat2.size(2),
out.size(2));
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
self.size(1) == out.size(1),
"self.size(1) %zd != out.size(1) %zd",
self.size(1),
Expand Down
8 changes: 4 additions & 4 deletions kernels/portable/cpu/op_convolution_backward.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ bool check_convolution_backward_args(
Tensor& grad_input,
Tensor& grad_weight,
Tensor& grad_bias) {
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
transposed == false, "Transposed Convolution Backward not supported yet");
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
weight.dim() == 4, "Only 2D Convolution Backward supported for now");

ET_LOG_AND_RETURN_IF_FALSE(tensors_have_same_dtype(weight, input));
Expand All @@ -58,7 +58,7 @@ bool check_convolution_backward_args(
ET_LOG_AND_RETURN_IF_FALSE(tensors_have_same_dtype(grad_bias, input));
}

ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
check_convolution_args(
input,
weight,
Expand Down Expand Up @@ -89,7 +89,7 @@ bool check_convolution_backward_args(
ET_LOG_AND_RETURN_IF_FALSE(
output_size_is_valid({output_sizes, output_ndim}, input.dim() - 2));

ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
grad_output.dim() == input.dim(),
"grad_output should have same number of dimensions as input");

Expand Down
6 changes: 3 additions & 3 deletions kernels/portable/cpu/op_linear_scratch_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ bool check_linear_scratch_example_args(
const optional<Tensor>& bias,
Tensor& out,
Tensor& scratch) {
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
input.size(1) == weight.size(1), "Unexpected weight size 1");

ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
scratch.size(0) == input.size(0), "Unexpected scratch size 0");

ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
scratch.size(1) == weight.size(0), "Unexpected scratch size 1");

return true;
Expand Down
2 changes: 1 addition & 1 deletion kernels/portable/cpu/op_repeat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ bool calculate_output_size(
Tensor::SizesType* out_sizes_ptr) {
ET_LOG_AND_RETURN_IF_FALSE(repeats.size() < kTensorDimensionLimit);

ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
repeats.size() >= self_sizes.size(),
"Repeats vector size is %zu must be >= self_sizes %zu.",
repeats.size(),
Expand Down
10 changes: 5 additions & 5 deletions kernels/portable/cpu/op_repeat_interleave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@ bool check_repeat_interleave_args(
int64_t output_size_value,
int64_t repeats_sum,
Tensor& out) {
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
repeats.scalar_type() == ScalarType::Int ||
repeats.scalar_type() == ScalarType::Long,
"repeats must be int or long");
ET_LOG_MSG_AND_RETURN_IF_FALSE(repeats.dim() == 1, "repeats must be 1D");
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(repeats.dim() == 1, "repeats must be 1D");
ET_CHECK_OR_RETURN_FALSE(
output_size_value == repeats_sum,
"output_size, if provided, must be equal to repeats.sum()");
ET_LOG_AND_RETURN_IF_FALSE(tensors_have_same_dtype(repeats, out));

if (repeats.scalar_type() == ScalarType::Long) {
const int64_t* const repeats_data = repeats.const_data_ptr<int64_t>();
for (size_t i = 0; i < repeats.numel(); ++i) {
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
repeats_data[i] >= 0, "repeats cannot be negative");
}
} else {
const int32_t* const repeats_data = repeats.const_data_ptr<int32_t>();
for (size_t i = 0; i < repeats.numel(); ++i) {
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
repeats_data[i] >= 0, "repeats cannot be negative");
}
}
Expand Down
2 changes: 1 addition & 1 deletion kernels/portable/cpu/op_topk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ bool check_topk_args(
if (dim < 0) {
dim += nonzero_dim(in);
}
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
k >= 0 && k <= nonempty_size(in, dim), "selected index k out of range");
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions kernels/portable/cpu/util/activation_ops_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace executor {
bool check_gelu_args(const Tensor& in, string_view approximate, Tensor& out) {
ET_LOG_AND_RETURN_IF_FALSE(tensors_have_same_dtype(in, out));
ET_LOG_AND_RETURN_IF_FALSE(in.scalar_type() != ScalarType::Bool);
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
approximate == "tanh" || approximate == "none",
"Invalid approximation format: %.*s for gelu",
static_cast<int>(approximate.length()),
Expand All @@ -32,15 +32,15 @@ bool check_glu_args(const Tensor& in, int64_t dim, Tensor& out) {
const size_t non_negative_dim = dim < 0 ? dim + in.dim() : dim;
const size_t dim_size = in.size(non_negative_dim);

ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
dim_size % 2 == 0,
"Halving dimension must be even, but dimension %zd is size %zd",
non_negative_dim,
dim_size);

ET_LOG_AND_RETURN_IF_FALSE(tensor_is_floating_type(out));
ET_LOG_AND_RETURN_IF_FALSE(tensors_have_same_rank(in, out));
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
out.size(non_negative_dim) == dim_size / 2,
"output tensor must have half the size of the input tensor along the specified dimension.");

Expand Down Expand Up @@ -73,7 +73,7 @@ bool check_log_softmax_args(
int64_t dim,
bool half_to_float,
Tensor& out) {
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
!half_to_float, "half to float conversion is not supported on CPU");
ET_LOG_AND_RETURN_IF_FALSE(tensors_have_same_dtype(in, out));
ET_LOG_AND_RETURN_IF_FALSE(tensor_has_dim(in, dim));
Expand Down
17 changes: 8 additions & 9 deletions kernels/portable/cpu/util/advanced_index_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bool check_indices_dtypes(TensorOptList indices) {
if (indices[i].has_value()) {
const Tensor& index = indices[i].value();
ScalarType ix_type = index.scalar_type();
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
ix_type == ScalarType::Long || ix_type == ScalarType::Int ||
ix_type == ScalarType::Byte || ix_type == ScalarType::Bool,
"Index tensors should be Long, Int, Byte or Bool");
Expand All @@ -47,7 +47,7 @@ bool check_mask_indices(const Tensor& in, TensorOptList indices) {
if (indices[i].has_value()) {
const Tensor& index = indices[i].value();
if (is_mask_index(index)) {
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
index.dim() > 0, "Zero-dimensional mask index not allowed");
for (auto j = 0; j < index.dim(); j++) {
if (index.size(j) != in.size(in_i + j)) {
Expand Down Expand Up @@ -156,7 +156,7 @@ int64_t query_integral_index(
bool check_index_args(const Tensor& in, TensorOptList indices, Tensor& out) {
ET_LOG_AND_RETURN_IF_FALSE(tensors_have_same_dtype(in, out));
ET_LOG_AND_RETURN_IF_FALSE(check_indices_dtypes(indices));
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
indices.size() <= in.dim(), "Indexing too many dimensions");
ET_LOG_AND_RETURN_IF_FALSE(check_mask_indices(in, indices));
return true;
Expand Down Expand Up @@ -197,8 +197,7 @@ bool get_indices_broadcast_shape(
} else if (rev_ix_sizes[0] == 1) {
rev_ix_sizes[0] = len;
} else if (len != 1 && rev_ix_sizes[0] != len) {
ET_LOG_MSG_AND_RETURN_IF_FALSE(
false, "Broadcast of mask index failed.");
ET_CHECK_OR_RETURN_FALSE(false, "Broadcast of mask index failed.");
}
} else {
for (size_t j = 0; j < index.dim(); j++) {
Expand All @@ -209,7 +208,7 @@ bool get_indices_broadcast_shape(
} else if (rev_ix_sizes[j] == 1) {
rev_ix_sizes[j] = rev_j_size;
} else if (rev_j_size != 1 && rev_ix_sizes[j] != rev_j_size) {
ET_LOG_MSG_AND_RETURN_IF_FALSE(false, "Broadcast of index failed.");
ET_CHECK_OR_RETURN_FALSE(false, "Broadcast of index failed.");
}
}
}
Expand Down Expand Up @@ -290,11 +289,11 @@ bool get_index_out_target_size(
size_t num_null_indices = get_num_null_indices(indices);
size_t num_indexed_dims = get_num_indexed_dims(indices);

ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
num_null_indices + num_indexed_dims <= in.dim(),
"Indexing too many dimensions");

ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
in.dim() + broadcast_ndim - num_indexed_dims <= kTensorDimensionLimit,
"Out tensor would exceed number of allowed dimensions");

Expand Down Expand Up @@ -441,7 +440,7 @@ bool get_in_coord(
if (index_val < 0) {
index_val += in.size(i);
}
ET_LOG_MSG_AND_RETURN_IF_FALSE(
ET_CHECK_OR_RETURN_FALSE(
index_val >= 0 && index_val < in.size(i),
"Index %" PRId64
" is out of bounds for input dimension %zd with size %zd.",
Expand Down
Loading
Loading