You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/07/03 22:08:33 UTC

[GitHub] piiswrong closed pull request #11107: [WIP][PoC] mxnet-lite

piiswrong closed pull request #11107: [WIP][PoC] mxnet-lite
URL: https://github.com/apache/incubator-mxnet/pull/11107
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/cpp-lite/example/Makefile b/cpp-lite/example/Makefile
new file mode 100644
index 00000000000..2b3a32516c0
--- /dev/null
+++ b/cpp-lite/example/Makefile
@@ -0,0 +1,41 @@
+# 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.
+
+prebuild :
+	$(shell cp -r ../../lib ./)
+
+CPPEX_SRC = $(wildcard *.cpp)
+CPPEX_EXE = $(patsubst %.cpp, %, $(CPPEX_SRC))
+
+CFLAGS += -I../../include -I../../3rdparty/nnvm/include -I../../3rdparty/dmlc-core/include  -I../include
+
+# CPPEX_CFLAGS += -I../include
+CPPEX_EXTRA_LDFLAGS := -L../../lib -lmxnet
+
+.PHONY: all clean
+
+all: prebuild  $(CPPEX_EXE)
+
+debug: CPPEX_CFLAGS += -DDEBUG -g
+debug: prebuild all
+
+
+$(CPPEX_EXE):% : %.cpp
+	$(CXX) -std=c++0x $(CFLAGS)  $(CPPEX_CFLAGS) -o $@ $(filter %.cpp %.a, $^) $(CPPEX_EXTRA_LDFLAGS)
+
+clean:
+	rm -f $(CPPEX_EXE)
diff --git a/cpp-lite/example/inference b/cpp-lite/example/inference
new file mode 100755
index 00000000000..078916a760b
Binary files /dev/null and b/cpp-lite/example/inference differ
diff --git a/cpp-lite/example/inference.cpp b/cpp-lite/example/inference.cpp
new file mode 100644
index 00000000000..a61375cca30
--- /dev/null
+++ b/cpp-lite/example/inference.cpp
@@ -0,0 +1,48 @@
+/*
+ * 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.
+ */
+
+/*!
+ */
+#include <fstream>
+#include <map>
+#include <string>
+#include <vector>
+#include <cstdlib>
+
+#include <mxnet-lite/symbol_block.h>
+
+
+int main() {
+  using namespace mxnet::lite;
+
+  Symbol out = Symbol::Load("resnet-symbol.json");
+  auto params = NDArray::LoadDict("resnet-0000.params");
+  SymbolBlock block(Context::CPU(0), {}, out, {"data"}, params);
+
+  NDArray x({1, 3, 32, 32}, Context::CPU(0), false);
+  auto y = block.Forward({x});
+
+  std::vector<float> y0(y[0].GetSize(), 0);
+  y[0].CopyTo(y0.data(), y0.size());
+
+  std::cout << "[";
+  for (const auto& i : y0) std::cout << i << ", ";
+  std::cout << "]" << std::endl;
+  return 0;
+}
diff --git a/cpp-lite/include/mxnet-lite/base.h b/cpp-lite/include/mxnet-lite/base.h
new file mode 100644
index 00000000000..ae132dc1be2
--- /dev/null
+++ b/cpp-lite/include/mxnet-lite/base.h
@@ -0,0 +1,34 @@
+/*
+ * 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.
+ */
+
+#ifndef MXNET_LITE_BASE_H_
+#define MXNET_LITE_BASE_H_
+
+#include <dmlc/logging.h>
+
+#define MXNET_CALL(x) CHECK_EQ((x), 0) << MXGetLastError()
+
+namespace mxnet {
+namespace lite {
+
+
+}  // namespace lite
+}  // namespace mxnet
+
+#endif  // MXNET_LITE_BASE_H_
diff --git a/cpp-lite/include/mxnet-lite/context.h b/cpp-lite/include/mxnet-lite/context.h
new file mode 100644
index 00000000000..61a287b1640
--- /dev/null
+++ b/cpp-lite/include/mxnet-lite/context.h
@@ -0,0 +1,82 @@
+/*
+ * 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.
+ */
+
+#ifndef MXNET_LITE_CONTEXT_H_
+#define MXNET_LITE_CONTEXT_H_
+
+namespace mxnet {
+namespace lite {
+
+enum DeviceType {
+  kCPU = 1,
+  kGPU = 2,
+  kCPUPinned = 3
+};
+
+/*!
+* \brief Context interface
+*/
+class Context {
+ public:
+  /*!
+   * \brief Return a GPU context
+   * \param device_id id of the device
+   * \return the corresponding GPU context
+   */
+  static Context GPU(int device_id = 0) {
+    return Context(DeviceType::kGPU, device_id);
+  }
+  /*!
+   * \brief Return a CPU context
+   * \param device_id id of the device. this is not needed by CPU
+   * \return the corresponding CPU context
+   */
+  static Context CPU(int device_id = 0) {
+    return Context(DeviceType::kCPU, device_id);
+  }
+  /*!
+  * \brief Context constructor
+  * \param type type of the device
+  * \param id id of the device
+  */
+  Context(const DeviceType &type, int id) : type_(type), id_(id) {}
+  /*!
+  * \return the type of the device
+  */
+  DeviceType GetDeviceType() const { return type_; }
+  /*!
+  * \return the id of the device
+  */
+  int GetDeviceId() const { return id_; }
+  bool operator==(const Context& other) {
+    return type_ == other.type_ && id_ == other.id_;
+  }
+  bool operator!=(const Context& other) {
+    return !(*this == other);
+  }
+
+ private:
+  DeviceType type_;
+  int id_;
+};
+
+}  // namespace lite
+}  // namespace mxnet
+
+#endif  // MXNET_LITE_CONTEXT_H_
diff --git a/cpp-lite/include/mxnet-lite/ndarray.h b/cpp-lite/include/mxnet-lite/ndarray.h
new file mode 100644
index 00000000000..f6598f8b44f
--- /dev/null
+++ b/cpp-lite/include/mxnet-lite/ndarray.h
@@ -0,0 +1,284 @@
+/*
+ * 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.
+ */
+
+#ifndef MXNET_LITE_NDARRAY_H_
+#define MXNET_LITE_NDARRAY_H_
+
+#include <map>
+#include <string>
+#include <vector>
+
+#include <dmlc/logging.h>
+#include <mxnet/c_api.h>
+
+#include <mxnet-lite/context.h>
+
+namespace mxnet {
+namespace lite {
+/*!
+* \brief NDArray interface
+*/
+class NDArray {
+ public:
+  /*!
+  * \brief Block until all the pending read/write operations with respect
+  *    to current NDArray are finished, and read/write can be performed.
+  */
+  static void WaitAll() {
+    CHECK_EQ(MXNDArrayWaitAll(), 0);
+  }
+  /*!
+  * \brief Load NDArrays from binary file.
+  * \param file_name name of the binary file.
+  * \param array_list a list of NDArrays returned, do not fill the list if
+  * nullptr is given.
+  * \param array_map a map from names to NDArrays returned, do not fill the map
+  * if nullptr is given or no names is stored in binary file.
+  */
+  static std::vector<NDArray> LoadList(const std::string &file_name) {
+    mx_uint out_size, out_name_size;
+    NDArrayHandle *out_arr;
+    const char **out_names;
+    CHECK_EQ(MXNDArrayLoad(file_name.c_str(), &out_size, &out_arr,
+                           &out_name_size, &out_names), 0);
+    std::vector<NDArray> array_list;
+    array_list.reserve(out_size);
+    for (mx_uint i = 0; i < out_size; ++i) {
+      array_list.push_back(NDArray(out_arr[i]));
+    }
+    return array_list;
+  }
+  /*!
+  * \brief Load map of NDArrays from binary file.
+  * \param file_name name of the binary file.
+  * \return a list of NDArrays.
+  */
+  static std::map<std::string, NDArray> LoadDict(const std::string &file_name) {
+    mx_uint out_size, out_name_size;
+    NDArrayHandle *out_arr;
+    const char **out_names;
+    CHECK_EQ(MXNDArrayLoad(file_name.c_str(), &out_size, &out_arr,
+                          &out_name_size, &out_names), 0);
+    CHECK_EQ(out_name_size, out_size)
+        << "Cannot load to dict because file was saved as list.";
+    std::map<std::string, NDArray> array_map;
+    for (mx_uint i = 0; i < out_size; ++i) {
+      array_map[out_names[i]] = NDArray(out_arr[i]);
+    }
+    return array_map;
+  }
+  /*!
+  * \brief save a map of string->NDArray to binary file.
+  * \param file_name name of the binary file.
+  * \param array_map a map from names to NDArrays.
+  */
+  static void Save(const std::string &file_name,
+                   const std::map<std::string, NDArray> &array_map) {
+    std::vector<NDArrayHandle> args;
+    std::vector<const char *> keys;
+    for (const auto &t : array_map) {
+      args.push_back(t.second.GetHandle());
+      keys.push_back(t.first.c_str());
+    }
+    CHECK_EQ(MXNDArraySave(file_name.c_str(), args.size(), args.data(),
+                           keys.data()), 0);
+  }
+  /*!
+  * \brief save a list of NDArrays to binary file.
+  * \param file_name name of the binary file.
+  * \param array_list a list of NDArrays.
+  */
+  static void Save(const std::string &file_name,
+                   const std::vector<NDArray> &array_list) {
+    std::vector<NDArrayHandle> args;
+    for (const auto &t : array_list) {
+      args.push_back(t.GetHandle());
+    }
+    CHECK_EQ(MXNDArraySave(file_name.c_str(), args.size(), args.data(),
+                           nullptr), 0);
+  }
+  /*!
+  * \brief construct with a none handle
+  */
+  NDArray() {
+    NDArrayHandle handle;
+    CHECK_EQ(MXNDArrayCreateNone(&handle), 0);
+    blob_ptr_ = std::make_shared<NDBlob>(handle);
+  }
+  /*!
+  * \brief construct with a NDArrayHandle
+  */
+  explicit NDArray(const NDArrayHandle &handle) {
+    blob_ptr_ = std::make_shared<NDBlob>(handle);
+  }
+  /*!
+  * \brief construct a new dynamic NDArray
+  * \param shape the shape of array
+  * \param constext context of NDArray
+  * \param delay_alloc whether delay the allocation
+  */
+  NDArray(const std::vector<mx_uint> &shape,
+          const Context &context,
+          bool delay_alloc = true) {
+    NDArrayHandle handle;
+    CHECK_EQ(MXNDArrayCreate(shape.data(), shape.size(), context.GetDeviceType(),
+                             context.GetDeviceId(), delay_alloc, &handle), 0);
+    blob_ptr_ = std::make_shared<NDBlob>(handle);
+  }
+  NDArray(const float *data, size_t size) {
+    NDArrayHandle handle;
+    CHECK_EQ(MXNDArrayCreateNone(&handle), 0);
+    MXNDArraySyncCopyFromCPU(handle, data, size);
+    blob_ptr_ = std::make_shared<NDBlob>(handle);
+  }
+  /*!
+  * \brief construct a new dynamic NDArray
+  * \param data the data to create NDArray from
+  * \param shape the shape of array
+  * \param constext context of NDArray
+  */
+  NDArray(const float *data,
+          const std::vector<mx_uint> &shape,
+          const Context &context) {
+    NDArrayHandle handle;
+    CHECK_EQ(MXNDArrayCreate(shape.data(), shape.size(), context.GetDeviceType(),
+                             context.GetDeviceId(), false, &handle), 0);
+    size_t size = shape.size() ? 1 : 0;
+    for (auto& i : shape) size *= i;
+    MXNDArraySyncCopyFromCPU(handle, data, size);
+    blob_ptr_ = std::make_shared<NDBlob>(handle);
+  }
+
+  void CopyFrom(const float *data, size_t size) {
+    CHECK_EQ(MXNDArraySyncCopyFromCPU(blob_ptr_->handle, data, size), 0);
+  }
+
+  void CopyTo(float *data, size_t size = 0) {
+     CHECK_EQ(MXNDArraySyncCopyToCPU(blob_ptr_->handle, data,
+                                     size > 0 ? size : GetSize()), 0);
+  }
+  /*!
+  * \brief return a new copy to this NDArray
+  * \param Context the new context of this NDArray
+  * \return the new copy
+  */
+  NDArray Copy(const Context &) const {
+    return NDArray();
+  }
+  /*!
+  * \brief Block until all the pending write operations with respect
+  *    to current NDArray are finished, and read can be performed.
+  */
+  void WaitToRead() const {
+    CHECK_EQ(MXNDArrayWaitToRead(blob_ptr_->handle), 0);
+  }
+  /*!
+  * \brief Block until all the pending read/write operations with respect
+  *    to current NDArray are finished, and write can be performed.
+  */
+  void WaitToWrite() const {
+    CHECK_EQ(MXNDArrayWaitToWrite(blob_ptr_->handle), 0);
+  }
+  /*!
+  * \return the size of current NDArray, a.k.a. the production of all shape dims
+  */
+  size_t GetSize() const {
+    size_t ret = 1;
+    for (auto &i : GetShape()) ret *= i;
+    return ret;
+  }
+  /*!
+  * \return the shape of current NDArray, in the form of mx_uint vector
+  */
+  std::vector<mx_uint> GetShape() const {
+    const mx_uint *out_pdata;
+    mx_uint out_dim;
+    CHECK_EQ(MXNDArrayGetShape(blob_ptr_->handle, &out_dim, &out_pdata), 0);
+    std::vector<mx_uint> ret;
+    for (mx_uint i = 0; i < out_dim; ++i) {
+      ret.push_back(out_pdata[i]);
+    }
+    return ret;
+  }
+  /*!
+  * \return the data type of current NDArray
+  */
+  int GetDType() const {
+    int ret;
+    CHECK_EQ(MXNDArrayGetDType(blob_ptr_->handle, &ret), 0);
+    return ret;
+  }
+  /*!
+  * \brief Get the pointer to data (IMPORTANT: The ndarray should not be in GPU)
+  * \return the data pointer to the current NDArray
+  */
+  const float *GetData() const {
+    if (GetDType() != 0) {
+      return NULL;
+    }
+    void *ret;
+    CHECK_EQ(MXNDArrayGetData(blob_ptr_->handle, &ret), 0);
+    return static_cast<float*>(ret);
+  }
+  /*!
+  * \return the context of NDArray
+  */
+  Context GetContext() const {
+    int out_dev_type;
+    int out_dev_id;
+    CHECK_EQ(MXNDArrayGetContext(blob_ptr_->handle, &out_dev_type,
+                                 &out_dev_id), 0);
+    return Context(static_cast<DeviceType>(out_dev_type), out_dev_id);
+  }
+  /*!
+  * \return the NDArrayHandle of the current NDArray
+  */
+  NDArrayHandle GetHandle() const {
+    return blob_ptr_->handle;
+  }
+
+ private:
+  /*!
+  * \brief struct to store NDArrayHandle
+  */
+  struct NDBlob {
+    /*!
+    * \brief default constructor
+    */
+    NDBlob() : handle(nullptr) {}
+    /*!
+    * \brief construct with a NDArrayHandle
+    * \param handle NDArrayHandle to store
+    */
+    explicit NDBlob(NDArrayHandle handle_) : handle(handle_) {}
+    /*!
+    * \brief destructor, free the NDArrayHandle
+    */
+    ~NDBlob() { MXNDArrayFree(handle); }
+    /*!
+    * \brief the NDArrayHandle
+    */
+    NDArrayHandle handle;
+  };
+  std::shared_ptr<NDBlob> blob_ptr_;
+};
+}  // namespace cpp
+}  // namespace mxnet
+
+#endif  // MXNET_LITE_NDARRAY_H_
diff --git a/cpp-lite/include/mxnet-lite/symbol.h b/cpp-lite/include/mxnet-lite/symbol.h
new file mode 100644
index 00000000000..0e396de3da6
--- /dev/null
+++ b/cpp-lite/include/mxnet-lite/symbol.h
@@ -0,0 +1,221 @@
+/*
+ * 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) 2016 by Contributors
+* \file symbol.h
+* \brief definition of symbol
+* \author Chuntao Hong, Zhang Chen
+*/
+
+#ifndef MXNET_LITE_SYMBOL_H_
+#define MXNET_LITE_SYMBOL_H_
+
+#include <map>
+#include <string>
+#include <vector>
+
+#include <mxnet/c_api.h>
+#include <nnvm/c_api.h>
+
+#include <mxnet-lite/ndarray.h>
+
+namespace mxnet {
+namespace lite {
+/*!
+* \brief Symbol interface
+*/
+class Symbol {
+ public:
+  /*!
+  * \brief construct a variable Symbol
+  * \param name the name of the variable
+  */
+  static Symbol Variable(const std::string &name) {
+    SymbolHandle handle;
+    CHECK_EQ(MXSymbolCreateVariable(name.c_str(), &(handle)), 0);
+    return Symbol(handle);
+  }
+  /*!
+  * \brief Create a symbol that groups symbols together
+  * \param symbols List of symbols to be groupe
+  */
+  static Symbol Group(const std::vector<Symbol> &symbols) {
+    SymbolHandle out;
+    std::vector<SymbolHandle> handle_list;
+    for (const auto &t : symbols) {
+      handle_list.push_back(t.GetHandle());
+    }
+    CHECK_EQ(MXSymbolCreateGroup(handle_list.size(), handle_list.data(), &out), 0);
+    return Symbol(out);
+  }
+  /*!
+  * \brief load Symbol from a JSON file
+  * \param file_name the name of the file
+  */
+  static Symbol Load(const std::string &file_name) {
+    SymbolHandle handle;
+    CHECK_EQ(MXSymbolCreateFromFile(file_name.c_str(), &(handle)), 0);
+    return Symbol(handle);
+  }
+  /*!
+  * \brief load Symbol from a JSON string
+  * \param json_str the JSON string
+  */
+  static Symbol FromJSON(const std::string &json_str) {
+    SymbolHandle handle;
+    CHECK_EQ(MXSymbolCreateFromJSON(json_str.c_str(), &(handle)), 0);
+    return Symbol(handle);
+  }
+  /*!
+  * \brief construct a Symbol with SymbolHandle
+  * \param handle the given SymbolHandle
+  */
+  explicit Symbol(SymbolHandle handle) {
+    blob_ptr_ = std::make_shared<SymBlob>(handle);
+  }
+
+  /*!
+  * \return the SymbolHandle
+  */
+  SymbolHandle GetHandle() const {
+    return (blob_ptr_) ? blob_ptr_->handle: NULL;
+  }
+
+  Symbol Copy() const {
+    SymbolHandle handle;
+    CHECK_EQ(MXSymbolCopy(GetHandle(), &handle), 0);
+    return Symbol(handle);
+  }
+  Symbol operator[](int index) const {
+    SymbolHandle out;
+    MXSymbolGetOutput(GetHandle(), index, &out);
+    return Symbol(out);
+  }
+  /*!
+  * \brief save Symbol to a file
+  * \param file_name the name of the file
+  */
+  void Save(const std::string &file_name) const {
+    CHECK_EQ(MXSymbolSaveToFile(GetHandle(), file_name.c_str()), 0);
+  }
+  /*!
+  * \brief save Symbol into a JSON string
+  */
+  std::string ToJSON() const {
+    const char *out_json;
+    CHECK_EQ(MXSymbolSaveToJSON(GetHandle(), &out_json), 0);
+    return std::string(out_json);
+  }
+  /*!
+  * \brief save Symbol into a JSON string
+  * \retutrn the symbol whose outputs are all the internals.
+  */
+  Symbol GetInternals() const {
+    SymbolHandle handle;
+    CHECK_EQ(MXSymbolGetInternals(GetHandle(), &handle), 0);
+    return Symbol(handle);
+  }
+  /*! \return get the descriptions of outputs for this symbol */
+  std::vector<std::string> ListOutputs() const {
+    std::vector<std::string> ret;
+    mx_uint size;
+    const char **sarr;
+    CHECK_EQ(MXSymbolListOutputs(GetHandle(), &size, &sarr), 0);
+    for (mx_uint i = 0; i < size; ++i) {
+      ret.push_back(std::string(sarr[i]));
+    }
+    return ret;
+  }
+  /*!
+  * \brief List the arguments names.
+  *
+  * The position of the returned list also corresponds to calling position in
+  *operator()
+  * \return the arguments list of this symbol, they can be either named or
+  *unnamed (empty string).
+  */
+  std::vector<std::string> ListArguments() const {
+    std::vector<std::string> ret;
+    mx_uint size;
+    const char **sarr;
+    CHECK_EQ(MXSymbolListArguments(GetHandle(), &size, &sarr), 0);
+    for (mx_uint i = 0; i < size; ++i) {
+      ret.push_back(std::string(sarr[i]));
+    }
+    return ret;
+  }
+  /*! \return get the descriptions of auxiliary data for this symbol */
+  std::vector<std::string> ListAuxiliaryStates() const {
+    std::vector<std::string> ret;
+    mx_uint size;
+    const char **sarr;
+    CHECK_EQ(MXSymbolListAuxiliaryStates(GetHandle(), &size, &sarr), 0);
+    for (mx_uint i = 0; i < size; ++i) {
+      ret.push_back(std::string(sarr[i]));
+    }
+    return ret;
+  }
+  /*!
+  * \brief List the arguments names.
+  *
+  * The position of the returned list also corresponds to calling position in
+  *operator()
+  * \return the arguments list of this symbol, they can be either named or
+  *unnamed (empty string).
+  */
+  std::vector<std::string> ListInputs() const {
+    std::vector<std::string> ret;
+    mx_uint size;
+    const char **sarr;
+    CHECK_EQ(NNSymbolListInputNames(GetHandle(), 0, &size, &sarr), 0);
+    for (mx_uint i = 0; i < size; ++i) {
+      ret.push_back(std::string(sarr[i]));
+    }
+    return ret;
+  }
+
+ private:
+  /*!
+  * \brief struct to store SymbolHandle
+  */
+  struct SymBlob {
+    /*!
+    * \brief default constructor
+    */
+    SymBlob() : handle(nullptr) {}
+    /*!
+    * \brief construct with SymbolHandle to store
+    */
+    explicit SymBlob(SymbolHandle handle_) : handle(handle_) {}
+    /*!
+    * \brief destructor, free the SymbolHandle
+    */
+    ~SymBlob() { MXSymbolFree(handle); }
+    /*!
+    * \brief the SymbolHandle to store
+    */
+    SymbolHandle handle;
+  };
+
+  std::shared_ptr<SymBlob> blob_ptr_;
+};
+}  // namespace lite
+}  // namespace mxnet
+#endif  // MXNET_LITE_SYMBOL_H_
diff --git a/cpp-lite/include/mxnet-lite/symbol_block.h b/cpp-lite/include/mxnet-lite/symbol_block.h
new file mode 100644
index 00000000000..d020ad389f3
--- /dev/null
+++ b/cpp-lite/include/mxnet-lite/symbol_block.h
@@ -0,0 +1,151 @@
+/*
+ * 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) 2016 by Contributors
+* \file symbol.h
+* \brief definition of symbol
+* \author Chuntao Hong, Zhang Chen
+*/
+
+#ifndef MXNET_LITE_SYMBOL_BLOCK_H_
+#define MXNET_LITE_SYMBOL_BLOCK_H_
+
+#include <map>
+#include <unordered_map>
+#include <string>
+#include <vector>
+
+#include <mxnet-lite/base.h>
+#include <mxnet-lite/ndarray.h>
+#include <mxnet-lite/symbol.h>
+
+namespace mxnet {
+namespace lite {
+
+class SymbolBlock {
+ public:
+  SymbolBlock(const Context& context,
+              const std::vector<std::pair<std::string, std::string> >& flags,
+              const Symbol& output,
+              const std::vector<std::string>& data_names,
+              const std::map<std::string, NDArray>& params) {
+    auto input_names = output.ListInputs();
+    num_inputs_ = input_names.size();
+    std::unordered_map<std::string, size_t> name_to_idx;
+    for (size_t i = 0; i < input_names.size(); ++i) {
+      name_to_idx[input_names[i]] = i;
+    }
+    for (const auto& i : data_names) {
+      const auto iter = name_to_idx.find(i);
+      CHECK(iter != name_to_idx.end()) << "Invalid input name " << i;
+      data_indices_.push_back(iter->second);
+    }
+    for (const auto& i : params) {
+      std::string name = i.first;
+      if (name.compare(0, 4, "arg:") || name.compare(0, 4, "aux:")) {
+        name = name.substr(4);
+      }
+      const auto iter = name_to_idx.find(name);
+      CHECK(iter != name_to_idx.end()) << "Invalid parameter name " << i.first;
+      param_indices_.push_back(iter->second);
+      if (i.second.GetContext() != context) {
+        params_.push_back(i.second.Copy(context));
+      } else {
+        params_.push_back(i.second);
+      }
+    }
+    std::vector<const char*> keys, vals;
+    for (const auto& i : flags) {
+      keys.push_back(i.first.c_str());
+      vals.push_back(i.second.c_str());
+    }
+    std::stringstream stream_data_indices, stream_param_indices;
+    stream_data_indices << "[";
+    for (const auto& i : data_indices_) stream_data_indices << i << ", ";
+    stream_data_indices << "]";
+    stream_param_indices << "[";
+    for (const auto& i : param_indices_) stream_param_indices << i << ", ";
+    stream_param_indices << "]";
+    std::string str_data_indices = stream_data_indices.str();
+    std::string str_param_indices = stream_param_indices.str();
+    keys.push_back("data_indices");
+    vals.push_back(str_data_indices.c_str());
+    keys.push_back("param_indices");
+    vals.push_back(str_param_indices.c_str());
+
+    CachedOpHandle handle;
+    MXNET_CALL(MXCreateCachedOpEx(
+        output.GetHandle(), keys.size(), keys.data(), vals.data(), &handle));
+    blob_ptr_ = std::make_shared<OpBlob>(handle);
+  }
+
+  std::vector<NDArray> Forward(const std::vector<NDArray>& data) {
+    std::vector<NDArrayHandle> inputs(num_inputs_);
+    CHECK_EQ(data.size(), data_indices_.size());
+    for (size_t i = 0; i < data.size(); ++i) {
+      inputs[data_indices_[i]] = data[i].GetHandle();
+    }
+    for (size_t i = 0; i < params_.size(); ++i) {
+      inputs[param_indices_[i]] = params_[i].GetHandle();
+    }
+    int num_outputs = 0;
+    NDArrayHandle *outputs = nullptr;
+    MXNET_CALL(MXInvokeCachedOp(
+        blob_ptr_->handle, num_inputs_, inputs.data(), &num_outputs,
+        &outputs));
+    std::vector<NDArray> ret;
+    for (int i = 0; i < num_outputs; ++i) {
+      ret.push_back(NDArray(outputs[i]));
+    }
+    return ret;
+  }
+
+ private:
+  /*!
+  * \brief struct to store CachedOpHandle
+  */
+  struct OpBlob {
+    /*!
+    * \brief default constructor
+    */
+    OpBlob() : handle(nullptr) {}
+    /*!
+    * \brief construct with CachedOpHandle to store
+    */
+    explicit OpBlob(CachedOpHandle handle_) : handle(handle_) {}
+    /*!
+    * \brief destructor, free the CachedOpHandle
+    */
+    ~OpBlob() { MXFreeCachedOp(handle); }
+    /*!
+    * \brief the CachedOpHandle to store
+    */
+    CachedOpHandle handle;
+  };
+  std::shared_ptr<OpBlob> blob_ptr_;
+  size_t num_inputs_;
+  std::vector<size_t> data_indices_;
+  std::vector<size_t> param_indices_;
+  std::vector<NDArray> params_;
+};
+
+}  // namespace lite
+}  // namespace mxnet
+#endif  // MXNET_LITE_SYMBOL_BLOCK_H_


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services