From 4c3a7ec6e9531b86471ed64e1d3e9c00cb218268 Mon Sep 17 00:00:00 2001 From: jiangzhichen Date: Mon, 15 May 2023 18:38:33 +0800 Subject: [PATCH 1/2] optimized `get` API --- examples/cpp/cxx_test.cc | 17 +++++++ hyperparameter/hyperparameter.h | 81 +++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/examples/cpp/cxx_test.cc b/examples/cpp/cxx_test.cc index e26c8f0..2c055bb 100644 --- a/examples/cpp/cxx_test.cc +++ b/examples/cpp/cxx_test.cc @@ -36,6 +36,23 @@ int main() << "returned: " << hp->get("x.y.z", "false") << std::endl; + // ======= opt api test ======= + + PUTPARAMS(xacc.eager, false); + PUTPARAMS(xacc.lazy.device, "xla"); + + std::string device_type = GETPARAMS(xacc.lazy.device, "xpu"); + std::cout << "\n:: (opt api) test put parameter" << std::endl + << "expected: xla" << std::endl + << "returned: " << device_type << std::endl + << "expected: 0" << std::endl + << "returned: " << GETPARAMS(xacc.eager, true) << std::endl; + + std::cout << "\n:: (opt api) test undefined" << std::endl + << "expected: 100" << std::endl + << "returned: " << GETPARAMS(xacc.dynamo.time, 100) << std::endl; + std::cout << "in main" << std::endl; + return 0; } \ No newline at end of file diff --git a/hyperparameter/hyperparameter.h b/hyperparameter/hyperparameter.h index 26a6cbe..ac9ae9c 100644 --- a/hyperparameter/hyperparameter.h +++ b/hyperparameter/hyperparameter.h @@ -33,6 +33,20 @@ namespace hyperparameter return finalize((len >= 32 ? h32bytes(p, len, seed) : seed + PRIME5) + len, p + (len & ~0x1F), len & 0x1F); } + template = 32, uint64_t>::type = true> + static constexpr uint64_t hash(const char *p, uint64_t seed = 42) { + return finalize(h32bytes(p, seed) + len, p + (len & ~0x1F), + len & 0x1F); + } + + template ::type = + true> static constexpr uint64_t + hash(const char *p, uint64_t seed = 42) { + return finalize(seed + PRIME5 + len, p + (len & ~0x1F), len & 0x1F); + } + private: static constexpr uint64_t PRIME1 = 11400714785074694791ULL; static constexpr uint64_t PRIME2 = 14029467366897019727ULL; @@ -80,6 +94,33 @@ namespace hyperparameter { return (len >= 8) ? (finalize(rotl(h ^ fetch64(p), 27) * PRIME1 + PRIME4, p + 8, len - 8)) : ((len >= 4) ? (finalize(rotl(h ^ fetch32(p), 23) * PRIME2 + PRIME3, p + 4, len - 4)) : ((len > 0) ? (finalize(rotl(h ^ fetch8(p), 11) * PRIME1, p + 1, len - 1)) : (mix1(mix1(mix1(h, PRIME2, 33), PRIME3, 29), 1, 32)))); } + + template = 8, uint64_t>::type = true> + static constexpr uint64_t finalize(const uint64_t h, const char *p) { + return finalize(rotl(h ^ fetch64(p), 27) * PRIME1 + PRIME4, + p + 8); + } + + template = 4), + uint64_t>::type = true> + static constexpr uint64_t finalize(const uint64_t h, const char *p) { + return finalize(rotl(h ^ fetch32(p), 23) * PRIME2 + PRIME3, + p + 4); + } + + template 0), + uint64_t>::type = true> + static constexpr uint64_t finalize(const uint64_t h, const char *p) { + return finalize(rotl(h ^ fetch8(p), 11) * PRIME1, p + 1); + } + + template ::type = true> + static constexpr uint64_t finalize(const uint64_t h, const char *p) { + return mix1(mix1(mix1(h, PRIME2, 33), PRIME3, 29), 1, 32); + } + static constexpr uint64_t h32bytes(const char *p, uint64_t len, const uint64_t v1, const uint64_t v2, const uint64_t v3, const uint64_t v4) { return (len >= 32) ? h32bytes(p + 32, len - 32, fetch64(p, v1), fetch64(p + 8, v2), fetch64(p + 16, v3), fetch64(p + 24, v4)) : mix3(mix3(mix3(mix3(rotl(v1, 1) + rotl(v2, 7) + rotl(v3, 12) + rotl(v4, 18), v1), v2), v3), v4); @@ -88,10 +129,35 @@ namespace hyperparameter { return h32bytes(p, len, seed + PRIME1 + PRIME2, seed + PRIME2, seed, seed - PRIME1); } + + template + static constexpr uint64_t h32bytes(const char *p, const uint64_t v1, + const uint64_t v2, const uint64_t v3, + const uint64_t v4) { + return (len >= 32) + ? h32bytes( + p + 32, fetch64(p, v1), fetch64(p + 8, v2), + fetch64(p + 16, v3), fetch64(p + 24, v4)) + : mix3(mix3(mix3(mix3(rotl(v1, 1) + rotl(v2, 7) + + rotl(v3, 12) + rotl(v4, 18), + v1), + v2), + v3), + v4); + } + + template + static constexpr uint64_t h32bytes(const char *p, const uint64_t seed) { + return h32bytes(p, seed + PRIME1 + PRIME2, seed + PRIME2, seed, + seed - PRIME1); + } }; constexpr uint64_t xxhash(const char *p, int len) { return xxh64::hash(p, len, 42); } + template + constexpr uint64_t xxhash(const char *p) { return xxh64::hash(p, (uint64_t)42); } + struct Hyperparameter { Storage *_storage; @@ -191,6 +257,21 @@ namespace hyperparameter { return storage_put_str(_storage, key, val); } + + std::shared_ptr get_hp() { + static std::shared_ptr hp; + if (!hp) { + hp = hyperparameter::create_shared(); + } + return hp; + } } +#define GETHP hyperparameter::get_hp() + +// Implicit create hyperparameter object +#define GETPARAMS(p, default_val) \ + (GETHP->get(hyperparameter::xxhash(#p), default_val)) +#define PUTPARAMS(p, default_val) (GETHP->put(#p, default_val)) + #endif \ No newline at end of file From b6fd6b085730f4fefcd174477b167cc27ac576b8 Mon Sep 17 00:00:00 2001 From: jiangzhichen Date: Mon, 15 May 2023 19:34:26 +0800 Subject: [PATCH 2/2] tirvial --- examples/cpp/cxx_test.cc | 10 ++--- hyperparameter/hyperparameter.h | 76 +++------------------------------ 2 files changed, 10 insertions(+), 76 deletions(-) diff --git a/examples/cpp/cxx_test.cc b/examples/cpp/cxx_test.cc index 2c055bb..5e1f4ff 100644 --- a/examples/cpp/cxx_test.cc +++ b/examples/cpp/cxx_test.cc @@ -38,19 +38,19 @@ int main() // ======= opt api test ======= - PUTPARAMS(xacc.eager, false); - PUTPARAMS(xacc.lazy.device, "xla"); + PUTPARAM(xacc.eager, false); + PUTPARAM(xacc.lazy.device, "xla"); - std::string device_type = GETPARAMS(xacc.lazy.device, "xpu"); + std::string device_type = GETPARAM(xacc.lazy.device, "xpu"); std::cout << "\n:: (opt api) test put parameter" << std::endl << "expected: xla" << std::endl << "returned: " << device_type << std::endl << "expected: 0" << std::endl - << "returned: " << GETPARAMS(xacc.eager, true) << std::endl; + << "returned: " << GETPARAM(xacc.eager, true) << std::endl; std::cout << "\n:: (opt api) test undefined" << std::endl << "expected: 100" << std::endl - << "returned: " << GETPARAMS(xacc.dynamo.time, 100) << std::endl; + << "returned: " << GETPARAM(xacc.dynamo.time, 100) << std::endl; std::cout << "in main" << std::endl; diff --git a/hyperparameter/hyperparameter.h b/hyperparameter/hyperparameter.h index ac9ae9c..71dd61b 100644 --- a/hyperparameter/hyperparameter.h +++ b/hyperparameter/hyperparameter.h @@ -32,21 +32,7 @@ namespace hyperparameter { return finalize((len >= 32 ? h32bytes(p, len, seed) : seed + PRIME5) + len, p + (len & ~0x1F), len & 0x1F); } - - template = 32, uint64_t>::type = true> - static constexpr uint64_t hash(const char *p, uint64_t seed = 42) { - return finalize(h32bytes(p, seed) + len, p + (len & ~0x1F), - len & 0x1F); - } - - template ::type = - true> static constexpr uint64_t - hash(const char *p, uint64_t seed = 42) { - return finalize(seed + PRIME5 + len, p + (len & ~0x1F), len & 0x1F); - } - + private: static constexpr uint64_t PRIME1 = 11400714785074694791ULL; static constexpr uint64_t PRIME2 = 14029467366897019727ULL; @@ -94,33 +80,6 @@ namespace hyperparameter { return (len >= 8) ? (finalize(rotl(h ^ fetch64(p), 27) * PRIME1 + PRIME4, p + 8, len - 8)) : ((len >= 4) ? (finalize(rotl(h ^ fetch32(p), 23) * PRIME2 + PRIME3, p + 4, len - 4)) : ((len > 0) ? (finalize(rotl(h ^ fetch8(p), 11) * PRIME1, p + 1, len - 1)) : (mix1(mix1(mix1(h, PRIME2, 33), PRIME3, 29), 1, 32)))); } - - template = 8, uint64_t>::type = true> - static constexpr uint64_t finalize(const uint64_t h, const char *p) { - return finalize(rotl(h ^ fetch64(p), 27) * PRIME1 + PRIME4, - p + 8); - } - - template = 4), - uint64_t>::type = true> - static constexpr uint64_t finalize(const uint64_t h, const char *p) { - return finalize(rotl(h ^ fetch32(p), 23) * PRIME2 + PRIME3, - p + 4); - } - - template 0), - uint64_t>::type = true> - static constexpr uint64_t finalize(const uint64_t h, const char *p) { - return finalize(rotl(h ^ fetch8(p), 11) * PRIME1, p + 1); - } - - template ::type = true> - static constexpr uint64_t finalize(const uint64_t h, const char *p) { - return mix1(mix1(mix1(h, PRIME2, 33), PRIME3, 29), 1, 32); - } - static constexpr uint64_t h32bytes(const char *p, uint64_t len, const uint64_t v1, const uint64_t v2, const uint64_t v3, const uint64_t v4) { return (len >= 32) ? h32bytes(p + 32, len - 32, fetch64(p, v1), fetch64(p + 8, v2), fetch64(p + 16, v3), fetch64(p + 24, v4)) : mix3(mix3(mix3(mix3(rotl(v1, 1) + rotl(v2, 7) + rotl(v3, 12) + rotl(v4, 18), v1), v2), v3), v4); @@ -129,35 +88,10 @@ namespace hyperparameter { return h32bytes(p, len, seed + PRIME1 + PRIME2, seed + PRIME2, seed, seed - PRIME1); } - - template - static constexpr uint64_t h32bytes(const char *p, const uint64_t v1, - const uint64_t v2, const uint64_t v3, - const uint64_t v4) { - return (len >= 32) - ? h32bytes( - p + 32, fetch64(p, v1), fetch64(p + 8, v2), - fetch64(p + 16, v3), fetch64(p + 24, v4)) - : mix3(mix3(mix3(mix3(rotl(v1, 1) + rotl(v2, 7) + - rotl(v3, 12) + rotl(v4, 18), - v1), - v2), - v3), - v4); - } - - template - static constexpr uint64_t h32bytes(const char *p, const uint64_t seed) { - return h32bytes(p, seed + PRIME1 + PRIME2, seed + PRIME2, seed, - seed - PRIME1); - } }; constexpr uint64_t xxhash(const char *p, int len) { return xxh64::hash(p, len, 42); } - template - constexpr uint64_t xxhash(const char *p) { return xxh64::hash(p, (uint64_t)42); } - struct Hyperparameter { Storage *_storage; @@ -267,11 +201,11 @@ namespace hyperparameter } } -#define GETHP hyperparameter::get_hp() +#define GETHP hyperparameter::get_hp() // Implicit create hyperparameter object -#define GETPARAMS(p, default_val) \ - (GETHP->get(hyperparameter::xxhash(#p), default_val)) -#define PUTPARAMS(p, default_val) (GETHP->put(#p, default_val)) +#define GETPARAM(p, default_val) \ + (GETHP->get(([](){ constexpr uint64_t x = hyperparameter::xxhash(#p,sizeof(#p)-1); return x;})(), default_val)) +#define PUTPARAM(p, default_val) (GETHP->put(#p, default_val)) #endif \ No newline at end of file