MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / ConcurrentSteps

Function ConcurrentSteps

tensorflow/cc/tutorials/example_trainer.cc:99–147  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

97}
98
99void ConcurrentSteps(const Options* opts, int session_index) {
100 // Creates a session.
101 SessionOptions options;
102 std::unique_ptr<Session> session(NewSession(options));
103 GraphDef def = CreateGraphDef();
104 if (options.target.empty()) {
105 graph::SetDefaultDevice(opts->use_gpu ? "/device:GPU:0" : "/cpu:0", &def);
106 }
107
108 TF_CHECK_OK(session->Create(def));
109
110 // Spawn M threads for M concurrent steps.
111 const int M = opts->num_concurrent_steps;
112 std::unique_ptr<thread::ThreadPool> step_threads(
113 new thread::ThreadPool(Env::Default(), "trainer", M));
114
115 for (int step = 0; step < M; ++step) {
116 step_threads->Schedule([&session, opts, session_index, step]() {
117 // Randomly initialize the input.
118 Tensor x(DT_FLOAT, TensorShape({2, 1}));
119 auto x_flat = x.flat<float>();
120 x_flat.setRandom();
121 Eigen::Tensor<float, 0, Eigen::RowMajor> inv_norm =
122 x_flat.square().sum().sqrt().inverse();
123 x_flat = x_flat * inv_norm();
124
125 // Iterations.
126 std::vector<Tensor> outputs;
127 for (int iter = 0; iter < opts->num_iterations; ++iter) {
128 outputs.clear();
129 TF_CHECK_OK(
130 session->Run({{"x", x}}, {"y:0", "y_normalized:0"}, {}, &outputs));
131 CHECK_EQ(size_t{2}, outputs.size());
132
133 const Tensor& y = outputs[0];
134 const Tensor& y_norm = outputs[1];
135 // Print out lambda, x, and y.
136 std::printf("%06d/%06d %s\n", session_index, step,
137 DebugString(x, y).c_str());
138 // Copies y_normalized to x.
139 x = y_norm;
140 }
141 });
142 }
143
144 // Delete the threadpool, thus waiting for all threads to complete.
145 step_threads.reset(nullptr);
146 TF_CHECK_OK(session->Close());
147}
148
149void ConcurrentSessions(const Options& opts) {
150 // Spawn N threads for N concurrent sessions.

Callers

nothing calls this directly

Calls 15

SetDefaultDeviceFunction · 0.85
DefaultFunction · 0.85
c_strMethod · 0.80
CreateGraphDefFunction · 0.70
DebugStringFunction · 0.70
NewSessionFunction · 0.50
TensorShapeClass · 0.50
emptyMethod · 0.45
CreateMethod · 0.45
ScheduleMethod · 0.45
inverseMethod · 0.45
sumMethod · 0.45

Tested by

no test coverage detected