Repository: tinygrad/tinygrad

Issue:
the example deep_deterministic_policy_gradient.py is giving error with ops_cpu.py  
the example deep_deterministic_policy_gradient.py is giving following error with ops_cpu.py 

AttributeError: 'float' object has no attribute 'dtype' 

```
PS C:\gh\tinygrad\examples> pip install gym --user
PS C:\gh\tinygrad\examples> python deep_deterministic_policy_gradient.py
```
```
Traceback (most recent call last):
  File "C:\gh\tinygrad\examples\deep_deterministic_policy_gradient.py", line 240, in <module>
    action = agent.choose_action(prev_state)
  File "C:\gh\tinygrad\examples\deep_deterministic_policy_gradient.py", line 192, in choose_action
    return mu.detach().numpy()
  File "c:\gh\tinygrad\tinygrad\tensor.py", line 110, in numpy
    def numpy(self) -> np.ndarray: return self.lazydata.toCPU()
  File "c:\gh\tinygrad\tinygrad\lazy.py", line 194, in toCPU
    realized = self_casted.contiguous().realize().realized
  File "c:\gh\tinygrad\tinygrad\lazy.py", line 145, in realize
    elif self.optype is LoadOps: LOAD_OPS_DISPATCHER[cast(LoadOps, self.op.op)](self)
  File "c:\gh\tinygrad\tinygrad\lazy.py", line 327, in _realize_contiguous
    realized = buffer.op.src[0].realize().realized
  File "c:\gh\tinygrad\tinygrad\lazy.py", line 148, in realize
    for x in self.op.buffers: x.realize()
  File "c:\gh\tinygrad\tinygrad\lazy.py", line 148, in realize
    for x in self.op.buffers: x.realize()
  File "c:\gh\tinygrad\tinygrad\lazy.py", line 148, in realize
    for x in self.op.buffers: x.realize()
  [Previous line repeated 4 more times]
  File "c:\gh\tinygrad\tinygrad\lazy.py", line 158, in realize
    self.realized = Device[self.device].exec_ast(self.op, output=self, **self._device_extra_args())
  File "c:\gh\tinygrad\tinygrad\ops.py", line 113, in exec_ast
    srcs = [self.exec_ast(cast(LazyOp, x), context=context, **kwargs) if x.__class__ is LazyOp else self.from_lazybuffer(x) for x in ast.src]
  File "c:\gh\tinygrad\tinygrad\ops.py", line 113, in <listcomp>
    srcs = [self.exec_ast(cast(LazyOp, x), context=context, **kwargs) if x.__class__ is LazyOp else self.from_lazybuffer(x) for x in ast.src]
  File "c:\gh\tinygrad\tinygrad\ops.py", line 113, in exec_ast
    srcs = [self.exec_ast(cast(LazyOp, x), context=context, **kwargs) if x.__class__ is LazyOp else self.from_lazybuffer(x) for x in ast.src]
  File "c:\gh\tinygrad\tinygrad\ops.py", line 113, in <listcomp>
    srcs = [self.exec_ast(cast(LazyOp, x), context=context, **kwargs) if x.__class__ is LazyOp else self.from_lazybuffer(x) for x in ast.src]
  File "c:\gh\tinygrad\tinygrad\ops.py", line 113, in exec_ast
    srcs = [self.exec_ast(cast(LazyOp, x), context=context, **kwargs) if x.__class__ is LazyOp else self.from_lazybuffer(x) for x in ast.src]
  File "c:\gh\tinygrad\tinygrad\ops.py", line 113, in <listcomp>
    srcs = [self.exec_ast(cast(LazyOp, x), context=context, **kwargs) if x.__class__ is LazyOp else self.from_lazybuffer(x) for x in ast.src]
  File "c:\gh\tinygrad\tinygrad\ops.py", line 113, in exec_ast
    srcs = [self.exec_ast(cast(LazyOp, x), context=context, **kwargs) if x.__class__ is LazyOp else self.from_lazybuffer(x) for x in ast.src]
  File "c:\gh\tinygrad\tinygrad\ops.py", line 113, in <listcomp>
    srcs = [self.exec_ast(cast(LazyOp, x), context=context, **kwargs) if x.__class__ is LazyOp else self.from_lazybuffer(x) for x in ast.src]
  File "c:\gh\tinygrad\tinygrad\ops.py", line 113, in exec_ast
    srcs = [self.exec_ast(cast(LazyOp, x), context=context, **kwargs) if x.__class__ is LazyOp else self.from_lazybuffer(x) for x in ast.src]
  File "c:\gh\tinygrad\tinygrad\ops.py", line 113, in <listcomp>
    srcs = [self.exec_ast(cast(LazyOp, x), context=context, **kwargs) if x.__class__ is LazyOp else self.from_lazybuffer(x) for x in ast.src]
  File "c:\gh\tinygrad\tinygrad\ops.py", line 115, in exec_ast
    ret = self.from_underlying(self.fxn_for_op[ast.op](*([self.to_underlying(x) for x in srcs] + ([ast.arg] if ast.arg is not None else []))))
  File "c:\gh\tinygrad\tinygrad\runtime\ops_cpu.py", line 38, in <lambda>
    BinaryOps.SUB: lambda x, y: np.subtract(*match_types(x, y)), BinaryOps.MUL: lambda x, y: np.multiply(*match_types(x, y)),
  File "c:\gh\tinygrad\tinygrad\runtime\ops_cpu.py", line 21, in match_types
    up = x.dtype if dtypes.from_np(x.dtype).priority > dtypes.from_np(y.dtype).priority else y.dtype
AttributeError: 'float' object has no attribute 'dtype'
```



Discussion:

Resolving patch:
diff --git a/examples/deep_deterministic_policy_gradient.py b/examples/deep_deterministic_policy_gradient.py
--- a/examples/deep_deterministic_policy_gradient.py
+++ b/examples/deep_deterministic_policy_gradient.py
@@ -10,9 +10,6 @@
 import gym
 
 
-DEVICE = "GPU" if getenv("GPU") else "CPU"
-
-
 class Actor:
   def __init__(self, num_actions: int, num_states: int, hidden_size: Tuple[int, int] = (400, 300)):
     self.l1 = Tensor.glorot_uniform(num_states, hidden_size[0])
@@ -73,11 +70,11 @@ def sample(self) -> Tuple[Tensor, Tensor, Tensor, Tensor, Tensor]:
     record_range = min(self.buffer_counter, self.buffer_capacity)
     batch_indices = np.random.choice(record_range, self.batch_size)
 
-    state_batch = Tensor(self.state_buffer[batch_indices], device=DEVICE, requires_grad=False)
-    action_batch = Tensor(self.action_buffer[batch_indices], device=DEVICE, requires_grad=False)
-    reward_batch = Tensor(self.reward_buffer[batch_indices], device=DEVICE, requires_grad=False)
-    next_state_batch = Tensor(self.next_state_buffer[batch_indices], device=DEVICE, requires_grad=False)
-    done_batch = Tensor(self.done_buffer[batch_indices], device=DEVICE, requires_grad=False)
+    state_batch = Tensor(self.state_buffer[batch_indices], requires_grad=False)
+    action_batch = Tensor(self.action_buffer[batch_indices], requires_grad=False)
+    reward_batch = Tensor(self.reward_buffer[batch_indices], requires_grad=False)
+    next_state_batch = Tensor(self.next_state_buffer[batch_indices], requires_grad=False)
+    done_batch = Tensor(self.done_buffer[batch_indices], requires_grad=False)
 
     return state_batch, action_batch, reward_batch, next_state_batch, done_batch
 
@@ -92,7 +89,6 @@ def __call__(self) -> Tensor:
         np.random.default_rng()
         .normal(self.mean, self.std_dev, size=self.mean.shape)
         .astype(np.float32),
-        device=DEVICE,
         requires_grad=False,
     )
 
@@ -158,9 +154,6 @@ def __init__(
     target_actor_params = get_parameters(self.target_actor)
     target_critic_params = get_parameters(self.target_critic)
 
-    if DEVICE == "GPU":
-      [x.gpu_() for x in actor_params + critic_params + target_actor_params + target_critic_params]
-
     self.actor_optimizer = optim.Adam(actor_params, lr_actor)
     self.critic_optimizer = optim.Adam(critic_params, lr_critic)
 
@@ -207,7 +200,7 @@ def learn(self) -> None:
     target_actions = self.target_actor.forward(next_state_batch, self.max_action)
     y = reward_batch + self.gamma * self.target_critic.forward(
         next_state_batch, target_actions.detach()
-    ) * (Tensor.ones(*done_batch.shape, device=DEVICE, requires_grad=False) - done_batch)
+    ) * (Tensor.ones(*done_batch.shape, requires_grad=False) - done_batch)
 
     self.critic_optimizer.zero_grad()
     critic_value = self.critic.forward(state_batch, action_batch)
@@ -236,7 +229,8 @@ def learn(self) -> None:
     done = False
 
     while not done:
-      prev_state = Tensor(prev_state, device=DEVICE, requires_grad=False)
+      print(episode)
+      prev_state = Tensor(prev_state, requires_grad=False)
       action = agent.choose_action(prev_state)
 
       state, reward, done, _, info = env.step(action)  # for older gym versions there is only one bool, so remove _


Test patch: