Transformer temporary data. An instance of this class holds all the information relevant to a call to a transformer instance (that is, a call to __call__). An instance is created for the life-time of the __call__ function and is passed as argument to the handlers.
| 317 | |
| 318 | |
| 319 | class _TmpInfo(object): |
| 320 | """Transformer temporary data. |
| 321 | |
| 322 | An instance of this class holds all the information relevant to a call |
| 323 | to a transformer instance (that is, a call to __call__). An instance |
| 324 | is created for the life-time of the __call__ function and is passed as |
| 325 | argument to the handlers. |
| 326 | """ |
| 327 | |
| 328 | def __init__(self, sgv, dst_graph, dst_scope, src_scope): |
| 329 | self.sgv = sgv |
| 330 | self.sgv_inputs_set = frozenset(sgv.inputs) |
| 331 | self.ops = frozenset(sgv.ops) |
| 332 | self.control_outputs = util.ControlOutputs(sgv.graph) |
| 333 | self.graph = sgv.graph |
| 334 | self.scope = src_scope |
| 335 | self.graph_ = dst_graph |
| 336 | self.scope_ = dst_scope |
| 337 | self.transformed_ops = {} |
| 338 | self.transformed_ts = {} |
| 339 | self.collections = dict((key, self.graph.get_collection(key)) |
| 340 | for key in self.graph.get_all_collection_keys()) |
| 341 | self.cyclic_ops = [] |
| 342 | self.transform_original_op_handler = transform_op_if_inside_handler |
| 343 | # The graph is transformed op by op, in the same order the original ops |
| 344 | # were created. However, this is sometimes not possible due to cycles |
| 345 | # (i.e. while loops). So when the transformer creates a new op whose |
| 346 | # inputs do not exist yet, temporary placeholders are created and stored |
| 347 | # in this `tmp_cyclic_ts` container. During a second pass, |
| 348 | # those temporary tensors are replaced by the proper transformed tensors |
| 349 | # (see the function `_finalize_cycles`). |
| 350 | self.tmp_cyclic_ts = [] |
| 351 | |
| 352 | def new_name(self, name): |
| 353 | """Compute a destination name from a source name. |
| 354 | |
| 355 | Args: |
| 356 | name: the name to be "transformed". |
| 357 | Returns: |
| 358 | The transformed name. |
| 359 | Raises: |
| 360 | ValueError: if the source scope is used (that is, not an empty string) |
| 361 | and the source name does not belong to the source scope. |
| 362 | """ |
| 363 | scope = self.scope |
| 364 | if not name.startswith(scope): |
| 365 | raise ValueError("{} does not belong to source scope: {}.".format( |
| 366 | name, scope)) |
| 367 | rel_name = name[len(scope):] |
| 368 | name_ = self.scope_ + rel_name |
| 369 | return name_ |
| 370 | |
| 371 | |
| 372 | class Transformer(object): |