| 4694 | del self.vars[name] |
| 4695 | |
| 4696 | def create_parameter(self, *args, **kwargs): |
| 4697 | global_block = self.program.global_block() |
| 4698 | param = None |
| 4699 | if in_dygraph_mode(): |
| 4700 | param = EagerParamBase(*args, **kwargs) |
| 4701 | else: |
| 4702 | param = Parameter(global_block, *args, **kwargs) |
| 4703 | # NOTE(Aurelius84): we deliver stop_gradient in append_op, so we |
| 4704 | # need record it state and reset it back after calling this API |
| 4705 | stop_gradient = param.stop_gradient |
| 4706 | |
| 4707 | if "initializer" in kwargs: |
| 4708 | |
| 4709 | def _is_inited_by(block, var): |
| 4710 | init_ops = [] |
| 4711 | for op in block.ops: |
| 4712 | if var.name in op.output_arg_names: |
| 4713 | # In startup_program, "broadcast" and "c_sync_comm_stream" |
| 4714 | # are treated as initialization ops that cause error. |
| 4715 | # Think of "broadcast" and "c_sync_comm_stream" as a special case here. |
| 4716 | # NOTE: "coalesce_tensor" is a special case for rnn with cudnn support |
| 4717 | if op.type in [ |
| 4718 | "broadcast", |
| 4719 | "c_sync_comm_stream", |
| 4720 | "coalesce_tensor", |
| 4721 | ]: |
| 4722 | continue |
| 4723 | init_ops.append(op) |
| 4724 | return init_ops |
| 4725 | |
| 4726 | initializer = kwargs["initializer"] |
| 4727 | init_ops = _is_inited_by(global_block, param) |
| 4728 | init_ops_len = len(init_ops) |
| 4729 | if init_ops_len > 1: |
| 4730 | raise RuntimeError( |
| 4731 | "param " |
| 4732 | + param.name |
| 4733 | + " is inited by multiple init ops " |
| 4734 | + str(init_ops) |
| 4735 | ) |
| 4736 | elif init_ops_len == 1: |
| 4737 | # TODO already inited, do nothing, should log a warning |
| 4738 | pass |
| 4739 | else: |
| 4740 | initializer(param, self) |
| 4741 | param.stop_gradient = stop_gradient |
| 4742 | return param |
| 4743 | |
| 4744 | def append_op(self, *args, **kwargs): |
| 4745 | """ |