apex icon indicating copy to clipboard operation
apex copied to clipboard

scaled_loss.backward Segmentation fault (core dumped)

Open yangchengtest opened this issue 6 years ago • 14 comments

CUDA:10.0.130 PYTORCH 1.3.0 MODEL:RESTNET18+PPM(semantic segmentation) Only use 1 gpu.

the print: scale loss start Segmentation fault (core dumped)

only return Segmentation fault (core dumped) How to find the errors?

My code: train: loss, acc = segmentation_module(batch_data) loss = loss.mean() acc = acc.mean() print ("scale loss start") with amp.scale_loss(loss, optimizers) as scaled_loss: scaled_loss.backward()

segmentation_module forward: device = torch.device('cuda:0') data = feed_dict[0]['img_data'].cuda(device) encoder_result = self.encoder(data, return_feature_maps=True) (pred, pred_deepsup) = self.decoder(encoder_result) seg_data = feed_dict[0]['seg_label'].cuda(device) loss = self.crit(pred, seg_data)

yangchengtest avatar Mar 17 '20 02:03 yangchengtest

@yangchengtest Did you forget to initialize the model?

Lornatang avatar Mar 17 '20 11:03 Lornatang

@Lornatang here is my initial code: segmentation_module.cuda(device = torch.device('cuda:{}'.format(gpus[0]))) # load nets into gpu new_model, optimizers = amp.initialize(segmentation_module, list(optimizers), opt_level="O1") optimizers = tuple(optimizers) segmentation_module = new_model then run the train code.

yangchengtest avatar Mar 18 '20 01:03 yangchengtest

@yangchengtest Do you seem to be initializing multiple optimizers at the same time?

Lornatang avatar Mar 18 '20 01:03 Lornatang

@Lornatang amp.initialize(segmentation_module, list(optimizers), opt_level="O1") init the optimizers at the same time. Here is the print: Selected optimization level O1: Insert automatic casts around Pytorch functions and Tensor methods.

Defaults for this optimization level are: enabled : True opt_level : O1 cast_model_type : None patch_torch_functions : True keep_batchnorm_fp32 : None master_weights : None loss_scale : dynamic Processing user overrides (additional kwargs that are not None)... After processing overrides, optimization options are: enabled : True opt_level : O1 cast_model_type : None patch_torch_functions : True keep_batchnorm_fp32 : None master_weights : None loss_scale : dynamic gpus: 0 0 1 scale loss start

yangchengtest avatar Mar 18 '20 01:03 yangchengtest

@yangchengtest Try the following code.


# init all model and optimizer
amp.initialize(model_1, optimizer_1, opt_level="O1")
amp.initialize(model_2, optimizer_2, opt_level="O1")
amp.initialize(model_3, optimizer_3, opt_level="O1")

# backward all losses
with amp.scale_loss(loss_1, optimizer_1) as scaled_loss_1:
    scaled_loss_1.backward()
with amp.scale_loss(loss_2, optimizer_2) as scaled_loss_2:
    scaled_loss_2.backward()
with amp.scale_loss(loss_3, optimizer_3) as scaled_loss_3:
    scaled_loss_3.backward()

Lornatang avatar Mar 18 '20 01:03 Lornatang

@Lornatang CV semantic segmentation 2 models combined,only one loss... I will try to use only one optimizer. My forward func: self.encoder here is one model. self.decoder here is another model. Can nn.module contains nn.modules?

yangchengtest avatar Mar 18 '20 01:03 yangchengtest

@yangchengtest gcc version ?

Lornatang avatar Mar 18 '20 01:03 Lornatang

@Lornatang gcc version 4.8.5 20150623 (Red Hat 4.8.5-39)

yangchengtest avatar Mar 18 '20 01:03 yangchengtest

@yangchengtest It is recommended that you remove apex completely. Then clone again. Run the following command.

$ pip3 install -v --no-cache-dir --global-option="--pyprof" --global-option="--cpp_ext" --global-option="--cuda_ext" ./

Lornatang avatar Mar 18 '20 01:03 Lornatang

@Lornatang reinstalled. The same error. When I compile apex. There is a warning: /usr/local/lib/python3.6/site-packages/torch/include/c10/core/TensorTypeSet.h(44): warning: integer conversion resulted in a change of sign

yangchengtest avatar Mar 18 '20 02:03 yangchengtest

@yangchengtest Try

python3 setup.py install --cuda_ext --cpp_ext

Lornatang avatar Mar 18 '20 02:03 Lornatang

@Lornatang The same error. here is a warning: Your compiler (g++ 4.8.5) may be ABI-incompatible with PyTorch! Please use a compiler that is ABI-compatible with GCC 4.9 and above. See https://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html.

See https://gist.github.com/goldsborough/d466f43e8ffc948ff92de7486c5216d6 for instructions on how to install GCC 4.9 or higher.

Did I need to update GCC version?

yangchengtest avatar Mar 18 '20 08:03 yangchengtest

@yangchengtest Yes.

gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Lornatang avatar Mar 18 '20 09:03 Lornatang

Thanks. Update the gcc version to 7.5.0 fix the problom.Thanks!~

yangchengtest avatar Mar 24 '20 05:03 yangchengtest