MCPcopy Create free account
hub / github.com/YeWR/EfficientZero / BatchWorker_GPU

Class BatchWorker_GPU

core/reanalyze_worker.py:267–513  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

265
266@ray.remote(num_gpus=0.125)
267class BatchWorker_GPU(object):
268 def __init__(self, worker_id, replay_buffer, storage, batch_storage, mcts_storage, config):
269 """GPU Batch Worker for reanalyzing targets, see Appendix.
270 receive the context from CPU maker and deal with GPU overheads
271 Parameters
272 ----------
273 worker_id: int
274 id of the worker
275 replay_buffer: Any
276 Replay buffer
277 storage: Any
278 The model storage
279 batch_storage: Any
280 The batch storage (batch queue)
281 mcts_storage: Ant
282 The mcts-related contexts storage
283 """
284 self.replay_buffer = replay_buffer
285 self.config = config
286 self.worker_id = worker_id
287
288 self.model = config.get_uniform_network()
289 self.model.to(config.device)
290 self.model.eval()
291
292 self.mcts_storage = mcts_storage
293 self.storage = storage
294 self.batch_storage = batch_storage
295
296 self.last_model_index = 0
297
298 def _prepare_reward_value(self, reward_value_context):
299 """prepare reward and value targets from the context of rewards and values
300 """
301 value_obs_lst, value_mask, state_index_lst, rewards_lst, traj_lens, td_steps_lst = reward_value_context
302 value_obs_lst = ray.get(value_obs_lst)
303 device = self.config.device
304 batch_size = len(value_obs_lst)
305
306 batch_values, batch_value_prefixs = [], []
307 with torch.no_grad():
308 value_obs_lst = prepare_observation_lst(value_obs_lst)
309 # split a full batch into slices of mini_infer_size: to save the GPU memory for more GPU actors
310 m_batch = self.config.mini_infer_size
311 slices = np.ceil(batch_size / m_batch).astype(np.int_)
312 network_output = []
313 for i in range(slices):
314 beg_index = m_batch * i
315 end_index = m_batch * (i + 1)
316 m_obs = torch.from_numpy(value_obs_lst[beg_index:end_index]).to(device).float() / 255.0
317 if self.config.amp_type == 'torch_amp':
318 with autocast():
319 m_output = self.model.initial_inference(m_obs)
320 else:
321 m_output = self.model.initial_inference(m_obs)
322 network_output.append(m_output)
323
324 # concat the output slices after model inference

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected