def _apply(self): shape = self.op.attr("shape") dtype = self.op.attr("dtype") value = self.op.attr("value") print("shape: ", shape) print("dtype: ", dtype) print("value: ", value) tensor = self._create_ge_tensor(shape, dtype, value) const = core.GEOperatorFactory.create_operator( "const" + self._accumulated_op_id(), "Const").set_attr_tensor( "value", tensor) self._mark_as_input(const) if self.op.block.var(self.op.output('Out')[0]).persistable: print("%s fill_constant" % (self.op.output('Out')[0])) var = core.GEOperatorFactory.create_operator( self.op.output('Out')[0], "Variable") var.update_output_desc("y", core.GETensorDesc( core.GEShape(shape), core.GEFormat.FORMAT_ND, core.GEDataType.DT_FLOAT)) assign = core.GEOperatorFactory.create_operator( "assign" + self._accumulated_op_id(), "Assign").set_input( "value", const).set_input("ref", var) return [const], [[0]] else: print( "self.op.output('Out')[0] is not persistable in fill_constant") return [const], [[0]]
def _create_ge_tensor(self, shape, dtype, value): tensor_desc = core.GETensorDesc( core.GEShape(shape), core.GEFormat.FORMAT_ND, self.ascend_helper.dtype2ge(dtype)) tensor = core.GETensor(tensor_desc) data = (value * np.ones(( shape))).reshape(shape).astype(self.ascend_helper.dtype2np(dtype)) buf = data.tobytes() data_8 = np.frombuffer(buf, dtype=np.uint8) tensor.set_data(data_8) return tensor
def _construct_input_map(self, input_varlist): ret_map = {} ge_in_operator = [] for id, var in enumerate(input_varlist): if var.is_data: # input data ge_input = core.GEOperatorFactory.create_operator( var.name, "Data").set_attr_int32("index", id) ret_map[var.name] = ge_input ge_in_operator.append(ge_input) else: # param, learning ... ge_input = core.GEOperatorFactory.create_operator(var.name, "Variable") ge_input.update_output_desc("y", core.GETensorDesc( core.GEShape(var.shape), core.GEFormat.FORMAT_ND, core.GEDataType.DT_FLOAT)) ret_map[var.name] = ge_input return ge_in_operator, ret_map
def _apply(self): shape = self.op.attr("shape") dtype = self.op.attr("dtype") mean = self.op.attr("mean") std = self.op.attr("std") seed = self.op.attr("seed") tensor1 = self._create_ge_tensor([len(shape)], 2, shape) shape_tensor = core.GEOperatorFactory.create_operator( "const" + self._accumulated_op_id(), "Const").set_attr_tensor( "value", tensor1) tensor2 = self._create_ge_tensor([1], dtype, mean) mean_tensor = core.GEOperatorFactory.create_operator( "const" + self._accumulated_op_id(), "Const").set_attr_tensor( "value", tensor2) tensor3 = self._create_ge_tensor([1], dtype, std) std_tensor = core.GEOperatorFactory.create_operator( "const" + self._accumulated_op_id(), "Const").set_attr_tensor( "value", tensor3) tensor4 = self._create_ge_tensor([1], dtype, mean - 2 * std) min_tensor = core.GEOperatorFactory.create_operator( "const" + self._accumulated_op_id(), "Const").set_attr_tensor( "value", tensor4) tensor5 = self._create_ge_tensor([1], dtype, mean + 2 * std) max_tensor = core.GEOperatorFactory.create_operator( "const" + self._accumulated_op_id(), "Const").set_attr_tensor( "value", tensor5) self._mark_as_input(shape_tensor) self._mark_as_input(mean_tensor) self._mark_as_input(std_tensor) self._mark_as_input(min_tensor) self._mark_as_input(max_tensor) truncated_normal = core.GEOperatorFactory.create_operator( "truncated_normal" + self._accumulated_op_id(), "ParameterizedTruncatedNormal").set_input( "shape", shape_tensor).set_input( "means", mean_tensor).set_input( "stdevs", std_tensor).set_input( "min", min_tensor).set_input( "max", max_tensor).set_attr_int32("seed", 0) ## wirte the output of truncatedNormal from startup_program to main_program if self.op.block.var(self.op.output('Out')[0]).persistable: print("%s is Persistable in truncated_normal" % (self.op.output('Out')[0])) #var = core.GEOperatorFactory.create_operator(self.op.output('Out')[0], "Variable").set_input("x", truncated_normal) var = core.GEOperatorFactory.create_operator( self.op.output('Out')[0], "Variable") var.update_output_desc("y", core.GETensorDesc( core.GEShape(shape), core.GEFormat.FORMAT_ND, core.GEDataType.DT_FLOAT)) assign = core.GEOperatorFactory.create_operator( "assign" + self._accumulated_op_id(), "Assign").set_input( "value", truncated_normal).set_input("ref", var) return [ shape_tensor, mean_tensor, std_tensor, min_tensor, max_tensor, truncated_normal ], [[-1]] else: print( "self.op.output('Out')[0] is not persistable in truncated_noraml" ) return [truncated_normal], [[0]] #[assign]