Global structure pruning in pytorch

yichao_Liu: <p>Hi, I have a question about structure pruning. I used pytorch structure pruning for global pruning. I could find 0 in weight_mask, before prune.remove(module, ‘weight’)<br>
My code is like this:<br>
`<br>
for name, module in model.model.named_modules():</p>
<pre><code>if isinstance(module, nn.Conv2d):
    if 0 in module.weight_mask:
        print(module.weight)
prune.remove(module, 'weight')
</code></pre>
<p>`<br>
print(module.weight) can show 0 weights, but after prune.remove, it still shows that the weight as original. Do you know what problem is there? Thank you so much</p>

Soumya_Kundu: <aside class="onebox githubgist" data-onebox-src="https://gist.github.com/aymuos15/10c8348148d3de5954a6b4cc5cfebf2c">
  <header class="source">

      <a href="https://gist.github.com/aymuos15/10c8348148d3de5954a6b4cc5cfebf2c" target="_blank" rel="noopener nofollow ugc">gist.github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://gist.github.com/aymuos15/10c8348148d3de5954a6b4cc5cfebf2c" target="_blank" rel="noopener nofollow ugc">https://gist.github.com/aymuos15/10c8348148d3de5954a6b4cc5cfebf2c</a></h4>

  <h5>Pruning_nuances_in_pytorch.py</h5>
  <pre><code class="Python">import torch
import torch.nn as nn
import torch.nn.utils.prune as prune
import numpy as np
import matplotlib.pyplot as plt

class SimpleCNN(nn.Module):
    def __init__(self, conv1_out=32, conv2_out=64):
        super(SimpleCNN, self).__init__()
        self.conv1 = nn.Conv2d(1, conv1_out, 3, 1)</code></pre>
   This file has been truncated. <a href="https://gist.github.com/aymuos15/10c8348148d3de5954a6b4cc5cfebf2c" target="_blank" rel="noopener nofollow ugc">show original</a>

<p>
</p>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>This gist should answer this question!</p>

yichao_Liu: <aside class="quote no-group" data-username="Soumya_Kundu" data-post="2" data-topic="209941">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/soumya_kundu/48/71716_2.png" class="avatar"> Soumya_Kundu:</div>
<blockquote>
<p><code>class SimpleCNN</code></p>
</blockquote>
</aside>
<p>Thank you. yes it doesn’t remove the 0 parameters, so I think in order to do it, a new model have  to be created, but in yolov8n model, there’s no architecture code.</p>

Soumya_Kundu: <aside class="onebox githubblob" data-onebox-src="https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/yolo/model.py">
  <header class="source">

      <a href="https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/yolo/model.py" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/yolo/model.py" target="_blank" rel="noopener nofollow ugc">ultralytics/ultralytics/blob/main/ultralytics/models/yolo/model.py</a></h4>


      <pre><code class="lang-py"># Ultralytics YOLO 🚀, AGPL-3.0 license

from pathlib import Path

from ultralytics.engine.model import Model
from ultralytics.models import yolo
from ultralytics.nn.tasks import ClassificationModel, DetectionModel, OBBModel, PoseModel, SegmentationModel, WorldModel
from ultralytics.utils import ROOT, yaml_load


class YOLO(Model):
    """YOLO (You Only Look Once) object detection model."""

    def __init__(self, model="yolov8n.pt", task=None, verbose=False):
        """Initialize YOLO model, switching to YOLOWorld if model filename contains '-world'."""
        path = Path(model)
        if "-world" in path.stem and path.suffix in {".pt", ".yaml", ".yml"}:  # if YOLOWorld PyTorch model
            new_instance = YOLOWorld(path, verbose=verbose)
            self.__class__ = type(new_instance)
            self.__dict__ = new_instance.__dict__
</code></pre>



  This file has been truncated. <a href="https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/yolo/model.py" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>Is this not what you’re looking for?</p>

yichao_Liu: <p>Thank you so much. I think the whole structure is written by yaml file. What I want is a python code architecture. This allows me to do some changes like pruning, but yaml file doesn’t allow me to do changes.</p>

Soumya_Kundu: <p>Sorry, should’ve made myself clear (and the late reply!) → You should look into the <a href="https://github.com/ultralytics/ultralytics/blob/main/ultralytics/engine/trainer.py#L58" rel="noopener nofollow ugc">trainer.py</a></p>
<p>This is what is inherited overall.</p>