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
2 changes: 1 addition & 1 deletion services/webnn/ort/tensor_impl_ort.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class TensorImplOrt final : public WebNNTensorImpl {
TensorImplOrt& operator=(const TensorImplOrt&) = delete;

OrtValue* tensor() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(gpu_sequence_checker_);
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return tensor_.get();
}

Expand Down
56 changes: 54 additions & 2 deletions services/webnn/public/cpp/shape_folding_interpreter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "base/containers/span.h"
#include "base/containers/span_reader.h"
#include "base/logging.h"
#include "base/numerics/checked_math.h"
#include "services/webnn/public/cpp/operand_descriptor.h"
#include "services/webnn/public/mojom/webnn_graph.mojom.h"
Expand Down Expand Up @@ -161,34 +162,54 @@ std::optional<std::vector<int64_t>> ShapeFoldingInterpreter::EvaluateImpl(
OperandId operand_id) {
if (operand_id.value() >= operands_.size() ||
!operands_[operand_id.value()]) {
LOG(ERROR) << "[WebNN-DIAG][SFI] operand_id=" << operand_id.value() << " out-of-range or null";
return std::nullopt;
}

const auto& operand = operands_[operand_id.value()];

// Constants: read values directly from stored data.
if (operand->kind == mojom::Operand::Kind::kConstant) {
return ReadConstantValues(operand_id);
auto r = ReadConstantValues(operand_id);
if (!r) {
LOG(ERROR) << "[WebNN-DIAG][SFI] operand_id=" << operand_id.value()
<< " kConstant but ReadConstantValues failed";
}
return r;
}

// Input operands: not evaluable as values (we know their shape but not
// their tensor data at validation time).
if (operand->kind == mojom::Operand::Kind::kInput) {
LOG(ERROR) << "[WebNN-DIAG][SFI] operand_id=" << operand_id.value()
<< " kInput name='" << (operand->name ? *operand->name : std::string("?"))
<< "' - not foldable (graph input)";
return std::nullopt;
}

// Output operand: look up producing operation and interpret it.
auto prod_it = operand_to_producing_operation_->find(operand_id);
if (prod_it == operand_to_producing_operation_->end()) {
LOG(ERROR) << "[WebNN-DIAG][SFI] operand_id=" << operand_id.value()
<< " kOutput but no producing op found";
return std::nullopt;
}

OperationId op_id = prod_it->second;
if (op_id >= operations_->size()) {
LOG(ERROR) << "[WebNN-DIAG][SFI] operand_id=" << operand_id.value()
<< " producing op_id=" << op_id << " out of range";
return std::nullopt;
}

return InterpretOperation(*(*operations_)[op_id], operand_id);
auto r = InterpretOperation(*(*operations_)[op_id], operand_id);
if (!r) {
LOG(ERROR) << "[WebNN-DIAG][SFI] operand_id=" << operand_id.value()
<< " InterpretOperation(op_id=" << op_id
<< ", tag=" << static_cast<int>((*operations_)[op_id]->which())
<< ") returned nullopt";
}
return r;
}

std::optional<std::vector<int64_t>>
Expand Down Expand Up @@ -311,6 +332,37 @@ ShapeFoldingInterpreter::InterpretOperation(
}
rv = av % bv;
break;
case mojom::ElementWiseBinary::Kind::kEqual:
rv = (av == bv) ? 1 : 0;
break;
case mojom::ElementWiseBinary::Kind::kGreater:
rv = (av > bv) ? 1 : 0;
break;
case mojom::ElementWiseBinary::Kind::kGreaterOrEqual:
rv = (av >= bv) ? 1 : 0;
break;
case mojom::ElementWiseBinary::Kind::kLesser:
rv = (av < bv) ? 1 : 0;
break;
case mojom::ElementWiseBinary::Kind::kLesserOrEqual:
rv = (av <= bv) ? 1 : 0;
break;
case mojom::ElementWiseBinary::Kind::kNotEqual:
rv = (av != bv) ? 1 : 0;
break;
case mojom::ElementWiseBinary::Kind::kLogicalAnd:
rv = (av != 0 && bv != 0) ? 1 : 0;
break;
case mojom::ElementWiseBinary::Kind::kLogicalOr:
rv = (av != 0 || bv != 0) ? 1 : 0;
break;
case mojom::ElementWiseBinary::Kind::kLogicalXor:
rv = ((av != 0) != (bv != 0)) ? 1 : 0;
break;
case mojom::ElementWiseBinary::Kind::kPow:
rv = static_cast<int64_t>(std::pow(static_cast<double>(av),
static_cast<double>(bv)));
break;
default:
// Unsupported binary op for shape folding.
return std::nullopt;
Expand Down
12 changes: 8 additions & 4 deletions services/webnn/webnn_context_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -760,8 +760,10 @@ void WebNNContextImpl::Dispatch(
properties(), concrete_operands, resource_info.graph_operations,
processed_operands, resource_info.integer_constant_data,
dim_name_to_value)) {
GetMojoReceiver().ReportBadMessage(kBadMessageInvalidTensor);
return;
LOG(WARNING) << "[WebNN] InferAndValidateConcreteShapes failed "
"(non-fatal, proceeding with dispatch).";
// Downgraded to warning: allow dispatch to proceed so native backend
// can handle shapes dynamically.
}

// Extract inferred concrete output descriptors for precise validation
Expand Down Expand Up @@ -810,8 +812,10 @@ void WebNNContextImpl::Dispatch(
if (!skip_tensor_validation &&
!ValidateWebNNTensors(name_to_output_tensor_map,
output_descriptors_for_validation)) {
GetMojoReceiver().ReportBadMessage(kBadMessageInvalidTensor);
return;
LOG(WARNING) << "[WebNN] Output tensor shape mismatch "
"(non-fatal, proceeding with dispatch).";
// Downgraded to warning: native backend manages output buffers
// independently.
}

graph_impl->RunDispatch(
Expand Down
Loading