leleamol commented on a change in pull request #10792: [MXNET-400] support string type for kvstore key in cpp-package URL: https://github.com/apache/incubator-mxnet/pull/10792#discussion_r236794233
########## File path: cpp-package/example/test_kvstore.cpp ########## @@ -0,0 +1,132 @@ +/* + * 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 "mxnet-cpp/MxNetCpp.h" + +using namespace mxnet::cpp; + +static int test_single_key() { + int ret = 0; + + std::string key = "singlekeytest"; + + NDArray result(Shape(4), Context::cpu()); + + // initialize data + NDArray data({0.f, 233.f, -0.12f, 9.f}, Shape(4), Context::cpu()); + KVStore::Init(key, data); + NDArray::WaitAll(); + + // retrieve result + KVStore::Pull(key, &result); + NDArray::WaitAll(); + + // compare + for (size_t j=0; j < result.Size(); j++) { + ret += (result.GetData()[j] == data.GetData()[j]) ? 0 : 1; + } + if (ret != 0) + return ret; + + // push gradient + NDArray grad({0.1f, -2.f, -4.4f, 0.f}, Shape(4), Context::cpu()); + KVStore::Push(key, grad); + NDArray::WaitAll(); + + // retrieve result + KVStore::Pull(key, &result); + NDArray::WaitAll(); + + // compare + for (size_t j=0; j < result.Size(); j++) { + ret += (result.GetData()[j] == grad.GetData()[j]) ? 0 : 1; + } + + return ret; +} + +static int test_multiple_key() { + int ret = 0; + + std::vector<std::string> keys(2); + keys[0] = "multikeytest-0"; + keys[1] = "multikeytest-1"; + + std::vector<NDArray> results(2); + results[0] = NDArray(Shape(4), Context::cpu()); + results[1] = NDArray(Shape(4), Context::cpu()); + + // initialize data + std::vector<NDArray> datas(2); + datas[0] = NDArray({0.f, 2.f, -3.12f, 4.f}, Shape(4), Context::cpu()); + datas[1] = NDArray({0.8f, -2.f, 6.6f, 77.f}, Shape(4), Context::cpu()); + KVStore::Init(keys, datas); + NDArray::WaitAll(); + + // retrieve result + KVStore::Pull(keys, &results); + NDArray::WaitAll(); + + // compare + for (size_t i=0; i < results.size(); i++) { + for (size_t j=0; j < results[i].Size(); j++) { + ret += (results[i].GetData()[j] == datas[i].GetData()[j]) ? 0 : 1; + } + } + if (ret != 0) + return ret; + + // push gradient, reduce for the second + std::vector<std::string> push_keys(3); + push_keys[0] = "multikeytest-0"; + push_keys[1] = "multikeytest-1"; + push_keys[2] = "multikeytest-1"; + + std::vector<NDArray> grads(3); + grads[0] = NDArray({0.2f, -0.3f, -1.1f, 0.0f}, Shape(4), Context::cpu()); + grads[1] = NDArray({2.f, 4.f, -4.f, -5.f}, Shape(4), Context::cpu()); + grads[2] = NDArray({-3.f, -0.2f, 12.f, -9.f}, Shape(4), Context::cpu()); + KVStore::Push(push_keys, grads); + NDArray::WaitAll(); + + // retrieve result + KVStore::Pull(keys, &results); + NDArray::WaitAll(); + + // compare the first + for (size_t j=0; j < results[0].Size(); j++) { + ret += (results[0].GetData()[j] == grads[0].GetData()[j]) ? 0 : 1; + } + + // compare the second + for (size_t j=0; j < results[1].Size(); j++) { + ret += (results[1].GetData()[j] == (grads[1].GetData()[j] + grads[2].GetData()[j])) ? 0 : 1; + } + + return ret; Review comment: Thanks for adding the unit test. However, it would be helpful if you can add some logging. Especially, for the cases when the test fails. ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services
