samskalicky commented on a change in pull request #17034: Dynamic subgraph property URL: https://github.com/apache/incubator-mxnet/pull/17034#discussion_r362103723
########## File path: example/extensions/lib_subgraph/subgraph_lib.cc ########## @@ -0,0 +1,278 @@ +/* + * 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. + */ + +/*! + * Copyright (c) 2019 by Contributors + * \file subgraph_lib.cc + * \brief subgraph operator implementation library file + */ + +#include <math.h> +#include <iostream> +#include <algorithm> +#include "lib_api.h" + +MXReturnValue parseAttrs(std::map<std::string, std::string> attrs, + int* num_in, int* num_out) { + *num_in = 1; + *num_out = 1; + if (attrs.count(SUBGRAPH_SYM_JSON)) { + // example of subgraph json parsing + JsonParser jp; + JsonVal val = jp.parse_to_json(attrs[SUBGRAPH_SYM_JSON]); + int input = 0; + for (auto &item : val.map[JsonVal("nodes")].list) { + if (item.map[JsonVal("op")].str == "null") + input++; + } + int output = val.map[JsonVal("heads")].list.size(); + *num_in = input; + *num_out = output; + } + return MX_SUCCESS; +} + +MXReturnValue inferType(std::map<std::string, std::string> attrs, + std::vector<int> &intypes, + std::vector<int> &outtypes) { + outtypes[0] = intypes[0]; + return MX_SUCCESS; +} + +MXReturnValue inferShape(std::map<std::string, std::string> attrs, + std::vector<std::vector<unsigned int>> &inshapes, + std::vector<std::vector<unsigned int>> &outshapes) { + outshapes[0] = inshapes[0]; + return MX_SUCCESS; +} + +/* function to execute log operator on floats */ +void myLog(MXTensor &in, MXTensor &out) { + float* inp = in.data<float>(); + float* outp = out.data<float>(); + for (int64_t i = 0; i < in.size(); i++) { + outp[i] = logf(inp[i]); + } +} +/* function to execute exp operator on floats */ +void myExp(MXTensor &in, MXTensor &out) { + float* inp = in.data<float>(); + float* outp =out.data<float>(); + for (int64_t i = 0; i < in.size(); i++) { + outp[i] = expf(inp[i]); + } +} + +/* function to execute ops in subgraph + * In MXNet, subgraphs are sorted in topological order + * so all we need to do is go through the ops in order + * and execute each op. + */ +MXReturnValue myExecutor(std::vector<MXTensor> inputs, + std::vector<MXTensor> outputs, + std::string subgraph_sym) { + std::cout << "Info: subgraph symbol is: " << std::endl; Review comment: no, this is part of the "user's external library" so they can have whatever print statements they want. this is just an example of a library. ---------------------------------------------------------------- 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] With regards, Apache Git Services
