I've seen similar tickets from 2018 on this topic, but I assume they've been fixed since. Getting a negative memory increment when running some code. Here is the code that I am running:
from torch import Tensor, linspace, meshgrid, zeros, square, div, sum, norm, sqrt, nan_to_num, nonzero, device, zeros_like, atan, cat, empty, subtract
import argparse
from torch.cuda import is_available
from torch.nn.functional import normalize
from scipy.constants import pi, epsilon_0
from math import ceil
from jaxtyping import Shaped
from matplotlib.pyplot import figure, show
from numpy import abs, min
import memory_profiler
#################################
# #
# Setup #
# #
#################################
# Checking for cuda-capable device (for gpu assistance)
device = device("cuda:0" if is_available() else "cpu")
# Parsing arguments
parser = argparse.ArgumentParser()
parser.add_argument('--charge', type=str, help='The name of the person.')
parser.add_argument('--side-length', type=float, help='The y-coordinate of observation.')
parser.add_argument('--x', type=float, help='The x-coordinate of observation.')
parser.add_argument('--y', type=float, help='The y-coordinate of observation.')
parser.add_argument('--z', type=float, help='The z-coordinate of observation.')
parser.add_argument('--n', type=float, help='The number of points.')
args = parser.parse_args() # get arguments
# set arguments to variables to save space
@profile
def main():
q = float(args.charge)
L = float(args.side_length)
x = float(args.x)
y = float(args.y)
z = float(args.z)
N = int(args.n)
sigma = q / L**2 # useful later
k= (q/N**2) / (4.0 * pi * epsilon_0)
_k = sigma / (pi*epsilon_0)
Ls = (L/2)**2
#### Preallocating tensors
# creating N number of points along z axis from 0 to z
z_coords = linspace(0, z, N, device=device)
_p = zeros(N, 1, 1, 3, device=device)
result = empty(N, 3)
estimated = empty(N,3)
# setting up tensor representing the square on the xy plane centered at the origin
# think of it like a cube of size 1*N*N where each point in that
# cube holds 3 values
# shape is [1,N,N,3]
t = zeros(1,N,N,3, device=device)
# Creating one-dim tensors of size N whose values are evenly spaced
# between -L/2 and L/2 for both x and y coordinates of charges
# throughout a square plane centered at the origin
x_coords = linspace(-L/2.0, L/2.0, N) if N > 1 else Tensor([0.0])
y_coords = linspace(-L/2.0, L/2.0, N) if N > 1 else Tensor([0.0])
# putting those coordinates into a 2D grid
_x, _y = meshgrid(x_coords, y_coords)
# putting the grid in for the x and y components along the last
# dimension of our tensor that represents the square
t[:,:,:,0] = _x
t[:,:,:,1] = _y
# point of observation
# shape is [1,1,1,3]
p = Tensor([[[[x,y,z]]]], device=device)
# print to console
point = p[0][0][0]
p = p.subtract(t)
# basic efield equation
# 1 q
# E = ------ * ---- *r_hat
# 4pi*e0 r^2
print("E-Field at ", point," is: ", nan_to_num(sum((div(k, square(p)))*normalize(p,dim=3), dim=(1,2))))
#################################
# #
# Finding Percent Error #
# #
#################################
# reshaping and adding to tensor of correct shape
z_coords = z_coords.view(N, 1, 1)
_p[:,:,:,2] = z_coords
# finding estimated at the points along the z axis
estimated = nan_to_num(sum((div(k, square(_p-t)))*normalize(_p-t,dim=3), dim=(1,2)))
# finding actual
#
# sigma / (L/2)^2 \
# E = ------- * tan^-1 ( -------------------------- )
# pi*e0 \ z* sqrt(z^2 + 2*(L/2)^2) /
#
e = (_k) * atan(div(Ls,_p * sqrt(square(_p) + 2*Ls))) * normalize(_p, dim=3)
actual = nan_to_num(e.squeeze(1).squeeze(1))
#################################
# #
# Printing to console #
# #
#################################
z_coords = z_coords.squeeze(-1).squeeze(-1)
a = actual[:, 2].squeeze(-1).cpu().numpy()
e = estimated[:, 2].squeeze(-1).cpu().numpy()
print("Plotting....")
#
# Plotting
#
fig = figure()
ax1 = fig.add_subplot(1,2,1)
ax1.plot(z_coords.cpu().numpy(), a, label="Actual")
ax1.plot(z_coords.cpu().numpy(), e, label="Estimate")
ax2= fig.add_subplot(1,2,2)
ax2.plot(z_coords.cpu().numpy(), abs(e-a)/a, label="Percent Error")
show()
main()
I've seen similar tickets from 2018 on this topic, but I assume they've been fixed since. Getting a negative memory increment when running some code. Here is the code that I am running:
This is how I am running it:
This is the output I get:
Line 102 has a memory increment of -223.578 MiB.
Here is an output of running
uname -a
on the M1 pro system: