The goal of my custom function is to calculate Hadamard(pointwise) product of input and weights, while the weights are parameters to be trained.
The scripts above can also be simplified as follows, I did not figure out my implementation errors.
    @staticmethod
	def forward(ctx, input, weight, bias=None):
		ctx.save_for_backward(input, weight, bias)

		output = torch.mul(input, weight)
		if bias is not None:
			output += bias

		return output
	@staticmethod	
	def backward(ctx, grad_output):
		input, weight, bias = ctx.saved_variables

		grad_input = grad_weight = grad_bias = None
		if ctx.needs_input_grad[0]:
			grad_input = torch.mul(grad_output, weight)
		if ctx.needs_input_grad[1]:
			grad_weight = torch.sum(grad_output*weight, 0)
		if bias is not None and ctx.needs_input_grad[2]:
			grad_bias = torch.sum(grad_output,0)

		if bias is not None:
			return grad_input, grad_weight, grad_bias
		else:
			return grad_input, grad_weight