comaniac commented on a change in pull request #5915: URL: https://github.com/apache/incubator-tvm/pull/5915#discussion_r454722390
########## File path: docs/deploy/arm_compute_lib.rst ########## @@ -0,0 +1,135 @@ +.. Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + +Relay Arm|reg| Compute Library Integration +========================================== + +Introduction +------------ + +Arm Compute Library (ACL) is an open source project that provides accelerated kernels for Arm CPU's +and GPU's. Currently the integration offloads operators to ACL to use hand-crafted assembler +routines in the library. By offloading select operators from a relay graph to ACL we can achieve +a performance boost on such devices. + +Building with ACL support +------------------------- + +The current implementation has two separate build options in cmake. The reason for this split is +because ACL cannot be used on an x86 machine. However, we still want to be able compile an ACL +runtime module on an x86 machine. + +* USE_ARM_COMPUTE_LIB=ON/OFF - Enabling this flag will add support for compiling an ACL runtime module. +* USE_ARM_COMPUTE_LIB_GRAPH_RUNTIME=ON/OFF/path-to-acl - Enabling this flag will allow the graph runtime to + compute the ACL offloaded functions. + +These flags can be used in different scenarios depending on your setup. For example, if you want +to compile ACL on an x86 machine and then run the module on a remote Arm device via RPC, you will +need to use USE_ACL=ON on the x86 machine and USE_GRAPH_RUNTIME_ACL=ON on the remote AArch64 Review comment: - s/USE_ACL/USE_ARM_COMPUTE_LIB/ - s/USE_GRAPH_RUNTIME_ACL/USE_ARM_COMPUTE_LIB_GRAPH_RUNTIME/ ########## File path: docs/deploy/arm_compute_lib.rst ########## @@ -0,0 +1,135 @@ +.. Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + +Relay Arm|reg| Compute Library Integration +========================================== + +Introduction +------------ + +Arm Compute Library (ACL) is an open source project that provides accelerated kernels for Arm CPU's +and GPU's. Currently the integration offloads operators to ACL to use hand-crafted assembler +routines in the library. By offloading select operators from a relay graph to ACL we can achieve +a performance boost on such devices. + +Building with ACL support +------------------------- + +The current implementation has two separate build options in cmake. The reason for this split is +because ACL cannot be used on an x86 machine. However, we still want to be able compile an ACL +runtime module on an x86 machine. + +* USE_ARM_COMPUTE_LIB=ON/OFF - Enabling this flag will add support for compiling an ACL runtime module. +* USE_ARM_COMPUTE_LIB_GRAPH_RUNTIME=ON/OFF/path-to-acl - Enabling this flag will allow the graph runtime to + compute the ACL offloaded functions. + +These flags can be used in different scenarios depending on your setup. For example, if you want +to compile ACL on an x86 machine and then run the module on a remote Arm device via RPC, you will +need to use USE_ACL=ON on the x86 machine and USE_GRAPH_RUNTIME_ACL=ON on the remote AArch64 +device. + +Usage +----- + +*Note:* this section may not stay up-to-date with changes to the API. Review comment: Use the RST note format: ```rst .. note:: This section... ``` ########## File path: docs/deploy/arm_compute_lib.rst ########## @@ -0,0 +1,135 @@ +.. Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + +Relay Arm|reg| Compute Library Integration +========================================== + +Introduction +------------ + +Arm Compute Library (ACL) is an open source project that provides accelerated kernels for Arm CPU's +and GPU's. Currently the integration offloads operators to ACL to use hand-crafted assembler +routines in the library. By offloading select operators from a relay graph to ACL we can achieve +a performance boost on such devices. + +Building with ACL support +------------------------- + +The current implementation has two separate build options in cmake. The reason for this split is +because ACL cannot be used on an x86 machine. However, we still want to be able compile an ACL +runtime module on an x86 machine. + +* USE_ARM_COMPUTE_LIB=ON/OFF - Enabling this flag will add support for compiling an ACL runtime module. +* USE_ARM_COMPUTE_LIB_GRAPH_RUNTIME=ON/OFF/path-to-acl - Enabling this flag will allow the graph runtime to + compute the ACL offloaded functions. + +These flags can be used in different scenarios depending on your setup. For example, if you want +to compile ACL on an x86 machine and then run the module on a remote Arm device via RPC, you will +need to use USE_ACL=ON on the x86 machine and USE_GRAPH_RUNTIME_ACL=ON on the remote AArch64 +device. + +Usage +----- + +*Note:* this section may not stay up-to-date with changes to the API. + +Create a relay graph. This may be a single operator or a whole graph. The intention is that any +relay graph can be input. The ACL integration will only pick supported operators to be offloaded +whilst the rest will be computed via TVM. (For this example we will use a single +max_pool2d operator). + +.. code:: python + + import tvm + from tvm import relay + + data_type = "float32" + data_shape = (1, 14, 14, 512) + strides = (2, 2) + padding = (0, 0, 0, 0) + pool_size = (2, 2) + layout = "NHWC" + output_shape = (1, 7, 7, 512) + + data = relay.var('data', shape=data_shape, dtype=data_type) + out = relay.nn.max_pool2d(data, pool_size=pool_size, strides=strides, layout=layout, padding=padding) + module = tvm.IRModule.from_expr(out) + + +Annotate and partition the graph for ACL. + +..code:: python + + from tvm.relay.op.contrib.arm_compute_lib import partition_for_arm_compute_lib + partition_for_arm_compute_lib(module) + + +Build the Relay graph. + +.. code:: python + + target = "llvm -mtriple=aarch64-linux-gnu -mattr=+neon" + with tvm.transform.PassContext(opt_level=3, disabled_pass=["AlterOpLayout"]): + json, lib, params = relay.build(module, target=target) + + +Export the module. + +.. code:: python + + lib_path = '~/lib_acl.so' + cross_compile = 'aarch64-linux-gnu-c++' + lib.export_library(lib_path, cc=cross_compile) + + +Run Inference. This must be on an Arm device. If compiling on x86 device and running on aarch64 +consider using the RPC mechanism. Review comment: s/consider/, consider/ ########## File path: src/runtime/contrib/arm_compute_lib/acl_runtime.cc ########## @@ -0,0 +1,399 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! + * \file src/runtime/contrib/arm_compute_lib/acl_runtime.cc + * \brief A simple JSON runtime for Arm Compute Library. + */ + +#include <tvm/runtime/ndarray.h> +#include <tvm/runtime/registry.h> + +#include "../../file_util.h" +#include "../json/json_node.h" +#include "../json/json_runtime.h" + +#ifdef TVM_GRAPH_RUNTIME_ARM_COMPUTE_LIB +#include <arm_compute/core/Types.h> +#include <arm_compute/runtime/NEON/functions/NEConvolutionLayer.h> +#include <arm_compute/runtime/NEON/functions/NEPoolingLayer.h> +#include <arm_compute/runtime/NEON/functions/NEReshapeLayer.h> + +#include "acl_allocator.h" +#include "acl_utils.h" +#endif + +namespace tvm { +namespace runtime { +namespace contrib { + +using namespace tvm::runtime::json; + +#ifdef TVM_GRAPH_RUNTIME_ARM_COMPUTE_LIB +using namespace arm_compute_lib; + +/*! + * \brief ACL objects we cache in order to avoid needing to construct + * a new layer each time. + */ +struct CachedLayer { + std::shared_ptr<arm_compute::IFunction> function; + std::vector<arm_compute::Tensor> inputs; + std::vector<arm_compute::Tensor> const_inputs; + std::vector<arm_compute::Tensor> outputs; +}; +#endif + +class ACLRuntime : public JSONRuntimeBase { + public: + /*! + * \brief The ACL runtime module. Deserialize the provided functions + * on creation and store in the layer cache. + * + * \param symbol_name The name of the function. + * \param graph_json serialized JSON representation of a sub-graph. + * \param const_names The names of each constant in the sub-graph. + * \params consts An array of constants pre-transposed to the correct layout expected by ACL. + */ + explicit ACLRuntime(const std::string& symbol_name, const std::string& graph_json, + const Array<String>& const_names, const Array<NDArray>& consts) + : JSONRuntimeBase(symbol_name, graph_json, const_names) { + this->constants_ = consts; + } + + /*! + * \brief Get a packed function. + * + * \param name The name/symbol of the function. + * \param sptr_to_self The pointer to the module node. + * \return The packed function. + */ + PackedFunc GetFunction(const std::string& name, const ObjectPtr<Object>& sptr_to_self) override { + if (name == "get_symbol") { + return PackedFunc( + [sptr_to_self, this](TVMArgs args, TVMRetValue* rv) { *rv = this->symbol_name_; }); + } else if (name == "get_const_vars") { + return PackedFunc( + [sptr_to_self, this](TVMArgs args, TVMRetValue* rv) { *rv = this->const_names_; }); + } else if (this->symbol_name_ == name) { + return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) { + CHECK(this->initialized_) << "The module has not been initialized"; + + // Bind argument tensors to data entries. + this->SetInputOutputBuffers(args); + // Execute the subgraph. + this->Run(); + }); + } else if ("__init_" + this->symbol_name_ == name) { + // The function to initialize constant tensors. + return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) { + this->Init(); + this->initialized_ = true; + *rv = 0; + }); + } else { + return PackedFunc(nullptr); + } + } + + /*! + * \brief Save a compiled network to a binary stream, which can then be + * serialized to disk. + * + * \param stream The stream to save the binary. + */ + void SaveToBinary(dmlc::Stream* stream) override { + // Save the symbol + stream->Write(symbol_name_); + // Save the graph + stream->Write(graph_json_); + // Save the required const names + std::vector<std::string> const_names; + for (const auto& it : const_names_) { + const_names.push_back(it); + } + stream->Write(const_names); + // Save the required constant data + stream->Write(constants_.size()); + for (const auto& it : constants_) { + it.Save(stream); + } + } + + /*! + * \brief Load a compiled network from stream. + * + * \param strm The binary stream to load. + * \return The created ACL module. + */ + static Module LoadFromBinary(void* strm) { + dmlc::Stream* stream = static_cast<dmlc::Stream*>(strm); + std::string symbol; + std::string graph_json; + std::vector<std::string> consts; + // Load the symbol + CHECK(stream->Read(&symbol)) << "Loading symbol name failed"; + CHECK(stream->Read(&graph_json)) << "Loading graph json failed"; + CHECK(stream->Read(&consts)) << "Loading the const name list failed"; + Array<String> const_names; + for (const auto& it : consts) { + const_names.push_back(it); + } + size_t const_data_count; + CHECK(stream->Read(&const_data_count)); + Array<NDArray> const_data; + for (size_t i = 0; i < const_data_count; ++i) { + runtime::NDArray temp; + CHECK(temp.Load(stream)) << "Failed to load constant"; + const_data.push_back(temp); + } + auto n = make_object<ACLRuntime>(symbol, graph_json, const_names, const_data); + return Module(n); + } + + /*! + * \brief The type key of the module. + * + * \return module type key. + */ + const char* type_key() const override { return "arm_compute_lib"; } + + /*! + * \brief Initialize runtime. Create ACL layer from JSON + * representation. + */ + void Init() { + CHECK_EQ(this->constants_.size(), const_idx_.size()) + << "The number of input constants must match the number expected."; + this->SetupConstants(this->constants_); +#ifdef TVM_GRAPH_RUNTIME_ARM_COMPUTE_LIB + BuildEngine(); +#endif + } + + // Do not accept constants from MetadataModule as they should be transposed + // by the ACL codegen so they have the correct expected layout. + void Init(const Array<NDArray>& constants) override { LOG(FATAL) << "Not implemented."; } + + /*! + * \brief Unpack inputs and outputs and run inference on a given layer. + * + * \param args Access inputs and outputs. + * \param function The layer to execute inference on. + * \return Status of inference. + */ + void Run() override { +#ifdef TVM_GRAPH_RUNTIME_ARM_COMPUTE_LIB Review comment: I think what zhiics meeant was that we can reduce the number of macro blocks. - If we move CachedLayout struct definition in the ACLRuntime as its inner struct, then we can merge the first block (L48) to the third block (L240). - We can also change the third block (L240) to the following form so that we can remove the second block (L185) ```c #ifdef TVM_GRAPH_RUNTIME_ARM_COMPUTE_LIB void BuildEngine() { // Build engine. } #else void BuildEngine() { // Do nothing. } #endif ``` As a result, we only need 2 macro blocks in this file. ########## File path: docs/deploy/arm_compute_lib.rst ########## @@ -0,0 +1,135 @@ +.. Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + +.. http://www.apache.org/licenses/LICENSE-2.0 + +.. Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + +Relay Arm|reg| Compute Library Integration +========================================== + +Introduction +------------ + +Arm Compute Library (ACL) is an open source project that provides accelerated kernels for Arm CPU's +and GPU's. Currently the integration offloads operators to ACL to use hand-crafted assembler +routines in the library. By offloading select operators from a relay graph to ACL we can achieve +a performance boost on such devices. + +Building with ACL support +------------------------- + +The current implementation has two separate build options in cmake. The reason for this split is +because ACL cannot be used on an x86 machine. However, we still want to be able compile an ACL +runtime module on an x86 machine. + +* USE_ARM_COMPUTE_LIB=ON/OFF - Enabling this flag will add support for compiling an ACL runtime module. +* USE_ARM_COMPUTE_LIB_GRAPH_RUNTIME=ON/OFF/path-to-acl - Enabling this flag will allow the graph runtime to + compute the ACL offloaded functions. + +These flags can be used in different scenarios depending on your setup. For example, if you want +to compile ACL on an x86 machine and then run the module on a remote Arm device via RPC, you will +need to use USE_ACL=ON on the x86 machine and USE_GRAPH_RUNTIME_ACL=ON on the remote AArch64 +device. + +Usage +----- + +*Note:* this section may not stay up-to-date with changes to the API. + +Create a relay graph. This may be a single operator or a whole graph. The intention is that any +relay graph can be input. The ACL integration will only pick supported operators to be offloaded +whilst the rest will be computed via TVM. (For this example we will use a single Review comment: s/whilst/whitelist/ p.s. maybe we should avoid using "whitelist" in the future orz ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
