Closed guanh01 closed 4 years ago
The Tensorflow script seems working on Linux Environment: python: Python 3.6.4 |Anaconda custom (64-bit)| (default, Jan 16 2018, 18:10:19) tensorflow: 1.7.8 (CPU) scalene: installed by pip System: Ubuntu 18.04 (Memory profiling not enabled)
PyTorch script fails though
Fatal Python error: GC object already tracked
Thread 0x00007f6d7e613700 (most recent call first):
Thread 0x00007f6d7ee14700 (most recent call first):
Current thread 0x00007f6ea0975240 (most recent call first):
File "test.py", line 31 in forward
File "/usr/local/share/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/torch/nn/modules/module.py", line 550 in __call__
File "test.py", line 55 in <module>
File "/usr/local/share/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/scalene/scalene.py", line 1394 in main
File "/usr/local/share/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/scalene/__main__.py", line 4 in main
File "/usr/local/share/anaconda3/envs/Tensorflow/lib/python3.6/site-packages/scalene/__main__.py", line 7 in <module>
File "/usr/local/share/anaconda3/envs/Tensorflow/lib/python3.6/runpy.py", line 85 in _run_code
File "/usr/local/share/anaconda3/envs/Tensorflow/lib/python3.6/runpy.py", line 193 in _run_module_as_main
Thanks for the report, @guanh01 ! :)
I tracked down the issue(s), and have successfully run both programs on Mac OS and Linux. You can get the latest version via pip install -U scalene
. (A note to you and @GammaPi - by default, scalene now runs with memory profiling -- you have to explicitly disable it with --cpu-only
. This is faster but of course does not give you memory usage info.)
The issues were as follows:
free
calls. Unfortunately, this may leak memory (possibly avoidably).
Just for curiosity, I tried to profile a tensorflow and a pytorch script using scalene but got segmentation faults for both scripts. The python scripts come from tensorflow and pytorch tutorial.
Environment:
python: 3.7 tensorflow: 2.2 pytorch: 1.5 scalene: installed using homebrew System: MacOS Catalina version 10.15.5
Below are the details to reproduce the error:
tensorflow
(x_train, y_train), (x_test, y_test) = mnist.load_data() x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(input_shape=(28, 28)), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(10) ])
predictions = model(x_train[:1]).numpy() print("predictions", predictions)
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) model.compile(optimizer='adam', loss=loss_fn, metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5) model.evaluate(x_test, y_test, verbose=2)
Similar for Pytorch script
class DynamicNet(torch.nn.Module): def init(self, D_in, H, D_out): """ In the constructor we construct three nn.Linear instances that we will use in the forward pass. """ super(DynamicNet, self).init() self.input_linear = torch.nn.Linear(D_in, H) self.middle_linear = torch.nn.Linear(H, H) self.output_linear = torch.nn.Linear(H, D_out)
N is batch size; D_in is input dimension;
H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1000, 100, 10
Create random Tensors to hold inputs and outputs
x = torch.randn(N, D_in) y = torch.randn(N, D_out)
Construct our model by instantiating the class defined above
model = DynamicNet(D_in, H, D_out)
Construct our loss function and an Optimizer. Training this strange model with
vanilla stochastic gradient descent is tough, so we use momentum
criterion = torch.nn.MSELoss(reduction='sum') optimizer = torch.optim.SGD(model.parameters(), lr=1e-4, momentum=0.9) for t in range(500):
Forward pass: Compute predicted y by passing x to the model
(base) ➜ scalene git:(master) ✗ python ./test/torch-dynamic-model.py 99 38.210121154785156 199 0.7706254720687866 299 2.6024699211120605 399 0.5532416701316833 499 0.3656597137451172
(base) ➜ scalene git:(master) ✗ scalene test/torch-dynamic-model.py /usr/local/bin/scalene: line 3: 23709 Segmentation fault: 11 DYLD_INSERT_LIBRARIES=/usr/local/Cellar/libscalene/HEAD-a49f5ca/lib/libscalene.dylib PYTHONMALLOC=malloc python3 -m scalene "$@"