10 #include <grpc/grpc.h> 11 #include <grpcpp/create_channel.h> 12 #include <grpcpp/security/credentials.h> 14 #include <boost/uuid/uuid.hpp> 15 #include <boost/uuid/uuid_generators.hpp> 16 #include <boost/uuid/uuid_io.hpp> 18 #include "jobs/scope_guard.hpp" 26 #ifndef DBG_SESSION_HPP 27 #define DBG_SESSION_HPP 55 return std::hash<std::string>()(ss.str());
63 return hasher(lhs) == hasher(rhs);
78 logs::infof(
"created session: %s",
sess_id_.c_str());
84 grpc::InsecureChannelCredentials()), client_cfg) {}
92 for (
auto root : roots)
95 root->accept(pfinder);
98 for (
auto& assocs : pfinder.
parents_)
100 for (
auto& parent_pair : assocs.second)
103 static_cast<teq::iOperableFunc*>(parent_pair.first));
108 tenncor::CreateGraphRequest request;
109 auto payload = request.mutable_payload();
113 auto tens = statpair.first;
114 auto& range = statpair.second;
120 auto node = payload->add_nodes();
122 auto tags = node->mutable_tags();
124 tenncor::Strings tag_str;
125 tag_str.add_strings(tens->to_string());
129 tenncor::Strings type_str;
130 if (0 == range.upper_)
132 type_str.add_strings(
"leaf");
136 type_str.add_strings(
"functor");
142 std::map<std::string,tenncor::Strings> outer_tags;
143 for (
auto& itags : inner_tags)
145 google::protobuf::RepeatedPtrField<std::string>
146 field(itags.second.begin(), itags.second.end());
147 tenncor::Strings otags;
148 otags.mutable_strings()->Swap(&field);
149 outer_tags.emplace(itags.first, otags);
151 tags->insert(outer_tags.begin(), outer_tags.end());
153 auto s = tens->shape();
154 google::protobuf::RepeatedField<uint32_t> shape(
156 node->mutable_shape()->Swap(&shape);
157 auto location = node->mutable_location();
158 location->set_maxheight(range.upper_);
159 location->set_minheight(range.lower_);
164 auto tens = statpair.first;
165 auto& range = statpair.second;
166 if (range.upper_ > 0)
170 for (
size_t i = 0, n = children.size(); i < n; ++i)
172 auto& child = children[i];
173 auto child_tens = child.get_tensor().get();
174 auto shaper = child.get_shaper();
175 auto coorder = child.get_coorder();
182 if (
false == estd::has(
edges_, edgeinfo))
186 auto edge = payload->add_edges();
189 edge->set_label(label);
192 edge->set_shaper(shaper->to_string());
196 edge->set_coorder(coorder->to_string());
209 jobs::ScopeGuard defer([
this]() { ++this->
update_it_; });
220 std::list<teq::iOperableFunc*> reqs;
224 acceptable.emplace(root.get());
232 if (estd::has(acceptable, op) &&
233 false == estd::has(ignored, op))
236 auto& children = op->get_children();
237 for (
auto& child : children)
239 acceptable.emplace(child.get_tensor().get());
244 std::vector<tenncor::UpdateNodeDataRequest> requests;
249 if (0 == statpair.second.upper_)
251 auto leaf =
static_cast<teq::iLeaf*
>(statpair.first);
252 egen::_GENERATED_DTYPE dtype =
253 (egen::_GENERATED_DTYPE) leaf->
type_code();
254 std::vector<float> data;
255 size_t nelems = leaf->shape().n_elems();
256 egen::type_convert(data, leaf->data(), dtype, nelems);
258 tenncor::UpdateNodeDataRequest request;
259 auto payload = request.mutable_payload();
262 google::protobuf::RepeatedField<float> field(
263 data.begin(), data.end());
264 payload->mutable_data()->Swap(&field);
265 requests.push_back(request);
270 for (
auto& op : reqs)
273 egen::_GENERATED_DTYPE dtype =
274 (egen::_GENERATED_DTYPE) op->type_code();
275 std::vector<float> data;
276 size_t nelems = op->shape().n_elems();
277 egen::type_convert(data, op->data(), dtype, nelems);
281 tenncor::UpdateNodeDataRequest request;
282 auto payload = request.mutable_payload();
285 google::protobuf::RepeatedField<float> field(
286 data.begin(), data.end());
287 payload->mutable_data()->Swap(&field);
288 requests.push_back(request);
299 jobs::ScopeGuard defer([
this]() { ++this->
update_it_; });
310 std::list<teq::iOperableFunc*> reqs;
312 for (
auto& root : targeted)
314 acceptable.emplace(root);
321 if (estd::has(acceptable, op) &&
322 false == estd::has(ignored, op))
325 auto& children = op->get_children();
326 for (
auto& child : children)
328 acceptable.emplace(child.get_tensor().get());
333 std::vector<tenncor::UpdateNodeDataRequest> requests;
334 requests.reserve(reqs.size());
338 if (0 == statpair.second.upper_)
340 auto leaf =
static_cast<teq::iLeaf*
>(statpair.first);
341 egen::_GENERATED_DTYPE dtype =
342 (egen::_GENERATED_DTYPE) leaf->
type_code();
343 std::vector<float> data;
344 size_t nelems = leaf->shape().n_elems();
345 egen::type_convert(data, leaf->data(), dtype, nelems);
347 tenncor::UpdateNodeDataRequest request;
348 auto payload = request.mutable_payload();
351 google::protobuf::RepeatedField<float> field(
352 data.begin(), data.end());
353 payload->mutable_data()->Swap(&field);
354 requests.push_back(request);
359 for (
auto& op : reqs)
362 egen::_GENERATED_DTYPE dtype =
363 (egen::_GENERATED_DTYPE) op->type_code();
364 std::vector<float> data;
365 size_t nelems = op->shape().n_elems();
366 egen::type_convert(data, op->data(), dtype, nelems);
369 tenncor::UpdateNodeDataRequest request;
370 auto payload = request.mutable_payload();
373 google::protobuf::RepeatedField<float> field(
374 data.begin(), data.end());
375 payload->mutable_data()->Swap(&field);
376 requests.push_back(request);
400 for (
auto& assocs : pfinder.
parents_)
402 for (
auto& parent_pair : assocs.second)
405 static_cast<teq::iOperableFunc*>(parent_pair.first));
410 tenncor::UpdateGraphRequest request;
411 auto payload = request.mutable_payload();
415 auto tens = statpair.first;
416 auto& range = statpair.second;
422 auto node = payload->add_nodes();
424 auto tags = node->mutable_tags();
426 tenncor::Strings tag_str;
427 tag_str.add_strings(tens->to_string());
431 tenncor::Strings type_str;
432 if (0 == range.upper_)
434 type_str.add_strings(
"leaf");
438 type_str.add_strings(
"functor");
444 std::map<std::string,tenncor::Strings> outer_tags;
445 for (
auto& itags : inner_tags)
447 google::protobuf::RepeatedPtrField<std::string>
448 field(itags.second.begin(), itags.second.end());
449 tenncor::Strings otags;
450 otags.mutable_strings()->Swap(&field);
451 outer_tags.emplace(itags.first, otags);
453 tags->insert(outer_tags.begin(), outer_tags.end());
455 auto s = tens->shape();
456 google::protobuf::RepeatedField<uint32_t> shape(
458 node->mutable_shape()->Swap(&shape);
459 auto location = node->mutable_location();
460 location->set_maxheight(range.upper_);
461 location->set_minheight(range.lower_);
466 auto tens = statpair.first;
467 auto& range = statpair.second;
468 if (range.upper_ > 0)
472 for (
size_t i = 0, n = children.size(); i < n; ++i)
474 auto& child = children[i];
475 auto child_tens = child.get_tensor().get();
476 auto shaper = child.get_shaper();
477 auto coorder = child.get_coorder();
484 if (
false == estd::has(
edges_, edgeinfo))
488 auto edge = payload->add_edges();
491 edge->set_label(label);
494 edge->set_shaper(shaper->to_string());
498 edge->set_coorder(coorder->to_string());
516 const std::chrono::time_point<std::chrono::system_clock>& deadline)
518 std::condition_variable client_done;
519 std::thread timed_killer(
523 std::unique_lock<std::mutex> lck(mtx);
524 client_done.wait_until(lck, deadline);
528 client_done.notify_one();
545 std::unique_ptr<tenncor::GraphEmitter::Stub>
stub_;
559 std::unordered_set<EdgeInfo,EdgeInfoHash>
edges_;
575 #endif // DBG_SESSION_HPP InteractiveSession(std::shared_ptr< grpc::ChannelInterface > channel, ClientConfig client_cfg=ClientConfig(), tag::TagRegistry ®istry=tag::get_reg())
Definition: session.hpp:72
std::unordered_set< teq::iTensor * > TensSetT
Hash set of raw tensor pointers.
Definition: itensor.hpp:63
std::unique_ptr< tenncor::GraphEmitter::Stub > stub_
GRPC Client.
Definition: session.hpp:545
void track(teq::TensptrsT roots) override
Implementation of iSession.
Definition: session.hpp:87
std::unordered_set< EdgeInfo, EdgeInfoHash > edges_
Definition: session.hpp:559
std::unordered_map< iTensor *, estd::NumRange< size_t > > graphsize_
Definition: traveler.hpp:105
void update_target(teq::TensSetT targeted, teq::TensSetT ignored={}) override
Implementation of iSession.
Definition: session.hpp:295
virtual const ArgsT & get_children(void) const =0
Return children nodes as a vector of raw pointers.
static boost::uuids::random_generator uuid_gen_
UUID random generator.
Definition: session.hpp:70
Session that makes GRPC client calls.
Definition: session.hpp:67
std::string label_
Definition: session.hpp:43
Interface of iOperation-defined operation node.
Definition: ifunctor.hpp:28
void update(teq::TensSetT ignored={}) override
Implementation of iSession.
Definition: session.hpp:207
Registry for associating tensors to tag collectives.
Definition: tag.hpp:165
static const std::string edge_label_fmt
Definition: session.hpp:36
std::unordered_map< teq::iTensor *, size_t > node_ids_
Definition: session.hpp:557
Definition: custom_functor.hpp:20
void create_graph(tenncor::CreateGraphRequest &request)
Add job that pass CreateGraphRequest.
Definition: client.hpp:84
Definition: session.hpp:47
size_t parent_
Definition: session.hpp:41
std::unordered_map< iTensor *, ParentMapT > parents_
Definition: traveler.hpp:189
void track(teq::TensptrsT roots) override
Implementation of iSession.
Definition: session.hpp:50
void optimize(const opt::OptCtx &rules)
Apply input optimization rules using opt module, then re-track.
Definition: session.hpp:146
virtual size_t type_code(void) const =0
Return data type encoding.
void join(void)
Wait until client completes its request calls.
Definition: session.hpp:509
std::unordered_map< teq::iTensor *, std::unordered_set< teq::iOperableFunc * > > parents_
Definition: session.hpp:568
Configuration wrapper for creating the client.
Definition: client.hpp:31
std::string sess_id_
Definition: session.hpp:554
Session interface that tracks and rapidly updates subgraphs.
Definition: session.hpp:27
std::vector< teq::iOperableFunc * > ops_
Operable functors ordered by height in the tracked graph.
Definition: session.hpp:158
InteractiveSession(std::string host, ClientConfig client_cfg=ClientConfig())
Definition: session.hpp:81
Traveler that for each child tracks the relationship to all parents.
Definition: traveler.hpp:162
teq::TensptrSetT tracked_
Definition: session.hpp:155
Encapsulation of all conversion rules.
Definition: optimize.hpp:23
Traveler that maps each tensor to its subtree's maximum depth.
Definition: traveler.hpp:57
GraphEmitterClient client_
Definition: session.hpp:563
size_t operator()(const EdgeInfo &edge) const
Definition: session.hpp:49
std::string to_string(teq::CoordptrT c)
Return brief hashable string representation of coordinate mapper.
std::vector< TensptrT > TensptrsT
Vector of tensor smart pointers.
Definition: itensor.hpp:60
void update_graph(tenncor::UpdateGraphRequest &request)
Add job that pass UpdateGraphRequest.
Definition: client.hpp:142
void join_then_stop(const std::chrono::time_point< std::chrono::system_clock > &deadline)
Wait until specified deadline, then terminate all jobs in the client.
Definition: session.hpp:515
void stop(void)
Kill all request jobs.
Definition: session.hpp:533
bool operator==(const EdgeInfo &lhs, const EdgeInfo &rhs)
Graph edge equality.
Definition: session.hpp:60
void update_node_data(std::vector< tenncor::UpdateNodeDataRequest > &requests, size_t update_it)
Add job that streams UpdateNodeDataRequest.
Definition: client.hpp:200
eteq::Session sess_
Session underneath.
Definition: session.hpp:548
static const size_t data_sync_interval
Definition: client.hpp:28
TagRegistry & get_reg(void)
Return reference to global tag registry.
TagRepsT get_tags(const teq::iTensor *tens)
Return all key-labels under the collective associated with tens.
Definition: tag.hpp:184
static const std::string tag_node_type
Definition: session.hpp:34
void clear(void)
Kill all request jobs.
Definition: client.hpp:279
Graph edge hashing.
Definition: session.hpp:47
size_t update_it_
Definition: session.hpp:561
Interface of traversible and differentiable nodes with shape information.
Definition: itensor.hpp:36
GRPC client that checks for server health and make graph creation and update calls.
Definition: client.hpp:49
bool is_connected(void)
Return true if the client is connected to the server.
Definition: client.hpp:267
std::string get_session_id(void) const
Return session id.
Definition: session.hpp:539
void update(teq::TensSetT ignored={}) override
Implementation of iSession.
Definition: session.hpp:82
tag::TagRegistry & registry_
Tag registry.
Definition: session.hpp:551
static const std::string tag_str_key
Definition: session.hpp:32
void join(void)
Wait until all request jobs are complete.
Definition: client.hpp:273
size_t child_
Definition: session.hpp:42
void update_target(teq::TensSetT target, teq::TensSetT ignored={}) override
Implementation of iSession.
Definition: session.hpp:114
Graph edge intermediate representation.
Definition: session.hpp:39
void optimize(const opt::OptCtx &rules)
Apply input optimization rules using opt module, then re-track.
Definition: session.hpp:383
Leaf of the graph commonly representing the variable in an equation.
Definition: ileaf.hpp:19
teq::GraphStat stat_
Definition: session.hpp:565
bool is_identity(iCoordMap *coorder)
Checks if the coord mapper is an identity mapper.