1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/ml/webnn/ml_operand.h"
#include <functional>
#include "base/numerics/safe_conversions.h"
#include "base/types/expected_macros.h"
#include "services/webnn/public/cpp/graph_validation_utils.h"
#include "services/webnn/public/cpp/operand_descriptor.h"
#include "services/webnn/public/cpp/webnn_errors.h"
#include "third_party/blink/renderer/modules/ml/webnn/ml_constant_operand.h"
#include "third_party/blink/renderer/modules/ml/webnn/ml_graph_builder.h"
#include "third_party/blink/renderer/modules/ml/webnn/ml_graph_utils.h"
#include "third_party/blink/renderer/modules/ml/webnn/ml_operator.h"
namespace blink {
// static
base::expected<MLOperand*, String> MLOperand::ValidateAndCreateInput(
const webnn::ContextProperties& context_properties,
MLGraphBuilder* builder,
V8MLOperandDataType::Enum v8_data_type,
Vector<uint32_t> dimensions,
String name) {
if (name.empty()) {
return base::unexpected("The name is empty.");
}
const webnn::OperandDataType data_type = FromBlinkDataType(v8_data_type);
if (!context_properties.data_type_limits.input.Has(data_type)) {
return base::unexpected(String(webnn::NotSupportedInputTypeError(
name.Utf8(), data_type, context_properties.data_type_limits.input)));
}
ASSIGN_OR_RETURN(
webnn::OperandDescriptor descriptor,
webnn::OperandDescriptor::Create(
context_properties, data_type, dimensions,
webnn::GetErrorLabelPrefix(base::StrCat({"input ", name.Utf8()}))),
[](std::string error) { return String(error); });
auto* input = MakeGarbageCollected<MLOperand>(
builder, webnn::mojom::blink::Operand::Kind::kInput,
std::move(descriptor));
input->name_ = std::move(name);
return input;
}
// static
MLOperand* MLOperand::CreateOutput(MLGraphBuilder* builder,
webnn::OperandDescriptor descriptor,
MLOperator* ml_operator) {
CHECK(ml_operator);
auto* output = MakeGarbageCollected<MLOperand>(
builder, webnn::mojom::blink::Operand::Kind::kOutput,
std::move(descriptor));
output->operator_ = ml_operator;
return output;
}
MLOperand::MLOperand(MLGraphBuilder* builder,
webnn::mojom::blink::Operand::Kind kind,
webnn::OperandDescriptor descriptor)
: builder_(builder), kind_(kind), descriptor_(std::move(descriptor)) {}
MLOperand::~MLOperand() = default;
MLGraphBuilder* MLOperand::Builder() const {
return builder_.Get();
}
webnn::mojom::blink::Operand::Kind MLOperand::Kind() const {
return kind_;
}
const String& MLOperand::Name() const {
CHECK_EQ(kind_, webnn::mojom::blink::Operand::Kind::kInput);
return name_;
}
MLOperator* MLOperand::Operator() const {
CHECK_EQ(kind_, webnn::mojom::blink::Operand::Kind::kOutput);
return operator_.Get();
}
HeapHashSet<Member<MLOperator>>& MLOperand::DependentOperators() {
return dependent_operators_;
}
const webnn::OperandDescriptor& MLOperand::Descriptor() const {
return descriptor_;
}
webnn::OperandDataType MLOperand::DataType() const {
return descriptor_.data_type();
}
const std::vector<uint32_t>& MLOperand::Shape() const {
return descriptor_.shape();
}
size_t MLOperand::NumberOfElements() const {
return descriptor_.NumberOfElements();
}
size_t MLOperand::ByteLength() const {
return descriptor_.PackedByteLength();
}
wtf_size_t MLOperand::Rank() const {
static_assert(sizeof(descriptor_.Rank()) == sizeof(wtf_size_t));
return static_cast<wtf_size_t>(descriptor_.Rank());
}
Vector<uint32_t> MLOperand::shape() const {
return Vector<uint32_t>(descriptor_.shape());
}
V8MLOperandDataType MLOperand::dataType() const {
return ToBlinkDataType(descriptor_.data_type());
}
MLConstantOperand const* MLOperand::AsConstantOperand() const {
CHECK_EQ(kind_, webnn::mojom::blink::Operand::Kind::kConstant);
return static_cast<MLConstantOperand const*>(this);
}
void MLOperand::AddDependentOperator(MLOperator* ml_operator) {
dependent_operators_.insert(ml_operator);
}
void MLOperand::Trace(Visitor* visitor) const {
visitor->Trace(builder_);
visitor->Trace(operator_);
visitor->Trace(dependent_operators_);
ScriptWrappable::Trace(visitor);
}
} // namespace blink
|