lhutton1 commented on a change in pull request #5915: URL: https://github.com/apache/incubator-tvm/pull/5915#discussion_r457986197
########## File path: src/relay/backend/contrib/arm_compute_lib/codegen.cc ########## @@ -0,0 +1,220 @@ +/* + * 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/relay/backend/contrib/arm_compute_lib/codegen.cc + * \brief Implementation of the Relay -> ACL JSON serializer. + */ +#include <tvm/ir/module.h> +#include <tvm/relay/attrs/nn.h> +#include <tvm/relay/type.h> + +#include <memory> +#include <string> +#include <vector> + +#include "../../utils.h" +#include "../codegen_json/codegen_json.h" + +namespace tvm { +namespace relay { +namespace contrib { + +/*! + * \brief Generates an ACLModule from a relay expression. This "compilation" + * does not require ACL since the actual conversion using ACL APIs is + * deferred until creation of the runtime. This step simply serializes the + * relay program into a JSON string. + */ +class ACLJSONSerializer : public backend::contrib::JSONSerializer { + using JSONGraphNode = tvm::runtime::json::JSONGraphNode; + using JSONGraphNodeEntry = tvm::runtime::json::JSONGraphNodeEntry; + + public: + ACLJSONSerializer(const std::string& symbol, const Expr& expr) : JSONSerializer(symbol, expr) {} + + /*! + * \brief Visit call nodes and generate appropriate JSON node. + * + * \param cn The current call node. + * \return A list of graph entry nodes. + */ + std::vector<JSONGraphNodeEntry> VisitExpr_(const CallNode* cn) override { + if (cn->op.as<OpNode>()) { + return JSONSerializer::VisitExpr_(cn); + } + if (!cn->op.as<FunctionNode>()) { + LOG(FATAL) << "Arm Compute Library JSON runtime does not support calls to " + << cn->op->GetTypeKey(); + } + auto fn = cn->op.as<FunctionNode>(); + auto comp = fn->GetAttr<String>(attr::kComposite); + CHECK(comp.defined()) << "Arm Compute Library JSON runtime only supports composite functions."; + const std::string name = comp.value(); + std::shared_ptr<JSONGraphNode> json_node; + if (name == "arm_compute_lib.conv2d") { + json_node = CreateCompositeConvJSONNode(cn); + } else { + LOG(FATAL) << "Unrecognized Arm Compute Library pattern: " << name; + } + return AddNode(json_node, GetRef<Expr>(cn)); + } + + private: + /*! + * \brief Create a JSON representation of a composite convolution. + * + * \param call The call to be represented. + * \return A JSON representation of a specific operator. + */ + std::shared_ptr<JSONGraphNode> CreateCompositeConvJSONNode(const CallNode* cn) { + const std::string name = "nn.conv2d"; + const CallNode* pad = nullptr; + const CallNode* conv; Review comment: My intention here was to express that conv should always be initialized, whilst pad and bias are optional. Happy to change it though. ---------------------------------------------------------------- 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: us...@infra.apache.org