def _generate_snippet_template(**kwargs): """Generate snippet template.""" template, exchange_msg, outputs_list, outputs_mapping = ONNXToMindSporeMapper._generate_snippet_template( **kwargs) op = kwargs.get("operation") args = kwargs.get("converted_params", dict()) weights = kwargs.get("weights") params = kwargs["raw_params"] ipt_shape = params["input_shape"] starts = SliceMapper._find_val_by_index(0, weights) ends = SliceMapper._find_val_by_index(1, weights) axes = SliceMapper._find_val_by_index(2, weights, np.array(list(range(len(ipt_shape))))) steps = SliceMapper._find_val_by_index(3, weights, np.array([1 for _ in range(len(ipt_shape))])) if not op: raise ValueError("Can not get MindSpore operation name.") if not weights: raise ValueError("Can not get required params from slice.") if axes.shape != (1,): ordered_begin = sorted(zip(starts.tolist(), axes.tolist()), key=lambda x: x[1], reverse=False) ordered_end = sorted(zip(ends.tolist(), axes.tolist()), key=lambda x: x[1], reverse=False) ordered_strides = sorted(zip(steps.tolist(), axes.tolist()), key=lambda x: x[1], reverse=False) begin = [i[0] for i in ordered_begin] end = [min(i[0], ipt_shape[i[1]]) for i in ordered_end] strides = [i[0] for i in ordered_strides] else: axis = axes.tolist()[0] begin = [0 for _ in range(len(ipt_shape))] end = list(ipt_shape) strides = [1 for _ in range(len(ipt_shape))] begin[axis] = starts.tolist()[0] end[axis] = min(ends.tolist()[0], end[axis]) strides[axis] = steps.tolist()[0] args['begin'] = tuple(begin) args['end'] = tuple(end) args['strides'] = tuple(strides) variable_slot = "var_0" init_template = f"self.{{{variable_slot}}} = {op}()" init_begin = f"self.{{{variable_slot}}}_begin = {{begin}}" init_end = f"self.{{{variable_slot}}}_end = {{end}}" init_strides = f"self.{{{variable_slot}}}_strides = {{strides}}" construct_template = f"opt_{{{variable_slot}}} = self.{{{variable_slot}}}" \ f"({{{ExchangeMessageKeywords.VariableScope.value.INPUTS.value}}}, " \ f"self.{{{variable_slot}}}_begin, self.{{{variable_slot}}}_end, " \ f"self.{{{variable_slot}}}_strides)" template = reset_init_or_construct(template, variable_slot, [init_template, init_begin, init_end, init_strides], TemplateKeywords.INIT.value) template = reset_init_or_construct(template, variable_slot, [construct_template], TemplateKeywords.CONSTRUCT.value) return template, exchange_msg, outputs_list, outputs_mapping
def _generate_snippet_template_with_weights(weights, args, template, op, trainable_params): """Generate template when weights exist.""" tensor = MulMapper._find_val_by_index(0, weights) w_shape = tensor.shape w_location = MulMapper._find_location_by_index(0, weights) variable_slot = "var_0" inputs_in_construct = [ f"{{{ExchangeMessageKeywords.VariableScope.value.INPUTS.value}}}" ] if w_location != -1: inputs_in_construct.insert(w_location, f"self.{{{variable_slot}}}_w") if w_shape: # Note: adding weight shape to args is now deprecated due to conflict of partial weights share processing. variable_slot_param_name = f"{variable_slot}/w" init_tensor = f"self.{{{variable_slot}}}_w = {{{variable_slot_param_name}}}" else: args["w_value"] = tensor.tolist() init_tensor = f"self.{{{variable_slot}}}_w = {{w_value}}" construct_template = f"opt_{{{variable_slot}}} = {' * '.join(inputs_in_construct)}" template = reset_init_or_construct(template, variable_slot, [init_tensor], TemplateKeywords.INIT.value) template = reset_init_or_construct(template, variable_slot, [construct_template], TemplateKeywords.CONSTRUCT.value) exchange_msg = { variable_slot: { ExchangeMessageKeywords.VariableScope.value.OPERATION.value: op, ExchangeMessageKeywords.VariableScope.value.VARIABLE_NAME.value: None, ExchangeMessageKeywords.VariableScope.value.OUTPUT_TYPE.value: ExchangeMessageKeywords.VariableScope.value.TSR_TYPE.value, ExchangeMessageKeywords.VariableScope.value.INPUTS.value: [], ExchangeMessageKeywords.VariableScope.value.ARGS.value: args, ExchangeMessageKeywords.VariableScope.value.WEIGHTS.value: weights, ExchangeMessageKeywords.VariableScope.value.TRAINABLE_PARAMS.value: trainable_params } } if w_shape: exchange_msg[variable_slot][ExchangeMessageKeywords.VariableScope. value.PARAMETERS_DECLARED.value] = { "w": "" } outputs_list = [f"opt_{{{variable_slot}}}"] outputs_mapping = ((0, 0), ) return template, exchange_msg, outputs_list, outputs_mapping
def _generate_snippet_template_with_weights(weights, args, template, op, trainable_params): """Generate template when weights exist.""" tensor = DivMapper._find_val_by_index(0, weights) w_shape = tensor.shape w_dtype = tensor.dtype w_location = DivMapper._find_location_by_index(0, weights) variable_slot = "var_0" inputs_in_construct = [ f"{{{ExchangeMessageKeywords.VariableScope.value.INPUTS.value}}}" ] if w_location != -1: inputs_in_construct.insert(w_location, f"self.{{{variable_slot}}}_w") if w_shape: args["w_shape"] = w_shape args["w_dtype"] = w_dtype init_tensor = f"self.{{{variable_slot}}}_w = " \ f"Parameter(Tensor(np.random.uniform(0, 1, {{w_shape}}).astype(np.{{w_dtype}})), " \ f"name=None)" else: args["w_value"] = tensor.tolist() init_tensor = f"self.{{{variable_slot}}}_w = {{w_value}}" construct_template = f"opt_{{{variable_slot}}} = {' / '.join(inputs_in_construct)}" template = reset_init_or_construct(template, variable_slot, [init_tensor], TemplateKeywords.INIT.value) template = reset_init_or_construct(template, variable_slot, [construct_template], TemplateKeywords.CONSTRUCT.value) exchange_msg = { variable_slot: { ExchangeMessageKeywords.VariableScope.value.OPERATION.value: op, ExchangeMessageKeywords.VariableScope.value.VARIABLE_NAME.value: None, ExchangeMessageKeywords.VariableScope.value.OUTPUT_TYPE.value: ExchangeMessageKeywords.VariableScope.value.TSR_TYPE.value, ExchangeMessageKeywords.VariableScope.value.INPUTS.value: [], ExchangeMessageKeywords.VariableScope.value.ARGS.value: args, ExchangeMessageKeywords.VariableScope.value.WEIGHTS.value: weights, ExchangeMessageKeywords.VariableScope.value.TRAINABLE_PARAMS.value: trainable_params } } outputs_list = [f"opt_{{{variable_slot}}}"] outputs_mapping = ((0, 0), ) return template, exchange_msg, outputs_list, outputs_mapping
def _generate_snippet_template(**kwargs): template, exchange_msg, outputs_list, outputs_mapping = ONNXToMindSporeMapper._generate_snippet_template( **kwargs) op = kwargs.get("operation") args = kwargs.get("converted_params") weights = kwargs.get("weights") trainable_params = kwargs.get("trainable_params", dict()) if not weights: return template, exchange_msg, outputs_list, outputs_mapping tensor = PowMapper._find_val_by_index(0, weights) input_shape = tensor.shape input_dtype = tensor.dtype input_location = PowMapper._find_location_by_index(0, weights) variable_slot = "var_0" init_template = f"self.{{{variable_slot}}} = {op}()" inputs_in_construct = [f"{{{ExchangeMessageKeywords.VariableScope.value.INPUTS.value}}}"] if input_location != -1: inputs_in_construct.insert(input_location, f"self.{{{variable_slot}}}_input_weight") if input_shape: args["input_shape"] = input_shape args["input_dtype"] = input_dtype init_tensor = f"self.{{{variable_slot}}}_input_weight = " \ f"Parameter(Tensor(np.random.uniform(0, 1, {{input_shape}}).astype(np.{{input_dtype}})), " \ f"name=None)" else: args["input_value"] = tensor.tolist() init_tensor = f"self.{{{variable_slot}}}_input_weight = {{input_value}}" construct_template = f"opt_{{{variable_slot}}} = self.{{{variable_slot}}}" \ f"({', '.join(inputs_in_construct)})" template = reset_init_or_construct(template, variable_slot, [init_template, init_tensor], TemplateKeywords.INIT.value) template = reset_init_or_construct(template, variable_slot, [construct_template], TemplateKeywords.CONSTRUCT.value) exchange_msg = { variable_slot: { ExchangeMessageKeywords.VariableScope.value.OPERATION.value: op, ExchangeMessageKeywords.VariableScope.value.VARIABLE_NAME.value: None, ExchangeMessageKeywords.VariableScope.value.OUTPUT_TYPE.value: ExchangeMessageKeywords.VariableScope.value.TSR_TYPE.value, ExchangeMessageKeywords.VariableScope.value.INPUTS.value: [], ExchangeMessageKeywords.VariableScope.value.ARGS.value: args, ExchangeMessageKeywords.VariableScope.value.WEIGHTS.value: weights, ExchangeMessageKeywords.VariableScope.value.TRAINABLE_PARAMS.value: trainable_params } } outputs_list = [f"opt_{{{variable_slot}}}"] outputs_mapping = ((0, 0),) return template, exchange_msg, outputs_list, outputs_mapping
def _generate_snippet_template(**kwargs): template, exchange_msg, outputs_list, outputs_mapping = ONNXToMindSporeMapper._generate_snippet_template( **kwargs) raw_params = kwargs.get('raw_params', dict()) trans_a = raw_params.get('transA', 0) if not trans_a: return template, exchange_msg, outputs_list, outputs_mapping op = kwargs.get("operation") args = kwargs.get("converted_params") weights = kwargs.get("weights") trainable_params = kwargs.get('trainable_params', dict()) if not op: raise ValueError("Can not get MindSpore operation name.") input_rank = len(raw_params['input_shape']) input_perm = [i for i in range(input_rank)] input_perm[-2], input_perm[-1] = input_perm[-1], input_perm[-2] variable_slot = "var_0" init_input_perm = f"self.{{{variable_slot}}}_input_perm = {{input_perm}}" init_template = f"self.{{{variable_slot}}} = {op}({', '.join(['%s={%s}' % (p, p) for p in args])})" inputs_in_construct = [f"{{{ExchangeMessageKeywords.VariableScope.value.INPUTS.value}}}", f"self.{{{variable_slot}}}_input_perm"] args['input_perm'] = tuple(input_perm) construct_transpose = f"opt_{{{variable_slot}}}_transpose = P.Transpose()({', '.join(inputs_in_construct)})" construct_template = f"opt_{{{variable_slot}}} = self.{{{variable_slot}}}(opt_{{{variable_slot}}}_transpose)" template = reset_init_or_construct(template, variable_slot, [init_input_perm, init_template], TemplateKeywords.INIT.value) template = reset_init_or_construct(template, variable_slot, [construct_transpose, construct_template], TemplateKeywords.CONSTRUCT.value) exchange_msg = { variable_slot: { ExchangeMessageKeywords.VariableScope.value.OPERATION.value: op, ExchangeMessageKeywords.VariableScope.value.VARIABLE_NAME.value: None, ExchangeMessageKeywords.VariableScope.value.OUTPUT_TYPE.value: ExchangeMessageKeywords.VariableScope.value.TSR_TYPE.value, ExchangeMessageKeywords.VariableScope.value.INPUTS.value: [], ExchangeMessageKeywords.VariableScope.value.ARGS.value: args, ExchangeMessageKeywords.VariableScope.value.WEIGHTS.value: weights, ExchangeMessageKeywords.VariableScope.value.TRAINABLE_PARAMS.value: trainable_params } } outputs_list = [f"opt_{{{variable_slot}}}"] outputs_mapping = ((0, 0),) return template, exchange_msg, outputs_list, outputs_mapping
def _generate_snippet_template(**kwargs): template, exchange_msg, outputs_list, outputs_mapping = ONNXToMindSporeMapper._generate_snippet_template( **kwargs) raw_params = kwargs.get("raw_params") perm = raw_params["perm"] variable_slot = "var_0" construct_template = f"opt_{{{variable_slot}}} = self.{{{variable_slot}}}" \ f"({{{ExchangeMessageKeywords.VariableScope.value.INPUTS.value}}}, {tuple(perm)})" template = reset_init_or_construct(template, variable_slot, [construct_template], TemplateKeywords.CONSTRUCT.value) return template, exchange_msg, outputs_list, outputs_mapping