Beispiel #1
0
 def from_backend_value(self, v, t):
     """Convert a backend value to an intermediate value."""
     if isinstance(t, abstract.AbstractScalar):
         return self.to_scalar(v)
     elif isinstance(t, abstract.AbstractArray):
         # Convert torch tensor to numpy tensor.
         output = self.to_numpy(v)
         # If possible and necessary, cast numpy tensor to expected tensor.
         array_type = t.element.xtype()
         if array_type and array_type not in _type_map:
             # Probably u16, u32 or u64. Let's cast.
             output = output.astype(type_to_np_dtype(array_type))
         return output
     elif isinstance(t, abstract.AbstractTuple):
         return tuple(
             self.from_backend_value(ve, te)
             for ve, te in zip(v, t.elements))
     elif isinstance(t, abstract.AbstractTaggedUnion):
         return TaggedValue(
             v.tag, self.from_backend_value(v.value, t.options.get(v.tag)))
     elif isinstance(t, abstract.AbstractRandomState):
         return RandomStateWrapper(self.to_numpy(v))
     elif isinstance(t, abstract.AbstractType):
         if isinstance(t.element, abstract.AbstractHandle):
             return HandleInstance
         else:
             myia_type = t.element.xtype()
             if myia_type in _type_map:
                 return getattr(np, type_to_np_dtype(myia_type))
             else:
                 return v
     else:
         raise NotImplementedError(f"Don't know what to do for {t}")
Beispiel #2
0
 def convert_random_state(self, v, t):
     return RandomStateWrapper(tuple(el.asnumpy().item() for el in v))