I think you are confusing your self, kernels in conv2d are already randomly defined for you.
You can check this by doing this:
X = nn.Conv2D( 1, 1, 3, 1, 1) #  ( input_c, output_c, k_size, stride, padding ), k_size can be (3,3) or 3 
X.weight # a single 3 x 3 kernel, if you want to output more kernels you can try and change "output_c" to see what happens  

Parameter containing:
tensor([[[[-0.2303, -0.0186, -0.2070],
          [ 0.3190, -0.2940, -0.1227],
          [ 0.1014,  0.0417, -0.3254]]]])

I am guessing you want to use predefined weights as values for the kernel in conv2D correct?
If so you can do the following.
X = torch.Tensor([[1 ,0, -1],[2, 0 ,-2], [1, 0 ,-1]])
X = torch.nn.Parameter( X ) # calling this turns tensor into "weight" parameter
print( "X's size", X.size() )
print("X's tensor values as weight: ", X)
Z = nn.Conv2d( 1, 1, 3, 1, 1) # creates a test convolution 
print("Z's pre-initalized weight from Conv2D: ", Z.weight, "its size is: ", Z.weight.size())
Z.weight = X # simply copying over the new weight 
print("Z's new weight ", Z.weight)


X’s size torch.Size([3, 3])
X’s tensor values as weight:  Parameter containing:
tensor([
[ 1.,  0., -1.],
[ 2.,  0., -2.],
[ 1.,  0., -1.]])
Z’s pre-initalized weight from Conv2D:  Parameter containing:
tensor([[[
[-0.1911,  0.0187,  0.0254],
[-0.0727, -0.1672,  0.0862],
[ 0.2888,  0.2479,  0.3203]]]])
Its size is:  torch.Size([1, 1, 3, 3])
Z’s new weight  Parameter containing:
tensor([
[ 1.,  0., -1.],
[ 2.,  0., -2.],
[ 1.,  0., -1.]])

Edited Finally if you want to make sure the output size is correct the above 2 post has the answers. You can also google the output convolution formula and the documentations for Conv2D also has it. Good Luck!