提交 5381bbda authored 作者: Arnaud Bergeron's avatar Arnaud Bergeron

Make broadcast fixup in filter_variable() depend on an argument and stop doing…

Make broadcast fixup in filter_variable() depend on an argument and stop doing it for shared variables.
上级 b2f62a1e
...@@ -101,7 +101,8 @@ def rebuild_collect_shared(outputs, ...@@ -101,7 +101,8 @@ def rebuild_collect_shared(outputs,
# Do not use default_update if a "real" update was # Do not use default_update if a "real" update was
# provided # provided
if v not in update_d: if v not in update_d:
v_update = v.type.filter_variable(v.default_update) v_update = v.type.filter_variable(v.default_update,
allow_convert=False)
if v_update.type != v.type: if v_update.type != v.type:
raise TypeError( raise TypeError(
'an update must have the same type as ' 'an update must have the same type as '
...@@ -197,7 +198,8 @@ def rebuild_collect_shared(outputs, ...@@ -197,7 +198,8 @@ def rebuild_collect_shared(outputs,
# filter_variable ensure smooth conversion of cpu/gpu Types # filter_variable ensure smooth conversion of cpu/gpu Types
try: try:
update_val = store_into.type.filter_variable(update_val) update_val = store_into.type.filter_variable(update_val,
allow_convert=False)
except TypeError: except TypeError:
err_msg = ('An update must have the same type as the' err_msg = ('An update must have the same type as the'
' original shared variable (shared_var=%s,' ' original shared variable (shared_var=%s,'
......
...@@ -21,7 +21,7 @@ class NullType(Type): ...@@ -21,7 +21,7 @@ class NullType(Type):
def filter(self, data, strict=False, allow_downcast=None): def filter(self, data, strict=False, allow_downcast=None):
raise ValueError("No values may be assigned to a NullType") raise ValueError("No values may be assigned to a NullType")
def filter_variable(self, other): def filter_variable(self, other, allow_convert=True):
raise ValueError("No values may be assigned to a NullType") raise ValueError("No values may be assigned to a NullType")
def may_share_memory(a, b): def may_share_memory(a, b):
......
...@@ -263,7 +263,7 @@ class PureType(object): ...@@ -263,7 +263,7 @@ class PureType(object):
# def filter_inplace(value, storage, strict=False, allow_downcast=None) # def filter_inplace(value, storage, strict=False, allow_downcast=None)
def filter_variable(self, other): def filter_variable(self, other, allow_convert=True):
"""Convert a symbolic variable into this Type, if compatible. """Convert a symbolic variable into this Type, if compatible.
For the moment, the only Types compatible with one another are For the moment, the only Types compatible with one another are
...@@ -277,7 +277,7 @@ class PureType(object): ...@@ -277,7 +277,7 @@ class PureType(object):
# a Constant of the appropriate Type. # a Constant of the appropriate Type.
other = self.Constant(type=self, data=other) other = self.Constant(type=self, data=other)
if other.type != self: if other.type != self and allow_convert:
other2 = self.convert_variable(other) other2 = self.convert_variable(other)
if other2 is not None: if other2 is not None:
return other2 return other2
...@@ -290,6 +290,24 @@ class PureType(object): ...@@ -290,6 +290,24 @@ class PureType(object):
% dict(othertype=other.type, other=other, self=self)) % dict(othertype=other.type, other=other, self=self))
return other return other
def convert_variable(self, var):
"""Patch variable so that its type will match self, if possible.
If the variable can't be converted, this should return None.
The conversion can only happen if the following implication is
true for all possible `val`.
self.is_valid_value(val) => var.type.is_valid_value(val)
For the majority of types this means that you can only have
non-broadcastable dimensions become broadcastable and not the
inverse.
The default is to not convert anything which is always safe.
"""
return None
def is_valid_value(self, a): def is_valid_value(self, a):
"""Required: Return True for any python object `a` that would be a """Required: Return True for any python object `a` that would be a
legal value for a Variable of this Type""" legal value for a Variable of this Type"""
...@@ -409,23 +427,6 @@ class Type(object2, PureType, CLinkerType): ...@@ -409,23 +427,6 @@ class Type(object2, PureType, CLinkerType):
do type-checking in pattern-based optimizations. do type-checking in pattern-based optimizations.
""" """
def convert_variable(self, var):
"""Patch variable so that its type will match self, if possible.
If the variable can't be converted, this should return None.
The conversion can only happen if the following implication is
true for all possible `val`.
self.is_valid_value(val) => var.type.is_valid_value(val)
For the majority of types this means that you can only have
non-broadcastable dimensions become broadcastable and not the
inverse.
The default is to not convert anything which is always safe.
"""
return None
class SingletonType(Type): class SingletonType(Type):
......
...@@ -119,7 +119,7 @@ class CudaNdarrayType(Type): ...@@ -119,7 +119,7 @@ class CudaNdarrayType(Type):
% (self, self.dtype, data, converted_data, self.dtype), % (self, self.dtype, data, converted_data, self.dtype),
data) data)
def filter_variable(self, other): def filter_variable(self, other, allow_convert=True):
"""Convert a Variable into a CudaNdarrayType, if compatible. """Convert a Variable into a CudaNdarrayType, if compatible.
This Variable should either already be a CudaNdarrayType, or be This Variable should either already be a CudaNdarrayType, or be
...@@ -146,8 +146,11 @@ class CudaNdarrayType(Type): ...@@ -146,8 +146,11 @@ class CudaNdarrayType(Type):
raise TypeError('Incompatible number of dimensions.' raise TypeError('Incompatible number of dimensions.'
' Expected %d, got %d.' % (self.ndim, other.ndim)) ' Expected %d, got %d.' % (self.ndim, other.ndim))
if other.type.broadcastable != self.broadcastable: if other.type.broadcastable != self.broadcastable:
type2 = other.type.clone(broadcastable=self.broadcastable) if allow_convert:
other2 = type2.convert_variable(other) type2 = other.type.clone(broadcastable=self.broadcastable)
other2 = type2.convert_variable(other)
else:
other2 = None
if other2 is None: if other2 is None:
raise TypeError('Incompatible broadcastable dimensions.' raise TypeError('Incompatible broadcastable dimensions.'
' Expected %s, got %s.' % ' Expected %s, got %s.' %
......
...@@ -89,7 +89,7 @@ class GpuArrayType(Type): ...@@ -89,7 +89,7 @@ class GpuArrayType(Type):
" dimension.", shp, self.broadcastable) " dimension.", shp, self.broadcastable)
return data return data
def filter_variable(self, other): def filter_variable(self, other, allow_convert=True):
if hasattr(other, '_as_GpuArrayVariable'): if hasattr(other, '_as_GpuArrayVariable'):
other = other._as_GpuArrayVariable() other = other._as_GpuArrayVariable()
...@@ -108,8 +108,11 @@ class GpuArrayType(Type): ...@@ -108,8 +108,11 @@ class GpuArrayType(Type):
raise TypeError('Incompatible number of dimensions.' raise TypeError('Incompatible number of dimensions.'
' Expected %d, got %d.' % (self.ndim, other.ndim)) ' Expected %d, got %d.' % (self.ndim, other.ndim))
if other.type.broadcastable != self.broadcastable: if other.type.broadcastable != self.broadcastable:
type2 = other.type.clone(broadcastable=self.broadcastable) if allow_convert:
other2 = type2.convert_variable(other) type2 = other.type.clone(broadcastable=self.broadcastable)
other2 = type2.convert_variable(other)
else:
other2 = None
if other2 is None: if other2 is None:
raise TypeError('Incompatible broadcastable dimensions.' raise TypeError('Incompatible broadcastable dimensions.'
' Expected %s, got %s.' % ' Expected %s, got %s.' %
......
...@@ -190,7 +190,7 @@ class TensorType(Type): ...@@ -190,7 +190,7 @@ class TensorType(Type):
raise ValueError("non-finite elements not allowed") raise ValueError("non-finite elements not allowed")
return data return data
def filter_variable(self, other): def filter_variable(self, other, allow_convert=True):
"""Convert a symbolic Variable into a TensorType, if compatible. """Convert a symbolic Variable into a TensorType, if compatible.
For the moment, only a TensorType or CudaNdarrayType will be For the moment, only a TensorType or CudaNdarrayType will be
...@@ -208,10 +208,11 @@ class TensorType(Type): ...@@ -208,10 +208,11 @@ class TensorType(Type):
if other.type == self: if other.type == self:
return other return other
# Attempt safe broadcast conversion. if allow_convert:
other2 = self.convert_variable(other) # Attempt safe broadcast conversion.
if other2 is not None and other2.type == self: other2 = self.convert_variable(other)
return other2 if other2 is not None and other2.type == self:
return other2
raise TypeError( raise TypeError(
'Cannot convert Type %(othertype)s ' 'Cannot convert Type %(othertype)s '
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论