MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / monkey_patch_variable

Function monkey_patch_variable

python/paddle/base/layers/math_op_patch.py:122–997  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

120
121
122def monkey_patch_variable():
123 def unique_tmp_name():
124 return default_main_program()._name_generator.generate("tmp")
125
126 def safe_get_dtype(var):
127 try:
128 dtype = var.dtype
129 except:
130 raise ValueError(f"Cannot get data type from {var.name}")
131 return dtype
132
133 def current_block(var):
134 return var.block.program.current_block()
135
136 def create_new_tmp_var(block, dtype):
137 tmp_name = unique_tmp_name()
138 return block.create_var(name=tmp_name, dtype=dtype)
139
140 def create_new_tmp_sparse_var(block, dtype, type):
141 tmp_name = unique_tmp_name()
142 return block.create_var(name=tmp_name, dtype=dtype, type=type)
143
144 def create_tensor(block, value, dtype, shape):
145 value = float(value)
146 var = create_new_tmp_var(block, dtype)
147 block.append_op(
148 type="fill_constant",
149 outputs={'Out': [var]},
150 attrs={
151 'dtype': var.dtype,
152 'shape': shape,
153 'value': value,
154 'force_cpu': False,
155 },
156 stop_gradient=True,
157 )
158 var.stop_gradient = True
159 return var
160
161 def create_scalar(block, value, dtype):
162 return create_tensor(block, value, dtype, shape=[])
163
164 def create_tensor_with_batchsize(ref_var, value, dtype):
165 assert isinstance(ref_var, Variable)
166 value = float(value)
167 block = current_block(ref_var)
168 var = create_new_tmp_var(block, dtype)
169 batch_dim = -1
170 out_shape = []
171 for i, d in enumerate(ref_var.shape):
172 if d < 0:
173 if batch_dim < 0:
174 batch_dim = i
175 out_shape.append(d)
176 else:
177 out_shape.append(1)
178 else:
179 out_shape.append(d)

Callers 2

__init__.pyFile · 0.85
__init__.pyFile · 0.85

Calls 1

_binary_creator_Function · 0.70

Tested by

no test coverage detected