def CreateGradientForOp(cls, forward_op, g_output): """Generate the OperatorDef for ``BackwardOp`` by ``ForwardOp``. Parameters ---------- forward_op : dragon_pb2.OperatorDef The OperatorDef of ``ForwardOp``. g_output : list of str The inputs of ``BackwardOp`` (Precomputed Grads). Returns ------- tuple The OpDef, outputs and defaults of ``BackwardOp``. References ---------- The wrapper of ``CreateGradientDefsCC``. """ g_ops, g_inputs, defaults = \ CreateGradientDefsCC(forward_op.SerializeToString(), g_output) for idx, g_op in enumerate(g_ops): new_def = pb.OperatorDef() new_def.ParseFromString(g_op) _, new_def.name = GetOperatorName() g_ops[idx] = new_def return g_ops, g_inputs, defaults
def MakeOperatorDef(op_type, inputs, outputs, name='', device_option=None, arg=None, engine=None, **kwargs): operator = pb.OperatorDef() operator.type = op_type operator.name = name operator.input.extend([str(tensor) for tensor in inputs]) operator.output.extend([str(tensor) for tensor in outputs]) if device_option is not None: operator.device_option.CopyFrom(device_option) if engine is not None: operator.engine = engine if 'random_seed' in kwargs: operator.device_option.random_seed = kwargs['random_seed'] del kwargs['random_seed'] if arg is not None: operator.arg.extend(arg) for k, v in kwargs.items(): if v is None: continue operator.arg.add().CopyFrom(MakeArgument(k, v)) return operator
def MutableOperatorDef(meta_def, inputs, outputs): op = pb.OperatorDef() op.CopyFrom(meta_def) op.ClearField('input') op.input.extend(inputs) op.ClearField('output') op.output.extend(outputs) return op
def CreateGradientForOp(cls, op_def, g_output): """ parse ops from string """ g_ops, g_inputs, defaults = CreateGradientDefsCC( op_def.SerializeToString(), g_output) for idx, g_op in enumerate(g_ops): new_def = pb.OperatorDef() new_def.ParseFromString(g_op) _, new_def.name = GetOperatorName() g_ops[idx] = new_def return g_ops, g_inputs, defaults
def CreateGradientForOp(cls, op_def, g_output): """ parse ops from string """ g_ops, g_inputs, defaults = CreateGradientDefsCC( op_def.SerializeToString(), g_output) if sys.version_info >= (3, 0): g_inputs = [g_input.decode('ascii') for g_input in g_inputs] for idx, g_op in enumerate(g_ops): new_def = pb.OperatorDef() new_def.ParseFromString(g_op) _, new_def.name = GetOperatorName() g_ops[idx] = new_def return g_ops, g_inputs, defaults