提交 0a9707ea authored 作者: lamblin's avatar lamblin

Merge pull request #447 from delallea/clearer_borrow

Simplified code for In.borrow and Param.borrow
...@@ -161,11 +161,10 @@ class In(SymbolicInput): ...@@ -161,11 +161,10 @@ class In(SymbolicInput):
True: permit the compiled function to modify the python object being passed as the input True: permit the compiled function to modify the python object being passed as the input
False: do not permit the compiled function to modify the python object being passed as the input. False: do not permit the compiled function to modify the python object being passed as the input.
borrow: Bool (default: False if mutable evaluate to False, True otherwise) borrow: Bool (default: take the same value as mutable)
True: permit the output of the compiled function to be aliased to the input True: permit the output of the compiled function to be aliased to the input
False: do not permit any output to be aliased to the input False: do not permit any output to be aliased to the input
strict: Bool (default: False) strict: Bool (default: False)
True: means that the value you pass for this input must have exactly the right type True: means that the value you pass for this input must have exactly the right type
False: the value you pass for this input may be cast automatically to the proper type False: the value you pass for this input may be cast automatically to the proper type
...@@ -201,24 +200,22 @@ class In(SymbolicInput): ...@@ -201,24 +200,22 @@ class In(SymbolicInput):
#in the function or from the caller #in the function or from the caller
self.shared = shared self.shared = shared
# mutable implies the output can be both aliased to the input and that the input can be
# destroyed. borrow simply implies the output can be aliased to the input. Thus
# mutable=True should require borrow=True. Raise warning when borrow is explicitely set
# to False with mutable=True.
if mutable:
if borrow == False:
_logger.warning("Symbolic input for variable %s (name=%s) has "
"flags mutable=True, borrow=False. This combination is "
"incompatible since mutable=True implies that the "
"input variable may be both aliased (borrow=True) and "
"over-written. We set borrow=True and continue.",
variable, name)
borrow = True
# borrow=None basically means False. We can't set default value to False because of the
# above business with mutable.
if borrow is None: if borrow is None:
borrow = False self.borrow = mutable
else:
self.borrow = borrow
# mutable implies the output can be both aliased to the input and that
# the input can be destroyed. borrow simply implies the output can be
# aliased to the input. Thus mutable=True should require borrow=True.
if mutable and not self.borrow:
raise AssertionError(
"Symbolic input for variable %s (name=%s) has "
"flags mutable=True, borrow=False. This combination is "
"incompatible since mutable=True implies that the "
"input variable may be both aliased (borrow=True) and "
"overwritten.",
variable, name)
if implicit is None: if implicit is None:
implicit = (isinstance(value, gof.Container) or implicit = (isinstance(value, gof.Container) or
...@@ -233,7 +230,6 @@ class In(SymbolicInput): ...@@ -233,7 +230,6 @@ class In(SymbolicInput):
autoname=autoname, autoname=autoname,
implicit=implicit) implicit=implicit)
self.value = value self.value = value
self.borrow = borrow
if self.implicit and value is None: if self.implicit and value is None:
raise TypeError('An implicit input must be given a default value') raise TypeError('An implicit input must be given a default value')
......
...@@ -268,9 +268,9 @@ class Param(object): ...@@ -268,9 +268,9 @@ class Param(object):
:param mutable: True -> function is allowed to modify this argument. :param mutable: True -> function is allowed to modify this argument.
:param borrow: True -> function is allowed to alias some output to :param borrow: Whether the function is allowed to alias some output to
this input this input. Using None (default) means we re-use the same value as the
`mutable` flag.
False: do not permit any output to be aliased to the input False: do not permit any output to be aliased to the input
:param strict: False -> function arguments may be copied or cast to match the :param strict: False -> function arguments may be copied or cast to match the
...@@ -289,25 +289,27 @@ class Param(object): ...@@ -289,25 +289,27 @@ class Param(object):
self.default = default self.default = default
self.name = name self.name = name
self.mutable = mutable self.mutable = mutable
# mutable implies the output can be both aliased to the input and that the input can be
# destroyed. borrow simply implies the output can be aliased to the input. Thus if borrow is None:
# mutable=True should require borrow=True. Raise warning when borrow is explicitely set self.borrow = self.mutable
# to False with mutable=True. else:
if mutable: self.borrow = borrow
# Do not compare with "if not borrow", because borrow can be None,
# and "not None" would be True. # mutable implies the output can be both aliased to the input and that
if borrow is False: # the input can be destroyed. borrow simply implies the output can be
_logger.warning("Symbolic input for variable %s (name=%s) has " # aliased to the input. Thus mutable=True should require borrow=True.
"flags mutable=True, borrow=False. This combination is " if self.mutable and not self.borrow:
"incompatible since mutable=True implies that the " raise AssertionError(
"input variable may be both aliased (borrow=True) and " "Symbolic input for variable %s (name=%s) has "
"over-written. We set borrow=True and continue.", "flags mutable=True, borrow=False. This combination is "
variable, name) "incompatible since mutable=True implies that the "
borrow = True "input variable may be both aliased (borrow=True) and "
"overwritten.",
variable, name)
self.strict = strict self.strict = strict
self.allow_downcast = allow_downcast self.allow_downcast = allow_downcast
self.implicit = implicit self.implicit = implicit
self.borrow = borrow
def pfunc(params, outputs=None, mode=None, updates=[], givens=[], def pfunc(params, outputs=None, mode=None, updates=[], givens=[],
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论