|
| 1 | +// Copyright 2020 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include "third_party/blink/renderer/modules/ml/v2/ops/matmul.h" |
| 6 | + |
| 7 | +#include <memory> |
| 8 | + |
| 9 | +#include "third_party/blink/renderer/modules/ml/neural_network_context.h" |
| 10 | + |
| 11 | +namespace blink { |
| 12 | + |
| 13 | +MatMul::MatMul(Operand* a, Operand* b) : Output({a, b}) {} |
| 14 | + |
| 15 | +void MatMul::AddLayer(NNModel* model, uint32_t& index) { |
| 16 | + Vector<uint32_t> input_indexes; |
| 17 | + // Add input index to input_indexes. |
| 18 | + for (auto& input : Output::Inputs()) { |
| 19 | + input_indexes.push_back(input->Index()); |
| 20 | + } |
| 21 | + |
| 22 | + // We can't get the bias size. |
| 23 | + uint32_t bias_index = index++; |
| 24 | + model->AddUnspecifiedOperand(); |
| 25 | + input_indexes.push_back(bias_index); |
| 26 | + |
| 27 | + // Add fused code operand and set the value. |
| 28 | + uint32_t fuse_index = index++; |
| 29 | + model->AddScalarOperand(fuse_index, 0); |
| 30 | + input_indexes.push_back(fuse_index); |
| 31 | + |
| 32 | + // There are no MatMul defined in Android NN API, We use kFullyConnected |
| 33 | + // instead of MatMul. |
| 34 | + uint32_t matmul_index = index++; |
| 35 | + model->AddScalarOperand(matmul_index, 0); |
| 36 | + input_indexes.push_back(matmul_index); |
| 37 | + |
| 38 | + // Add MatMul output operand. |
| 39 | + uint32_t output_index = index++; |
| 40 | + Operand::SetIndex(output_index); |
| 41 | + model->AddUnspecifiedOperand(); |
| 42 | + |
| 43 | + model->AddOperation(NeuralNetworkContext::kFullyConnected, input_indexes, |
| 44 | + {output_index}); |
| 45 | +} |
| 46 | + |
| 47 | +} // namespace blink |
0 commit comments