Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NODE][REFLECTION] Support NDArray as field #1452

Merged
merged 2 commits into from
Jul 18, 2018
Merged

Conversation

tqchen
Copy link
Member

@tqchen tqchen commented Jul 18, 2018

Support NDArray as a serializable and reflectable field of Node system. This is going to be useful to introduce constant node in the language.

Example Testcase

In C++

struct NDArrayWrapperNode : public ::tvm::Node {
  std::string name;
  tvm::runtime::NDArray array;

  void VisitAttrs(tvm::AttrVisitor* v) final {
    v->Visit("name", &name);
    v->Visit("array", &array);
  }

  static constexpr const char* _type_key = "NDArrayWrapper";
  TVM_DECLARE_NODE_TYPE_INFO(NDArrayWrapperNode, Node);
};

TVM_DEFINE_NODE_REF(NDArrayWrapper, NDArrayWrapperNode);
def test_ndarray_reflection():
    x = np.random.uniform(size=(10, 2)).astype("float32")
    xx = tvm.nd.array(x)
    xnode = tvm.make.node("NDArrayWrapper", name="xx", array=xx)
    xnode2 = tvm.make.node("NDArrayWrapper", name="x2", array=xx)
    assert xnode.array.same_as(xx)
    json_str = tvm.save_json([xnode, xnode2])
    json_dict = json.loads(json_str)
    b64_str = json_dict["b64ndarrays"][0]
    decoded = py_str(base64.b64encode(base64.b64decode(b64_str)))
    assert b64_str == decoded
    xlist = tvm.load_json(json_str)
    np.testing.assert_equal(xlist[0].array.asnumpy(), xx.asnumpy())
    assert xlist[1].array == xlist[0].array

@tqchen
Copy link
Member Author

tqchen commented Jul 18, 2018

@jroesch can you review?

Copy link
Member

@jroesch jroesch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modulo a few nitpicks looks good to me, did you write the base64 encoding code yourself?

uint64_t header = kTVMNDArrayMagic, reserved = 0;
strm->Write(header);
strm->Write(reserved);
// always save data as CPU context
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we maybe expand on this note, we expect that after deserialization the user will reallocate the data with the appropriate device type and id?

/*!
* \brief wrapper node container for exchange.
*/
struct NDArrayWrapperNode : public ::tvm::Node {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this struct?

data.push_back(LoadDLTensor(strm));
tvm::runtime::NDArray temp;
temp.Load(strm);
std::shared_ptr<NDArrayWrapperNode> n
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these wrappers just so we get a 2-tuple like piece of data in Python?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, these are wrappers to get 2-tuple pieces of data, we put it in nnvm so they can be deprecated in future. Used to test the features introduced

return not self.__eq__(other)

def same_as(self, other):
"""check object identity equality"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we capitalize this?

TVM_CCALL(TVMArrayCopyFromBytes(dst, &bytes[0], data_byte_size));
NDArray temp;
temp.Load(strm);
temp.CopyTo(dst);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@tqchen
Copy link
Member Author

tqchen commented Jul 18, 2018

@jroesch I have addressed your comments, please check again. base64 encoding was adapted from code that I wrote in previous projects, so we can keep things self-contained. The NDArrayWrapper is mainly used to test the introduced feature. It also simplifies the previous hack in nnvm.

We can phase it out later(that is why i didn't put it in tvm)

@tqchen tqchen merged commit 5b5465b into apache:master Jul 18, 2018
@jroesch
Copy link
Member

jroesch commented Jul 19, 2018

👍 everything looked good, sorry took me a while to get back to it, didn't check again until now.

tqchen added a commit to tqchen/tvm that referenced this pull request Aug 4, 2018
sergei-mironov pushed a commit to sergei-mironov/tvm that referenced this pull request Aug 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants